1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948bn - multiprecision integer arithmetics 6*2175Sjp161948 7*2175Sjp161948=head1 SYNOPSIS 8*2175Sjp161948 9*2175Sjp161948 #include <openssl/bn.h> 10*2175Sjp161948 11*2175Sjp161948 BIGNUM *BN_new(void); 12*2175Sjp161948 void BN_free(BIGNUM *a); 13*2175Sjp161948 void BN_init(BIGNUM *); 14*2175Sjp161948 void BN_clear(BIGNUM *a); 15*2175Sjp161948 void BN_clear_free(BIGNUM *a); 16*2175Sjp161948 17*2175Sjp161948 BN_CTX *BN_CTX_new(void); 18*2175Sjp161948 void BN_CTX_init(BN_CTX *c); 19*2175Sjp161948 void BN_CTX_free(BN_CTX *c); 20*2175Sjp161948 21*2175Sjp161948 BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b); 22*2175Sjp161948 BIGNUM *BN_dup(const BIGNUM *a); 23*2175Sjp161948 24*2175Sjp161948 BIGNUM *BN_swap(BIGNUM *a, BIGNUM *b); 25*2175Sjp161948 26*2175Sjp161948 int BN_num_bytes(const BIGNUM *a); 27*2175Sjp161948 int BN_num_bits(const BIGNUM *a); 28*2175Sjp161948 int BN_num_bits_word(BN_ULONG w); 29*2175Sjp161948 30*2175Sjp161948 void BN_set_negative(BIGNUM *a, int n); 31*2175Sjp161948 int BN_is_negative(const BIGNUM *a); 32*2175Sjp161948 33*2175Sjp161948 int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 34*2175Sjp161948 int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 35*2175Sjp161948 int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx); 36*2175Sjp161948 int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx); 37*2175Sjp161948 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d, 38*2175Sjp161948 BN_CTX *ctx); 39*2175Sjp161948 int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 40*2175Sjp161948 int BN_nnmod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 41*2175Sjp161948 int BN_mod_add(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, 42*2175Sjp161948 BN_CTX *ctx); 43*2175Sjp161948 int BN_mod_sub(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, 44*2175Sjp161948 BN_CTX *ctx); 45*2175Sjp161948 int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, 46*2175Sjp161948 BN_CTX *ctx); 47*2175Sjp161948 int BN_mod_sqr(BIGNUM *ret, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 48*2175Sjp161948 int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx); 49*2175Sjp161948 int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, 50*2175Sjp161948 const BIGNUM *m, BN_CTX *ctx); 51*2175Sjp161948 int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx); 52*2175Sjp161948 53*2175Sjp161948 int BN_add_word(BIGNUM *a, BN_ULONG w); 54*2175Sjp161948 int BN_sub_word(BIGNUM *a, BN_ULONG w); 55*2175Sjp161948 int BN_mul_word(BIGNUM *a, BN_ULONG w); 56*2175Sjp161948 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); 57*2175Sjp161948 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); 58*2175Sjp161948 59*2175Sjp161948 int BN_cmp(BIGNUM *a, BIGNUM *b); 60*2175Sjp161948 int BN_ucmp(BIGNUM *a, BIGNUM *b); 61*2175Sjp161948 int BN_is_zero(BIGNUM *a); 62*2175Sjp161948 int BN_is_one(BIGNUM *a); 63*2175Sjp161948 int BN_is_word(BIGNUM *a, BN_ULONG w); 64*2175Sjp161948 int BN_is_odd(BIGNUM *a); 65*2175Sjp161948 66*2175Sjp161948 int BN_zero(BIGNUM *a); 67*2175Sjp161948 int BN_one(BIGNUM *a); 68*2175Sjp161948 const BIGNUM *BN_value_one(void); 69*2175Sjp161948 int BN_set_word(BIGNUM *a, unsigned long w); 70*2175Sjp161948 unsigned long BN_get_word(BIGNUM *a); 71*2175Sjp161948 72*2175Sjp161948 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); 73*2175Sjp161948 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); 74*2175Sjp161948 int BN_rand_range(BIGNUM *rnd, BIGNUM *range); 75*2175Sjp161948 int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range); 76*2175Sjp161948 77*2175Sjp161948 BIGNUM *BN_generate_prime(BIGNUM *ret, int bits,int safe, BIGNUM *add, 78*2175Sjp161948 BIGNUM *rem, void (*callback)(int, int, void *), void *cb_arg); 79*2175Sjp161948 int BN_is_prime(const BIGNUM *p, int nchecks, 80*2175Sjp161948 void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg); 81*2175Sjp161948 82*2175Sjp161948 int BN_set_bit(BIGNUM *a, int n); 83*2175Sjp161948 int BN_clear_bit(BIGNUM *a, int n); 84*2175Sjp161948 int BN_is_bit_set(const BIGNUM *a, int n); 85*2175Sjp161948 int BN_mask_bits(BIGNUM *a, int n); 86*2175Sjp161948 int BN_lshift(BIGNUM *r, const BIGNUM *a, int n); 87*2175Sjp161948 int BN_lshift1(BIGNUM *r, BIGNUM *a); 88*2175Sjp161948 int BN_rshift(BIGNUM *r, BIGNUM *a, int n); 89*2175Sjp161948 int BN_rshift1(BIGNUM *r, BIGNUM *a); 90*2175Sjp161948 91*2175Sjp161948 int BN_bn2bin(const BIGNUM *a, unsigned char *to); 92*2175Sjp161948 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 93*2175Sjp161948 char *BN_bn2hex(const BIGNUM *a); 94*2175Sjp161948 char *BN_bn2dec(const BIGNUM *a); 95*2175Sjp161948 int BN_hex2bn(BIGNUM **a, const char *str); 96*2175Sjp161948 int BN_dec2bn(BIGNUM **a, const char *str); 97*2175Sjp161948 int BN_print(BIO *fp, const BIGNUM *a); 98*2175Sjp161948 int BN_print_fp(FILE *fp, const BIGNUM *a); 99*2175Sjp161948 int BN_bn2mpi(const BIGNUM *a, unsigned char *to); 100*2175Sjp161948 BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret); 101*2175Sjp161948 102*2175Sjp161948 BIGNUM *BN_mod_inverse(BIGNUM *r, BIGNUM *a, const BIGNUM *n, 103*2175Sjp161948 BN_CTX *ctx); 104*2175Sjp161948 105*2175Sjp161948 BN_RECP_CTX *BN_RECP_CTX_new(void); 106*2175Sjp161948 void BN_RECP_CTX_init(BN_RECP_CTX *recp); 107*2175Sjp161948 void BN_RECP_CTX_free(BN_RECP_CTX *recp); 108*2175Sjp161948 int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx); 109*2175Sjp161948 int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *a, BIGNUM *b, 110*2175Sjp161948 BN_RECP_CTX *recp, BN_CTX *ctx); 111*2175Sjp161948 112*2175Sjp161948 BN_MONT_CTX *BN_MONT_CTX_new(void); 113*2175Sjp161948 void BN_MONT_CTX_init(BN_MONT_CTX *ctx); 114*2175Sjp161948 void BN_MONT_CTX_free(BN_MONT_CTX *mont); 115*2175Sjp161948 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx); 116*2175Sjp161948 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from); 117*2175Sjp161948 int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, 118*2175Sjp161948 BN_MONT_CTX *mont, BN_CTX *ctx); 119*2175Sjp161948 int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont, 120*2175Sjp161948 BN_CTX *ctx); 121*2175Sjp161948 int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont, 122*2175Sjp161948 BN_CTX *ctx); 123*2175Sjp161948 124*2175Sjp161948 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, 125*2175Sjp161948 BIGNUM *mod); 126*2175Sjp161948 void BN_BLINDING_free(BN_BLINDING *b); 127*2175Sjp161948 int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx); 128*2175Sjp161948 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 129*2175Sjp161948 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 130*2175Sjp161948 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, 131*2175Sjp161948 BN_CTX *ctx); 132*2175Sjp161948 int BN_BLINDING_invert_ex(BIGNUM *n,const BIGNUM *r,BN_BLINDING *b, 133*2175Sjp161948 BN_CTX *ctx); 134*2175Sjp161948 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *); 135*2175Sjp161948 void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long); 136*2175Sjp161948 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *); 137*2175Sjp161948 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); 138*2175Sjp161948 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, 139*2175Sjp161948 const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, 140*2175Sjp161948 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 141*2175Sjp161948 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), 142*2175Sjp161948 BN_MONT_CTX *m_ctx); 143*2175Sjp161948 144*2175Sjp161948=head1 DESCRIPTION 145*2175Sjp161948 146*2175Sjp161948This library performs arithmetic operations on integers of arbitrary 147*2175Sjp161948size. It was written for use in public key cryptography, such as RSA 148*2175Sjp161948and Diffie-Hellman. 149*2175Sjp161948 150*2175Sjp161948It uses dynamic memory allocation for storing its data structures. 151*2175Sjp161948That means that there is no limit on the size of the numbers 152*2175Sjp161948manipulated by these functions, but return values must always be 153*2175Sjp161948checked in case a memory allocation error has occurred. 154*2175Sjp161948 155*2175Sjp161948The basic object in this library is a B<BIGNUM>. It is used to hold a 156*2175Sjp161948single large integer. This type should be considered opaque and fields 157*2175Sjp161948should not be modified or accessed directly. 158*2175Sjp161948 159*2175Sjp161948The creation of B<BIGNUM> objects is described in L<BN_new(3)|BN_new(3)>; 160*2175Sjp161948L<BN_add(3)|BN_add(3)> describes most of the arithmetic operations. 161*2175Sjp161948Comparison is described in L<BN_cmp(3)|BN_cmp(3)>; L<BN_zero(3)|BN_zero(3)> 162*2175Sjp161948describes certain assignments, L<BN_rand(3)|BN_rand(3)> the generation of 163*2175Sjp161948random numbers, L<BN_generate_prime(3)|BN_generate_prime(3)> deals with prime 164*2175Sjp161948numbers and L<BN_set_bit(3)|BN_set_bit(3)> with bit operations. The conversion 165*2175Sjp161948of B<BIGNUM>s to external formats is described in L<BN_bn2bin(3)|BN_bn2bin(3)>. 166*2175Sjp161948 167*2175Sjp161948=head1 SEE ALSO 168*2175Sjp161948 169*2175Sjp161948L<bn_internal(3)|bn_internal(3)>, 170*2175Sjp161948L<dh(3)|dh(3)>, L<err(3)|err(3)>, L<rand(3)|rand(3)>, L<rsa(3)|rsa(3)>, 171*2175Sjp161948L<BN_new(3)|BN_new(3)>, L<BN_CTX_new(3)|BN_CTX_new(3)>, 172*2175Sjp161948L<BN_copy(3)|BN_copy(3)>, L<BN_swap(3)|BN_swap(3)>, L<BN_num_bytes(3)|BN_num_bytes(3)>, 173*2175Sjp161948L<BN_add(3)|BN_add(3)>, L<BN_add_word(3)|BN_add_word(3)>, 174*2175Sjp161948L<BN_cmp(3)|BN_cmp(3)>, L<BN_zero(3)|BN_zero(3)>, L<BN_rand(3)|BN_rand(3)>, 175*2175Sjp161948L<BN_generate_prime(3)|BN_generate_prime(3)>, L<BN_set_bit(3)|BN_set_bit(3)>, 176*2175Sjp161948L<BN_bn2bin(3)|BN_bn2bin(3)>, L<BN_mod_inverse(3)|BN_mod_inverse(3)>, 177*2175Sjp161948L<BN_mod_mul_reciprocal(3)|BN_mod_mul_reciprocal(3)>, 178*2175Sjp161948L<BN_mod_mul_montgomery(3)|BN_mod_mul_montgomery(3)>, 179*2175Sjp161948L<BN_BLINDING_new(3)|BN_BLINDING_new(3)> 180*2175Sjp161948 181*2175Sjp161948=cut 182