128 #include <openssl/e_os2.h>
129 #ifndef OPENSSL_NO_FP_API
132 #include <openssl/ossl_typ.h>
133 #include <openssl/crypto.h>
151 #ifndef OPENSSL_SMALL_FOOTPRINT
168 #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
169 defined(OPENSSL_SYS_WIN32) || defined(linux)
178 #ifdef SIXTY_FOUR_BIT_LONG
179 #define BN_ULLONG unsigned long long
180 #define BN_ULONG unsigned long
186 #define BN_MASK (0xffffffffffffffffffffffffffffffffLL)
187 #define BN_MASK2 (0xffffffffffffffffL)
188 #define BN_MASK2l (0xffffffffL)
189 #define BN_MASK2h (0xffffffff00000000L)
190 #define BN_MASK2h1 (0xffffffff80000000L)
191 #define BN_TBIT (0x8000000000000000L)
192 #define BN_DEC_CONV (10000000000000000000UL)
193 #define BN_DEC_FMT1 "%lu"
194 #define BN_DEC_FMT2 "%019lu"
195 #define BN_DEC_NUM 19
196 #define BN_HEX_FMT1 "%lX"
197 #define BN_HEX_FMT2 "%016lX"
205 #ifdef SIXTY_FOUR_BIT
208 #define BN_ULONG unsigned long long
209 #define BN_LONG long long
214 #define BN_MASK2 (0xffffffffffffffffLL)
215 #define BN_MASK2l (0xffffffffL)
216 #define BN_MASK2h (0xffffffff00000000LL)
217 #define BN_MASK2h1 (0xffffffff80000000LL)
218 #define BN_TBIT (0x8000000000000000LL)
219 #define BN_DEC_CONV (10000000000000000000ULL)
220 #define BN_DEC_FMT1 "%llu"
221 #define BN_DEC_FMT2 "%019llu"
222 #define BN_DEC_NUM 19
223 #define BN_HEX_FMT1 "%llX"
224 #define BN_HEX_FMT2 "%016llX"
227 #ifdef THIRTY_TWO_BIT
229 # if defined(_WIN32) && !defined(__GNUC__)
230 # define BN_ULLONG unsigned __int64
231 # define BN_MASK (0xffffffffffffffffI64)
233 # define BN_ULLONG unsigned long long
234 # define BN_MASK (0xffffffffffffffffLL)
237 #define BN_ULONG unsigned int
243 #define BN_MASK2 (0xffffffffL)
244 #define BN_MASK2l (0xffff)
245 #define BN_MASK2h1 (0xffff8000L)
246 #define BN_MASK2h (0xffff0000L)
247 #define BN_TBIT (0x80000000L)
248 #define BN_DEC_CONV (1000000000L)
249 #define BN_DEC_FMT1 "%u"
250 #define BN_DEC_FMT2 "%09u"
252 #define BN_HEX_FMT1 "%X"
253 #define BN_HEX_FMT2 "%08X"
264 #if defined(OPENSSL_SYS_VMS)
265 # if __INITIAL_POINTER_SIZE == 64
266 # define PTR_SIZE_INT long long
268 # define PTR_SIZE_INT int
271 # define PTR_SIZE_INT size_t
274 #define BN_DEFAULT_BITS 1280
276 #define BN_FLG_MALLOCED 0x01
277 #define BN_FLG_STATIC_DATA 0x02
278 #define BN_FLG_CONSTTIME 0x04
284 #ifndef OPENSSL_NO_DEPRECATED
285 #define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME
290 #ifndef OPENSSL_NO_DEPRECATED
291 #define BN_FLG_FREE 0x8000
293 #define BN_set_flags(b,n) ((b)->flags|=(n))
294 #define BN_get_flags(b,n) ((b)->flags&(n))
298 #define BN_with_flags(dest,b,n) ((dest)->d=(b)->d, \
299 (dest)->top=(b)->top, \
300 (dest)->dmax=(b)->dmax, \
301 (dest)->neg=(b)->neg, \
302 (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
303 | ((b)->flags & ~BN_FLG_MALLOCED) \
304 | BN_FLG_STATIC_DATA \
311 typedef struct bignum_ctx BN_CTX;
312 typedef struct bn_blinding_st BN_BLINDING;
361 void (*cb_1)(int, int,
void *);
367 int BN_GENCB_call(
BN_GENCB *cb,
int a,
int b);
369 #define BN_GENCB_set_old(gencb, callback, cb_arg) { \
370 BN_GENCB *tmp_gencb = (gencb); \
371 tmp_gencb->ver = 1; \
372 tmp_gencb->arg = (cb_arg); \
373 tmp_gencb->cb.cb_1 = (callback); }
375 #define BN_GENCB_set(gencb, callback, cb_arg) { \
376 BN_GENCB *tmp_gencb = (gencb); \
377 tmp_gencb->ver = 2; \
378 tmp_gencb->arg = (cb_arg); \
379 tmp_gencb->cb.cb_2 = (callback); }
381 #define BN_prime_checks 0
389 #define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \
402 #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
405 #define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
406 (((w) == 0) && ((a)->top == 0)))
407 #define BN_is_zero(a) ((a)->top == 0)
408 #define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg)
409 #define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
410 #define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
412 #define BN_one(a) (BN_set_word((a),1))
413 #define BN_zero_ex(a) \
415 BIGNUM *_tmp_bn = (a); \
419 #ifdef OPENSSL_NO_DEPRECATED
420 #define BN_zero(a) BN_zero_ex(a)
422 #define BN_zero(a) (BN_set_word((a),0))
425 const BIGNUM *BN_value_one(
void);
426 char * BN_options(
void);
427 BN_CTX *BN_CTX_new(
void);
428 #ifndef OPENSSL_NO_DEPRECATED
429 void BN_CTX_init(BN_CTX *c);
431 void BN_CTX_free(BN_CTX *c);
432 void BN_CTX_start(BN_CTX *ctx);
433 BIGNUM *BN_CTX_get(BN_CTX *ctx);
434 void BN_CTX_end(BN_CTX *ctx);
435 int BN_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
436 int BN_pseudo_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
438 int BN_pseudo_rand_range(
BIGNUM *rnd,
const BIGNUM *range);
439 int BN_num_bits(
const BIGNUM *a);
440 int BN_num_bits_word(BN_ULONG);
443 void BN_clear_free(
BIGNUM *a);
446 BIGNUM *BN_bin2bn(
const unsigned char *s,
int len,
BIGNUM *ret);
447 int BN_bn2bin(
const BIGNUM *a,
unsigned char *to);
448 BIGNUM *BN_mpi2bn(
const unsigned char *s,
int len,
BIGNUM *ret);
449 int BN_bn2mpi(
const BIGNUM *a,
unsigned char *to);
460 void BN_set_negative(
BIGNUM *b,
int n);
465 #define BN_is_negative(a) ((a)->neg != 0)
469 #define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
476 const BIGNUM *m, BN_CTX *ctx);
483 BN_ULONG BN_mod_word(
const BIGNUM *a, BN_ULONG w);
484 BN_ULONG BN_div_word(
BIGNUM *a, BN_ULONG w);
485 int BN_mul_word(
BIGNUM *a, BN_ULONG w);
486 int BN_add_word(
BIGNUM *a, BN_ULONG w);
487 int BN_sub_word(
BIGNUM *a, BN_ULONG w);
488 int BN_set_word(
BIGNUM *a, BN_ULONG w);
489 BN_ULONG BN_get_word(
const BIGNUM *a);
493 int BN_is_bit_set(
const BIGNUM *a,
int n);
499 const BIGNUM *m,BN_CTX *ctx);
504 int BN_mod_exp_mont_word(
BIGNUM *r, BN_ULONG a,
const BIGNUM *p,
510 const BIGNUM *m,BN_CTX *ctx);
512 int BN_mask_bits(
BIGNUM *a,
int n);
513 #ifndef OPENSSL_NO_FP_API
514 int BN_print_fp(FILE *fp,
const BIGNUM *a);
519 int BN_print(
void *fp,
const BIGNUM *a);
521 int BN_reciprocal(
BIGNUM *r,
const BIGNUM *m,
int len, BN_CTX *ctx);
527 int BN_set_bit(
BIGNUM *a,
int n);
528 int BN_clear_bit(
BIGNUM *a,
int n);
529 char * BN_bn2hex(
const BIGNUM *a);
530 char * BN_bn2dec(
const BIGNUM *a);
531 int BN_hex2bn(
BIGNUM **a,
const char *str);
532 int BN_dec2bn(
BIGNUM **a,
const char *str);
533 int BN_asc2bn(
BIGNUM **a,
const char *str);
535 int BN_kronecker(
const BIGNUM *a,
const BIGNUM *b,BN_CTX *ctx);
541 void BN_consttime_swap(BN_ULONG swap,
BIGNUM *a,
BIGNUM *b,
int nwords);
544 #ifndef OPENSSL_NO_DEPRECATED
545 BIGNUM *BN_generate_prime(
BIGNUM *ret,
int bits,
int safe,
547 void (*callback)(
int,
int,
void *),
void *cb_arg);
548 int BN_is_prime(
const BIGNUM *p,
int nchecks,
549 void (*callback)(
int,
int,
void *),
550 BN_CTX *ctx,
void *cb_arg);
551 int BN_is_prime_fasttest(
const BIGNUM *p,
int nchecks,
552 void (*callback)(
int,
int,
void *),BN_CTX *ctx,
void *cb_arg,
553 int do_trial_division);
557 int BN_generate_prime_ex(
BIGNUM *ret,
int bits,
int safe,
const BIGNUM *add,
559 int BN_is_prime_ex(
const BIGNUM *p,
int nchecks, BN_CTX *ctx,
BN_GENCB *cb);
560 int BN_is_prime_fasttest_ex(
const BIGNUM *p,
int nchecks, BN_CTX *ctx,
561 int do_trial_division,
BN_GENCB *cb);
563 int BN_X931_generate_Xpq(
BIGNUM *Xp,
BIGNUM *Xq,
int nbits, BN_CTX *ctx);
571 const BIGNUM *e, BN_CTX *ctx,
578 #define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
579 (r),(a),&((mont)->RR),(mont),(ctx))
586 const BIGNUM *mod, BN_CTX *ctx);
589 #define BN_BLINDING_NO_UPDATE 0x00000001
590 #define BN_BLINDING_NO_RECREATE 0x00000002
593 void BN_BLINDING_free(BN_BLINDING *b);
594 int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx);
595 int BN_BLINDING_convert(
BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
596 int BN_BLINDING_invert(
BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
597 int BN_BLINDING_convert_ex(
BIGNUM *n,
BIGNUM *r, BN_BLINDING *b, BN_CTX *);
598 int BN_BLINDING_invert_ex(
BIGNUM *n,
const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
599 #ifndef OPENSSL_NO_DEPRECATED
600 unsigned long BN_BLINDING_get_thread_id(
const BN_BLINDING *);
601 void BN_BLINDING_set_thread_id(BN_BLINDING *,
unsigned long);
604 unsigned long BN_BLINDING_get_flags(
const BN_BLINDING *);
605 void BN_BLINDING_set_flags(BN_BLINDING *,
unsigned long);
606 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
612 #ifndef OPENSSL_NO_DEPRECATED
613 void BN_set_params(
int mul,
int high,
int low,
int mont);
614 int BN_get_params(
int which);
624 const BIGNUM *m, BN_CTX *ctx);
628 #ifndef OPENSSL_NO_EC2M
640 #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
643 const BIGNUM *p, BN_CTX *ctx);
649 const BIGNUM *p, BN_CTX *ctx);
651 const BIGNUM *p, BN_CTX *ctx);
656 #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
662 int BN_GF2m_mod_arr(
BIGNUM *r,
const BIGNUM *a,
const int p[]);
665 const int p[], BN_CTX *ctx);
666 int BN_GF2m_mod_sqr_arr(
BIGNUM *r,
const BIGNUM *a,
const int p[],
668 int BN_GF2m_mod_inv_arr(
BIGNUM *r,
const BIGNUM *b,
const int p[],
671 const int p[], BN_CTX *ctx);
673 const int p[], BN_CTX *ctx);
675 const int p[], BN_CTX *ctx);
676 int BN_GF2m_mod_solve_quad_arr(
BIGNUM *r,
const BIGNUM *a,
677 const int p[], BN_CTX *ctx);
678 int BN_GF2m_poly2arr(
const BIGNUM *a,
int p[],
int max);
679 int BN_GF2m_arr2poly(
const int p[],
BIGNUM *a);
691 const BIGNUM *BN_get0_nist_prime_192(
void);
692 const BIGNUM *BN_get0_nist_prime_224(
void);
693 const BIGNUM *BN_get0_nist_prime_256(
void);
694 const BIGNUM *BN_get0_nist_prime_384(
void);
695 const BIGNUM *BN_get0_nist_prime_521(
void);
699 #define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
700 (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2))
701 #define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
703 #ifndef OPENSSL_NO_DEPRECATED
742 #ifndef RAND_pseudo_bytes
743 int RAND_pseudo_bytes(
unsigned char *buf,
int num);
744 #define BN_DEBUG_TRIX
746 #define bn_pollute(a) \
748 const BIGNUM *_bnum1 = (a); \
749 if(_bnum1->top < _bnum1->dmax) { \
750 unsigned char _tmp_char; \
754 BN_ULONG *_not_const; \
755 memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
756 RAND_pseudo_bytes(&_tmp_char, 1); \
757 memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
758 (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
762 #undef RAND_pseudo_bytes
765 #define bn_pollute(a)
767 #define bn_check_top(a) \
769 const BIGNUM *_bnum2 = (a); \
770 if (_bnum2 != NULL) { \
771 assert((_bnum2->top == 0) || \
772 (_bnum2->d[_bnum2->top - 1] != 0)); \
773 bn_pollute(_bnum2); \
777 #define bn_fix_top(a) bn_check_top(a)
779 #define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
780 #define bn_wcheck_size(bn, words) \
782 const BIGNUM *_bnum2 = (bn); \
783 assert(words <= (_bnum2)->dmax && words >= (_bnum2)->top); \
788 #define bn_pollute(a)
789 #define bn_check_top(a)
790 #define bn_fix_top(a) bn_correct_top(a)
791 #define bn_check_size(bn, bits)
792 #define bn_wcheck_size(bn, words)
796 #define bn_correct_top(a) \
799 int tmp_top = (a)->top; \
802 for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \
803 if (*(ftl--)) break; \
804 (a)->top = tmp_top; \
809 BN_ULONG bn_mul_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
810 BN_ULONG bn_mul_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
811 void bn_sqr_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num);
812 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
813 BN_ULONG bn_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
int num);
814 BN_ULONG bn_sub_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
int num);
828 int BN_bntest_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
834 void ERR_load_BN_strings(
void);
839 #define BN_F_BNRAND 127
840 #define BN_F_BN_BLINDING_CONVERT_EX 100
841 #define BN_F_BN_BLINDING_CREATE_PARAM 128
842 #define BN_F_BN_BLINDING_INVERT_EX 101
843 #define BN_F_BN_BLINDING_NEW 102
844 #define BN_F_BN_BLINDING_UPDATE 103
845 #define BN_F_BN_BN2DEC 104
846 #define BN_F_BN_BN2HEX 105
847 #define BN_F_BN_CTX_GET 116
848 #define BN_F_BN_CTX_NEW 106
849 #define BN_F_BN_CTX_START 129
850 #define BN_F_BN_DIV 107
851 #define BN_F_BN_DIV_NO_BRANCH 138
852 #define BN_F_BN_DIV_RECP 130
853 #define BN_F_BN_EXP 123
854 #define BN_F_BN_EXPAND2 108
855 #define BN_F_BN_EXPAND_INTERNAL 120
856 #define BN_F_BN_GF2M_MOD 131
857 #define BN_F_BN_GF2M_MOD_EXP 132
858 #define BN_F_BN_GF2M_MOD_MUL 133
859 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134
860 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135
861 #define BN_F_BN_GF2M_MOD_SQR 136
862 #define BN_F_BN_GF2M_MOD_SQRT 137
863 #define BN_F_BN_MOD_EXP2_MONT 118
864 #define BN_F_BN_MOD_EXP_MONT 109
865 #define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
866 #define BN_F_BN_MOD_EXP_MONT_WORD 117
867 #define BN_F_BN_MOD_EXP_RECP 125
868 #define BN_F_BN_MOD_EXP_SIMPLE 126
869 #define BN_F_BN_MOD_INVERSE 110
870 #define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
871 #define BN_F_BN_MOD_LSHIFT_QUICK 119
872 #define BN_F_BN_MOD_MUL_RECIPROCAL 111
873 #define BN_F_BN_MOD_SQRT 121
874 #define BN_F_BN_MPI2BN 112
875 #define BN_F_BN_NEW 113
876 #define BN_F_BN_RAND 114
877 #define BN_F_BN_RAND_RANGE 122
878 #define BN_F_BN_USUB 115
881 #define BN_R_ARG2_LT_ARG3 100
882 #define BN_R_BAD_RECIPROCAL 101
883 #define BN_R_BIGNUM_TOO_LONG 114
884 #define BN_R_CALLED_WITH_EVEN_MODULUS 102
885 #define BN_R_DIV_BY_ZERO 103
886 #define BN_R_ENCODING_ERROR 104
887 #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
888 #define BN_R_INPUT_NOT_REDUCED 110
889 #define BN_R_INVALID_LENGTH 106
890 #define BN_R_INVALID_RANGE 115
891 #define BN_R_NOT_A_SQUARE 111
892 #define BN_R_NOT_INITIALIZED 107
893 #define BN_R_NO_INVERSE 108
894 #define BN_R_NO_SOLUTION 116
895 #define BN_R_P_IS_NOT_PRIME 112
896 #define BN_R_TOO_MANY_ITERATIONS 113
897 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109