xref: /openbsd-src/lib/libcrypto/man/BN_bn2bin.3 (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1.Dd $Mdocdate: February 23 2015 $
2.Dt BN_BN2BIN 3
3.Os
4.Sh NAME
5.Nm BN_bn2bin ,
6.Nm BN_bin2bn ,
7.Nm BN_bn2hex ,
8.Nm BN_bn2dec ,
9.Nm BN_hex2bn ,
10.Nm BN_dec2bn ,
11.Nm BN_print ,
12.Nm BN_print_fp ,
13.Nm BN_bn2mpi ,
14.Nm BN_mpi2bn
15.Nd format conversions
16.Sh SYNOPSIS
17.In openssl/bn.h
18.Ft int
19.Fo BN_bn2bin
20.Fa "const BIGNUM *a"
21.Fa "unsigned char *to"
22.Fc
23.Ft BIGNUM *
24.Fo BN_bin2bn
25.Fa "const unsigned char *s"
26.Fa "int len"
27.Fa "BIGNUM *ret"
28.Fc
29.Ft char *
30.Fo BN_bn2hex
31.Fa "const BIGNUM *a"
32.Fc
33.Ft char *
34.Fo BN_bn2dec
35.Fa "const BIGNUM *a"
36.Fc
37.Ft int
38.Fo BN_hex2bn
39.Fa "BIGNUM **a"
40.Fa "const char *str"
41.Fc
42.Ft int
43.Fo BN_dec2bn
44.Fa "BIGNUM **a"
45.Fa "const char *str"
46.Fc
47.Ft int
48.Fo BN_print
49.Fa "BIO *fp"
50.Fa "const BIGNUM *a"
51.Fc
52.Ft int
53.Fo BN_print_fp
54.Fa "FILE *fp"
55.Fa "const BIGNUM *a"
56.Fc
57.Ft int
58.Fo BN_bn2mpi
59.Fa "const BIGNUM *a"
60.Fa "unsigned char *to"
61.Fc
62.Ft BIGNUM *
63.Fo BN_mpi2bn
64.Fa "unsigned char *s"
65.Fa "int len"
66.Fa "BIGNUM *ret"
67.Fc
68.Sh DESCRIPTION
69.Fn BN_bn2bin
70converts the absolute value of
71.Fa a
72into big-endian form and stores it at
73.Fa to .
74.Fa to
75must point to
76.Fn BN_num_bytes a
77bytes of memory.
78.Pp
79.Fn BN_bin2bn
80converts the positive integer in big-endian form of length
81.Fa len
82at
83.Fa s
84into a
85.Vt BIGNUM
86and places it in
87.Fa ret .
88If
89.Fa ret
90is
91.Dv NULL ,
92a new
93.Vt BIGNUM
94is created.
95.Pp
96.Fn BN_bn2hex
97and
98.Fn BN_bn2dec
99return printable strings containing the hexadecimal and decimal encoding of
100.Fa a
101respectively.
102For negative numbers, the string is prefaced with a leading minus sign.
103The string must be freed later using
104.Xr free 3 .
105.Pp
106.Fn BN_hex2bn
107converts the string
108.Fa str
109containing a hexadecimal number to a
110.Vt BIGNUM
111and stores it in
112.Pf * Fa a .
113If
114.Pf * Fa a
115is
116.Dv NULL ,
117a new
118.Vt BIGNUM
119is created.
120If
121.Fa a
122is
123.Dv NULL ,
124it only computes the number's length in hexadecimal digits.
125If the string starts with a minus sign, the number is negative.
126.Fn BN_dec2bn
127is the same using the decimal system.
128.Pp
129.Fn BN_print
130and
131.Fn BN_print_fp
132write the hexadecimal encoding of
133.Fa a ,
134with a leading minus sign for negative numbers, to the
135.Vt BIO
136or
137.Vt FILE
138.Fa fp .
139.Pp
140.Fn BN_bn2mpi
141and
142.Fn BN_mpi2bn
143convert
144.Vt BIGNUM Ns s
145from and to a format that consists of the number's length in bytes
146represented as a 4-byte big-endian number, and the number itself in
147big-endian format, where the most significant bit signals a negative
148number (the representation of numbers with the MSB set is prefixed with
149a NUL byte).
150.Pp
151.Fn BN_bn2mpi
152stores the representation of
153.Fa a
154at
155.Fa to ,
156where
157.Fa to
158must be large enough to hold the result.
159The size can be determined by calling
160.Fn BN_bn2mpi a , NULL .
161.Pp
162.Fn BN_mpi2bn
163converts the
164.Fa len
165bytes long representation at
166.Fa s
167to a
168.Vt BIGNUM
169and stores it at
170.Fa ret ,
171or in a newly allocated
172.Vt BIGNUM
173if
174.Fa ret
175is
176.Dv NULL .
177.Sh RETURN VALUES
178.Fn BN_bn2bin
179returns the length of the big-endian number placed at
180.Fa to .
181.Fn BN_bin2bn
182returns the
183.Vt BIGNUM ,
184or
185.Dv NULL
186on error.
187.Pp
188.Fn BN_bn2hex
189and
190.Fn BN_bn2dec
191return a NUL-terminated string, or
192.Dv NULL
193on error.
194.Fn BN_hex2bn
195and
196.Fn BN_dec2bn
197return the number's length in hexadecimal or decimal digits, and 0 on
198error.
199.Pp
200.Fn BN_print_fp
201and
202.Fn BN_print
203return 1 on success, 0 on write errors.
204.Pp
205.Fn BN_bn2mpi
206returns the length of the representation.
207.Fn BN_mpi2bn
208returns the
209.Vt BIGNUM ,
210or
211.Dv NULL
212on error.
213.Pp
214The error codes can be obtained by
215.Xr ERR_get_error 3 .
216.Sh SEE ALSO
217.Xr ASN1_INTEGER_to_BN 3 ,
218.Xr bn 3 ,
219.Xr BN_num_bytes 3 ,
220.Xr BN_zero 3 ,
221.Xr ERR_get_error 3
222.Sh HISTORY
223.Fn BN_bn2bin ,
224.Fn BN_bin2bn ,
225.Fn BN_print_fp ,
226and
227.Fn BN_print
228are available in all versions of SSLeay and OpenSSL.
229.Pp
230.Fn BN_bn2hex ,
231.Fn BN_bn2dec ,
232.Fn BN_hex2bn ,
233.Fn BN_dec2bn ,
234.Fn BN_bn2mpi ,
235and
236.Fn BN_mpi2bn
237were added in SSLeay 0.9.0.
238