75 #include <openssl/e_os2.h>
76 #ifndef OPENSSL_NO_FP_API
79 #include <openssl/ossl_typ.h>
112 #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
113 defined(OPENSSL_SYS_WIN32) || defined(linux)
122 #ifdef SIXTY_FOUR_BIT_LONG
123 #define BN_ULLONG unsigned long long
124 #define BN_ULONG unsigned long
130 #define BN_MASK (0xffffffffffffffffffffffffffffffffLL)
131 #define BN_MASK2 (0xffffffffffffffffL)
132 #define BN_MASK2l (0xffffffffL)
133 #define BN_MASK2h (0xffffffff00000000L)
134 #define BN_MASK2h1 (0xffffffff80000000L)
135 #define BN_TBIT (0x8000000000000000L)
136 #define BN_DEC_CONV (10000000000000000000UL)
137 #define BN_DEC_FMT1 "%lu"
138 #define BN_DEC_FMT2 "%019lu"
139 #define BN_DEC_NUM 19
147 #ifdef SIXTY_FOUR_BIT
150 #define BN_ULONG unsigned long long
151 #define BN_LONG long long
156 #define BN_MASK2 (0xffffffffffffffffLL)
157 #define BN_MASK2l (0xffffffffL)
158 #define BN_MASK2h (0xffffffff00000000LL)
159 #define BN_MASK2h1 (0xffffffff80000000LL)
160 #define BN_TBIT (0x8000000000000000LL)
161 #define BN_DEC_CONV (10000000000000000000ULL)
162 #define BN_DEC_FMT1 "%llu"
163 #define BN_DEC_FMT2 "%019llu"
164 #define BN_DEC_NUM 19
167 #ifdef THIRTY_TWO_BIT
169 # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
170 # define BN_ULLONG unsigned __int64
172 # define BN_ULLONG unsigned long long
175 #define BN_ULONG unsigned long
181 #ifdef OPENSSL_SYS_WIN32
183 #define BN_MASK (0xffffffffffffffffL)
185 #define BN_MASK (0xffffffffffffffffLL)
187 #define BN_MASK2 (0xffffffffL)
188 #define BN_MASK2l (0xffff)
189 #define BN_MASK2h1 (0xffff8000L)
190 #define BN_MASK2h (0xffff0000L)
191 #define BN_TBIT (0x80000000L)
192 #define BN_DEC_CONV (1000000000L)
193 #define BN_DEC_FMT1 "%lu"
194 #define BN_DEC_FMT2 "%09lu"
202 #define BN_ULLONG unsigned long
203 #define BN_ULONG unsigned short
204 #define BN_LONG short
209 #define BN_MASK (0xffffffff)
210 #define BN_MASK2 (0xffff)
211 #define BN_MASK2l (0xff)
212 #define BN_MASK2h1 (0xff80)
213 #define BN_MASK2h (0xff00)
214 #define BN_TBIT (0x8000)
215 #define BN_DEC_CONV (100000)
216 #define BN_DEC_FMT1 "%u"
217 #define BN_DEC_FMT2 "%05u"
225 #define BN_ULLONG unsigned short
226 #define BN_ULONG unsigned char
232 #define BN_MASK (0xffff)
233 #define BN_MASK2 (0xff)
234 #define BN_MASK2l (0xf)
235 #define BN_MASK2h1 (0xf8)
236 #define BN_MASK2h (0xf0)
237 #define BN_TBIT (0x80)
238 #define BN_DEC_CONV (100)
239 #define BN_DEC_FMT1 "%u"
240 #define BN_DEC_FMT2 "%02u"
244 #define BN_DEFAULT_BITS 1280
246 #define BN_FLG_MALLOCED 0x01
247 #define BN_FLG_STATIC_DATA 0x02
248 #define BN_FLG_CONSTTIME 0x04
254 #ifndef OPENSSL_NO_DEPRECATED
255 #define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME
260 #ifndef OPENSSL_NO_DEPRECATED
261 #define BN_FLG_FREE 0x8000
263 #define BN_set_flags(b,n) ((b)->flags|=(n))
264 #define BN_get_flags(b,n) ((b)->flags&(n))
268 #define BN_with_flags(dest,b,n) ((dest)->d=(b)->d, \
269 (dest)->top=(b)->top, \
270 (dest)->dmax=(b)->dmax, \
271 (dest)->neg=(b)->neg, \
272 (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
273 | ((b)->flags & ~BN_FLG_MALLOCED) \
274 | BN_FLG_STATIC_DATA \
330 void (*cb_1)(int, int,
void *);
336 int BN_GENCB_call(
BN_GENCB *cb,
int a,
int b);
338 #define BN_GENCB_set_old(gencb, callback, cb_arg) { \
339 BN_GENCB *tmp_gencb = (gencb); \
340 tmp_gencb->ver = 1; \
341 tmp_gencb->arg = (cb_arg); \
342 tmp_gencb->cb.cb_1 = (callback); }
344 #define BN_GENCB_set(gencb, callback, cb_arg) { \
345 BN_GENCB *tmp_gencb = (gencb); \
346 tmp_gencb->ver = 2; \
347 tmp_gencb->arg = (cb_arg); \
348 tmp_gencb->cb.cb_2 = (callback); }
350 #define BN_prime_checks 0
358 #define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \
371 #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
374 #define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
375 (((w) == 0) && ((a)->top == 0)))
376 #define BN_is_zero(a) ((a)->top == 0)
377 #define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg)
378 #define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
379 #define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
381 #define BN_one(a) (BN_set_word((a),1))
382 #define BN_zero_ex(a) \
384 BIGNUM *_tmp_bn = (a); \
388 #ifdef OPENSSL_NO_DEPRECATED
389 #define BN_zero(a) BN_zero_ex(a)
391 #define BN_zero(a) (BN_set_word((a),0))
394 const BIGNUM *BN_value_one(
void);
395 char * BN_options(
void);
397 #ifndef OPENSSL_NO_DEPRECATED
398 void BN_CTX_init(
BN_CTX *c);
400 void BN_CTX_free(
BN_CTX *c);
401 void BN_CTX_start(
BN_CTX *ctx);
403 void BN_CTX_end(
BN_CTX *ctx);
404 int BN_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
405 int BN_pseudo_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
408 int BN_num_bits(
const BIGNUM *a);
409 int BN_num_bits_word(BN_ULONG);
412 void BN_clear_free(
BIGNUM *a);
415 BIGNUM *BN_bin2bn(
const unsigned char *s,
int len,
BIGNUM *ret);
416 int BN_bn2bin(
const BIGNUM *a,
unsigned char *to);
417 BIGNUM *BN_mpi2bn(
const unsigned char *s,
int len,
BIGNUM *ret);
418 int BN_bn2mpi(
const BIGNUM *a,
unsigned char *to);
429 void BN_set_negative(
BIGNUM *b,
int n);
434 #define BN_is_negative(a) ((a)->neg != 0)
438 #define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
452 BN_ULONG BN_mod_word(
const BIGNUM *a, BN_ULONG w);
453 BN_ULONG BN_div_word(
BIGNUM *a, BN_ULONG w);
454 int BN_mul_word(
BIGNUM *a, BN_ULONG w);
455 int BN_add_word(
BIGNUM *a, BN_ULONG w);
456 int BN_sub_word(
BIGNUM *a, BN_ULONG w);
457 int BN_set_word(
BIGNUM *a, BN_ULONG w);
458 BN_ULONG BN_get_word(
const BIGNUM *a);
462 int BN_is_bit_set(
const BIGNUM *a,
int n);
473 int BN_mod_exp_mont_word(
BIGNUM *r, BN_ULONG a,
const BIGNUM *p,
481 int BN_mask_bits(
BIGNUM *a,
int n);
482 #ifndef OPENSSL_NO_FP_API
483 int BN_print_fp(FILE *fp,
const BIGNUM *a);
488 int BN_print(
void *fp,
const BIGNUM *a);
496 int BN_set_bit(
BIGNUM *a,
int n);
497 int BN_clear_bit(
BIGNUM *a,
int n);
498 char * BN_bn2hex(
const BIGNUM *a);
499 char * BN_bn2dec(
const BIGNUM *a);
500 int BN_hex2bn(
BIGNUM **a,
const char *str);
501 int BN_dec2bn(
BIGNUM **a,
const char *str);
510 #ifndef OPENSSL_NO_DEPRECATED
511 BIGNUM *BN_generate_prime(
BIGNUM *ret,
int bits,
int safe,
513 void (*callback)(
int,
int,
void *),
void *cb_arg);
514 int BN_is_prime(
const BIGNUM *p,
int nchecks,
515 void (*callback)(
int,
int,
void *),
516 BN_CTX *ctx,
void *cb_arg);
517 int BN_is_prime_fasttest(
const BIGNUM *p,
int nchecks,
518 void (*callback)(
int,
int,
void *),
BN_CTX *ctx,
void *cb_arg,
519 int do_trial_division);
523 int BN_generate_prime_ex(
BIGNUM *ret,
int bits,
int safe,
const BIGNUM *add,
526 int BN_is_prime_fasttest_ex(
const BIGNUM *p,
int nchecks,
BN_CTX *ctx,
527 int do_trial_division,
BN_GENCB *cb);
533 #define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
534 (r),(a),&((mont)->RR),(mont),(ctx))
544 #define BN_BLINDING_NO_UPDATE 0x00000001
545 #define BN_BLINDING_NO_RECREATE 0x00000002
554 unsigned long BN_BLINDING_get_thread_id(
const BN_BLINDING *);
555 void BN_BLINDING_set_thread_id(
BN_BLINDING *,
unsigned long);
556 unsigned long BN_BLINDING_get_flags(
const BN_BLINDING *);
557 void BN_BLINDING_set_flags(
BN_BLINDING *,
unsigned long);
564 #ifndef OPENSSL_NO_DEPRECATED
565 void BN_set_params(
int mul,
int high,
int low,
int mont);
566 int BN_get_params(
int which);
590 #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
606 #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
612 int BN_GF2m_mod_arr(
BIGNUM *r,
const BIGNUM *a,
const unsigned int p[]);
615 const unsigned int p[],
BN_CTX *ctx);
616 int BN_GF2m_mod_sqr_arr(
BIGNUM *r,
const BIGNUM *a,
const unsigned int p[],
618 int BN_GF2m_mod_inv_arr(
BIGNUM *r,
const BIGNUM *b,
const unsigned int p[],
621 const unsigned int p[],
BN_CTX *ctx);
623 const unsigned int p[],
BN_CTX *ctx);
625 const unsigned int p[],
BN_CTX *ctx);
626 int BN_GF2m_mod_solve_quad_arr(
BIGNUM *r,
const BIGNUM *a,
627 const unsigned int p[],
BN_CTX *ctx);
628 int BN_GF2m_poly2arr(
const BIGNUM *a,
unsigned int p[],
int max);
629 int BN_GF2m_arr2poly(
const unsigned int p[],
BIGNUM *a);
639 const BIGNUM *BN_get0_nist_prime_192(
void);
640 const BIGNUM *BN_get0_nist_prime_224(
void);
641 const BIGNUM *BN_get0_nist_prime_256(
void);
642 const BIGNUM *BN_get0_nist_prime_384(
void);
643 const BIGNUM *BN_get0_nist_prime_521(
void);
647 #define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
648 (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2))
649 #define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
651 #ifndef OPENSSL_NO_DEPRECATED
690 #ifndef RAND_pseudo_bytes
691 int RAND_pseudo_bytes(
unsigned char *buf,
int num);
692 #define BN_DEBUG_TRIX
694 #define bn_pollute(a) \
696 const BIGNUM *_bnum1 = (a); \
697 if(_bnum1->top < _bnum1->dmax) { \
698 unsigned char _tmp_char; \
702 BN_ULONG *_not_const; \
703 memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
704 RAND_pseudo_bytes(&_tmp_char, 1); \
705 memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
706 (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
710 #undef RAND_pseudo_bytes
713 #define bn_pollute(a)
715 #define bn_check_top(a) \
717 const BIGNUM *_bnum2 = (a); \
718 if (_bnum2 != NULL) { \
719 assert((_bnum2->top == 0) || \
720 (_bnum2->d[_bnum2->top - 1] != 0)); \
721 bn_pollute(_bnum2); \
725 #define bn_fix_top(a) bn_check_top(a)
729 #define bn_pollute(a)
730 #define bn_check_top(a)
731 #define bn_fix_top(a) bn_correct_top(a)
735 #define bn_correct_top(a) \
740 for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
741 if (*(ftl--)) break; \
746 BN_ULONG bn_mul_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
747 BN_ULONG bn_mul_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num, BN_ULONG w);
748 void bn_sqr_words(BN_ULONG *rp,
const BN_ULONG *ap,
int num);
749 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
750 BN_ULONG bn_add_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
int num);
751 BN_ULONG bn_sub_words(BN_ULONG *rp,
const BN_ULONG *ap,
const BN_ULONG *bp,
int num);
765 int BN_bntest_rand(
BIGNUM *rnd,
int bits,
int top,
int bottom);
771 void ERR_load_BN_strings(
void);
776 #define BN_F_BNRAND 127
777 #define BN_F_BN_BLINDING_CONVERT_EX 100
778 #define BN_F_BN_BLINDING_CREATE_PARAM 128
779 #define BN_F_BN_BLINDING_INVERT_EX 101
780 #define BN_F_BN_BLINDING_NEW 102
781 #define BN_F_BN_BLINDING_UPDATE 103
782 #define BN_F_BN_BN2DEC 104
783 #define BN_F_BN_BN2HEX 105
784 #define BN_F_BN_CTX_GET 116
785 #define BN_F_BN_CTX_NEW 106
786 #define BN_F_BN_CTX_START 129
787 #define BN_F_BN_DIV 107
788 #define BN_F_BN_DIV_NO_BRANCH 138
789 #define BN_F_BN_DIV_RECP 130
790 #define BN_F_BN_EXP 123
791 #define BN_F_BN_EXPAND2 108
792 #define BN_F_BN_EXPAND_INTERNAL 120
793 #define BN_F_BN_GF2M_MOD 131
794 #define BN_F_BN_GF2M_MOD_EXP 132
795 #define BN_F_BN_GF2M_MOD_MUL 133
796 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134
797 #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135
798 #define BN_F_BN_GF2M_MOD_SQR 136
799 #define BN_F_BN_GF2M_MOD_SQRT 137
800 #define BN_F_BN_MOD_EXP2_MONT 118
801 #define BN_F_BN_MOD_EXP_MONT 109
802 #define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
803 #define BN_F_BN_MOD_EXP_MONT_WORD 117
804 #define BN_F_BN_MOD_EXP_RECP 125
805 #define BN_F_BN_MOD_EXP_SIMPLE 126
806 #define BN_F_BN_MOD_INVERSE 110
807 #define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
808 #define BN_F_BN_MOD_LSHIFT_QUICK 119
809 #define BN_F_BN_MOD_MUL_RECIPROCAL 111
810 #define BN_F_BN_MOD_SQRT 121
811 #define BN_F_BN_MPI2BN 112
812 #define BN_F_BN_NEW 113
813 #define BN_F_BN_RAND 114
814 #define BN_F_BN_RAND_RANGE 122
815 #define BN_F_BN_USUB 115
818 #define BN_R_ARG2_LT_ARG3 100
819 #define BN_R_BAD_RECIPROCAL 101
820 #define BN_R_BIGNUM_TOO_LONG 114
821 #define BN_R_CALLED_WITH_EVEN_MODULUS 102
822 #define BN_R_DIV_BY_ZERO 103
823 #define BN_R_ENCODING_ERROR 104
824 #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
825 #define BN_R_INPUT_NOT_REDUCED 110
826 #define BN_R_INVALID_LENGTH 106
827 #define BN_R_INVALID_RANGE 115
828 #define BN_R_NOT_A_SQUARE 111
829 #define BN_R_NOT_INITIALIZED 107
830 #define BN_R_NO_INVERSE 108
831 #define BN_R_NO_SOLUTION 116
832 #define BN_R_P_IS_NOT_PRIME 112
833 #define BN_R_TOO_MANY_ITERATIONS 113
834 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
Definition: bn_blind.c:118