bn_gf2m.c 30 KB

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