BN_new() -- SSLeay 0.9.0b -- January 1999

NAME

BN_bn2bin, BN_bin2bn, BN_bn2hex, BN_hex2bn, BN_bn2dec, BN_dec2bn,
BN_bn2mpi, BN_mpi2bn, BN_print_fp, BN_print -- BN conversion and format routines

SYNOPSIS

#include "bn.h"

int BN_bn2bin(a, to)
BIGNUM *a;
unsigned char *to;

BIGNUM *BN_bin2bn(s, len, ret)
unsigned char *s;
int len;
BIGNUM *ret;

char *BN_bn2hex(a)
BIGNUM *a;

int BN_hex2bn(a, str)
BIGNUM **a;
char *str;

char *BN_bn2dec(a)
BIGNUM *a;

int BN_dec2bn(a, str)
BIGNUM **a;
char *str;

int BN_bn2mpi(a, to)
BIGNUM *a;
unsigned char *to;

BIGNUM *BN_mpi2bn(s, len, ret)
unsigned char *s;
int len;
BIGNUM *ret;

int BN_print_fp(fp, a)
FILE *fp;
BIGNUM *a;

int BN_print(fp, a)
BIO *fp;
BIGNUM *a;

DESCRIPTION

BN_bn2bin converts a to a byte string which is placed in to, which must have been previously allocated to be big enough to hold the result. The length of the value of to in bytes is returned.

to has its most significant byte in t[0] (bigendian form).

To determine in advance how long to must be, so that you can pre-allocate it, call BN_num_bytes(a).

This function does not handle negative numbers.

BN_bin2bn converts len bytes in s into a BIGNUM which is placed in ret. If retis NULL, a new BIGNUM will be allocated for the user. The new value is returned, or NULL on error.

The number in s should have its most significant byte in s[0] (bigendian form).

This function does not handle negative numbers.

BN_bn2hex converts a to a printable hex string and returns the result. The returned string is prefaced with a leading '-' if the value is negative.

BN_hex2bn converts the printable hex string str to a BIGNUM and places the result in a. If a is NULL, a new BIGNUM will be allocated for the user. The new value will be returned, or NULL on error. If the string starts with a leading '-' then the new value will be negative.

bn2dec converts a to a printable decimal string and returns the result. The returned string is prefaced with a leading '-' if the value is negative.

BN_dec2bn converts the printable decimal string str to a BIGNUM and places the result in a. If a is NULL, a new BIGNUM will be allocated for the user. The new value will be returned, or NULL on error. If the string starts with a leading '-' then the new value will be negative.

BN_bn2mpi converts a to an mpi and places the result in to, which must have been previously allocated to be large enough to hold the result. The number of bytes in to is returned. If to is NULL the number of bytes that would be used is returned, so you can call this function first with NULL to to get the length, then alloc to, and then call the function again with the newly allocated to.

The mpi format is as follows:

to[0] through to[3] contain the length in bytes of the remainder of to.

The rest of to contains the BN_bn2bin form of the number.

to[4] has the high bit set if the value is negative; if the value is positive but the msb has the high bit set then to[4] will be a leading byte of 0 to distinguish it from a negative value.

BN_mpi2bn converts len bytes of the mpi s to a BIGNUM and places the result in ret. If ret is NULL, a new BIGNUM will be allocated for the user. The new value will be returned, or NULL on error.

BN_print_fp converts a to a printable hex string as in BN_bn2hex, and then prints the result to file fp. It returns 0 on succes 1 on error.

BN_print does exactly what BN_print_fp does, except that it prints to a BIO and not a file. The function BN_print_fp actually calls this routine.

The following macros are provided for the convenience of the user:

#define BN_ascii2bn(a)  BN_hex2bn(a)
#define BN_bn2ascii(a)  BN_bn2hex(a)

So if you don't like calling BN_hex2bn because you forget what it does, you can call BN_ascii2bn instead :)