1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson ([email protected]).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young ([email protected])"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson ([email protected])"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/bn.h>
58
59 #include <assert.h>
60 #include <limits.h>
61 #include <string.h>
62
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65
66 #include "internal.h"
67 #include "../delocate.h"
68
69
70 // BN_MAX_WORDS is the maximum number of words allowed in a |BIGNUM|. It is
71 // sized so byte and bit counts of a |BIGNUM| always fit in |int|, with room to
72 // spare.
73 #define BN_MAX_WORDS (INT_MAX / (4 * BN_BITS2))
74
BN_new(void)75 BIGNUM *BN_new(void) {
76 BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
77
78 if (bn == NULL) {
79 return NULL;
80 }
81
82 OPENSSL_memset(bn, 0, sizeof(BIGNUM));
83 bn->flags = BN_FLG_MALLOCED;
84
85 return bn;
86 }
87
BN_secure_new(void)88 BIGNUM *BN_secure_new(void) { return BN_new(); }
89
BN_init(BIGNUM * bn)90 void BN_init(BIGNUM *bn) {
91 OPENSSL_memset(bn, 0, sizeof(BIGNUM));
92 }
93
BN_free(BIGNUM * bn)94 void BN_free(BIGNUM *bn) {
95 if (bn == NULL) {
96 return;
97 }
98
99 if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
100 OPENSSL_free(bn->d);
101 }
102
103 if (bn->flags & BN_FLG_MALLOCED) {
104 OPENSSL_free(bn);
105 } else {
106 bn->d = NULL;
107 }
108 }
109
BN_clear_free(BIGNUM * bn)110 void BN_clear_free(BIGNUM *bn) {
111 BN_free(bn);
112 }
113
BN_dup(const BIGNUM * src)114 BIGNUM *BN_dup(const BIGNUM *src) {
115 BIGNUM *copy;
116
117 if (src == NULL) {
118 return NULL;
119 }
120
121 copy = BN_new();
122 if (copy == NULL) {
123 return NULL;
124 }
125
126 if (!BN_copy(copy, src)) {
127 BN_free(copy);
128 return NULL;
129 }
130
131 return copy;
132 }
133
BN_copy(BIGNUM * dest,const BIGNUM * src)134 BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) {
135 if (src == dest) {
136 return dest;
137 }
138
139 if (!bn_wexpand(dest, src->width)) {
140 return NULL;
141 }
142
143 OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->width);
144
145 dest->width = src->width;
146 dest->neg = src->neg;
147 return dest;
148 }
149
BN_clear(BIGNUM * bn)150 void BN_clear(BIGNUM *bn) {
151 if (bn->d != NULL) {
152 OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0]));
153 }
154
155 bn->width = 0;
156 bn->neg = 0;
157 }
158
DEFINE_METHOD_FUNCTION(BIGNUM,BN_value_one)159 DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) {
160 static const BN_ULONG kOneLimbs[1] = { 1 };
161 out->d = (BN_ULONG*) kOneLimbs;
162 out->width = 1;
163 out->dmax = 1;
164 out->neg = 0;
165 out->flags = BN_FLG_STATIC_DATA;
166 }
167
168 // BN_num_bits_word returns the minimum number of bits needed to represent the
169 // value in |l|.
BN_num_bits_word(BN_ULONG l)170 unsigned BN_num_bits_word(BN_ULONG l) {
171 // |BN_num_bits| is often called on RSA prime factors. These have public bit
172 // lengths, but all bits beyond the high bit are secret, so count bits in
173 // constant time.
174 BN_ULONG x, mask;
175 int bits = (l != 0);
176
177 #if BN_BITS2 > 32
178 // Look at the upper half of |x|. |x| is at most 64 bits long.
179 x = l >> 32;
180 // Set |mask| to all ones if |x| (the top 32 bits of |l|) is non-zero and all
181 // all zeros otherwise.
182 mask = 0u - x;
183 mask = (0u - (mask >> (BN_BITS2 - 1)));
184 // If |x| is non-zero, the lower half is included in the bit count in full,
185 // and we count the upper half. Otherwise, we count the lower half.
186 bits += 32 & mask;
187 l ^= (x ^ l) & mask; // |l| is |x| if |mask| and remains |l| otherwise.
188 #endif
189
190 // The remaining blocks are analogous iterations at lower powers of two.
191 x = l >> 16;
192 mask = 0u - x;
193 mask = (0u - (mask >> (BN_BITS2 - 1)));
194 bits += 16 & mask;
195 l ^= (x ^ l) & mask;
196
197 x = l >> 8;
198 mask = 0u - x;
199 mask = (0u - (mask >> (BN_BITS2 - 1)));
200 bits += 8 & mask;
201 l ^= (x ^ l) & mask;
202
203 x = l >> 4;
204 mask = 0u - x;
205 mask = (0u - (mask >> (BN_BITS2 - 1)));
206 bits += 4 & mask;
207 l ^= (x ^ l) & mask;
208
209 x = l >> 2;
210 mask = 0u - x;
211 mask = (0u - (mask >> (BN_BITS2 - 1)));
212 bits += 2 & mask;
213 l ^= (x ^ l) & mask;
214
215 x = l >> 1;
216 mask = 0u - x;
217 mask = (0u - (mask >> (BN_BITS2 - 1)));
218 bits += 1 & mask;
219
220 return bits;
221 }
222
BN_num_bits(const BIGNUM * bn)223 unsigned BN_num_bits(const BIGNUM *bn) {
224 const int width = bn_minimal_width(bn);
225 if (width == 0) {
226 return 0;
227 }
228
229 return (width - 1) * BN_BITS2 + BN_num_bits_word(bn->d[width - 1]);
230 }
231
BN_num_bytes(const BIGNUM * bn)232 unsigned BN_num_bytes(const BIGNUM *bn) {
233 return (BN_num_bits(bn) + 7) / 8;
234 }
235
BN_zero(BIGNUM * bn)236 void BN_zero(BIGNUM *bn) {
237 bn->width = bn->neg = 0;
238 }
239
BN_one(BIGNUM * bn)240 int BN_one(BIGNUM *bn) {
241 return BN_set_word(bn, 1);
242 }
243
BN_set_word(BIGNUM * bn,BN_ULONG value)244 int BN_set_word(BIGNUM *bn, BN_ULONG value) {
245 if (value == 0) {
246 BN_zero(bn);
247 return 1;
248 }
249
250 if (!bn_wexpand(bn, 1)) {
251 return 0;
252 }
253
254 bn->neg = 0;
255 bn->d[0] = value;
256 bn->width = 1;
257 return 1;
258 }
259
BN_set_u64(BIGNUM * bn,uint64_t value)260 int BN_set_u64(BIGNUM *bn, uint64_t value) {
261 #if BN_BITS2 == 64
262 return BN_set_word(bn, value);
263 #elif BN_BITS2 == 32
264 if (value <= BN_MASK2) {
265 return BN_set_word(bn, (BN_ULONG)value);
266 }
267
268 if (!bn_wexpand(bn, 2)) {
269 return 0;
270 }
271
272 bn->neg = 0;
273 bn->d[0] = (BN_ULONG)value;
274 bn->d[1] = (BN_ULONG)(value >> 32);
275 bn->width = 2;
276 return 1;
277 #else
278 #error "BN_BITS2 must be 32 or 64."
279 #endif
280 }
281
bn_set_words(BIGNUM * bn,const BN_ULONG * words,size_t num)282 int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
283 if (!bn_wexpand(bn, num)) {
284 return 0;
285 }
286 OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG));
287 // |bn_wexpand| verified that |num| isn't too large.
288 bn->width = (int)num;
289 bn->neg = 0;
290 return 1;
291 }
292
bn_set_static_words(BIGNUM * bn,const BN_ULONG * words,size_t num)293 void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
294 if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
295 OPENSSL_free(bn->d);
296 }
297 bn->d = (BN_ULONG *)words;
298
299 assert(num <= BN_MAX_WORDS);
300 bn->width = (int)num;
301 bn->dmax = (int)num;
302 bn->neg = 0;
303 bn->flags |= BN_FLG_STATIC_DATA;
304 }
305
bn_fits_in_words(const BIGNUM * bn,size_t num)306 int bn_fits_in_words(const BIGNUM *bn, size_t num) {
307 // All words beyond |num| must be zero.
308 BN_ULONG mask = 0;
309 for (size_t i = num; i < (size_t)bn->width; i++) {
310 mask |= bn->d[i];
311 }
312 return mask == 0;
313 }
314
bn_copy_words(BN_ULONG * out,size_t num,const BIGNUM * bn)315 int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn) {
316 if (bn->neg) {
317 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
318 return 0;
319 }
320
321 size_t width = (size_t)bn->width;
322 if (width > num) {
323 if (!bn_fits_in_words(bn, num)) {
324 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
325 return 0;
326 }
327 width = num;
328 }
329
330 OPENSSL_memset(out, 0, sizeof(BN_ULONG) * num);
331 OPENSSL_memcpy(out, bn->d, sizeof(BN_ULONG) * width);
332 return 1;
333 }
334
BN_is_negative(const BIGNUM * bn)335 int BN_is_negative(const BIGNUM *bn) {
336 return bn->neg != 0;
337 }
338
BN_set_negative(BIGNUM * bn,int sign)339 void BN_set_negative(BIGNUM *bn, int sign) {
340 if (sign && !BN_is_zero(bn)) {
341 bn->neg = 1;
342 } else {
343 bn->neg = 0;
344 }
345 }
346
bn_wexpand(BIGNUM * bn,size_t words)347 int bn_wexpand(BIGNUM *bn, size_t words) {
348 BN_ULONG *a;
349
350 if (words <= (size_t)bn->dmax) {
351 return 1;
352 }
353
354 if (words > BN_MAX_WORDS) {
355 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
356 return 0;
357 }
358
359 if (bn->flags & BN_FLG_STATIC_DATA) {
360 OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
361 return 0;
362 }
363
364 a = OPENSSL_calloc(words, sizeof(BN_ULONG));
365 if (a == NULL) {
366 return 0;
367 }
368
369 OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->width);
370
371 OPENSSL_free(bn->d);
372 bn->d = a;
373 bn->dmax = (int)words;
374
375 return 1;
376 }
377
bn_expand(BIGNUM * bn,size_t bits)378 int bn_expand(BIGNUM *bn, size_t bits) {
379 if (bits + BN_BITS2 - 1 < bits) {
380 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
381 return 0;
382 }
383 return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
384 }
385
bn_resize_words(BIGNUM * bn,size_t words)386 int bn_resize_words(BIGNUM *bn, size_t words) {
387 if ((size_t)bn->width <= words) {
388 if (!bn_wexpand(bn, words)) {
389 return 0;
390 }
391 OPENSSL_memset(bn->d + bn->width, 0,
392 (words - bn->width) * sizeof(BN_ULONG));
393 bn->width = (int)words;
394 return 1;
395 }
396
397 // All words beyond the new width must be zero.
398 if (!bn_fits_in_words(bn, words)) {
399 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
400 return 0;
401 }
402 bn->width = (int)words;
403 return 1;
404 }
405
bn_select_words(BN_ULONG * r,BN_ULONG mask,const BN_ULONG * a,const BN_ULONG * b,size_t num)406 void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a,
407 const BN_ULONG *b, size_t num) {
408 for (size_t i = 0; i < num; i++) {
409 static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
410 "crypto_word_t is too small");
411 r[i] = constant_time_select_w(mask, a[i], b[i]);
412 }
413 }
414
bn_minimal_width(const BIGNUM * bn)415 int bn_minimal_width(const BIGNUM *bn) {
416 int ret = bn->width;
417 while (ret > 0 && bn->d[ret - 1] == 0) {
418 ret--;
419 }
420 return ret;
421 }
422
bn_set_minimal_width(BIGNUM * bn)423 void bn_set_minimal_width(BIGNUM *bn) {
424 bn->width = bn_minimal_width(bn);
425 if (bn->width == 0) {
426 bn->neg = 0;
427 }
428 }
429