1bn_internal(3) OpenSSL bn_internal(3)
2
3
4
6 bn_mul_words, bn_mul_add_words, bn_sqr_words, bn_div_words,
7 bn_add_words, bn_sub_words, bn_mul_comba4, bn_mul_comba8,
8 bn_sqr_comba4, bn_sqr_comba8, bn_cmp_words, bn_mul_normal,
9 bn_mul_low_normal, bn_mul_recursive, bn_mul_part_recursive,
10 bn_mul_low_recursive, bn_mul_high, bn_sqr_normal, bn_sqr_recursive,
11 bn_expand, bn_wexpand, bn_expand2, bn_fix_top, bn_check_top, bn_print,
12 bn_dump, bn_set_max, bn_set_high, bn_set_low - BIGNUM library internal
13 functions
14
16 BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
17 BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num,
18 BN_ULONG w);
19 void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
20 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
21 BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
22 int num);
23 BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
24 int num);
25
26 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
27 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
28 void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a);
29 void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a);
30
31 int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n);
32
33 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
34 int nb);
35 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
36 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
37 int dna,int dnb,BN_ULONG *tmp);
38 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
39 int n, int tna,int tnb, BN_ULONG *tmp);
40 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
41 int n2, BN_ULONG *tmp);
42 void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l,
43 int n2, BN_ULONG *tmp);
44
45 void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp);
46 void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *tmp);
47
48 void mul(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
49 void mul_add(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
50 void sqr(BN_ULONG r0, BN_ULONG r1, BN_ULONG a);
51
52 BIGNUM *bn_expand(BIGNUM *a, int bits);
53 BIGNUM *bn_wexpand(BIGNUM *a, int n);
54 BIGNUM *bn_expand2(BIGNUM *a, int n);
55 void bn_fix_top(BIGNUM *a);
56
57 void bn_check_top(BIGNUM *a);
58 void bn_print(BIGNUM *a);
59 void bn_dump(BN_ULONG *d, int n);
60 void bn_set_max(BIGNUM *a);
61 void bn_set_high(BIGNUM *r, BIGNUM *a, int n);
62 void bn_set_low(BIGNUM *r, BIGNUM *a, int n);
63
65 This page documents the internal functions used by the OpenSSL BIGNUM
66 implementation. They are described here to facilitate debugging and
67 extending the library. They are not to be used by applications.
68
69 The BIGNUM structure
70
71 typedef struct bignum_st
72 {
73 int top; /* number of words used in d */
74 BN_ULONG *d; /* pointer to an array containing the integer value */
75 int max; /* size of the d array */
76 int neg; /* sign */
77 } BIGNUM;
78
79 The integer value is stored in d, a malloc()ed array of words
80 (BN_ULONG), least significant word first. A BN_ULONG can be either 16,
81 32 or 64 bits in size, depending on the 'number of bits' (BITS2) speci‐
82 fied in "openssl/bn.h".
83
84 max is the size of the d array that has been allocated. top is the
85 number of words being used, so for a value of 4, bn.d[0]=4 and
86 bn.top=1. neg is 1 if the number is negative. When a BIGNUM is 0, the
87 d field can be NULL and top == 0.
88
89 Various routines in this library require the use of temporary BIGNUM
90 variables during their execution. Since dynamic memory allocation to
91 create BIGNUMs is rather expensive when used in conjunction with
92 repeated subroutine calls, the BN_CTX structure is used. This struc‐
93 ture contains BN_CTX_NUM BIGNUMs, see BN_CTX_start(3).
94
95 Low-level arithmetic operations
96
97 These functions are implemented in C and for several platforms in
98 assembly language:
99
100 bn_mul_words(rp, ap, num, w) operates on the num word arrays rp and ap.
101 It computes ap * w, places the result in rp, and returns the high word
102 (carry).
103
104 bn_mul_add_words(rp, ap, num, w) operates on the num word arrays rp and
105 ap. It computes ap * w + rp, places the result in rp, and returns the
106 high word (carry).
107
108 bn_sqr_words(rp, ap, n) operates on the num word array ap and the 2*num
109 word array ap. It computes ap * ap word-wise, and places the low and
110 high bytes of the result in rp.
111
112 bn_div_words(h, l, d) divides the two word number (h,l) by d and
113 returns the result.
114
115 bn_add_words(rp, ap, bp, num) operates on the num word arrays ap, bp
116 and rp. It computes ap + bp, places the result in rp, and returns the
117 high word (carry).
118
119 bn_sub_words(rp, ap, bp, num) operates on the num word arrays ap, bp
120 and rp. It computes ap - bp, places the result in rp, and returns the
121 carry (1 if bp > ap, 0 otherwise).
122
123 bn_mul_comba4(r, a, b) operates on the 4 word arrays a and b and the 8
124 word array r. It computes a*b and places the result in r.
125
126 bn_mul_comba8(r, a, b) operates on the 8 word arrays a and b and the 16
127 word array r. It computes a*b and places the result in r.
128
129 bn_sqr_comba4(r, a, b) operates on the 4 word arrays a and b and the 8
130 word array r.
131
132 bn_sqr_comba8(r, a, b) operates on the 8 word arrays a and b and the 16
133 word array r.
134
135 The following functions are implemented in C:
136
137 bn_cmp_words(a, b, n) operates on the n word arrays a and b. It
138 returns 1, 0 and -1 if a is greater than, equal and less than b.
139
140 bn_mul_normal(r, a, na, b, nb) operates on the na word array a, the nb
141 word array b and the na+nb word array r. It computes a*b and places
142 the result in r.
143
144 bn_mul_low_normal(r, a, b, n) operates on the n word arrays r, a and b.
145 It computes the n low words of a*b and places the result in r.
146
147 bn_mul_recursive(r, a, b, n2, dna, dnb, t) operates on the word arrays
148 a and b of length n2+dna and n2+dnb (dna and dnb are currently allowed
149 to be 0 or negative) and the 2*n2 word arrays r and t. n2 must be a
150 power of 2. It computes a*b and places the result in r.
151
152 bn_mul_part_recursive(r, a, b, n, tna, tnb, tmp) operates on the word
153 arrays a and b of length n+tna and n+tnb and the 4*n word arrays r and
154 tmp.
155
156 bn_mul_low_recursive(r, a, b, n2, tmp) operates on the n2 word arrays r
157 and tmp and the n2/2 word arrays a and b.
158
159 bn_mul_high(r, a, b, l, n2, tmp) operates on the n2 word arrays r, a, b
160 and l (?) and the 3*n2 word array tmp.
161
162 BN_mul() calls bn_mul_normal(), or an optimized implementation if the
163 factors have the same size: bn_mul_comba8() is used if they are 8 words
164 long, bn_mul_recursive() if they are larger than BN_MULL_SIZE_NORMAL
165 and the size is an exact multiple of the word size, and
166 bn_mul_part_recursive() for others that are larger than
167 BN_MULL_SIZE_NORMAL.
168
169 bn_sqr_normal(r, a, n, tmp) operates on the n word array a and the 2*n
170 word arrays tmp and r.
171
172 The implementations use the following macros which, depending on the
173 architecture, may use "long long" C operations or inline assembler.
174 They are defined in "bn_lcl.h".
175
176 mul(r, a, w, c) computes w*a+c and places the low word of the result in
177 r and the high word in c.
178
179 mul_add(r, a, w, c) computes w*a+r+c and places the low word of the
180 result in r and the high word in c.
181
182 sqr(r0, r1, a) computes a*a and places the low word of the result in r0
183 and the high word in r1.
184
185 Size changes
186
187 bn_expand() ensures that b has enough space for a bits bit number.
188 bn_wexpand() ensures that b has enough space for an n word number. If
189 the number has to be expanded, both macros call bn_expand2(), which
190 allocates a new d array and copies the data. They return NULL on
191 error, b otherwise.
192
193 The bn_fix_top() macro reduces a->top to point to the most significant
194 non-zero word plus one when a has shrunk.
195
196 Debugging
197
198 bn_check_top() verifies that "((a)->top >= 0 && (a)->top <= (a)->max)".
199 A violation will cause the program to abort.
200
201 bn_print() prints a to stderr. bn_dump() prints n words at d (in
202 reverse order, i.e. most significant word first) to stderr.
203
204 bn_set_max() makes a a static number with a max of its current size.
205 This is used by bn_set_low() and bn_set_high() to make r a read-only
206 BIGNUM that contains the n low or high words of a.
207
208 If BN_DEBUG is not defined, bn_check_top(), bn_print(), bn_dump() and
209 bn_set_max() are defined as empty macros.
210
212 bn(3)
213
214
215
2160.9.8b 2003-11-06 bn_internal(3)