bn_gf2m.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. /* crypto/bn/bn_gf2m.c */
  2. /* ====================================================================
  3. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  4. *
  5. * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
  6. * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
  7. * to the OpenSSL project.
  8. *
  9. * The ECC Code is licensed pursuant to the OpenSSL open source
  10. * license provided below.
  11. *
  12. * In addition, Sun covenants to all licensees who provide a reciprocal
  13. * covenant with respect to their own patents if any, not to sue under
  14. * current and future patent claims necessarily infringed by the making,
  15. * using, practicing, selling, offering for sale and/or otherwise
  16. * disposing of the ECC Code as delivered hereunder (or portions thereof),
  17. * provided that such covenant shall not apply:
  18. * 1) for code that a licensee deletes from the ECC Code;
  19. * 2) separates from the ECC Code; or
  20. * 3) for infringements caused by:
  21. * i) the modification of the ECC Code or
  22. * ii) the combination of the ECC Code with other software or
  23. * devices where such combination causes the infringement.
  24. *
  25. * The software is originally written by Sheueling Chang Shantz and
  26. * Douglas Stebila of Sun Microsystems Laboratories.
  27. *
  28. */
  29. /*
  30. * NOTE: This file is licensed pursuant to the OpenSSL license below and may
  31. * be modified; but after modifications, the above covenant may no longer
  32. * apply! In such cases, the corresponding paragraph ["In addition, Sun
  33. * covenants ... causes the infringement."] and this note can be edited out;
  34. * but please keep the Sun copyright notice and attribution.
  35. */
  36. /* ====================================================================
  37. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. *
  43. * 1. Redistributions of source code must retain the above copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. *
  46. * 2. Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. *
  51. * 3. All advertising materials mentioning features or use of this
  52. * software must display the following acknowledgment:
  53. * "This product includes software developed by the OpenSSL Project
  54. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  55. *
  56. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  57. * endorse or promote products derived from this software without
  58. * prior written permission. For written permission, please contact
  59. * openssl-core@openssl.org.
  60. *
  61. * 5. Products derived from this software may not be called "OpenSSL"
  62. * nor may "OpenSSL" appear in their names without prior written
  63. * permission of the OpenSSL Project.
  64. *
  65. * 6. Redistributions of any form whatsoever must retain the following
  66. * acknowledgment:
  67. * "This product includes software developed by the OpenSSL Project
  68. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  69. *
  70. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  71. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  73. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  74. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  75. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  76. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  77. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  78. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  79. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  80. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  81. * OF THE POSSIBILITY OF SUCH DAMAGE.
  82. * ====================================================================
  83. *
  84. * This product includes cryptographic software written by Eric Young
  85. * (eay@cryptsoft.com). This product includes software written by Tim
  86. * Hudson (tjh@cryptsoft.com).
  87. *
  88. */
  89. #include <assert.h>
  90. #include <limits.h>
  91. #include <stdio.h>
  92. #include "cryptlib.h"
  93. #include "bn_lcl.h"
  94. /*
  95. * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
  96. * fail.
  97. */
  98. #define MAX_ITERATIONS 50
  99. static const BN_ULONG SQR_tb[16] = { 0, 1, 4, 5, 16, 17, 20, 21,
  100. 64, 65, 68, 69, 80, 81, 84, 85
  101. };
  102. /* Platform-specific macros to accelerate squaring. */
  103. #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
  104. # define SQR1(w) \
  105. SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
  106. SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
  107. SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
  108. SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
  109. # define SQR0(w) \
  110. SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
  111. SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
  112. SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
  113. SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
  114. #endif
  115. #ifdef THIRTY_TWO_BIT
  116. # define SQR1(w) \
  117. SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
  118. SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
  119. # define SQR0(w) \
  120. SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
  121. SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
  122. #endif
  123. #ifdef SIXTEEN_BIT
  124. # define SQR1(w) \
  125. SQR_tb[(w) >> 12 & 0xF] << 8 | SQR_tb[(w) >> 8 & 0xF]
  126. # define SQR0(w) \
  127. SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
  128. #endif
  129. #ifdef EIGHT_BIT
  130. # define SQR1(w) \
  131. SQR_tb[(w) >> 4 & 0xF]
  132. # define SQR0(w) \
  133. SQR_tb[(w) & 15]
  134. #endif
  135. /*
  136. * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
  137. * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
  138. * the variables have the right amount of space allocated.
  139. */
  140. #ifdef EIGHT_BIT
  141. static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
  142. const BN_ULONG b)
  143. {
  144. register BN_ULONG h, l, s;
  145. BN_ULONG tab[4], top1b = a >> 7;
  146. register BN_ULONG a1, a2;
  147. a1 = a & (0x7F);
  148. a2 = a1 << 1;
  149. tab[0] = 0;
  150. tab[1] = a1;
  151. tab[2] = a2;
  152. tab[3] = a1 ^ a2;
  153. s = tab[b & 0x3];
  154. l = s;
  155. s = tab[b >> 2 & 0x3];
  156. l ^= s << 2;
  157. h = s >> 6;
  158. s = tab[b >> 4 & 0x3];
  159. l ^= s << 4;
  160. h ^= s >> 4;
  161. s = tab[b >> 6];
  162. l ^= s << 6;
  163. h ^= s >> 2;
  164. /* compensate for the top bit of a */
  165. if (top1b & 01) {
  166. l ^= b << 7;
  167. h ^= b >> 1;
  168. }
  169. *r1 = h;
  170. *r0 = l;
  171. }
  172. #endif
  173. #ifdef SIXTEEN_BIT
  174. static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
  175. const BN_ULONG b)
  176. {
  177. register BN_ULONG h, l, s;
  178. BN_ULONG tab[4], top1b = a >> 15;
  179. register BN_ULONG a1, a2;
  180. a1 = a & (0x7FFF);
  181. a2 = a1 << 1;
  182. tab[0] = 0;
  183. tab[1] = a1;
  184. tab[2] = a2;
  185. tab[3] = a1 ^ a2;
  186. s = tab[b & 0x3];
  187. l = s;
  188. s = tab[b >> 2 & 0x3];
  189. l ^= s << 2;
  190. h = s >> 14;
  191. s = tab[b >> 4 & 0x3];
  192. l ^= s << 4;
  193. h ^= s >> 12;
  194. s = tab[b >> 6 & 0x3];
  195. l ^= s << 6;
  196. h ^= s >> 10;
  197. s = tab[b >> 8 & 0x3];
  198. l ^= s << 8;
  199. h ^= s >> 8;
  200. s = tab[b >> 10 & 0x3];
  201. l ^= s << 10;
  202. h ^= s >> 6;
  203. s = tab[b >> 12 & 0x3];
  204. l ^= s << 12;
  205. h ^= s >> 4;
  206. s = tab[b >> 14];
  207. l ^= s << 14;
  208. h ^= s >> 2;
  209. /* compensate for the top bit of a */
  210. if (top1b & 01) {
  211. l ^= b << 15;
  212. h ^= b >> 1;
  213. }
  214. *r1 = h;
  215. *r0 = l;
  216. }
  217. #endif
  218. #ifdef THIRTY_TWO_BIT
  219. static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
  220. const BN_ULONG b)
  221. {
  222. register BN_ULONG h, l, s;
  223. BN_ULONG tab[8], top2b = a >> 30;
  224. register BN_ULONG a1, a2, a4;
  225. a1 = a & (0x3FFFFFFF);
  226. a2 = a1 << 1;
  227. a4 = a2 << 1;
  228. tab[0] = 0;
  229. tab[1] = a1;
  230. tab[2] = a2;
  231. tab[3] = a1 ^ a2;
  232. tab[4] = a4;
  233. tab[5] = a1 ^ a4;
  234. tab[6] = a2 ^ a4;
  235. tab[7] = a1 ^ a2 ^ a4;
  236. s = tab[b & 0x7];
  237. l = s;
  238. s = tab[b >> 3 & 0x7];
  239. l ^= s << 3;
  240. h = s >> 29;
  241. s = tab[b >> 6 & 0x7];
  242. l ^= s << 6;
  243. h ^= s >> 26;
  244. s = tab[b >> 9 & 0x7];
  245. l ^= s << 9;
  246. h ^= s >> 23;
  247. s = tab[b >> 12 & 0x7];
  248. l ^= s << 12;
  249. h ^= s >> 20;
  250. s = tab[b >> 15 & 0x7];
  251. l ^= s << 15;
  252. h ^= s >> 17;
  253. s = tab[b >> 18 & 0x7];
  254. l ^= s << 18;
  255. h ^= s >> 14;
  256. s = tab[b >> 21 & 0x7];
  257. l ^= s << 21;
  258. h ^= s >> 11;
  259. s = tab[b >> 24 & 0x7];
  260. l ^= s << 24;
  261. h ^= s >> 8;
  262. s = tab[b >> 27 & 0x7];
  263. l ^= s << 27;
  264. h ^= s >> 5;
  265. s = tab[b >> 30];
  266. l ^= s << 30;
  267. h ^= s >> 2;
  268. /* compensate for the top two bits of a */
  269. if (top2b & 01) {
  270. l ^= b << 30;
  271. h ^= b >> 2;
  272. }
  273. if (top2b & 02) {
  274. l ^= b << 31;
  275. h ^= b >> 1;
  276. }
  277. *r1 = h;
  278. *r0 = l;
  279. }
  280. #endif
  281. #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
  282. static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
  283. const BN_ULONG b)
  284. {
  285. register BN_ULONG h, l, s;
  286. BN_ULONG tab[16], top3b = a >> 61;
  287. register BN_ULONG a1, a2, a4, a8;
  288. a1 = a & (0x1FFFFFFFFFFFFFFFULL);
  289. a2 = a1 << 1;
  290. a4 = a2 << 1;
  291. a8 = a4 << 1;
  292. tab[0] = 0;
  293. tab[1] = a1;
  294. tab[2] = a2;
  295. tab[3] = a1 ^ a2;
  296. tab[4] = a4;
  297. tab[5] = a1 ^ a4;
  298. tab[6] = a2 ^ a4;
  299. tab[7] = a1 ^ a2 ^ a4;
  300. tab[8] = a8;
  301. tab[9] = a1 ^ a8;
  302. tab[10] = a2 ^ a8;
  303. tab[11] = a1 ^ a2 ^ a8;
  304. tab[12] = a4 ^ a8;
  305. tab[13] = a1 ^ a4 ^ a8;
  306. tab[14] = a2 ^ a4 ^ a8;
  307. tab[15] = a1 ^ a2 ^ a4 ^ a8;
  308. s = tab[b & 0xF];
  309. l = s;
  310. s = tab[b >> 4 & 0xF];
  311. l ^= s << 4;
  312. h = s >> 60;
  313. s = tab[b >> 8 & 0xF];
  314. l ^= s << 8;
  315. h ^= s >> 56;
  316. s = tab[b >> 12 & 0xF];
  317. l ^= s << 12;
  318. h ^= s >> 52;
  319. s = tab[b >> 16 & 0xF];
  320. l ^= s << 16;
  321. h ^= s >> 48;
  322. s = tab[b >> 20 & 0xF];
  323. l ^= s << 20;
  324. h ^= s >> 44;
  325. s = tab[b >> 24 & 0xF];
  326. l ^= s << 24;
  327. h ^= s >> 40;
  328. s = tab[b >> 28 & 0xF];
  329. l ^= s << 28;
  330. h ^= s >> 36;
  331. s = tab[b >> 32 & 0xF];
  332. l ^= s << 32;
  333. h ^= s >> 32;
  334. s = tab[b >> 36 & 0xF];
  335. l ^= s << 36;
  336. h ^= s >> 28;
  337. s = tab[b >> 40 & 0xF];
  338. l ^= s << 40;
  339. h ^= s >> 24;
  340. s = tab[b >> 44 & 0xF];
  341. l ^= s << 44;
  342. h ^= s >> 20;
  343. s = tab[b >> 48 & 0xF];
  344. l ^= s << 48;
  345. h ^= s >> 16;
  346. s = tab[b >> 52 & 0xF];
  347. l ^= s << 52;
  348. h ^= s >> 12;
  349. s = tab[b >> 56 & 0xF];
  350. l ^= s << 56;
  351. h ^= s >> 8;
  352. s = tab[b >> 60];
  353. l ^= s << 60;
  354. h ^= s >> 4;
  355. /* compensate for the top three bits of a */
  356. if (top3b & 01) {
  357. l ^= b << 61;
  358. h ^= b >> 3;
  359. }
  360. if (top3b & 02) {
  361. l ^= b << 62;
  362. h ^= b >> 2;
  363. }
  364. if (top3b & 04) {
  365. l ^= b << 63;
  366. h ^= b >> 1;
  367. }
  368. *r1 = h;
  369. *r0 = l;
  370. }
  371. #endif
  372. /*
  373. * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
  374. * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
  375. * ensure that the variables have the right amount of space allocated.
  376. */
  377. static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
  378. const BN_ULONG b1, const BN_ULONG b0)
  379. {
  380. BN_ULONG m1, m0;
  381. /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
  382. bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
  383. bn_GF2m_mul_1x1(r + 1, r, a0, b0);
  384. bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
  385. /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
  386. r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
  387. r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
  388. }
  389. /*
  390. * Add polynomials a and b and store result in r; r could be a or b, a and b
  391. * could be equal; r is the bitwise XOR of a and b.
  392. */
  393. int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  394. {
  395. int i;
  396. const BIGNUM *at, *bt;
  397. bn_check_top(a);
  398. bn_check_top(b);
  399. if (a->top < b->top) {
  400. at = b;
  401. bt = a;
  402. } else {
  403. at = a;
  404. bt = b;
  405. }
  406. if (bn_wexpand(r, at->top) == NULL)
  407. return 0;
  408. for (i = 0; i < bt->top; i++) {
  409. r->d[i] = at->d[i] ^ bt->d[i];
  410. }
  411. for (; i < at->top; i++) {
  412. r->d[i] = at->d[i];
  413. }
  414. r->top = at->top;
  415. bn_correct_top(r);
  416. return 1;
  417. }
  418. /*-
  419. * Some functions allow for representation of the irreducible polynomials
  420. * as an int[], say p. The irreducible f(t) is then of the form:
  421. * t^p[0] + t^p[1] + ... + t^p[k]
  422. * where m = p[0] > p[1] > ... > p[k] = 0.
  423. */
  424. /* Performs modular reduction of a and store result in r. r could be a. */
  425. int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[])
  426. {
  427. int j, k;
  428. int n, dN, d0, d1;
  429. BN_ULONG zz, *z;
  430. bn_check_top(a);
  431. if (!p[0]) {
  432. /* reduction mod 1 => return 0 */
  433. BN_zero(r);
  434. return 1;
  435. }
  436. /*
  437. * Since the algorithm does reduction in the r value, if a != r, copy the
  438. * contents of a into r so we can do reduction in r.
  439. */
  440. if (a != r) {
  441. if (!bn_wexpand(r, a->top))
  442. return 0;
  443. for (j = 0; j < a->top; j++) {
  444. r->d[j] = a->d[j];
  445. }
  446. r->top = a->top;
  447. }
  448. z = r->d;
  449. /* start reduction */
  450. dN = p[0] / BN_BITS2;
  451. for (j = r->top - 1; j > dN;) {
  452. zz = z[j];
  453. if (z[j] == 0) {
  454. j--;
  455. continue;
  456. }
  457. z[j] = 0;
  458. for (k = 1; p[k] != 0; k++) {
  459. /* reducing component t^p[k] */
  460. n = p[0] - p[k];
  461. d0 = n % BN_BITS2;
  462. d1 = BN_BITS2 - d0;
  463. n /= BN_BITS2;
  464. z[j - n] ^= (zz >> d0);
  465. if (d0)
  466. z[j - n - 1] ^= (zz << d1);
  467. }
  468. /* reducing component t^0 */
  469. n = dN;
  470. d0 = p[0] % BN_BITS2;
  471. d1 = BN_BITS2 - d0;
  472. z[j - n] ^= (zz >> d0);
  473. if (d0)
  474. z[j - n - 1] ^= (zz << d1);
  475. }
  476. /* final round of reduction */
  477. while (j == dN) {
  478. d0 = p[0] % BN_BITS2;
  479. zz = z[dN] >> d0;
  480. if (zz == 0)
  481. break;
  482. d1 = BN_BITS2 - d0;
  483. /* clear up the top d1 bits */
  484. if (d0)
  485. z[dN] = (z[dN] << d1) >> d1;
  486. else
  487. z[dN] = 0;
  488. z[0] ^= zz; /* reduction t^0 component */
  489. for (k = 1; p[k] != 0; k++) {
  490. BN_ULONG tmp_ulong;
  491. /* reducing component t^p[k] */
  492. n = p[k] / BN_BITS2;
  493. d0 = p[k] % BN_BITS2;
  494. d1 = BN_BITS2 - d0;
  495. z[n] ^= (zz << d0);
  496. tmp_ulong = zz >> d1;
  497. if (d0 && tmp_ulong)
  498. z[n + 1] ^= tmp_ulong;
  499. }
  500. }
  501. bn_correct_top(r);
  502. return 1;
  503. }
  504. /*
  505. * Performs modular reduction of a by p and store result in r. r could be a.
  506. * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
  507. * function is only provided for convenience; for best performance, use the
  508. * BN_GF2m_mod_arr function.
  509. */
  510. int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
  511. {
  512. int ret = 0;
  513. const int max = BN_num_bits(p);
  514. unsigned int *arr = NULL;
  515. bn_check_top(a);
  516. bn_check_top(p);
  517. if ((arr =
  518. (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
  519. goto err;
  520. ret = BN_GF2m_poly2arr(p, arr, max);
  521. if (!ret || ret > max) {
  522. BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH);
  523. goto err;
  524. }
  525. ret = BN_GF2m_mod_arr(r, a, arr);
  526. bn_check_top(r);
  527. err:
  528. if (arr)
  529. OPENSSL_free(arr);
  530. return ret;
  531. }
  532. /*
  533. * Compute the product of two polynomials a and b, reduce modulo p, and store
  534. * the result in r. r could be a or b; a could be b.
  535. */
  536. int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  537. const unsigned int p[], BN_CTX *ctx)
  538. {
  539. int zlen, i, j, k, ret = 0;
  540. BIGNUM *s;
  541. BN_ULONG x1, x0, y1, y0, zz[4];
  542. bn_check_top(a);
  543. bn_check_top(b);
  544. if (a == b) {
  545. return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
  546. }
  547. BN_CTX_start(ctx);
  548. if ((s = BN_CTX_get(ctx)) == NULL)
  549. goto err;
  550. zlen = a->top + b->top + 4;
  551. if (!bn_wexpand(s, zlen))
  552. goto err;
  553. s->top = zlen;
  554. for (i = 0; i < zlen; i++)
  555. s->d[i] = 0;
  556. for (j = 0; j < b->top; j += 2) {
  557. y0 = b->d[j];
  558. y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
  559. for (i = 0; i < a->top; i += 2) {
  560. x0 = a->d[i];
  561. x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
  562. bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
  563. for (k = 0; k < 4; k++)
  564. s->d[i + j + k] ^= zz[k];
  565. }
  566. }
  567. bn_correct_top(s);
  568. if (BN_GF2m_mod_arr(r, s, p))
  569. ret = 1;
  570. bn_check_top(r);
  571. err:
  572. BN_CTX_end(ctx);
  573. return ret;
  574. }
  575. /*
  576. * Compute the product of two polynomials a and b, reduce modulo p, and store
  577. * the result in r. r could be a or b; a could equal b. This function calls
  578. * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
  579. * only provided for convenience; for best performance, use the
  580. * BN_GF2m_mod_mul_arr function.
  581. */
  582. int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  583. const BIGNUM *p, BN_CTX *ctx)
  584. {
  585. int ret = 0;
  586. const int max = BN_num_bits(p);
  587. unsigned int *arr = NULL;
  588. bn_check_top(a);
  589. bn_check_top(b);
  590. bn_check_top(p);
  591. if ((arr =
  592. (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
  593. goto err;
  594. ret = BN_GF2m_poly2arr(p, arr, max);
  595. if (!ret || ret > max) {
  596. BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);
  597. goto err;
  598. }
  599. ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
  600. bn_check_top(r);
  601. err:
  602. if (arr)
  603. OPENSSL_free(arr);
  604. return ret;
  605. }
  606. /* Square a, reduce the result mod p, and store it in a. r could be a. */
  607. int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[],
  608. BN_CTX *ctx)
  609. {
  610. int i, ret = 0;
  611. BIGNUM *s;
  612. bn_check_top(a);
  613. BN_CTX_start(ctx);
  614. if ((s = BN_CTX_get(ctx)) == NULL)
  615. return 0;
  616. if (!bn_wexpand(s, 2 * a->top))
  617. goto err;
  618. for (i = a->top - 1; i >= 0; i--) {
  619. s->d[2 * i + 1] = SQR1(a->d[i]);
  620. s->d[2 * i] = SQR0(a->d[i]);
  621. }
  622. s->top = 2 * a->top;
  623. bn_correct_top(s);
  624. if (!BN_GF2m_mod_arr(r, s, p))
  625. goto err;
  626. bn_check_top(r);
  627. ret = 1;
  628. err:
  629. BN_CTX_end(ctx);
  630. return ret;
  631. }
  632. /*
  633. * Square a, reduce the result mod p, and store it in a. r could be a. This
  634. * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
  635. * wrapper function is only provided for convenience; for best performance,
  636. * use the BN_GF2m_mod_sqr_arr function.
  637. */
  638. int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  639. {
  640. int ret = 0;
  641. const int max = BN_num_bits(p);
  642. unsigned int *arr = NULL;
  643. bn_check_top(a);
  644. bn_check_top(p);
  645. if ((arr =
  646. (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
  647. goto err;
  648. ret = BN_GF2m_poly2arr(p, arr, max);
  649. if (!ret || ret > max) {
  650. BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);
  651. goto err;
  652. }
  653. ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
  654. bn_check_top(r);
  655. err:
  656. if (arr)
  657. OPENSSL_free(arr);
  658. return ret;
  659. }
  660. /*
  661. * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
  662. * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
  663. * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
  664. * Curve Cryptography Over Binary Fields".
  665. */
  666. int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  667. {
  668. BIGNUM *b, *c, *u, *v, *tmp;
  669. int ret = 0;
  670. bn_check_top(a);
  671. bn_check_top(p);
  672. BN_CTX_start(ctx);
  673. b = BN_CTX_get(ctx);
  674. c = BN_CTX_get(ctx);
  675. u = BN_CTX_get(ctx);
  676. v = BN_CTX_get(ctx);
  677. if (v == NULL)
  678. goto err;
  679. if (!BN_one(b))
  680. goto err;
  681. if (!BN_GF2m_mod(u, a, p))
  682. goto err;
  683. if (!BN_copy(v, p))
  684. goto err;
  685. if (BN_is_zero(u))
  686. goto err;
  687. while (1) {
  688. while (!BN_is_odd(u)) {
  689. if (BN_is_zero(u))
  690. goto err;
  691. if (!BN_rshift1(u, u))
  692. goto err;
  693. if (BN_is_odd(b)) {
  694. if (!BN_GF2m_add(b, b, p))
  695. goto err;
  696. }
  697. if (!BN_rshift1(b, b))
  698. goto err;
  699. }
  700. if (BN_abs_is_word(u, 1))
  701. break;
  702. if (BN_num_bits(u) < BN_num_bits(v)) {
  703. tmp = u;
  704. u = v;
  705. v = tmp;
  706. tmp = b;
  707. b = c;
  708. c = tmp;
  709. }
  710. if (!BN_GF2m_add(u, u, v))
  711. goto err;
  712. if (!BN_GF2m_add(b, b, c))
  713. goto err;
  714. }
  715. if (!BN_copy(r, b))
  716. goto err;
  717. bn_check_top(r);
  718. ret = 1;
  719. err:
  720. BN_CTX_end(ctx);
  721. return ret;
  722. }
  723. /*
  724. * Invert xx, reduce modulo p, and store the result in r. r could be xx.
  725. * This function calls down to the BN_GF2m_mod_inv implementation; this
  726. * wrapper function is only provided for convenience; for best performance,
  727. * use the BN_GF2m_mod_inv function.
  728. */
  729. int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const unsigned int p[],
  730. BN_CTX *ctx)
  731. {
  732. BIGNUM *field;
  733. int ret = 0;
  734. bn_check_top(xx);
  735. BN_CTX_start(ctx);
  736. if ((field = BN_CTX_get(ctx)) == NULL)
  737. goto err;
  738. if (!BN_GF2m_arr2poly(p, field))
  739. goto err;
  740. ret = BN_GF2m_mod_inv(r, xx, field, ctx);
  741. bn_check_top(r);
  742. err:
  743. BN_CTX_end(ctx);
  744. return ret;
  745. }
  746. #ifndef OPENSSL_SUN_GF2M_DIV
  747. /*
  748. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  749. * or y, x could equal y.
  750. */
  751. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  752. const BIGNUM *p, BN_CTX *ctx)
  753. {
  754. BIGNUM *xinv = NULL;
  755. int ret = 0;
  756. bn_check_top(y);
  757. bn_check_top(x);
  758. bn_check_top(p);
  759. BN_CTX_start(ctx);
  760. xinv = BN_CTX_get(ctx);
  761. if (xinv == NULL)
  762. goto err;
  763. if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
  764. goto err;
  765. if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
  766. goto err;
  767. bn_check_top(r);
  768. ret = 1;
  769. err:
  770. BN_CTX_end(ctx);
  771. return ret;
  772. }
  773. #else
  774. /*
  775. * Divide y by x, reduce modulo p, and store the result in r. r could be x
  776. * or y, x could equal y. Uses algorithm Modular_Division_GF(2^m) from
  777. * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to the
  778. * Great Divide".
  779. */
  780. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  781. const BIGNUM *p, BN_CTX *ctx)
  782. {
  783. BIGNUM *a, *b, *u, *v;
  784. int ret = 0;
  785. bn_check_top(y);
  786. bn_check_top(x);
  787. bn_check_top(p);
  788. BN_CTX_start(ctx);
  789. a = BN_CTX_get(ctx);
  790. b = BN_CTX_get(ctx);
  791. u = BN_CTX_get(ctx);
  792. v = BN_CTX_get(ctx);
  793. if (v == NULL)
  794. goto err;
  795. /* reduce x and y mod p */
  796. if (!BN_GF2m_mod(u, y, p))
  797. goto err;
  798. if (!BN_GF2m_mod(a, x, p))
  799. goto err;
  800. if (!BN_copy(b, p))
  801. goto err;
  802. while (!BN_is_odd(a)) {
  803. if (!BN_rshift1(a, a))
  804. goto err;
  805. if (BN_is_odd(u))
  806. if (!BN_GF2m_add(u, u, p))
  807. goto err;
  808. if (!BN_rshift1(u, u))
  809. goto err;
  810. }
  811. do {
  812. if (BN_GF2m_cmp(b, a) > 0) {
  813. if (!BN_GF2m_add(b, b, a))
  814. goto err;
  815. if (!BN_GF2m_add(v, v, u))
  816. goto err;
  817. do {
  818. if (!BN_rshift1(b, b))
  819. goto err;
  820. if (BN_is_odd(v))
  821. if (!BN_GF2m_add(v, v, p))
  822. goto err;
  823. if (!BN_rshift1(v, v))
  824. goto err;
  825. } while (!BN_is_odd(b));
  826. } else if (BN_abs_is_word(a, 1))
  827. break;
  828. else {
  829. if (!BN_GF2m_add(a, a, b))
  830. goto err;
  831. if (!BN_GF2m_add(u, u, v))
  832. goto err;
  833. do {
  834. if (!BN_rshift1(a, a))
  835. goto err;
  836. if (BN_is_odd(u))
  837. if (!BN_GF2m_add(u, u, p))
  838. goto err;
  839. if (!BN_rshift1(u, u))
  840. goto err;
  841. } while (!BN_is_odd(a));
  842. }
  843. } while (1);
  844. if (!BN_copy(r, u))
  845. goto err;
  846. bn_check_top(r);
  847. ret = 1;
  848. err:
  849. BN_CTX_end(ctx);
  850. return ret;
  851. }
  852. #endif
  853. /*
  854. * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
  855. * * or yy, xx could equal yy. This function calls down to the
  856. * BN_GF2m_mod_div implementation; this wrapper function is only provided for
  857. * convenience; for best performance, use the BN_GF2m_mod_div function.
  858. */
  859. int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
  860. const unsigned int p[], BN_CTX *ctx)
  861. {
  862. BIGNUM *field;
  863. int ret = 0;
  864. bn_check_top(yy);
  865. bn_check_top(xx);
  866. BN_CTX_start(ctx);
  867. if ((field = BN_CTX_get(ctx)) == NULL)
  868. goto err;
  869. if (!BN_GF2m_arr2poly(p, field))
  870. goto err;
  871. ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
  872. bn_check_top(r);
  873. err:
  874. BN_CTX_end(ctx);
  875. return ret;
  876. }
  877. /*
  878. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  879. * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
  880. * P1363.
  881. */
  882. int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  883. const unsigned int p[], BN_CTX *ctx)
  884. {
  885. int ret = 0, i, n;
  886. BIGNUM *u;
  887. bn_check_top(a);
  888. bn_check_top(b);
  889. if (BN_is_zero(b))
  890. return (BN_one(r));
  891. if (BN_abs_is_word(b, 1))
  892. return (BN_copy(r, a) != NULL);
  893. BN_CTX_start(ctx);
  894. if ((u = BN_CTX_get(ctx)) == NULL)
  895. goto err;
  896. if (!BN_GF2m_mod_arr(u, a, p))
  897. goto err;
  898. n = BN_num_bits(b) - 1;
  899. for (i = n - 1; i >= 0; i--) {
  900. if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
  901. goto err;
  902. if (BN_is_bit_set(b, i)) {
  903. if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
  904. goto err;
  905. }
  906. }
  907. if (!BN_copy(r, u))
  908. goto err;
  909. bn_check_top(r);
  910. ret = 1;
  911. err:
  912. BN_CTX_end(ctx);
  913. return ret;
  914. }
  915. /*
  916. * Compute the bth power of a, reduce modulo p, and store the result in r. r
  917. * could be a. This function calls down to the BN_GF2m_mod_exp_arr
  918. * implementation; this wrapper function is only provided for convenience;
  919. * for best performance, use the BN_GF2m_mod_exp_arr function.
  920. */
  921. int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  922. const BIGNUM *p, BN_CTX *ctx)
  923. {
  924. int ret = 0;
  925. const int max = BN_num_bits(p);
  926. unsigned int *arr = NULL;
  927. bn_check_top(a);
  928. bn_check_top(b);
  929. bn_check_top(p);
  930. if ((arr =
  931. (unsigned int *)OPENSSL_malloc(sizeof(unsigned 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 unsigned 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);
  981. unsigned int *arr = NULL;
  982. bn_check_top(a);
  983. bn_check_top(p);
  984. if ((arr =
  985. (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
  986. goto err;
  987. ret = BN_GF2m_poly2arr(p, arr, max);
  988. if (!ret || ret > max) {
  989. BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);
  990. goto err;
  991. }
  992. ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
  993. bn_check_top(r);
  994. err:
  995. if (arr)
  996. OPENSSL_free(arr);
  997. return ret;
  998. }
  999. /*
  1000. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  1001. * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
  1002. */
  1003. int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_,
  1004. const unsigned int p[], BN_CTX *ctx)
  1005. {
  1006. int ret = 0, count = 0;
  1007. unsigned int j;
  1008. BIGNUM *a, *z, *rho, *w, *w2, *tmp;
  1009. bn_check_top(a_);
  1010. if (!p[0]) {
  1011. /* reduction mod 1 => return 0 */
  1012. BN_zero(r);
  1013. return 1;
  1014. }
  1015. BN_CTX_start(ctx);
  1016. a = BN_CTX_get(ctx);
  1017. z = BN_CTX_get(ctx);
  1018. w = BN_CTX_get(ctx);
  1019. if (w == NULL)
  1020. goto err;
  1021. if (!BN_GF2m_mod_arr(a, a_, p))
  1022. goto err;
  1023. if (BN_is_zero(a)) {
  1024. BN_zero(r);
  1025. ret = 1;
  1026. goto err;
  1027. }
  1028. if (p[0] & 0x1) { /* m is odd */
  1029. /* compute half-trace of a */
  1030. if (!BN_copy(z, a))
  1031. goto err;
  1032. for (j = 1; j <= (p[0] - 1) / 2; j++) {
  1033. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1034. goto err;
  1035. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1036. goto err;
  1037. if (!BN_GF2m_add(z, z, a))
  1038. goto err;
  1039. }
  1040. } else { /* m is even */
  1041. rho = BN_CTX_get(ctx);
  1042. w2 = BN_CTX_get(ctx);
  1043. tmp = BN_CTX_get(ctx);
  1044. if (tmp == NULL)
  1045. goto err;
  1046. do {
  1047. if (!BN_rand(rho, p[0], 0, 0))
  1048. goto err;
  1049. if (!BN_GF2m_mod_arr(rho, rho, p))
  1050. goto err;
  1051. BN_zero(z);
  1052. if (!BN_copy(w, rho))
  1053. goto err;
  1054. for (j = 1; j <= p[0] - 1; j++) {
  1055. if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
  1056. goto err;
  1057. if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
  1058. goto err;
  1059. if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
  1060. goto err;
  1061. if (!BN_GF2m_add(z, z, tmp))
  1062. goto err;
  1063. if (!BN_GF2m_add(w, w2, rho))
  1064. goto err;
  1065. }
  1066. count++;
  1067. } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
  1068. if (BN_is_zero(w)) {
  1069. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);
  1070. goto err;
  1071. }
  1072. }
  1073. if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
  1074. goto err;
  1075. if (!BN_GF2m_add(w, z, w))
  1076. goto err;
  1077. if (BN_GF2m_cmp(w, a)) {
  1078. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
  1079. goto err;
  1080. }
  1081. if (!BN_copy(r, z))
  1082. goto err;
  1083. bn_check_top(r);
  1084. ret = 1;
  1085. err:
  1086. BN_CTX_end(ctx);
  1087. return ret;
  1088. }
  1089. /*
  1090. * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
  1091. * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
  1092. * implementation; this wrapper function is only provided for convenience;
  1093. * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
  1094. */
  1095. int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  1096. BN_CTX *ctx)
  1097. {
  1098. int ret = 0;
  1099. const int max = BN_num_bits(p);
  1100. unsigned int *arr = NULL;
  1101. bn_check_top(a);
  1102. bn_check_top(p);
  1103. if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) *
  1104. max)) == NULL)
  1105. goto err;
  1106. ret = BN_GF2m_poly2arr(p, arr, max);
  1107. if (!ret || ret > max) {
  1108. BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);
  1109. goto err;
  1110. }
  1111. ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
  1112. bn_check_top(r);
  1113. err:
  1114. if (arr)
  1115. OPENSSL_free(arr);
  1116. return ret;
  1117. }
  1118. /*
  1119. * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
  1120. * x^i , where a_0 is *not* zero) into an array of integers corresponding to
  1121. * the bits with non-zero coefficient. Up to max elements of the array will
  1122. * be filled. Return value is total number of coefficients that would be
  1123. * extracted if array was large enough.
  1124. */
  1125. int BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max)
  1126. {
  1127. int i, j, k = 0;
  1128. BN_ULONG mask;
  1129. if (BN_is_zero(a) || !BN_is_bit_set(a, 0))
  1130. /*
  1131. * a_0 == 0 => return error (the unsigned int array must be
  1132. * terminated by 0)
  1133. */
  1134. return 0;
  1135. for (i = a->top - 1; i >= 0; i--) {
  1136. if (!a->d[i])
  1137. /* skip word if a->d[i] == 0 */
  1138. continue;
  1139. mask = BN_TBIT;
  1140. for (j = BN_BITS2 - 1; j >= 0; j--) {
  1141. if (a->d[i] & mask) {
  1142. if (k < max)
  1143. p[k] = BN_BITS2 * i + j;
  1144. k++;
  1145. }
  1146. mask >>= 1;
  1147. }
  1148. }
  1149. return k;
  1150. }
  1151. /*
  1152. * Convert the coefficient array representation of a polynomial to a
  1153. * bit-string. The array must be terminated by 0.
  1154. */
  1155. int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a)
  1156. {
  1157. int i;
  1158. bn_check_top(a);
  1159. BN_zero(a);
  1160. for (i = 0; p[i] != 0; i++) {
  1161. if (BN_set_bit(a, p[i]) == 0)
  1162. return 0;
  1163. }
  1164. BN_set_bit(a, 0);
  1165. bn_check_top(a);
  1166. return 1;
  1167. }
  1168. /*
  1169. * Constant-time conditional swap of a and b.
  1170. * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
  1171. * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
  1172. * and that no more than nwords are used by either a or b.
  1173. * a and b cannot be the same number
  1174. */
  1175. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  1176. {
  1177. BN_ULONG t;
  1178. int i;
  1179. bn_wcheck_size(a, nwords);
  1180. bn_wcheck_size(b, nwords);
  1181. assert(a != b);
  1182. assert((condition & (condition - 1)) == 0);
  1183. assert(sizeof(BN_ULONG) >= sizeof(int));
  1184. condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
  1185. t = (a->top ^ b->top) & condition;
  1186. a->top ^= t;
  1187. b->top ^= t;
  1188. #define BN_CONSTTIME_SWAP(ind) \
  1189. do { \
  1190. t = (a->d[ind] ^ b->d[ind]) & condition; \
  1191. a->d[ind] ^= t; \
  1192. b->d[ind] ^= t; \
  1193. } while (0)
  1194. switch (nwords) {
  1195. default:
  1196. for (i = 10; i < nwords; i++)
  1197. BN_CONSTTIME_SWAP(i);
  1198. /* Fallthrough */
  1199. case 10:
  1200. BN_CONSTTIME_SWAP(9); /* Fallthrough */
  1201. case 9:
  1202. BN_CONSTTIME_SWAP(8); /* Fallthrough */
  1203. case 8:
  1204. BN_CONSTTIME_SWAP(7); /* Fallthrough */
  1205. case 7:
  1206. BN_CONSTTIME_SWAP(6); /* Fallthrough */
  1207. case 6:
  1208. BN_CONSTTIME_SWAP(5); /* Fallthrough */
  1209. case 5:
  1210. BN_CONSTTIME_SWAP(4); /* Fallthrough */
  1211. case 4:
  1212. BN_CONSTTIME_SWAP(3); /* Fallthrough */
  1213. case 3:
  1214. BN_CONSTTIME_SWAP(2); /* Fallthrough */
  1215. case 2:
  1216. BN_CONSTTIME_SWAP(1); /* Fallthrough */
  1217. case 1:
  1218. BN_CONSTTIME_SWAP(0);
  1219. }
  1220. #undef BN_CONSTTIME_SWAP
  1221. }