LIBNETPGPBN(3)

NAME
libnetpgpbn - BIGNUM library of multi-precision integers

LIBRARY
libnetpgpbn

SYNOPSIS
#include <netpgp/bn.h>
BIGNUM *
BN_new(void);
BIGNUM *
BN_dup(const BIGNUM *orig);
int
BN_copy(BIGNUM *to, const BIGNUM *from);
void
BN_swap(BIGNUM *a, BIGNUM *b);

void
BN_init(BIGNUM *bn);
void
BN_free(BIGNUM *bn);
void
BN_clear(BIGNUM *bn);
void
BN_clear_free(BIGNUM *bn);

void
BN_clear_free(BIGNUM *bn);

int
BN_cmp(BIGNUM *lhs, BIGNUM *rhs);
int
BN_is_negative(BIGNUM *bn);
int
BN_is_zero(BIGNUM *bn);
int
BN_is_odd(BIGNUM *bn);
int
BN_is_even(BIGNUM *bn);

BIGNUM *
BN_bin2bn(const uint8_t *buf, int size, BIGNUM *bn);
int
BN_bn2bin(BIGNUM *bn, uint8_t *buf);
int
BN_bn2bin(BIGNUM *bn, uint8_t *buf);
char *
BN_bn2hex(const BIGNUM *bn);
char *
BN_bn2dec(const BIGNUM *bn);
int
BN_hex2bn(BIGNUM **bn, const char *str);
int
BN_dec2bn(BIGNUM **bn, const char *str);
int
BN_print_fp(FILE *fp, const BIGNUM *bn);

int
BN_add(BIGNUM *sum, const BIGNUM *a, const BIGNUM *b);
int
BN_sub(BIGNUM *sum, const BIGNUM *a, const BIGNUM *b);
int
BN_mul(BIGNUM *product, const BIGNUM *a, const BIGNUM *b, BN_CTX *context);
int
BN_div(BIGNUM *quotient, BIGNUM *remainder, const BIGNUM *a, const BIGNUM *b, BN_CTX *context);

int
BN_lshift(BIGNUM *result, const BIGNUM *bn, int n);
int
BN_lshift1(BIGNUM *result, const BIGNUM *bn);
int
BN_rshift(BIGNUM *result, const BIGNUM *bn, int n);
int
BN_rshift1(BIGNUM *result, const BIGNUM *bn);

int
BN_set_word(BIGNUM *result, unsigned long val);
int
BN_set_negative(BIGNUM *result, int val);

int
BN_num_bytes(const BIGNUM *bn);
int
BN_num_bits(const BIGNUM *bn);

int
BN_mod_exp(BIGNUM *result, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *context);
BIGNUM *
BN_mod_inverse(BIGNUM *result, BIGNUM *a, BIGNUM *n, BN_CTX *context);
int
BN_mod_mul(BIGNUM *result, BIGNUM *a, BIGNUM *b, BIGNUM *m, BN_CTX *context);
int
BN_mod_sub(BIGNUM *result, BIGNUM *a, BIGNUM *b, BIGNUM *m, BN_CTX *context);

BN_CTX *
BN_CTX_new(void);
BIGNUM *
BN_CTX_get(BN_CTX *context);
void
BN_CTX_start(BN_CTX *context);
void
BN_CTX_end(BN_CTX *context);
void
BN_CTX_init(BN_CTX *context);
void
BN_CTX_free(BN_CTX *context);
int
BN_rand(BIGNUM *result, int bits, int top, int bottom);
int
BN_rand_range(BIGNUM *result, BIGNUM *range);
int
BN_is_prime(const BIGNUM *bn, int checks, void (*callback)(int int void), BN_CTX *context, void *callbackarg);

const BIGNUM *
BN_value_one(void);
int
BN_is_bit_set(const BIGNUM *bn, int n);

DESCRIPTION
libnetpgpbn emulates the API of the openssl bn(3) library. It is implemented using Tom St Denis's “libtommath” library.

EXAMPLES
The following code fragment will make a JSON object out of the string “Hello <USERNAME>\n” in the buffer called buf where “USERNAME” is the name of the user taken from the runtime environment. The encoded text will be in an allocated buffer called s.

    mj_t atom;
    char buf[BUFSIZ];
    char *s;
    int cc;
    (void) memset(atom, 0x0, sizeof(atom));
    cc = snprintf(buf, sizeof(buf), "Hello %s\n", getenv("USER"));
    mj_create(atom, "string", buf, cc);
    cc = mj_asprint(s, atom, MJ_JSON_ENCODE);

Next, the following example will take the (binary) text which has been encoded into JSON and is in the buffer buf, such as in the previous example, and re-create the original text:

    int from, to, tok, cc;
    char *s;
    mj_t atom;
    (void) memset(atom, 0x0, sizeof(atom));
    from = to = tok = 0;
    mj_parse(atom, buf, from, to, tok);
    cc = mj_asprint(s, atom, MJ_HUMAN);
    printf("%.*s", cc, s);

The s pointer points to allocated storage with the original NUL-terminated string in it.

SEE ALSO
bn(3)

HISTORY
The libnetpgpbn library first appeared in NetBSD 7.0.

AUTHORS
Alistair Crooks <agc@NetBSD.org>.