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