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