| 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 <limits.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/err.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
|
| 12 |
#include "internal.h" |
| 13 |
#include "../delocate.h" |
| 14 |
|
| 15 |
|
| 16 |
// BN_MAX_WORDS is the maximum number of words allowed in a |BIGNUM|. It is |
| 17 |
// sized so byte and bit counts of a |BIGNUM| always fit in |int|, with room to |
| 18 |
// spare. |
| 19 |
#define BN_MAX_WORDS (INT_MAX / (4 * BN_BITS2)) |
| 20 |
|
| 21 |
BIGNUM *BN_new(void) { |
| 22 |
BIGNUM *bn = OPENSSL_zalloc(sizeof(BIGNUM)); |
| 23 |
|
| 24 |
if (bn == NULL) { |
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
bn->flags = BN_FLG_MALLOCED; |
| 29 |
|
| 30 |
return bn; |
| 31 |
} |
| 32 |
|
| 33 |
BIGNUM *BN_secure_new(void) { return BN_new(); } |
| 34 |
|
| 35 |
void BN_init(BIGNUM *bn) { |
| 36 |
OPENSSL_memset(bn, 0, sizeof(BIGNUM)); |
| 37 |
} |
| 38 |
|
| 39 |
void BN_free(BIGNUM *bn) { |
| 40 |
if (bn == NULL) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if ((bn->flags & BN_FLG_STATIC_DATA) == 0) { |
| 45 |
OPENSSL_free(bn->d); |
| 46 |
} |
| 47 |
|
| 48 |
if (bn->flags & BN_FLG_MALLOCED) { |
| 49 |
OPENSSL_free(bn); |
| 50 |
} else { |
| 51 |
bn->d = NULL; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void BN_clear_free(BIGNUM *bn) { |
| 56 |
BN_free(bn); |
| 57 |
} |
| 58 |
|
| 59 |
BIGNUM *BN_dup(const BIGNUM *src) { |
| 60 |
BIGNUM *copy; |
| 61 |
|
| 62 |
if (src == NULL) { |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
copy = BN_new(); |
| 67 |
if (copy == NULL) { |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
if (!BN_copy(copy, src)) { |
| 72 |
BN_free(copy); |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
return copy; |
| 77 |
} |
| 78 |
|
| 79 |
BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) { |
| 80 |
if (src == dest) { |
| 81 |
return dest; |
| 82 |
} |
| 83 |
|
| 84 |
if (!bn_wexpand(dest, src->width)) { |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->width); |
| 89 |
|
| 90 |
dest->width = src->width; |
| 91 |
dest->neg = src->neg; |
| 92 |
return dest; |
| 93 |
} |
| 94 |
|
| 95 |
void BN_clear(BIGNUM *bn) { |
| 96 |
if (bn->d != NULL) { |
| 97 |
OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0])); |
| 98 |
} |
| 99 |
|
| 100 |
bn->width = 0; |
| 101 |
bn->neg = 0; |
| 102 |
} |
| 103 |
|
| 104 |
DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) { |
| 105 |
static const BN_ULONG kOneLimbs[1] = { 1 }; |
| 106 |
out->d = (BN_ULONG*) kOneLimbs; |
| 107 |
out->width = 1; |
| 108 |
out->dmax = 1; |
| 109 |
out->neg = 0; |
| 110 |
out->flags = BN_FLG_STATIC_DATA; |
| 111 |
} |
| 112 |
|
| 113 |
// BN_num_bits_word returns the minimum number of bits needed to represent the |
| 114 |
// value in |l|. |
| 115 |
unsigned BN_num_bits_word(BN_ULONG l) { |
| 116 |
// |BN_num_bits| is often called on RSA prime factors. These have public bit |
| 117 |
// lengths, but all bits beyond the high bit are secret, so count bits in |
| 118 |
// constant time. |
| 119 |
BN_ULONG x, mask; |
| 120 |
int bits = (l != 0); |
| 121 |
|
| 122 |
#if BN_BITS2 > 32 |
| 123 |
// Look at the upper half of |x|. |x| is at most 64 bits long. |
| 124 |
x = l >> 32; |
| 125 |
// Set |mask| to all ones if |x| (the top 32 bits of |l|) is non-zero and all |
| 126 |
// all zeros otherwise. |
| 127 |
mask = 0u - x; |
| 128 |
mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 129 |
// If |x| is non-zero, the lower half is included in the bit count in full, |
| 130 |
// and we count the upper half. Otherwise, we count the lower half. |
| 131 |
bits += 32 & mask; |
| 132 |
l ^= (x ^ l) & mask; // |l| is |x| if |mask| and remains |l| otherwise. |
| 133 |
#endif |
| 134 |
|
| 135 |
// The remaining blocks are analogous iterations at lower powers of two. |
| 136 |
x = l >> 16; |
| 137 |
mask = 0u - x; |
| 138 |
mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 139 |
bits += 16 & mask; |
| 140 |
l ^= (x ^ l) & mask; |
| 141 |
|
| 142 |
x = l >> 8; |
| 143 |
mask = 0u - x; |
| 144 |
mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 145 |
bits += 8 & mask; |
| 146 |
l ^= (x ^ l) & mask; |
| 147 |
|
| 148 |
x = l >> 4; |
| 149 |
mask = 0u - x; |
| 150 |
mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 151 |
bits += 4 & mask; |
| 152 |
l ^= (x ^ l) & mask; |
| 153 |
|
| 154 |
x = l >> 2; |
| 155 |
mask = 0u - x; |
| 156 |
mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 157 |
bits += 2 & mask; |
| 158 |
l ^= (x ^ l) & mask; |
| 159 |
|
| 160 |
x = l >> 1; |
| 161 |
mask = 0u - x; |
| 162 |
mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 163 |
bits += 1 & mask; |
| 164 |
|
| 165 |
return bits; |
| 166 |
} |
| 167 |
|
| 168 |
// |BN_num_bits| and |BN_num_bytes| return ints in OpenSSL. In theory, a swap |
| 169 |
// from OpenSSL to AWS-LC is not type-safe. However, in practice, the bit length |
| 170 |
// should be representable by a int due to |BN_MAX_WORDS|. The maximum bit size |
| 171 |
// of a BIGNUM is right-shifted by 2. If bit-sizes can be |
| 172 |
// represented by an int, so the byte/word size. |
| 173 |
unsigned BN_num_bits(const BIGNUM *bn) { |
| 174 |
const int width = bn_minimal_width(bn); |
| 175 |
if (width == 0) { |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
return (width - 1) * BN_BITS2 + BN_num_bits_word(bn->d[width - 1]); |
| 180 |
} |
| 181 |
|
| 182 |
unsigned BN_num_bytes(const BIGNUM *bn) { |
| 183 |
return (BN_num_bits(bn) + 7) / 8; |
| 184 |
} |
| 185 |
|
| 186 |
// ibmtpm performs a direct check of output to a signed value. This won't work |
| 187 |
// if below returns an unsigned value. Hence, the int return type. |
| 188 |
int BN_get_minimal_width(const BIGNUM *bn) { |
| 189 |
return bn_minimal_width(bn); |
| 190 |
} |
| 191 |
|
| 192 |
void BN_zero(BIGNUM *bn) { |
| 193 |
bn->width = bn->neg = 0; |
| 194 |
} |
| 195 |
|
| 196 |
int BN_one(BIGNUM *bn) { |
| 197 |
return BN_set_word(bn, 1); |
| 198 |
} |
| 199 |
|
| 200 |
int BN_set_word(BIGNUM *bn, BN_ULONG value) { |
| 201 |
if (value == 0) { |
| 202 |
BN_zero(bn); |
| 203 |
return 1; |
| 204 |
} |
| 205 |
|
| 206 |
if (!bn_wexpand(bn, 1)) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
bn->neg = 0; |
| 211 |
bn->d[0] = value; |
| 212 |
bn->width = 1; |
| 213 |
return 1; |
| 214 |
} |
| 215 |
|
| 216 |
int BN_set_u64(BIGNUM *bn, uint64_t value) { |
| 217 |
#if BN_BITS2 == 64 |
| 218 |
return BN_set_word(bn, value); |
| 219 |
#elif BN_BITS2 == 32 |
| 220 |
if (value <= BN_MASK2) { |
| 221 |
return BN_set_word(bn, (BN_ULONG)value); |
| 222 |
} |
| 223 |
|
| 224 |
if (!bn_wexpand(bn, 2)) { |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
bn->neg = 0; |
| 229 |
bn->d[0] = (BN_ULONG)value; |
| 230 |
bn->d[1] = (BN_ULONG)(value >> 32); |
| 231 |
bn->width = 2; |
| 232 |
return 1; |
| 233 |
#else |
| 234 |
#error "BN_BITS2 must be 32 or 64." |
| 235 |
#endif |
| 236 |
} |
| 237 |
|
| 238 |
int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { |
| 239 |
if (!bn_wexpand(bn, num)) { |
| 240 |
return 0; |
| 241 |
} |
| 242 |
OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG)); |
| 243 |
// |bn_wexpand| verified that |num| isn't too large. |
| 244 |
bn->width = (int)num; |
| 245 |
bn->neg = 0; |
| 246 |
return 1; |
| 247 |
} |
| 248 |
|
| 249 |
void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { |
| 250 |
if ((bn->flags & BN_FLG_STATIC_DATA) == 0) { |
| 251 |
OPENSSL_free(bn->d); |
| 252 |
} |
| 253 |
bn->d = (BN_ULONG *)words; |
| 254 |
|
| 255 |
assert(num <= BN_MAX_WORDS); |
| 256 |
bn->width = (int)num; |
| 257 |
bn->dmax = (int)num; |
| 258 |
bn->neg = 0; |
| 259 |
bn->flags |= BN_FLG_STATIC_DATA; |
| 260 |
} |
| 261 |
|
| 262 |
int bn_fits_in_words(const BIGNUM *bn, size_t num) { |
| 263 |
// All words beyond |num| must be zero. |
| 264 |
BN_ULONG mask = 0; |
| 265 |
for (size_t i = num; i < (size_t)bn->width; i++) { |
| 266 |
mask |= bn->d[i]; |
| 267 |
} |
| 268 |
return mask == 0; |
| 269 |
} |
| 270 |
|
| 271 |
int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn) { |
| 272 |
if (bn->neg) { |
| 273 |
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
size_t width = (size_t)bn->width; |
| 278 |
if (width > num) { |
| 279 |
if (!bn_fits_in_words(bn, num)) { |
| 280 |
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
width = num; |
| 284 |
} |
| 285 |
|
| 286 |
OPENSSL_memset(out, 0, sizeof(BN_ULONG) * num); |
| 287 |
OPENSSL_memcpy(out, bn->d, sizeof(BN_ULONG) * width); |
| 288 |
return 1; |
| 289 |
} |
| 290 |
|
| 291 |
int BN_is_negative(const BIGNUM *bn) { |
| 292 |
return bn->neg != 0; |
| 293 |
} |
| 294 |
|
| 295 |
void BN_set_negative(BIGNUM *bn, int sign) { |
| 296 |
if (sign && !BN_is_zero(bn)) { |
| 297 |
bn->neg = 1; |
| 298 |
} else { |
| 299 |
bn->neg = 0; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
int bn_wexpand(BIGNUM *bn, size_t words) { |
| 304 |
BN_ULONG *a; |
| 305 |
|
| 306 |
if (words <= (size_t)bn->dmax) { |
| 307 |
return 1; |
| 308 |
} |
| 309 |
|
| 310 |
if (words > BN_MAX_WORDS) { |
| 311 |
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
if (bn->flags & BN_FLG_STATIC_DATA) { |
| 316 |
OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
a = OPENSSL_calloc(words, sizeof(BN_ULONG)); |
| 321 |
if (a == NULL) { |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->width); |
| 326 |
|
| 327 |
OPENSSL_free(bn->d); |
| 328 |
bn->d = a; |
| 329 |
bn->dmax = (int)words; |
| 330 |
|
| 331 |
return 1; |
| 332 |
} |
| 333 |
|
| 334 |
int bn_expand(BIGNUM *bn, size_t bits) { |
| 335 |
if (bits + BN_BITS2 - 1 < bits) { |
| 336 |
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2); |
| 340 |
} |
| 341 |
|
| 342 |
int bn_resize_words(BIGNUM *bn, size_t words) { |
| 343 |
#if (defined(OPENSSL_PPC64LE) || defined(OPENSSL_PPC64BE)) && defined(__clang__) && __clang_major__ < 10 |
| 344 |
// This is a workaround for a miscompilation bug in Clang 7.0.1 on POWER. |
| 345 |
// The unittests catch the miscompilation, if it occurs, and it manifests |
| 346 |
// as a crash in |bn_fits_in_words|. |
| 347 |
// |
| 348 |
// The bug only triggers if building in FIPS mode and with -O3. Clang 8.0.1 |
| 349 |
// has the same bug but this workaround is not effective there---I've not |
| 350 |
// been able to find a workaround for 8.0.1. |
| 351 |
// |
| 352 |
// At the time of writing (2019-08-08), Clang git does *not* have this bug |
| 353 |
// and does not need this workaroud. The current git version should go on to |
| 354 |
// be Clang 10 thus, once we can depend on that, this can be removed. |
| 355 |
if (value_barrier_w((size_t)bn->width == words)) { |
| 356 |
return 1; |
| 357 |
} |
| 358 |
#endif |
| 359 |
|
| 360 |
if ((size_t)bn->width <= words) { |
| 361 |
if (!bn_wexpand(bn, words)) { |
| 362 |
return 0; |
| 363 |
} |
| 364 |
OPENSSL_memset(bn->d + bn->width, 0, |
| 365 |
(words - bn->width) * sizeof(BN_ULONG)); |
| 366 |
bn->width = (int)words; |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
// All words beyond the new width must be zero. |
| 371 |
if (!bn_fits_in_words(bn, words)) { |
| 372 |
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
bn->width = (int)words; |
| 376 |
return 1; |
| 377 |
} |
| 378 |
|
| 379 |
void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a, |
| 380 |
const BN_ULONG *b, size_t num) { |
| 381 |
for (size_t i = 0; i < num; i++) { |
| 382 |
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 383 |
crypto_word_t_is_too_small) |
| 384 |
r[i] = constant_time_select_w(mask, a[i], b[i]); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
int bn_minimal_width(const BIGNUM *bn) { |
| 389 |
int ret = bn->width; |
| 390 |
while (ret > 0 && bn->d[ret - 1] == 0) { |
| 391 |
ret--; |
| 392 |
} |
| 393 |
return ret; |
| 394 |
} |
| 395 |
|
| 396 |
void bn_set_minimal_width(BIGNUM *bn) { |
| 397 |
bn->width = bn_minimal_width(bn); |
| 398 |
if (bn->width == 0) { |
| 399 |
bn->neg = 0; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
int BN_get_flags(const BIGNUM *bn, int flags) { |
| 404 |
return bn->flags & flags; |
| 405 |
} |
| 406 |
|
| 407 |
void BN_set_flags(BIGNUM *b, int n) { } |