curve448.c 21 KB

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