2
0

bn_gf2m.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 "internal/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, OSSL_NELEM(arr));
  432. if (!ret || ret > (int)OSSL_NELEM(arr)) {
  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 = OPENSSL_malloc(sizeof(*arr) * 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. OPENSSL_free(arr);
  510. return ret;
  511. }
  512. /* Square a, reduce the result mod p, and store it in a. r could be a. */
  513. int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  514. BN_CTX *ctx)
  515. {
  516. int i, ret = 0;
  517. BIGNUM *s;
  518. bn_check_top(a);
  519. BN_CTX_start(ctx);
  520. if ((s = BN_CTX_get(ctx)) == NULL)
  521. goto err;
  522. if (!bn_wexpand(s, 2 * a->top))
  523. goto err;
  524. for (i = a->top - 1; i >= 0; i--) {
  525. s->d[2 * i + 1] = SQR1(a->d[i]);
  526. s->d[2 * i] = SQR0(a->d[i]);
  527. }
  528. s->top = 2 * a->top;
  529. bn_correct_top(s);
  530. if (!BN_GF2m_mod_arr(r, s, p))
  531. goto err;
  532. bn_check_top(r);
  533. ret = 1;
  534. err:
  535. BN_CTX_end(ctx);
  536. return ret;
  537. }
  538. /*
  539. * Square a, reduce the result mod p, and store it in a. r could be a. This
  540. * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
  541. * wrapper function is only provided for convenience; for best performance,
  542. * use the BN_GF2m_mod_sqr_arr function.
  543. */
  544. int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  545. {
  546. int ret = 0;
  547. const int max = BN_num_bits(p) + 1;
  548. int *arr = NULL;
  549. bn_check_top(a);
  550. bn_check_top(p);
  551. if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
  552. goto err;
  553. ret = BN_GF2m_poly2arr(p, arr, max);
  554. if (!ret || ret > max) {
  555. BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);
  556. goto err;
  557. }
  558. ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
  559. bn_check_top(r);
  560. err:
  561. OPENSSL_free(arr);
  562. return ret;
  563. }
  564. /*
  565. * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
  566. * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
  567. * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
  568. * Curve Cryptography Over Binary Fields".
  569. */
  570. int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  571. {
  572. BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
  573. int ret = 0;
  574. bn_check_top(a);
  575. bn_check_top(p);
  576. BN_CTX_start(ctx);
  577. if ((b = BN_CTX_get(ctx)) == NULL)
  578. goto err;
  579. if ((c = BN_CTX_get(ctx)) == NULL)
  580. goto err;
  581. if ((u = BN_CTX_get(ctx)) == NULL)
  582. goto err;
  583. if ((v = BN_CTX_get(ctx)) == NULL)
  584. goto err;
  585. if (!BN_GF2m_mod(u, a, p))
  586. goto err;
  587. if (BN_is_zero(u))
  588. goto err;
  589. if (!BN_copy(v, p))
  590. goto err;
  591. # if 0
  592. if (!BN_one(b))
  593. goto err;
  594. while (1) {
  595. while (!BN_is_odd(u)) {
  596. if (BN_is_zero(u))
  597. goto err;
  598. if (!BN_rshift1(u, u))
  599. goto err;
  600. if (BN_is_odd(b)) {
  601. if (!BN_GF2m_add(b, b, p))
  602. goto err;
  603. }
  604. if (!BN_rshift1(b, b))
  605. goto err;
  606. }
  607. if (BN_abs_is_word(u, 1))
  608. break;
  609. if (BN_num_bits(u) < BN_num_bits(v)) {
  610. tmp = u;
  611. u = v;
  612. v = tmp;
  613. tmp = b;
  614. b = c;
  615. c = tmp;
  616. }
  617. if (!BN_GF2m_add(u, u, v))
  618. goto err;
  619. if (!BN_GF2m_add(b, b, c))
  620. goto err;
  621. }
  622. # else
  623. {
  624. int i;
  625. int ubits = BN_num_bits(u);
  626. int vbits = BN_num_bits(v); /* v is copy of p */
  627. int top = p->top;
  628. BN_ULONG *udp, *bdp, *vdp, *cdp;
  629. if (!bn_wexpand(u, top))
  630. goto err;
  631. udp = u->d;
  632. for (i = u->top; i < top; i++)
  633. udp[i] = 0;
  634. u->top = top;
  635. if (!bn_wexpand(b, top))
  636. goto err;
  637. bdp = b->d;
  638. bdp[0] = 1;
  639. for (i = 1; i < top; i++)
  640. bdp[i] = 0;
  641. b->top = top;
  642. if (!bn_wexpand(c, top))
  643. goto err;
  644. cdp = c->d;
  645. for (i = 0; i < top; i++)
  646. cdp[i] = 0;
  647. c->top = top;
  648. vdp = v->d; /* It pays off to "cache" *->d pointers,
  649. * because it allows optimizer to be more
  650. * aggressive. But we don't have to "cache"
  651. * p->d, because *p is declared 'const'... */
  652. while (1) {
  653. while (ubits && !(udp[0] & 1)) {
  654. BN_ULONG u0, u1, b0, b1, mask;
  655. u0 = udp[0];
  656. b0 = bdp[0];
  657. mask = (BN_ULONG)0 - (b0 & 1);
  658. b0 ^= p->d[0] & mask;
  659. for (i = 0; i < top - 1; i++) {
  660. u1 = udp[i + 1];
  661. udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
  662. u0 = u1;
  663. b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
  664. bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
  665. b0 = b1;
  666. }
  667. udp[i] = u0 >> 1;
  668. bdp[i] = b0 >> 1;
  669. ubits--;
  670. }
  671. if (ubits <= BN_BITS2) {
  672. if (udp[0] == 0) /* poly was reducible */
  673. goto err;
  674. if (udp[0] == 1)
  675. break;
  676. }
  677. if (ubits < vbits) {
  678. i = ubits;
  679. ubits = vbits;
  680. vbits = i;
  681. tmp = u;
  682. u = v;
  683. v = tmp;
  684. tmp = b;
  685. b = c;
  686. c = tmp;
  687. udp = vdp;
  688. vdp = v->d;
  689. bdp = cdp;
  690. cdp = c->d;
  691. }
  692. for (i = 0; i < top; i++) {
  693. udp[i] ^= vdp[i];
  694. bdp[i] ^= cdp[i];
  695. }
  696. if (ubits == vbits) {
  697. BN_ULONG ul;
  698. int utop = (ubits - 1) / BN_BITS2;
  699. while ((ul = udp[utop]) == 0 && utop)
  700. utop--;
  701. ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
  702. }
  703. }
  704. bn_correct_top(b);
  705. }
  706. # endif
  707. if (!BN_copy(r, b))
  708. goto err;
  709. bn_check_top(r);
  710. ret = 1;
  711. err:
  712. # ifdef BN_DEBUG /* BN_CTX_end would complain about the
  713. * expanded form */
  714. bn_correct_top(c);
  715. bn_correct_top(u);
  716. bn_correct_top(v);
  717. # endif
  718. BN_CTX_end(ctx);
  719. return ret;
  720. }
  721. /*
  722. * Invert xx, reduce modulo p, and store the result in r. r could be xx.
  723. * This function calls down to the BN_GF2m_mod_inv implementation; this
  724. * wrapper function is only provided for convenience; for best performance,
  725. * use the BN_GF2m_mod_inv function.
  726. */
  727. int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
  728. BN_CTX *ctx)
  729. {
  730. BIGNUM *field;
  731. int ret = 0;
  732. bn_check_top(xx);
  733. BN_CTX_start(ctx);
  734. if ((field = BN_CTX_get(ctx)) == NULL)
  735. goto err;
  736. if (!BN_GF2m_arr2poly(p, field))
  737. goto err;
  738. ret = BN_GF2m_mod_inv(r, xx, field, ctx);
  739. bn_check_top(r);
  740. err:
  741. BN_CTX_end(ctx);
  742. return ret;
  743. }
  744. # ifndef OPENSSL_SUN_GF2M_DIV
  745. /*
  746. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  747. * or y, x could equal y.
  748. */
  749. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  750. const BIGNUM *p, BN_CTX *ctx)
  751. {
  752. BIGNUM *xinv = NULL;
  753. int ret = 0;
  754. bn_check_top(y);
  755. bn_check_top(x);
  756. bn_check_top(p);
  757. BN_CTX_start(ctx);
  758. xinv = BN_CTX_get(ctx);
  759. if (xinv == NULL)
  760. goto err;
  761. if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
  762. goto err;
  763. if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
  764. goto err;
  765. bn_check_top(r);
  766. ret = 1;
  767. err:
  768. BN_CTX_end(ctx);
  769. return ret;
  770. }
  771. # else
  772. /*
  773. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  774. * or y, x could equal y. Uses algorithm Modular_Division_GF(2^m) from
  775. * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to the
  776. * Great Divide".
  777. */
  778. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  779. const BIGNUM *p, BN_CTX *ctx)
  780. {
  781. BIGNUM *a, *b, *u, *v;
  782. int ret = 0;
  783. bn_check_top(y);
  784. bn_check_top(x);
  785. bn_check_top(p);
  786. BN_CTX_start(ctx);
  787. a = BN_CTX_get(ctx);
  788. b = BN_CTX_get(ctx);
  789. u = BN_CTX_get(ctx);
  790. v = BN_CTX_get(ctx);
  791. if (v == NULL)
  792. goto err;
  793. /* reduce x and y mod p */
  794. if (!BN_GF2m_mod(u, y, p))
  795. goto err;
  796. if (!BN_GF2m_mod(a, x, p))
  797. goto err;
  798. if (!BN_copy(b, p))
  799. goto err;
  800. while (!BN_is_odd(a)) {
  801. if (!BN_rshift1(a, a))
  802. goto err;
  803. if (BN_is_odd(u))
  804. if (!BN_GF2m_add(u, u, p))
  805. goto err;
  806. if (!BN_rshift1(u, u))
  807. goto err;
  808. }
  809. do {
  810. if (BN_GF2m_cmp(b, a) > 0) {
  811. if (!BN_GF2m_add(b, b, a))
  812. goto err;
  813. if (!BN_GF2m_add(v, v, u))
  814. goto err;
  815. do {
  816. if (!BN_rshift1(b, b))
  817. goto err;
  818. if (BN_is_odd(v))
  819. if (!BN_GF2m_add(v, v, p))
  820. goto err;
  821. if (!BN_rshift1(v, v))
  822. goto err;
  823. } while (!BN_is_odd(b));
  824. } else if (BN_abs_is_word(a, 1))
  825. break;
  826. else {
  827. if (!BN_GF2m_add(a, a, b))
  828. goto err;
  829. if (!BN_GF2m_add(u, u, v))
  830. goto err;
  831. do {
  832. if (!BN_rshift1(a, a))
  833. goto err;
  834. if (BN_is_odd(u))
  835. if (!BN_GF2m_add(u, u, p))
  836. goto err;
  837. if (!BN_rshift1(u, u))
  838. goto err;
  839. } while (!BN_is_odd(a));
  840. }
  841. } while (1);
  842. if (!BN_copy(r, u))
  843. goto err;
  844. bn_check_top(r);
  845. ret = 1;
  846. err:
  847. BN_CTX_end(ctx);
  848. return ret;
  849. }
  850. # endif
  851. /*
  852. * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
  853. * * or yy, xx could equal yy. This function calls down to the
  854. * BN_GF2m_mod_div implementation; this wrapper function is only provided for
  855. * convenience; for best performance, use the BN_GF2m_mod_div function.
  856. */
  857. int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
  858. const int p[], BN_CTX *ctx)
  859. {
  860. BIGNUM *field;
  861. int ret = 0;
  862. bn_check_top(yy);
  863. bn_check_top(xx);
  864. BN_CTX_start(ctx);
  865. if ((field = BN_CTX_get(ctx)) == NULL)
  866. goto err;
  867. if (!BN_GF2m_arr2poly(p, field))
  868. goto err;
  869. ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
  870. bn_check_top(r);
  871. err:
  872. BN_CTX_end(ctx);
  873. return ret;
  874. }
  875. /*
  876. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  877. * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
  878. * P1363.
  879. */
  880. int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  881. const int p[], BN_CTX *ctx)
  882. {
  883. int ret = 0, i, n;
  884. BIGNUM *u;
  885. bn_check_top(a);
  886. bn_check_top(b);
  887. if (BN_is_zero(b))
  888. return (BN_one(r));
  889. if (BN_abs_is_word(b, 1))
  890. return (BN_copy(r, a) != NULL);
  891. BN_CTX_start(ctx);
  892. if ((u = BN_CTX_get(ctx)) == NULL)
  893. goto err;
  894. if (!BN_GF2m_mod_arr(u, a, p))
  895. goto err;
  896. n = BN_num_bits(b) - 1;
  897. for (i = n - 1; i >= 0; i--) {
  898. if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
  899. goto err;
  900. if (BN_is_bit_set(b, i)) {
  901. if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
  902. goto err;
  903. }
  904. }
  905. if (!BN_copy(r, u))
  906. goto err;
  907. bn_check_top(r);
  908. ret = 1;
  909. err:
  910. BN_CTX_end(ctx);
  911. return ret;
  912. }
  913. /*
  914. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  915. * could be a. This function calls down to the BN_GF2m_mod_exp_arr
  916. * implementation; this wrapper function is only provided for convenience;
  917. * for best performance, use the BN_GF2m_mod_exp_arr function.
  918. */
  919. int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  920. const BIGNUM *p, BN_CTX *ctx)
  921. {
  922. int ret = 0;
  923. const int max = BN_num_bits(p) + 1;
  924. int *arr = NULL;
  925. bn_check_top(a);
  926. bn_check_top(b);
  927. bn_check_top(p);
  928. if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
  929. goto err;
  930. ret = BN_GF2m_poly2arr(p, arr, max);
  931. if (!ret || ret > max) {
  932. BNerr(BN_F_BN_GF2M_MOD_EXP, BN_R_INVALID_LENGTH);
  933. goto err;
  934. }
  935. ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
  936. bn_check_top(r);
  937. err:
  938. OPENSSL_free(arr);
  939. return ret;
  940. }
  941. /*
  942. * Compute the square root of a, reduce modulo p, and store the result in r.
  943. * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
  944. */
  945. int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  946. BN_CTX *ctx)
  947. {
  948. int ret = 0;
  949. BIGNUM *u;
  950. bn_check_top(a);
  951. if (!p[0]) {
  952. /* reduction mod 1 => return 0 */
  953. BN_zero(r);
  954. return 1;
  955. }
  956. BN_CTX_start(ctx);
  957. if ((u = BN_CTX_get(ctx)) == NULL)
  958. goto err;
  959. if (!BN_set_bit(u, p[0] - 1))
  960. goto err;
  961. ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
  962. bn_check_top(r);
  963. err:
  964. BN_CTX_end(ctx);
  965. return ret;
  966. }
  967. /*
  968. * Compute the square root of a, reduce modulo p, and store the result in r.
  969. * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
  970. * implementation; this wrapper function is only provided for convenience;
  971. * for best performance, use the BN_GF2m_mod_sqrt_arr function.
  972. */
  973. int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  974. {
  975. int ret = 0;
  976. const int max = BN_num_bits(p) + 1;
  977. int *arr = NULL;
  978. bn_check_top(a);
  979. bn_check_top(p);
  980. if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
  981. goto err;
  982. ret = BN_GF2m_poly2arr(p, arr, max);
  983. if (!ret || ret > max) {
  984. BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);
  985. goto err;
  986. }
  987. ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
  988. bn_check_top(r);
  989. err:
  990. OPENSSL_free(arr);
  991. return ret;
  992. }
  993. /*
  994. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  995. * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
  996. */
  997. int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
  998. BN_CTX *ctx)
  999. {
  1000. int ret = 0, count = 0, j;
  1001. BIGNUM *a, *z, *rho, *w, *w2, *tmp;
  1002. bn_check_top(a_);
  1003. if (!p[0]) {
  1004. /* reduction mod 1 => return 0 */
  1005. BN_zero(r);
  1006. return 1;
  1007. }
  1008. BN_CTX_start(ctx);
  1009. a = BN_CTX_get(ctx);
  1010. z = BN_CTX_get(ctx);
  1011. w = BN_CTX_get(ctx);
  1012. if (w == NULL)
  1013. goto err;
  1014. if (!BN_GF2m_mod_arr(a, a_, p))
  1015. goto err;
  1016. if (BN_is_zero(a)) {
  1017. BN_zero(r);
  1018. ret = 1;
  1019. goto err;
  1020. }
  1021. if (p[0] & 0x1) { /* m is odd */
  1022. /* compute half-trace of a */
  1023. if (!BN_copy(z, a))
  1024. goto err;
  1025. for (j = 1; j <= (p[0] - 1) / 2; j++) {
  1026. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1027. goto err;
  1028. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1029. goto err;
  1030. if (!BN_GF2m_add(z, z, a))
  1031. goto err;
  1032. }
  1033. } else { /* m is even */
  1034. rho = BN_CTX_get(ctx);
  1035. w2 = BN_CTX_get(ctx);
  1036. tmp = BN_CTX_get(ctx);
  1037. if (tmp == NULL)
  1038. goto err;
  1039. do {
  1040. if (!BN_rand(rho, p[0], 0, 0))
  1041. goto err;
  1042. if (!BN_GF2m_mod_arr(rho, rho, p))
  1043. goto err;
  1044. BN_zero(z);
  1045. if (!BN_copy(w, rho))
  1046. goto err;
  1047. for (j = 1; j <= p[0] - 1; j++) {
  1048. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1049. goto err;
  1050. if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
  1051. goto err;
  1052. if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
  1053. goto err;
  1054. if (!BN_GF2m_add(z, z, tmp))
  1055. goto err;
  1056. if (!BN_GF2m_add(w, w2, rho))
  1057. goto err;
  1058. }
  1059. count++;
  1060. } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
  1061. if (BN_is_zero(w)) {
  1062. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);
  1063. goto err;
  1064. }
  1065. }
  1066. if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
  1067. goto err;
  1068. if (!BN_GF2m_add(w, z, w))
  1069. goto err;
  1070. if (BN_GF2m_cmp(w, a)) {
  1071. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
  1072. goto err;
  1073. }
  1074. if (!BN_copy(r, z))
  1075. goto err;
  1076. bn_check_top(r);
  1077. ret = 1;
  1078. err:
  1079. BN_CTX_end(ctx);
  1080. return ret;
  1081. }
  1082. /*
  1083. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  1084. * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
  1085. * implementation; this wrapper function is only provided for convenience;
  1086. * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
  1087. */
  1088. int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  1089. BN_CTX *ctx)
  1090. {
  1091. int ret = 0;
  1092. const int max = BN_num_bits(p) + 1;
  1093. int *arr = NULL;
  1094. bn_check_top(a);
  1095. bn_check_top(p);
  1096. if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
  1097. goto err;
  1098. ret = BN_GF2m_poly2arr(p, arr, max);
  1099. if (!ret || ret > max) {
  1100. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);
  1101. goto err;
  1102. }
  1103. ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
  1104. bn_check_top(r);
  1105. err:
  1106. OPENSSL_free(arr);
  1107. return ret;
  1108. }
  1109. /*
  1110. * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
  1111. * x^i) into an array of integers corresponding to the bits with non-zero
  1112. * coefficient. Array is terminated with -1. Up to max elements of the array
  1113. * will be filled. Return value is total number of array elements that would
  1114. * be filled if array was large enough.
  1115. */
  1116. int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
  1117. {
  1118. int i, j, k = 0;
  1119. BN_ULONG mask;
  1120. if (BN_is_zero(a))
  1121. return 0;
  1122. for (i = a->top - 1; i >= 0; i--) {
  1123. if (!a->d[i])
  1124. /* skip word if a->d[i] == 0 */
  1125. continue;
  1126. mask = BN_TBIT;
  1127. for (j = BN_BITS2 - 1; j >= 0; j--) {
  1128. if (a->d[i] & mask) {
  1129. if (k < max)
  1130. p[k] = BN_BITS2 * i + j;
  1131. k++;
  1132. }
  1133. mask >>= 1;
  1134. }
  1135. }
  1136. if (k < max) {
  1137. p[k] = -1;
  1138. k++;
  1139. }
  1140. return k;
  1141. }
  1142. /*
  1143. * Convert the coefficient array representation of a polynomial to a
  1144. * bit-string. The array must be terminated by -1.
  1145. */
  1146. int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
  1147. {
  1148. int i;
  1149. bn_check_top(a);
  1150. BN_zero(a);
  1151. for (i = 0; p[i] != -1; i++) {
  1152. if (BN_set_bit(a, p[i]) == 0)
  1153. return 0;
  1154. }
  1155. bn_check_top(a);
  1156. return 1;
  1157. }
  1158. #endif