bn_gf2m.c 29 KB

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