129 # include <openssl/e_os2.h>
130 # ifndef OPENSSL_NO_FP_API
133 # include <openssl/ossl_typ.h>
134 # include <openssl/crypto.h>
152 # ifndef OPENSSL_SMALL_FOOTPRINT
153 # define BN_MUL_COMBA
154 # define BN_SQR_COMBA
155 # define BN_RECURSION
170 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
171 defined(OPENSSL_SYS_WIN32) || defined(linux)
181 # ifdef SIXTY_FOUR_BIT_LONG
182 # define BN_ULLONG unsigned long long
183 # define BN_ULONG unsigned long
184 # define BN_LONG long
189 # define BN_MASK (0xffffffffffffffffffffffffffffffffLL)
190 # define BN_MASK2 (0xffffffffffffffffL)
191 # define BN_MASK2l (0xffffffffL)
192 # define BN_MASK2h (0xffffffff00000000L)
193 # define BN_MASK2h1 (0xffffffff80000000L)
194 # define BN_TBIT (0x8000000000000000L)
195 # define BN_DEC_CONV (10000000000000000000UL)
196 # define BN_DEC_FMT1 "%lu"
197 # define BN_DEC_FMT2 "%019lu"
198 # define BN_DEC_NUM 19
199 # define BN_HEX_FMT1 "%lX"
200 # define BN_HEX_FMT2 "%016lX"
209 # ifdef SIXTY_FOUR_BIT
212 # define BN_ULONG unsigned long long
213 # define BN_LONG long long
218 # define BN_MASK2 (0xffffffffffffffffLL)
219 # define BN_MASK2l (0xffffffffL)
220 # define BN_MASK2h (0xffffffff00000000LL)
221 # define BN_MASK2h1 (0xffffffff80000000LL)
222 # define BN_TBIT (0x8000000000000000LL)
223 # define BN_DEC_CONV (10000000000000000000ULL)
224 # define BN_DEC_FMT1 "%llu"
225 # define BN_DEC_FMT2 "%019llu"
226 # define BN_DEC_NUM 19
227 # define BN_HEX_FMT1 "%llX"
228 # define BN_HEX_FMT2 "%016llX"
231 # ifdef THIRTY_TWO_BIT
233 # if defined(_WIN32) && !defined(__GNUC__)
234 # define BN_ULLONG unsigned __int64
235 # define BN_MASK (0xffffffffffffffffI64)
237 # define BN_ULLONG unsigned long long
238 # define BN_MASK (0xffffffffffffffffLL)
241 # define BN_ULONG unsigned int
247 # define BN_MASK2 (0xffffffffL)
248 # define BN_MASK2l (0xffff)
249 # define BN_MASK2h1 (0xffff8000L)
250 # define BN_MASK2h (0xffff0000L)
251 # define BN_TBIT (0x80000000L)
252 # define BN_DEC_CONV (1000000000L)
253 # define BN_DEC_FMT1 "%u"
254 # define BN_DEC_FMT2 "%09u"
255 # define BN_DEC_NUM 9
256 # define BN_HEX_FMT1 "%X"
257 # define BN_HEX_FMT2 "%08X"
260 # define BN_DEFAULT_BITS 1280
262 # define BN_FLG_MALLOCED 0x01
263 # define BN_FLG_STATIC_DATA 0x02
271 # define BN_FLG_CONSTTIME 0x04
273 # ifdef OPENSSL_NO_DEPRECATED
275 # define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME
282 # ifndef OPENSSL_NO_DEPRECATED
283 # define BN_FLG_FREE 0x8000
286 # define BN_set_flags(b,n) ((b)->flags|=(n))
287 # define BN_get_flags(b,n) ((b)->flags&(n))
293 # define BN_with_flags(dest,b,n) ((dest)->d=(b)->d, \
294 (dest)->top=(b)->top, \
295 (dest)->dmax=(b)->dmax, \
296 (dest)->neg=(b)->neg, \
297 (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
298 | ((b)->flags & ~BN_FLG_MALLOCED) \
299 | BN_FLG_STATIC_DATA \
306 typedef struct bignum_ctx BN_CTX;
307 typedef struct bn_blinding_st BN_BLINDING;
354 void (*cb_1) (int, int,
void *);
360 int BN_GENCB_call(
BN_GENCB *cb,
int a,
int b);
362 # define BN_GENCB_set_old(gencb, callback, cb_arg) { \
363 BN_GENCB *tmp_gencb = (gencb); \
364 tmp_gencb->ver = 1; \
365 tmp_gencb->arg = (cb_arg); \
366 tmp_gencb->cb.cb_1 = (callback); }
368 # define BN_GENCB_set(gencb, callback, cb_arg) { \
369 BN_GENCB *tmp_gencb = (gencb); \
370 tmp_gencb->ver = 2; \
371 tmp_gencb->arg = (cb_arg); \
372 tmp_gencb->cb.cb_2 = (callback); }
374 # define BN_prime_checks 0
440 # define BN_prime_checks_for_size(b) ((b) >= 3747 ? 3 : \
449 # define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
452 # define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
453 (((w) == 0) && ((a)->top == 0)))
454 # define BN_is_zero(a) ((a)->top == 0)
455 # define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg)
456 # define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
457 # define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
459 # define BN_one(a) (BN_set_word((a),1))
460 # define BN_zero_ex(a) \
462 BIGNUM *_tmp_bn = (a); \
466 # ifdef OPENSSL_NO_DEPRECATED
467 # define BN_zero(a) BN_zero_ex(a)
469 # define BN_zero(a) (BN_set_word((a),0))
472 const BIGNUM *BN_value_one(
void);
473 char *BN_options(
void);
474 BN_CTX *BN_CTX_new(
void);
475 # ifndef OPENSSL_NO_DEPRECATED
476 void BN_CTX_init(BN_CTX *c);
478 void BN_CTX_free(BN_CTX *c);
479 void BN_CTX_start(BN_CTX *ctx);
480 BIGNUM *BN_CTX_get(BN_CTX *ctx);
481 void BN_CTX_end(BN_CTX *ctx);
482 int BN_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
483 int BN_pseudo_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
485 int BN_pseudo_rand_range(
BIGNUM *rnd,
const BIGNUM *range);
486 int BN_num_bits(
const BIGNUM *a);
487 int BN_num_bits_word(BN_ULONG);
490 void BN_clear_free(
BIGNUM *a);
493 BIGNUM *BN_bin2bn(
const unsigned char *s,
int len,
BIGNUM *ret);
494 int BN_bn2bin(
const BIGNUM *a,
unsigned char *to);
495 BIGNUM *BN_mpi2bn(
const unsigned char *s,
int len,
BIGNUM *ret);
496 int BN_bn2mpi(
const BIGNUM *a,
unsigned char *to);
507 void BN_set_negative(
BIGNUM *b,
int n);
512 # define BN_is_negative(a) ((a)->neg != 0)
516 # define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
535 BN_ULONG BN_mod_word(
const BIGNUM *a, BN_ULONG w);
536 BN_ULONG BN_div_word(
BIGNUM *a, BN_ULONG w);
537 int BN_mul_word(
BIGNUM *a, BN_ULONG w);
538 int BN_add_word(
BIGNUM *a, BN_ULONG w);
539 int BN_sub_word(
BIGNUM *a, BN_ULONG w);
540 int BN_set_word(
BIGNUM *a, BN_ULONG w);
541 BN_ULONG BN_get_word(
const BIGNUM *a);
545 int BN_is_bit_set(
const BIGNUM *a,
int n);
551 const BIGNUM *m, BN_CTX *ctx);
555 const BIGNUM *m, BN_CTX *ctx,
557 int BN_mod_exp_mont_word(
BIGNUM *r, BN_ULONG a,
const BIGNUM *p,
563 const BIGNUM *m, BN_CTX *ctx);
565 int BN_mask_bits(
BIGNUM *a,
int n);
566 # ifndef OPENSSL_NO_FP_API
567 int BN_print_fp(FILE *fp,
const BIGNUM *a);
572 int BN_print(
void *fp,
const BIGNUM *a);
574 int BN_reciprocal(
BIGNUM *r,
const BIGNUM *m,
int len, BN_CTX *ctx);
580 int BN_set_bit(
BIGNUM *a,
int n);
581 int BN_clear_bit(
BIGNUM *a,
int n);
582 char *BN_bn2hex(
const BIGNUM *a);
583 char *BN_bn2dec(
const BIGNUM *a);
584 int BN_hex2bn(
BIGNUM **a,
const char *str);
585 int BN_dec2bn(
BIGNUM **a,
const char *str);
586 int BN_asc2bn(
BIGNUM **a,
const char *str);
588 int BN_kronecker(
const BIGNUM *a,
const BIGNUM *b, BN_CTX *ctx);
596 void BN_consttime_swap(BN_ULONG swap,
BIGNUM *a,
BIGNUM *b,
int nwords);
599 # ifndef OPENSSL_NO_DEPRECATED
600 BIGNUM *BN_generate_prime(
BIGNUM *ret,
int bits,
int safe,
602 void (*callback) (
int,
int,
void *),
void *cb_arg);
603 int BN_is_prime(
const BIGNUM *p,
int nchecks,
604 void (*callback) (
int,
int,
void *),
605 BN_CTX *ctx,
void *cb_arg);
606 int BN_is_prime_fasttest(
const BIGNUM *p,
int nchecks,
607 void (*callback) (
int,
int,
void *), BN_CTX *ctx,
608 void *cb_arg,
int do_trial_division);
612 int BN_generate_prime_ex(
BIGNUM *ret,
int bits,
int safe,
const BIGNUM *add,
614 int BN_is_prime_ex(
const BIGNUM *p,
int nchecks, BN_CTX *ctx,
BN_GENCB *cb);
615 int BN_is_prime_fasttest_ex(
const BIGNUM *p,
int nchecks, BN_CTX *ctx,
616 int do_trial_division,
BN_GENCB *cb);
618 int BN_X931_generate_Xpq(
BIGNUM *Xp,
BIGNUM *Xq,
int nbits, BN_CTX *ctx);
632 # define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
633 (r),(a),&((mont)->RR),(mont),(ctx))
640 const BIGNUM *mod, BN_CTX *ctx);
643 # define BN_BLINDING_NO_UPDATE 0x00000001
644 # define BN_BLINDING_NO_RECREATE 0x00000002
647 void BN_BLINDING_free(BN_BLINDING *b);
648 int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
649 int BN_BLINDING_convert(
BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
650 int BN_BLINDING_invert(
BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
651 int BN_BLINDING_convert_ex(
BIGNUM *n,
BIGNUM *r, BN_BLINDING *b, BN_CTX *);
652 int BN_BLINDING_invert_ex(
BIGNUM *n,
const BIGNUM *r, BN_BLINDING *b,
654 # ifndef OPENSSL_NO_DEPRECATED
655 unsigned long BN_BLINDING_get_thread_id(
const BN_BLINDING *);
656 void BN_BLINDING_set_thread_id(BN_BLINDING *,
unsigned long);
659 unsigned long BN_BLINDING_get_flags(
const BN_BLINDING *);
660 void BN_BLINDING_set_flags(BN_BLINDING *,
unsigned long);
661 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
663 int (*bn_mod_exp) (
BIGNUM *r,
671 # ifndef OPENSSL_NO_DEPRECATED
672 void BN_set_params(
int mul,
int high,
int low,
int mont);
673 int BN_get_params(
int which);
683 const BIGNUM *m, BN_CTX *ctx);
687 # ifndef OPENSSL_NO_EC2M
700 # define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
707 const BIGNUM *p, BN_CTX *ctx);
714 const BIGNUM *p, BN_CTX *ctx);
717 const BIGNUM *p, BN_CTX *ctx);
724 # define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
732 int BN_GF2m_mod_arr(
BIGNUM *r,
const BIGNUM *a,
const int p[]);
735 const int p[], BN_CTX *ctx);
737 int BN_GF2m_mod_sqr_arr(
BIGNUM *r,
const BIGNUM *a,
const int p[],
740 int BN_GF2m_mod_inv_arr(
BIGNUM *r,
const BIGNUM *b,
const int p[],
744 const int p[], BN_CTX *ctx);
747 const int p[], BN_CTX *ctx);
750 const int p[], BN_CTX *ctx);
752 int BN_GF2m_mod_solve_quad_arr(
BIGNUM *r,
const BIGNUM *a,
753 const int p[], BN_CTX *ctx);
754 int BN_GF2m_poly2arr(
const BIGNUM *a,
int p[],
int max);
755 int BN_GF2m_arr2poly(
const int p[],
BIGNUM *a);
768 const BIGNUM *BN_get0_nist_prime_192(
void);
769 const BIGNUM *BN_get0_nist_prime_224(
void);
770 const BIGNUM *BN_get0_nist_prime_256(
void);
771 const BIGNUM *BN_get0_nist_prime_384(
void);
772 const BIGNUM *BN_get0_nist_prime_521(
void);
776 # define bn_expand(a,bits) \
778 bits > (INT_MAX - BN_BITS2 + 1) ? \
781 (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax) ? \
784 bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2) \
787 # define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
789 # ifndef OPENSSL_NO_DEPRECATED
836 # define BN_FLG_FIXED_TOP 0x10000
837 # ifdef BN_DEBUG_RAND
839 # ifndef RAND_pseudo_bytes
840 int RAND_pseudo_bytes(
unsigned char *buf,
int num);
841 # define BN_DEBUG_TRIX
843 # define bn_pollute(a) \
845 const BIGNUM *_bnum1 = (a); \
846 if(_bnum1->top < _bnum1->dmax) { \
847 unsigned char _tmp_char; \
851 BN_ULONG *_not_const; \
852 memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
854 RAND_pseudo_bytes(&_tmp_char, 1); \
855 memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
856 (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
859 # ifdef BN_DEBUG_TRIX
860 # undef RAND_pseudo_bytes
863 # define bn_pollute(a)
865 # define bn_check_top(a) \
867 const BIGNUM *_bnum2 = (a); \
868 if (_bnum2 != NULL) { \
869 int _top = _bnum2->top; \
870 assert((_top == 0) || \
871 (_bnum2->flags & BN_FLG_FIXED_TOP) || \
872 (_bnum2->d[_top - 1] != 0)); \
873 bn_pollute(_bnum2); \
877 # define bn_fix_top(a) bn_check_top(a)
879 # define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
880 # define bn_wcheck_size(bn, words) \
882 const BIGNUM *_bnum2 = (bn); \
883 assert((words) <= (_bnum2)->dmax && (words) >= (_bnum2)->top); \
890 # define BN_FLG_FIXED_TOP 0
891 # define bn_pollute(a)
892 # define bn_check_top(a)
893 # define bn_fix_top(a) bn_correct_top(a)
894 # define bn_check_size(bn, bits)
895 # define bn_wcheck_size(bn, words)
899 # define bn_correct_top(a) \
902 int tmp_top = (a)->top; \
905 for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \
906 if (*(ftl--)) break; \
907 (a)->top = tmp_top; \
914 BN_ULONG bn_mul_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num,
916 BN_ULONG bn_mul_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
917 void bn_sqr_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num);
918 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
919 BN_ULONG bn_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
921 BN_ULONG bn_sub_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
936 int BN_bntest_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
943 void ERR_load_BN_strings(
void);
948 # define BN_F_BNRAND 127
949 # define BN_F_BN_BLINDING_CONVERT_EX 100
950 # define BN_F_BN_BLINDING_CREATE_PARAM 128
951 # define BN_F_BN_BLINDING_INVERT_EX 101
952 # define BN_F_BN_BLINDING_NEW 102
953 # define BN_F_BN_BLINDING_UPDATE 103
954 # define BN_F_BN_BN2DEC 104
955 # define BN_F_BN_BN2HEX 105
956 # define BN_F_BN_CTX_GET 116
957 # define BN_F_BN_CTX_NEW 106
958 # define BN_F_BN_CTX_START 129
959 # define BN_F_BN_DIV 107
960 # define BN_F_BN_DIV_NO_BRANCH 138
961 # define BN_F_BN_DIV_RECP 130
962 # define BN_F_BN_EXP 123
963 # define BN_F_BN_EXPAND2 108
964 # define BN_F_BN_EXPAND_INTERNAL 120
965 # define BN_F_BN_GF2M_MOD 131
966 # define BN_F_BN_GF2M_MOD_EXP 132
967 # define BN_F_BN_GF2M_MOD_MUL 133
968 # define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134
969 # define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135
970 # define BN_F_BN_GF2M_MOD_SQR 136
971 # define BN_F_BN_GF2M_MOD_SQRT 137
972 # define BN_F_BN_LSHIFT 145
973 # define BN_F_BN_MOD_EXP2_MONT 118
974 # define BN_F_BN_MOD_EXP_MONT 109
975 # define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
976 # define BN_F_BN_MOD_EXP_MONT_WORD 117
977 # define BN_F_BN_MOD_EXP_RECP 125
978 # define BN_F_BN_MOD_EXP_SIMPLE 126
979 # define BN_F_BN_MOD_INVERSE 110
980 # define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
981 # define BN_F_BN_MOD_LSHIFT_QUICK 119
982 # define BN_F_BN_MOD_MUL_RECIPROCAL 111
983 # define BN_F_BN_MOD_SQRT 121
984 # define BN_F_BN_MPI2BN 112
985 # define BN_F_BN_NEW 113
986 # define BN_F_BN_RAND 114
987 # define BN_F_BN_RAND_RANGE 122
988 # define BN_F_BN_RSHIFT 146
989 # define BN_F_BN_USUB 115
992 # define BN_R_ARG2_LT_ARG3 100
993 # define BN_R_BAD_RECIPROCAL 101
994 # define BN_R_BIGNUM_TOO_LONG 114
995 # define BN_R_BITS_TOO_SMALL 118
996 # define BN_R_CALLED_WITH_EVEN_MODULUS 102
997 # define BN_R_DIV_BY_ZERO 103
998 # define BN_R_ENCODING_ERROR 104
999 # define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
1000 # define BN_R_INPUT_NOT_REDUCED 110
1001 # define BN_R_INVALID_LENGTH 106
1002 # define BN_R_INVALID_RANGE 115
1003 # define BN_R_INVALID_SHIFT 119
1004 # define BN_R_NOT_A_SQUARE 111
1005 # define BN_R_NOT_INITIALIZED 107
1006 # define BN_R_NO_INVERSE 108
1007 # define BN_R_NO_SOLUTION 116
1008 # define BN_R_P_IS_NOT_PRIME 112
1009 # define BN_R_TOO_MANY_ITERATIONS 113
1010 # define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109