/src/openssl/crypto/ec/curve448/curve448.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright 2015-2016 Cryptography Research, Inc. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | * |
10 | | * Originally written by Mike Hamburg |
11 | | */ |
12 | | #include <openssl/crypto.h> |
13 | | #include "word.h" |
14 | | #include "field.h" |
15 | | |
16 | | #include "point_448.h" |
17 | | #include "ed448.h" |
18 | | #include "crypto/ecx.h" |
19 | | #include "curve448_local.h" |
20 | | |
21 | 0 | #define COFACTOR 4 |
22 | | |
23 | 0 | #define C448_WNAF_FIXED_TABLE_BITS 5 |
24 | 0 | #define C448_WNAF_VAR_TABLE_BITS 3 |
25 | | |
26 | 0 | #define EDWARDS_D (-39081) |
27 | | |
28 | | static const curve448_scalar_t precomputed_scalarmul_adjustment = { |
29 | | { |
30 | | { |
31 | | SC_LIMB(0xc873d6d54a7bb0cfULL), SC_LIMB(0xe933d8d723a70aadULL), |
32 | | SC_LIMB(0xbb124b65129c96fdULL), SC_LIMB(0x00000008335dc163ULL) |
33 | | } |
34 | | } |
35 | | }; |
36 | | |
37 | 0 | #define TWISTED_D (EDWARDS_D - 1) |
38 | | |
39 | 0 | #define WBITS C448_WORD_BITS /* NB this may be different from ARCH_WORD_BITS */ |
40 | | |
41 | | /* Inverse. */ |
42 | | static void gf_invert(gf y, const gf x, int assert_nonzero) |
43 | 0 | { |
44 | 0 | mask_t ret; |
45 | 0 | gf t1, t2; |
46 | |
|
47 | 0 | ossl_gf_sqr(t1, x); /* o^2 */ |
48 | 0 | ret = gf_isr(t2, t1); /* +-1/sqrt(o^2) = +-1/o */ |
49 | 0 | (void)ret; |
50 | 0 | if (assert_nonzero) |
51 | 0 | assert(ret); |
52 | 0 | ossl_gf_sqr(t1, t2); |
53 | 0 | ossl_gf_mul(t2, t1, x); /* not direct to y in case of alias. */ |
54 | 0 | gf_copy(y, t2); |
55 | 0 | } |
56 | | |
57 | | /** identity = (0,1) */ |
58 | | const curve448_point_t ossl_curve448_point_identity = { |
59 | | {{{{0}}}, {{{1}}}, {{{1}}}, {{{0}}}} |
60 | | }; |
61 | | |
62 | | static void point_double_internal(curve448_point_t p, const curve448_point_t q, |
63 | | int before_double) |
64 | 0 | { |
65 | 0 | gf a, b, c, d; |
66 | |
|
67 | 0 | ossl_gf_sqr(c, q->x); |
68 | 0 | ossl_gf_sqr(a, q->y); |
69 | 0 | gf_add_nr(d, c, a); /* 2+e */ |
70 | 0 | gf_add_nr(p->t, q->y, q->x); /* 2+e */ |
71 | 0 | ossl_gf_sqr(b, p->t); |
72 | 0 | gf_subx_nr(b, b, d, 3); /* 4+e */ |
73 | 0 | gf_sub_nr(p->t, a, c); /* 3+e */ |
74 | 0 | ossl_gf_sqr(p->x, q->z); |
75 | 0 | gf_add_nr(p->z, p->x, p->x); /* 2+e */ |
76 | 0 | gf_subx_nr(a, p->z, p->t, 4); /* 6+e */ |
77 | 0 | if (GF_HEADROOM == 5) |
78 | 0 | gf_weak_reduce(a); /* or 1+e */ |
79 | 0 | ossl_gf_mul(p->x, a, b); |
80 | 0 | ossl_gf_mul(p->z, p->t, a); |
81 | 0 | ossl_gf_mul(p->y, p->t, d); |
82 | 0 | if (!before_double) |
83 | 0 | ossl_gf_mul(p->t, b, d); |
84 | 0 | } |
85 | | |
86 | | void ossl_curve448_point_double(curve448_point_t p, const curve448_point_t q) |
87 | 0 | { |
88 | 0 | point_double_internal(p, q, 0); |
89 | 0 | } |
90 | | |
91 | | /* Operations on [p]niels */ |
92 | | static ossl_inline void cond_neg_niels(niels_t n, mask_t neg) |
93 | 0 | { |
94 | 0 | gf_cond_swap(n->a, n->b, neg); |
95 | 0 | gf_cond_neg(n->c, neg); |
96 | 0 | } |
97 | | |
98 | | static void pt_to_pniels(pniels_t b, const curve448_point_t a) |
99 | 0 | { |
100 | 0 | gf_sub(b->n->a, a->y, a->x); |
101 | 0 | gf_add(b->n->b, a->x, a->y); |
102 | 0 | gf_mulw(b->n->c, a->t, 2 * TWISTED_D); |
103 | 0 | gf_add(b->z, a->z, a->z); |
104 | 0 | } |
105 | | |
106 | | static void pniels_to_pt(curve448_point_t e, const pniels_t d) |
107 | 0 | { |
108 | 0 | gf eu; |
109 | |
|
110 | 0 | gf_add(eu, d->n->b, d->n->a); |
111 | 0 | gf_sub(e->y, d->n->b, d->n->a); |
112 | 0 | ossl_gf_mul(e->t, e->y, eu); |
113 | 0 | ossl_gf_mul(e->x, d->z, e->y); |
114 | 0 | ossl_gf_mul(e->y, d->z, eu); |
115 | 0 | ossl_gf_sqr(e->z, d->z); |
116 | 0 | } |
117 | | |
118 | | static void niels_to_pt(curve448_point_t e, const niels_t n) |
119 | 0 | { |
120 | 0 | gf_add(e->y, n->b, n->a); |
121 | 0 | gf_sub(e->x, n->b, n->a); |
122 | 0 | ossl_gf_mul(e->t, e->y, e->x); |
123 | 0 | gf_copy(e->z, ONE); |
124 | 0 | } |
125 | | |
126 | | static void add_niels_to_pt(curve448_point_t d, const niels_t e, |
127 | | int before_double) |
128 | 0 | { |
129 | 0 | gf a, b, c; |
130 | |
|
131 | 0 | gf_sub_nr(b, d->y, d->x); /* 3+e */ |
132 | 0 | ossl_gf_mul(a, e->a, b); |
133 | 0 | gf_add_nr(b, d->x, d->y); /* 2+e */ |
134 | 0 | ossl_gf_mul(d->y, e->b, b); |
135 | 0 | ossl_gf_mul(d->x, e->c, d->t); |
136 | 0 | gf_add_nr(c, a, d->y); /* 2+e */ |
137 | 0 | gf_sub_nr(b, d->y, a); /* 3+e */ |
138 | 0 | gf_sub_nr(d->y, d->z, d->x); /* 3+e */ |
139 | 0 | gf_add_nr(a, d->x, d->z); /* 2+e */ |
140 | 0 | ossl_gf_mul(d->z, a, d->y); |
141 | 0 | ossl_gf_mul(d->x, d->y, b); |
142 | 0 | ossl_gf_mul(d->y, a, c); |
143 | 0 | if (!before_double) |
144 | 0 | ossl_gf_mul(d->t, b, c); |
145 | 0 | } |
146 | | |
147 | | static void sub_niels_from_pt(curve448_point_t d, const niels_t e, |
148 | | int before_double) |
149 | 0 | { |
150 | 0 | gf a, b, c; |
151 | |
|
152 | 0 | gf_sub_nr(b, d->y, d->x); /* 3+e */ |
153 | 0 | ossl_gf_mul(a, e->b, b); |
154 | 0 | gf_add_nr(b, d->x, d->y); /* 2+e */ |
155 | 0 | ossl_gf_mul(d->y, e->a, b); |
156 | 0 | ossl_gf_mul(d->x, e->c, d->t); |
157 | 0 | gf_add_nr(c, a, d->y); /* 2+e */ |
158 | 0 | gf_sub_nr(b, d->y, a); /* 3+e */ |
159 | 0 | gf_add_nr(d->y, d->z, d->x); /* 2+e */ |
160 | 0 | gf_sub_nr(a, d->z, d->x); /* 3+e */ |
161 | 0 | ossl_gf_mul(d->z, a, d->y); |
162 | 0 | ossl_gf_mul(d->x, d->y, b); |
163 | 0 | ossl_gf_mul(d->y, a, c); |
164 | 0 | if (!before_double) |
165 | 0 | ossl_gf_mul(d->t, b, c); |
166 | 0 | } |
167 | | |
168 | | static void add_pniels_to_pt(curve448_point_t p, const pniels_t pn, |
169 | | int before_double) |
170 | 0 | { |
171 | 0 | gf L0; |
172 | |
|
173 | 0 | ossl_gf_mul(L0, p->z, pn->z); |
174 | 0 | gf_copy(p->z, L0); |
175 | 0 | add_niels_to_pt(p, pn->n, before_double); |
176 | 0 | } |
177 | | |
178 | | static void sub_pniels_from_pt(curve448_point_t p, const pniels_t pn, |
179 | | int before_double) |
180 | 0 | { |
181 | 0 | gf L0; |
182 | |
|
183 | 0 | ossl_gf_mul(L0, p->z, pn->z); |
184 | 0 | gf_copy(p->z, L0); |
185 | 0 | sub_niels_from_pt(p, pn->n, before_double); |
186 | 0 | } |
187 | | |
188 | | c448_bool_t |
189 | | ossl_curve448_point_eq(const curve448_point_t p, |
190 | | const curve448_point_t q) |
191 | 0 | { |
192 | 0 | mask_t succ; |
193 | 0 | gf a, b; |
194 | | |
195 | | /* equality mod 2-torsion compares x/y */ |
196 | 0 | ossl_gf_mul(a, p->y, q->x); |
197 | 0 | ossl_gf_mul(b, q->y, p->x); |
198 | 0 | succ = gf_eq(a, b); |
199 | |
|
200 | 0 | return mask_to_bool(succ); |
201 | 0 | } |
202 | | |
203 | | c448_bool_t |
204 | | ossl_curve448_point_valid(const curve448_point_t p) |
205 | 0 | { |
206 | 0 | mask_t out; |
207 | 0 | gf a, b, c; |
208 | |
|
209 | 0 | ossl_gf_mul(a, p->x, p->y); |
210 | 0 | ossl_gf_mul(b, p->z, p->t); |
211 | 0 | out = gf_eq(a, b); |
212 | 0 | ossl_gf_sqr(a, p->x); |
213 | 0 | ossl_gf_sqr(b, p->y); |
214 | 0 | gf_sub(a, b, a); |
215 | 0 | ossl_gf_sqr(b, p->t); |
216 | 0 | gf_mulw(c, b, TWISTED_D); |
217 | 0 | ossl_gf_sqr(b, p->z); |
218 | 0 | gf_add(b, b, c); |
219 | 0 | out &= gf_eq(a, b); |
220 | 0 | out &= ~gf_eq(p->z, ZERO); |
221 | 0 | return mask_to_bool(out); |
222 | 0 | } |
223 | | |
224 | | static ossl_inline void constant_time_lookup_niels(niels_s * RESTRICT ni, |
225 | | const niels_t *table, |
226 | | int nelts, int idx) |
227 | 0 | { |
228 | 0 | constant_time_lookup(ni, table, sizeof(niels_s), nelts, idx); |
229 | 0 | } |
230 | | |
231 | | void |
232 | | ossl_curve448_precomputed_scalarmul(curve448_point_t out, |
233 | | const curve448_precomputed_s *table, |
234 | | const curve448_scalar_t scalar) |
235 | 0 | { |
236 | 0 | unsigned int i, j, k; |
237 | 0 | const unsigned int n = COMBS_N, t = COMBS_T, s = COMBS_S; |
238 | 0 | niels_t ni; |
239 | 0 | curve448_scalar_t scalar1x; |
240 | |
|
241 | 0 | ossl_curve448_scalar_add(scalar1x, scalar, precomputed_scalarmul_adjustment); |
242 | 0 | ossl_curve448_scalar_halve(scalar1x, scalar1x); |
243 | |
|
244 | 0 | for (i = s; i > 0; i--) { |
245 | 0 | if (i != s) |
246 | 0 | point_double_internal(out, out, 0); |
247 | |
|
248 | 0 | for (j = 0; j < n; j++) { |
249 | 0 | int tab = 0; |
250 | 0 | mask_t invert; |
251 | |
|
252 | 0 | for (k = 0; k < t; k++) { |
253 | 0 | unsigned int bit = (i - 1) + s * (k + j * t); |
254 | |
|
255 | 0 | if (bit < C448_SCALAR_BITS) |
256 | 0 | tab |= |
257 | 0 | (scalar1x->limb[bit / WBITS] >> (bit % WBITS) & 1) << k; |
258 | 0 | } |
259 | |
|
260 | 0 | invert = (tab >> (t - 1)) - 1; |
261 | 0 | tab ^= invert; |
262 | 0 | tab &= (1 << (t - 1)) - 1; |
263 | |
|
264 | 0 | constant_time_lookup_niels(ni, &table->table[j << (t - 1)], |
265 | 0 | 1 << (t - 1), tab); |
266 | |
|
267 | 0 | cond_neg_niels(ni, invert); |
268 | 0 | if ((i != s) || j != 0) |
269 | 0 | add_niels_to_pt(out, ni, j == n - 1 && i != 1); |
270 | 0 | else |
271 | 0 | niels_to_pt(out, ni); |
272 | 0 | } |
273 | 0 | } |
274 | |
|
275 | 0 | OPENSSL_cleanse(ni, sizeof(ni)); |
276 | 0 | OPENSSL_cleanse(scalar1x, sizeof(scalar1x)); |
277 | 0 | } |
278 | | |
279 | | void |
280 | | ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa( |
281 | | uint8_t enc[EDDSA_448_PUBLIC_BYTES], |
282 | | const curve448_point_t p) |
283 | 0 | { |
284 | 0 | gf x, y, z, t; |
285 | 0 | curve448_point_t q; |
286 | | |
287 | | /* The point is now on the twisted curve. Move it to untwisted. */ |
288 | 0 | curve448_point_copy(q, p); |
289 | |
|
290 | 0 | { |
291 | | /* 4-isogeny: 2xy/(y^+x^2), (y^2-x^2)/(2z^2-y^2+x^2) */ |
292 | 0 | gf u; |
293 | |
|
294 | 0 | ossl_gf_sqr(x, q->x); |
295 | 0 | ossl_gf_sqr(t, q->y); |
296 | 0 | gf_add(u, x, t); |
297 | 0 | gf_add(z, q->y, q->x); |
298 | 0 | ossl_gf_sqr(y, z); |
299 | 0 | gf_sub(y, y, u); |
300 | 0 | gf_sub(z, t, x); |
301 | 0 | ossl_gf_sqr(x, q->z); |
302 | 0 | gf_add(t, x, x); |
303 | 0 | gf_sub(t, t, z); |
304 | 0 | ossl_gf_mul(x, t, y); |
305 | 0 | ossl_gf_mul(y, z, u); |
306 | 0 | ossl_gf_mul(z, u, t); |
307 | 0 | OPENSSL_cleanse(u, sizeof(u)); |
308 | 0 | } |
309 | | |
310 | | /* Affinize */ |
311 | 0 | gf_invert(z, z, 1); |
312 | 0 | ossl_gf_mul(t, x, z); |
313 | 0 | ossl_gf_mul(x, y, z); |
314 | | |
315 | | /* Encode */ |
316 | 0 | enc[EDDSA_448_PRIVATE_BYTES - 1] = 0; |
317 | 0 | gf_serialize(enc, x, 1); |
318 | 0 | enc[EDDSA_448_PRIVATE_BYTES - 1] |= 0x80 & gf_lobit(t); |
319 | |
|
320 | 0 | OPENSSL_cleanse(x, sizeof(x)); |
321 | 0 | OPENSSL_cleanse(y, sizeof(y)); |
322 | 0 | OPENSSL_cleanse(z, sizeof(z)); |
323 | 0 | OPENSSL_cleanse(t, sizeof(t)); |
324 | 0 | ossl_curve448_point_destroy(q); |
325 | 0 | } |
326 | | |
327 | | c448_error_t |
328 | | ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio( |
329 | | curve448_point_t p, |
330 | | const uint8_t enc[EDDSA_448_PUBLIC_BYTES]) |
331 | 0 | { |
332 | 0 | uint8_t enc2[EDDSA_448_PUBLIC_BYTES]; |
333 | 0 | mask_t low; |
334 | 0 | mask_t succ; |
335 | |
|
336 | 0 | memcpy(enc2, enc, sizeof(enc2)); |
337 | |
|
338 | 0 | low = ~word_is_zero(enc2[EDDSA_448_PRIVATE_BYTES - 1] & 0x80); |
339 | 0 | enc2[EDDSA_448_PRIVATE_BYTES - 1] &= ~0x80; |
340 | |
|
341 | 0 | succ = gf_deserialize(p->y, enc2, 1, 0); |
342 | 0 | succ &= word_is_zero(enc2[EDDSA_448_PRIVATE_BYTES - 1]); |
343 | |
|
344 | 0 | ossl_gf_sqr(p->x, p->y); |
345 | 0 | gf_sub(p->z, ONE, p->x); /* num = 1-y^2 */ |
346 | 0 | gf_mulw(p->t, p->x, EDWARDS_D); /* dy^2 */ |
347 | 0 | gf_sub(p->t, ONE, p->t); /* denom = 1-dy^2 or 1-d + dy^2 */ |
348 | |
|
349 | 0 | ossl_gf_mul(p->x, p->z, p->t); |
350 | 0 | succ &= gf_isr(p->t, p->x); /* 1/sqrt(num * denom) */ |
351 | |
|
352 | 0 | ossl_gf_mul(p->x, p->t, p->z); /* sqrt(num / denom) */ |
353 | 0 | gf_cond_neg(p->x, gf_lobit(p->x) ^ low); |
354 | 0 | gf_copy(p->z, ONE); |
355 | |
|
356 | 0 | { |
357 | 0 | gf a, b, c, d; |
358 | | |
359 | | /* 4-isogeny 2xy/(y^2-ax^2), (y^2+ax^2)/(2-y^2-ax^2) */ |
360 | 0 | ossl_gf_sqr(c, p->x); |
361 | 0 | ossl_gf_sqr(a, p->y); |
362 | 0 | gf_add(d, c, a); |
363 | 0 | gf_add(p->t, p->y, p->x); |
364 | 0 | ossl_gf_sqr(b, p->t); |
365 | 0 | gf_sub(b, b, d); |
366 | 0 | gf_sub(p->t, a, c); |
367 | 0 | ossl_gf_sqr(p->x, p->z); |
368 | 0 | gf_add(p->z, p->x, p->x); |
369 | 0 | gf_sub(a, p->z, d); |
370 | 0 | ossl_gf_mul(p->x, a, b); |
371 | 0 | ossl_gf_mul(p->z, p->t, a); |
372 | 0 | ossl_gf_mul(p->y, p->t, d); |
373 | 0 | ossl_gf_mul(p->t, b, d); |
374 | 0 | OPENSSL_cleanse(a, sizeof(a)); |
375 | 0 | OPENSSL_cleanse(b, sizeof(b)); |
376 | 0 | OPENSSL_cleanse(c, sizeof(c)); |
377 | 0 | OPENSSL_cleanse(d, sizeof(d)); |
378 | 0 | } |
379 | |
|
380 | 0 | OPENSSL_cleanse(enc2, sizeof(enc2)); |
381 | 0 | assert(ossl_curve448_point_valid(p) || ~succ); |
382 | | |
383 | 0 | return c448_succeed_if(mask_to_bool(succ)); |
384 | 0 | } |
385 | | |
386 | | c448_error_t |
387 | | ossl_x448_int(uint8_t out[X_PUBLIC_BYTES], |
388 | | const uint8_t base[X_PUBLIC_BYTES], |
389 | | const uint8_t scalar[X_PRIVATE_BYTES]) |
390 | 0 | { |
391 | 0 | gf x1, x2, z2, x3, z3, t1, t2; |
392 | 0 | int t; |
393 | 0 | mask_t swap = 0; |
394 | 0 | mask_t nz; |
395 | |
|
396 | 0 | (void)gf_deserialize(x1, base, 1, 0); |
397 | 0 | gf_copy(x2, ONE); |
398 | 0 | gf_copy(z2, ZERO); |
399 | 0 | gf_copy(x3, x1); |
400 | 0 | gf_copy(z3, ONE); |
401 | |
|
402 | 0 | for (t = X_PRIVATE_BITS - 1; t >= 0; t--) { |
403 | 0 | uint8_t sb = scalar[t / 8]; |
404 | 0 | mask_t k_t; |
405 | | |
406 | | /* Scalar conditioning */ |
407 | 0 | if (t / 8 == 0) |
408 | 0 | sb &= -(uint8_t)COFACTOR; |
409 | 0 | else if (t == X_PRIVATE_BITS - 1) |
410 | 0 | sb = -1; |
411 | |
|
412 | 0 | k_t = (sb >> (t % 8)) & 1; |
413 | 0 | k_t = 0 - k_t; /* set to all 0s or all 1s */ |
414 | |
|
415 | 0 | swap ^= k_t; |
416 | 0 | gf_cond_swap(x2, x3, swap); |
417 | 0 | gf_cond_swap(z2, z3, swap); |
418 | 0 | swap = k_t; |
419 | | |
420 | | /* |
421 | | * The "_nr" below skips coefficient reduction. In the following |
422 | | * comments, "2+e" is saying that the coefficients are at most 2+epsilon |
423 | | * times the reduction limit. |
424 | | */ |
425 | 0 | gf_add_nr(t1, x2, z2); /* A = x2 + z2 */ /* 2+e */ |
426 | 0 | gf_sub_nr(t2, x2, z2); /* B = x2 - z2 */ /* 3+e */ |
427 | 0 | gf_sub_nr(z2, x3, z3); /* D = x3 - z3 */ /* 3+e */ |
428 | 0 | ossl_gf_mul(x2, t1, z2); /* DA */ |
429 | 0 | gf_add_nr(z2, z3, x3); /* C = x3 + z3 */ /* 2+e */ |
430 | 0 | ossl_gf_mul(x3, t2, z2); /* CB */ |
431 | 0 | gf_sub_nr(z3, x2, x3); /* DA-CB */ /* 3+e */ |
432 | 0 | ossl_gf_sqr(z2, z3); /* (DA-CB)^2 */ |
433 | 0 | ossl_gf_mul(z3, x1, z2); /* z3 = x1(DA-CB)^2 */ |
434 | 0 | gf_add_nr(z2, x2, x3); /* (DA+CB) */ /* 2+e */ |
435 | 0 | ossl_gf_sqr(x3, z2); /* x3 = (DA+CB)^2 */ |
436 | |
|
437 | 0 | ossl_gf_sqr(z2, t1); /* AA = A^2 */ |
438 | 0 | ossl_gf_sqr(t1, t2); /* BB = B^2 */ |
439 | 0 | ossl_gf_mul(x2, z2, t1); /* x2 = AA*BB */ |
440 | 0 | gf_sub_nr(t2, z2, t1); /* E = AA-BB */ /* 3+e */ |
441 | |
|
442 | 0 | gf_mulw(t1, t2, -EDWARDS_D); /* E*-d = a24*E */ |
443 | 0 | gf_add_nr(t1, t1, z2); /* AA + a24*E */ /* 2+e */ |
444 | 0 | ossl_gf_mul(z2, t2, t1); /* z2 = E(AA+a24*E) */ |
445 | 0 | } |
446 | | |
447 | | /* Finish */ |
448 | 0 | gf_cond_swap(x2, x3, swap); |
449 | 0 | gf_cond_swap(z2, z3, swap); |
450 | 0 | gf_invert(z2, z2, 0); |
451 | 0 | ossl_gf_mul(x1, x2, z2); |
452 | 0 | gf_serialize(out, x1, 1); |
453 | 0 | nz = ~gf_eq(x1, ZERO); |
454 | |
|
455 | 0 | OPENSSL_cleanse(x1, sizeof(x1)); |
456 | 0 | OPENSSL_cleanse(x2, sizeof(x2)); |
457 | 0 | OPENSSL_cleanse(z2, sizeof(z2)); |
458 | 0 | OPENSSL_cleanse(x3, sizeof(x3)); |
459 | 0 | OPENSSL_cleanse(z3, sizeof(z3)); |
460 | 0 | OPENSSL_cleanse(t1, sizeof(t1)); |
461 | 0 | OPENSSL_cleanse(t2, sizeof(t2)); |
462 | |
|
463 | 0 | return c448_succeed_if(mask_to_bool(nz)); |
464 | 0 | } |
465 | | |
466 | | void |
467 | | ossl_curve448_point_mul_by_ratio_and_encode_like_x448(uint8_t |
468 | | out[X_PUBLIC_BYTES], |
469 | | const curve448_point_t p) |
470 | 0 | { |
471 | 0 | curve448_point_t q; |
472 | |
|
473 | 0 | curve448_point_copy(q, p); |
474 | 0 | gf_invert(q->t, q->x, 0); /* 1/x */ |
475 | 0 | ossl_gf_mul(q->z, q->t, q->y); /* y/x */ |
476 | 0 | ossl_gf_sqr(q->y, q->z); /* (y/x)^2 */ |
477 | 0 | gf_serialize(out, q->y, 1); |
478 | 0 | ossl_curve448_point_destroy(q); |
479 | 0 | } |
480 | | |
481 | | void ossl_x448_derive_public_key(uint8_t out[X_PUBLIC_BYTES], |
482 | | const uint8_t scalar[X_PRIVATE_BYTES]) |
483 | 0 | { |
484 | | /* Scalar conditioning */ |
485 | 0 | uint8_t scalar2[X_PRIVATE_BYTES]; |
486 | 0 | curve448_scalar_t the_scalar; |
487 | 0 | curve448_point_t p; |
488 | 0 | unsigned int i; |
489 | |
|
490 | 0 | memcpy(scalar2, scalar, sizeof(scalar2)); |
491 | 0 | scalar2[0] &= -(uint8_t)COFACTOR; |
492 | |
|
493 | 0 | scalar2[X_PRIVATE_BYTES - 1] &= ~((0u - 1u) << ((X_PRIVATE_BITS + 7) % 8)); |
494 | 0 | scalar2[X_PRIVATE_BYTES - 1] |= 1 << ((X_PRIVATE_BITS + 7) % 8); |
495 | |
|
496 | 0 | ossl_curve448_scalar_decode_long(the_scalar, scalar2, sizeof(scalar2)); |
497 | | |
498 | | /* Compensate for the encoding ratio */ |
499 | 0 | for (i = 1; i < X448_ENCODE_RATIO; i <<= 1) |
500 | 0 | ossl_curve448_scalar_halve(the_scalar, the_scalar); |
501 | |
|
502 | 0 | ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base, |
503 | 0 | the_scalar); |
504 | 0 | ossl_curve448_point_mul_by_ratio_and_encode_like_x448(out, p); |
505 | 0 | ossl_curve448_point_destroy(p); |
506 | 0 | } |
507 | | |
508 | | /* Control for variable-time scalar multiply algorithms. */ |
509 | | struct smvt_control { |
510 | | int power, addend; |
511 | | }; |
512 | | |
513 | | #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)) |
514 | 0 | # define NUMTRAILINGZEROS __builtin_ctz |
515 | | #else |
516 | | # define NUMTRAILINGZEROS numtrailingzeros |
517 | | static uint32_t numtrailingzeros(uint32_t i) |
518 | | { |
519 | | uint32_t tmp; |
520 | | uint32_t num = 31; |
521 | | |
522 | | if (i == 0) |
523 | | return 32; |
524 | | |
525 | | tmp = i << 16; |
526 | | if (tmp != 0) { |
527 | | i = tmp; |
528 | | num -= 16; |
529 | | } |
530 | | tmp = i << 8; |
531 | | if (tmp != 0) { |
532 | | i = tmp; |
533 | | num -= 8; |
534 | | } |
535 | | tmp = i << 4; |
536 | | if (tmp != 0) { |
537 | | i = tmp; |
538 | | num -= 4; |
539 | | } |
540 | | tmp = i << 2; |
541 | | if (tmp != 0) { |
542 | | i = tmp; |
543 | | num -= 2; |
544 | | } |
545 | | tmp = i << 1; |
546 | | if (tmp != 0) |
547 | | num--; |
548 | | |
549 | | return num; |
550 | | } |
551 | | #endif |
552 | | |
553 | | static int recode_wnaf(struct smvt_control *control, |
554 | | /* [nbits/(table_bits + 1) + 3] */ |
555 | | const curve448_scalar_t scalar, |
556 | | unsigned int table_bits) |
557 | 0 | { |
558 | 0 | unsigned int table_size = C448_SCALAR_BITS / (table_bits + 1) + 3; |
559 | 0 | int position = table_size - 1; /* at the end */ |
560 | 0 | uint64_t current = scalar->limb[0] & 0xFFFF; |
561 | 0 | uint32_t mask = (1 << (table_bits + 1)) - 1; |
562 | 0 | unsigned int w; |
563 | 0 | const unsigned int B_OVER_16 = sizeof(scalar->limb[0]) / 2; |
564 | 0 | unsigned int n, i; |
565 | | |
566 | | /* place the end marker */ |
567 | 0 | control[position].power = -1; |
568 | 0 | control[position].addend = 0; |
569 | 0 | position--; |
570 | | |
571 | | /* |
572 | | * PERF: Could negate scalar if it's large. But then would need more cases |
573 | | * in the actual code that uses it, all for an expected reduction of like |
574 | | * 1/5 op. Probably not worth it. |
575 | | */ |
576 | |
|
577 | 0 | for (w = 1; w < (C448_SCALAR_BITS - 1) / 16 + 3; w++) { |
578 | 0 | if (w < (C448_SCALAR_BITS - 1) / 16 + 1) { |
579 | | /* Refill the 16 high bits of current */ |
580 | 0 | current += (uint32_t)((scalar->limb[w / B_OVER_16] |
581 | 0 | >> (16 * (w % B_OVER_16))) << 16); |
582 | 0 | } |
583 | |
|
584 | 0 | while (current & 0xFFFF) { |
585 | 0 | uint32_t pos = NUMTRAILINGZEROS((uint32_t)current); |
586 | 0 | uint32_t odd = (uint32_t)current >> pos; |
587 | 0 | int32_t delta = odd & mask; |
588 | |
|
589 | 0 | assert(position >= 0); |
590 | 0 | if (odd & (1 << (table_bits + 1))) |
591 | 0 | delta -= (1 << (table_bits + 1)); |
592 | | /* |
593 | | * Coverity gets confused by the value of pos, thinking it might be |
594 | | * 32. This would require current & 0xFFFF to be zero which isn't |
595 | | * possible. Suppress this false positive, since adding a check |
596 | | * isn't desirable. |
597 | | */ |
598 | | /* coverity[overflow_before_widen] */ |
599 | 0 | current -= delta * (1 << pos); |
600 | 0 | control[position].power = pos + 16 * (w - 1); |
601 | 0 | control[position].addend = delta; |
602 | 0 | position--; |
603 | 0 | } |
604 | 0 | current >>= 16; |
605 | 0 | } |
606 | 0 | assert(current == 0); |
607 | | |
608 | 0 | position++; |
609 | 0 | n = table_size - position; |
610 | 0 | for (i = 0; i < n; i++) |
611 | 0 | control[i] = control[i + position]; |
612 | |
|
613 | 0 | return n - 1; |
614 | 0 | } |
615 | | |
616 | | static void prepare_wnaf_table(pniels_t *output, |
617 | | const curve448_point_t working, |
618 | | unsigned int tbits) |
619 | 0 | { |
620 | 0 | curve448_point_t tmp; |
621 | 0 | int i; |
622 | 0 | pniels_t twop; |
623 | |
|
624 | 0 | pt_to_pniels(output[0], working); |
625 | |
|
626 | 0 | if (tbits == 0) |
627 | 0 | return; |
628 | | |
629 | 0 | ossl_curve448_point_double(tmp, working); |
630 | 0 | pt_to_pniels(twop, tmp); |
631 | |
|
632 | 0 | add_pniels_to_pt(tmp, output[0], 0); |
633 | 0 | pt_to_pniels(output[1], tmp); |
634 | |
|
635 | 0 | for (i = 2; i < 1 << tbits; i++) { |
636 | 0 | add_pniels_to_pt(tmp, twop, 0); |
637 | 0 | pt_to_pniels(output[i], tmp); |
638 | 0 | } |
639 | |
|
640 | 0 | ossl_curve448_point_destroy(tmp); |
641 | 0 | OPENSSL_cleanse(twop, sizeof(twop)); |
642 | 0 | } |
643 | | |
644 | | void |
645 | | ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo, |
646 | | const curve448_scalar_t scalar1, |
647 | | const curve448_point_t base2, |
648 | | const curve448_scalar_t scalar2) |
649 | 0 | { |
650 | 0 | const int table_bits_var = C448_WNAF_VAR_TABLE_BITS; |
651 | 0 | const int table_bits_pre = C448_WNAF_FIXED_TABLE_BITS; |
652 | 0 | struct smvt_control control_var[C448_SCALAR_BITS / |
653 | 0 | (C448_WNAF_VAR_TABLE_BITS + 1) + 3]; |
654 | 0 | struct smvt_control control_pre[C448_SCALAR_BITS / |
655 | 0 | (C448_WNAF_FIXED_TABLE_BITS + 1) + 3]; |
656 | 0 | int ncb_pre = recode_wnaf(control_pre, scalar1, table_bits_pre); |
657 | 0 | int ncb_var = recode_wnaf(control_var, scalar2, table_bits_var); |
658 | 0 | pniels_t precmp_var[1 << C448_WNAF_VAR_TABLE_BITS]; |
659 | 0 | int contp = 0, contv = 0, i; |
660 | |
|
661 | 0 | prepare_wnaf_table(precmp_var, base2, table_bits_var); |
662 | 0 | i = control_var[0].power; |
663 | |
|
664 | 0 | if (i < 0) { |
665 | 0 | curve448_point_copy(combo, ossl_curve448_point_identity); |
666 | 0 | return; |
667 | 0 | } |
668 | 0 | if (i > control_pre[0].power) { |
669 | 0 | pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]); |
670 | 0 | contv++; |
671 | 0 | } else if (i == control_pre[0].power && i >= 0) { |
672 | 0 | pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]); |
673 | 0 | add_niels_to_pt(combo, |
674 | 0 | ossl_curve448_wnaf_base[control_pre[0].addend >> 1], |
675 | 0 | i); |
676 | 0 | contv++; |
677 | 0 | contp++; |
678 | 0 | } else { |
679 | 0 | i = control_pre[0].power; |
680 | 0 | niels_to_pt(combo, ossl_curve448_wnaf_base[control_pre[0].addend >> 1]); |
681 | 0 | contp++; |
682 | 0 | } |
683 | |
|
684 | 0 | for (i--; i >= 0; i--) { |
685 | 0 | int cv = (i == control_var[contv].power); |
686 | 0 | int cp = (i == control_pre[contp].power); |
687 | |
|
688 | 0 | point_double_internal(combo, combo, i && !(cv || cp)); |
689 | |
|
690 | 0 | if (cv) { |
691 | 0 | assert(control_var[contv].addend); |
692 | | |
693 | 0 | if (control_var[contv].addend > 0) |
694 | 0 | add_pniels_to_pt(combo, |
695 | 0 | precmp_var[control_var[contv].addend >> 1], |
696 | 0 | i && !cp); |
697 | 0 | else |
698 | 0 | sub_pniels_from_pt(combo, |
699 | 0 | precmp_var[(-control_var[contv].addend) |
700 | 0 | >> 1], i && !cp); |
701 | 0 | contv++; |
702 | 0 | } |
703 | | |
704 | 0 | if (cp) { |
705 | 0 | assert(control_pre[contp].addend); |
706 | | |
707 | 0 | if (control_pre[contp].addend > 0) |
708 | 0 | add_niels_to_pt(combo, |
709 | 0 | ossl_curve448_wnaf_base[control_pre[contp].addend |
710 | 0 | >> 1], i); |
711 | 0 | else |
712 | 0 | sub_niels_from_pt(combo, |
713 | 0 | ossl_curve448_wnaf_base[(-control_pre |
714 | 0 | [contp].addend) >> 1], i); |
715 | 0 | contp++; |
716 | 0 | } |
717 | 0 | } |
718 | | |
719 | | /* This function is non-secret, but whatever this is cheap. */ |
720 | 0 | OPENSSL_cleanse(control_var, sizeof(control_var)); |
721 | 0 | OPENSSL_cleanse(control_pre, sizeof(control_pre)); |
722 | 0 | OPENSSL_cleanse(precmp_var, sizeof(precmp_var)); |
723 | |
|
724 | 0 | assert(contv == ncb_var); |
725 | 0 | (void)ncb_var; |
726 | 0 | assert(contp == ncb_pre); |
727 | 0 | (void)ncb_pre; |
728 | 0 | } |
729 | | |
730 | | void ossl_curve448_point_destroy(curve448_point_t point) |
731 | 0 | { |
732 | 0 | OPENSSL_cleanse(point, sizeof(curve448_point_t)); |
733 | 0 | } |
734 | | |
735 | | int ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56], |
736 | | const uint8_t peer_public_value[56]) |
737 | 0 | { |
738 | 0 | return ossl_x448_int(out_shared_key, peer_public_value, private_key) |
739 | 0 | == C448_SUCCESS; |
740 | 0 | } |
741 | | |
742 | | void ossl_x448_public_from_private(uint8_t out_public_value[56], |
743 | | const uint8_t private_key[56]) |
744 | 0 | { |
745 | 0 | ossl_x448_derive_public_key(out_public_value, private_key); |
746 | 0 | } |