bn.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* $OpenBSD: bn.h,v 1.77 2024/03/02 09:27:31 tb Exp $ */
  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-2006 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 <stdio.h>
  127. #include <stdlib.h>
  128. #include <openssl/opensslconf.h>
  129. #include <openssl/ossl_typ.h>
  130. #include <openssl/crypto.h>
  131. #include <openssl/bio.h>
  132. #ifdef __cplusplus
  133. extern "C" {
  134. #endif
  135. /* This next option uses the C libraries (2 word)/(1 word) function.
  136. * If it is not defined, I use my C version (which is slower).
  137. * The reason for this flag is that when the particular C compiler
  138. * library routine is used, and the library is linked with a different
  139. * compiler, the library is missing. This mostly happens when the
  140. * library is built with gcc and then linked using normal cc. This would
  141. * be a common occurrence because gcc normally produces code that is
  142. * 2 times faster than system compilers for the big number stuff.
  143. * For machines with only one compiler (or shared libraries), this should
  144. * be on. Again this in only really a problem on machines
  145. * using "long long's", are 32bit, and are not using my assembler code. */
  146. /* #define BN_DIV2W */
  147. #ifdef _LP64
  148. #undef BN_LLONG
  149. #define BN_ULONG unsigned long
  150. #define BN_LONG long
  151. #define BN_BITS 128
  152. #define BN_BYTES 8
  153. #define BN_BITS2 64
  154. #define BN_BITS4 32
  155. #define BN_MASK2 (0xffffffffffffffffL)
  156. #define BN_MASK2l (0xffffffffL)
  157. #define BN_MASK2h (0xffffffff00000000L)
  158. #define BN_MASK2h1 (0xffffffff80000000L)
  159. #define BN_TBIT (0x8000000000000000L)
  160. #define BN_DEC_CONV (10000000000000000000UL)
  161. #define BN_DEC_FMT1 "%lu"
  162. #define BN_DEC_FMT2 "%019lu"
  163. #define BN_DEC_NUM 19
  164. #define BN_HEX_FMT1 "%lX"
  165. #define BN_HEX_FMT2 "%016lX"
  166. #else
  167. #define BN_ULLONG unsigned long long
  168. #define BN_LLONG
  169. #define BN_ULONG unsigned int
  170. #define BN_LONG int
  171. #define BN_BITS 64
  172. #define BN_BYTES 4
  173. #define BN_BITS2 32
  174. #define BN_BITS4 16
  175. #define BN_MASK (0xffffffffffffffffLL)
  176. #define BN_MASK2 (0xffffffffL)
  177. #define BN_MASK2l (0xffff)
  178. #define BN_MASK2h1 (0xffff8000L)
  179. #define BN_MASK2h (0xffff0000L)
  180. #define BN_TBIT (0x80000000L)
  181. #define BN_DEC_CONV (1000000000L)
  182. #define BN_DEC_FMT1 "%u"
  183. #define BN_DEC_FMT2 "%09u"
  184. #define BN_DEC_NUM 9
  185. #define BN_HEX_FMT1 "%X"
  186. #define BN_HEX_FMT2 "%08X"
  187. #endif
  188. #define BN_FLG_MALLOCED 0x01
  189. #define BN_FLG_STATIC_DATA 0x02
  190. #define BN_FLG_CONSTTIME 0x04 /* avoid leaking exponent information through timing,
  191. * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime,
  192. * BN_div() will call BN_div_no_branch,
  193. * BN_mod_inverse() will call BN_mod_inverse_no_branch.
  194. */
  195. void BN_set_flags(BIGNUM *b, int n);
  196. int BN_get_flags(const BIGNUM *b, int n);
  197. void BN_with_flags(BIGNUM *dest, const BIGNUM *src, int flags);
  198. /* Values for |top| in BN_rand() */
  199. #define BN_RAND_TOP_ANY -1
  200. #define BN_RAND_TOP_ONE 0
  201. #define BN_RAND_TOP_TWO 1
  202. /* Values for |bottom| in BN_rand() */
  203. #define BN_RAND_BOTTOM_ANY 0
  204. #define BN_RAND_BOTTOM_ODD 1
  205. BN_GENCB *BN_GENCB_new(void);
  206. void BN_GENCB_free(BN_GENCB *cb);
  207. /* Wrapper function to make using BN_GENCB easier, */
  208. int BN_GENCB_call(BN_GENCB *cb, int a, int b);
  209. /* Populate a BN_GENCB structure with an "old"-style callback */
  210. void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *),
  211. void *cb_arg);
  212. /* Populate a BN_GENCB structure with a "new"-style callback */
  213. void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *),
  214. void *cb_arg);
  215. void *BN_GENCB_get_arg(BN_GENCB *cb);
  216. #define BN_prime_checks 0 /* default: select number of iterations
  217. based on the size of the number */
  218. /*
  219. * BN_prime_checks_for_size() returns the number of Miller-Rabin
  220. * iterations that will be done for checking that a random number
  221. * is probably prime. The error rate for accepting a composite
  222. * number as prime depends on the size of the prime |b|. The error
  223. * rates used are for calculating an RSA key with 2 primes, and so
  224. * the level is what you would expect for a key of double the size
  225. * of the prime.
  226. *
  227. * This table is generated using the algorithm of FIPS PUB 186-4
  228. * Digital Signature Standard (DSS), section F.1, page 117.
  229. * (https://dx.doi.org/10.6028/NIST.FIPS.186-4)
  230. *
  231. * The following magma script was used to generate the output:
  232. * securitybits:=125;
  233. * k:=1024;
  234. * for t:=1 to 65 do
  235. * for M:=3 to Floor(2*Sqrt(k-1)-1) do
  236. * S:=0;
  237. * // Sum over m
  238. * for m:=3 to M do
  239. * s:=0;
  240. * // Sum over j
  241. * for j:=2 to m do
  242. * s+:=(RealField(32)!2)^-(j+(k-1)/j);
  243. * end for;
  244. * S+:=2^(m-(m-1)*t)*s;
  245. * end for;
  246. * A:=2^(k-2-M*t);
  247. * B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S;
  248. * pkt:=2.00743*Log(2)*k*2^-k*(A+B);
  249. * seclevel:=Floor(-Log(2,pkt));
  250. * if seclevel ge securitybits then
  251. * printf "k: %5o, security: %o bits (t: %o, M: %o)\n",k,seclevel,t,M;
  252. * break;
  253. * end if;
  254. * end for;
  255. * if seclevel ge securitybits then break; end if;
  256. * end for;
  257. *
  258. * It can be run online at:
  259. * http://magma.maths.usyd.edu.au/calc
  260. *
  261. * And will output:
  262. * k: 1024, security: 129 bits (t: 6, M: 23)
  263. *
  264. * k is the number of bits of the prime, securitybits is the level
  265. * we want to reach.
  266. *
  267. * prime length | RSA key size | # MR tests | security level
  268. * -------------+--------------|------------+---------------
  269. * (b) >= 6394 | >= 12788 | 3 | 256 bit
  270. * (b) >= 3747 | >= 7494 | 3 | 192 bit
  271. * (b) >= 1345 | >= 2690 | 4 | 128 bit
  272. * (b) >= 1080 | >= 2160 | 5 | 128 bit
  273. * (b) >= 852 | >= 1704 | 5 | 112 bit
  274. * (b) >= 476 | >= 952 | 5 | 80 bit
  275. * (b) >= 400 | >= 800 | 6 | 80 bit
  276. * (b) >= 347 | >= 694 | 7 | 80 bit
  277. * (b) >= 308 | >= 616 | 8 | 80 bit
  278. * (b) >= 55 | >= 110 | 27 | 64 bit
  279. * (b) >= 6 | >= 12 | 34 | 64 bit
  280. */
  281. #define BN_prime_checks_for_size(b) ((b) >= 3747 ? 3 : \
  282. (b) >= 1345 ? 4 : \
  283. (b) >= 476 ? 5 : \
  284. (b) >= 400 ? 6 : \
  285. (b) >= 347 ? 7 : \
  286. (b) >= 308 ? 8 : \
  287. (b) >= 55 ? 27 : \
  288. /* b >= 6 */ 34)
  289. #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
  290. int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w);
  291. int BN_is_zero(const BIGNUM *a);
  292. int BN_is_one(const BIGNUM *a);
  293. int BN_is_word(const BIGNUM *a, const BN_ULONG w);
  294. int BN_is_odd(const BIGNUM *a);
  295. void BN_zero(BIGNUM *a);
  296. int BN_one(BIGNUM *a);
  297. const BIGNUM *BN_value_one(void);
  298. BN_CTX *BN_CTX_new(void);
  299. void BN_CTX_free(BN_CTX *c);
  300. void BN_CTX_start(BN_CTX *ctx);
  301. BIGNUM *BN_CTX_get(BN_CTX *ctx);
  302. void BN_CTX_end(BN_CTX *ctx);
  303. int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
  304. int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
  305. int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
  306. int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
  307. int BN_num_bits(const BIGNUM *a);
  308. int BN_num_bits_word(BN_ULONG);
  309. BIGNUM *BN_new(void);
  310. void BN_clear_free(BIGNUM *a);
  311. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
  312. void BN_swap(BIGNUM *a, BIGNUM *b);
  313. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
  314. int BN_bn2bin(const BIGNUM *a, unsigned char *to);
  315. int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen);
  316. BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret);
  317. int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen);
  318. BIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret);
  319. int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
  320. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  321. int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  322. int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  323. int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  324. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
  325. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
  326. void BN_set_negative(BIGNUM *b, int n);
  327. int BN_is_negative(const BIGNUM *b);
  328. #ifndef LIBRESSL_INTERNAL
  329. int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
  330. BN_CTX *ctx);
  331. #define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
  332. #endif
  333. int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
  334. int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
  335. int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
  336. int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
  337. int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
  338. int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  339. const BIGNUM *m, BN_CTX *ctx);
  340. int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  341. int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  342. int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
  343. int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx);
  344. int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
  345. BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
  346. BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
  347. int BN_mul_word(BIGNUM *a, BN_ULONG w);
  348. int BN_add_word(BIGNUM *a, BN_ULONG w);
  349. int BN_sub_word(BIGNUM *a, BN_ULONG w);
  350. int BN_set_word(BIGNUM *a, BN_ULONG w);
  351. BN_ULONG BN_get_word(const BIGNUM *a);
  352. int BN_cmp(const BIGNUM *a, const BIGNUM *b);
  353. void BN_free(BIGNUM *a);
  354. int BN_is_bit_set(const BIGNUM *a, int n);
  355. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
  356. int BN_lshift1(BIGNUM *r, const BIGNUM *a);
  357. int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
  358. #ifndef LIBRESSL_INTERNAL
  359. int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  360. const BIGNUM *m, BN_CTX *ctx);
  361. int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  362. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  363. #endif
  364. int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  365. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont);
  366. int BN_mask_bits(BIGNUM *a, int n);
  367. int BN_print_fp(FILE *fp, const BIGNUM *a);
  368. int BN_print(BIO *fp, const BIGNUM *a);
  369. int BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
  370. int BN_rshift1(BIGNUM *r, const BIGNUM *a);
  371. void BN_clear(BIGNUM *a);
  372. BIGNUM *BN_dup(const BIGNUM *a);
  373. int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
  374. int BN_set_bit(BIGNUM *a, int n);
  375. int BN_clear_bit(BIGNUM *a, int n);
  376. char * BN_bn2hex(const BIGNUM *a);
  377. char * BN_bn2dec(const BIGNUM *a);
  378. int BN_hex2bn(BIGNUM **a, const char *str);
  379. int BN_dec2bn(BIGNUM **a, const char *str);
  380. int BN_asc2bn(BIGNUM **a, const char *str);
  381. #ifndef LIBRESSL_INTERNAL
  382. int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
  383. #endif
  384. int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */
  385. #ifndef LIBRESSL_INTERNAL
  386. BIGNUM *BN_mod_inverse(BIGNUM *ret,
  387. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
  388. #endif
  389. BIGNUM *BN_mod_sqrt(BIGNUM *ret,
  390. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
  391. void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
  392. int BN_security_bits(int L, int N);
  393. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
  394. const BIGNUM *rem, BN_GENCB *cb);
  395. int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
  396. int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
  397. int do_trial_division, BN_GENCB *cb);
  398. BN_MONT_CTX *BN_MONT_CTX_new(void );
  399. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  400. BN_MONT_CTX *mont, BN_CTX *ctx);
  401. int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  402. BN_CTX *ctx);
  403. int BN_from_montgomery(BIGNUM *r, const BIGNUM *a,
  404. BN_MONT_CTX *mont, BN_CTX *ctx);
  405. void BN_MONT_CTX_free(BN_MONT_CTX *mont);
  406. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx);
  407. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
  408. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
  409. const BIGNUM *mod, BN_CTX *ctx);
  410. /* Primes from RFC 2409 */
  411. BIGNUM *BN_get_rfc2409_prime_768(BIGNUM *bn);
  412. BIGNUM *BN_get_rfc2409_prime_1024(BIGNUM *bn);
  413. /* Primes from RFC 3526 */
  414. BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *bn);
  415. BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *bn);
  416. BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *bn);
  417. BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *bn);
  418. BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *bn);
  419. BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *bn);
  420. void ERR_load_BN_strings(void);
  421. /* Error codes for the BN functions. */
  422. /* Function codes. */
  423. #define BN_F_BNRAND 127
  424. #define BN_F_BN_BLINDING_CONVERT_EX 100
  425. #define BN_F_BN_BLINDING_CREATE_PARAM 128
  426. #define BN_F_BN_BLINDING_INVERT_EX 101
  427. #define BN_F_BN_BLINDING_NEW 102
  428. #define BN_F_BN_BLINDING_UPDATE 103
  429. #define BN_F_BN_BN2DEC 104
  430. #define BN_F_BN_BN2HEX 105
  431. #define BN_F_BN_CTX_GET 116
  432. #define BN_F_BN_CTX_NEW 106
  433. #define BN_F_BN_CTX_START 129
  434. #define BN_F_BN_DIV 107
  435. #define BN_F_BN_DIV_NO_BRANCH 138
  436. #define BN_F_BN_DIV_RECP 130
  437. #define BN_F_BN_EXP 123
  438. #define BN_F_BN_EXPAND2 108
  439. #define BN_F_BN_GENERATE_PRIME_EX 140
  440. #define BN_F_BN_EXPAND_INTERNAL 120
  441. #define BN_F_BN_GF2M_MOD 131
  442. #define BN_F_BN_GF2M_MOD_EXP 132
  443. #define BN_F_BN_GF2M_MOD_MUL 133
  444. #define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134
  445. #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135
  446. #define BN_F_BN_GF2M_MOD_SQR 136
  447. #define BN_F_BN_GF2M_MOD_SQRT 137
  448. #define BN_F_BN_MOD_EXP2_MONT 118
  449. #define BN_F_BN_MOD_EXP_MONT 109
  450. #define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
  451. #define BN_F_BN_MOD_EXP_MONT_WORD 117
  452. #define BN_F_BN_MOD_EXP_RECP 125
  453. #define BN_F_BN_MOD_EXP_SIMPLE 126
  454. #define BN_F_BN_MOD_INVERSE 110
  455. #define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
  456. #define BN_F_BN_MOD_LSHIFT_QUICK 119
  457. #define BN_F_BN_MOD_MUL_RECIPROCAL 111
  458. #define BN_F_BN_MOD_SQRT 121
  459. #define BN_F_BN_MPI2BN 112
  460. #define BN_F_BN_NEW 113
  461. #define BN_F_BN_RAND 114
  462. #define BN_F_BN_RAND_RANGE 122
  463. #define BN_F_BN_USUB 115
  464. /* Reason codes. */
  465. #define BN_R_ARG2_LT_ARG3 100
  466. #define BN_R_BAD_RECIPROCAL 101
  467. #define BN_R_BIGNUM_TOO_LONG 114
  468. #define BN_R_BITS_TOO_SMALL 117
  469. #define BN_R_CALLED_WITH_EVEN_MODULUS 102
  470. #define BN_R_DIV_BY_ZERO 103
  471. #define BN_R_ENCODING_ERROR 104
  472. #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
  473. #define BN_R_INPUT_NOT_REDUCED 110
  474. #define BN_R_INVALID_ARGUMENT 118
  475. #define BN_R_INVALID_LENGTH 106
  476. #define BN_R_INVALID_RANGE 115
  477. #define BN_R_NOT_A_SQUARE 111
  478. #define BN_R_NOT_INITIALIZED 107
  479. #define BN_R_NO_INVERSE 108
  480. #define BN_R_NO_SOLUTION 116
  481. #define BN_R_P_IS_NOT_PRIME 112
  482. #define BN_R_TOO_MANY_ITERATIONS 113
  483. #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
  484. #ifdef __cplusplus
  485. }
  486. #endif
  487. #endif