summaryrefslogtreecommitdiff
path: root/lib/libcrypto/man/BN_add.3
blob: fb9563f8225e4519ac56f00df4a7b19ceb3d8c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
.\"	$OpenBSD: BN_add.3,v 1.5 2016/12/10 21:13:25 schwarze Exp $
.\"	OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100
.\"
.\" This file was written by Ulf Moeller <ulf@openssl.org>
.\" and Bodo Moeller <bodo@openssl.org>.
.\" Copyright (c) 2000, 2001, 2015 The OpenSSL Project.  All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in
.\"    the documentation and/or other materials provided with the
.\"    distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\"    software must display the following acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\"    endorse or promote products derived from this software without
.\"    prior written permission. For written permission, please contact
.\"    openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\"    nor may "OpenSSL" appear in their names without prior written
.\"    permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\"    acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: December 10 2016 $
.Dt BN_ADD 3
.Os
.Sh NAME
.Nm BN_add ,
.Nm BN_sub ,
.Nm BN_mul ,
.Nm BN_sqr ,
.Nm BN_div ,
.Nm BN_mod ,
.Nm BN_nnmod ,
.Nm BN_mod_add ,
.Nm BN_mod_sub ,
.Nm BN_mod_mul ,
.Nm BN_mod_sqr ,
.Nm BN_exp ,
.Nm BN_mod_exp ,
.Nm BN_gcd
.Nd arithmetic operations on BIGNUMs
.Sh SYNOPSIS
.In openssl/bn.h
.Ft int
.Fo BN_add
.Fa "BIGNUM *r"
.Fa "const BIGNUM *a"
.Fa "const BIGNUM *b"
.Fc
.Ft int
.Fo BN_sub
.Fa "BIGNUM *r"
.Fa "const BIGNUM *a"
.Fa "const BIGNUM *b"
.Fc
.Ft int
.Fo BN_mul
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BIGNUM *b"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_sqr
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_div
.Fa "BIGNUM *dv"
.Fa "BIGNUM *rem"
.Fa "const BIGNUM *a"
.Fa "const BIGNUM *d"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_mod
.Fa "BIGNUM *rem"
.Fa "const BIGNUM *a"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_nnmod
.Fa "BIGNUM *r"
.Fa "const BIGNUM *a"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_mod_add
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BIGNUM *b"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_mod_sub
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BIGNUM *b"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_mod_mul
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BIGNUM *b"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_mod_sqr
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_exp
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BIGNUM *p"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_mod_exp
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "const BIGNUM *p"
.Fa "const BIGNUM *m"
.Fa "BN_CTX *ctx"
.Fc
.Ft int
.Fo BN_gcd
.Fa "BIGNUM *r"
.Fa "BIGNUM *a"
.Fa "BIGNUM *b"
.Fa "BN_CTX *ctx"
.Fc
.Sh DESCRIPTION
.Fn BN_add
adds
.Fa a
and
.Fa b
and places the result in
.Fa r
.Pq Li r=a+b .
.Fa r
may be the same
.Vt BIGNUM
as
.Fa a
or
.Fa b .
.Pp
.Fn BN_sub
subtracts
.Fa b
from
.Fa a
and places the result in
.Fa r
.Pq Li r=a-b .
.Fa r
may be the same
.Vt BIGNUM
as
.Fa a
or
.Fa b .
.Pp
.Fn BN_mul
multiplies
.Fa a
and
.Fa b
and places the result in
.Fa r
.Pq Li r=a*b .
.Fa r
may be the same
.Vt BIGNUM
as
.Fa a
or
.Fa b .
For multiplication by powers of 2, use
.Xr BN_lshift 3 .
.Pp
.Fn BN_sqr
takes the square of
.Fa a
and places the result in
.Fa r
.Pq Li r=a^2 .
.Fa r
and
.Fa a
may be the same
.Vt BIGNUM .
This function is faster than
.Fn BN_mul r a a .
.Pp
.Fn BN_div
divides
.Fa a
by
.Fa d
and places the result in
.Fa dv
and the remainder in
.Fa rem
.Pq Li dv=a/d , rem=a%d .
Either of
.Fa dv
and
.Fa rem
may be
.Dv NULL ,
in which case the respective value is not returned.
The result is rounded towards zero; thus if
.Fa a
is negative, the remainder will be zero or negative.
For division by powers of 2, use
.Fn BN_rshift 3 .
.Pp
.Fn BN_mod
corresponds to
.Fn BN_div
with
.Fa dv
set to
.Dv NULL .
.Pp
.Fn BN_nnmod
reduces
.Fa a
modulo
.Fa m
and places the non-negative remainder in
.Fa r .
.Pp
.Fn BN_mod_add
adds
.Fa a
to
.Fa b
modulo
.Fa m
and places the non-negative result in
.Fa r .
.Pp
.Fn BN_mod_sub
subtracts
.Fa b
from
.Fa a
modulo
.Fa m
and places the non-negative result in
.Fa r .
.Pp
.Fn BN_mod_mul
multiplies
.Fa a
by
.Fa b
and finds the non-negative remainder respective to modulus
.Fa m
.Pq Li r=(a*b)%m .
.Fa r
may be the same
.Vt BIGNUM
as
.Fa a
or
.Fa b .
For more efficient algorithms for repeated computations using the same
modulus, see
.Xr BN_mod_mul_montgomery 3
and
.Xr BN_mod_mul_reciprocal 3 .
.Pp
.Fn BN_mod_sqr
takes the square of
.Fa a
modulo
.Fa m
and places the result in
.Fa r .
.Pp
.Fn BN_exp
raises
.Fa a
to the
.Fa p Ns -th
power and places the result in
.Fa r
.Pq Li r=a^p .
This function is faster than repeated applications of
.Fn BN_mul .
.Pp
.Fn BN_mod_exp
computes
.Fa a
to the
.Fa p Ns -th
power modulo
.Fa m
.Pq Li r=(a^p)%m .
This function uses less time and space than
.Fn BN_exp .
.Pp
.Fn BN_gcd
computes the greatest common divisor of
.Fa a
and
.Fa b
and places the result in
.Fa r .
.Fa r
may be the same
.Vt BIGNUM
as
.Fa a
or
.Fa b .
.Pp
For all functions,
.Fa ctx
is a previously allocated
.Vt BN_CTX
used for temporary variables; see
.Xr BN_CTX_new 3 .
.Pp
Unless noted otherwise, the result
.Vt BIGNUM
must be different from the arguments.
.Sh RETURN VALUES
For all functions, 1 is returned for success, 0 on error.
The return value should always be checked, for example:
.Pp
.Dl if (!BN_add(r,a,b)) goto err;
.Pp
The error codes can be obtained by
.Xr ERR_get_error 3 .
.Sh SEE ALSO
.Xr BN_add_word 3 ,
.Xr BN_CTX_new 3 ,
.Xr BN_new 3 ,
.Xr BN_set_bit 3 ,
.Xr BN_set_negative 3 ,
.Xr ERR_get_error 3
.Sh HISTORY
.Fn BN_add ,
.Fn BN_sub ,
.Fn BN_sqr ,
.Fn BN_div ,
.Fn BN_mod ,
.Fn BN_mod_mul ,
.Fn BN_mod_exp ,
and
.Fn BN_gcd
are available in all versions of SSLeay and OpenSSL.
The
.Fa ctx
argument to
.Fn BN_mul
was added in SSLeay 0.9.1b.
.Fn BN_exp
appeared in SSLeay 0.9.0.
.Fn BN_nnmod ,
.Fn BN_mod_add ,
.Fn BN_mod_sub ,
and
.Fn BN_mod_sqr
were added in OpenSSL 0.9.7.