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