1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
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 (tjh@cryptsoft.com).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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
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.] */
57 #include <openssl/bn.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
66 #include "../delocate.h"
69 BIGNUM *BN_new(void) {
70 BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
73 OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
77 OPENSSL_memset(bn, 0, sizeof(BIGNUM));
78 bn->flags = BN_FLG_MALLOCED;
83 void BN_init(BIGNUM *bn) {
84 OPENSSL_memset(bn, 0, sizeof(BIGNUM));
87 void BN_free(BIGNUM *bn) {
92 if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
96 if (bn->flags & BN_FLG_MALLOCED) {
103 void BN_clear_free(BIGNUM *bn) {
111 if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
114 OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0]));
118 should_free = (bn->flags & BN_FLG_MALLOCED) != 0;
122 OPENSSL_cleanse(bn, sizeof(BIGNUM));
126 BIGNUM *BN_dup(const BIGNUM *src) {
138 if (!BN_copy(copy, src)) {
146 BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) {
151 if (!bn_wexpand(dest, src->width)) {
155 OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->width);
157 dest->width = src->width;
158 dest->neg = src->neg;
162 void BN_clear(BIGNUM *bn) {
164 OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0]));
171 DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) {
172 static const BN_ULONG kOneLimbs[1] = { 1 };
173 out->d = (BN_ULONG*) kOneLimbs;
177 out->flags = BN_FLG_STATIC_DATA;
180 // BN_num_bits_word returns the minimum number of bits needed to represent the
182 unsigned BN_num_bits_word(BN_ULONG l) {
183 // |BN_num_bits| is often called on RSA prime factors. These have public bit
184 // lengths, but all bits beyond the high bit are secret, so count bits in
190 // Look at the upper half of |x|. |x| is at most 64 bits long.
192 // Set |mask| to all ones if |x| (the top 32 bits of |l|) is non-zero and all
193 // all zeros otherwise.
195 mask = (0u - (mask >> (BN_BITS2 - 1)));
196 // If |x| is non-zero, the lower half is included in the bit count in full,
197 // and we count the upper half. Otherwise, we count the lower half.
199 l ^= (x ^ l) & mask; // |l| is |x| if |mask| and remains |l| otherwise.
202 // The remaining blocks are analogous iterations at lower powers of two.
205 mask = (0u - (mask >> (BN_BITS2 - 1)));
211 mask = (0u - (mask >> (BN_BITS2 - 1)));
217 mask = (0u - (mask >> (BN_BITS2 - 1)));
223 mask = (0u - (mask >> (BN_BITS2 - 1)));
229 mask = (0u - (mask >> (BN_BITS2 - 1)));
235 unsigned BN_num_bits(const BIGNUM *bn) {
236 const int width = bn_minimal_width(bn);
241 return (width - 1) * BN_BITS2 + BN_num_bits_word(bn->d[width - 1]);
244 unsigned BN_num_bytes(const BIGNUM *bn) {
245 return (BN_num_bits(bn) + 7) / 8;
248 void BN_zero(BIGNUM *bn) {
249 bn->width = bn->neg = 0;
252 int BN_one(BIGNUM *bn) {
253 return BN_set_word(bn, 1);
256 int BN_set_word(BIGNUM *bn, BN_ULONG value) {
262 if (!bn_wexpand(bn, 1)) {
272 int BN_set_u64(BIGNUM *bn, uint64_t value) {
274 return BN_set_word(bn, value);
276 if (value <= BN_MASK2) {
277 return BN_set_word(bn, (BN_ULONG)value);
280 if (!bn_wexpand(bn, 2)) {
285 bn->d[0] = (BN_ULONG)value;
286 bn->d[1] = (BN_ULONG)(value >> 32);
290 #error "BN_BITS2 must be 32 or 64."
294 int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
295 if (!bn_wexpand(bn, num)) {
298 OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG));
299 // |bn_wexpand| verified that |num| isn't too large.
300 bn->width = (int)num;
305 int bn_fits_in_words(const BIGNUM *bn, size_t num) {
306 // All words beyond |num| must be zero.
308 for (size_t i = num; i < (size_t)bn->width; i++) {
314 int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn) {
316 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
320 size_t width = (size_t)bn->width;
322 if (!bn_fits_in_words(bn, num)) {
323 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
329 OPENSSL_memset(out, 0, sizeof(BN_ULONG) * num);
330 OPENSSL_memcpy(out, bn->d, sizeof(BN_ULONG) * width);
334 int BN_is_negative(const BIGNUM *bn) {
338 void BN_set_negative(BIGNUM *bn, int sign) {
339 if (sign && !BN_is_zero(bn)) {
346 int bn_wexpand(BIGNUM *bn, size_t words) {
349 if (words <= (size_t)bn->dmax) {
353 if (words > (INT_MAX / (4 * BN_BITS2))) {
354 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
358 if (bn->flags & BN_FLG_STATIC_DATA) {
359 OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
363 a = OPENSSL_malloc(sizeof(BN_ULONG) * words);
365 OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
369 OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->width);
373 bn->dmax = (int)words;
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);
383 return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
386 int bn_resize_words(BIGNUM *bn, size_t words) {
387 if ((size_t)bn->width <= words) {
388 if (!bn_wexpand(bn, words)) {
391 OPENSSL_memset(bn->d + bn->width, 0,
392 (words - bn->width) * sizeof(BN_ULONG));
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);
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 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
410 crypto_word_t_too_small);
411 r[i] = constant_time_select_w(mask, a[i], b[i]);
415 int bn_minimal_width(const BIGNUM *bn) {
417 while (ret > 0 && bn->d[ret - 1] == 0) {
423 void bn_set_minimal_width(BIGNUM *bn) {
424 bn->width = bn_minimal_width(bn);
425 if (bn->width == 0) {