5 bn - multiprecision integer arithmetics
9 #include <openssl/bn.h>
12 void BN_free(BIGNUM *a);
13 void BN_clear(BIGNUM *a);
14 void BN_clear_free(BIGNUM *a);
16 BN_CTX *BN_CTX_new(void);
17 void BN_CTX_free(BN_CTX *c);
19 BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
20 BIGNUM *BN_dup(const BIGNUM *a);
22 BIGNUM *BN_swap(BIGNUM *a, BIGNUM *b);
24 int BN_num_bytes(const BIGNUM *a);
25 int BN_num_bits(const BIGNUM *a);
26 int BN_num_bits_word(BN_ULONG w);
28 void BN_set_negative(BIGNUM *a, int n);
29 int BN_is_negative(const BIGNUM *a);
31 int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
32 int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
33 int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
34 int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);
35 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d,
37 int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
38 int BN_nnmod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
39 int BN_mod_add(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
41 int BN_mod_sub(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
43 int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
45 int BN_mod_sqr(BIGNUM *ret, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
46 int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
47 int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
48 const BIGNUM *m, BN_CTX *ctx);
49 int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
51 int BN_add_word(BIGNUM *a, BN_ULONG w);
52 int BN_sub_word(BIGNUM *a, BN_ULONG w);
53 int BN_mul_word(BIGNUM *a, BN_ULONG w);
54 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
55 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
57 int BN_cmp(BIGNUM *a, BIGNUM *b);
58 int BN_ucmp(BIGNUM *a, BIGNUM *b);
59 int BN_is_zero(BIGNUM *a);
60 int BN_is_one(BIGNUM *a);
61 int BN_is_word(BIGNUM *a, BN_ULONG w);
62 int BN_is_odd(BIGNUM *a);
64 int BN_zero(BIGNUM *a);
65 int BN_one(BIGNUM *a);
66 const BIGNUM *BN_value_one(void);
67 int BN_set_word(BIGNUM *a, unsigned long w);
68 unsigned long BN_get_word(BIGNUM *a);
70 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
71 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
72 int BN_rand_range(BIGNUM *rnd, BIGNUM *range);
73 int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);
75 int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add,
76 const BIGNUM *rem, BN_GENCB *cb);
78 int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb);
80 int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx,
81 int do_trial_division, BN_GENCB *cb);
83 int BN_GENCB_call(BN_GENCB *cb, int a, int b);
84 BN_GENCB *BN_GENCB_new(void);
85 void BN_GENCB_free(BN_GENCB *cb);
86 void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *), void *cb_arg);
87 void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *), void *cb_arg);
88 void *BN_GENCB_get_arg(BN_GENCB *cb);
90 int BN_set_bit(BIGNUM *a, int n);
91 int BN_clear_bit(BIGNUM *a, int n);
92 int BN_is_bit_set(const BIGNUM *a, int n);
93 int BN_mask_bits(BIGNUM *a, int n);
94 int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
95 int BN_lshift1(BIGNUM *r, BIGNUM *a);
96 int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
97 int BN_rshift1(BIGNUM *r, BIGNUM *a);
99 int BN_bn2bin(const BIGNUM *a, unsigned char *to);
100 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
101 char *BN_bn2hex(const BIGNUM *a);
102 char *BN_bn2dec(const BIGNUM *a);
103 int BN_hex2bn(BIGNUM **a, const char *str);
104 int BN_dec2bn(BIGNUM **a, const char *str);
105 int BN_print(BIO *fp, const BIGNUM *a);
106 int BN_print_fp(FILE *fp, const BIGNUM *a);
107 int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
108 BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
110 BIGNUM *BN_mod_inverse(BIGNUM *r, BIGNUM *a, const BIGNUM *n,
113 BN_RECP_CTX *BN_RECP_CTX_new(void);
114 void BN_RECP_CTX_free(BN_RECP_CTX *recp);
115 int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx);
116 int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *a, BIGNUM *b,
117 BN_RECP_CTX *recp, BN_CTX *ctx);
119 BN_MONT_CTX *BN_MONT_CTX_new(void);
120 void BN_MONT_CTX_free(BN_MONT_CTX *mont);
121 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
122 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
123 int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
124 BN_MONT_CTX *mont, BN_CTX *ctx);
125 int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
127 int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
130 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai,
132 void BN_BLINDING_free(BN_BLINDING *b);
133 int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx);
134 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
135 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
136 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b,
138 int BN_BLINDING_invert_ex(BIGNUM *n,const BIGNUM *r,BN_BLINDING *b,
140 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
141 void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
142 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
143 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
144 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
145 const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
146 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
147 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
152 This library performs arithmetic operations on integers of arbitrary
153 size. It was written for use in public key cryptography, such as RSA
156 It uses dynamic memory allocation for storing its data structures.
157 That means that there is no limit on the size of the numbers
158 manipulated by these functions, but return values must always be
159 checked in case a memory allocation error has occurred.
161 The basic object in this library is a B<BIGNUM>. It is used to hold a
162 single large integer. This type should be considered opaque and fields
163 should not be modified or accessed directly.
165 The creation of B<BIGNUM> objects is described in L<BN_new(3)|BN_new(3)>;
166 L<BN_add(3)|BN_add(3)> describes most of the arithmetic operations.
167 Comparison is described in L<BN_cmp(3)|BN_cmp(3)>; L<BN_zero(3)|BN_zero(3)>
168 describes certain assignments, L<BN_rand(3)|BN_rand(3)> the generation of
169 random numbers, L<BN_generate_prime(3)|BN_generate_prime(3)> deals with prime
170 numbers and L<BN_set_bit(3)|BN_set_bit(3)> with bit operations. The conversion
171 of B<BIGNUM>s to external formats is described in L<BN_bn2bin(3)|BN_bn2bin(3)>.
175 L<bn_internal(3)|bn_internal(3)>,
176 L<dh(3)|dh(3)>, L<err(3)|err(3)>, L<rand(3)|rand(3)>, L<rsa(3)|rsa(3)>,
177 L<BN_new(3)|BN_new(3)>, L<BN_CTX_new(3)|BN_CTX_new(3)>,
178 L<BN_copy(3)|BN_copy(3)>, L<BN_swap(3)|BN_swap(3)>, L<BN_num_bytes(3)|BN_num_bytes(3)>,
179 L<BN_add(3)|BN_add(3)>, L<BN_add_word(3)|BN_add_word(3)>,
180 L<BN_cmp(3)|BN_cmp(3)>, L<BN_zero(3)|BN_zero(3)>, L<BN_rand(3)|BN_rand(3)>,
181 L<BN_generate_prime(3)|BN_generate_prime(3)>, L<BN_set_bit(3)|BN_set_bit(3)>,
182 L<BN_bn2bin(3)|BN_bn2bin(3)>, L<BN_mod_inverse(3)|BN_mod_inverse(3)>,
183 L<BN_mod_mul_reciprocal(3)|BN_mod_mul_reciprocal(3)>,
184 L<BN_mod_mul_montgomery(3)|BN_mod_mul_montgomery(3)>,
185 L<BN_BLINDING_new(3)|BN_BLINDING_new(3)>