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