| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/bn.h> |
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/type_check.h> |
| 10 |
|
| 11 |
#include "internal.h" |
| 12 |
|
| 13 |
|
| 14 |
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { |
| 15 |
int i, nw, lb, rb; |
| 16 |
BN_ULONG *t, *f; |
| 17 |
BN_ULONG l; |
| 18 |
|
| 19 |
if (n < 0) { |
| 20 |
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 21 |
return 0; |
| 22 |
} |
| 23 |
|
| 24 |
r->neg = a->neg; |
| 25 |
nw = n / BN_BITS2; |
| 26 |
if (!bn_wexpand(r, a->width + nw + 1)) { |
| 27 |
return 0; |
| 28 |
} |
| 29 |
lb = n % BN_BITS2; |
| 30 |
rb = BN_BITS2 - lb; |
| 31 |
f = a->d; |
| 32 |
t = r->d; |
| 33 |
t[a->width + nw] = 0; |
| 34 |
if (lb == 0) { |
| 35 |
for (i = a->width - 1; i >= 0; i--) { |
| 36 |
t[nw + i] = f[i]; |
| 37 |
} |
| 38 |
} else { |
| 39 |
for (i = a->width - 1; i >= 0; i--) { |
| 40 |
l = f[i]; |
| 41 |
t[nw + i + 1] |= l >> rb; |
| 42 |
t[nw + i] = l << lb; |
| 43 |
} |
| 44 |
} |
| 45 |
OPENSSL_memset(t, 0, nw * sizeof(t[0])); |
| 46 |
r->width = a->width + nw + 1; |
| 47 |
bn_set_minimal_width(r); |
| 48 |
|
| 49 |
return 1; |
| 50 |
} |
| 51 |
|
| 52 |
int BN_lshift1(BIGNUM *r, const BIGNUM *a) { |
| 53 |
BN_ULONG *ap, *rp, t, c; |
| 54 |
int i; |
| 55 |
|
| 56 |
if (r != a) { |
| 57 |
r->neg = a->neg; |
| 58 |
if (!bn_wexpand(r, a->width + 1)) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
r->width = a->width; |
| 62 |
} else { |
| 63 |
if (!bn_wexpand(r, a->width + 1)) { |
| 64 |
return 0; |
| 65 |
} |
| 66 |
} |
| 67 |
ap = a->d; |
| 68 |
rp = r->d; |
| 69 |
c = 0; |
| 70 |
for (i = 0; i < a->width; i++) { |
| 71 |
t = *(ap++); |
| 72 |
*(rp++) = (t << 1) | c; |
| 73 |
c = t >> (BN_BITS2 - 1); |
| 74 |
} |
| 75 |
if (c) { |
| 76 |
*rp = 1; |
| 77 |
r->width++; |
| 78 |
} |
| 79 |
|
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift, |
| 84 |
size_t num) { |
| 85 |
unsigned shift_bits = shift % BN_BITS2; |
| 86 |
size_t shift_words = shift / BN_BITS2; |
| 87 |
if (shift_words >= num) { |
| 88 |
OPENSSL_memset(r, 0, num * sizeof(BN_ULONG)); |
| 89 |
return; |
| 90 |
} |
| 91 |
if (shift_bits == 0) { |
| 92 |
OPENSSL_memmove(r, a + shift_words, (num - shift_words) * sizeof(BN_ULONG)); |
| 93 |
} else { |
| 94 |
for (size_t i = shift_words; i < num - 1; i++) { |
| 95 |
r[i - shift_words] = |
| 96 |
(a[i] >> shift_bits) | (a[i + 1] << (BN_BITS2 - shift_bits)); |
| 97 |
} |
| 98 |
r[num - 1 - shift_words] = a[num - 1] >> shift_bits; |
| 99 |
} |
| 100 |
OPENSSL_memset(r + num - shift_words, 0, shift_words * sizeof(BN_ULONG)); |
| 101 |
} |
| 102 |
|
| 103 |
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { |
| 104 |
if (n < 0) { |
| 105 |
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
if (!bn_wexpand(r, a->width)) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
bn_rshift_words(r->d, a->d, n, a->width); |
| 113 |
r->neg = a->neg; |
| 114 |
r->width = a->width; |
| 115 |
bn_set_minimal_width(r); |
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a, unsigned n, |
| 120 |
BN_CTX *ctx) { |
| 121 |
int ret = 0; |
| 122 |
BN_CTX_start(ctx); |
| 123 |
BIGNUM *tmp = BN_CTX_get(ctx); |
| 124 |
if (tmp == NULL || |
| 125 |
!BN_copy(r, a) || |
| 126 |
!bn_wexpand(tmp, r->width)) { |
| 127 |
goto err; |
| 128 |
} |
| 129 |
|
| 130 |
// Shift conditionally by powers of two. |
| 131 |
unsigned max_bits = BN_BITS2 * r->width; |
| 132 |
for (unsigned i = 0; (max_bits >> i) != 0; i++) { |
| 133 |
BN_ULONG mask = (n >> i) & 1; |
| 134 |
mask = 0 - mask; |
| 135 |
bn_rshift_words(tmp->d, r->d, 1u << i, r->width); |
| 136 |
bn_select_words(r->d, mask, tmp->d /* apply shift */, |
| 137 |
r->d /* ignore shift */, r->width); |
| 138 |
} |
| 139 |
|
| 140 |
ret = 1; |
| 141 |
|
| 142 |
err: |
| 143 |
BN_CTX_end(ctx); |
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num) { |
| 148 |
if (num == 0) { |
| 149 |
return; |
| 150 |
} |
| 151 |
for (size_t i = 0; i < num - 1; i++) { |
| 152 |
r[i] = (a[i] >> 1) | (a[i + 1] << (BN_BITS2 - 1)); |
| 153 |
} |
| 154 |
r[num - 1] = a[num - 1] >> 1; |
| 155 |
} |
| 156 |
|
| 157 |
int BN_rshift1(BIGNUM *r, const BIGNUM *a) { |
| 158 |
if (!bn_wexpand(r, a->width)) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
bn_rshift1_words(r->d, a->d, a->width); |
| 162 |
r->width = a->width; |
| 163 |
r->neg = a->neg; |
| 164 |
bn_set_minimal_width(r); |
| 165 |
return 1; |
| 166 |
} |
| 167 |
|
| 168 |
int BN_set_bit(BIGNUM *a, int n) { |
| 169 |
if (n < 0) { |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
int i = n / BN_BITS2; |
| 174 |
int j = n % BN_BITS2; |
| 175 |
if (a->width <= i) { |
| 176 |
if (!bn_wexpand(a, i + 1)) { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
for (int k = a->width; k < i + 1; k++) { |
| 180 |
a->d[k] = 0; |
| 181 |
} |
| 182 |
a->width = i + 1; |
| 183 |
} |
| 184 |
|
| 185 |
a->d[i] |= (((BN_ULONG)1) << j); |
| 186 |
|
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
int BN_clear_bit(BIGNUM *a, int n) { |
| 191 |
int i, j; |
| 192 |
|
| 193 |
if (n < 0) { |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
i = n / BN_BITS2; |
| 198 |
j = n % BN_BITS2; |
| 199 |
if (a->width <= i) { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
a->d[i] &= (~(((BN_ULONG)1) << j)); |
| 204 |
bn_set_minimal_width(a); |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit) { |
| 209 |
size_t i = bit / BN_BITS2; |
| 210 |
size_t j = bit % BN_BITS2; |
| 211 |
if (i >= num) { |
| 212 |
return 0; |
| 213 |
} |
| 214 |
return (a[i] >> j) & 1; |
| 215 |
} |
| 216 |
|
| 217 |
int BN_is_bit_set(const BIGNUM *a, int n) { |
| 218 |
if (n < 0) { |
| 219 |
return 0; |
| 220 |
} |
| 221 |
return bn_is_bit_set_words(a->d, a->width, n); |
| 222 |
} |
| 223 |
|
| 224 |
int BN_mask_bits(BIGNUM *a, int n) { |
| 225 |
if (n < 0) { |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
int w = n / BN_BITS2; |
| 230 |
int b = n % BN_BITS2; |
| 231 |
if (w >= a->width) { |
| 232 |
return 1; |
| 233 |
} |
| 234 |
if (b == 0) { |
| 235 |
a->width = w; |
| 236 |
} else { |
| 237 |
a->width = w + 1; |
| 238 |
a->d[w] &= ~(BN_MASK2 << b); |
| 239 |
} |
| 240 |
|
| 241 |
bn_set_minimal_width(a); |
| 242 |
return 1; |
| 243 |
} |
| 244 |
|
| 245 |
static int bn_count_low_zero_bits_word(BN_ULONG l) { |
| 246 |
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 247 |
crypto_word_t_is_too_small) |
| 248 |
OPENSSL_STATIC_ASSERT(sizeof(int) <= sizeof(crypto_word_t), |
| 249 |
crypto_word_t_is_too_small) |
| 250 |
OPENSSL_STATIC_ASSERT(BN_BITS2 == sizeof(BN_ULONG) * 8, |
| 251 |
BN_ULONG_has_padding_bits) |
| 252 |
// C has very bizarre rules for types smaller than an int. |
| 253 |
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) >= sizeof(int), |
| 254 |
BN_ULONG_gets_promoted_to_int) |
| 255 |
|
| 256 |
crypto_word_t mask; |
| 257 |
int bits = 0; |
| 258 |
|
| 259 |
#if BN_BITS2 > 32 |
| 260 |
// Check if the lower half of |x| are all zero. |
| 261 |
mask = constant_time_is_zero_w(l << (BN_BITS2 - 32)); |
| 262 |
// If the lower half is all zeros, it is included in the bit count and we |
| 263 |
// count the upper half. Otherwise, we count the lower half. |
| 264 |
bits += 32 & mask; |
| 265 |
l = constant_time_select_w(mask, l >> 32, l); |
| 266 |
#endif |
| 267 |
|
| 268 |
// The remaining blocks are analogous iterations at lower powers of two. |
| 269 |
mask = constant_time_is_zero_w(l << (BN_BITS2 - 16)); |
| 270 |
bits += 16 & mask; |
| 271 |
l = constant_time_select_w(mask, l >> 16, l); |
| 272 |
|
| 273 |
mask = constant_time_is_zero_w(l << (BN_BITS2 - 8)); |
| 274 |
bits += 8 & mask; |
| 275 |
l = constant_time_select_w(mask, l >> 8, l); |
| 276 |
|
| 277 |
mask = constant_time_is_zero_w(l << (BN_BITS2 - 4)); |
| 278 |
bits += 4 & mask; |
| 279 |
l = constant_time_select_w(mask, l >> 4, l); |
| 280 |
|
| 281 |
mask = constant_time_is_zero_w(l << (BN_BITS2 - 2)); |
| 282 |
bits += 2 & mask; |
| 283 |
l = constant_time_select_w(mask, l >> 2, l); |
| 284 |
|
| 285 |
mask = constant_time_is_zero_w(l << (BN_BITS2 - 1)); |
| 286 |
bits += 1 & mask; |
| 287 |
|
| 288 |
return bits; |
| 289 |
} |
| 290 |
|
| 291 |
int BN_count_low_zero_bits(const BIGNUM *bn) { |
| 292 |
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 293 |
crypto_word_t_is_too_small) |
| 294 |
OPENSSL_STATIC_ASSERT(sizeof(int) <= sizeof(crypto_word_t), |
| 295 |
crypto_word_t_is_too_small) |
| 296 |
|
| 297 |
int ret = 0; |
| 298 |
crypto_word_t saw_nonzero = 0; |
| 299 |
for (int i = 0; i < bn->width; i++) { |
| 300 |
crypto_word_t nonzero = ~constant_time_is_zero_w(bn->d[i]); |
| 301 |
crypto_word_t first_nonzero = ~saw_nonzero & nonzero; |
| 302 |
saw_nonzero |= nonzero; |
| 303 |
|
| 304 |
int bits = bn_count_low_zero_bits_word(bn->d[i]); |
| 305 |
ret |= first_nonzero & (i * BN_BITS2 + bits); |
| 306 |
} |
| 307 |
|
| 308 |
// If got to the end of |bn| and saw no non-zero words, |bn| is zero. |ret| |
| 309 |
// will then remain zero. |
| 310 |
return ret; |
| 311 |
} |