9bae6aba537d4bce970890c5a5685f10702383e8
[dragonfly.git] / secure / lib / libcrypto / man / bn_internal.3
1 .\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.19)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 'br\}
42 .\"
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
44 .ie \n(.g .ds Aq \(aq
45 .el       .ds Aq '
46 .\"
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD.  Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
51 .ie \nF \{\
52 .    de IX
53 .    tm Index:\\$1\t\\n%\t"\\$2"
54 ..
55 .    nr % 0
56 .    rr F
57 .\}
58 .el \{\
59 .    de IX
60 ..
61 .\}
62 .\"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
65 .    \" fudge factors for nroff and troff
66 .if n \{\
67 .    ds #H 0
68 .    ds #V .8m
69 .    ds #F .3m
70 .    ds #[ \f1
71 .    ds #] \fP
72 .\}
73 .if t \{\
74 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
75 .    ds #V .6m
76 .    ds #F 0
77 .    ds #[ \&
78 .    ds #] \&
79 .\}
80 .    \" simple accents for nroff and troff
81 .if n \{\
82 .    ds ' \&
83 .    ds ` \&
84 .    ds ^ \&
85 .    ds , \&
86 .    ds ~ ~
87 .    ds /
88 .\}
89 .if t \{\
90 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
96 .\}
97 .    \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 .    \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 .    \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
112 \{\
113 .    ds : e
114 .    ds 8 ss
115 .    ds o a
116 .    ds d- d\h'-1'\(ga
117 .    ds D- D\h'-1'\(hy
118 .    ds th \o'bp'
119 .    ds Th \o'LP'
120 .    ds ae ae
121 .    ds Ae AE
122 .\}
123 .rm #[ #] #H #V #F C
124 .\" ========================================================================
125 .\"
126 .IX Title "bn_internal 3"
127 .TH bn_internal 3 "2012-04-19" "1.0.1a" "OpenSSL"
128 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
130 .if n .ad l
131 .nh
132 .SH "NAME"
133 bn_mul_words, bn_mul_add_words, bn_sqr_words, bn_div_words,
134 bn_add_words, bn_sub_words, bn_mul_comba4, bn_mul_comba8,
135 bn_sqr_comba4, bn_sqr_comba8, bn_cmp_words, bn_mul_normal,
136 bn_mul_low_normal, bn_mul_recursive, bn_mul_part_recursive,
137 bn_mul_low_recursive, bn_mul_high, bn_sqr_normal, bn_sqr_recursive,
138 bn_expand, bn_wexpand, bn_expand2, bn_fix_top, bn_check_top,
139 bn_print, bn_dump, bn_set_max, bn_set_high, bn_set_low \- BIGNUM
140 library internal functions
141 .SH "SYNOPSIS"
142 .IX Header "SYNOPSIS"
143 .Vb 1
144 \& #include <openssl/bn.h>
145 \&
146 \& BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
147 \& BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num,
148 \&   BN_ULONG w);
149 \& void     bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
150 \& BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
151 \& BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
152 \&   int num);
153 \& BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
154 \&   int num);
155 \&
156 \& void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
157 \& void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
158 \& void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a);
159 \& void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a);
160 \&
161 \& int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n);
162 \&
163 \& void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
164 \&   int nb);
165 \& void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
166 \& void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
167 \&   int dna,int dnb,BN_ULONG *tmp);
168 \& void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
169 \&   int n, int tna,int tnb, BN_ULONG *tmp);
170 \& void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
171 \&   int n2, BN_ULONG *tmp);
172 \& void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l,
173 \&   int n2, BN_ULONG *tmp);
174 \&
175 \& void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp);
176 \& void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *tmp);
177 \&
178 \& void mul(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
179 \& void mul_add(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
180 \& void sqr(BN_ULONG r0, BN_ULONG r1, BN_ULONG a);
181 \&
182 \& BIGNUM *bn_expand(BIGNUM *a, int bits);
183 \& BIGNUM *bn_wexpand(BIGNUM *a, int n);
184 \& BIGNUM *bn_expand2(BIGNUM *a, int n);
185 \& void bn_fix_top(BIGNUM *a);
186 \&
187 \& void bn_check_top(BIGNUM *a);
188 \& void bn_print(BIGNUM *a);
189 \& void bn_dump(BN_ULONG *d, int n);
190 \& void bn_set_max(BIGNUM *a);
191 \& void bn_set_high(BIGNUM *r, BIGNUM *a, int n);
192 \& void bn_set_low(BIGNUM *r, BIGNUM *a, int n);
193 .Ve
194 .SH "DESCRIPTION"
195 .IX Header "DESCRIPTION"
196 This page documents the internal functions used by the OpenSSL
197 \&\fB\s-1BIGNUM\s0\fR implementation. They are described here to facilitate
198 debugging and extending the library. They are \fInot\fR to be used by
199 applications.
200 .SS "The \s-1BIGNUM\s0 structure"
201 .IX Subsection "The BIGNUM structure"
202 .Vb 1
203 \& typedef struct bignum_st BIGNUM;
204 \&
205 \& struct bignum_st
206 \&        {
207 \&        BN_ULONG *d;    /* Pointer to an array of \*(AqBN_BITS2\*(Aq bit chunks. */
208 \&        int top;        /* Index of last used d +1. */
209 \&        /* The next are internal book keeping for bn_expand. */
210 \&        int dmax;       /* Size of the d array. */
211 \&        int neg;        /* one if the number is negative */
212 \&        int flags;
213 \&        };
214 .Ve
215 .PP
216 The integer value is stored in \fBd\fR, a \fImalloc()\fRed array of words (\fB\s-1BN_ULONG\s0\fR),
217 least significant word first. A \fB\s-1BN_ULONG\s0\fR can be either 16, 32 or 64 bits
218 in size, depending on the 'number of bits' (\fB\s-1BITS2\s0\fR) specified in
219 \&\f(CW\*(C`openssl/bn.h\*(C'\fR.
220 .PP
221 \&\fBdmax\fR is the size of the \fBd\fR array that has been allocated.  \fBtop\fR
222 is the number of words being used, so for a value of 4, bn.d[0]=4 and
223 bn.top=1.  \fBneg\fR is 1 if the number is negative.  When a \fB\s-1BIGNUM\s0\fR is
224 \&\fB0\fR, the \fBd\fR field can be \fB\s-1NULL\s0\fR and \fBtop\fR == \fB0\fR.
225 .PP
226 \&\fBflags\fR is a bit field of flags which are defined in \f(CW\*(C`openssl/bn.h\*(C'\fR. The 
227 flags begin with \fB\s-1BN_FLG_\s0\fR. The macros BN_set_flags(b,n) and 
228 BN_get_flags(b,n) exist to enable or fetch flag(s) \fBn\fR from \fB\s-1BIGNUM\s0\fR
229 structure \fBb\fR.
230 .PP
231 Various routines in this library require the use of temporary
232 \&\fB\s-1BIGNUM\s0\fR variables during their execution.  Since dynamic memory
233 allocation to create \fB\s-1BIGNUM\s0\fRs is rather expensive when used in
234 conjunction with repeated subroutine calls, the \fB\s-1BN_CTX\s0\fR structure is
235 used.  This structure contains \fB\s-1BN_CTX_NUM\s0\fR \fB\s-1BIGNUM\s0\fRs, see
236 \&\fIBN_CTX_start\fR\|(3).
237 .SS "Low-level arithmetic operations"
238 .IX Subsection "Low-level arithmetic operations"
239 These functions are implemented in C and for several platforms in
240 assembly language:
241 .PP
242 bn_mul_words(\fBrp\fR, \fBap\fR, \fBnum\fR, \fBw\fR) operates on the \fBnum\fR word
243 arrays \fBrp\fR and \fBap\fR.  It computes \fBap\fR * \fBw\fR, places the result
244 in \fBrp\fR, and returns the high word (carry).
245 .PP
246 bn_mul_add_words(\fBrp\fR, \fBap\fR, \fBnum\fR, \fBw\fR) operates on the \fBnum\fR
247 word arrays \fBrp\fR and \fBap\fR.  It computes \fBap\fR * \fBw\fR + \fBrp\fR, places
248 the result in \fBrp\fR, and returns the high word (carry).
249 .PP
250 bn_sqr_words(\fBrp\fR, \fBap\fR, \fBn\fR) operates on the \fBnum\fR word array
251 \&\fBap\fR and the 2*\fBnum\fR word array \fBap\fR.  It computes \fBap\fR * \fBap\fR
252 word-wise, and places the low and high bytes of the result in \fBrp\fR.
253 .PP
254 bn_div_words(\fBh\fR, \fBl\fR, \fBd\fR) divides the two word number (\fBh\fR,\fBl\fR)
255 by \fBd\fR and returns the result.
256 .PP
257 bn_add_words(\fBrp\fR, \fBap\fR, \fBbp\fR, \fBnum\fR) operates on the \fBnum\fR word
258 arrays \fBap\fR, \fBbp\fR and \fBrp\fR.  It computes \fBap\fR + \fBbp\fR, places the
259 result in \fBrp\fR, and returns the high word (carry).
260 .PP
261 bn_sub_words(\fBrp\fR, \fBap\fR, \fBbp\fR, \fBnum\fR) operates on the \fBnum\fR word
262 arrays \fBap\fR, \fBbp\fR and \fBrp\fR.  It computes \fBap\fR \- \fBbp\fR, places the
263 result in \fBrp\fR, and returns the carry (1 if \fBbp\fR > \fBap\fR, 0
264 otherwise).
265 .PP
266 bn_mul_comba4(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 4 word arrays \fBa\fR and
267 \&\fBb\fR and the 8 word array \fBr\fR.  It computes \fBa\fR*\fBb\fR and places the
268 result in \fBr\fR.
269 .PP
270 bn_mul_comba8(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 8 word arrays \fBa\fR and
271 \&\fBb\fR and the 16 word array \fBr\fR.  It computes \fBa\fR*\fBb\fR and places the
272 result in \fBr\fR.
273 .PP
274 bn_sqr_comba4(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 4 word arrays \fBa\fR and
275 \&\fBb\fR and the 8 word array \fBr\fR.
276 .PP
277 bn_sqr_comba8(\fBr\fR, \fBa\fR, \fBb\fR) operates on the 8 word arrays \fBa\fR and
278 \&\fBb\fR and the 16 word array \fBr\fR.
279 .PP
280 The following functions are implemented in C:
281 .PP
282 bn_cmp_words(\fBa\fR, \fBb\fR, \fBn\fR) operates on the \fBn\fR word arrays \fBa\fR
283 and \fBb\fR.  It returns 1, 0 and \-1 if \fBa\fR is greater than, equal and
284 less than \fBb\fR.
285 .PP
286 bn_mul_normal(\fBr\fR, \fBa\fR, \fBna\fR, \fBb\fR, \fBnb\fR) operates on the \fBna\fR
287 word array \fBa\fR, the \fBnb\fR word array \fBb\fR and the \fBna\fR+\fBnb\fR word
288 array \fBr\fR.  It computes \fBa\fR*\fBb\fR and places the result in \fBr\fR.
289 .PP
290 bn_mul_low_normal(\fBr\fR, \fBa\fR, \fBb\fR, \fBn\fR) operates on the \fBn\fR word
291 arrays \fBr\fR, \fBa\fR and \fBb\fR.  It computes the \fBn\fR low words of
292 \&\fBa\fR*\fBb\fR and places the result in \fBr\fR.
293 .PP
294 bn_mul_recursive(\fBr\fR, \fBa\fR, \fBb\fR, \fBn2\fR, \fBdna\fR, \fBdnb\fR, \fBt\fR) operates
295 on the word arrays \fBa\fR and \fBb\fR of length \fBn2\fR+\fBdna\fR and \fBn2\fR+\fBdnb\fR
296 (\fBdna\fR and \fBdnb\fR are currently allowed to be 0 or negative) and the 2*\fBn2\fR
297 word arrays \fBr\fR and \fBt\fR.  \fBn2\fR must be a power of 2.  It computes
298 \&\fBa\fR*\fBb\fR and places the result in \fBr\fR.
299 .PP
300 bn_mul_part_recursive(\fBr\fR, \fBa\fR, \fBb\fR, \fBn\fR, \fBtna\fR, \fBtnb\fR, \fBtmp\fR)
301 operates on the word arrays \fBa\fR and \fBb\fR of length \fBn\fR+\fBtna\fR and
302 \&\fBn\fR+\fBtnb\fR and the 4*\fBn\fR word arrays \fBr\fR and \fBtmp\fR.
303 .PP
304 bn_mul_low_recursive(\fBr\fR, \fBa\fR, \fBb\fR, \fBn2\fR, \fBtmp\fR) operates on the
305 \&\fBn2\fR word arrays \fBr\fR and \fBtmp\fR and the \fBn2\fR/2 word arrays \fBa\fR
306 and \fBb\fR.
307 .PP
308 bn_mul_high(\fBr\fR, \fBa\fR, \fBb\fR, \fBl\fR, \fBn2\fR, \fBtmp\fR) operates on the
309 \&\fBn2\fR word arrays \fBr\fR, \fBa\fR, \fBb\fR and \fBl\fR (?) and the 3*\fBn2\fR word
310 array \fBtmp\fR.
311 .PP
312 \&\fIBN_mul()\fR calls \fIbn_mul_normal()\fR, or an optimized implementation if the
313 factors have the same size: \fIbn_mul_comba8()\fR is used if they are 8
314 words long, \fIbn_mul_recursive()\fR if they are larger than
315 \&\fB\s-1BN_MULL_SIZE_NORMAL\s0\fR and the size is an exact multiple of the word
316 size, and \fIbn_mul_part_recursive()\fR for others that are larger than
317 \&\fB\s-1BN_MULL_SIZE_NORMAL\s0\fR.
318 .PP
319 bn_sqr_normal(\fBr\fR, \fBa\fR, \fBn\fR, \fBtmp\fR) operates on the \fBn\fR word array
320 \&\fBa\fR and the 2*\fBn\fR word arrays \fBtmp\fR and \fBr\fR.
321 .PP
322 The implementations use the following macros which, depending on the
323 architecture, may use \*(L"long long\*(R" C operations or inline assembler.
324 They are defined in \f(CW\*(C`bn_lcl.h\*(C'\fR.
325 .PP
326 mul(\fBr\fR, \fBa\fR, \fBw\fR, \fBc\fR) computes \fBw\fR*\fBa\fR+\fBc\fR and places the
327 low word of the result in \fBr\fR and the high word in \fBc\fR.
328 .PP
329 mul_add(\fBr\fR, \fBa\fR, \fBw\fR, \fBc\fR) computes \fBw\fR*\fBa\fR+\fBr\fR+\fBc\fR and
330 places the low word of the result in \fBr\fR and the high word in \fBc\fR.
331 .PP
332 sqr(\fBr0\fR, \fBr1\fR, \fBa\fR) computes \fBa\fR*\fBa\fR and places the low word
333 of the result in \fBr0\fR and the high word in \fBr1\fR.
334 .SS "Size changes"
335 .IX Subsection "Size changes"
336 \&\fIbn_expand()\fR ensures that \fBb\fR has enough space for a \fBbits\fR bit
337 number.  \fIbn_wexpand()\fR ensures that \fBb\fR has enough space for an
338 \&\fBn\fR word number.  If the number has to be expanded, both macros
339 call \fIbn_expand2()\fR, which allocates a new \fBd\fR array and copies the
340 data.  They return \fB\s-1NULL\s0\fR on error, \fBb\fR otherwise.
341 .PP
342 The \fIbn_fix_top()\fR macro reduces \fBa\->top\fR to point to the most
343 significant non-zero word plus one when \fBa\fR has shrunk.
344 .SS "Debugging"
345 .IX Subsection "Debugging"
346 \&\fIbn_check_top()\fR verifies that \f(CW\*(C`((a)\->top >= 0 && (a)\->top
347 <= (a)\->dmax)\*(C'\fR.  A violation will cause the program to abort.
348 .PP
349 \&\fIbn_print()\fR prints \fBa\fR to stderr. \fIbn_dump()\fR prints \fBn\fR words at \fBd\fR
350 (in reverse order, i.e. most significant word first) to stderr.
351 .PP
352 \&\fIbn_set_max()\fR makes \fBa\fR a static number with a \fBdmax\fR of its current size.
353 This is used by \fIbn_set_low()\fR and \fIbn_set_high()\fR to make \fBr\fR a read-only
354 \&\fB\s-1BIGNUM\s0\fR that contains the \fBn\fR low or high words of \fBa\fR.
355 .PP
356 If \fB\s-1BN_DEBUG\s0\fR is not defined, \fIbn_check_top()\fR, \fIbn_print()\fR, \fIbn_dump()\fR
357 and \fIbn_set_max()\fR are defined as empty macros.
358 .SH "SEE ALSO"
359 .IX Header "SEE ALSO"
360 \&\fIbn\fR\|(3)