2
0

bn_gf2m.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  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. #include <assert.h>
  11. #include <limits.h>
  12. #include <stdio.h>
  13. #include "internal/cryptlib.h"
  14. #include "bn_local.h"
  15. #ifndef OPENSSL_NO_EC2M
  16. /*
  17. * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
  18. * fail.
  19. */
  20. # define MAX_ITERATIONS 50
  21. # define SQR_nibble(w) ((((w) & 8) << 3) \
  22. | (((w) & 4) << 2) \
  23. | (((w) & 2) << 1) \
  24. | ((w) & 1))
  25. /* Platform-specific macros to accelerate squaring. */
  26. # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
  27. # define SQR1(w) \
  28. SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | \
  29. SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | \
  30. SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | \
  31. SQR_nibble((w) >> 36) << 8 | SQR_nibble((w) >> 32)
  32. # define SQR0(w) \
  33. SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | \
  34. SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | \
  35. SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \
  36. SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) )
  37. # endif
  38. # ifdef THIRTY_TWO_BIT
  39. # define SQR1(w) \
  40. SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | \
  41. SQR_nibble((w) >> 20) << 8 | SQR_nibble((w) >> 16)
  42. # define SQR0(w) \
  43. SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \
  44. SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) )
  45. # endif
  46. # if !defined(OPENSSL_BN_ASM_GF2m)
  47. /*
  48. * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
  49. * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
  50. * the variables have the right amount of space allocated.
  51. */
  52. # ifdef THIRTY_TWO_BIT
  53. static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
  54. const BN_ULONG b)
  55. {
  56. register BN_ULONG h, l, s;
  57. BN_ULONG tab[8], top2b = a >> 30;
  58. register BN_ULONG a1, a2, a4;
  59. a1 = a & (0x3FFFFFFF);
  60. a2 = a1 << 1;
  61. a4 = a2 << 1;
  62. tab[0] = 0;
  63. tab[1] = a1;
  64. tab[2] = a2;
  65. tab[3] = a1 ^ a2;
  66. tab[4] = a4;
  67. tab[5] = a1 ^ a4;
  68. tab[6] = a2 ^ a4;
  69. tab[7] = a1 ^ a2 ^ a4;
  70. s = tab[b & 0x7];
  71. l = s;
  72. s = tab[b >> 3 & 0x7];
  73. l ^= s << 3;
  74. h = s >> 29;
  75. s = tab[b >> 6 & 0x7];
  76. l ^= s << 6;
  77. h ^= s >> 26;
  78. s = tab[b >> 9 & 0x7];
  79. l ^= s << 9;
  80. h ^= s >> 23;
  81. s = tab[b >> 12 & 0x7];
  82. l ^= s << 12;
  83. h ^= s >> 20;
  84. s = tab[b >> 15 & 0x7];
  85. l ^= s << 15;
  86. h ^= s >> 17;
  87. s = tab[b >> 18 & 0x7];
  88. l ^= s << 18;
  89. h ^= s >> 14;
  90. s = tab[b >> 21 & 0x7];
  91. l ^= s << 21;
  92. h ^= s >> 11;
  93. s = tab[b >> 24 & 0x7];
  94. l ^= s << 24;
  95. h ^= s >> 8;
  96. s = tab[b >> 27 & 0x7];
  97. l ^= s << 27;
  98. h ^= s >> 5;
  99. s = tab[b >> 30];
  100. l ^= s << 30;
  101. h ^= s >> 2;
  102. /* compensate for the top two bits of a */
  103. if (top2b & 01) {
  104. l ^= b << 30;
  105. h ^= b >> 2;
  106. }
  107. if (top2b & 02) {
  108. l ^= b << 31;
  109. h ^= b >> 1;
  110. }
  111. *r1 = h;
  112. *r0 = l;
  113. }
  114. # endif
  115. # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
  116. static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
  117. const BN_ULONG b)
  118. {
  119. register BN_ULONG h, l, s;
  120. BN_ULONG tab[16], top3b = a >> 61;
  121. register BN_ULONG a1, a2, a4, a8;
  122. a1 = a & (0x1FFFFFFFFFFFFFFFULL);
  123. a2 = a1 << 1;
  124. a4 = a2 << 1;
  125. a8 = a4 << 1;
  126. tab[0] = 0;
  127. tab[1] = a1;
  128. tab[2] = a2;
  129. tab[3] = a1 ^ a2;
  130. tab[4] = a4;
  131. tab[5] = a1 ^ a4;
  132. tab[6] = a2 ^ a4;
  133. tab[7] = a1 ^ a2 ^ a4;
  134. tab[8] = a8;
  135. tab[9] = a1 ^ a8;
  136. tab[10] = a2 ^ a8;
  137. tab[11] = a1 ^ a2 ^ a8;
  138. tab[12] = a4 ^ a8;
  139. tab[13] = a1 ^ a4 ^ a8;
  140. tab[14] = a2 ^ a4 ^ a8;
  141. tab[15] = a1 ^ a2 ^ a4 ^ a8;
  142. s = tab[b & 0xF];
  143. l = s;
  144. s = tab[b >> 4 & 0xF];
  145. l ^= s << 4;
  146. h = s >> 60;
  147. s = tab[b >> 8 & 0xF];
  148. l ^= s << 8;
  149. h ^= s >> 56;
  150. s = tab[b >> 12 & 0xF];
  151. l ^= s << 12;
  152. h ^= s >> 52;
  153. s = tab[b >> 16 & 0xF];
  154. l ^= s << 16;
  155. h ^= s >> 48;
  156. s = tab[b >> 20 & 0xF];
  157. l ^= s << 20;
  158. h ^= s >> 44;
  159. s = tab[b >> 24 & 0xF];
  160. l ^= s << 24;
  161. h ^= s >> 40;
  162. s = tab[b >> 28 & 0xF];
  163. l ^= s << 28;
  164. h ^= s >> 36;
  165. s = tab[b >> 32 & 0xF];
  166. l ^= s << 32;
  167. h ^= s >> 32;
  168. s = tab[b >> 36 & 0xF];
  169. l ^= s << 36;
  170. h ^= s >> 28;
  171. s = tab[b >> 40 & 0xF];
  172. l ^= s << 40;
  173. h ^= s >> 24;
  174. s = tab[b >> 44 & 0xF];
  175. l ^= s << 44;
  176. h ^= s >> 20;
  177. s = tab[b >> 48 & 0xF];
  178. l ^= s << 48;
  179. h ^= s >> 16;
  180. s = tab[b >> 52 & 0xF];
  181. l ^= s << 52;
  182. h ^= s >> 12;
  183. s = tab[b >> 56 & 0xF];
  184. l ^= s << 56;
  185. h ^= s >> 8;
  186. s = tab[b >> 60];
  187. l ^= s << 60;
  188. h ^= s >> 4;
  189. /* compensate for the top three bits of a */
  190. if (top3b & 01) {
  191. l ^= b << 61;
  192. h ^= b >> 3;
  193. }
  194. if (top3b & 02) {
  195. l ^= b << 62;
  196. h ^= b >> 2;
  197. }
  198. if (top3b & 04) {
  199. l ^= b << 63;
  200. h ^= b >> 1;
  201. }
  202. *r1 = h;
  203. *r0 = l;
  204. }
  205. # endif
  206. /*
  207. * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
  208. * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
  209. * ensure that the variables have the right amount of space allocated.
  210. */
  211. static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
  212. const BN_ULONG b1, const BN_ULONG b0)
  213. {
  214. BN_ULONG m1, m0;
  215. /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
  216. bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
  217. bn_GF2m_mul_1x1(r + 1, r, a0, b0);
  218. bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
  219. /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
  220. r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
  221. r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
  222. }
  223. # else
  224. void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
  225. BN_ULONG b0);
  226. # endif
  227. /*
  228. * Add polynomials a and b and store result in r; r could be a or b, a and b
  229. * could be equal; r is the bitwise XOR of a and b.
  230. */
  231. int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  232. {
  233. int i;
  234. const BIGNUM *at, *bt;
  235. bn_check_top(a);
  236. bn_check_top(b);
  237. if (a->top < b->top) {
  238. at = b;
  239. bt = a;
  240. } else {
  241. at = a;
  242. bt = b;
  243. }
  244. if (bn_wexpand(r, at->top) == NULL)
  245. return 0;
  246. for (i = 0; i < bt->top; i++) {
  247. r->d[i] = at->d[i] ^ bt->d[i];
  248. }
  249. for (; i < at->top; i++) {
  250. r->d[i] = at->d[i];
  251. }
  252. r->top = at->top;
  253. bn_correct_top(r);
  254. return 1;
  255. }
  256. /*-
  257. * Some functions allow for representation of the irreducible polynomials
  258. * as an int[], say p. The irreducible f(t) is then of the form:
  259. * t^p[0] + t^p[1] + ... + t^p[k]
  260. * where m = p[0] > p[1] > ... > p[k] = 0.
  261. */
  262. /* Performs modular reduction of a and store result in r. r could be a. */
  263. int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
  264. {
  265. int j, k;
  266. int n, dN, d0, d1;
  267. BN_ULONG zz, *z;
  268. bn_check_top(a);
  269. if (p[0] == 0) {
  270. /* reduction mod 1 => return 0 */
  271. BN_zero(r);
  272. return 1;
  273. }
  274. /*
  275. * Since the algorithm does reduction in the r value, if a != r, copy the
  276. * contents of a into r so we can do reduction in r.
  277. */
  278. if (a != r) {
  279. if (!bn_wexpand(r, a->top))
  280. return 0;
  281. for (j = 0; j < a->top; j++) {
  282. r->d[j] = a->d[j];
  283. }
  284. r->top = a->top;
  285. }
  286. z = r->d;
  287. /* start reduction */
  288. dN = p[0] / BN_BITS2;
  289. for (j = r->top - 1; j > dN;) {
  290. zz = z[j];
  291. if (z[j] == 0) {
  292. j--;
  293. continue;
  294. }
  295. z[j] = 0;
  296. for (k = 1; p[k] != 0; k++) {
  297. /* reducing component t^p[k] */
  298. n = p[0] - p[k];
  299. d0 = n % BN_BITS2;
  300. d1 = BN_BITS2 - d0;
  301. n /= BN_BITS2;
  302. z[j - n] ^= (zz >> d0);
  303. if (d0)
  304. z[j - n - 1] ^= (zz << d1);
  305. }
  306. /* reducing component t^0 */
  307. n = dN;
  308. d0 = p[0] % BN_BITS2;
  309. d1 = BN_BITS2 - d0;
  310. z[j - n] ^= (zz >> d0);
  311. if (d0)
  312. z[j - n - 1] ^= (zz << d1);
  313. }
  314. /* final round of reduction */
  315. while (j == dN) {
  316. d0 = p[0] % BN_BITS2;
  317. zz = z[dN] >> d0;
  318. if (zz == 0)
  319. break;
  320. d1 = BN_BITS2 - d0;
  321. /* clear up the top d1 bits */
  322. if (d0)
  323. z[dN] = (z[dN] << d1) >> d1;
  324. else
  325. z[dN] = 0;
  326. z[0] ^= zz; /* reduction t^0 component */
  327. for (k = 1; p[k] != 0; k++) {
  328. BN_ULONG tmp_ulong;
  329. /* reducing component t^p[k] */
  330. n = p[k] / BN_BITS2;
  331. d0 = p[k] % BN_BITS2;
  332. d1 = BN_BITS2 - d0;
  333. z[n] ^= (zz << d0);
  334. if (d0 && (tmp_ulong = zz >> d1))
  335. z[n + 1] ^= tmp_ulong;
  336. }
  337. }
  338. bn_correct_top(r);
  339. return 1;
  340. }
  341. /*
  342. * Performs modular reduction of a by p and store result in r. r could be a.
  343. * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
  344. * function is only provided for convenience; for best performance, use the
  345. * BN_GF2m_mod_arr function.
  346. */
  347. int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
  348. {
  349. int ret = 0;
  350. int arr[6];
  351. bn_check_top(a);
  352. bn_check_top(p);
  353. ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
  354. if (!ret || ret > (int)OSSL_NELEM(arr)) {
  355. ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
  356. return 0;
  357. }
  358. ret = BN_GF2m_mod_arr(r, a, arr);
  359. bn_check_top(r);
  360. return ret;
  361. }
  362. /*
  363. * Compute the product of two polynomials a and b, reduce modulo p, and store
  364. * the result in r. r could be a or b; a could be b.
  365. */
  366. int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  367. const int p[], BN_CTX *ctx)
  368. {
  369. int zlen, i, j, k, ret = 0;
  370. BIGNUM *s;
  371. BN_ULONG x1, x0, y1, y0, zz[4];
  372. bn_check_top(a);
  373. bn_check_top(b);
  374. if (a == b) {
  375. return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
  376. }
  377. BN_CTX_start(ctx);
  378. if ((s = BN_CTX_get(ctx)) == NULL)
  379. goto err;
  380. zlen = a->top + b->top + 4;
  381. if (!bn_wexpand(s, zlen))
  382. goto err;
  383. s->top = zlen;
  384. for (i = 0; i < zlen; i++)
  385. s->d[i] = 0;
  386. for (j = 0; j < b->top; j += 2) {
  387. y0 = b->d[j];
  388. y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
  389. for (i = 0; i < a->top; i += 2) {
  390. x0 = a->d[i];
  391. x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
  392. bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
  393. for (k = 0; k < 4; k++)
  394. s->d[i + j + k] ^= zz[k];
  395. }
  396. }
  397. bn_correct_top(s);
  398. if (BN_GF2m_mod_arr(r, s, p))
  399. ret = 1;
  400. bn_check_top(r);
  401. err:
  402. BN_CTX_end(ctx);
  403. return ret;
  404. }
  405. /*
  406. * Compute the product of two polynomials a and b, reduce modulo p, and store
  407. * the result in r. r could be a or b; a could equal b. This function calls
  408. * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
  409. * only provided for convenience; for best performance, use the
  410. * BN_GF2m_mod_mul_arr function.
  411. */
  412. int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  413. const BIGNUM *p, BN_CTX *ctx)
  414. {
  415. int ret = 0;
  416. const int max = BN_num_bits(p) + 1;
  417. int *arr;
  418. bn_check_top(a);
  419. bn_check_top(b);
  420. bn_check_top(p);
  421. arr = OPENSSL_malloc(sizeof(*arr) * max);
  422. if (arr == NULL) {
  423. ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
  424. return 0;
  425. }
  426. ret = BN_GF2m_poly2arr(p, arr, max);
  427. if (!ret || ret > max) {
  428. ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
  429. goto err;
  430. }
  431. ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
  432. bn_check_top(r);
  433. err:
  434. OPENSSL_free(arr);
  435. return ret;
  436. }
  437. /* Square a, reduce the result mod p, and store it in a. r could be a. */
  438. int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  439. BN_CTX *ctx)
  440. {
  441. int i, ret = 0;
  442. BIGNUM *s;
  443. bn_check_top(a);
  444. BN_CTX_start(ctx);
  445. if ((s = BN_CTX_get(ctx)) == NULL)
  446. goto err;
  447. if (!bn_wexpand(s, 2 * a->top))
  448. goto err;
  449. for (i = a->top - 1; i >= 0; i--) {
  450. s->d[2 * i + 1] = SQR1(a->d[i]);
  451. s->d[2 * i] = SQR0(a->d[i]);
  452. }
  453. s->top = 2 * a->top;
  454. bn_correct_top(s);
  455. if (!BN_GF2m_mod_arr(r, s, p))
  456. goto err;
  457. bn_check_top(r);
  458. ret = 1;
  459. err:
  460. BN_CTX_end(ctx);
  461. return ret;
  462. }
  463. /*
  464. * Square a, reduce the result mod p, and store it in a. r could be a. This
  465. * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
  466. * wrapper function is only provided for convenience; for best performance,
  467. * use the BN_GF2m_mod_sqr_arr function.
  468. */
  469. int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  470. {
  471. int ret = 0;
  472. const int max = BN_num_bits(p) + 1;
  473. int *arr;
  474. bn_check_top(a);
  475. bn_check_top(p);
  476. arr = OPENSSL_malloc(sizeof(*arr) * max);
  477. if (arr == NULL) {
  478. ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
  479. return 0;
  480. }
  481. ret = BN_GF2m_poly2arr(p, arr, max);
  482. if (!ret || ret > max) {
  483. ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
  484. goto err;
  485. }
  486. ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
  487. bn_check_top(r);
  488. err:
  489. OPENSSL_free(arr);
  490. return ret;
  491. }
  492. /*
  493. * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
  494. * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
  495. * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
  496. * Curve Cryptography Over Binary Fields".
  497. */
  498. static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
  499. const BIGNUM *p, BN_CTX *ctx)
  500. {
  501. BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
  502. int ret = 0;
  503. bn_check_top(a);
  504. bn_check_top(p);
  505. BN_CTX_start(ctx);
  506. b = BN_CTX_get(ctx);
  507. c = BN_CTX_get(ctx);
  508. u = BN_CTX_get(ctx);
  509. v = BN_CTX_get(ctx);
  510. if (v == NULL)
  511. goto err;
  512. if (!BN_GF2m_mod(u, a, p))
  513. goto err;
  514. if (BN_is_zero(u))
  515. goto err;
  516. if (!BN_copy(v, p))
  517. goto err;
  518. # if 0
  519. if (!BN_one(b))
  520. goto err;
  521. while (1) {
  522. while (!BN_is_odd(u)) {
  523. if (BN_is_zero(u))
  524. goto err;
  525. if (!BN_rshift1(u, u))
  526. goto err;
  527. if (BN_is_odd(b)) {
  528. if (!BN_GF2m_add(b, b, p))
  529. goto err;
  530. }
  531. if (!BN_rshift1(b, b))
  532. goto err;
  533. }
  534. if (BN_abs_is_word(u, 1))
  535. break;
  536. if (BN_num_bits(u) < BN_num_bits(v)) {
  537. tmp = u;
  538. u = v;
  539. v = tmp;
  540. tmp = b;
  541. b = c;
  542. c = tmp;
  543. }
  544. if (!BN_GF2m_add(u, u, v))
  545. goto err;
  546. if (!BN_GF2m_add(b, b, c))
  547. goto err;
  548. }
  549. # else
  550. {
  551. int i;
  552. int ubits = BN_num_bits(u);
  553. int vbits = BN_num_bits(v); /* v is copy of p */
  554. int top = p->top;
  555. BN_ULONG *udp, *bdp, *vdp, *cdp;
  556. if (!bn_wexpand(u, top))
  557. goto err;
  558. udp = u->d;
  559. for (i = u->top; i < top; i++)
  560. udp[i] = 0;
  561. u->top = top;
  562. if (!bn_wexpand(b, top))
  563. goto err;
  564. bdp = b->d;
  565. bdp[0] = 1;
  566. for (i = 1; i < top; i++)
  567. bdp[i] = 0;
  568. b->top = top;
  569. if (!bn_wexpand(c, top))
  570. goto err;
  571. cdp = c->d;
  572. for (i = 0; i < top; i++)
  573. cdp[i] = 0;
  574. c->top = top;
  575. vdp = v->d; /* It pays off to "cache" *->d pointers,
  576. * because it allows optimizer to be more
  577. * aggressive. But we don't have to "cache"
  578. * p->d, because *p is declared 'const'... */
  579. while (1) {
  580. while (ubits && !(udp[0] & 1)) {
  581. BN_ULONG u0, u1, b0, b1, mask;
  582. u0 = udp[0];
  583. b0 = bdp[0];
  584. mask = (BN_ULONG)0 - (b0 & 1);
  585. b0 ^= p->d[0] & mask;
  586. for (i = 0; i < top - 1; i++) {
  587. u1 = udp[i + 1];
  588. udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
  589. u0 = u1;
  590. b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
  591. bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
  592. b0 = b1;
  593. }
  594. udp[i] = u0 >> 1;
  595. bdp[i] = b0 >> 1;
  596. ubits--;
  597. }
  598. if (ubits <= BN_BITS2) {
  599. if (udp[0] == 0) /* poly was reducible */
  600. goto err;
  601. if (udp[0] == 1)
  602. break;
  603. }
  604. if (ubits < vbits) {
  605. i = ubits;
  606. ubits = vbits;
  607. vbits = i;
  608. tmp = u;
  609. u = v;
  610. v = tmp;
  611. tmp = b;
  612. b = c;
  613. c = tmp;
  614. udp = vdp;
  615. vdp = v->d;
  616. bdp = cdp;
  617. cdp = c->d;
  618. }
  619. for (i = 0; i < top; i++) {
  620. udp[i] ^= vdp[i];
  621. bdp[i] ^= cdp[i];
  622. }
  623. if (ubits == vbits) {
  624. BN_ULONG ul;
  625. int utop = (ubits - 1) / BN_BITS2;
  626. while ((ul = udp[utop]) == 0 && utop)
  627. utop--;
  628. ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
  629. }
  630. }
  631. bn_correct_top(b);
  632. }
  633. # endif
  634. if (!BN_copy(r, b))
  635. goto err;
  636. bn_check_top(r);
  637. ret = 1;
  638. err:
  639. # ifdef BN_DEBUG
  640. /* BN_CTX_end would complain about the expanded form */
  641. bn_correct_top(c);
  642. bn_correct_top(u);
  643. bn_correct_top(v);
  644. # endif
  645. BN_CTX_end(ctx);
  646. return ret;
  647. }
  648. /*-
  649. * Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
  650. * This is not constant time.
  651. * But it does eliminate first order deduction on the input.
  652. */
  653. int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  654. {
  655. BIGNUM *b = NULL;
  656. int ret = 0;
  657. BN_CTX_start(ctx);
  658. if ((b = BN_CTX_get(ctx)) == NULL)
  659. goto err;
  660. /* generate blinding value */
  661. do {
  662. if (!BN_priv_rand_ex(b, BN_num_bits(p) - 1,
  663. BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
  664. goto err;
  665. } while (BN_is_zero(b));
  666. /* r := a * b */
  667. if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
  668. goto err;
  669. /* r := 1/(a * b) */
  670. if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
  671. goto err;
  672. /* r := b/(a * b) = 1/a */
  673. if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
  674. goto err;
  675. ret = 1;
  676. err:
  677. BN_CTX_end(ctx);
  678. return ret;
  679. }
  680. /*
  681. * Invert xx, reduce modulo p, and store the result in r. r could be xx.
  682. * This function calls down to the BN_GF2m_mod_inv implementation; this
  683. * wrapper function is only provided for convenience; for best performance,
  684. * use the BN_GF2m_mod_inv function.
  685. */
  686. int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
  687. BN_CTX *ctx)
  688. {
  689. BIGNUM *field;
  690. int ret = 0;
  691. bn_check_top(xx);
  692. BN_CTX_start(ctx);
  693. if ((field = BN_CTX_get(ctx)) == NULL)
  694. goto err;
  695. if (!BN_GF2m_arr2poly(p, field))
  696. goto err;
  697. ret = BN_GF2m_mod_inv(r, xx, field, ctx);
  698. bn_check_top(r);
  699. err:
  700. BN_CTX_end(ctx);
  701. return ret;
  702. }
  703. /*
  704. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  705. * or y, x could equal y.
  706. */
  707. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  708. const BIGNUM *p, BN_CTX *ctx)
  709. {
  710. BIGNUM *xinv = NULL;
  711. int ret = 0;
  712. bn_check_top(y);
  713. bn_check_top(x);
  714. bn_check_top(p);
  715. BN_CTX_start(ctx);
  716. xinv = BN_CTX_get(ctx);
  717. if (xinv == NULL)
  718. goto err;
  719. if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
  720. goto err;
  721. if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
  722. goto err;
  723. bn_check_top(r);
  724. ret = 1;
  725. err:
  726. BN_CTX_end(ctx);
  727. return ret;
  728. }
  729. /*
  730. * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
  731. * * or yy, xx could equal yy. This function calls down to the
  732. * BN_GF2m_mod_div implementation; this wrapper function is only provided for
  733. * convenience; for best performance, use the BN_GF2m_mod_div function.
  734. */
  735. int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
  736. const int p[], BN_CTX *ctx)
  737. {
  738. BIGNUM *field;
  739. int ret = 0;
  740. bn_check_top(yy);
  741. bn_check_top(xx);
  742. BN_CTX_start(ctx);
  743. if ((field = BN_CTX_get(ctx)) == NULL)
  744. goto err;
  745. if (!BN_GF2m_arr2poly(p, field))
  746. goto err;
  747. ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
  748. bn_check_top(r);
  749. err:
  750. BN_CTX_end(ctx);
  751. return ret;
  752. }
  753. /*
  754. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  755. * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
  756. * P1363.
  757. */
  758. int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  759. const int p[], BN_CTX *ctx)
  760. {
  761. int ret = 0, i, n;
  762. BIGNUM *u;
  763. bn_check_top(a);
  764. bn_check_top(b);
  765. if (BN_is_zero(b))
  766. return BN_one(r);
  767. if (BN_abs_is_word(b, 1))
  768. return (BN_copy(r, a) != NULL);
  769. BN_CTX_start(ctx);
  770. if ((u = BN_CTX_get(ctx)) == NULL)
  771. goto err;
  772. if (!BN_GF2m_mod_arr(u, a, p))
  773. goto err;
  774. n = BN_num_bits(b) - 1;
  775. for (i = n - 1; i >= 0; i--) {
  776. if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
  777. goto err;
  778. if (BN_is_bit_set(b, i)) {
  779. if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
  780. goto err;
  781. }
  782. }
  783. if (!BN_copy(r, u))
  784. goto err;
  785. bn_check_top(r);
  786. ret = 1;
  787. err:
  788. BN_CTX_end(ctx);
  789. return ret;
  790. }
  791. /*
  792. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  793. * could be a. This function calls down to the BN_GF2m_mod_exp_arr
  794. * implementation; this wrapper function is only provided for convenience;
  795. * for best performance, use the BN_GF2m_mod_exp_arr function.
  796. */
  797. int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  798. const BIGNUM *p, BN_CTX *ctx)
  799. {
  800. int ret = 0;
  801. const int max = BN_num_bits(p) + 1;
  802. int *arr;
  803. bn_check_top(a);
  804. bn_check_top(b);
  805. bn_check_top(p);
  806. arr = OPENSSL_malloc(sizeof(*arr) * max);
  807. if (arr == NULL) {
  808. ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
  809. return 0;
  810. }
  811. ret = BN_GF2m_poly2arr(p, arr, max);
  812. if (!ret || ret > max) {
  813. ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
  814. goto err;
  815. }
  816. ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
  817. bn_check_top(r);
  818. err:
  819. OPENSSL_free(arr);
  820. return ret;
  821. }
  822. /*
  823. * Compute the square root of a, reduce modulo p, and store the result in r.
  824. * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
  825. */
  826. int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  827. BN_CTX *ctx)
  828. {
  829. int ret = 0;
  830. BIGNUM *u;
  831. bn_check_top(a);
  832. if (p[0] == 0) {
  833. /* reduction mod 1 => return 0 */
  834. BN_zero(r);
  835. return 1;
  836. }
  837. BN_CTX_start(ctx);
  838. if ((u = BN_CTX_get(ctx)) == NULL)
  839. goto err;
  840. if (!BN_set_bit(u, p[0] - 1))
  841. goto err;
  842. ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
  843. bn_check_top(r);
  844. err:
  845. BN_CTX_end(ctx);
  846. return ret;
  847. }
  848. /*
  849. * Compute the square root of a, reduce modulo p, and store the result in r.
  850. * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
  851. * implementation; this wrapper function is only provided for convenience;
  852. * for best performance, use the BN_GF2m_mod_sqrt_arr function.
  853. */
  854. int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  855. {
  856. int ret = 0;
  857. const int max = BN_num_bits(p) + 1;
  858. int *arr;
  859. bn_check_top(a);
  860. bn_check_top(p);
  861. arr = OPENSSL_malloc(sizeof(*arr) * max);
  862. if (arr == NULL) {
  863. ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
  864. return 0;
  865. }
  866. ret = BN_GF2m_poly2arr(p, arr, max);
  867. if (!ret || ret > max) {
  868. ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
  869. goto err;
  870. }
  871. ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
  872. bn_check_top(r);
  873. err:
  874. OPENSSL_free(arr);
  875. return ret;
  876. }
  877. /*
  878. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  879. * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
  880. */
  881. int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
  882. BN_CTX *ctx)
  883. {
  884. int ret = 0, count = 0, j;
  885. BIGNUM *a, *z, *rho, *w, *w2, *tmp;
  886. bn_check_top(a_);
  887. if (p[0] == 0) {
  888. /* reduction mod 1 => return 0 */
  889. BN_zero(r);
  890. return 1;
  891. }
  892. BN_CTX_start(ctx);
  893. a = BN_CTX_get(ctx);
  894. z = BN_CTX_get(ctx);
  895. w = BN_CTX_get(ctx);
  896. if (w == NULL)
  897. goto err;
  898. if (!BN_GF2m_mod_arr(a, a_, p))
  899. goto err;
  900. if (BN_is_zero(a)) {
  901. BN_zero(r);
  902. ret = 1;
  903. goto err;
  904. }
  905. if (p[0] & 0x1) { /* m is odd */
  906. /* compute half-trace of a */
  907. if (!BN_copy(z, a))
  908. goto err;
  909. for (j = 1; j <= (p[0] - 1) / 2; j++) {
  910. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  911. goto err;
  912. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  913. goto err;
  914. if (!BN_GF2m_add(z, z, a))
  915. goto err;
  916. }
  917. } else { /* m is even */
  918. rho = BN_CTX_get(ctx);
  919. w2 = BN_CTX_get(ctx);
  920. tmp = BN_CTX_get(ctx);
  921. if (tmp == NULL)
  922. goto err;
  923. do {
  924. if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
  925. 0, ctx))
  926. goto err;
  927. if (!BN_GF2m_mod_arr(rho, rho, p))
  928. goto err;
  929. BN_zero(z);
  930. if (!BN_copy(w, rho))
  931. goto err;
  932. for (j = 1; j <= p[0] - 1; j++) {
  933. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  934. goto err;
  935. if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
  936. goto err;
  937. if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
  938. goto err;
  939. if (!BN_GF2m_add(z, z, tmp))
  940. goto err;
  941. if (!BN_GF2m_add(w, w2, rho))
  942. goto err;
  943. }
  944. count++;
  945. } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
  946. if (BN_is_zero(w)) {
  947. ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
  948. goto err;
  949. }
  950. }
  951. if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
  952. goto err;
  953. if (!BN_GF2m_add(w, z, w))
  954. goto err;
  955. if (BN_GF2m_cmp(w, a)) {
  956. ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
  957. goto err;
  958. }
  959. if (!BN_copy(r, z))
  960. goto err;
  961. bn_check_top(r);
  962. ret = 1;
  963. err:
  964. BN_CTX_end(ctx);
  965. return ret;
  966. }
  967. /*
  968. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  969. * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
  970. * implementation; this wrapper function is only provided for convenience;
  971. * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
  972. */
  973. int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  974. BN_CTX *ctx)
  975. {
  976. int ret = 0;
  977. const int max = BN_num_bits(p) + 1;
  978. int *arr;
  979. bn_check_top(a);
  980. bn_check_top(p);
  981. arr = OPENSSL_malloc(sizeof(*arr) * max);
  982. if (arr == NULL) {
  983. ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
  984. goto err;
  985. }
  986. ret = BN_GF2m_poly2arr(p, arr, max);
  987. if (!ret || ret > max) {
  988. ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
  989. goto err;
  990. }
  991. ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
  992. bn_check_top(r);
  993. err:
  994. OPENSSL_free(arr);
  995. return ret;
  996. }
  997. /*
  998. * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
  999. * x^i) into an array of integers corresponding to the bits with non-zero
  1000. * coefficient. Array is terminated with -1. Up to max elements of the array
  1001. * will be filled. Return value is total number of array elements that would
  1002. * be filled if array was large enough.
  1003. */
  1004. int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
  1005. {
  1006. int i, j, k = 0;
  1007. BN_ULONG mask;
  1008. if (BN_is_zero(a))
  1009. return 0;
  1010. for (i = a->top - 1; i >= 0; i--) {
  1011. if (!a->d[i])
  1012. /* skip word if a->d[i] == 0 */
  1013. continue;
  1014. mask = BN_TBIT;
  1015. for (j = BN_BITS2 - 1; j >= 0; j--) {
  1016. if (a->d[i] & mask) {
  1017. if (k < max)
  1018. p[k] = BN_BITS2 * i + j;
  1019. k++;
  1020. }
  1021. mask >>= 1;
  1022. }
  1023. }
  1024. if (k < max) {
  1025. p[k] = -1;
  1026. k++;
  1027. }
  1028. return k;
  1029. }
  1030. /*
  1031. * Convert the coefficient array representation of a polynomial to a
  1032. * bit-string. The array must be terminated by -1.
  1033. */
  1034. int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
  1035. {
  1036. int i;
  1037. bn_check_top(a);
  1038. BN_zero(a);
  1039. for (i = 0; p[i] != -1; i++) {
  1040. if (BN_set_bit(a, p[i]) == 0)
  1041. return 0;
  1042. }
  1043. bn_check_top(a);
  1044. return 1;
  1045. }
  1046. #endif