bn_gf2m.c 30 KB

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