bn_gf2m.c 34 KB

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