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