BN_bn2bin.3ossl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "BN_BN2BIN 3ossl"
  58. .TH BN_BN2BIN 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. BN_bn2binpad, BN_signed_bn2bin, BN_bn2bin, BN_bin2bn, BN_signed_bin2bn,
  65. BN_bn2lebinpad, BN_signed_bn2lebin, BN_lebin2bn, BN_signed_lebin2bn,
  66. BN_bn2nativepad, BN_signed_bn2native, BN_native2bn, BN_signed_native2bn,
  67. BN_bn2hex, BN_bn2dec, BN_hex2bn, BN_dec2bn,
  68. BN_print, BN_print_fp, BN_bn2mpi, BN_mpi2bn \- format conversions
  69. .SH SYNOPSIS
  70. .IX Header "SYNOPSIS"
  71. .Vb 1
  72. \& #include <openssl/bn.h>
  73. \&
  74. \& int BN_bn2bin(const BIGNUM *a, unsigned char *to);
  75. \& int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen);
  76. \& int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen);
  77. \& BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
  78. \& BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
  79. \&
  80. \& int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen);
  81. \& int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen);
  82. \& BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret);
  83. \& BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret);
  84. \&
  85. \& int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen);
  86. \& int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen);
  87. \& BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret);
  88. \& BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret);
  89. \&
  90. \& char *BN_bn2hex(const BIGNUM *a);
  91. \& char *BN_bn2dec(const BIGNUM *a);
  92. \& int BN_hex2bn(BIGNUM **a, const char *str);
  93. \& int BN_dec2bn(BIGNUM **a, const char *str);
  94. \&
  95. \& int BN_print(BIO *fp, const BIGNUM *a);
  96. \& int BN_print_fp(FILE *fp, const BIGNUM *a);
  97. \&
  98. \& int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
  99. \& BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
  100. .Ve
  101. .SH DESCRIPTION
  102. .IX Header "DESCRIPTION"
  103. \&\fBBN_bn2bin()\fR converts the absolute value of \fBa\fR into big-endian form
  104. and stores it at \fBto\fR. \fBto\fR must point to BN_num_bytes(\fBa\fR) bytes of
  105. memory.
  106. .PP
  107. \&\fBBN_bn2binpad()\fR also converts the absolute value of \fBa\fR into big-endian form
  108. and stores it at \fBto\fR. \fBtolen\fR indicates the length of the output buffer
  109. \&\fBto\fR. The result is padded with zeros if necessary. If \fBtolen\fR is less than
  110. BN_num_bytes(\fBa\fR) an error is returned.
  111. .PP
  112. \&\fBBN_signed_bn2bin()\fR converts the value of \fBa\fR into big-endian signed 2's
  113. complements form and stores it at \fBto\fR. \fBtolen\fR indicates the length of
  114. the output buffer \fBto\fR. The result is signed extended (padded with 0x00
  115. for positive numbers or with 0xff for negative numbers) if necessary.
  116. If \fBtolen\fR is smaller than the necessary size (which may be
  117. \&\f(CW\*(C`<BN_num_bytes(\fR\f(CBa\fR\f(CW) + 1\*(C'\fR>), an error is returned.
  118. .PP
  119. \&\fBBN_bin2bn()\fR converts the positive integer in big-endian form of length
  120. \&\fBlen\fR at \fBs\fR into a \fBBIGNUM\fR and places it in \fBret\fR. If \fBret\fR is
  121. NULL, a new \fBBIGNUM\fR is created.
  122. .PP
  123. \&\fBBN_signed_bin2bn()\fR converts the integer in big-endian signed 2's complement
  124. form of length \fBlen\fR at \fBs\fR into a \fBBIGNUM\fR and places it in \fBret\fR. If
  125. \&\fBret\fR is NULL, a new \fBBIGNUM\fR is created.
  126. .PP
  127. \&\fBBN_bn2lebinpad()\fR, \fBBN_signed_bn2lebin()\fR and \fBBN_lebin2bn()\fR are identical to
  128. \&\fBBN_bn2binpad()\fR, \fBBN_signed_bn2bin()\fR and \fBBN_bin2bn()\fR except the buffer is in
  129. little-endian format.
  130. .PP
  131. \&\fBBN_bn2nativepad()\fR, \fBBN_signed_bn2native()\fR and \fBBN_native2bn()\fR are identical
  132. to \fBBN_bn2binpad()\fR, \fBBN_signed_bn2bin()\fR and \fBBN_bin2bn()\fR except the buffer is
  133. in native format, i.e. most significant byte first on big-endian platforms,
  134. and least significant byte first on little-endian platforms.
  135. .PP
  136. \&\fBBN_bn2hex()\fR and \fBBN_bn2dec()\fR return printable strings containing the
  137. hexadecimal and decimal encoding of \fBa\fR respectively. For negative
  138. numbers, the string is prefaced with a leading '\-'. The string must be
  139. freed later using \fBOPENSSL_free()\fR.
  140. .PP
  141. \&\fBBN_hex2bn()\fR takes as many characters as possible from the string \fBstr\fR,
  142. including the leading character '\-' which means negative, to form a valid
  143. hexadecimal number representation and converts them to a \fBBIGNUM\fR and
  144. stores it in **\fBa\fR. If *\fBa\fR is NULL, a new \fBBIGNUM\fR is created. If
  145. \&\fBa\fR is NULL, it only computes the length of valid representation.
  146. A "negative zero" is converted to zero.
  147. \&\fBBN_dec2bn()\fR is the same using the decimal system.
  148. .PP
  149. \&\fBBN_print()\fR and \fBBN_print_fp()\fR write the hexadecimal encoding of \fBa\fR,
  150. with a leading '\-' for negative numbers, to the \fBBIO\fR or \fBFILE\fR
  151. \&\fBfp\fR.
  152. .PP
  153. \&\fBBN_bn2mpi()\fR and \fBBN_mpi2bn()\fR convert \fBBIGNUM\fRs from and to a format
  154. that consists of the number's length in bytes represented as a 4\-byte
  155. big-endian number, and the number itself in big-endian format, where
  156. the most significant bit signals a negative number (the representation
  157. of numbers with the MSB set is prefixed with null byte).
  158. .PP
  159. \&\fBBN_bn2mpi()\fR stores the representation of \fBa\fR at \fBto\fR, where \fBto\fR
  160. must be large enough to hold the result. The size can be determined by
  161. calling BN_bn2mpi(\fBa\fR, NULL).
  162. .PP
  163. \&\fBBN_mpi2bn()\fR converts the \fBlen\fR bytes long representation at \fBs\fR to
  164. a \fBBIGNUM\fR and stores it at \fBret\fR, or in a newly allocated \fBBIGNUM\fR
  165. if \fBret\fR is NULL.
  166. .SH "RETURN VALUES"
  167. .IX Header "RETURN VALUES"
  168. \&\fBBN_bn2bin()\fR returns the length of the big-endian number placed at \fBto\fR.
  169. \&\fBBN_bin2bn()\fR returns the \fBBIGNUM\fR, NULL on error.
  170. .PP
  171. \&\fBBN_bn2binpad()\fR, \fBBN_signed_bn2bin()\fR, \fBBN_bn2lebinpad()\fR, \fBBN_signed_bn2lebin()\fR,
  172. \&\fBBN_bn2nativepad()\fR, and_signed \fBBN_bn2native()\fR return the number of bytes
  173. written or \-1 if the supplied buffer is too small.
  174. .PP
  175. \&\fBBN_bn2hex()\fR and \fBBN_bn2dec()\fR return a NUL-terminated string, or NULL
  176. on error. \fBBN_hex2bn()\fR and \fBBN_dec2bn()\fR return the number of characters
  177. used in parsing, or 0 on error, in which
  178. case no new \fBBIGNUM\fR will be created.
  179. .PP
  180. \&\fBBN_print_fp()\fR and \fBBN_print()\fR return 1 on success, 0 on write errors.
  181. .PP
  182. \&\fBBN_bn2mpi()\fR returns the length of the representation. \fBBN_mpi2bn()\fR
  183. returns the \fBBIGNUM\fR, and NULL on error.
  184. .PP
  185. The error codes can be obtained by \fBERR_get_error\fR\|(3).
  186. .SH "SEE ALSO"
  187. .IX Header "SEE ALSO"
  188. \&\fBERR_get_error\fR\|(3), \fBBN_zero\fR\|(3),
  189. \&\fBASN1_INTEGER_to_BN\fR\|(3),
  190. \&\fBBN_num_bytes\fR\|(3)
  191. .SH COPYRIGHT
  192. .IX Header "COPYRIGHT"
  193. Copyright 2000\-2022 The OpenSSL Project Authors. All Rights Reserved.
  194. .PP
  195. Licensed under the Apache License 2.0 (the "License"). You may not use
  196. this file except in compliance with the License. You can obtain a copy
  197. in the file LICENSE in the source distribution or at
  198. <https://www.openssl.org/source/license.html>.