1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948BN_bn2bin, BN_bin2bn, BN_bn2hex, BN_bn2dec, BN_hex2bn, BN_dec2bn, 6*2175Sjp161948BN_print, BN_print_fp, BN_bn2mpi, BN_mpi2bn - format conversions 7*2175Sjp161948 8*2175Sjp161948=head1 SYNOPSIS 9*2175Sjp161948 10*2175Sjp161948 #include <openssl/bn.h> 11*2175Sjp161948 12*2175Sjp161948 int BN_bn2bin(const BIGNUM *a, unsigned char *to); 13*2175Sjp161948 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 14*2175Sjp161948 15*2175Sjp161948 char *BN_bn2hex(const BIGNUM *a); 16*2175Sjp161948 char *BN_bn2dec(const BIGNUM *a); 17*2175Sjp161948 int BN_hex2bn(BIGNUM **a, const char *str); 18*2175Sjp161948 int BN_dec2bn(BIGNUM **a, const char *str); 19*2175Sjp161948 20*2175Sjp161948 int BN_print(BIO *fp, const BIGNUM *a); 21*2175Sjp161948 int BN_print_fp(FILE *fp, const BIGNUM *a); 22*2175Sjp161948 23*2175Sjp161948 int BN_bn2mpi(const BIGNUM *a, unsigned char *to); 24*2175Sjp161948 BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret); 25*2175Sjp161948 26*2175Sjp161948=head1 DESCRIPTION 27*2175Sjp161948 28*2175Sjp161948BN_bn2bin() converts the absolute value of B<a> into big-endian form 29*2175Sjp161948and stores it at B<to>. B<to> must point to BN_num_bytes(B<a>) bytes of 30*2175Sjp161948memory. 31*2175Sjp161948 32*2175Sjp161948BN_bin2bn() converts the positive integer in big-endian form of length 33*2175Sjp161948B<len> at B<s> into a B<BIGNUM> and places it in B<ret>. If B<ret> is 34*2175Sjp161948NULL, a new B<BIGNUM> is created. 35*2175Sjp161948 36*2175Sjp161948BN_bn2hex() and BN_bn2dec() return printable strings containing the 37*2175Sjp161948hexadecimal and decimal encoding of B<a> respectively. For negative 38*2175Sjp161948numbers, the string is prefaced with a leading '-'. The string must be 39*2175Sjp161948freed later using OPENSSL_free(). 40*2175Sjp161948 41*2175Sjp161948BN_hex2bn() converts the string B<str> containing a hexadecimal number 42*2175Sjp161948to a B<BIGNUM> and stores it in **B<bn>. If *B<bn> is NULL, a new 43*2175Sjp161948B<BIGNUM> is created. If B<bn> is NULL, it only computes the number's 44*2175Sjp161948length in hexadecimal digits. If the string starts with '-', the 45*2175Sjp161948number is negative. BN_dec2bn() is the same using the decimal system. 46*2175Sjp161948 47*2175Sjp161948BN_print() and BN_print_fp() write the hexadecimal encoding of B<a>, 48*2175Sjp161948with a leading '-' for negative numbers, to the B<BIO> or B<FILE> 49*2175Sjp161948B<fp>. 50*2175Sjp161948 51*2175Sjp161948BN_bn2mpi() and BN_mpi2bn() convert B<BIGNUM>s from and to a format 52*2175Sjp161948that consists of the number's length in bytes represented as a 4-byte 53*2175Sjp161948big-endian number, and the number itself in big-endian format, where 54*2175Sjp161948the most significant bit signals a negative number (the representation 55*2175Sjp161948of numbers with the MSB set is prefixed with null byte). 56*2175Sjp161948 57*2175Sjp161948BN_bn2mpi() stores the representation of B<a> at B<to>, where B<to> 58*2175Sjp161948must be large enough to hold the result. The size can be determined by 59*2175Sjp161948calling BN_bn2mpi(B<a>, NULL). 60*2175Sjp161948 61*2175Sjp161948BN_mpi2bn() converts the B<len> bytes long representation at B<s> to 62*2175Sjp161948a B<BIGNUM> and stores it at B<ret>, or in a newly allocated B<BIGNUM> 63*2175Sjp161948if B<ret> is NULL. 64*2175Sjp161948 65*2175Sjp161948=head1 RETURN VALUES 66*2175Sjp161948 67*2175Sjp161948BN_bn2bin() returns the length of the big-endian number placed at B<to>. 68*2175Sjp161948BN_bin2bn() returns the B<BIGNUM>, NULL on error. 69*2175Sjp161948 70*2175Sjp161948BN_bn2hex() and BN_bn2dec() return a null-terminated string, or NULL 71*2175Sjp161948on error. BN_hex2bn() and BN_dec2bn() return the number's length in 72*2175Sjp161948hexadecimal or decimal digits, and 0 on error. 73*2175Sjp161948 74*2175Sjp161948BN_print_fp() and BN_print() return 1 on success, 0 on write errors. 75*2175Sjp161948 76*2175Sjp161948BN_bn2mpi() returns the length of the representation. BN_mpi2bn() 77*2175Sjp161948returns the B<BIGNUM>, and NULL on error. 78*2175Sjp161948 79*2175Sjp161948The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>. 80*2175Sjp161948 81*2175Sjp161948=head1 SEE ALSO 82*2175Sjp161948 83*2175Sjp161948L<bn(3)|bn(3)>, L<ERR_get_error(3)|ERR_get_error(3)>, L<BN_zero(3)|BN_zero(3)>, 84*2175Sjp161948L<ASN1_INTEGER_to_BN(3)|ASN1_INTEGER_to_BN(3)>, 85*2175Sjp161948L<BN_num_bytes(3)|BN_num_bytes(3)> 86*2175Sjp161948 87*2175Sjp161948=head1 HISTORY 88*2175Sjp161948 89*2175Sjp161948BN_bn2bin(), BN_bin2bn(), BN_print_fp() and BN_print() are available 90*2175Sjp161948in all versions of SSLeay and OpenSSL. 91*2175Sjp161948 92*2175Sjp161948BN_bn2hex(), BN_bn2dec(), BN_hex2bn(), BN_dec2bn(), BN_bn2mpi() and 93*2175Sjp161948BN_mpi2bn() were added in SSLeay 0.9.0. 94*2175Sjp161948 95*2175Sjp161948=cut 96