bn_internal.pod 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. =pod
  2. =head1 NAME
  3. bn_mul_words, bn_mul_add_words, bn_sqr_words, bn_div_words,
  4. bn_add_words, bn_sub_words, bn_mul_comba4, bn_mul_comba8,
  5. bn_sqr_comba4, bn_sqr_comba8, bn_cmp_words, bn_mul_normal,
  6. bn_mul_low_normal, bn_mul_recursive, bn_mul_part_recursive,
  7. bn_mul_low_recursive, bn_mul_high, bn_sqr_normal, bn_sqr_recursive,
  8. bn_expand, bn_wexpand, bn_expand2, bn_fix_top, bn_check_top,
  9. bn_print, bn_dump, bn_set_max, bn_set_high, bn_set_low - BIGNUM
  10. library internal functions
  11. =head1 SYNOPSIS
  12. #include <openssl/bn.h>
  13. BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
  14. BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num,
  15. BN_ULONG w);
  16. void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
  17. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
  18. BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
  19. int num);
  20. BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
  21. int num);
  22. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
  23. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
  24. void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a);
  25. void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a);
  26. int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n);
  27. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
  28. int nb);
  29. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
  30. void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  31. int dna,int dnb,BN_ULONG *tmp);
  32. void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
  33. int n, int tna,int tnb, BN_ULONG *tmp);
  34. void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
  35. int n2, BN_ULONG *tmp);
  36. void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l,
  37. int n2, BN_ULONG *tmp);
  38. void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp);
  39. void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *tmp);
  40. void mul(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
  41. void mul_add(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
  42. void sqr(BN_ULONG r0, BN_ULONG r1, BN_ULONG a);
  43. BIGNUM *bn_expand(BIGNUM *a, int bits);
  44. BIGNUM *bn_wexpand(BIGNUM *a, int n);
  45. BIGNUM *bn_expand2(BIGNUM *a, int n);
  46. void bn_fix_top(BIGNUM *a);
  47. void bn_check_top(BIGNUM *a);
  48. void bn_print(BIGNUM *a);
  49. void bn_dump(BN_ULONG *d, int n);
  50. void bn_set_max(BIGNUM *a);
  51. void bn_set_high(BIGNUM *r, BIGNUM *a, int n);
  52. void bn_set_low(BIGNUM *r, BIGNUM *a, int n);
  53. =head1 DESCRIPTION
  54. This page documents the internal functions used by the OpenSSL
  55. B<BIGNUM> implementation. They are described here to facilitate
  56. debugging and extending the library. They are I<not> to be used by
  57. applications.
  58. =head2 The BIGNUM structure
  59. typedef struct bignum_st BIGNUM;
  60. struct bignum_st
  61. {
  62. BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
  63. int top; /* Index of last used d +1. */
  64. /* The next are internal book keeping for bn_expand. */
  65. int dmax; /* Size of the d array. */
  66. int neg; /* one if the number is negative */
  67. int flags;
  68. };
  69. The integer value is stored in B<d>, a malloc()ed array of words (B<BN_ULONG>),
  70. least significant word first. A B<BN_ULONG> can be either 16, 32 or 64 bits
  71. in size, depending on the 'number of bits' (B<BITS2>) specified in
  72. C<openssl/bn.h>.
  73. B<dmax> is the size of the B<d> array that has been allocated. B<top>
  74. is the number of words being used, so for a value of 4, bn.d[0]=4 and
  75. bn.top=1. B<neg> is 1 if the number is negative. When a B<BIGNUM> is
  76. B<0>, the B<d> field can be B<NULL> and B<top> == B<0>.
  77. B<flags> is a bit field of flags which are defined in C<openssl/bn.h>. The
  78. flags begin with B<BN_FLG_>. The macros BN_set_flags(b,n) and
  79. BN_get_flags(b,n) exist to enable or fetch flag(s) B<n> from B<BIGNUM>
  80. structure B<b>.
  81. Various routines in this library require the use of temporary
  82. B<BIGNUM> variables during their execution. Since dynamic memory
  83. allocation to create B<BIGNUM>s is rather expensive when used in
  84. conjunction with repeated subroutine calls, the B<BN_CTX> structure is
  85. used. This structure contains B<BN_CTX_NUM> B<BIGNUM>s, see
  86. L<BN_CTX_start(3)|BN_CTX_start(3)>.
  87. =head2 Low-level arithmetic operations
  88. These functions are implemented in C and for several platforms in
  89. assembly language:
  90. bn_mul_words(B<rp>, B<ap>, B<num>, B<w>) operates on the B<num> word
  91. arrays B<rp> and B<ap>. It computes B<ap> * B<w>, places the result
  92. in B<rp>, and returns the high word (carry).
  93. bn_mul_add_words(B<rp>, B<ap>, B<num>, B<w>) operates on the B<num>
  94. word arrays B<rp> and B<ap>. It computes B<ap> * B<w> + B<rp>, places
  95. the result in B<rp>, and returns the high word (carry).
  96. bn_sqr_words(B<rp>, B<ap>, B<n>) operates on the B<num> word array
  97. B<ap> and the 2*B<num> word array B<ap>. It computes B<ap> * B<ap>
  98. word-wise, and places the low and high bytes of the result in B<rp>.
  99. bn_div_words(B<h>, B<l>, B<d>) divides the two word number (B<h>,B<l>)
  100. by B<d> and returns the result.
  101. bn_add_words(B<rp>, B<ap>, B<bp>, B<num>) operates on the B<num> word
  102. arrays B<ap>, B<bp> and B<rp>. It computes B<ap> + B<bp>, places the
  103. result in B<rp>, and returns the high word (carry).
  104. bn_sub_words(B<rp>, B<ap>, B<bp>, B<num>) operates on the B<num> word
  105. arrays B<ap>, B<bp> and B<rp>. It computes B<ap> - B<bp>, places the
  106. result in B<rp>, and returns the carry (1 if B<bp> E<gt> B<ap>, 0
  107. otherwise).
  108. bn_mul_comba4(B<r>, B<a>, B<b>) operates on the 4 word arrays B<a> and
  109. B<b> and the 8 word array B<r>. It computes B<a>*B<b> and places the
  110. result in B<r>.
  111. bn_mul_comba8(B<r>, B<a>, B<b>) operates on the 8 word arrays B<a> and
  112. B<b> and the 16 word array B<r>. It computes B<a>*B<b> and places the
  113. result in B<r>.
  114. bn_sqr_comba4(B<r>, B<a>, B<b>) operates on the 4 word arrays B<a> and
  115. B<b> and the 8 word array B<r>.
  116. bn_sqr_comba8(B<r>, B<a>, B<b>) operates on the 8 word arrays B<a> and
  117. B<b> and the 16 word array B<r>.
  118. The following functions are implemented in C:
  119. bn_cmp_words(B<a>, B<b>, B<n>) operates on the B<n> word arrays B<a>
  120. and B<b>. It returns 1, 0 and -1 if B<a> is greater than, equal and
  121. less than B<b>.
  122. bn_mul_normal(B<r>, B<a>, B<na>, B<b>, B<nb>) operates on the B<na>
  123. word array B<a>, the B<nb> word array B<b> and the B<na>+B<nb> word
  124. array B<r>. It computes B<a>*B<b> and places the result in B<r>.
  125. bn_mul_low_normal(B<r>, B<a>, B<b>, B<n>) operates on the B<n> word
  126. arrays B<r>, B<a> and B<b>. It computes the B<n> low words of
  127. B<a>*B<b> and places the result in B<r>.
  128. bn_mul_recursive(B<r>, B<a>, B<b>, B<n2>, B<dna>, B<dnb>, B<t>) operates
  129. on the word arrays B<a> and B<b> of length B<n2>+B<dna> and B<n2>+B<dnb>
  130. (B<dna> and B<dnb> are currently allowed to be 0 or negative) and the 2*B<n2>
  131. word arrays B<r> and B<t>. B<n2> must be a power of 2. It computes
  132. B<a>*B<b> and places the result in B<r>.
  133. bn_mul_part_recursive(B<r>, B<a>, B<b>, B<n>, B<tna>, B<tnb>, B<tmp>)
  134. operates on the word arrays B<a> and B<b> of length B<n>+B<tna> and
  135. B<n>+B<tnb> and the 4*B<n> word arrays B<r> and B<tmp>.
  136. bn_mul_low_recursive(B<r>, B<a>, B<b>, B<n2>, B<tmp>) operates on the
  137. B<n2> word arrays B<r> and B<tmp> and the B<n2>/2 word arrays B<a>
  138. and B<b>.
  139. bn_mul_high(B<r>, B<a>, B<b>, B<l>, B<n2>, B<tmp>) operates on the
  140. B<n2> word arrays B<r>, B<a>, B<b> and B<l> (?) and the 3*B<n2> word
  141. array B<tmp>.
  142. BN_mul() calls bn_mul_normal(), or an optimized implementation if the
  143. factors have the same size: bn_mul_comba8() is used if they are 8
  144. words long, bn_mul_recursive() if they are larger than
  145. B<BN_MULL_SIZE_NORMAL> and the size is an exact multiple of the word
  146. size, and bn_mul_part_recursive() for others that are larger than
  147. B<BN_MULL_SIZE_NORMAL>.
  148. bn_sqr_normal(B<r>, B<a>, B<n>, B<tmp>) operates on the B<n> word array
  149. B<a> and the 2*B<n> word arrays B<tmp> and B<r>.
  150. The implementations use the following macros which, depending on the
  151. architecture, may use "long long" C operations or inline assembler.
  152. They are defined in C<bn_lcl.h>.
  153. mul(B<r>, B<a>, B<w>, B<c>) computes B<w>*B<a>+B<c> and places the
  154. low word of the result in B<r> and the high word in B<c>.
  155. mul_add(B<r>, B<a>, B<w>, B<c>) computes B<w>*B<a>+B<r>+B<c> and
  156. places the low word of the result in B<r> and the high word in B<c>.
  157. sqr(B<r0>, B<r1>, B<a>) computes B<a>*B<a> and places the low word
  158. of the result in B<r0> and the high word in B<r1>.
  159. =head2 Size changes
  160. bn_expand() ensures that B<b> has enough space for a B<bits> bit
  161. number. bn_wexpand() ensures that B<b> has enough space for an
  162. B<n> word number. If the number has to be expanded, both macros
  163. call bn_expand2(), which allocates a new B<d> array and copies the
  164. data. They return B<NULL> on error, B<b> otherwise.
  165. The bn_fix_top() macro reduces B<a-E<gt>top> to point to the most
  166. significant non-zero word plus one when B<a> has shrunk.
  167. =head2 Debugging
  168. bn_check_top() verifies that C<((a)-E<gt>top E<gt>= 0 && (a)-E<gt>top
  169. E<lt>= (a)-E<gt>dmax)>. A violation will cause the program to abort.
  170. bn_print() prints B<a> to stderr. bn_dump() prints B<n> words at B<d>
  171. (in reverse order, i.e. most significant word first) to stderr.
  172. bn_set_max() makes B<a> a static number with a B<dmax> of its current size.
  173. This is used by bn_set_low() and bn_set_high() to make B<r> a read-only
  174. B<BIGNUM> that contains the B<n> low or high words of B<a>.
  175. If B<BN_DEBUG> is not defined, bn_check_top(), bn_print(), bn_dump()
  176. and bn_set_max() are defined as empty macros.
  177. =head1 SEE ALSO
  178. L<bn(3)|bn(3)>
  179. =cut