bn_gf2m.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. * [email protected].
  60. *
  61. * 5. Products derived from this software may not be called "OpenSSL"
  62. * nor may "OpenSSL" appear in their names without prior written
  63. * permission of the OpenSSL Project.
  64. *
  65. * 6. Redistributions of any form whatsoever must retain the following
  66. * acknowledgment:
  67. * "This product includes software developed by the OpenSSL Project
  68. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  69. *
  70. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  71. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  73. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  74. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  75. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  76. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  77. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  78. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  79. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  80. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  81. * OF THE POSSIBILITY OF SUCH DAMAGE.
  82. * ====================================================================
  83. *
  84. * This product includes cryptographic software written by Eric Young
  85. * ([email protected]). This product includes software written by Tim
  86. * Hudson ([email protected]).
  87. *
  88. */
  89. #include <assert.h>
  90. #include <limits.h>
  91. #include <stdio.h>
  92. #include "cryptlib.h"
  93. #include "bn_lcl.h"
  94. #ifndef OPENSSL_NO_EC2M
  95. /*
  96. * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
  97. * fail.
  98. */
  99. # define MAX_ITERATIONS 50
  100. 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. tmp_ulong = zz >> d1;
  413. if (d0 && tmp_ulong)
  414. z[n + 1] ^= tmp_ulong;
  415. }
  416. }
  417. bn_correct_top(r);
  418. return 1;
  419. }
  420. /*
  421. * Performs modular reduction of a by p and store result in r. r could be a.
  422. * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
  423. * function is only provided for convenience; for best performance, use the
  424. * BN_GF2m_mod_arr function.
  425. */
  426. int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
  427. {
  428. int ret = 0;
  429. int arr[6];
  430. bn_check_top(a);
  431. bn_check_top(p);
  432. ret = BN_GF2m_poly2arr(p, arr, sizeof(arr) / sizeof(arr[0]));
  433. if (!ret || ret > (int)(sizeof(arr) / sizeof(arr[0]))) {
  434. BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH);
  435. return 0;
  436. }
  437. ret = BN_GF2m_mod_arr(r, a, arr);
  438. bn_check_top(r);
  439. return ret;
  440. }
  441. /*
  442. * Compute the product of two polynomials a and b, reduce modulo p, and store
  443. * the result in r. r could be a or b; a could be b.
  444. */
  445. int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  446. const int p[], BN_CTX *ctx)
  447. {
  448. int zlen, i, j, k, ret = 0;
  449. BIGNUM *s;
  450. BN_ULONG x1, x0, y1, y0, zz[4];
  451. bn_check_top(a);
  452. bn_check_top(b);
  453. if (a == b) {
  454. return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
  455. }
  456. BN_CTX_start(ctx);
  457. if ((s = BN_CTX_get(ctx)) == NULL)
  458. goto err;
  459. zlen = a->top + b->top + 4;
  460. if (!bn_wexpand(s, zlen))
  461. goto err;
  462. s->top = zlen;
  463. for (i = 0; i < zlen; i++)
  464. s->d[i] = 0;
  465. for (j = 0; j < b->top; j += 2) {
  466. y0 = b->d[j];
  467. y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
  468. for (i = 0; i < a->top; i += 2) {
  469. x0 = a->d[i];
  470. x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
  471. bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
  472. for (k = 0; k < 4; k++)
  473. s->d[i + j + k] ^= zz[k];
  474. }
  475. }
  476. bn_correct_top(s);
  477. if (BN_GF2m_mod_arr(r, s, p))
  478. ret = 1;
  479. bn_check_top(r);
  480. err:
  481. BN_CTX_end(ctx);
  482. return ret;
  483. }
  484. /*
  485. * Compute the product of two polynomials a and b, reduce modulo p, and store
  486. * the result in r. r could be a or b; a could equal b. This function calls
  487. * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
  488. * only provided for convenience; for best performance, use the
  489. * BN_GF2m_mod_mul_arr function.
  490. */
  491. int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  492. const BIGNUM *p, BN_CTX *ctx)
  493. {
  494. int ret = 0;
  495. const int max = BN_num_bits(p) + 1;
  496. int *arr = NULL;
  497. bn_check_top(a);
  498. bn_check_top(b);
  499. bn_check_top(p);
  500. if ((arr = (int *)OPENSSL_malloc(sizeof(int) * max)) == NULL)
  501. goto err;
  502. ret = BN_GF2m_poly2arr(p, arr, max);
  503. if (!ret || ret > max) {
  504. BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);
  505. goto err;
  506. }
  507. ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
  508. bn_check_top(r);
  509. err:
  510. if (arr)
  511. OPENSSL_free(arr);
  512. return ret;
  513. }
  514. /* Square a, reduce the result mod p, and store it in a. r could be a. */
  515. int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  516. BN_CTX *ctx)
  517. {
  518. int i, ret = 0;
  519. BIGNUM *s;
  520. bn_check_top(a);
  521. BN_CTX_start(ctx);
  522. if ((s = BN_CTX_get(ctx)) == NULL)
  523. goto err;
  524. if (!bn_wexpand(s, 2 * a->top))
  525. goto err;
  526. for (i = a->top - 1; i >= 0; i--) {
  527. s->d[2 * i + 1] = SQR1(a->d[i]);
  528. s->d[2 * i] = SQR0(a->d[i]);
  529. }
  530. s->top = 2 * a->top;
  531. bn_correct_top(s);
  532. if (!BN_GF2m_mod_arr(r, s, p))
  533. goto err;
  534. bn_check_top(r);
  535. ret = 1;
  536. err:
  537. BN_CTX_end(ctx);
  538. return ret;
  539. }
  540. /*
  541. * Square a, reduce the result mod p, and store it in a. r could be a. This
  542. * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
  543. * wrapper function is only provided for convenience; for best performance,
  544. * use the BN_GF2m_mod_sqr_arr function.
  545. */
  546. int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  547. {
  548. int ret = 0;
  549. const int max = BN_num_bits(p) + 1;
  550. int *arr = NULL;
  551. bn_check_top(a);
  552. bn_check_top(p);
  553. if ((arr = (int *)OPENSSL_malloc(sizeof(int) * max)) == NULL)
  554. goto err;
  555. ret = BN_GF2m_poly2arr(p, arr, max);
  556. if (!ret || ret > max) {
  557. BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);
  558. goto err;
  559. }
  560. ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
  561. bn_check_top(r);
  562. err:
  563. if (arr)
  564. OPENSSL_free(arr);
  565. return ret;
  566. }
  567. /*
  568. * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
  569. * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
  570. * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
  571. * Curve Cryptography Over Binary Fields".
  572. */
  573. int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  574. {
  575. BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
  576. int ret = 0;
  577. bn_check_top(a);
  578. bn_check_top(p);
  579. BN_CTX_start(ctx);
  580. if ((b = BN_CTX_get(ctx)) == NULL)
  581. goto err;
  582. if ((c = BN_CTX_get(ctx)) == NULL)
  583. goto err;
  584. if ((u = BN_CTX_get(ctx)) == NULL)
  585. goto err;
  586. if ((v = BN_CTX_get(ctx)) == NULL)
  587. goto err;
  588. if (!BN_GF2m_mod(u, a, p))
  589. goto err;
  590. if (BN_is_zero(u))
  591. goto err;
  592. if (!BN_copy(v, p))
  593. goto err;
  594. # if 0
  595. if (!BN_one(b))
  596. goto err;
  597. while (1) {
  598. while (!BN_is_odd(u)) {
  599. if (BN_is_zero(u))
  600. goto err;
  601. if (!BN_rshift1(u, u))
  602. goto err;
  603. if (BN_is_odd(b)) {
  604. if (!BN_GF2m_add(b, b, p))
  605. goto err;
  606. }
  607. if (!BN_rshift1(b, b))
  608. goto err;
  609. }
  610. if (BN_abs_is_word(u, 1))
  611. break;
  612. if (BN_num_bits(u) < BN_num_bits(v)) {
  613. tmp = u;
  614. u = v;
  615. v = tmp;
  616. tmp = b;
  617. b = c;
  618. c = tmp;
  619. }
  620. if (!BN_GF2m_add(u, u, v))
  621. goto err;
  622. if (!BN_GF2m_add(b, b, c))
  623. goto err;
  624. }
  625. # else
  626. {
  627. int i;
  628. int ubits = BN_num_bits(u);
  629. int vbits = BN_num_bits(v); /* v is copy of p */
  630. int top = p->top;
  631. BN_ULONG *udp, *bdp, *vdp, *cdp;
  632. if (!bn_wexpand(u, top))
  633. goto err;
  634. udp = u->d;
  635. for (i = u->top; i < top; i++)
  636. udp[i] = 0;
  637. u->top = top;
  638. if (!bn_wexpand(b, top))
  639. goto err;
  640. bdp = b->d;
  641. bdp[0] = 1;
  642. for (i = 1; i < top; i++)
  643. bdp[i] = 0;
  644. b->top = top;
  645. if (!bn_wexpand(c, top))
  646. goto err;
  647. cdp = c->d;
  648. for (i = 0; i < top; i++)
  649. cdp[i] = 0;
  650. c->top = top;
  651. vdp = v->d; /* It pays off to "cache" *->d pointers,
  652. * because it allows optimizer to be more
  653. * aggressive. But we don't have to "cache"
  654. * p->d, because *p is declared 'const'... */
  655. while (1) {
  656. while (ubits && !(udp[0] & 1)) {
  657. BN_ULONG u0, u1, b0, b1, mask;
  658. u0 = udp[0];
  659. b0 = bdp[0];
  660. mask = (BN_ULONG)0 - (b0 & 1);
  661. b0 ^= p->d[0] & mask;
  662. for (i = 0; i < top - 1; i++) {
  663. u1 = udp[i + 1];
  664. udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
  665. u0 = u1;
  666. b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
  667. bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
  668. b0 = b1;
  669. }
  670. udp[i] = u0 >> 1;
  671. bdp[i] = b0 >> 1;
  672. ubits--;
  673. }
  674. if (ubits <= BN_BITS2) {
  675. if (udp[0] == 0) /* poly was reducible */
  676. goto err;
  677. if (udp[0] == 1)
  678. break;
  679. }
  680. if (ubits < vbits) {
  681. i = ubits;
  682. ubits = vbits;
  683. vbits = i;
  684. tmp = u;
  685. u = v;
  686. v = tmp;
  687. tmp = b;
  688. b = c;
  689. c = tmp;
  690. udp = vdp;
  691. vdp = v->d;
  692. bdp = cdp;
  693. cdp = c->d;
  694. }
  695. for (i = 0; i < top; i++) {
  696. udp[i] ^= vdp[i];
  697. bdp[i] ^= cdp[i];
  698. }
  699. if (ubits == vbits) {
  700. BN_ULONG ul;
  701. int utop = (ubits - 1) / BN_BITS2;
  702. while ((ul = udp[utop]) == 0 && utop)
  703. utop--;
  704. ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
  705. }
  706. }
  707. bn_correct_top(b);
  708. }
  709. # endif
  710. if (!BN_copy(r, b))
  711. goto err;
  712. bn_check_top(r);
  713. ret = 1;
  714. err:
  715. # ifdef BN_DEBUG /* BN_CTX_end would complain about the
  716. * expanded form */
  717. bn_correct_top(c);
  718. bn_correct_top(u);
  719. bn_correct_top(v);
  720. # endif
  721. BN_CTX_end(ctx);
  722. return ret;
  723. }
  724. /*
  725. * Invert xx, reduce modulo p, and store the result in r. r could be xx.
  726. * This function calls down to the BN_GF2m_mod_inv implementation; this
  727. * wrapper function is only provided for convenience; for best performance,
  728. * use the BN_GF2m_mod_inv function.
  729. */
  730. int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
  731. BN_CTX *ctx)
  732. {
  733. BIGNUM *field;
  734. int ret = 0;
  735. bn_check_top(xx);
  736. BN_CTX_start(ctx);
  737. if ((field = BN_CTX_get(ctx)) == NULL)
  738. goto err;
  739. if (!BN_GF2m_arr2poly(p, field))
  740. goto err;
  741. ret = BN_GF2m_mod_inv(r, xx, field, ctx);
  742. bn_check_top(r);
  743. err:
  744. BN_CTX_end(ctx);
  745. return ret;
  746. }
  747. # ifndef OPENSSL_SUN_GF2M_DIV
  748. /*
  749. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  750. * or y, x could equal y.
  751. */
  752. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  753. const BIGNUM *p, BN_CTX *ctx)
  754. {
  755. BIGNUM *xinv = NULL;
  756. int ret = 0;
  757. bn_check_top(y);
  758. bn_check_top(x);
  759. bn_check_top(p);
  760. BN_CTX_start(ctx);
  761. xinv = BN_CTX_get(ctx);
  762. if (xinv == NULL)
  763. goto err;
  764. if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
  765. goto err;
  766. if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
  767. goto err;
  768. bn_check_top(r);
  769. ret = 1;
  770. err:
  771. BN_CTX_end(ctx);
  772. return ret;
  773. }
  774. # else
  775. /*
  776. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  777. * or y, x could equal y. Uses algorithm Modular_Division_GF(2^m) from
  778. * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to the
  779. * Great Divide".
  780. */
  781. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  782. const BIGNUM *p, BN_CTX *ctx)
  783. {
  784. BIGNUM *a, *b, *u, *v;
  785. int ret = 0;
  786. bn_check_top(y);
  787. bn_check_top(x);
  788. bn_check_top(p);
  789. BN_CTX_start(ctx);
  790. a = BN_CTX_get(ctx);
  791. b = BN_CTX_get(ctx);
  792. u = BN_CTX_get(ctx);
  793. v = BN_CTX_get(ctx);
  794. if (v == NULL)
  795. goto err;
  796. /* reduce x and y mod p */
  797. if (!BN_GF2m_mod(u, y, p))
  798. goto err;
  799. if (!BN_GF2m_mod(a, x, p))
  800. goto err;
  801. if (!BN_copy(b, p))
  802. goto err;
  803. while (!BN_is_odd(a)) {
  804. if (!BN_rshift1(a, a))
  805. goto err;
  806. if (BN_is_odd(u))
  807. if (!BN_GF2m_add(u, u, p))
  808. goto err;
  809. if (!BN_rshift1(u, u))
  810. goto err;
  811. }
  812. do {
  813. if (BN_GF2m_cmp(b, a) > 0) {
  814. if (!BN_GF2m_add(b, b, a))
  815. goto err;
  816. if (!BN_GF2m_add(v, v, u))
  817. goto err;
  818. do {
  819. if (!BN_rshift1(b, b))
  820. goto err;
  821. if (BN_is_odd(v))
  822. if (!BN_GF2m_add(v, v, p))
  823. goto err;
  824. if (!BN_rshift1(v, v))
  825. goto err;
  826. } while (!BN_is_odd(b));
  827. } else if (BN_abs_is_word(a, 1))
  828. break;
  829. else {
  830. if (!BN_GF2m_add(a, a, b))
  831. goto err;
  832. if (!BN_GF2m_add(u, u, v))
  833. goto err;
  834. do {
  835. if (!BN_rshift1(a, a))
  836. goto err;
  837. if (BN_is_odd(u))
  838. if (!BN_GF2m_add(u, u, p))
  839. goto err;
  840. if (!BN_rshift1(u, u))
  841. goto err;
  842. } while (!BN_is_odd(a));
  843. }
  844. } while (1);
  845. if (!BN_copy(r, u))
  846. goto err;
  847. bn_check_top(r);
  848. ret = 1;
  849. err:
  850. BN_CTX_end(ctx);
  851. return ret;
  852. }
  853. # endif
  854. /*
  855. * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
  856. * * or yy, xx could equal yy. This function calls down to the
  857. * BN_GF2m_mod_div implementation; this wrapper function is only provided for
  858. * convenience; for best performance, use the BN_GF2m_mod_div function.
  859. */
  860. int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
  861. const int p[], BN_CTX *ctx)
  862. {
  863. BIGNUM *field;
  864. int ret = 0;
  865. bn_check_top(yy);
  866. bn_check_top(xx);
  867. BN_CTX_start(ctx);
  868. if ((field = BN_CTX_get(ctx)) == NULL)
  869. goto err;
  870. if (!BN_GF2m_arr2poly(p, field))
  871. goto err;
  872. ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
  873. bn_check_top(r);
  874. err:
  875. BN_CTX_end(ctx);
  876. return ret;
  877. }
  878. /*
  879. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  880. * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
  881. * P1363.
  882. */
  883. int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  884. const int p[], BN_CTX *ctx)
  885. {
  886. int ret = 0, i, n;
  887. BIGNUM *u;
  888. bn_check_top(a);
  889. bn_check_top(b);
  890. if (BN_is_zero(b))
  891. return (BN_one(r));
  892. if (BN_abs_is_word(b, 1))
  893. return (BN_copy(r, a) != NULL);
  894. BN_CTX_start(ctx);
  895. if ((u = BN_CTX_get(ctx)) == NULL)
  896. goto err;
  897. if (!BN_GF2m_mod_arr(u, a, p))
  898. goto err;
  899. n = BN_num_bits(b) - 1;
  900. for (i = n - 1; i >= 0; i--) {
  901. if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
  902. goto err;
  903. if (BN_is_bit_set(b, i)) {
  904. if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
  905. goto err;
  906. }
  907. }
  908. if (!BN_copy(r, u))
  909. goto err;
  910. bn_check_top(r);
  911. ret = 1;
  912. err:
  913. BN_CTX_end(ctx);
  914. return ret;
  915. }
  916. /*
  917. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  918. * could be a. This function calls down to the BN_GF2m_mod_exp_arr
  919. * implementation; this wrapper function is only provided for convenience;
  920. * for best performance, use the BN_GF2m_mod_exp_arr function.
  921. */
  922. int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  923. const BIGNUM *p, BN_CTX *ctx)
  924. {
  925. int ret = 0;
  926. const int max = BN_num_bits(p) + 1;
  927. int *arr = NULL;
  928. bn_check_top(a);
  929. bn_check_top(b);
  930. bn_check_top(p);
  931. if ((arr = (int *)OPENSSL_malloc(sizeof(int) * max)) == NULL)
  932. goto err;
  933. ret = BN_GF2m_poly2arr(p, arr, max);
  934. if (!ret || ret > max) {
  935. BNerr(BN_F_BN_GF2M_MOD_EXP, BN_R_INVALID_LENGTH);
  936. goto err;
  937. }
  938. ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
  939. bn_check_top(r);
  940. err:
  941. if (arr)
  942. OPENSSL_free(arr);
  943. return ret;
  944. }
  945. /*
  946. * Compute the square root of a, reduce modulo p, and store the result in r.
  947. * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
  948. */
  949. int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  950. BN_CTX *ctx)
  951. {
  952. int ret = 0;
  953. BIGNUM *u;
  954. bn_check_top(a);
  955. if (!p[0]) {
  956. /* reduction mod 1 => return 0 */
  957. BN_zero(r);
  958. return 1;
  959. }
  960. BN_CTX_start(ctx);
  961. if ((u = BN_CTX_get(ctx)) == NULL)
  962. goto err;
  963. if (!BN_set_bit(u, p[0] - 1))
  964. goto err;
  965. ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
  966. bn_check_top(r);
  967. err:
  968. BN_CTX_end(ctx);
  969. return ret;
  970. }
  971. /*
  972. * Compute the square root of a, reduce modulo p, and store the result in r.
  973. * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
  974. * implementation; this wrapper function is only provided for convenience;
  975. * for best performance, use the BN_GF2m_mod_sqrt_arr function.
  976. */
  977. int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  978. {
  979. int ret = 0;
  980. const int max = BN_num_bits(p) + 1;
  981. int *arr = NULL;
  982. bn_check_top(a);
  983. bn_check_top(p);
  984. if ((arr = (int *)OPENSSL_malloc(sizeof(int) * max)) == NULL)
  985. goto err;
  986. ret = BN_GF2m_poly2arr(p, arr, max);
  987. if (!ret || ret > max) {
  988. BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);
  989. goto err;
  990. }
  991. ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
  992. bn_check_top(r);
  993. err:
  994. if (arr)
  995. OPENSSL_free(arr);
  996. return ret;
  997. }
  998. /*
  999. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  1000. * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
  1001. */
  1002. int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
  1003. BN_CTX *ctx)
  1004. {
  1005. int ret = 0, count = 0, j;
  1006. BIGNUM *a, *z, *rho, *w, *w2, *tmp;
  1007. bn_check_top(a_);
  1008. if (!p[0]) {
  1009. /* reduction mod 1 => return 0 */
  1010. BN_zero(r);
  1011. return 1;
  1012. }
  1013. BN_CTX_start(ctx);
  1014. a = BN_CTX_get(ctx);
  1015. z = BN_CTX_get(ctx);
  1016. w = BN_CTX_get(ctx);
  1017. if (w == NULL)
  1018. goto err;
  1019. if (!BN_GF2m_mod_arr(a, a_, p))
  1020. goto err;
  1021. if (BN_is_zero(a)) {
  1022. BN_zero(r);
  1023. ret = 1;
  1024. goto err;
  1025. }
  1026. if (p[0] & 0x1) { /* m is odd */
  1027. /* compute half-trace of a */
  1028. if (!BN_copy(z, a))
  1029. goto err;
  1030. for (j = 1; j <= (p[0] - 1) / 2; j++) {
  1031. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1032. goto err;
  1033. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1034. goto err;
  1035. if (!BN_GF2m_add(z, z, a))
  1036. goto err;
  1037. }
  1038. } else { /* m is even */
  1039. rho = BN_CTX_get(ctx);
  1040. w2 = BN_CTX_get(ctx);
  1041. tmp = BN_CTX_get(ctx);
  1042. if (tmp == NULL)
  1043. goto err;
  1044. do {
  1045. if (!BN_rand(rho, p[0], 0, 0))
  1046. goto err;
  1047. if (!BN_GF2m_mod_arr(rho, rho, p))
  1048. goto err;
  1049. BN_zero(z);
  1050. if (!BN_copy(w, rho))
  1051. goto err;
  1052. for (j = 1; j <= p[0] - 1; j++) {
  1053. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1054. goto err;
  1055. if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
  1056. goto err;
  1057. if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
  1058. goto err;
  1059. if (!BN_GF2m_add(z, z, tmp))
  1060. goto err;
  1061. if (!BN_GF2m_add(w, w2, rho))
  1062. goto err;
  1063. }
  1064. count++;
  1065. } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
  1066. if (BN_is_zero(w)) {
  1067. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);
  1068. goto err;
  1069. }
  1070. }
  1071. if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
  1072. goto err;
  1073. if (!BN_GF2m_add(w, z, w))
  1074. goto err;
  1075. if (BN_GF2m_cmp(w, a)) {
  1076. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
  1077. goto err;
  1078. }
  1079. if (!BN_copy(r, z))
  1080. goto err;
  1081. bn_check_top(r);
  1082. ret = 1;
  1083. err:
  1084. BN_CTX_end(ctx);
  1085. return ret;
  1086. }
  1087. /*
  1088. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  1089. * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
  1090. * implementation; this wrapper function is only provided for convenience;
  1091. * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
  1092. */
  1093. int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  1094. BN_CTX *ctx)
  1095. {
  1096. int ret = 0;
  1097. const int max = BN_num_bits(p) + 1;
  1098. int *arr = NULL;
  1099. bn_check_top(a);
  1100. bn_check_top(p);
  1101. if ((arr = (int *)OPENSSL_malloc(sizeof(int) * max)) == NULL)
  1102. goto err;
  1103. ret = BN_GF2m_poly2arr(p, arr, max);
  1104. if (!ret || ret > max) {
  1105. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);
  1106. goto err;
  1107. }
  1108. ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
  1109. bn_check_top(r);
  1110. err:
  1111. if (arr)
  1112. OPENSSL_free(arr);
  1113. return ret;
  1114. }
  1115. /*
  1116. * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
  1117. * x^i) into an array of integers corresponding to the bits with non-zero
  1118. * coefficient. Array is terminated with -1. Up to max elements of the array
  1119. * will be filled. Return value is total number of array elements that would
  1120. * be filled if array was large enough.
  1121. */
  1122. int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
  1123. {
  1124. int i, j, k = 0;
  1125. BN_ULONG mask;
  1126. if (BN_is_zero(a))
  1127. return 0;
  1128. for (i = a->top - 1; i >= 0; i--) {
  1129. if (!a->d[i])
  1130. /* skip word if a->d[i] == 0 */
  1131. continue;
  1132. mask = BN_TBIT;
  1133. for (j = BN_BITS2 - 1; j >= 0; j--) {
  1134. if (a->d[i] & mask) {
  1135. if (k < max)
  1136. p[k] = BN_BITS2 * i + j;
  1137. k++;
  1138. }
  1139. mask >>= 1;
  1140. }
  1141. }
  1142. if (k < max) {
  1143. p[k] = -1;
  1144. k++;
  1145. }
  1146. return k;
  1147. }
  1148. /*
  1149. * Convert the coefficient array representation of a polynomial to a
  1150. * bit-string. The array must be terminated by -1.
  1151. */
  1152. int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
  1153. {
  1154. int i;
  1155. bn_check_top(a);
  1156. BN_zero(a);
  1157. for (i = 0; p[i] != -1; i++) {
  1158. if (BN_set_bit(a, p[i]) == 0)
  1159. return 0;
  1160. }
  1161. bn_check_top(a);
  1162. return 1;
  1163. }
  1164. #endif