1 .\" $OpenBSD: bn_dump.3,v 1.6 2016/12/10 21:32:14 schwarze Exp $
2 .\" OpenSSL crypto/bn/README.pod aebb9aac Jul 19 09:27:53 2016 -0400
4 .\" This file was written by Ulf Moeller <ulf@openssl.org>.
5 .\" Copyright (c) 2000, 2003, 2006, 2009 The OpenSSL Project.
6 .\" All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in
17 .\" the documentation and/or other materials provided with the
20 .\" 3. All advertising materials mentioning features or use of this
21 .\" software must display the following acknowledgment:
22 .\" "This product includes software developed by the OpenSSL Project
23 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 .\" endorse or promote products derived from this software without
27 .\" prior written permission. For written permission, please contact
28 .\" openssl-core@openssl.org.
30 .\" 5. Products derived from this software may not be called "OpenSSL"
31 .\" nor may "OpenSSL" appear in their names without prior written
32 .\" permission of the OpenSSL Project.
34 .\" 6. Redistributions of any form whatsoever must retain the following
36 .\" "This product includes software developed by the OpenSSL Project
37 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
52 .Dd $Mdocdate: December 10 2016 $
57 .Nm bn_mul_add_words ,
68 .Nm bn_mul_low_normal ,
69 .Nm bn_mul_recursive ,
70 .Nm bn_mul_part_recursive ,
71 .Nm bn_mul_low_recursive ,
74 .Nm bn_sqr_recursive ,
88 .Nd BIGNUM library internal functions
168 .Fo bn_mul_low_normal
185 .Fo bn_mul_part_recursive
195 .Fo bn_mul_low_recursive
294 This page documents the internal functions used by the OpenSSL
297 They are described here to facilitate debugging and extending the
301 to be used by applications.
302 .Ss The BIGNUM structure
304 typedef struct bignum_st BIGNUM;
307 BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
308 int top; /* Index of last used d +1. */
309 /* The next are internal book keeping for bn_expand. */
310 int dmax; /* Size of the d array. */
311 int neg; /* one if the number is negative */
316 The integer value is stored in
322 least significant word first.
325 can be either 16, 32 or 64 bits in size, depending on the 'number of
334 array that has been allocated.
336 is the number of words being used, so for a value of 4, bn.d[0]=4 and
339 is 1 if the number is negative.
351 is a bit field of flags which are defined in
359 exist to enable or fetch flag(s)
366 Various routines in this library require the use of temporary
368 variables during their execution.
369 Since dynamic memory allocation to create
371 is rather expensive when used in conjunction with repeated subroutine
375 This structure contains BN_CTX_NUM
379 .Ss Low level arithmetic operations
380 These functions are implemented in C and for several platforms in
383 .Fn bn_mul_words rp ap num w
396 and returns the high word (carry).
398 .Fn bn_mul_add_words rp ap num w
413 and returns the high word (carry).
415 .Fn bn_sqr_words rp ap num
428 word-wise, and places the low and high bytes of the result in
431 .Fn bn_div_words h l d
432 divides the two word number
436 and returns the result.
438 .Fn bn_add_words rp ap bp num
452 and returns the high word (carry).
454 .Fn bn_sub_words rp ap bp num
468 and returns the carry (1 if
474 .Fn bn_mul_comba4 r a b
475 operates on the 4 word arrays
483 and places the result in
486 .Fn bn_mul_comba8 r a b
487 operates on the 8-word arrays
491 and the 16-word array
495 and places the result in
498 .Fn bn_sqr_comba4 r a b
499 operates on the 4-word arrays
506 .Fn bn_sqr_comba8 r a b
507 operates on the 8-word arrays
511 and the 16 word array
514 The following functions are implemented in C:
516 .Fn bn_cmp_words a b n
523 It returns 1, 0 and -1 if
525 is greater than, equal and less than
528 .Fn bn_mul_normal r a na b nb
543 and places the result in
546 .Fn bn_mul_low_normal r a b n
558 and places the result in
561 .Fn bn_mul_recursive r a b n2 dna dnb t
562 operates on the word arrays
567 .Fa n2 Ns + Ns Fa dna
569 .Fa n2 Ns + Ns Fa dnb
573 are currently allowed to be 0 or negative) and the
580 must be a power of 2.
583 and places the result in
586 .Fn bn_mul_part_recursive r a b n tna tnb tmp
587 operates on the word arrays
602 .Fn bn_mul_low_recursive r a b n2 tmp
616 .Fn bn_mul_high r a b l n2 tmp
633 or an optimized implementation if the factors have the same size:
635 is used if they are 8 words long,
637 if they are larger than
638 .Dv BN_MULL_SIZE_NORMAL
639 and the size is an exact multiple of the word size, and
640 .Fn bn_mul_part_recursive
641 for others that are larger than
642 .Dv BN_MULL_SIZE_NORMAL .
644 .Fn bn_sqr_normal r a n tmp
656 The implementations use the following macros which, depending on the
657 architecture, may use
659 C operations or inline assembler.
665 .Fa w Ns * Ns Fa a Ns + Ns Fa c
666 and places the low word of the result in
673 .Fa w Ns * Ns Fa a Ns + Ns Fa r Ns + Ns Fa c
674 and places the low word of the result in
682 and places the low word of the result in
690 has enough space for a
696 has enough space for an
699 If the number has to be expanded, both macros call
701 which allocates a new
703 array and copies the data.
713 .Fa a Ns -> Ns Fa top
714 to point to the most significant non-zero word plus one when
720 .Ql ((a)-\(ratop \(ra= 0 && (a)-\(ratop \(la= (a)-\(radmax) .
721 A violation will cause the program to abort.
733 (in reverse order, i.e.\&
734 most significant word first) to
740 a static number with a
764 are defined as empty macros.