bn_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /* crypto/bn/bn_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #ifndef BN_DEBUG
  59. # undef NDEBUG /* avoid conflicting definitions */
  60. # define NDEBUG
  61. #endif
  62. #include <assert.h>
  63. #include <limits.h>
  64. #include <stdio.h>
  65. #include "cryptlib.h"
  66. #include "bn_lcl.h"
  67. const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT;
  68. /* For a 32 bit machine
  69. * 2 - 4 == 128
  70. * 3 - 8 == 256
  71. * 4 - 16 == 512
  72. * 5 - 32 == 1024
  73. * 6 - 64 == 2048
  74. * 7 - 128 == 4096
  75. * 8 - 256 == 8192
  76. */
  77. static int bn_limit_bits=0;
  78. static int bn_limit_num=8; /* (1<<bn_limit_bits) */
  79. static int bn_limit_bits_low=0;
  80. static int bn_limit_num_low=8; /* (1<<bn_limit_bits_low) */
  81. static int bn_limit_bits_high=0;
  82. static int bn_limit_num_high=8; /* (1<<bn_limit_bits_high) */
  83. static int bn_limit_bits_mont=0;
  84. static int bn_limit_num_mont=8; /* (1<<bn_limit_bits_mont) */
  85. void BN_set_params(int mult, int high, int low, int mont)
  86. {
  87. if (mult >= 0)
  88. {
  89. if (mult > (sizeof(int)*8)-1)
  90. mult=sizeof(int)*8-1;
  91. bn_limit_bits=mult;
  92. bn_limit_num=1<<mult;
  93. }
  94. if (high >= 0)
  95. {
  96. if (high > (sizeof(int)*8)-1)
  97. high=sizeof(int)*8-1;
  98. bn_limit_bits_high=high;
  99. bn_limit_num_high=1<<high;
  100. }
  101. if (low >= 0)
  102. {
  103. if (low > (sizeof(int)*8)-1)
  104. low=sizeof(int)*8-1;
  105. bn_limit_bits_low=low;
  106. bn_limit_num_low=1<<low;
  107. }
  108. if (mont >= 0)
  109. {
  110. if (mont > (sizeof(int)*8)-1)
  111. mont=sizeof(int)*8-1;
  112. bn_limit_bits_mont=mont;
  113. bn_limit_num_mont=1<<mont;
  114. }
  115. }
  116. int BN_get_params(int which)
  117. {
  118. if (which == 0) return(bn_limit_bits);
  119. else if (which == 1) return(bn_limit_bits_high);
  120. else if (which == 2) return(bn_limit_bits_low);
  121. else if (which == 3) return(bn_limit_bits_mont);
  122. else return(0);
  123. }
  124. const BIGNUM *BN_value_one(void)
  125. {
  126. static BN_ULONG data_one=1L;
  127. static BIGNUM const_one={&data_one,1,1,0};
  128. return(&const_one);
  129. }
  130. char *BN_options(void)
  131. {
  132. static int init=0;
  133. static char data[16];
  134. if (!init)
  135. {
  136. init++;
  137. #ifdef BN_LLONG
  138. sprintf(data,"bn(%d,%d)",(int)sizeof(BN_ULLONG)*8,
  139. (int)sizeof(BN_ULONG)*8);
  140. #else
  141. sprintf(data,"bn(%d,%d)",(int)sizeof(BN_ULONG)*8,
  142. (int)sizeof(BN_ULONG)*8);
  143. #endif
  144. }
  145. return(data);
  146. }
  147. int BN_num_bits_word(BN_ULONG l)
  148. {
  149. static const char bits[256]={
  150. 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
  151. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  152. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  153. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  154. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  155. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  156. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  157. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  158. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  159. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  160. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  161. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  162. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  163. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  164. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  165. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  166. };
  167. #if defined(SIXTY_FOUR_BIT_LONG)
  168. if (l & 0xffffffff00000000L)
  169. {
  170. if (l & 0xffff000000000000L)
  171. {
  172. if (l & 0xff00000000000000L)
  173. {
  174. return(bits[(int)(l>>56)]+56);
  175. }
  176. else return(bits[(int)(l>>48)]+48);
  177. }
  178. else
  179. {
  180. if (l & 0x0000ff0000000000L)
  181. {
  182. return(bits[(int)(l>>40)]+40);
  183. }
  184. else return(bits[(int)(l>>32)]+32);
  185. }
  186. }
  187. else
  188. #else
  189. #ifdef SIXTY_FOUR_BIT
  190. if (l & 0xffffffff00000000LL)
  191. {
  192. if (l & 0xffff000000000000LL)
  193. {
  194. if (l & 0xff00000000000000LL)
  195. {
  196. return(bits[(int)(l>>56)]+56);
  197. }
  198. else return(bits[(int)(l>>48)]+48);
  199. }
  200. else
  201. {
  202. if (l & 0x0000ff0000000000LL)
  203. {
  204. return(bits[(int)(l>>40)]+40);
  205. }
  206. else return(bits[(int)(l>>32)]+32);
  207. }
  208. }
  209. else
  210. #endif
  211. #endif
  212. {
  213. #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
  214. if (l & 0xffff0000L)
  215. {
  216. if (l & 0xff000000L)
  217. return(bits[(int)(l>>24L)]+24);
  218. else return(bits[(int)(l>>16L)]+16);
  219. }
  220. else
  221. #endif
  222. {
  223. #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
  224. if (l & 0xff00L)
  225. return(bits[(int)(l>>8)]+8);
  226. else
  227. #endif
  228. return(bits[(int)(l )] );
  229. }
  230. }
  231. }
  232. int BN_num_bits(const BIGNUM *a)
  233. {
  234. BN_ULONG l;
  235. int i;
  236. bn_check_top(a);
  237. if (a->top == 0) return(0);
  238. l=a->d[a->top-1];
  239. assert(l != 0);
  240. i=(a->top-1)*BN_BITS2;
  241. return(i+BN_num_bits_word(l));
  242. }
  243. void BN_clear_free(BIGNUM *a)
  244. {
  245. int i;
  246. if (a == NULL) return;
  247. if (a->d != NULL)
  248. {
  249. OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0]));
  250. if (!(BN_get_flags(a,BN_FLG_STATIC_DATA)))
  251. OPENSSL_free(a->d);
  252. }
  253. i=BN_get_flags(a,BN_FLG_MALLOCED);
  254. OPENSSL_cleanse(a,sizeof(BIGNUM));
  255. if (i)
  256. OPENSSL_free(a);
  257. }
  258. void BN_free(BIGNUM *a)
  259. {
  260. if (a == NULL) return;
  261. if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA)))
  262. OPENSSL_free(a->d);
  263. a->flags|=BN_FLG_FREE; /* REMOVE? */
  264. if (a->flags & BN_FLG_MALLOCED)
  265. OPENSSL_free(a);
  266. }
  267. void BN_init(BIGNUM *a)
  268. {
  269. memset(a,0,sizeof(BIGNUM));
  270. }
  271. BIGNUM *BN_new(void)
  272. {
  273. BIGNUM *ret;
  274. if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL)
  275. {
  276. BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);
  277. return(NULL);
  278. }
  279. ret->flags=BN_FLG_MALLOCED;
  280. ret->top=0;
  281. ret->neg=0;
  282. ret->dmax=0;
  283. ret->d=NULL;
  284. return(ret);
  285. }
  286. /* This is used both by bn_expand2() and bn_dup_expand() */
  287. /* The caller MUST check that words > b->dmax before calling this */
  288. static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
  289. {
  290. BN_ULONG *A,*a = NULL;
  291. const BN_ULONG *B;
  292. int i;
  293. if (words > (INT_MAX/(4*BN_BITS2)))
  294. {
  295. BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG);
  296. return NULL;
  297. }
  298. bn_check_top(b);
  299. if (BN_get_flags(b,BN_FLG_STATIC_DATA))
  300. {
  301. BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  302. return(NULL);
  303. }
  304. a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*(words+1));
  305. if (A == NULL)
  306. {
  307. BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE);
  308. return(NULL);
  309. }
  310. #if 1
  311. B=b->d;
  312. /* Check if the previous number needs to be copied */
  313. if (B != NULL)
  314. {
  315. for (i=b->top>>2; i>0; i--,A+=4,B+=4)
  316. {
  317. /*
  318. * The fact that the loop is unrolled
  319. * 4-wise is a tribute to Intel. It's
  320. * the one that doesn't have enough
  321. * registers to accomodate more data.
  322. * I'd unroll it 8-wise otherwise:-)
  323. *
  324. * <appro@fy.chalmers.se>
  325. */
  326. BN_ULONG a0,a1,a2,a3;
  327. a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
  328. A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
  329. }
  330. switch (b->top&3)
  331. {
  332. case 3: A[2]=B[2];
  333. case 2: A[1]=B[1];
  334. case 1: A[0]=B[0];
  335. case 0: /* workaround for ultrix cc: without 'case 0', the optimizer does
  336. * the switch table by doing a=top&3; a--; goto jump_table[a];
  337. * which fails for top== 0 */
  338. ;
  339. }
  340. }
  341. /* Now need to zero any data between b->top and b->max */
  342. /* XXX Why? */
  343. A= &(a[b->top]);
  344. for (i=(words - b->top)>>3; i>0; i--,A+=8)
  345. {
  346. A[0]=0; A[1]=0; A[2]=0; A[3]=0;
  347. A[4]=0; A[5]=0; A[6]=0; A[7]=0;
  348. }
  349. for (i=(words - b->top)&7; i>0; i--,A++)
  350. A[0]=0;
  351. #else
  352. memset(A,0,sizeof(BN_ULONG)*(words+1));
  353. memcpy(A,b->d,sizeof(b->d[0])*b->top);
  354. #endif
  355. return(a);
  356. }
  357. /* This is an internal function that can be used instead of bn_expand2()
  358. * when there is a need to copy BIGNUMs instead of only expanding the
  359. * data part, while still expanding them.
  360. * Especially useful when needing to expand BIGNUMs that are declared
  361. * 'const' and should therefore not be changed.
  362. * The reason to use this instead of a BN_dup() followed by a bn_expand2()
  363. * is memory allocation overhead. A BN_dup() followed by a bn_expand2()
  364. * will allocate new memory for the BIGNUM data twice, and free it once,
  365. * while bn_dup_expand() makes sure allocation is made only once.
  366. */
  367. BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
  368. {
  369. BIGNUM *r = NULL;
  370. /* This function does not work if
  371. * words <= b->dmax && top < words
  372. * because BN_dup() does not preserve 'dmax'!
  373. * (But bn_dup_expand() is not used anywhere yet.)
  374. */
  375. if (words > b->dmax)
  376. {
  377. BN_ULONG *a = bn_expand_internal(b, words);
  378. if (a)
  379. {
  380. r = BN_new();
  381. if (r)
  382. {
  383. r->top = b->top;
  384. r->dmax = words;
  385. r->neg = b->neg;
  386. r->d = a;
  387. }
  388. else
  389. {
  390. /* r == NULL, BN_new failure */
  391. OPENSSL_free(a);
  392. }
  393. }
  394. /* If a == NULL, there was an error in allocation in
  395. bn_expand_internal(), and NULL should be returned */
  396. }
  397. else
  398. {
  399. r = BN_dup(b);
  400. }
  401. return r;
  402. }
  403. /* This is an internal function that should not be used in applications.
  404. * It ensures that 'b' has enough room for a 'words' word number number.
  405. * It is mostly used by the various BIGNUM routines. If there is an error,
  406. * NULL is returned. If not, 'b' is returned. */
  407. BIGNUM *bn_expand2(BIGNUM *b, int words)
  408. {
  409. if (words > b->dmax)
  410. {
  411. BN_ULONG *a = bn_expand_internal(b, words);
  412. if (a)
  413. {
  414. if (b->d)
  415. OPENSSL_free(b->d);
  416. b->d=a;
  417. b->dmax=words;
  418. }
  419. else
  420. b = NULL;
  421. }
  422. return b;
  423. }
  424. BIGNUM *BN_dup(const BIGNUM *a)
  425. {
  426. BIGNUM *r, *t;
  427. if (a == NULL) return NULL;
  428. bn_check_top(a);
  429. t = BN_new();
  430. if (t == NULL) return(NULL);
  431. r = BN_copy(t, a);
  432. /* now r == t || r == NULL */
  433. if (r == NULL)
  434. BN_free(t);
  435. return r;
  436. }
  437. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  438. {
  439. int i;
  440. BN_ULONG *A;
  441. const BN_ULONG *B;
  442. bn_check_top(b);
  443. if (a == b) return(a);
  444. if (bn_wexpand(a,b->top) == NULL) return(NULL);
  445. #if 1
  446. A=a->d;
  447. B=b->d;
  448. for (i=b->top>>2; i>0; i--,A+=4,B+=4)
  449. {
  450. BN_ULONG a0,a1,a2,a3;
  451. a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
  452. A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
  453. }
  454. switch (b->top&3)
  455. {
  456. case 3: A[2]=B[2];
  457. case 2: A[1]=B[1];
  458. case 1: A[0]=B[0];
  459. case 0: ; /* ultrix cc workaround, see comments in bn_expand_internal */
  460. }
  461. #else
  462. memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
  463. #endif
  464. /* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/
  465. a->top=b->top;
  466. if ((a->top == 0) && (a->d != NULL))
  467. a->d[0]=0;
  468. a->neg=b->neg;
  469. return(a);
  470. }
  471. void BN_swap(BIGNUM *a, BIGNUM *b)
  472. {
  473. int flags_old_a, flags_old_b;
  474. BN_ULONG *tmp_d;
  475. int tmp_top, tmp_dmax, tmp_neg;
  476. flags_old_a = a->flags;
  477. flags_old_b = b->flags;
  478. tmp_d = a->d;
  479. tmp_top = a->top;
  480. tmp_dmax = a->dmax;
  481. tmp_neg = a->neg;
  482. a->d = b->d;
  483. a->top = b->top;
  484. a->dmax = b->dmax;
  485. a->neg = b->neg;
  486. b->d = tmp_d;
  487. b->top = tmp_top;
  488. b->dmax = tmp_dmax;
  489. b->neg = tmp_neg;
  490. a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
  491. b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
  492. }
  493. void BN_clear(BIGNUM *a)
  494. {
  495. if (a->d != NULL)
  496. memset(a->d,0,a->dmax*sizeof(a->d[0]));
  497. a->top=0;
  498. a->neg=0;
  499. }
  500. BN_ULONG BN_get_word(const BIGNUM *a)
  501. {
  502. int i,n;
  503. BN_ULONG ret=0;
  504. n=BN_num_bytes(a);
  505. if (n > sizeof(BN_ULONG))
  506. return(BN_MASK2);
  507. for (i=a->top-1; i>=0; i--)
  508. {
  509. #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
  510. ret<<=BN_BITS4; /* stops the compiler complaining */
  511. ret<<=BN_BITS4;
  512. #else
  513. ret=0;
  514. #endif
  515. ret|=a->d[i];
  516. }
  517. return(ret);
  518. }
  519. int BN_set_word(BIGNUM *a, BN_ULONG w)
  520. {
  521. int i,n;
  522. if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);
  523. n=sizeof(BN_ULONG)/BN_BYTES;
  524. a->neg=0;
  525. a->top=0;
  526. a->d[0]=(BN_ULONG)w&BN_MASK2;
  527. if (a->d[0] != 0) a->top=1;
  528. for (i=1; i<n; i++)
  529. {
  530. /* the following is done instead of
  531. * w>>=BN_BITS2 so compilers don't complain
  532. * on builds where sizeof(long) == BN_TYPES */
  533. #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
  534. w>>=BN_BITS4;
  535. w>>=BN_BITS4;
  536. #else
  537. w=0;
  538. #endif
  539. a->d[i]=(BN_ULONG)w&BN_MASK2;
  540. if (a->d[i] != 0) a->top=i+1;
  541. }
  542. return(1);
  543. }
  544. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  545. {
  546. unsigned int i,m;
  547. unsigned int n;
  548. BN_ULONG l;
  549. if (ret == NULL) ret=BN_new();
  550. if (ret == NULL) return(NULL);
  551. l=0;
  552. n=len;
  553. if (n == 0)
  554. {
  555. ret->top=0;
  556. return(ret);
  557. }
  558. if (bn_expand(ret,(int)(n+2)*8) == NULL)
  559. return(NULL);
  560. i=((n-1)/BN_BYTES)+1;
  561. m=((n-1)%(BN_BYTES));
  562. ret->top=i;
  563. ret->neg=0;
  564. while (n-- > 0)
  565. {
  566. l=(l<<8L)| *(s++);
  567. if (m-- == 0)
  568. {
  569. ret->d[--i]=l;
  570. l=0;
  571. m=BN_BYTES-1;
  572. }
  573. }
  574. /* need to call this due to clear byte at top if avoiding
  575. * having the top bit set (-ve number) */
  576. bn_fix_top(ret);
  577. return(ret);
  578. }
  579. /* ignore negative */
  580. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  581. {
  582. int n,i;
  583. BN_ULONG l;
  584. n=i=BN_num_bytes(a);
  585. while (i-- > 0)
  586. {
  587. l=a->d[i/BN_BYTES];
  588. *(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
  589. }
  590. return(n);
  591. }
  592. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  593. {
  594. int i;
  595. BN_ULONG t1,t2,*ap,*bp;
  596. bn_check_top(a);
  597. bn_check_top(b);
  598. i=a->top-b->top;
  599. if (i != 0) return(i);
  600. ap=a->d;
  601. bp=b->d;
  602. for (i=a->top-1; i>=0; i--)
  603. {
  604. t1= ap[i];
  605. t2= bp[i];
  606. if (t1 != t2)
  607. return(t1 > t2?1:-1);
  608. }
  609. return(0);
  610. }
  611. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  612. {
  613. int i;
  614. int gt,lt;
  615. BN_ULONG t1,t2;
  616. if ((a == NULL) || (b == NULL))
  617. {
  618. if (a != NULL)
  619. return(-1);
  620. else if (b != NULL)
  621. return(1);
  622. else
  623. return(0);
  624. }
  625. bn_check_top(a);
  626. bn_check_top(b);
  627. if (a->neg != b->neg)
  628. {
  629. if (a->neg)
  630. return(-1);
  631. else return(1);
  632. }
  633. if (a->neg == 0)
  634. { gt=1; lt= -1; }
  635. else { gt= -1; lt=1; }
  636. if (a->top > b->top) return(gt);
  637. if (a->top < b->top) return(lt);
  638. for (i=a->top-1; i>=0; i--)
  639. {
  640. t1=a->d[i];
  641. t2=b->d[i];
  642. if (t1 > t2) return(gt);
  643. if (t1 < t2) return(lt);
  644. }
  645. return(0);
  646. }
  647. int BN_set_bit(BIGNUM *a, int n)
  648. {
  649. int i,j,k;
  650. i=n/BN_BITS2;
  651. j=n%BN_BITS2;
  652. if (a->top <= i)
  653. {
  654. if (bn_wexpand(a,i+1) == NULL) return(0);
  655. for(k=a->top; k<i+1; k++)
  656. a->d[k]=0;
  657. a->top=i+1;
  658. }
  659. a->d[i]|=(((BN_ULONG)1)<<j);
  660. return(1);
  661. }
  662. int BN_clear_bit(BIGNUM *a, int n)
  663. {
  664. int i,j;
  665. i=n/BN_BITS2;
  666. j=n%BN_BITS2;
  667. if (a->top <= i) return(0);
  668. a->d[i]&=(~(((BN_ULONG)1)<<j));
  669. bn_fix_top(a);
  670. return(1);
  671. }
  672. int BN_is_bit_set(const BIGNUM *a, int n)
  673. {
  674. int i,j;
  675. if (n < 0) return(0);
  676. i=n/BN_BITS2;
  677. j=n%BN_BITS2;
  678. if (a->top <= i) return(0);
  679. return((a->d[i]&(((BN_ULONG)1)<<j))?1:0);
  680. }
  681. int BN_mask_bits(BIGNUM *a, int n)
  682. {
  683. int b,w;
  684. w=n/BN_BITS2;
  685. b=n%BN_BITS2;
  686. if (w >= a->top) return(0);
  687. if (b == 0)
  688. a->top=w;
  689. else
  690. {
  691. a->top=w+1;
  692. a->d[w]&= ~(BN_MASK2<<b);
  693. }
  694. bn_fix_top(a);
  695. return(1);
  696. }
  697. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  698. {
  699. int i;
  700. BN_ULONG aa,bb;
  701. aa=a[n-1];
  702. bb=b[n-1];
  703. if (aa != bb) return((aa > bb)?1:-1);
  704. for (i=n-2; i>=0; i--)
  705. {
  706. aa=a[i];
  707. bb=b[i];
  708. if (aa != bb) return((aa > bb)?1:-1);
  709. }
  710. return(0);
  711. }
  712. /* Here follows a specialised variants of bn_cmp_words(). It has the
  713. property of performing the operation on arrays of different sizes.
  714. The sizes of those arrays is expressed through cl, which is the
  715. common length ( basicall, min(len(a),len(b)) ), and dl, which is the
  716. delta between the two lengths, calculated as len(a)-len(b).
  717. All lengths are the number of BN_ULONGs... */
  718. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
  719. int cl, int dl)
  720. {
  721. int n,i;
  722. n = cl-1;
  723. if (dl < 0)
  724. {
  725. for (i=dl; i<0; i++)
  726. {
  727. if (b[n-i] != 0)
  728. return -1; /* a < b */
  729. }
  730. }
  731. if (dl > 0)
  732. {
  733. for (i=dl; i>0; i--)
  734. {
  735. if (a[n+i] != 0)
  736. return 1; /* a > b */
  737. }
  738. }
  739. return bn_cmp_words(a,b,cl);
  740. }