bn_gf2m.c 34 KB

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