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