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>
61 #include <openssl/err.h>
62 #include <openssl/type_check.h>
67 int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) {
73 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
79 if (!bn_wexpand(r, a->width + nw + 1)) {
88 for (i = a->width - 1; i >= 0; i--) {
92 for (i = a->width - 1; i >= 0; i--) {
94 t[nw + i + 1] |= l >> rb;
98 OPENSSL_memset(t, 0, nw * sizeof(t[0]));
99 r->width = a->width + nw + 1;
100 bn_set_minimal_width(r);
105 int BN_lshift1(BIGNUM *r, const BIGNUM *a) {
106 BN_ULONG *ap, *rp, t, c;
111 if (!bn_wexpand(r, a->width + 1)) {
116 if (!bn_wexpand(r, a->width + 1)) {
123 for (i = 0; i < a->width; i++) {
125 *(rp++) = (t << 1) | c;
126 c = t >> (BN_BITS2 - 1);
136 static void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift,
138 unsigned shift_bits = shift % BN_BITS2;
139 size_t shift_words = shift / BN_BITS2;
140 if (shift_words >= num) {
141 OPENSSL_memset(r, 0, num * sizeof(BN_ULONG));
144 if (shift_bits == 0) {
145 OPENSSL_memmove(r, a + shift_words, (num - shift_words) * sizeof(BN_ULONG));
147 for (size_t i = shift_words; i < num - 1; i++) {
149 (a[i] >> shift_bits) | (a[i + 1] << (BN_BITS2 - shift_bits));
151 r[num - 1 - shift_words] = a[num - 1] >> shift_bits;
153 OPENSSL_memset(r + num - shift_words, 0, shift_words * sizeof(BN_ULONG));
156 int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) {
158 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
162 if (!bn_wexpand(r, a->width)) {
165 bn_rshift_words(r->d, a->d, n, a->width);
168 bn_set_minimal_width(r);
172 int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a, unsigned n,
176 BIGNUM *tmp = BN_CTX_get(ctx);
179 !bn_wexpand(tmp, r->width)) {
183 // Shift conditionally by powers of two.
184 unsigned max_bits = BN_BITS2 * r->width;
185 for (unsigned i = 0; (max_bits >> i) != 0; i++) {
186 BN_ULONG mask = (n >> i) & 1;
188 bn_rshift_words(tmp->d, r->d, 1u << i, r->width);
189 bn_select_words(r->d, mask, tmp->d /* apply shift */,
190 r->d /* ignore shift */, r->width);
200 void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num) {
204 for (size_t i = 0; i < num - 1; i++) {
205 r[i] = (a[i] >> 1) | (a[i + 1] << (BN_BITS2 - 1));
207 r[num - 1] = a[num - 1] >> 1;
210 int BN_rshift1(BIGNUM *r, const BIGNUM *a) {
211 if (!bn_wexpand(r, a->width)) {
214 bn_rshift1_words(r->d, a->d, a->width);
217 bn_set_minimal_width(r);
221 int BN_set_bit(BIGNUM *a, int n) {
226 int i = n / BN_BITS2;
227 int j = n % BN_BITS2;
229 if (!bn_wexpand(a, i + 1)) {
232 for (int k = a->width; k < i + 1; k++) {
238 a->d[i] |= (((BN_ULONG)1) << j);
243 int BN_clear_bit(BIGNUM *a, int n) {
256 a->d[i] &= (~(((BN_ULONG)1) << j));
257 bn_set_minimal_width(a);
261 int bn_is_bit_set_words(const BN_ULONG *a, size_t num, unsigned bit) {
262 unsigned i = bit / BN_BITS2;
263 unsigned j = bit % BN_BITS2;
267 return (a[i] >> j) & 1;
270 int BN_is_bit_set(const BIGNUM *a, int n) {
274 return bn_is_bit_set_words(a->d, a->width, n);
277 int BN_mask_bits(BIGNUM *a, int n) {
282 int w = n / BN_BITS2;
283 int b = n % BN_BITS2;
291 a->d[w] &= ~(BN_MASK2 << b);
294 bn_set_minimal_width(a);
298 static int bn_count_low_zero_bits_word(BN_ULONG l) {
299 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
300 crypto_word_t_too_small);
301 OPENSSL_COMPILE_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
302 crypto_word_t_too_small_2);
303 OPENSSL_COMPILE_ASSERT(BN_BITS2 == sizeof(BN_ULONG) * 8,
304 bn_ulong_has_padding_bits);
305 // C has very bizarre rules for types smaller than an int.
306 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) >= sizeof(int),
307 bn_ulong_is_promoted_to_int);
313 // Check if the lower half of |x| are all zero.
314 mask = constant_time_is_zero_w(l << (BN_BITS2 - 32));
315 // If the lower half is all zeros, it is included in the bit count and we
316 // count the upper half. Otherwise, we count the lower half.
318 l = constant_time_select_w(mask, l >> 32, l);
321 // The remaining blocks are analogous iterations at lower powers of two.
322 mask = constant_time_is_zero_w(l << (BN_BITS2 - 16));
324 l = constant_time_select_w(mask, l >> 16, l);
326 mask = constant_time_is_zero_w(l << (BN_BITS2 - 8));
328 l = constant_time_select_w(mask, l >> 8, l);
330 mask = constant_time_is_zero_w(l << (BN_BITS2 - 4));
332 l = constant_time_select_w(mask, l >> 4, l);
334 mask = constant_time_is_zero_w(l << (BN_BITS2 - 2));
336 l = constant_time_select_w(mask, l >> 2, l);
338 mask = constant_time_is_zero_w(l << (BN_BITS2 - 1));
344 int BN_count_low_zero_bits(const BIGNUM *bn) {
345 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
346 crypto_word_t_too_small);
347 OPENSSL_COMPILE_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
348 crypto_word_t_too_small_2);
351 crypto_word_t saw_nonzero = 0;
352 for (int i = 0; i < bn->width; i++) {
353 crypto_word_t nonzero = ~constant_time_is_zero_w(bn->d[i]);
354 crypto_word_t first_nonzero = ~saw_nonzero & nonzero;
355 saw_nonzero |= nonzero;
357 int bits = bn_count_low_zero_bits_word(bn->d[i]);
358 ret |= first_nonzero & (i * BN_BITS2 + bits);
361 // If got to the end of |bn| and saw no non-zero words, |bn| is zero. |ret|
362 // will then remain zero.