1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimBN_bn2binpad, 6*b077aed3SPierre ProncheryBN_bn2bin, BN_bin2bn, BN_bn2lebinpad, BN_lebin2bn, 7*b077aed3SPierre ProncheryBN_bn2nativepad, BN_native2bn, BN_bn2hex, BN_bn2dec, BN_hex2bn, BN_dec2bn, 8*b077aed3SPierre ProncheryBN_print, BN_print_fp, BN_bn2mpi, BN_mpi2bn - format conversions 9e71b7053SJung-uk Kim 10e71b7053SJung-uk Kim=head1 SYNOPSIS 11e71b7053SJung-uk Kim 12e71b7053SJung-uk Kim #include <openssl/bn.h> 13e71b7053SJung-uk Kim 14e71b7053SJung-uk Kim int BN_bn2bin(const BIGNUM *a, unsigned char *to); 15e71b7053SJung-uk Kim int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen); 16e71b7053SJung-uk Kim BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 17e71b7053SJung-uk Kim 18e71b7053SJung-uk Kim int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen); 19e71b7053SJung-uk Kim BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret); 20e71b7053SJung-uk Kim 21*b077aed3SPierre Pronchery int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen); 22*b077aed3SPierre Pronchery BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret); 23*b077aed3SPierre Pronchery 24e71b7053SJung-uk Kim char *BN_bn2hex(const BIGNUM *a); 25e71b7053SJung-uk Kim char *BN_bn2dec(const BIGNUM *a); 26e71b7053SJung-uk Kim int BN_hex2bn(BIGNUM **a, const char *str); 27e71b7053SJung-uk Kim int BN_dec2bn(BIGNUM **a, const char *str); 28e71b7053SJung-uk Kim 29e71b7053SJung-uk Kim int BN_print(BIO *fp, const BIGNUM *a); 30e71b7053SJung-uk Kim int BN_print_fp(FILE *fp, const BIGNUM *a); 31e71b7053SJung-uk Kim 32e71b7053SJung-uk Kim int BN_bn2mpi(const BIGNUM *a, unsigned char *to); 33e71b7053SJung-uk Kim BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret); 34e71b7053SJung-uk Kim 35e71b7053SJung-uk Kim=head1 DESCRIPTION 36e71b7053SJung-uk Kim 37e71b7053SJung-uk KimBN_bn2bin() converts the absolute value of B<a> into big-endian form 38e71b7053SJung-uk Kimand stores it at B<to>. B<to> must point to BN_num_bytes(B<a>) bytes of 39e71b7053SJung-uk Kimmemory. 40e71b7053SJung-uk Kim 41e71b7053SJung-uk KimBN_bn2binpad() also converts the absolute value of B<a> into big-endian form 42e71b7053SJung-uk Kimand stores it at B<to>. B<tolen> indicates the length of the output buffer 4358f35182SJung-uk KimB<to>. The result is padded with zeros if necessary. If B<tolen> is less than 44e71b7053SJung-uk KimBN_num_bytes(B<a>) an error is returned. 45e71b7053SJung-uk Kim 46e71b7053SJung-uk KimBN_bin2bn() converts the positive integer in big-endian form of length 47e71b7053SJung-uk KimB<len> at B<s> into a B<BIGNUM> and places it in B<ret>. If B<ret> is 48e71b7053SJung-uk KimNULL, a new B<BIGNUM> is created. 49e71b7053SJung-uk Kim 50e71b7053SJung-uk KimBN_bn2lebinpad() and BN_lebin2bn() are identical to BN_bn2binpad() and 51e71b7053SJung-uk KimBN_bin2bn() except the buffer is in little-endian format. 52e71b7053SJung-uk Kim 53*b077aed3SPierre ProncheryBN_bn2nativepad() and BN_native2bn() are identical to BN_bn2binpad() and 54*b077aed3SPierre ProncheryBN_bin2bn() except the buffer is in native format, i.e. most significant 55*b077aed3SPierre Proncherybyte first on big-endian platforms, and least significant byte first on 56*b077aed3SPierre Proncherylittle-endian platforms. 57*b077aed3SPierre Pronchery 58e71b7053SJung-uk KimBN_bn2hex() and BN_bn2dec() return printable strings containing the 59e71b7053SJung-uk Kimhexadecimal and decimal encoding of B<a> respectively. For negative 60e71b7053SJung-uk Kimnumbers, the string is prefaced with a leading '-'. The string must be 61e71b7053SJung-uk Kimfreed later using OPENSSL_free(). 62e71b7053SJung-uk Kim 63e71b7053SJung-uk KimBN_hex2bn() takes as many characters as possible from the string B<str>, 64e71b7053SJung-uk Kimincluding the leading character '-' which means negative, to form a valid 65e71b7053SJung-uk Kimhexadecimal number representation and converts them to a B<BIGNUM> and 66e71b7053SJung-uk Kimstores it in **B<a>. If *B<a> is NULL, a new B<BIGNUM> is created. If 67e71b7053SJung-uk KimB<a> is NULL, it only computes the length of valid representation. 68e71b7053SJung-uk KimA "negative zero" is converted to zero. 69e71b7053SJung-uk KimBN_dec2bn() is the same using the decimal system. 70e71b7053SJung-uk Kim 71e71b7053SJung-uk KimBN_print() and BN_print_fp() write the hexadecimal encoding of B<a>, 72e71b7053SJung-uk Kimwith a leading '-' for negative numbers, to the B<BIO> or B<FILE> 73e71b7053SJung-uk KimB<fp>. 74e71b7053SJung-uk Kim 75e71b7053SJung-uk KimBN_bn2mpi() and BN_mpi2bn() convert B<BIGNUM>s from and to a format 76e71b7053SJung-uk Kimthat consists of the number's length in bytes represented as a 4-byte 77e71b7053SJung-uk Kimbig-endian number, and the number itself in big-endian format, where 78e71b7053SJung-uk Kimthe most significant bit signals a negative number (the representation 79e71b7053SJung-uk Kimof numbers with the MSB set is prefixed with null byte). 80e71b7053SJung-uk Kim 81e71b7053SJung-uk KimBN_bn2mpi() stores the representation of B<a> at B<to>, where B<to> 82e71b7053SJung-uk Kimmust be large enough to hold the result. The size can be determined by 83e71b7053SJung-uk Kimcalling BN_bn2mpi(B<a>, NULL). 84e71b7053SJung-uk Kim 85e71b7053SJung-uk KimBN_mpi2bn() converts the B<len> bytes long representation at B<s> to 86e71b7053SJung-uk Kima B<BIGNUM> and stores it at B<ret>, or in a newly allocated B<BIGNUM> 87e71b7053SJung-uk Kimif B<ret> is NULL. 88e71b7053SJung-uk Kim 89e71b7053SJung-uk Kim=head1 RETURN VALUES 90e71b7053SJung-uk Kim 91e71b7053SJung-uk KimBN_bn2bin() returns the length of the big-endian number placed at B<to>. 92e71b7053SJung-uk KimBN_bin2bn() returns the B<BIGNUM>, NULL on error. 93e71b7053SJung-uk Kim 94*b077aed3SPierre ProncheryBN_bn2binpad(), BN_bn2lebinpad(), and BN_bn2nativepad() return the number of bytes written or -1 if the supplied 95e71b7053SJung-uk Kimbuffer is too small. 96e71b7053SJung-uk Kim 97*b077aed3SPierre ProncheryBN_bn2hex() and BN_bn2dec() return a NUL-terminated string, or NULL 98e71b7053SJung-uk Kimon error. BN_hex2bn() and BN_dec2bn() return the number of characters 99e71b7053SJung-uk Kimused in parsing, or 0 on error, in which 100e71b7053SJung-uk Kimcase no new B<BIGNUM> will be created. 101e71b7053SJung-uk Kim 102e71b7053SJung-uk KimBN_print_fp() and BN_print() return 1 on success, 0 on write errors. 103e71b7053SJung-uk Kim 104e71b7053SJung-uk KimBN_bn2mpi() returns the length of the representation. BN_mpi2bn() 105e71b7053SJung-uk Kimreturns the B<BIGNUM>, and NULL on error. 106e71b7053SJung-uk Kim 107e71b7053SJung-uk KimThe error codes can be obtained by L<ERR_get_error(3)>. 108e71b7053SJung-uk Kim 109e71b7053SJung-uk Kim=head1 SEE ALSO 110e71b7053SJung-uk Kim 111e71b7053SJung-uk KimL<ERR_get_error(3)>, L<BN_zero(3)>, 112e71b7053SJung-uk KimL<ASN1_INTEGER_to_BN(3)>, 113e71b7053SJung-uk KimL<BN_num_bytes(3)> 114e71b7053SJung-uk Kim 115e71b7053SJung-uk Kim=head1 COPYRIGHT 116e71b7053SJung-uk Kim 117*b077aed3SPierre ProncheryCopyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. 118e71b7053SJung-uk Kim 119*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 120e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 121e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 122e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 123e71b7053SJung-uk Kim 124e71b7053SJung-uk Kim=cut 125