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 \
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);
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);
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))
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);
504 int BN_mod_exp_mont_word(
BIGNUM *r, BN_ULONG a,
const BIGNUM *p,
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);
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);
542 #ifndef OPENSSL_NO_DEPRECATED
543 BIGNUM *BN_generate_prime(
BIGNUM *ret,
int bits,
int safe,
545 void (*callback)(
int,
int,
void *),
void *cb_arg);
546 int BN_is_prime(
const BIGNUM *p,
int nchecks,
547 void (*callback)(
int,
int,
void *),
548 BN_CTX *ctx,
void *cb_arg);
549 int BN_is_prime_fasttest(
const BIGNUM *p,
int nchecks,
550 void (*callback)(
int,
int,
void *),
BN_CTX *ctx,
void *cb_arg,
551 int do_trial_division);
555 int BN_generate_prime_ex(
BIGNUM *ret,
int bits,
int safe,
const BIGNUM *add,
558 int BN_is_prime_fasttest_ex(
const BIGNUM *p,
int nchecks,
BN_CTX *ctx,
559 int do_trial_division,
BN_GENCB *cb);
576 #define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
577 (r),(a),&((mont)->RR),(mont),(ctx))
587 #define BN_BLINDING_NO_UPDATE 0x00000001
588 #define BN_BLINDING_NO_RECREATE 0x00000002
597 #ifndef OPENSSL_NO_DEPRECATED
598 unsigned long BN_BLINDING_get_thread_id(
const BN_BLINDING *);
599 void BN_BLINDING_set_thread_id(
BN_BLINDING *,
unsigned long);
602 unsigned long BN_BLINDING_get_flags(
const BN_BLINDING *);
603 void BN_BLINDING_set_flags(
BN_BLINDING *,
unsigned long);
610 #ifndef OPENSSL_NO_DEPRECATED
611 void BN_set_params(
int mul,
int high,
int low,
int mont);
612 int BN_get_params(
int which);
626 #ifndef OPENSSL_NO_EC2M
638 #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
654 #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
660 int BN_GF2m_mod_arr(
BIGNUM *r,
const BIGNUM *a,
const int p[]);
663 const int p[],
BN_CTX *ctx);
664 int BN_GF2m_mod_sqr_arr(
BIGNUM *r,
const BIGNUM *a,
const int p[],
666 int BN_GF2m_mod_inv_arr(
BIGNUM *r,
const BIGNUM *b,
const int p[],
669 const int p[],
BN_CTX *ctx);
671 const int p[],
BN_CTX *ctx);
673 const int p[],
BN_CTX *ctx);
674 int BN_GF2m_mod_solve_quad_arr(
BIGNUM *r,
const BIGNUM *a,
675 const int p[],
BN_CTX *ctx);
676 int BN_GF2m_poly2arr(
const BIGNUM *a,
int p[],
int max);
677 int BN_GF2m_arr2poly(
const int p[],
BIGNUM *a);
689 const BIGNUM *BN_get0_nist_prime_192(
void);
690 const BIGNUM *BN_get0_nist_prime_224(
void);
691 const BIGNUM *BN_get0_nist_prime_256(
void);
692 const BIGNUM *BN_get0_nist_prime_384(
void);
693 const BIGNUM *BN_get0_nist_prime_521(
void);
697 #define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
698 (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2))
699 #define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
701 #ifndef OPENSSL_NO_DEPRECATED
740 #ifndef RAND_pseudo_bytes
741 int RAND_pseudo_bytes(
unsigned char *buf,
int num);
742 #define BN_DEBUG_TRIX
744 #define bn_pollute(a) \
746 const BIGNUM *_bnum1 = (a); \
747 if(_bnum1->top < _bnum1->dmax) { \
748 unsigned char _tmp_char; \
752 BN_ULONG *_not_const; \
753 memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
754 RAND_pseudo_bytes(&_tmp_char, 1); \
755 memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
756 (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
760 #undef RAND_pseudo_bytes
763 #define bn_pollute(a)
765 #define bn_check_top(a) \
767 const BIGNUM *_bnum2 = (a); \
768 if (_bnum2 != NULL) { \
769 assert((_bnum2->top == 0) || \
770 (_bnum2->d[_bnum2->top - 1] != 0)); \
771 bn_pollute(_bnum2); \
775 #define bn_fix_top(a) bn_check_top(a)
779 #define bn_pollute(a)
780 #define bn_check_top(a)
781 #define bn_fix_top(a) bn_correct_top(a)
785 #define bn_correct_top(a) \
788 int tmp_top = (a)->top; \
791 for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \
792 if (*(ftl--)) break; \
793 (a)->top = tmp_top; \
798 BN_ULONG bn_mul_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
799 BN_ULONG bn_mul_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
800 void bn_sqr_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num);
801 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
802 BN_ULONG bn_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
int num);
803 BN_ULONG bn_sub_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
int num);
817 int BN_bntest_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
823 void ERR_load_BN_strings(
void);
828 #define BN_F_BNRAND 127
829 #define BN_F_BN_BLINDING_CONVERT_EX 100
830 #define BN_F_BN_BLINDING_CREATE_PARAM 128
831 #define BN_F_BN_BLINDING_INVERT_EX 101
832 #define BN_F_BN_BLINDING_NEW 102
833 #define BN_F_BN_BLINDING_UPDATE 103
834 #define BN_F_BN_BN2DEC 104
835 #define BN_F_BN_BN2HEX 105
836 #define BN_F_BN_CTX_GET 116
837 #define BN_F_BN_CTX_NEW 106
838 #define BN_F_BN_CTX_START 129
839 #define BN_F_BN_DIV 107
840 #define BN_F_BN_DIV_NO_BRANCH 138
841 #define BN_F_BN_DIV_RECP 130
842 #define BN_F_BN_EXP 123
843 #define BN_F_BN_EXPAND2 108
844 #define BN_F_BN_EXPAND_INTERNAL 120
845 #define BN_F_BN_GF2M_MOD 131
846 #define BN_F_BN_GF2M_MOD_EXP 132
847 #define BN_F_BN_GF2M_MOD_MUL 133
848 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134
849 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135
850 #define BN_F_BN_GF2M_MOD_SQR 136
851 #define BN_F_BN_GF2M_MOD_SQRT 137
852 #define BN_F_BN_MOD_EXP2_MONT 118
853 #define BN_F_BN_MOD_EXP_MONT 109
854 #define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
855 #define BN_F_BN_MOD_EXP_MONT_WORD 117
856 #define BN_F_BN_MOD_EXP_RECP 125
857 #define BN_F_BN_MOD_EXP_SIMPLE 126
858 #define BN_F_BN_MOD_INVERSE 110
859 #define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
860 #define BN_F_BN_MOD_LSHIFT_QUICK 119
861 #define BN_F_BN_MOD_MUL_RECIPROCAL 111
862 #define BN_F_BN_MOD_SQRT 121
863 #define BN_F_BN_MPI2BN 112
864 #define BN_F_BN_NEW 113
865 #define BN_F_BN_RAND 114
866 #define BN_F_BN_RAND_RANGE 122
867 #define BN_F_BN_USUB 115
870 #define BN_R_ARG2_LT_ARG3 100
871 #define BN_R_BAD_RECIPROCAL 101
872 #define BN_R_BIGNUM_TOO_LONG 114
873 #define BN_R_CALLED_WITH_EVEN_MODULUS 102
874 #define BN_R_DIV_BY_ZERO 103
875 #define BN_R_ENCODING_ERROR 104
876 #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
877 #define BN_R_INPUT_NOT_REDUCED 110
878 #define BN_R_INVALID_LENGTH 106
879 #define BN_R_INVALID_RANGE 115
880 #define BN_R_NOT_A_SQUARE 111
881 #define BN_R_NOT_INITIALIZED 107
882 #define BN_R_NO_INVERSE 108
883 #define BN_R_NO_SOLUTION 116
884 #define BN_R_P_IS_NOT_PRIME 112
885 #define BN_R_TOO_MANY_ITERATIONS 113
886 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
Definition: bn_blind.c:118