1.\" $NetBSD: libnetpgpbn.3,v 1.4 2014/02/17 07:23:18 agc Exp $
2.\"
3.\" Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.Dd February 16, 2014
27.Dt LIBNETPGPBN 3
28.Os
29.Sh NAME
30.Nm libnetpgpbn
31.Nd BIGNUM library of multi-precision integers
32.Sh LIBRARY
33.Lb libnetpgpbn
34.Sh SYNOPSIS
35.In netpgp/bn.h
36.Ft BIGNUM *
37.Fo BN_new
38.Fa "void"
39.Fc
40.Ft BIGNUM *
41.Fo BN_dup
42.Fa "const BIGNUM *orig"
43.Fc
44.Ft int
45.Fo BN_copy
46.Fa "BIGNUM *to" "const BIGNUM *from"
47.Fc
48.Ft void
49.Fo BN_swap
50.Fa "BIGNUM *a" "BIGNUM *b"
51.Fc
52.Pp
53.Ft void
54.Fo BN_init
55.Fa "BIGNUM *bn"
56.Fc
57.Ft void
58.Fo BN_free
59.Fa "BIGNUM *bn"
60.Fc
61.Ft void
62.Fo BN_clear
63.Fa "BIGNUM *bn"
64.Fc
65.Ft void
66.Fo BN_clear_free
67.Fa "BIGNUM *bn"
68.Fc
69.Pp
70.Ft void
71.Fo BN_clear_free
72.Fa "BIGNUM *bn"
73.Fc
74.Pp
75.Ft int
76.Fo BN_cmp
77.Fa "BIGNUM *lhs" "BIGNUM *rhs"
78.Fc
79.Ft int
80.Fo BN_is_negative
81.Fa "BIGNUM *bn"
82.Fc
83.Ft int
84.Fo BN_is_zero
85.Fa "BIGNUM *bn"
86.Fc
87.Ft int
88.Fo BN_is_odd
89.Fa "BIGNUM *bn"
90.Fc
91.Ft int
92.Fo BN_is_even
93.Fa "BIGNUM *bn"
94.Fc
95.Pp
96.Ft BIGNUM *
97.Fo BN_bin2bn
98.Fa "const uint8_t *buf" "int size" "BIGNUM *bn"
99.Fc
100.Ft int
101.Fo BN_bn2bin
102.Fa "BIGNUM *bn" "uint8_t *buf"
103.Fc
104.Ft int
105.Fo BN_bn2bin
106.Fa "BIGNUM *bn" "uint8_t *buf"
107.Fc
108.Ft char *
109.Fo BN_bn2hex
110.Fa "const BIGNUM *bn"
111.Fc
112.Ft char *
113.Fo BN_bn2dec
114.Fa "const BIGNUM *bn"
115.Fc
116.Ft int
117.Fo BN_hex2bn
118.Fa "BIGNUM **bn" "const char *str"
119.Fc
120.Ft int
121.Fo BN_dec2bn
122.Fa "BIGNUM **bn" "const char *str"
123.Fc
124.Ft int
125.Fo BN_print_fp
126.Fa "FILE *fp" "const BIGNUM *bn"
127.Fc
128.Pp
129.Ft int
130.Fo BN_add
131.Fa "BIGNUM *sum" "const BIGNUM *a" "const BIGNUM *b"
132.Fc
133.Ft int
134.Fo BN_sub
135.Fa "BIGNUM *sum" "const BIGNUM *a" "const BIGNUM *b"
136.Fc
137.Ft int
138.Fo BN_mul
139.Fa "BIGNUM *product" "const BIGNUM *a" "const BIGNUM *b" "BN_CTX *context"
140.Fc
141.Ft int
142.Fo BN_div
143.Fa "BIGNUM *quotient" "BIGNUM *remainder" "const BIGNUM *a" "const BIGNUM *b" "BN_CTX *context"
144.Fc
145.Pp
146.Ft int
147.Fo BN_lshift
148.Fa "BIGNUM *result" "const BIGNUM *bn" "int n"
149.Fc
150.Ft int
151.Fo BN_lshift1
152.Fa "BIGNUM *result" "const BIGNUM *bn"
153.Fc
154.Ft int
155.Fo BN_rshift
156.Fa "BIGNUM *result" "const BIGNUM *bn" "int n"
157.Fc
158.Ft int
159.Fo BN_rshift1
160.Fa "BIGNUM *result" "const BIGNUM *bn"
161.Fc
162.Pp
163.Ft int
164.Fo BN_set_word
165.Fa "BIGNUM *result" "unsigned long val"
166.Fc
167.Ft int
168.Fo BN_set_negative
169.Fa "BIGNUM *result" "int val"
170.Fc
171.Pp
172.Ft int
173.Fo BN_num_bytes
174.Fa "const BIGNUM *bn"
175.Fc
176.Ft int
177.Fo BN_num_bits
178.Fa "const BIGNUM *bn"
179.Fc
180.Pp
181.Ft int
182.Fo BN_mod_exp
183.Fa "BIGNUM *result" "BIGNUM *a" "BIGNUM *p" "BIGNUM *m" "BN_CTX *context"
184.Fc
185.Ft BIGNUM *
186.Fo BN_mod_inverse
187.Fa "BIGNUM *result" "BIGNUM *a" "BIGNUM *n" "BN_CTX *context"
188.Fc
189.Ft int
190.Fo BN_mod_mul
191.Fa "BIGNUM *result" "BIGNUM *a" "BIGNUM *b" "BIGNUM *m" "BN_CTX *context"
192.Fc
193.Ft int
194.Fo BN_mod_sub
195.Fa "BIGNUM *result" "BIGNUM *a" "BIGNUM *b" "BIGNUM *m" "BN_CTX *context"
196.Fc
197.Pp
198.Ft BN_CTX *
199.Fo BN_CTX_new
200.Fa "void"
201.Fc
202.Ft BIGNUM *
203.Fo BN_CTX_get
204.Fa "BN_CTX *context"
205.Fc
206.Ft void
207.Fo BN_CTX_start
208.Fa "BN_CTX *context"
209.Fc
210.Ft void
211.Fo BN_CTX_end
212.Fa "BN_CTX *context"
213.Fc
214.Ft void
215.Fo BN_CTX_init
216.Fa "BN_CTX *context"
217.Fc
218.Ft void
219.Fo BN_CTX_free
220.Fa "BN_CTX *context"
221.Fc
222.Ft int
223.Fo BN_rand
224.Fa "BIGNUM *result" "int bits" "int top" "int bottom"
225.Fc
226.Ft int
227.Fo BN_rand_range
228.Fa "BIGNUM *result" "BIGNUM *range"
229.Fc
230.Ft int
231.Fo BN_is_prime
232.Fa "const BIGNUM *bn" "int checks" "void (*callback)(int int void)"
233.Fa "BN_CTX *context" "void *callbackarg"
234.Fc
235.Pp
236.Ft const BIGNUM *
237.Fo BN_value_one
238.Fa "void"
239.Fc
240.Ft int
241.Fo BN_is_bit_set
242.Fa "const BIGNUM *bn" "int n"
243.Fc
244.Sh DESCRIPTION
245.Nm
246emulates the API of the openssl
247.Xr bn 3
248library.
249It is implemented using Tom St Denis's
250.Dq libtommath
251library.
252.Sh EXAMPLES
253The following code fragment will make a JSON object
254out of the string
255.Dq Hello <USERNAME>\en
256in the
257buffer called
258.Va buf
259where
260.Dq USERNAME
261is the name of the user taken from the runtime environment.
262The encoded text will be in an allocated buffer called
263.Va s .
264.Bd -literal -offset indent
265mj_t atom;
266char buf[BUFSIZ];
267char *s;
268int cc;
269
270(void) memset(\*[Am]atom, 0x0, sizeof(atom));
271cc = snprintf(buf, sizeof(buf), "Hello %s\en", getenv("USER"));
272mj_create(\*[Am]atom, "string", buf, cc);
273cc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_JSON_ENCODE);
274.Ed
275.Pp
276Next, the following example will take the (binary) text which has been encoded into
277JSON and is in the buffer
278.Va buf ,
279such as in the previous example, and re-create the original text:
280.Bd -literal -offset indent
281int from, to, tok, cc;
282char *s;
283mj_t atom;
284
285(void) memset(\*[Am]atom, 0x0, sizeof(atom));
286from = to = tok = 0;
287mj_parse(\*[Am]atom, buf, \*[Am]from, \*[Am]to, \*[Am]tok);
288cc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_HUMAN);
289printf("%.*s", cc, s);
290.Ed
291.Pp
292The
293.Va s
294pointer points to allocated storage with the original NUL-terminated string
295in it.
296.Sh SEE ALSO
297.Xr bn 3
298.Sh HISTORY
299The
300.Nm
301library first appeared in
302.Nx 7.0 .
303.Sh AUTHORS
304.An Alistair Crooks Aq Mt agc@NetBSD.org .
305