minor docs changes (added links is the openssl(1) text)
[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_init(BIGNUM *);
14  void BN_clear(BIGNUM *a);
15  void BN_clear_free(BIGNUM *a);
16
17  BN_CTX *BN_CTX_new(void);
18  void BN_CTX_init(BN_CTX *c);
19  void BN_CTX_free(BN_CTX *c);
20
21  BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
22  BIGNUM *BN_dup(const BIGNUM *a);
23
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);
27
28  int BN_add(BIGNUM *r, BIGNUM *a, BIGNUM *b);
29  int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
30  int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
31  int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d,
32          BN_CTX *ctx);
33  int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);
34  int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
35  int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
36          BN_CTX *ctx);
37  int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
38  int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
39          const BIGNUM *m, BN_CTX *ctx);
40  int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
41
42  int BN_add_word(BIGNUM *a, BN_ULONG w);
43  int BN_sub_word(BIGNUM *a, BN_ULONG w);
44  int BN_mul_word(BIGNUM *a, BN_ULONG w);
45  BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
46  BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
47
48  int BN_cmp(BIGNUM *a, BIGNUM *b);
49  int BN_ucmp(BIGNUM *a, BIGNUM *b);
50  int BN_is_zero(BIGNUM *a);
51  int BN_is_one(BIGNUM *a);
52  int BN_is_word(BIGNUM *a, BN_ULONG w);
53  int BN_is_odd(BIGNUM *a);
54
55  int BN_zero(BIGNUM *a);
56  int BN_one(BIGNUM *a);
57  BIGNUM *BN_value_one(void);
58  int BN_set_word(BIGNUM *a, unsigned long w);
59  unsigned long BN_get_word(BIGNUM *a);
60
61  int BN_rand(BIGNUM *rnd, int bits, int top,int bottom);
62  int BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom);
63
64  BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,BIGNUM *add,
65          BIGNUM *rem,void (*callback)(int,int,void *),void *cb_arg);
66  int BN_is_prime(const BIGNUM *p,int nchecks,void (*callback)(int,int,void *),
67          BN_CTX *ctx,void *cb_arg);
68
69  int BN_set_bit(BIGNUM *a, int n);
70  int BN_clear_bit(BIGNUM *a, int n);
71  int BN_is_bit_set(const BIGNUM *a, int n);
72  int BN_mask_bits(BIGNUM *a, int n);
73  int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
74  int BN_lshift1(BIGNUM *r, BIGNUM *a);
75  int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
76  int BN_rshift1(BIGNUM *r, BIGNUM *a);
77
78  int BN_bn2bin(const BIGNUM *a, unsigned char *to);
79  BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
80  char *BN_bn2hex(const BIGNUM *a);
81  char *BN_bn2dec(const BIGNUM *a);
82  int BN_hex2bn(BIGNUM **a, const char *str);
83  int BN_dec2bn(BIGNUM **a, const char *str);
84  int BN_print(BIO *fp, const BIGNUM *a);
85  int BN_print_fp(FILE *fp, const BIGNUM *a);
86  int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
87  BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
88
89  BIGNUM *BN_mod_inverse(BIGNUM *r, BIGNUM *a, const BIGNUM *n,
90      BN_CTX *ctx);
91
92  BN_RECP_CTX *BN_RECP_CTX_new(void);
93  void BN_RECP_CTX_init(BN_RECP_CTX *recp);
94  void BN_RECP_CTX_free(BN_RECP_CTX *recp);
95  int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx);
96  int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *a, BIGNUM *b,
97         BN_RECP_CTX *recp, BN_CTX *ctx);
98
99  BN_MONT_CTX *BN_MONT_CTX_new(void);
100  void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
101  void BN_MONT_CTX_free(BN_MONT_CTX *mont);
102  int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
103  BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
104  int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
105          BN_MONT_CTX *mont, BN_CTX *ctx);
106  int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
107          BN_CTX *ctx);
108  int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
109          BN_CTX *ctx);
110
111
112 =head1 DESCRIPTION
113
114 This library performs arithmetic operations on integers of arbitrary
115 size. It was written for use in public key cryptography, such as RSA
116 and Diffie-Hellman.
117
118 It uses dynamic memory allocation for storing its data structures.
119 That means that there is no limit on the size of the numbers
120 manipulated by these functions, but return values must always be
121 checked in case a memory allocation error has occurred.
122
123 The basic object in this library is a B<BIGNUM>. It is used to hold a
124 single large integer. This type should be considered opaque and fields
125 should not be modified or accessed directly.
126
127 The creation of B<BIGNUM> objects is described in L<BN_new(3)|BN_new(3)>;
128 L<BN_add(3)|BN_add(3)> describes most of the arithmetic operations.
129 Comparison is described in L<BN_cmp(3)|BN_cmp(3)>; L<BN_zero(3)|BN_zero(3)>
130 describes certain assignments, L<BN_rand(3)|BN_rand(3)> the generation of
131 random numbers, L<BN_generate_prime(3)|BN_generate_prime(3)> deals with prime
132 numbers and L<BN_set_bit(3)|BN_set_bit(3)> with bit operations. The conversion
133 of B<BIGNUM>s to external formats is described in L<BN_bn2bin(3)|BN_bn2bin(3)>.
134
135 =head1 INTERNALS
136
137 The following description is based on the SSLeay documentation:
138
139  typedef struct bignum_st
140         {
141         int top;        /* Index of last used d. */
142         BN_ULONG *d;    /* Pointer to an array of 'BITS2' bit chunks. */
143         int max;        /* Size of the d array. */
144         int neg;
145         } BIGNUM;
146
147 The big number is stored in B<d>, a malloc()ed array of B<BN_ULONG>s,
148 least significant first. A B<BN_ULONG> can be either 16, 32 or 64 bits
149 in size, depending on the 'number of bits' specified in bn.h. 
150
151 B<max> is the size of the B<d> array that has been allocated.  B<top>
152 is the 'last' entry being used, so for a value of 4, bn.d[0]=4 and
153 bn.top=1. B<neg> is 1 if the number is negative.  When a BIGNUM is
154 '0', the B<d> field can be NULL and B<top> == 0. Various routines in
155 this library require the use of temporary B<BIGNUM> variables during
156 their execution.  Since dynamic memory allocation to create B<BIGNUM>s
157 is rather expensive when used in conjunction with repeated subroutine
158 calls, the B<BN_CTX> structure is used.  This structure contains
159 B<BN_CTX_NUM> B<BIGNUM>s.  B<BN_CTX_NUM> is the maximum number of
160 temporary B<BIGNUM>s any publicly exported function will use.
161
162  #define BN_CTX_NUM  12
163  typedef struct bignum_ctx
164         {
165         int tos;                    /* top of stack */
166         BIGNUM *bn[BN_CTX_NUM];     /* The variables */
167         } BN_CTX;
168
169 B<tos> is the index of the first unused B<BIGNUM> in the B<bn> array.
170
171 =head1 SEE ALSO
172
173 L<dh(3)|dh(3)>, L<err(3)|err(3)>, L<rand(3)|rand(3)>, L<rsa(3)|rsa(3)>,
174 L<BN_new(3)|BN_new(3)>, L<BN_CTX_new(3)|BN_CTX_new(3)>,
175 L<BN_copy(3)|BN_copy(3)>, L<BN_num_bytes(3)|BN_num_bytes(3)>,
176 L<BN_add(3)|BN_add(3)>, L<BN_add_word(3)|BN_add_word(3)>,
177 L<BN_cmp(3)|BN_cmp(3)>, L<BN_zero(3)|BN_zero(3)>, L<BN_rand(3)|BN_rand(3)>,
178 L<BN_generate_prime(3)|BN_generate_prime(3)>, L<BN_set_bit(3)|BN_set_bit(3)>,
179 L<BN_bn2bin(3)|BN_bn2bin(3)>, L<BN_mod_inverse(3)|BN_mod_inverse(3)>,
180 L<BN_mod_mul_reciprocal(3)|BN_mod_mul_reciprocal(3)>,
181 L<BN_mod_mul_montgomery(3)|BN_mod_mul_montgomery(3)> 
182
183 =cut