1 .\" $NetBSD: libcrypto.pl,v 1.3 2007/11/27 22:16:03 christos Exp $
3 .\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05)
6 .\" ========================================================================
7 .de Sh \" Subsection heading
15 .de Sp \" Vertical space (when we can't use .PP)
19 .de Vb \" Begin verbatim text
24 .de Ve \" End verbatim text
28 .\" Set up some character translations and predefined strings. \*(-- will
29 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
30 .\" double quote, and \*(R" will give a right double quote. \*(C+ will
31 .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
32 .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
33 .\" nothing in troff, for use with C<>.
35 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
39 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
40 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
53 .\" Escape single quotes in literal strings from groff's Unicode transform.
57 .\" If the F register is turned on, we'll generate index entries on stderr for
58 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
59 .\" entries marked with X<> in POD. Of course, you'll have to process the
60 .\" output yourself in some meaningful fashion.
63 . tm Index:\\$1\t\\n%\t"\\$2"
73 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
74 .\" Fear. Run. Save yourself. No user-serviceable parts.
75 . \" fudge factors for nroff and troff
84 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
90 . \" simple accents for nroff and troff
100 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
101 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
102 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
103 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
104 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
105 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
107 . \" troff and (daisy-wheel) nroff accents
108 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
109 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
110 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
111 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
112 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
113 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
114 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
115 .ds ae a\h'-(\w'a'u*4/10)'e
116 .ds Ae A\h'-(\w'A'u*4/10)'E
117 . \" corrections for vroff
118 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
119 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
120 . \" for low resolution devices (crt and lpr)
121 .if \n(.H>23 .if \n(.V>19 \
134 .\" ========================================================================
136 .IX Title "bn_internal 3"
137 .TH bn_internal 3 "2006-05-14" "1.1.0-dev" "OpenSSL"
138 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
139 .\" way too many mistakes in technical documents.
143 bn_mul_words, bn_mul_add_words, bn_sqr_words, bn_div_words,
144 bn_add_words, bn_sub_words, bn_mul_comba4, bn_mul_comba8,
145 bn_sqr_comba4, bn_sqr_comba8, bn_cmp_words, bn_mul_normal,
146 bn_mul_low_normal, bn_mul_recursive, bn_mul_part_recursive,
147 bn_mul_low_recursive, bn_mul_high, bn_sqr_normal, bn_sqr_recursive,
148 bn_expand, bn_wexpand, bn_expand2, bn_fix_top, bn_check_top,
149 bn_print, bn_dump, bn_set_max, bn_set_high, bn_set_low \- BIGNUM
150 library internal functions
154 .IX Header "SYNOPSIS"
156 \& #include <openssl/bn.h>
158 \& BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
159 \& BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num,
161 \& void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
162 \& BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
163 \& BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
165 \& BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
168 \& void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
169 \& void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
170 \& void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a);
171 \& void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a);
173 \& int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n);
175 \& void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
177 \& void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
178 \& void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
179 \& int dna,int dnb,BN_ULONG *tmp);
180 \& void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
181 \& int n, int tna,int tnb, BN_ULONG *tmp);
182 \& void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
183 \& int n2, BN_ULONG *tmp);
184 \& void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l,
185 \& int n2, BN_ULONG *tmp);
187 \& void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp);
188 \& void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *tmp);
190 \& void mul(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
191 \& void mul_add(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
192 \& void sqr(BN_ULONG r0, BN_ULONG r1, BN_ULONG a);
194 \& BIGNUM *bn_expand(BIGNUM *a, int bits);
195 \& BIGNUM *bn_wexpand(BIGNUM *a, int n);
196 \& BIGNUM *bn_expand2(BIGNUM *a, int n);
197 \& void bn_fix_top(BIGNUM *a);
199 \& void bn_check_top(BIGNUM *a);
200 \& void bn_print(BIGNUM *a);
201 \& void bn_dump(BN_ULONG *d, int n);
202 \& void bn_set_max(BIGNUM *a);
203 \& void bn_set_high(BIGNUM *r, BIGNUM *a, int n);
204 \& void bn_set_low(BIGNUM *r, BIGNUM *a, int n);
207 .IX Header "DESCRIPTION"
208 This page documents the internal functions used by the OpenSSL
209 \&\fB\s-1BIGNUM\s0\fR implementation. They are described here to facilitate
210 debugging and extending the library. They are \fInot\fR to be used by
212 .Sh "The \s-1BIGNUM\s0 structure"
213 .IX Subsection "The BIGNUM structure"
215 \& typedef struct bignum_st
217 \& int top; /* number of words used in d */
218 \& BN_ULONG *d; /* pointer to an array containing the integer value */
219 \& int max; /* size of the d array */
220 \& int neg; /* sign */
224 The integer value is stored in \fBd\fR, a \fImalloc()\fRed array of words (\fB\s-1BN_ULONG\s0\fR),
225 least significant word first. A \fB\s-1BN_ULONG\s0\fR can be either 16, 32 or 64 bits
226 in size, depending on the 'number of bits' (\fB\s-1BITS2\s0\fR) specified in
227 \&\f(CW\*(C`openssl/bn.h\*(C'\fR.
229 \&\fBmax\fR is the size of the \fBd\fR array that has been allocated. \fBtop\fR
230 is the number of words being used, so for a value of 4, bn.d[0]=4 and
231 bn.top=1. \fBneg\fR is 1 if the number is negative. When a \fB\s-1BIGNUM\s0\fR is
232 \&\fB0\fR, the \fBd\fR field can be \fB\s-1NULL\s0\fR and \fBtop\fR == \fB0\fR.
234 Various routines in this library require the use of temporary
235 \&\fB\s-1BIGNUM\s0\fR variables during their execution. Since dynamic memory
236 allocation to create \fB\s-1BIGNUM\s0\fRs is rather expensive when used in
237 conjunction with repeated subroutine calls, the \fB\s-1BN_CTX\s0\fR structure is
238 used. This structure contains \fB\s-1BN_CTX_NUM\s0\fR \fB\s-1BIGNUM\s0\fRs, see
239 \&\fIBN_CTX_start\fR\|(3).
240 .Sh "Low-level arithmetic operations"
241 .IX Subsection "Low-level arithmetic operations"
242 These functions are implemented in C and for several platforms in
245 bn_mul_words(\fBrp\fR, \fBap\fR, \fBnum\fR, \fBw\fR) operates on the \fBnum\fR word
246 arrays \fBrp\fR and \fBap\fR. It computes \fBap\fR * \fBw\fR, places the result
247 in \fBrp\fR, and returns the high word (carry).
249 bn_mul_add_words(\fBrp\fR, \fBap\fR, \fBnum\fR, \fBw\fR) operates on the \fBnum\fR
250 word arrays \fBrp\fR and \fBap\fR. It computes \fBap\fR * \fBw\fR + \fBrp\fR, places
251 the result in \fBrp\fR, and returns the high word (carry).
253 bn_sqr_words(\fBrp\fR, \fBap\fR, \fBn\fR) operates on the \fBnum\fR word array
254 \&\fBap\fR and the 2*\fBnum\fR word array \fBap\fR. It computes \fBap\fR * \fBap\fR
255 word-wise, and places the low and high bytes of the result in \fBrp\fR.
257 bn_div_words(\fBh\fR, \fBl\fR, \fBd\fR) divides the two word number (\fBh\fR,\fBl\fR)
258 by \fBd\fR and returns the result.
260 bn_add_words(\fBrp\fR, \fBap\fR, \fBbp\fR, \fBnum\fR) operates on the \fBnum\fR word
261 arrays \fBap\fR, \fBbp\fR and \fBrp\fR. It computes \fBap\fR + \fBbp\fR, places the
262 result in \fBrp\fR, and returns the high word (carry).
264 bn_sub_words(\fBrp\fR, \fBap\fR, \fBbp\fR, \fBnum\fR) operates on the \fBnum\fR word
265 arrays \fBap\fR, \fBbp\fR and \fBrp\fR. It computes \fBap\fR \- \fBbp\fR, places the
266 result in \fBrp\fR, and returns the carry (1 if \fBbp\fR > \fBap\fR, 0
269 bn_mul_comba4(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 4 word arrays \fBa\fR and
270 \&\fBb\fR and the 8 word array \fBr\fR. It computes \fBa\fR*\fBb\fR and places the
273 bn_mul_comba8(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 8 word arrays \fBa\fR and
274 \&\fBb\fR and the 16 word array \fBr\fR. It computes \fBa\fR*\fBb\fR and places the
277 bn_sqr_comba4(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 4 word arrays \fBa\fR and
278 \&\fBb\fR and the 8 word array \fBr\fR.
280 bn_sqr_comba8(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 8 word arrays \fBa\fR and
281 \&\fBb\fR and the 16 word array \fBr\fR.
283 The following functions are implemented in C:
285 bn_cmp_words(\fBa\fR, \fBb\fR, \fBn\fR) operates on the \fBn\fR word arrays \fBa\fR
286 and \fBb\fR. It returns 1, 0 and \-1 if \fBa\fR is greater than, equal and
289 bn_mul_normal(\fBr\fR, \fBa\fR, \fBna\fR, \fBb\fR, \fBnb\fR) operates on the \fBna\fR
290 word array \fBa\fR, the \fBnb\fR word array \fBb\fR and the \fBna\fR+\fBnb\fR word
291 array \fBr\fR. It computes \fBa\fR*\fBb\fR and places the result in \fBr\fR.
293 bn_mul_low_normal(\fBr\fR, \fBa\fR, \fBb\fR, \fBn\fR) operates on the \fBn\fR word
294 arrays \fBr\fR, \fBa\fR and \fBb\fR. It computes the \fBn\fR low words of
295 \&\fBa\fR*\fBb\fR and places the result in \fBr\fR.
297 bn_mul_recursive(\fBr\fR, \fBa\fR, \fBb\fR, \fBn2\fR, \fBdna\fR, \fBdnb\fR, \fBt\fR) operates
298 on the word arrays \fBa\fR and \fBb\fR of length \fBn2\fR+\fBdna\fR and \fBn2\fR+\fBdnb\fR
299 (\fBdna\fR and \fBdnb\fR are currently allowed to be 0 or negative) and the 2*\fBn2\fR
300 word arrays \fBr\fR and \fBt\fR. \fBn2\fR must be a power of 2. It computes
301 \&\fBa\fR*\fBb\fR and places the result in \fBr\fR.
303 bn_mul_part_recursive(\fBr\fR, \fBa\fR, \fBb\fR, \fBn\fR, \fBtna\fR, \fBtnb\fR, \fBtmp\fR)
304 operates on the word arrays \fBa\fR and \fBb\fR of length \fBn\fR+\fBtna\fR and
305 \&\fBn\fR+\fBtnb\fR and the 4*\fBn\fR word arrays \fBr\fR and \fBtmp\fR.
307 bn_mul_low_recursive(\fBr\fR, \fBa\fR, \fBb\fR, \fBn2\fR, \fBtmp\fR) operates on the
308 \&\fBn2\fR word arrays \fBr\fR and \fBtmp\fR and the \fBn2\fR/2 word arrays \fBa\fR
311 bn_mul_high(\fBr\fR, \fBa\fR, \fBb\fR, \fBl\fR, \fBn2\fR, \fBtmp\fR) operates on the
312 \&\fBn2\fR word arrays \fBr\fR, \fBa\fR, \fBb\fR and \fBl\fR (?) and the 3*\fBn2\fR word
315 \&\fIBN_mul()\fR calls \fIbn_mul_normal()\fR, or an optimized implementation if the
316 factors have the same size: \fIbn_mul_comba8()\fR is used if they are 8
317 words long, \fIbn_mul_recursive()\fR if they are larger than
318 \&\fB\s-1BN_MULL_SIZE_NORMAL\s0\fR and the size is an exact multiple of the word
319 size, and \fIbn_mul_part_recursive()\fR for others that are larger than
320 \&\fB\s-1BN_MULL_SIZE_NORMAL\s0\fR.
322 bn_sqr_normal(\fBr\fR, \fBa\fR, \fBn\fR, \fBtmp\fR) operates on the \fBn\fR word array
323 \&\fBa\fR and the 2*\fBn\fR word arrays \fBtmp\fR and \fBr\fR.
325 The implementations use the following macros which, depending on the
326 architecture, may use \*(L"long long\*(R" C operations or inline assembler.
327 They are defined in \f(CW\*(C`bn_lcl.h\*(C'\fR.
329 mul(\fBr\fR, \fBa\fR, \fBw\fR, \fBc\fR) computes \fBw\fR*\fBa\fR+\fBc\fR and places the
330 low word of the result in \fBr\fR and the high word in \fBc\fR.
332 mul_add(\fBr\fR, \fBa\fR, \fBw\fR, \fBc\fR) computes \fBw\fR*\fBa\fR+\fBr\fR+\fBc\fR and
333 places the low word of the result in \fBr\fR and the high word in \fBc\fR.
335 sqr(\fBr0\fR, \fBr1\fR, \fBa\fR) computes \fBa\fR*\fBa\fR and places the low word
336 of the result in \fBr0\fR and the high word in \fBr1\fR.
338 .IX Subsection "Size changes"
339 \&\fIbn_expand()\fR ensures that \fBb\fR has enough space for a \fBbits\fR bit
340 number. \fIbn_wexpand()\fR ensures that \fBb\fR has enough space for an
341 \&\fBn\fR word number. If the number has to be expanded, both macros
342 call \fIbn_expand2()\fR, which allocates a new \fBd\fR array and copies the
343 data. They return \fB\s-1NULL\s0\fR on error, \fBb\fR otherwise.
345 The \fIbn_fix_top()\fR macro reduces \fBa\->top\fR to point to the most
346 significant non-zero word plus one when \fBa\fR has shrunk.
348 .IX Subsection "Debugging"
349 \&\fIbn_check_top()\fR verifies that \f(CW\*(C`((a)\->top >= 0 && (a)\->top
350 <= (a)\->max)\*(C'\fR. A violation will cause the program to abort.
352 \&\fIbn_print()\fR prints \fBa\fR to stderr. \fIbn_dump()\fR prints \fBn\fR words at \fBd\fR
353 (in reverse order, i.e. most significant word first) to stderr.
355 \&\fIbn_set_max()\fR makes \fBa\fR a static number with a \fBmax\fR of its current size.
356 This is used by \fIbn_set_low()\fR and \fIbn_set_high()\fR to make \fBr\fR a read-only
357 \&\fB\s-1BIGNUM\s0\fR that contains the \fBn\fR low or high words of \fBa\fR.
359 If \fB\s-1BN_DEBUG\s0\fR is not defined, \fIbn_check_top()\fR, \fIbn_print()\fR, \fIbn_dump()\fR
360 and \fIbn_set_max()\fR are defined as empty macros.
362 .IX Header "SEE ALSO"
363 \&\fIopenssl_bn\fR\|(3)