Update the dasync engine to add a pipeline cipher
[openssl.git] / doc / crypto / bn.pod
1 =pod
2
3 =head1 NAME
4
5 bn - multiprecision integer arithmetics
6
7 =head1 SYNOPSIS
8
9  #include <openssl/bn.h>
10
11  BIGNUM *BN_new(void);
12  void BN_free(BIGNUM *a);
13  void BN_clear(BIGNUM *a);
14  void BN_clear_free(BIGNUM *a);
15
16  BN_CTX *BN_CTX_new(void);
17  BN_CTX *BN_CTX_secure_new(void);
18  void BN_CTX_free(BN_CTX *c);
19
20  BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
21  BIGNUM *BN_dup(const BIGNUM *a);
22
23  BIGNUM *BN_swap(BIGNUM *a, BIGNUM *b);
24
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
29  void BN_set_negative(BIGNUM *a, int n);
30  int  BN_is_negative(const BIGNUM *a);
31
32  int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
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);
35  int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);
36  int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d,
37          BN_CTX *ctx);
38  int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
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);
44  int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
45          BN_CTX *ctx);
46  int BN_mod_sqr(BIGNUM *ret, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
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);
56  BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
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);
67  const BIGNUM *BN_value_one(void);
68  int BN_set_word(BIGNUM *a, unsigned long w);
69  unsigned long BN_get_word(BIGNUM *a);
70
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);
73  int BN_rand_range(BIGNUM *rnd, BIGNUM *range);
74  int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);
75
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);
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);
106  int BN_print(BIO *fp, const BIGNUM *a);
107  int BN_print_fp(FILE *fp, const BIGNUM *a);
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);
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);
121  void BN_MONT_CTX_free(BN_MONT_CTX *mont);
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
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);
141  unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
142  void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
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);
150
151 =head1 DESCRIPTION
152
153 This library performs arithmetic operations on integers of arbitrary
154 size. It was written for use in public key cryptography, such as RSA
155 and Diffie-Hellman.
156
157 It uses dynamic memory allocation for storing its data structures.
158 That means that there is no limit on the size of the numbers
159 manipulated by these functions, but return values must always be
160 checked in case a memory allocation error has occurred.
161
162 The basic object in this library is a B<BIGNUM>. It is used to hold a
163 single large integer. This type should be considered opaque and fields
164 should not be modified or accessed directly.
165
166 The creation of B<BIGNUM> objects is described in L<BN_new(3)>;
167 L<BN_add(3)> describes most of the arithmetic operations.
168 Comparison is described in L<BN_cmp(3)>; L<BN_zero(3)>
169 describes certain assignments, L<BN_rand(3)> the generation of
170 random numbers, L<BN_generate_prime(3)> deals with prime
171 numbers and L<BN_set_bit(3)> with bit operations. The conversion
172 of B<BIGNUM>s to external formats is described in L<BN_bn2bin(3)>.
173
174 =head1 SEE ALSO
175
176 L<bn_internal(3)>,
177 L<dh(3)>, L<err(3)>, L<rand(3)>, L<rsa(3)>,
178 L<BN_new(3)>, L<BN_CTX_new(3)>,
179 L<BN_copy(3)>, L<BN_swap(3)>, L<BN_num_bytes(3)>,
180 L<BN_add(3)>, L<BN_add_word(3)>,
181 L<BN_cmp(3)>, L<BN_zero(3)>, L<BN_rand(3)>,
182 L<BN_generate_prime(3)>, L<BN_set_bit(3)>,
183 L<BN_bn2bin(3)>, L<BN_mod_inverse(3)>,
184 L<BN_mod_mul_reciprocal(3)>,
185 L<BN_mod_mul_montgomery(3)>,
186 L<BN_BLINDING_new(3)>
187
188 =cut