xref: /onnv-gate/usr/src/common/openssl/doc/crypto/bn_internal.pod (revision 2175:b0b2f052a486)
1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948bn_mul_words, bn_mul_add_words, bn_sqr_words, bn_div_words,
6*2175Sjp161948bn_add_words, bn_sub_words, bn_mul_comba4, bn_mul_comba8,
7*2175Sjp161948bn_sqr_comba4, bn_sqr_comba8, bn_cmp_words, bn_mul_normal,
8*2175Sjp161948bn_mul_low_normal, bn_mul_recursive, bn_mul_part_recursive,
9*2175Sjp161948bn_mul_low_recursive, bn_mul_high, bn_sqr_normal, bn_sqr_recursive,
10*2175Sjp161948bn_expand, bn_wexpand, bn_expand2, bn_fix_top, bn_check_top,
11*2175Sjp161948bn_print, bn_dump, bn_set_max, bn_set_high, bn_set_low - BIGNUM
12*2175Sjp161948library internal functions
13*2175Sjp161948
14*2175Sjp161948=head1 SYNOPSIS
15*2175Sjp161948
16*2175Sjp161948 BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
17*2175Sjp161948 BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num,
18*2175Sjp161948   BN_ULONG w);
19*2175Sjp161948 void     bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
20*2175Sjp161948 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
21*2175Sjp161948 BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
22*2175Sjp161948   int num);
23*2175Sjp161948 BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
24*2175Sjp161948   int num);
25*2175Sjp161948
26*2175Sjp161948 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
27*2175Sjp161948 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
28*2175Sjp161948 void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a);
29*2175Sjp161948 void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a);
30*2175Sjp161948
31*2175Sjp161948 int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n);
32*2175Sjp161948
33*2175Sjp161948 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
34*2175Sjp161948   int nb);
35*2175Sjp161948 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
36*2175Sjp161948 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
37*2175Sjp161948   int dna,int dnb,BN_ULONG *tmp);
38*2175Sjp161948 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
39*2175Sjp161948   int n, int tna,int tnb, BN_ULONG *tmp);
40*2175Sjp161948 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
41*2175Sjp161948   int n2, BN_ULONG *tmp);
42*2175Sjp161948 void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l,
43*2175Sjp161948   int n2, BN_ULONG *tmp);
44*2175Sjp161948
45*2175Sjp161948 void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp);
46*2175Sjp161948 void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *tmp);
47*2175Sjp161948
48*2175Sjp161948 void mul(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
49*2175Sjp161948 void mul_add(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
50*2175Sjp161948 void sqr(BN_ULONG r0, BN_ULONG r1, BN_ULONG a);
51*2175Sjp161948
52*2175Sjp161948 BIGNUM *bn_expand(BIGNUM *a, int bits);
53*2175Sjp161948 BIGNUM *bn_wexpand(BIGNUM *a, int n);
54*2175Sjp161948 BIGNUM *bn_expand2(BIGNUM *a, int n);
55*2175Sjp161948 void bn_fix_top(BIGNUM *a);
56*2175Sjp161948
57*2175Sjp161948 void bn_check_top(BIGNUM *a);
58*2175Sjp161948 void bn_print(BIGNUM *a);
59*2175Sjp161948 void bn_dump(BN_ULONG *d, int n);
60*2175Sjp161948 void bn_set_max(BIGNUM *a);
61*2175Sjp161948 void bn_set_high(BIGNUM *r, BIGNUM *a, int n);
62*2175Sjp161948 void bn_set_low(BIGNUM *r, BIGNUM *a, int n);
63*2175Sjp161948
64*2175Sjp161948=head1 DESCRIPTION
65*2175Sjp161948
66*2175Sjp161948This page documents the internal functions used by the OpenSSL
67*2175Sjp161948B<BIGNUM> implementation. They are described here to facilitate
68*2175Sjp161948debugging and extending the library. They are I<not> to be used by
69*2175Sjp161948applications.
70*2175Sjp161948
71*2175Sjp161948=head2 The BIGNUM structure
72*2175Sjp161948
73*2175Sjp161948 typedef struct bignum_st
74*2175Sjp161948        {
75*2175Sjp161948        int top;      /* number of words used in d */
76*2175Sjp161948        BN_ULONG *d;  /* pointer to an array containing the integer value */
77*2175Sjp161948        int max;      /* size of the d array */
78*2175Sjp161948        int neg;      /* sign */
79*2175Sjp161948        } BIGNUM;
80*2175Sjp161948
81*2175Sjp161948The integer value is stored in B<d>, a malloc()ed array of words (B<BN_ULONG>),
82*2175Sjp161948least significant word first. A B<BN_ULONG> can be either 16, 32 or 64 bits
83*2175Sjp161948in size, depending on the 'number of bits' (B<BITS2>) specified in
84*2175Sjp161948C<openssl/bn.h>.
85*2175Sjp161948
86*2175Sjp161948B<max> is the size of the B<d> array that has been allocated.  B<top>
87*2175Sjp161948is the number of words being used, so for a value of 4, bn.d[0]=4 and
88*2175Sjp161948bn.top=1.  B<neg> is 1 if the number is negative.  When a B<BIGNUM> is
89*2175Sjp161948B<0>, the B<d> field can be B<NULL> and B<top> == B<0>.
90*2175Sjp161948
91*2175Sjp161948Various routines in this library require the use of temporary
92*2175Sjp161948B<BIGNUM> variables during their execution.  Since dynamic memory
93*2175Sjp161948allocation to create B<BIGNUM>s is rather expensive when used in
94*2175Sjp161948conjunction with repeated subroutine calls, the B<BN_CTX> structure is
95*2175Sjp161948used.  This structure contains B<BN_CTX_NUM> B<BIGNUM>s, see
96*2175Sjp161948L<BN_CTX_start(3)|BN_CTX_start(3)>.
97*2175Sjp161948
98*2175Sjp161948=head2 Low-level arithmetic operations
99*2175Sjp161948
100*2175Sjp161948These functions are implemented in C and for several platforms in
101*2175Sjp161948assembly language:
102*2175Sjp161948
103*2175Sjp161948bn_mul_words(B<rp>, B<ap>, B<num>, B<w>) operates on the B<num> word
104*2175Sjp161948arrays B<rp> and B<ap>.  It computes B<ap> * B<w>, places the result
105*2175Sjp161948in B<rp>, and returns the high word (carry).
106*2175Sjp161948
107*2175Sjp161948bn_mul_add_words(B<rp>, B<ap>, B<num>, B<w>) operates on the B<num>
108*2175Sjp161948word arrays B<rp> and B<ap>.  It computes B<ap> * B<w> + B<rp>, places
109*2175Sjp161948the result in B<rp>, and returns the high word (carry).
110*2175Sjp161948
111*2175Sjp161948bn_sqr_words(B<rp>, B<ap>, B<n>) operates on the B<num> word array
112*2175Sjp161948B<ap> and the 2*B<num> word array B<ap>.  It computes B<ap> * B<ap>
113*2175Sjp161948word-wise, and places the low and high bytes of the result in B<rp>.
114*2175Sjp161948
115*2175Sjp161948bn_div_words(B<h>, B<l>, B<d>) divides the two word number (B<h>,B<l>)
116*2175Sjp161948by B<d> and returns the result.
117*2175Sjp161948
118*2175Sjp161948bn_add_words(B<rp>, B<ap>, B<bp>, B<num>) operates on the B<num> word
119*2175Sjp161948arrays B<ap>, B<bp> and B<rp>.  It computes B<ap> + B<bp>, places the
120*2175Sjp161948result in B<rp>, and returns the high word (carry).
121*2175Sjp161948
122*2175Sjp161948bn_sub_words(B<rp>, B<ap>, B<bp>, B<num>) operates on the B<num> word
123*2175Sjp161948arrays B<ap>, B<bp> and B<rp>.  It computes B<ap> - B<bp>, places the
124*2175Sjp161948result in B<rp>, and returns the carry (1 if B<bp> E<gt> B<ap>, 0
125*2175Sjp161948otherwise).
126*2175Sjp161948
127*2175Sjp161948bn_mul_comba4(B<r>, B<a>, B<b>) operates on the 4 word arrays B<a> and
128*2175Sjp161948B<b> and the 8 word array B<r>.  It computes B<a>*B<b> and places the
129*2175Sjp161948result in B<r>.
130*2175Sjp161948
131*2175Sjp161948bn_mul_comba8(B<r>, B<a>, B<b>) operates on the 8 word arrays B<a> and
132*2175Sjp161948B<b> and the 16 word array B<r>.  It computes B<a>*B<b> and places the
133*2175Sjp161948result in B<r>.
134*2175Sjp161948
135*2175Sjp161948bn_sqr_comba4(B<r>, B<a>, B<b>) operates on the 4 word arrays B<a> and
136*2175Sjp161948B<b> and the 8 word array B<r>.
137*2175Sjp161948
138*2175Sjp161948bn_sqr_comba8(B<r>, B<a>, B<b>) operates on the 8 word arrays B<a> and
139*2175Sjp161948B<b> and the 16 word array B<r>.
140*2175Sjp161948
141*2175Sjp161948The following functions are implemented in C:
142*2175Sjp161948
143*2175Sjp161948bn_cmp_words(B<a>, B<b>, B<n>) operates on the B<n> word arrays B<a>
144*2175Sjp161948and B<b>.  It returns 1, 0 and -1 if B<a> is greater than, equal and
145*2175Sjp161948less than B<b>.
146*2175Sjp161948
147*2175Sjp161948bn_mul_normal(B<r>, B<a>, B<na>, B<b>, B<nb>) operates on the B<na>
148*2175Sjp161948word array B<a>, the B<nb> word array B<b> and the B<na>+B<nb> word
149*2175Sjp161948array B<r>.  It computes B<a>*B<b> and places the result in B<r>.
150*2175Sjp161948
151*2175Sjp161948bn_mul_low_normal(B<r>, B<a>, B<b>, B<n>) operates on the B<n> word
152*2175Sjp161948arrays B<r>, B<a> and B<b>.  It computes the B<n> low words of
153*2175Sjp161948B<a>*B<b> and places the result in B<r>.
154*2175Sjp161948
155*2175Sjp161948bn_mul_recursive(B<r>, B<a>, B<b>, B<n2>, B<dna>, B<dnb>, B<t>) operates
156*2175Sjp161948on the word arrays B<a> and B<b> of length B<n2>+B<dna> and B<n2>+B<dnb>
157*2175Sjp161948(B<dna> and B<dnb> are currently allowed to be 0 or negative) and the 2*B<n2>
158*2175Sjp161948word arrays B<r> and B<t>.  B<n2> must be a power of 2.  It computes
159*2175Sjp161948B<a>*B<b> and places the result in B<r>.
160*2175Sjp161948
161*2175Sjp161948bn_mul_part_recursive(B<r>, B<a>, B<b>, B<n>, B<tna>, B<tnb>, B<tmp>)
162*2175Sjp161948operates on the word arrays B<a> and B<b> of length B<n>+B<tna> and
163*2175Sjp161948B<n>+B<tnb> and the 4*B<n> word arrays B<r> and B<tmp>.
164*2175Sjp161948
165*2175Sjp161948bn_mul_low_recursive(B<r>, B<a>, B<b>, B<n2>, B<tmp>) operates on the
166*2175Sjp161948B<n2> word arrays B<r> and B<tmp> and the B<n2>/2 word arrays B<a>
167*2175Sjp161948and B<b>.
168*2175Sjp161948
169*2175Sjp161948bn_mul_high(B<r>, B<a>, B<b>, B<l>, B<n2>, B<tmp>) operates on the
170*2175Sjp161948B<n2> word arrays B<r>, B<a>, B<b> and B<l> (?) and the 3*B<n2> word
171*2175Sjp161948array B<tmp>.
172*2175Sjp161948
173*2175Sjp161948BN_mul() calls bn_mul_normal(), or an optimized implementation if the
174*2175Sjp161948factors have the same size: bn_mul_comba8() is used if they are 8
175*2175Sjp161948words long, bn_mul_recursive() if they are larger than
176*2175Sjp161948B<BN_MULL_SIZE_NORMAL> and the size is an exact multiple of the word
177*2175Sjp161948size, and bn_mul_part_recursive() for others that are larger than
178*2175Sjp161948B<BN_MULL_SIZE_NORMAL>.
179*2175Sjp161948
180*2175Sjp161948bn_sqr_normal(B<r>, B<a>, B<n>, B<tmp>) operates on the B<n> word array
181*2175Sjp161948B<a> and the 2*B<n> word arrays B<tmp> and B<r>.
182*2175Sjp161948
183*2175Sjp161948The implementations use the following macros which, depending on the
184*2175Sjp161948architecture, may use "long long" C operations or inline assembler.
185*2175Sjp161948They are defined in C<bn_lcl.h>.
186*2175Sjp161948
187*2175Sjp161948mul(B<r>, B<a>, B<w>, B<c>) computes B<w>*B<a>+B<c> and places the
188*2175Sjp161948low word of the result in B<r> and the high word in B<c>.
189*2175Sjp161948
190*2175Sjp161948mul_add(B<r>, B<a>, B<w>, B<c>) computes B<w>*B<a>+B<r>+B<c> and
191*2175Sjp161948places the low word of the result in B<r> and the high word in B<c>.
192*2175Sjp161948
193*2175Sjp161948sqr(B<r0>, B<r1>, B<a>) computes B<a>*B<a> and places the low word
194*2175Sjp161948of the result in B<r0> and the high word in B<r1>.
195*2175Sjp161948
196*2175Sjp161948=head2 Size changes
197*2175Sjp161948
198*2175Sjp161948bn_expand() ensures that B<b> has enough space for a B<bits> bit
199*2175Sjp161948number.  bn_wexpand() ensures that B<b> has enough space for an
200*2175Sjp161948B<n> word number.  If the number has to be expanded, both macros
201*2175Sjp161948call bn_expand2(), which allocates a new B<d> array and copies the
202*2175Sjp161948data.  They return B<NULL> on error, B<b> otherwise.
203*2175Sjp161948
204*2175Sjp161948The bn_fix_top() macro reduces B<a-E<gt>top> to point to the most
205*2175Sjp161948significant non-zero word plus one when B<a> has shrunk.
206*2175Sjp161948
207*2175Sjp161948=head2 Debugging
208*2175Sjp161948
209*2175Sjp161948bn_check_top() verifies that C<((a)-E<gt>top E<gt>= 0 && (a)-E<gt>top
210*2175Sjp161948E<lt>= (a)-E<gt>max)>.  A violation will cause the program to abort.
211*2175Sjp161948
212*2175Sjp161948bn_print() prints B<a> to stderr. bn_dump() prints B<n> words at B<d>
213*2175Sjp161948(in reverse order, i.e. most significant word first) to stderr.
214*2175Sjp161948
215*2175Sjp161948bn_set_max() makes B<a> a static number with a B<max> of its current size.
216*2175Sjp161948This is used by bn_set_low() and bn_set_high() to make B<r> a read-only
217*2175Sjp161948B<BIGNUM> that contains the B<n> low or high words of B<a>.
218*2175Sjp161948
219*2175Sjp161948If B<BN_DEBUG> is not defined, bn_check_top(), bn_print(), bn_dump()
220*2175Sjp161948and bn_set_max() are defined as empty macros.
221*2175Sjp161948
222*2175Sjp161948=head1 SEE ALSO
223*2175Sjp161948
224*2175Sjp161948L<bn(3)|bn(3)>
225*2175Sjp161948
226*2175Sjp161948=cut
227