Scroll to navigation

BN_DUMP(3) Library Functions Manual BN_DUMP(3)

NAME

bn_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_mul_normal, bn_expand, bn_wexpandBIGNUM library internal functions

SYNOPSIS

#include bn_local.h

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);

void
bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);

BIGNUM *
bn_expand(BIGNUM *a, int bits);

BIGNUM *
bn_wexpand(BIGNUM *a, int n);

DESCRIPTION

This page documents some internal functions used by the BIGNUM implementation. They are described here to facilitate debugging and extending the library. They are to be used by applications.

The BIGNUM structure

typedef 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 d, a malloc(3)'ed array of words (BN_ULONG), least significant word first. BN_ULONG is a macro that expands to unsigned long (= uint64_t) on _LP64 platforms and unsigned int (= uint32_t) elsewhere.

dmax is the size of the d array that has been allocated. top is the number of words being used, so for a value of 4, bn.d[0]=4 and bn.top=1. neg is 1 if the number is negative. When a BIGNUM is 0, the d field can be NULL and top == 0.

flags is a bit field of flags which are defined in <openssl/bn.h>. The flags begin with BN_FLG_. The functions BN_set_flags(3) and BN_get_flags(3) enable or inspect flags.

Various routines in this library require the use of temporary BIGNUM variables during their execution. Since dynamic memory allocation to create BIGNUMs is rather expensive when used in conjunction with repeated subroutine calls, the BN_CTX structure is used. This structure contains BN_CTX_NUM BIGNUMs; see BN_CTX_start(3).

Low level arithmetic operations

These functions are implemented in C and for several platforms in assembly language:

(rp, ap, num, w) operates on the num word arrays rp and ap. It computes ap * w, places the result in rp, and returns the high word (carry).

(rp, ap, num, w) operates on the num word arrays rp and ap. It computes ap * w + rp, places the result in rp, and returns the high word (carry).

(rp, ap, num) operates on the num word array ap and the 2*num word array ap. It computes ap * ap word-wise, and places the low and high bytes of the result in rp.

(h, l, d) divides the two word number (h, l) by d and returns the result.

(rp, ap, bp, num) operates on the num word arrays ap, bp and rp. It computes ap + bp, places the result in rp, and returns the high word (carry).

(rp, ap, bp, num) operates on the num word arrays ap, bp and rp. It computes ap - bp, places the result in rp, and returns the carry (1 if bpap, 0 otherwise).

(r, a, b) operates on the 4 word arrays a and b and the 8-word array r. It computes a*b and places the result in r.

(r, a, b) operates on the 8-word arrays a and b and the 16-word array r. It computes a*b and places the result in r.

(r, a, b) operates on the 4-word arrays a and b and the 8-word array r.

(r, a, b) operates on the 8-word arrays a and b and the 16 word array r.

The following functions are implemented in C:

(r, a, na, b, nb) operates on the na word array a, the nb word array b and the na+nb word array r. It computes a*b and places the result in r.

BN_mul(3) calls () if both factors are 4 words long, bn_mul_comba8() if both factors are 8 words long, or bn_mul_normal() otherwise.

Size changes

() ensures that b has enough space for a bits bit number. () ensures that b has enough space for an n word number. They return 0 on error or 1 otherwise.

SEE ALSO

BN_new(3)

November 16, 2023 Linux 6.4.0-150600.23.25-default