OpenSSL bn_internal(3openssl)
NNNNAAAAMMMMEEEEbn_mul_words, bn_mul_add_words, bn_sqr_words, bn_div_words,
bn_add_words, bn_sub_words, bn_mul_comba4, bn_mul_comba8,
bn_sqr_comba4, bn_sqr_comba8, bn_cmp_words, bn_mul_normal,
bn_mul_low_normal, bn_mul_recursive, bn_mul_part_recursive,
bn_mul_low_recursive, bn_mul_high, bn_sqr_normal,
bn_sqr_recursive, bn_expand, bn_wexpand, bn_expand2,
bn_fix_top, bn_check_top, bn_print, bn_dump, bn_set_max,
bn_set_high, bn_set_low - BIGNUM library internal functions
SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS#include
BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num,
BN_ULONG w);
void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
int num);BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,
int num);void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a);
void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a);
int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n);
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
int nb);void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n);
void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
int dna,int dnb,BN_ULONG *tmp);
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
int n, int tna,int tnb, BN_ULONG *tmp);
void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b,
int n2, BN_ULONG *tmp);
void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l,
int n2, BN_ULONG *tmp);
void bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp);
void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *tmp);
void mul(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
void mul_add(BN_ULONG r, BN_ULONG a, BN_ULONG w, BN_ULONG c);
void sqr(BN_ULONG r0, BN_ULONG r1, BN_ULONG a);
27/Mar/2010 Last change: 0.9.8o 1
OpenSSL bn_internal(3openssl)
BIGNUM *bn_expand(BIGNUM *a, int bits);
BIGNUM *bn_wexpand(BIGNUM *a, int n);
BIGNUM *bn_expand2(BIGNUM *a, int n);
void bn_fix_top(BIGNUM *a);
void bn_check_top(BIGNUM *a);
void bn_print(BIGNUM *a);
void bn_dump(BN_ULONG *d, int n);
void bn_set_max(BIGNUM *a);
void bn_set_high(BIGNUM *r, BIGNUM *a, int n);
void bn_set_low(BIGNUM *r, BIGNUM *a, int n);
DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN This page documents the internal functions used by the OpenSSL BBBBIIIIGGGGNNNNUUUUMMMM implementation. They are described here to facilitate debugging and extending the library. They are not to be used by applications. TTTThhhheeee BBBBIIIIGGGGNNNNUUUUMMMM ssssttttrrrruuuuccccttttuuuurrrreeeetypedef struct bignum_st BIGNUM;
struct bignum_st
{BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
int top; /* Index of last used d +1. *//* The next are internal book keeping for bn_expand. */
int dmax; /* Size of the d array. */ int neg; /* one if the number is negative */ int flags; }; The integer value is stored in dddd, a malloc()ed array ofwords (BBBBNNNN_UUUULLLLOOOONNNNGGGG), least significant word first. A BBBBNNNN_UUUULLLLOOOONNNNGGGG
can be either 16, 32 or 64 bits in size, depending on the 'number of bits' (BBBBIIIITTTTSSSS2222) specified in openssl/bn.h. ddddmmmmaaaaxxxx is the size of the dddd array that has been allocated. ttttoooopppp is the number of words being used, so for a value of 4, bn.d[0]=4 and bn.top=1. nnnneeeegggg is 1 if the number is negative. When a BBBBIIIIGGGGNNNNUUUUMMMM is 0000, the dddd field can be NNNNUUUULLLLLLLL and ttttoooopppp == 0000. ffffllllaaaaggggssss is a bit field of flags which are defined inopenssl/bn.h. The flags begin with BBBBNNNN_FFFFLLLLGGGG_. The macros
BN_set_flags(b,n) and BN_get_flags(b,n) exist to enable or
fetch flag(s) nnnn from BBBBIIIIGGGGNNNNUUUUMMMM structure bbbb. Various routines in this library require the use of temporary BBBBIIIIGGGGNNNNUUUUMMMM variables during their execution. Since dynamic memory allocation to create BBBBIIIIGGGGNNNNUUUUMMMMs is rather expensive when used in conjunction with repeated subroutine27/Mar/2010 Last change: 0.9.8o 2
OpenSSL bn_internal(3openssl)
calls, the BBBBNNNN_CCCCTTTTXXXX structure is used. This structure
contains BBBBNNNN_CCCCTTTTXXXX_NNNNUUUUMMMM BBBBIIIIGGGGNNNNUUUUMMMMs, see BN_CTX_start(3).
LLLLoooowwww---llleeeevvvveeeellll aaaarrrriiiitttthhhhmmmmeeeettttiiiicccc ooooppppeeeerrrraaaattttiiiioooonnnnssss
These functions are implemented in C and for several platforms in assembly language:bn_mul_words(rrrrpppp, aaaapppp, nnnnuuuummmm, wwww) operates on the nnnnuuuummmm word arrays
rrrrpppp and aaaapppp. It computes aaaapppp * wwww, places the result in rrrrpppp, and returns the high word (carry).bn_mul_add_words(rrrrpppp, aaaapppp, nnnnuuuummmm, wwww) operates on the nnnnuuuummmm word
arrays rrrrpppp and aaaapppp. It computes aaaapppp * wwww + rrrrpppp, places the result in rrrrpppp, and returns the high word (carry).bn_sqr_words(rrrrpppp, aaaapppp, nnnn) operates on the nnnnuuuummmm word array aaaapppp
and the 2*nnnnuuuummmm word array aaaapppp. It computes aaaapppp * aaaapppp word-wise,
and places the low and high bytes of the result in rrrrpppp.bn_div_words(hhhh, llll, dddd) divides the two word number (hhhh,llll) by dddd
and returns the result.bn_add_words(rrrrpppp, aaaapppp, bbbbpppp, nnnnuuuummmm) operates on the nnnnuuuummmm word
arrays aaaapppp, bbbbpppp and rrrrpppp. It computes aaaapppp + bbbbpppp, places the result in rrrrpppp, and returns the high word (carry).bn_sub_words(rrrrpppp, aaaapppp, bbbbpppp, nnnnuuuummmm) operates on the nnnnuuuummmm word
arrays aaaapppp, bbbbpppp and rrrrpppp. It computes aaaapppp - bbbbpppp, places the
result in rrrrpppp, and returns the carry (1 if bbbbpppp > aaaapppp, 0 otherwise).bn_mul_comba4(rrrr, aaaa, bbbb) operates on the 4 word arrays aaaa and bbbb
and the 8 word array rrrr. It computes aaaa*bbbb and places the result in rrrr.bn_mul_comba8(rrrr, aaaa, bbbb) operates on the 8 word arrays aaaa and bbbb
and the 16 word array rrrr. It computes aaaa*bbbb and places the result in rrrr.bn_sqr_comba4(rrrr, aaaa, bbbb) operates on the 4 word arrays aaaa and bbbb
and the 8 word array rrrr.bn_sqr_comba8(rrrr, aaaa, bbbb) operates on the 8 word arrays aaaa and bbbb
and the 16 word array rrrr. The following functions are implemented in C:bn_cmp_words(aaaa, bbbb, nnnn) operates on the nnnn word arrays aaaa and bbbb.
It returns 1, 0 and -1 if aaaa is greater than, equal and less
than bbbb.27/Mar/2010 Last change: 0.9.8o 3
OpenSSL bn_internal(3openssl)
bn_mul_normal(rrrr, aaaa, nnnnaaaa, bbbb, nnnnbbbb) operates on the nnnnaaaa word array
aaaa, the nnnnbbbb word array bbbb and the nnnnaaaa+nnnnbbbb word array rrrr. It computes aaaa*bbbb and places the result in rrrr.bn_mul_low_normal(rrrr, aaaa, bbbb, nnnn) operates on the nnnn word arrays
rrrr, aaaa and bbbb. It computes the nnnn low words of aaaa*bbbb and places the result in rrrr.bn_mul_recursive(rrrr, aaaa, bbbb, nnnn2222, ddddnnnnaaaa, ddddnnnnbbbb, tttt) operates on the
word arrays aaaa and bbbb of length nnnn2222+ddddnnnnaaaa and nnnn2222+ddddnnnnbbbb (ddddnnnnaaaa and ddddnnnnbbbb are currently allowed to be 0 or negative) and the 2*nnnn2222 word arrays rrrr and tttt. nnnn2222 must be a power of 2. It computes aaaa*bbbb and places the result in rrrr.bn_mul_part_recursive(rrrr, aaaa, bbbb, nnnn, ttttnnnnaaaa, ttttnnnnbbbb, ttttmmmmpppp) operates on
the word arrays aaaa and bbbb of length nnnn+ttttnnnnaaaa and nnnn+ttttnnnnbbbb and the 4*nnnn word arrays rrrr and ttttmmmmpppp.bn_mul_low_recursive(rrrr, aaaa, bbbb, nnnn2222, ttttmmmmpppp) operates on the nnnn2222
word arrays rrrr and ttttmmmmpppp and the nnnn2222/2 word arrays aaaa and bbbb.bn_mul_high(rrrr, aaaa, bbbb, llll, nnnn2222, ttttmmmmpppp) operates on the nnnn2222 word
arrays rrrr, aaaa, bbbb and llll (?) and the 3*nnnn2222 word array ttttmmmmpppp.BN_mul() calls bn_mul_normal(), or an optimized
implementation if the factors have the same size:bn_mul_comba8() is used if they are 8 words long,
bn_mul_recursive() if they are larger than
BBBBNNNN_MMMMUUUULLLLLLLL_SSSSIIIIZZZZEEEE_NNNNOOOORRRRMMMMAAAALLLL and the size is an exact multiple of the
word size, and bn_mul_part_recursive() for others that are
larger than BBBBNNNN_MMMMUUUULLLLLLLL_SSSSIIIIZZZZEEEE_NNNNOOOORRRRMMMMAAAALLLL.
bn_sqr_normal(rrrr, aaaa, nnnn, ttttmmmmpppp) operates on the nnnn word array aaaa
and the 2*nnnn word arrays ttttmmmmpppp and rrrr. The implementations use the following macros which, depending on the architecture, may use "long long" C operations or inline assembler. They are defined inbn_lcl.h.
mul(rrrr, aaaa, wwww, cccc) computes wwww*aaaa+cccc and places the low word of the result in rrrr and the high word in cccc.mul_add(rrrr, aaaa, wwww, cccc) computes wwww*aaaa+rrrr+cccc and places the low word
of the result in rrrr and the high word in cccc. sqr(rrrr0000, rrrr1111, aaaa) computes aaaa*aaaa and places the low word of the result in rrrr0000 and the high word in rrrr1111.27/Mar/2010 Last change: 0.9.8o 4
OpenSSL bn_internal(3openssl)
SSSSiiiizzzzeeee cccchhhhaaaannnnggggeeeessssbn_expand() ensures that bbbb has enough space for a bbbbiiiittttssss bit
number. bn_wexpand() ensures that bbbb has enough space for an
nnnn word number. If the number has to be expanded, bothmacros call bn_expand2(), which allocates a new dddd array and
copies the data. They return NNNNUUUULLLLLLLL on error, bbbb otherwise.The bn_fix_top() macro reduces aaaa---->>>>ttttoooopppp to point to the most
significant non-zero word plus one when aaaa has shrunk.
DDDDeeeebbbbuuuuggggggggiiiinnnnggggbn_check_top() verifies that ((a)->top >= 0 && (a)->top <=
(a)->dmax). A violation will cause the program to abort.
bn_print() prints aaaa to stderr. bn_dump() prints nnnn words at dddd
(in reverse order, i.e. most significant word first) to stderr.bn_set_max() makes aaaa a static number with a ddddmmmmaaaaxxxx of its
current size. This is used by bn_set_low() and
bn_set_high() to make rrrr a read-only BBBBIIIIGGGGNNNNUUUUMMMM that contains the
nnnn low or high words of aaaa.If BBBBNNNN_DDDDEEEEBBBBUUUUGGGG is not defined, bn_check_top(), bn_print(),
bn_dump() and bn_set_max() are defined as empty macros.
SSSSEEEEEEEE AAAALLLLSSSSOOOO bn(3)27/Mar/2010 Last change: 0.9.8o 5