Botan 3.9.0
Crypto and TLS for C&
gf2m_rootfind_dcmp.cpp
Go to the documentation of this file.
1/*
2 * (C) 2014 cryptosource GmbH
3 * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 *
7 */
8
9#include <botan/internal/polyn_gf2m.h>
10
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/code_based_util.h>
15
16namespace Botan {
17
18namespace {
19
20void patch_root_array(gf2m res_root_arr[], size_t res_root_arr_len, size_t root_pos) {
21 volatile gf2m patch_elem = 0x01;
22 volatile gf2m cond_mask = static_cast<gf2m>(root_pos == res_root_arr_len);
23 cond_mask = expand_mask_16bit(cond_mask);
24 cond_mask = ~cond_mask; /* now cond = 1 if not enough roots */
25 patch_elem = patch_elem & cond_mask;
26 for(size_t i = 0; i < res_root_arr_len; i++) {
27 patch_elem = patch_elem + 1;
28 gf2m masked_patch_elem = patch_elem & cond_mask;
29 res_root_arr[i] ^= masked_patch_elem++;
30 }
31}
32
33class gf2m_decomp_rootfind_state {
34 public:
35 gf2m_decomp_rootfind_state(const polyn_gf2m& p_polyn, size_t code_length);
36
37 void calc_LiK(const polyn_gf2m& sigma);
38 gf2m calc_Fxj_j_neq_0(const polyn_gf2m& sigma, gf2m j_gray);
39 void calc_next_Aij();
40 void calc_Ai_zero(const polyn_gf2m& sigma);
41 secure_vector<gf2m> find_roots(const polyn_gf2m& sigma);
42
43 private:
44 size_t m_code_length;
45 secure_vector<gf2m> m_Lik; // size is outer_summands * m
46 secure_vector<gf2m> m_Aij; // ...
47 uint32_t m_outer_summands;
48 gf2m m_j;
49 gf2m m_j_gray;
50 gf2m m_sigma_3_l;
51 gf2m m_sigma_3_neq_0_mask;
52};
53
54/**
55* calculates ceil((t-4)/5) = outer_summands - 1
56*/
57uint32_t brootf_decomp_calc_sum_limit(uint32_t t) {
58 if(t < 4) {
59 return 0;
60 }
61 uint32_t result = t - 4;
62 result += 4;
63 result /= 5;
64 return result;
65}
66
67gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(const polyn_gf2m& polyn, size_t code_length) :
68 m_code_length(code_length), m_j(0), m_j_gray(0) {
69 std::shared_ptr<GF2m_Field> sp_field = polyn.get_sp_field();
70 int deg_sigma = polyn.get_degree();
71 if(deg_sigma <= 3) {
72 throw Internal_Error("Unexpected degree in gf2m_decomp_rootfind_state");
73 }
74
75 gf2m coeff_3 = polyn.get_coef(3);
76 gf2m coeff_head = polyn.get_coef(deg_sigma); /* dummy value for SCA CM */
77 if(coeff_3 != 0) {
78 this->m_sigma_3_l = sp_field->gf_l_from_n(coeff_3);
79 this->m_sigma_3_neq_0_mask = 0xFFFF;
80 } else {
81 // dummy value needed for timing countermeasure
82 this->m_sigma_3_l = sp_field->gf_l_from_n(coeff_head);
83 this->m_sigma_3_neq_0_mask = 0;
84 }
85
86 this->m_outer_summands = 1 + brootf_decomp_calc_sum_limit(deg_sigma);
87 this->m_Lik.resize(this->m_outer_summands * sp_field->get_extension_degree());
88 this->m_Aij.resize(this->m_outer_summands);
89}
90
91void gf2m_decomp_rootfind_state::calc_Ai_zero(const polyn_gf2m& sigma) {
92 /*
93 * this function assumes this the first gray code element is zero
94 */
95 for(uint32_t i = 0; i < this->m_outer_summands; i++) {
96 this->m_Aij[i] = sigma.get_coef(5 * i);
97 }
98 this->m_j = 0;
99 this->m_j_gray = 0;
100}
101
102void gf2m_decomp_rootfind_state::calc_next_Aij() {
103 /*
104 * upon function entry, we have in the state j, Aij.
105 * first thing, we declare Aij Aij_minusone and increase j.
106 * Case j=0 upon function entry also included, then Aij contains A_{i,j=0}.
107 */
108 uint32_t Lik_pos_base = 0;
109
110 this->m_j++;
111
112 gf2m new_j_gray = lex_to_gray(this->m_j);
113
114 if((this->m_j & 1) != 0) {
115 /* half of the times */
116 Lik_pos_base = 0;
117 } else if((this->m_j & 2) != 0) {
118 /* one quarter of the times */
119 Lik_pos_base = this->m_outer_summands;
120 } else if((this->m_j & 4) != 0) {
121 /* one eighth of the times */
122 Lik_pos_base = this->m_outer_summands * 2;
123 } else if((this->m_j & 8) != 0) {
124 /* one sixteenth of the times */
125 Lik_pos_base = this->m_outer_summands * 3;
126 } else if((this->m_j & 16) != 0) {
127 Lik_pos_base = this->m_outer_summands * 4;
128 } else {
129 gf2m delta_offs = 5;
130 gf2m diff = this->m_j_gray ^ new_j_gray;
131 while(((static_cast<gf2m>(1) << delta_offs) & diff) == 0) {
132 delta_offs++;
133 }
134 Lik_pos_base = delta_offs * this->m_outer_summands;
135 }
136 this->m_j_gray = new_j_gray;
137
138 for(uint32_t i = 0; i < this->m_outer_summands; i++) {
139 this->m_Aij[i] ^= this->m_Lik[Lik_pos_base + i];
140 }
141}
142
143void gf2m_decomp_rootfind_state::calc_LiK(const polyn_gf2m& sigma) {
144 std::shared_ptr<GF2m_Field> sp_field = sigma.get_sp_field();
145 uint32_t d = sigma.get_degree();
146 for(uint32_t k = 0; k < sp_field->get_extension_degree(); k++) {
147 uint32_t Lik_pos_base = k * this->m_outer_summands;
148 gf2m alpha_l_k_tt2_ttj[4];
149 alpha_l_k_tt2_ttj[0] = sp_field->gf_l_from_n(static_cast<gf2m>(1) << k);
150 alpha_l_k_tt2_ttj[1] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[0], alpha_l_k_tt2_ttj[0]);
151 alpha_l_k_tt2_ttj[2] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[1], alpha_l_k_tt2_ttj[1]);
152
153 alpha_l_k_tt2_ttj[3] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[2], alpha_l_k_tt2_ttj[2]);
154 for(uint32_t i = 0; i < this->m_outer_summands; i++) {
155 const uint32_t five_i = 5 * i;
156 const uint32_t Lik_pos = Lik_pos_base + i;
157 this->m_Lik[Lik_pos] = 0;
158 for(size_t j = 0; j <= 3; j++) {
159 uint32_t f_ind = five_i + (static_cast<uint32_t>(1) << j);
160 if(f_ind > d) {
161 break;
162 }
163 gf2m f = sigma.get_coef(f_ind);
164
165 gf2m x = sp_field->gf_mul_zrz(alpha_l_k_tt2_ttj[j], f);
166 this->m_Lik[Lik_pos] ^= x;
167 }
168 }
169 }
170}
171
172gf2m gf2m_decomp_rootfind_state::calc_Fxj_j_neq_0(const polyn_gf2m& sigma, gf2m j_gray) {
173 //needs the A_{ij} to compute F(x)_j
174 gf2m sum = 0;
175 std::shared_ptr<GF2m_Field> sp_field = sigma.get_sp_field();
176 const gf2m jl_gray = sp_field->gf_l_from_n(j_gray);
177 gf2m xl_j_tt_5 = sp_field->gf_square_rr(jl_gray);
178 gf2m xl_gray_tt_3 = sp_field->gf_mul_rrr(xl_j_tt_5, jl_gray);
179 xl_j_tt_5 = sp_field->gf_mul_rrr(xl_j_tt_5, xl_gray_tt_3);
180
181 sum = sp_field->gf_mul_nrr(xl_gray_tt_3, this->m_sigma_3_l);
182 sum &= this->m_sigma_3_neq_0_mask;
183 /* here, we rely on compiler to be unable to optimize
184 * for the state->sigma_3_neq_0_mask value
185 */
186 /* treat i = 0 special: */
187 sum ^= this->m_Aij[0];
188 /* treat i = 1 special also */
189
190 if(this->m_outer_summands > 1) {
191 gf2m x = sp_field->gf_mul_zrz(xl_j_tt_5, this->m_Aij[1]); /* x_j^{5i} A_i^j */
192 sum ^= x;
193 }
194
195 gf2m xl_j_tt_5i = xl_j_tt_5;
196
197 for(uint32_t i = 2; i < this->m_outer_summands; i++) {
198 xl_j_tt_5i = sp_field->gf_mul_rrr(xl_j_tt_5i, xl_j_tt_5);
199 // now x_j_tt_5i lives up to its name
200 gf2m x = sp_field->gf_mul_zrz(xl_j_tt_5i, this->m_Aij[i]); /* x_j^{5i} A_i^(j) */
201 sum ^= x;
202 }
203 return sum;
204}
205
206secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(const polyn_gf2m& sigma) {
207 const int sigma_degree = sigma.get_degree();
208 BOTAN_ASSERT(sigma_degree > 0, "Valid sigma");
209 secure_vector<gf2m> result(sigma_degree);
210 uint32_t root_pos = 0;
211
212 this->calc_Ai_zero(sigma);
213 this->calc_LiK(sigma);
214 for(;;) {
215 gf2m eval_result = 0;
216
217 if(this->m_j_gray == 0) {
218 eval_result = sigma.get_coef(0);
219 } else {
220 eval_result = this->calc_Fxj_j_neq_0(sigma, this->m_j_gray);
221 }
222
223 if(eval_result == 0) {
224 result[root_pos] = this->m_j_gray;
225 root_pos++;
226 }
227
228 if(this->m_j + static_cast<uint32_t>(1) == m_code_length) {
229 break;
230 }
231 this->calc_next_Aij();
232 }
233
234 // side channel / fault attack countermeasure:
235 patch_root_array(result.data(), result.size(), root_pos);
236 return result;
237}
238
239} // end anonymous namespace
240
241secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m& polyn, size_t code_length) {
242 gf2m_decomp_rootfind_state state(polyn, code_length);
243 return state.find_roots(polyn);
244}
245
246} // end namespace Botan
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
gf2m lex_to_gray(gf2m lex)
uint16_t expand_mask_16bit(T tst)
BOTAN_FORCE_INLINE constexpr T sigma(T x)
Definition rotate.h:45
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)
uint16_t gf2m