1*4724848cSchristos=pod 2*4724848cSchristos 3*4724848cSchristos=head1 NAME 4*4724848cSchristos 5*4724848cSchristosBN_bn2binpad, 6*4724848cSchristosBN_bn2bin, BN_bin2bn, BN_bn2lebinpad, BN_lebin2bn, BN_bn2hex, BN_bn2dec, 7*4724848cSchristosBN_hex2bn, BN_dec2bn, BN_print, BN_print_fp, BN_bn2mpi, 8*4724848cSchristosBN_mpi2bn - format conversions 9*4724848cSchristos 10*4724848cSchristos=head1 SYNOPSIS 11*4724848cSchristos 12*4724848cSchristos #include <openssl/bn.h> 13*4724848cSchristos 14*4724848cSchristos int BN_bn2bin(const BIGNUM *a, unsigned char *to); 15*4724848cSchristos int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen); 16*4724848cSchristos BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 17*4724848cSchristos 18*4724848cSchristos int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen); 19*4724848cSchristos BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret); 20*4724848cSchristos 21*4724848cSchristos char *BN_bn2hex(const BIGNUM *a); 22*4724848cSchristos char *BN_bn2dec(const BIGNUM *a); 23*4724848cSchristos int BN_hex2bn(BIGNUM **a, const char *str); 24*4724848cSchristos int BN_dec2bn(BIGNUM **a, const char *str); 25*4724848cSchristos 26*4724848cSchristos int BN_print(BIO *fp, const BIGNUM *a); 27*4724848cSchristos int BN_print_fp(FILE *fp, const BIGNUM *a); 28*4724848cSchristos 29*4724848cSchristos int BN_bn2mpi(const BIGNUM *a, unsigned char *to); 30*4724848cSchristos BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret); 31*4724848cSchristos 32*4724848cSchristos=head1 DESCRIPTION 33*4724848cSchristos 34*4724848cSchristosBN_bn2bin() converts the absolute value of B<a> into big-endian form 35*4724848cSchristosand stores it at B<to>. B<to> must point to BN_num_bytes(B<a>) bytes of 36*4724848cSchristosmemory. 37*4724848cSchristos 38*4724848cSchristosBN_bn2binpad() also converts the absolute value of B<a> into big-endian form 39*4724848cSchristosand stores it at B<to>. B<tolen> indicates the length of the output buffer 40*4724848cSchristosB<to>. The result is padded with zeros if necessary. If B<tolen> is less than 41*4724848cSchristosBN_num_bytes(B<a>) an error is returned. 42*4724848cSchristos 43*4724848cSchristosBN_bin2bn() converts the positive integer in big-endian form of length 44*4724848cSchristosB<len> at B<s> into a B<BIGNUM> and places it in B<ret>. If B<ret> is 45*4724848cSchristosNULL, a new B<BIGNUM> is created. 46*4724848cSchristos 47*4724848cSchristosBN_bn2lebinpad() and BN_lebin2bn() are identical to BN_bn2binpad() and 48*4724848cSchristosBN_bin2bn() except the buffer is in little-endian format. 49*4724848cSchristos 50*4724848cSchristosBN_bn2hex() and BN_bn2dec() return printable strings containing the 51*4724848cSchristoshexadecimal and decimal encoding of B<a> respectively. For negative 52*4724848cSchristosnumbers, the string is prefaced with a leading '-'. The string must be 53*4724848cSchristosfreed later using OPENSSL_free(). 54*4724848cSchristos 55*4724848cSchristosBN_hex2bn() takes as many characters as possible from the string B<str>, 56*4724848cSchristosincluding the leading character '-' which means negative, to form a valid 57*4724848cSchristoshexadecimal number representation and converts them to a B<BIGNUM> and 58*4724848cSchristosstores it in **B<a>. If *B<a> is NULL, a new B<BIGNUM> is created. If 59*4724848cSchristosB<a> is NULL, it only computes the length of valid representation. 60*4724848cSchristosA "negative zero" is converted to zero. 61*4724848cSchristosBN_dec2bn() is the same using the decimal system. 62*4724848cSchristos 63*4724848cSchristosBN_print() and BN_print_fp() write the hexadecimal encoding of B<a>, 64*4724848cSchristoswith a leading '-' for negative numbers, to the B<BIO> or B<FILE> 65*4724848cSchristosB<fp>. 66*4724848cSchristos 67*4724848cSchristosBN_bn2mpi() and BN_mpi2bn() convert B<BIGNUM>s from and to a format 68*4724848cSchristosthat consists of the number's length in bytes represented as a 4-byte 69*4724848cSchristosbig-endian number, and the number itself in big-endian format, where 70*4724848cSchristosthe most significant bit signals a negative number (the representation 71*4724848cSchristosof numbers with the MSB set is prefixed with null byte). 72*4724848cSchristos 73*4724848cSchristosBN_bn2mpi() stores the representation of B<a> at B<to>, where B<to> 74*4724848cSchristosmust be large enough to hold the result. The size can be determined by 75*4724848cSchristoscalling BN_bn2mpi(B<a>, NULL). 76*4724848cSchristos 77*4724848cSchristosBN_mpi2bn() converts the B<len> bytes long representation at B<s> to 78*4724848cSchristosa B<BIGNUM> and stores it at B<ret>, or in a newly allocated B<BIGNUM> 79*4724848cSchristosif B<ret> is NULL. 80*4724848cSchristos 81*4724848cSchristos=head1 RETURN VALUES 82*4724848cSchristos 83*4724848cSchristosBN_bn2bin() returns the length of the big-endian number placed at B<to>. 84*4724848cSchristosBN_bin2bn() returns the B<BIGNUM>, NULL on error. 85*4724848cSchristos 86*4724848cSchristosBN_bn2binpad() returns the number of bytes written or -1 if the supplied 87*4724848cSchristosbuffer is too small. 88*4724848cSchristos 89*4724848cSchristosBN_bn2hex() and BN_bn2dec() return a null-terminated string, or NULL 90*4724848cSchristoson error. BN_hex2bn() and BN_dec2bn() return the number of characters 91*4724848cSchristosused in parsing, or 0 on error, in which 92*4724848cSchristoscase no new B<BIGNUM> will be created. 93*4724848cSchristos 94*4724848cSchristosBN_print_fp() and BN_print() return 1 on success, 0 on write errors. 95*4724848cSchristos 96*4724848cSchristosBN_bn2mpi() returns the length of the representation. BN_mpi2bn() 97*4724848cSchristosreturns the B<BIGNUM>, and NULL on error. 98*4724848cSchristos 99*4724848cSchristosThe error codes can be obtained by L<ERR_get_error(3)>. 100*4724848cSchristos 101*4724848cSchristos=head1 SEE ALSO 102*4724848cSchristos 103*4724848cSchristosL<ERR_get_error(3)>, L<BN_zero(3)>, 104*4724848cSchristosL<ASN1_INTEGER_to_BN(3)>, 105*4724848cSchristosL<BN_num_bytes(3)> 106*4724848cSchristos 107*4724848cSchristos=head1 COPYRIGHT 108*4724848cSchristos 109*4724848cSchristosCopyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. 110*4724848cSchristos 111*4724848cSchristosLicensed under the OpenSSL license (the "License"). You may not use 112*4724848cSchristosthis file except in compliance with the License. You can obtain a copy 113*4724848cSchristosin the file LICENSE in the source distribution or at 114*4724848cSchristosL<https://www.openssl.org/source/license.html>. 115*4724848cSchristos 116*4724848cSchristos=cut 117