curve448.c 22 KB

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