bn_gf2m.c 31 KB

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