bn.h 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /* crypto/bn/bn.h */
  2. /* Copyright (C) 1995-1997 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. /* ====================================================================
  59. * Copyright (c) 1998-2018 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the Eric Young open source
  118. * license provided above.
  119. *
  120. * The binary polynomial arithmetic software is originally written by
  121. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  122. *
  123. */
  124. #ifndef HEADER_BN_H
  125. # define HEADER_BN_H
  126. # include <limits.h>
  127. # include <openssl/e_os2.h>
  128. # ifndef OPENSSL_NO_FP_API
  129. # include <stdio.h> /* FILE */
  130. # endif
  131. # include <openssl/ossl_typ.h>
  132. # include <openssl/crypto.h>
  133. #ifdef __cplusplus
  134. extern "C" {
  135. #endif
  136. /*
  137. * These preprocessor symbols control various aspects of the bignum headers
  138. * and library code. They're not defined by any "normal" configuration, as
  139. * they are intended for development and testing purposes. NB: defining all
  140. * three can be useful for debugging application code as well as openssl
  141. * itself. BN_DEBUG - turn on various debugging alterations to the bignum
  142. * code BN_DEBUG_RAND - uses random poisoning of unused words to trip up
  143. * mismanagement of bignum internals. You must also define BN_DEBUG.
  144. */
  145. /* #define BN_DEBUG */
  146. /* #define BN_DEBUG_RAND */
  147. # ifndef OPENSSL_SMALL_FOOTPRINT
  148. # define BN_MUL_COMBA
  149. # define BN_SQR_COMBA
  150. # define BN_RECURSION
  151. # endif
  152. /*
  153. * This next option uses the C libraries (2 word)/(1 word) function. If it is
  154. * not defined, I use my C version (which is slower). The reason for this
  155. * flag is that when the particular C compiler library routine is used, and
  156. * the library is linked with a different compiler, the library is missing.
  157. * This mostly happens when the library is built with gcc and then linked
  158. * using normal cc. This would be a common occurrence because gcc normally
  159. * produces code that is 2 times faster than system compilers for the big
  160. * number stuff. For machines with only one compiler (or shared libraries),
  161. * this should be on. Again this in only really a problem on machines using
  162. * "long long's", are 32bit, and are not using my assembler code.
  163. */
  164. # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
  165. defined(OPENSSL_SYS_WIN32) || defined(linux)
  166. # ifndef BN_DIV2W
  167. # define BN_DIV2W
  168. # endif
  169. # endif
  170. /*
  171. * assuming long is 64bit - this is the DEC Alpha unsigned long long is only
  172. * 64 bits :-(, don't define BN_LLONG for the DEC Alpha
  173. */
  174. # ifdef SIXTY_FOUR_BIT_LONG
  175. # define BN_ULLONG unsigned long long
  176. # define BN_ULONG unsigned long
  177. # define BN_LONG long
  178. # define BN_BITS 128
  179. # define BN_BYTES 8
  180. # define BN_BITS2 64
  181. # define BN_BITS4 32
  182. # define BN_MASK (0xffffffffffffffffffffffffffffffffLL)
  183. # define BN_MASK2 (0xffffffffffffffffL)
  184. # define BN_MASK2l (0xffffffffL)
  185. # define BN_MASK2h (0xffffffff00000000L)
  186. # define BN_MASK2h1 (0xffffffff80000000L)
  187. # define BN_TBIT (0x8000000000000000L)
  188. # define BN_DEC_CONV (10000000000000000000UL)
  189. # define BN_DEC_FMT1 "%lu"
  190. # define BN_DEC_FMT2 "%019lu"
  191. # define BN_DEC_NUM 19
  192. # define BN_HEX_FMT1 "%lX"
  193. # define BN_HEX_FMT2 "%016lX"
  194. # endif
  195. /*
  196. * This is where the long long data type is 64 bits, but long is 32. For
  197. * machines where there are 64bit registers, this is the mode to use. IRIX,
  198. * on R4000 and above should use this mode, along with the relevant assembler
  199. * code :-). Do NOT define BN_LLONG.
  200. */
  201. # ifdef SIXTY_FOUR_BIT
  202. # undef BN_LLONG
  203. # undef BN_ULLONG
  204. # define BN_ULONG unsigned long long
  205. # define BN_LONG long long
  206. # define BN_BITS 128
  207. # define BN_BYTES 8
  208. # define BN_BITS2 64
  209. # define BN_BITS4 32
  210. # define BN_MASK2 (0xffffffffffffffffLL)
  211. # define BN_MASK2l (0xffffffffL)
  212. # define BN_MASK2h (0xffffffff00000000LL)
  213. # define BN_MASK2h1 (0xffffffff80000000LL)
  214. # define BN_TBIT (0x8000000000000000LL)
  215. # define BN_DEC_CONV (10000000000000000000ULL)
  216. # define BN_DEC_FMT1 "%llu"
  217. # define BN_DEC_FMT2 "%019llu"
  218. # define BN_DEC_NUM 19
  219. # define BN_HEX_FMT1 "%llX"
  220. # define BN_HEX_FMT2 "%016llX"
  221. # endif
  222. # ifdef THIRTY_TWO_BIT
  223. # ifdef BN_LLONG
  224. # if defined(_WIN32) && !defined(__GNUC__)
  225. # define BN_ULLONG unsigned __int64
  226. # define BN_MASK (0xffffffffffffffffI64)
  227. # else
  228. # define BN_ULLONG unsigned long long
  229. # define BN_MASK (0xffffffffffffffffLL)
  230. # endif
  231. # endif
  232. # define BN_ULONG unsigned int
  233. # define BN_LONG int
  234. # define BN_BITS 64
  235. # define BN_BYTES 4
  236. # define BN_BITS2 32
  237. # define BN_BITS4 16
  238. # define BN_MASK2 (0xffffffffL)
  239. # define BN_MASK2l (0xffff)
  240. # define BN_MASK2h1 (0xffff8000L)
  241. # define BN_MASK2h (0xffff0000L)
  242. # define BN_TBIT (0x80000000L)
  243. # define BN_DEC_CONV (1000000000L)
  244. # define BN_DEC_FMT1 "%u"
  245. # define BN_DEC_FMT2 "%09u"
  246. # define BN_DEC_NUM 9
  247. # define BN_HEX_FMT1 "%X"
  248. # define BN_HEX_FMT2 "%08X"
  249. # endif
  250. # define BN_DEFAULT_BITS 1280
  251. # define BN_FLG_MALLOCED 0x01
  252. # define BN_FLG_STATIC_DATA 0x02
  253. /*
  254. * avoid leaking exponent information through timing,
  255. * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime,
  256. * BN_div() will call BN_div_no_branch,
  257. * BN_mod_inverse() will call BN_mod_inverse_no_branch.
  258. */
  259. # define BN_FLG_CONSTTIME 0x04
  260. # ifdef OPENSSL_NO_DEPRECATED
  261. /* deprecated name for the flag */
  262. # define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME
  263. /*
  264. * avoid leaking exponent information through timings
  265. * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime)
  266. */
  267. # endif
  268. # ifndef OPENSSL_NO_DEPRECATED
  269. # define BN_FLG_FREE 0x8000
  270. /* used for debuging */
  271. # endif
  272. # define BN_set_flags(b,n) ((b)->flags|=(n))
  273. # define BN_get_flags(b,n) ((b)->flags&(n))
  274. /*
  275. * get a clone of a BIGNUM with changed flags, for *temporary* use only (the
  276. * two BIGNUMs cannot not be used in parallel!)
  277. */
  278. # define BN_with_flags(dest,b,n) ((dest)->d=(b)->d, \
  279. (dest)->top=(b)->top, \
  280. (dest)->dmax=(b)->dmax, \
  281. (dest)->neg=(b)->neg, \
  282. (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
  283. | ((b)->flags & ~BN_FLG_MALLOCED) \
  284. | BN_FLG_STATIC_DATA \
  285. | (n)))
  286. /* Already declared in ossl_typ.h */
  287. # if 0
  288. typedef struct bignum_st BIGNUM;
  289. /* Used for temp variables (declaration hidden in bn_lcl.h) */
  290. typedef struct bignum_ctx BN_CTX;
  291. typedef struct bn_blinding_st BN_BLINDING;
  292. typedef struct bn_mont_ctx_st BN_MONT_CTX;
  293. typedef struct bn_recp_ctx_st BN_RECP_CTX;
  294. typedef struct bn_gencb_st BN_GENCB;
  295. # endif
  296. struct bignum_st {
  297. BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit
  298. * chunks. */
  299. int top; /* Index of last used d +1. */
  300. /* The next are internal book keeping for bn_expand. */
  301. int dmax; /* Size of the d array. */
  302. int neg; /* one if the number is negative */
  303. int flags;
  304. };
  305. /* Used for montgomery multiplication */
  306. struct bn_mont_ctx_st {
  307. int ri; /* number of bits in R */
  308. BIGNUM RR; /* used to convert to montgomery form */
  309. BIGNUM N; /* The modulus */
  310. BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only
  311. * stored for bignum algorithm) */
  312. BN_ULONG n0[2]; /* least significant word(s) of Ni; (type
  313. * changed with 0.9.9, was "BN_ULONG n0;"
  314. * before) */
  315. int flags;
  316. };
  317. /*
  318. * Used for reciprocal division/mod functions It cannot be shared between
  319. * threads
  320. */
  321. struct bn_recp_ctx_st {
  322. BIGNUM N; /* the divisor */
  323. BIGNUM Nr; /* the reciprocal */
  324. int num_bits;
  325. int shift;
  326. int flags;
  327. };
  328. /* Used for slow "generation" functions. */
  329. struct bn_gencb_st {
  330. unsigned int ver; /* To handle binary (in)compatibility */
  331. void *arg; /* callback-specific data */
  332. union {
  333. /* if(ver==1) - handles old style callbacks */
  334. void (*cb_1) (int, int, void *);
  335. /* if(ver==2) - new callback style */
  336. int (*cb_2) (int, int, BN_GENCB *);
  337. } cb;
  338. };
  339. /* Wrapper function to make using BN_GENCB easier, */
  340. int BN_GENCB_call(BN_GENCB *cb, int a, int b);
  341. /* Macro to populate a BN_GENCB structure with an "old"-style callback */
  342. # define BN_GENCB_set_old(gencb, callback, cb_arg) { \
  343. BN_GENCB *tmp_gencb = (gencb); \
  344. tmp_gencb->ver = 1; \
  345. tmp_gencb->arg = (cb_arg); \
  346. tmp_gencb->cb.cb_1 = (callback); }
  347. /* Macro to populate a BN_GENCB structure with a "new"-style callback */
  348. # define BN_GENCB_set(gencb, callback, cb_arg) { \
  349. BN_GENCB *tmp_gencb = (gencb); \
  350. tmp_gencb->ver = 2; \
  351. tmp_gencb->arg = (cb_arg); \
  352. tmp_gencb->cb.cb_2 = (callback); }
  353. # define BN_prime_checks 0 /* default: select number of iterations based
  354. * on the size of the number */
  355. /*
  356. * BN_prime_checks_for_size() returns the number of Miller-Rabin iterations
  357. * that will be done for checking that a random number is probably prime. The
  358. * error rate for accepting a composite number as prime depends on the size of
  359. * the prime |b|. The error rates used are for calculating an RSA key with 2 primes,
  360. * and so the level is what you would expect for a key of double the size of the
  361. * prime.
  362. *
  363. * This table is generated using the algorithm of FIPS PUB 186-4
  364. * Digital Signature Standard (DSS), section F.1, page 117.
  365. * (https://dx.doi.org/10.6028/NIST.FIPS.186-4)
  366. *
  367. * The following magma script was used to generate the output:
  368. * securitybits:=125;
  369. * k:=1024;
  370. * for t:=1 to 65 do
  371. * for M:=3 to Floor(2*Sqrt(k-1)-1) do
  372. * S:=0;
  373. * // Sum over m
  374. * for m:=3 to M do
  375. * s:=0;
  376. * // Sum over j
  377. * for j:=2 to m do
  378. * s+:=(RealField(32)!2)^-(j+(k-1)/j);
  379. * end for;
  380. * S+:=2^(m-(m-1)*t)*s;
  381. * end for;
  382. * A:=2^(k-2-M*t);
  383. * B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S;
  384. * pkt:=2.00743*Log(2)*k*2^-k*(A+B);
  385. * seclevel:=Floor(-Log(2,pkt));
  386. * if seclevel ge securitybits then
  387. * printf "k: %5o, security: %o bits (t: %o, M: %o)\n",k,seclevel,t,M;
  388. * break;
  389. * end if;
  390. * end for;
  391. * if seclevel ge securitybits then break; end if;
  392. * end for;
  393. *
  394. * It can be run online at:
  395. * http://magma.maths.usyd.edu.au/calc
  396. *
  397. * And will output:
  398. * k: 1024, security: 129 bits (t: 6, M: 23)
  399. *
  400. * k is the number of bits of the prime, securitybits is the level we want to
  401. * reach.
  402. *
  403. * prime length | RSA key size | # MR tests | security level
  404. * -------------+--------------|------------+---------------
  405. * (b) >= 6394 | >= 12788 | 3 | 256 bit
  406. * (b) >= 3747 | >= 7494 | 3 | 192 bit
  407. * (b) >= 1345 | >= 2690 | 4 | 128 bit
  408. * (b) >= 1080 | >= 2160 | 5 | 128 bit
  409. * (b) >= 852 | >= 1704 | 5 | 112 bit
  410. * (b) >= 476 | >= 952 | 5 | 80 bit
  411. * (b) >= 400 | >= 800 | 6 | 80 bit
  412. * (b) >= 347 | >= 694 | 7 | 80 bit
  413. * (b) >= 308 | >= 616 | 8 | 80 bit
  414. * (b) >= 55 | >= 110 | 27 | 64 bit
  415. * (b) >= 6 | >= 12 | 34 | 64 bit
  416. */
  417. # define BN_prime_checks_for_size(b) ((b) >= 3747 ? 3 : \
  418. (b) >= 1345 ? 4 : \
  419. (b) >= 476 ? 5 : \
  420. (b) >= 400 ? 6 : \
  421. (b) >= 347 ? 7 : \
  422. (b) >= 308 ? 8 : \
  423. (b) >= 55 ? 27 : \
  424. /* b >= 6 */ 34)
  425. # define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
  426. /* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */
  427. # define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
  428. (((w) == 0) && ((a)->top == 0)))
  429. # define BN_is_zero(a) ((a)->top == 0)
  430. # define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg)
  431. # define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
  432. # define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
  433. # define BN_one(a) (BN_set_word((a),1))
  434. # define BN_zero_ex(a) \
  435. do { \
  436. BIGNUM *_tmp_bn = (a); \
  437. _tmp_bn->top = 0; \
  438. _tmp_bn->neg = 0; \
  439. } while(0)
  440. # ifdef OPENSSL_NO_DEPRECATED
  441. # define BN_zero(a) BN_zero_ex(a)
  442. # else
  443. # define BN_zero(a) (BN_set_word((a),0))
  444. # endif
  445. const BIGNUM *BN_value_one(void);
  446. char *BN_options(void);
  447. BN_CTX *BN_CTX_new(void);
  448. # ifndef OPENSSL_NO_DEPRECATED
  449. void BN_CTX_init(BN_CTX *c);
  450. # endif
  451. void BN_CTX_free(BN_CTX *c);
  452. void BN_CTX_start(BN_CTX *ctx);
  453. BIGNUM *BN_CTX_get(BN_CTX *ctx);
  454. void BN_CTX_end(BN_CTX *ctx);
  455. int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
  456. int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
  457. int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
  458. int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
  459. int BN_num_bits(const BIGNUM *a);
  460. int BN_num_bits_word(BN_ULONG);
  461. BIGNUM *BN_new(void);
  462. void BN_init(BIGNUM *);
  463. void BN_clear_free(BIGNUM *a);
  464. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
  465. void BN_swap(BIGNUM *a, BIGNUM *b);
  466. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
  467. int BN_bn2bin(const BIGNUM *a, unsigned char *to);
  468. BIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret);
  469. int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
  470. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  471. int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  472. int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  473. int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  474. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
  475. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
  476. /** BN_set_negative sets sign of a BIGNUM
  477. * \param b pointer to the BIGNUM object
  478. * \param n 0 if the BIGNUM b should be positive and a value != 0 otherwise
  479. */
  480. void BN_set_negative(BIGNUM *b, int n);
  481. /** BN_is_negative returns 1 if the BIGNUM is negative
  482. * \param a pointer to the BIGNUM object
  483. * \return 1 if a < 0 and 0 otherwise
  484. */
  485. # define BN_is_negative(a) ((a)->neg != 0)
  486. int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
  487. BN_CTX *ctx);
  488. # define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
  489. int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
  490. int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  491. BN_CTX *ctx);
  492. int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  493. const BIGNUM *m);
  494. int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  495. BN_CTX *ctx);
  496. int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  497. const BIGNUM *m);
  498. int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  499. BN_CTX *ctx);
  500. int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  501. int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  502. int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
  503. int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
  504. BN_CTX *ctx);
  505. int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
  506. BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
  507. BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
  508. int BN_mul_word(BIGNUM *a, BN_ULONG w);
  509. int BN_add_word(BIGNUM *a, BN_ULONG w);
  510. int BN_sub_word(BIGNUM *a, BN_ULONG w);
  511. int BN_set_word(BIGNUM *a, BN_ULONG w);
  512. BN_ULONG BN_get_word(const BIGNUM *a);
  513. int BN_cmp(const BIGNUM *a, const BIGNUM *b);
  514. void BN_free(BIGNUM *a);
  515. int BN_is_bit_set(const BIGNUM *a, int n);
  516. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
  517. int BN_lshift1(BIGNUM *r, const BIGNUM *a);
  518. int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  519. int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  520. const BIGNUM *m, BN_CTX *ctx);
  521. int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  522. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  523. int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  524. const BIGNUM *m, BN_CTX *ctx,
  525. BN_MONT_CTX *in_mont);
  526. int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
  527. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  528. int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1,
  529. const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
  530. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  531. int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  532. const BIGNUM *m, BN_CTX *ctx);
  533. int BN_mask_bits(BIGNUM *a, int n);
  534. # ifndef OPENSSL_NO_FP_API
  535. int BN_print_fp(FILE *fp, const BIGNUM *a);
  536. # endif
  537. # ifdef HEADER_BIO_H
  538. int BN_print(BIO *fp, const BIGNUM *a);
  539. # else
  540. int BN_print(void *fp, const BIGNUM *a);
  541. # endif
  542. int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
  543. int BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
  544. int BN_rshift1(BIGNUM *r, const BIGNUM *a);
  545. void BN_clear(BIGNUM *a);
  546. BIGNUM *BN_dup(const BIGNUM *a);
  547. int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
  548. int BN_set_bit(BIGNUM *a, int n);
  549. int BN_clear_bit(BIGNUM *a, int n);
  550. char *BN_bn2hex(const BIGNUM *a);
  551. char *BN_bn2dec(const BIGNUM *a);
  552. int BN_hex2bn(BIGNUM **a, const char *str);
  553. int BN_dec2bn(BIGNUM **a, const char *str);
  554. int BN_asc2bn(BIGNUM **a, const char *str);
  555. int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
  556. int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); /* returns
  557. * -2 for
  558. * error */
  559. BIGNUM *BN_mod_inverse(BIGNUM *ret,
  560. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
  561. BIGNUM *BN_mod_sqrt(BIGNUM *ret,
  562. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
  563. void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
  564. /* Deprecated versions */
  565. # ifndef OPENSSL_NO_DEPRECATED
  566. BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,
  567. const BIGNUM *add, const BIGNUM *rem,
  568. void (*callback) (int, int, void *), void *cb_arg);
  569. int BN_is_prime(const BIGNUM *p, int nchecks,
  570. void (*callback) (int, int, void *),
  571. BN_CTX *ctx, void *cb_arg);
  572. int BN_is_prime_fasttest(const BIGNUM *p, int nchecks,
  573. void (*callback) (int, int, void *), BN_CTX *ctx,
  574. void *cb_arg, int do_trial_division);
  575. # endif /* !defined(OPENSSL_NO_DEPRECATED) */
  576. /* Newer versions */
  577. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
  578. const BIGNUM *rem, BN_GENCB *cb);
  579. int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
  580. int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
  581. int do_trial_division, BN_GENCB *cb);
  582. int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx);
  583. int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
  584. const BIGNUM *Xp, const BIGNUM *Xp1,
  585. const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,
  586. BN_GENCB *cb);
  587. int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1,
  588. BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e,
  589. BN_CTX *ctx, BN_GENCB *cb);
  590. BN_MONT_CTX *BN_MONT_CTX_new(void);
  591. void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
  592. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  593. BN_MONT_CTX *mont, BN_CTX *ctx);
  594. # define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
  595. (r),(a),&((mont)->RR),(mont),(ctx))
  596. int BN_from_montgomery(BIGNUM *r, const BIGNUM *a,
  597. BN_MONT_CTX *mont, BN_CTX *ctx);
  598. void BN_MONT_CTX_free(BN_MONT_CTX *mont);
  599. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx);
  600. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
  601. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
  602. const BIGNUM *mod, BN_CTX *ctx);
  603. /* BN_BLINDING flags */
  604. # define BN_BLINDING_NO_UPDATE 0x00000001
  605. # define BN_BLINDING_NO_RECREATE 0x00000002
  606. BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod);
  607. void BN_BLINDING_free(BN_BLINDING *b);
  608. int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
  609. int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
  610. int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
  611. int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
  612. int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
  613. BN_CTX *);
  614. # ifndef OPENSSL_NO_DEPRECATED
  615. unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
  616. void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
  617. # endif
  618. CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *);
  619. unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
  620. void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
  621. BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
  622. const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
  623. int (*bn_mod_exp) (BIGNUM *r,
  624. const BIGNUM *a,
  625. const BIGNUM *p,
  626. const BIGNUM *m,
  627. BN_CTX *ctx,
  628. BN_MONT_CTX *m_ctx),
  629. BN_MONT_CTX *m_ctx);
  630. # ifndef OPENSSL_NO_DEPRECATED
  631. void BN_set_params(int mul, int high, int low, int mont);
  632. int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */
  633. # endif
  634. void BN_RECP_CTX_init(BN_RECP_CTX *recp);
  635. BN_RECP_CTX *BN_RECP_CTX_new(void);
  636. void BN_RECP_CTX_free(BN_RECP_CTX *recp);
  637. int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx);
  638. int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
  639. BN_RECP_CTX *recp, BN_CTX *ctx);
  640. int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  641. const BIGNUM *m, BN_CTX *ctx);
  642. int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
  643. BN_RECP_CTX *recp, BN_CTX *ctx);
  644. # ifndef OPENSSL_NO_EC2M
  645. /*
  646. * Functions for arithmetic over binary polynomials represented by BIGNUMs.
  647. * The BIGNUM::neg property of BIGNUMs representing binary polynomials is
  648. * ignored. Note that input arguments are not const so that their bit arrays
  649. * can be expanded to the appropriate size if needed.
  650. */
  651. /*
  652. * r = a + b
  653. */
  654. int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  655. # define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
  656. /*
  657. * r=a mod p
  658. */
  659. int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p);
  660. /* r = (a * b) mod p */
  661. int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  662. const BIGNUM *p, BN_CTX *ctx);
  663. /* r = (a * a) mod p */
  664. int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  665. /* r = (1 / b) mod p */
  666. int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx);
  667. /* r = (a / b) mod p */
  668. int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  669. const BIGNUM *p, BN_CTX *ctx);
  670. /* r = (a ^ b) mod p */
  671. int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  672. const BIGNUM *p, BN_CTX *ctx);
  673. /* r = sqrt(a) mod p */
  674. int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  675. BN_CTX *ctx);
  676. /* r^2 + r = a mod p */
  677. int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  678. BN_CTX *ctx);
  679. # define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
  680. /*-
  681. * Some functions allow for representation of the irreducible polynomials
  682. * as an unsigned int[], say p. The irreducible f(t) is then of the form:
  683. * t^p[0] + t^p[1] + ... + t^p[k]
  684. * where m = p[0] > p[1] > ... > p[k] = 0.
  685. */
  686. /* r = a mod p */
  687. int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]);
  688. /* r = (a * b) mod p */
  689. int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  690. const int p[], BN_CTX *ctx);
  691. /* r = (a * a) mod p */
  692. int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
  693. BN_CTX *ctx);
  694. /* r = (1 / b) mod p */
  695. int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[],
  696. BN_CTX *ctx);
  697. /* r = (a / b) mod p */
  698. int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  699. const int p[], BN_CTX *ctx);
  700. /* r = (a ^ b) mod p */
  701. int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  702. const int p[], BN_CTX *ctx);
  703. /* r = sqrt(a) mod p */
  704. int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a,
  705. const int p[], BN_CTX *ctx);
  706. /* r^2 + r = a mod p */
  707. int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a,
  708. const int p[], BN_CTX *ctx);
  709. int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max);
  710. int BN_GF2m_arr2poly(const int p[], BIGNUM *a);
  711. # endif
  712. /*
  713. * faster mod functions for the 'NIST primes' 0 <= a < p^2
  714. */
  715. int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  716. int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  717. int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  718. int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  719. int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  720. const BIGNUM *BN_get0_nist_prime_192(void);
  721. const BIGNUM *BN_get0_nist_prime_224(void);
  722. const BIGNUM *BN_get0_nist_prime_256(void);
  723. const BIGNUM *BN_get0_nist_prime_384(void);
  724. const BIGNUM *BN_get0_nist_prime_521(void);
  725. /* library internal functions */
  726. # define bn_expand(a,bits) \
  727. ( \
  728. bits > (INT_MAX - BN_BITS2 + 1) ? \
  729. NULL \
  730. : \
  731. (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax) ? \
  732. (a) \
  733. : \
  734. bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2) \
  735. )
  736. # define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
  737. BIGNUM *bn_expand2(BIGNUM *a, int words);
  738. # ifndef OPENSSL_NO_DEPRECATED
  739. BIGNUM *bn_dup_expand(const BIGNUM *a, int words); /* unused */
  740. # endif
  741. /*-
  742. * Bignum consistency macros
  743. * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
  744. * bignum data after direct manipulations on the data. There is also an
  745. * "internal" macro, bn_check_top(), for verifying that there are no leading
  746. * zeroes. Unfortunately, some auditing is required due to the fact that
  747. * bn_fix_top() has become an overabused duct-tape because bignum data is
  748. * occasionally passed around in an inconsistent state. So the following
  749. * changes have been made to sort this out;
  750. * - bn_fix_top()s implementation has been moved to bn_correct_top()
  751. * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
  752. * bn_check_top() is as before.
  753. * - if BN_DEBUG *is* defined;
  754. * - bn_check_top() tries to pollute unused words even if the bignum 'top' is
  755. * consistent. (ed: only if BN_DEBUG_RAND is defined)
  756. * - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
  757. * The idea is to have debug builds flag up inconsistent bignums when they
  758. * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
  759. * the use of bn_fix_top() was appropriate (ie. it follows directly after code
  760. * that manipulates the bignum) it is converted to bn_correct_top(), and if it
  761. * was not appropriate, we convert it permanently to bn_check_top() and track
  762. * down the cause of the bug. Eventually, no internal code should be using the
  763. * bn_fix_top() macro. External applications and libraries should try this with
  764. * their own code too, both in terms of building against the openssl headers
  765. * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
  766. * defined. This not only improves external code, it provides more test
  767. * coverage for openssl's own code.
  768. */
  769. # ifdef BN_DEBUG
  770. /* We only need assert() when debugging */
  771. # include <assert.h>
  772. /*
  773. * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
  774. * bn_correct_top, in other words such vectors are permitted to have zeros
  775. * in most significant limbs. Such vectors are used internally to achieve
  776. * execution time invariance for critical operations with private keys.
  777. * It's BN_DEBUG-only flag, because user application is not supposed to
  778. * observe it anyway. Moreover, optimizing compiler would actually remove
  779. * all operations manipulating the bit in question in non-BN_DEBUG build.
  780. */
  781. # define BN_FLG_FIXED_TOP 0x10000
  782. # ifdef BN_DEBUG_RAND
  783. /* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */
  784. # ifndef RAND_pseudo_bytes
  785. int RAND_pseudo_bytes(unsigned char *buf, int num);
  786. # define BN_DEBUG_TRIX
  787. # endif
  788. # define bn_pollute(a) \
  789. do { \
  790. const BIGNUM *_bnum1 = (a); \
  791. if(_bnum1->top < _bnum1->dmax) { \
  792. unsigned char _tmp_char; \
  793. /* We cast away const without the compiler knowing, any \
  794. * *genuinely* constant variables that aren't mutable \
  795. * wouldn't be constructed with top!=dmax. */ \
  796. BN_ULONG *_not_const; \
  797. memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
  798. /* Debug only - safe to ignore error return */ \
  799. RAND_pseudo_bytes(&_tmp_char, 1); \
  800. memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
  801. (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
  802. } \
  803. } while(0)
  804. # ifdef BN_DEBUG_TRIX
  805. # undef RAND_pseudo_bytes
  806. # endif
  807. # else
  808. # define bn_pollute(a)
  809. # endif
  810. # define bn_check_top(a) \
  811. do { \
  812. const BIGNUM *_bnum2 = (a); \
  813. if (_bnum2 != NULL) { \
  814. int _top = _bnum2->top; \
  815. assert((_top == 0) || \
  816. (_bnum2->flags & BN_FLG_FIXED_TOP) || \
  817. (_bnum2->d[_top - 1] != 0)); \
  818. bn_pollute(_bnum2); \
  819. } \
  820. } while(0)
  821. # define bn_fix_top(a) bn_check_top(a)
  822. # define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
  823. # define bn_wcheck_size(bn, words) \
  824. do { \
  825. const BIGNUM *_bnum2 = (bn); \
  826. assert((words) <= (_bnum2)->dmax && (words) >= (_bnum2)->top); \
  827. /* avoid unused variable warning with NDEBUG */ \
  828. (void)(_bnum2); \
  829. } while(0)
  830. # else /* !BN_DEBUG */
  831. # define BN_FLG_FIXED_TOP 0
  832. # define bn_pollute(a)
  833. # define bn_check_top(a)
  834. # define bn_fix_top(a) bn_correct_top(a)
  835. # define bn_check_size(bn, bits)
  836. # define bn_wcheck_size(bn, words)
  837. # endif
  838. # define bn_correct_top(a) \
  839. { \
  840. BN_ULONG *ftl; \
  841. int tmp_top = (a)->top; \
  842. if (tmp_top > 0) \
  843. { \
  844. for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \
  845. if (*(ftl--)) break; \
  846. (a)->top = tmp_top; \
  847. } \
  848. if ((a)->top == 0) \
  849. (a)->neg = 0; \
  850. bn_pollute(a); \
  851. }
  852. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
  853. BN_ULONG w);
  854. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
  855. void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
  856. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
  857. BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  858. int num);
  859. BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  860. int num);
  861. /* Primes from RFC 2409 */
  862. BIGNUM *get_rfc2409_prime_768(BIGNUM *bn);
  863. BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn);
  864. /* Primes from RFC 3526 */
  865. BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn);
  866. BIGNUM *get_rfc3526_prime_2048(BIGNUM *bn);
  867. BIGNUM *get_rfc3526_prime_3072(BIGNUM *bn);
  868. BIGNUM *get_rfc3526_prime_4096(BIGNUM *bn);
  869. BIGNUM *get_rfc3526_prime_6144(BIGNUM *bn);
  870. BIGNUM *get_rfc3526_prime_8192(BIGNUM *bn);
  871. int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom);
  872. /* BEGIN ERROR CODES */
  873. /*
  874. * The following lines are auto generated by the script mkerr.pl. Any changes
  875. * made after this point may be overwritten when the script is next run.
  876. */
  877. void ERR_load_BN_strings(void);
  878. /* Error codes for the BN functions. */
  879. /* Function codes. */
  880. # define BN_F_BNRAND 127
  881. # define BN_F_BN_BLINDING_CONVERT_EX 100
  882. # define BN_F_BN_BLINDING_CREATE_PARAM 128
  883. # define BN_F_BN_BLINDING_INVERT_EX 101
  884. # define BN_F_BN_BLINDING_NEW 102
  885. # define BN_F_BN_BLINDING_UPDATE 103
  886. # define BN_F_BN_BN2DEC 104
  887. # define BN_F_BN_BN2HEX 105
  888. # define BN_F_BN_CTX_GET 116
  889. # define BN_F_BN_CTX_NEW 106
  890. # define BN_F_BN_CTX_START 129
  891. # define BN_F_BN_DIV 107
  892. # define BN_F_BN_DIV_NO_BRANCH 138
  893. # define BN_F_BN_DIV_RECP 130
  894. # define BN_F_BN_EXP 123
  895. # define BN_F_BN_EXPAND2 108
  896. # define BN_F_BN_EXPAND_INTERNAL 120
  897. # define BN_F_BN_GF2M_MOD 131
  898. # define BN_F_BN_GF2M_MOD_EXP 132
  899. # define BN_F_BN_GF2M_MOD_MUL 133
  900. # define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134
  901. # define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135
  902. # define BN_F_BN_GF2M_MOD_SQR 136
  903. # define BN_F_BN_GF2M_MOD_SQRT 137
  904. # define BN_F_BN_LSHIFT 145
  905. # define BN_F_BN_MOD_EXP2_MONT 118
  906. # define BN_F_BN_MOD_EXP_MONT 109
  907. # define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
  908. # define BN_F_BN_MOD_EXP_MONT_WORD 117
  909. # define BN_F_BN_MOD_EXP_RECP 125
  910. # define BN_F_BN_MOD_EXP_SIMPLE 126
  911. # define BN_F_BN_MOD_INVERSE 110
  912. # define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
  913. # define BN_F_BN_MOD_LSHIFT_QUICK 119
  914. # define BN_F_BN_MOD_MUL_RECIPROCAL 111
  915. # define BN_F_BN_MOD_SQRT 121
  916. # define BN_F_BN_MPI2BN 112
  917. # define BN_F_BN_NEW 113
  918. # define BN_F_BN_RAND 114
  919. # define BN_F_BN_RAND_RANGE 122
  920. # define BN_F_BN_RSHIFT 146
  921. # define BN_F_BN_USUB 115
  922. /* Reason codes. */
  923. # define BN_R_ARG2_LT_ARG3 100
  924. # define BN_R_BAD_RECIPROCAL 101
  925. # define BN_R_BIGNUM_TOO_LONG 114
  926. # define BN_R_BITS_TOO_SMALL 118
  927. # define BN_R_CALLED_WITH_EVEN_MODULUS 102
  928. # define BN_R_DIV_BY_ZERO 103
  929. # define BN_R_ENCODING_ERROR 104
  930. # define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
  931. # define BN_R_INPUT_NOT_REDUCED 110
  932. # define BN_R_INVALID_LENGTH 106
  933. # define BN_R_INVALID_RANGE 115
  934. # define BN_R_INVALID_SHIFT 119
  935. # define BN_R_NOT_A_SQUARE 111
  936. # define BN_R_NOT_INITIALIZED 107
  937. # define BN_R_NO_INVERSE 108
  938. # define BN_R_NO_SOLUTION 116
  939. # define BN_R_P_IS_NOT_PRIME 112
  940. # define BN_R_TOO_MANY_ITERATIONS 113
  941. # define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
  942. #ifdef __cplusplus
  943. }
  944. #endif
  945. #endif