| 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 <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
|
| 9 |
#include "internal.h" |
| 10 |
|
| 11 |
void bn_big_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in, |
| 12 |
size_t in_len) { |
| 13 |
// The caller should have sized |out| to fit |in| without truncating. This |
| 14 |
// condition ensures we do not overflow |out|, so use a runtime check. |
| 15 |
BSSL_CHECK(in_len <= out_len * sizeof(BN_ULONG)); |
| 16 |
|
| 17 |
// Load whole words. |
| 18 |
while (in_len >= sizeof(BN_ULONG)) { |
| 19 |
in_len -= sizeof(BN_ULONG); |
| 20 |
out[0] = CRYPTO_load_word_be(in + in_len); |
| 21 |
out++; |
| 22 |
out_len--; |
| 23 |
} |
| 24 |
|
| 25 |
// Load the last partial word. |
| 26 |
if (in_len != 0) { |
| 27 |
BN_ULONG word = 0; |
| 28 |
for (size_t i = 0; i < in_len; i++) { |
| 29 |
word = (word << 8) | in[i]; |
| 30 |
} |
| 31 |
out[0] = word; |
| 32 |
out++; |
| 33 |
out_len--; |
| 34 |
} |
| 35 |
|
| 36 |
// Fill the remainder with zeros. |
| 37 |
OPENSSL_memset(out, 0, out_len * sizeof(BN_ULONG)); |
| 38 |
} |
| 39 |
|
| 40 |
BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 41 |
BIGNUM *bn = NULL; |
| 42 |
if (ret == NULL) { |
| 43 |
bn = BN_new(); |
| 44 |
if (bn == NULL) { |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
ret = bn; |
| 48 |
} |
| 49 |
|
| 50 |
if (len == 0) { |
| 51 |
ret->width = 0; |
| 52 |
return ret; |
| 53 |
} |
| 54 |
|
| 55 |
size_t num_words = ((len - 1) / BN_BYTES) + 1; |
| 56 |
if (!bn_wexpand(ret, num_words)) { |
| 57 |
BN_free(bn); |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
// |bn_wexpand| must check bounds on |num_words| to write it into |
| 62 |
// |ret->dmax|. |
| 63 |
assert(num_words <= INT_MAX); |
| 64 |
ret->width = (int)num_words; |
| 65 |
ret->neg = 0; |
| 66 |
|
| 67 |
bn_big_endian_to_words(ret->d, ret->width, in, len); |
| 68 |
return ret; |
| 69 |
} |
| 70 |
|
| 71 |
BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 72 |
BIGNUM *bn = NULL; |
| 73 |
if (ret == NULL) { |
| 74 |
bn = BN_new(); |
| 75 |
if (bn == NULL) { |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
ret = bn; |
| 79 |
} |
| 80 |
|
| 81 |
if (len == 0) { |
| 82 |
ret->width = 0; |
| 83 |
ret->neg = 0; |
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
// Reserve enough space in |ret|. |
| 88 |
size_t num_words = ((len - 1) / BN_BYTES) + 1; |
| 89 |
if (!bn_wexpand(ret, num_words)) { |
| 90 |
BN_free(bn); |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
ret->width = (int)num_words; |
| 94 |
|
| 95 |
bn_little_endian_to_words(ret->d, ret->width, in, len); |
| 96 |
|
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
void bn_little_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in, const size_t in_len) { |
| 101 |
assert(out_len > 0); |
| 102 |
#ifdef OPENSSL_BIG_ENDIAN |
| 103 |
size_t in_index = 0; |
| 104 |
for (size_t i = 0; i < out_len; i++) { |
| 105 |
if ((in_len-in_index) < sizeof(BN_ULONG)) { |
| 106 |
// Load the last partial word. |
| 107 |
BN_ULONG word = 0; |
| 108 |
// size_t is unsigned, so j >= 0 is always true. |
| 109 |
for (size_t j = in_len-1; j >= in_index && j < in_len; j--) { |
| 110 |
word = (word << 8) | in[j]; |
| 111 |
} |
| 112 |
in_index = in_len; |
| 113 |
out[i] = word; |
| 114 |
|
| 115 |
// Fill the remainder with zeros. |
| 116 |
OPENSSL_memset(out + i + 1, 0, (out_len - i - 1) * sizeof(BN_ULONG)); |
| 117 |
break; |
| 118 |
} |
| 119 |
|
| 120 |
out[i] = CRYPTO_load_word_le(in + in_index); |
| 121 |
in_index += sizeof(BN_ULONG); |
| 122 |
} |
| 123 |
|
| 124 |
// The caller should have sized the output to avoid truncation. |
| 125 |
assert(in_index == in_len); |
| 126 |
#else |
| 127 |
OPENSSL_memcpy(out, in, in_len); |
| 128 |
// Fill the remainder with zeros. |
| 129 |
OPENSSL_memset( ((uint8_t*)out) + in_len, 0, sizeof(BN_ULONG)*out_len - in_len); |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
// fits_in_bytes returns one if the |num_words| words in |words| can be |
| 134 |
// represented in |num_bytes| bytes. |
| 135 |
static int fits_in_bytes(const BN_ULONG *words, size_t num_words, |
| 136 |
size_t num_bytes) { |
| 137 |
uint8_t mask = 0; |
| 138 |
#ifdef OPENSSL_BIG_ENDIAN |
| 139 |
for (size_t i = num_bytes / BN_BYTES; i < num_words; i++) { |
| 140 |
BN_ULONG word = words[i]; |
| 141 |
for (size_t j = 0; j < BN_BYTES; j++) { |
| 142 |
if ((i * BN_BYTES) + j < num_bytes) { |
| 143 |
// For the first word we don't need to check any bytes shorter than len |
| 144 |
continue ; |
| 145 |
} else { |
| 146 |
mask |= (word >> (j * 8)) & 0xff; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
#else |
| 151 |
const uint8_t *bytes = (const uint8_t *)words; |
| 152 |
size_t tot_bytes = num_words * sizeof(BN_ULONG); |
| 153 |
for (size_t i = num_bytes; i < tot_bytes; i++) { |
| 154 |
mask |= bytes[i]; |
| 155 |
} |
| 156 |
#endif |
| 157 |
return mask == 0; |
| 158 |
} |
| 159 |
|
| 160 |
// Asserts that the BIGNUM can be represented within |num| bytes. |
| 161 |
// The logic is consistent with `fits_in_bytes` but assertions will fail when false. |
| 162 |
void bn_assert_fits_in_bytes(const BIGNUM *bn, size_t num) { |
| 163 |
const uint8_t *bytes = (const uint8_t *)bn->d; |
| 164 |
size_t tot_bytes = bn->width * sizeof(BN_ULONG); |
| 165 |
if (tot_bytes > num) { |
| 166 |
CONSTTIME_DECLASSIFY(bytes + num, tot_bytes - num); |
| 167 |
// Avoids compiler error: unused variable 'byte' or 'word' |
| 168 |
// The assert statements below are only effective in DEBUG builds |
| 169 |
#ifndef NDEBUG |
| 170 |
#ifdef OPENSSL_BIG_ENDIAN |
| 171 |
for (int i = num / BN_BYTES; i < bn->width; i++) { |
| 172 |
BN_ULONG word = bn->d[i]; |
| 173 |
for (size_t j = 0; j < BN_BYTES; j++) { |
| 174 |
if ((i * BN_BYTES) + j < num) { |
| 175 |
// For the first word we don't need to check any bytes shorter than len |
| 176 |
continue; |
| 177 |
} else { |
| 178 |
uint8_t byte = (word >> (j * 8)) & 0xff; |
| 179 |
assert(byte == 0); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
#else |
| 184 |
for (size_t i = num; i < tot_bytes; i++) { |
| 185 |
assert(bytes[i] == 0); |
| 186 |
} |
| 187 |
#endif |
| 188 |
#endif |
| 189 |
(void)bytes; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void bn_words_to_big_endian(uint8_t *out, size_t out_len, const BN_ULONG *in, |
| 194 |
size_t in_len) { |
| 195 |
// The caller should have selected an output length without truncation. |
| 196 |
declassify_assert(fits_in_bytes(in, in_len, out_len)); |
| 197 |
size_t num_bytes = in_len * sizeof(BN_ULONG); |
| 198 |
if (out_len < num_bytes) { |
| 199 |
num_bytes = out_len; |
| 200 |
} |
| 201 |
|
| 202 |
// The redundant `i < out_len` guard below silences a GCC 14+ |
| 203 |
// `-Wstringop-overflow` false positive under LTO (see aws-lc#3166). |
| 204 |
#ifdef OPENSSL_BIG_ENDIAN |
| 205 |
for (size_t i = 0; i < num_bytes && i < out_len; i++) { |
| 206 |
BN_ULONG l = in[i / BN_BYTES]; |
| 207 |
out[out_len - i - 1] = (uint8_t)(l >> (8 * (i % BN_BYTES))) & 0xff; |
| 208 |
} |
| 209 |
#else |
| 210 |
const uint8_t *bytes = (const uint8_t *)in; |
| 211 |
for (size_t i = 0; i < num_bytes && i < out_len; i++) { |
| 212 |
out[out_len - i - 1] = bytes[i]; |
| 213 |
} |
| 214 |
#endif |
| 215 |
// Pad out the rest of the buffer with zeroes. |
| 216 |
OPENSSL_memset(out, 0, out_len - num_bytes); |
| 217 |
} |
| 218 |
|
| 219 |
size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) { |
| 220 |
size_t n = BN_num_bytes(in); |
| 221 |
bn_words_to_big_endian(out, n, in->d, in->width); |
| 222 |
return n; |
| 223 |
} |
| 224 |
|
| 225 |
void bn_words_to_little_endian(uint8_t *out, size_t out_len, const BN_ULONG *in, const size_t in_len) { |
| 226 |
// The caller should have selected an output length without truncation. |
| 227 |
assert(fits_in_bytes(in, in_len, out_len)); |
| 228 |
size_t num_bytes = in_len * sizeof(BN_ULONG); |
| 229 |
if (out_len < num_bytes) { |
| 230 |
num_bytes = out_len; |
| 231 |
} |
| 232 |
#ifdef OPENSSL_BIG_ENDIAN |
| 233 |
size_t byte_idx = 0; |
| 234 |
for (size_t word_idx = 0; word_idx < in_len; word_idx++) { |
| 235 |
BN_ULONG l = in[word_idx]; |
| 236 |
for(size_t j = 0; j < BN_BYTES && byte_idx < num_bytes; j++) { |
| 237 |
out[byte_idx] = (uint8_t)(l & 0xff); |
| 238 |
l >>= 8; |
| 239 |
byte_idx++; |
| 240 |
} |
| 241 |
} |
| 242 |
#else |
| 243 |
const uint8_t *bytes = (const uint8_t *)in; |
| 244 |
OPENSSL_memcpy(out, bytes, num_bytes); |
| 245 |
#endif |
| 246 |
// Fill the remainder with zeros. |
| 247 |
OPENSSL_memset(out + num_bytes, 0, out_len - num_bytes); |
| 248 |
} |
| 249 |
|
| 250 |
int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) { |
| 251 |
if (!fits_in_bytes(in->d, in->width, len)) { |
| 252 |
return 0; |
| 253 |
} |
| 254 |
bn_words_to_little_endian(out, len, in->d, in->width); |
| 255 |
return 1; |
| 256 |
} |
| 257 |
|
| 258 |
int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) { |
| 259 |
if (!fits_in_bytes(in->d, in->width, len)) { |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
bn_words_to_big_endian(out, len, in->d, in->width); |
| 264 |
return 1; |
| 265 |
} |
| 266 |
|
| 267 |
BN_ULONG BN_get_word(const BIGNUM *bn) { |
| 268 |
switch (bn_minimal_width(bn)) { |
| 269 |
case 0: |
| 270 |
return 0; |
| 271 |
case 1: |
| 272 |
return bn->d[0]; |
| 273 |
default: |
| 274 |
return BN_MASK2; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
int BN_get_u64(const BIGNUM *bn, uint64_t *out) { |
| 279 |
switch (bn_minimal_width(bn)) { |
| 280 |
case 0: |
| 281 |
*out = 0; |
| 282 |
return 1; |
| 283 |
case 1: |
| 284 |
*out = bn->d[0]; |
| 285 |
return 1; |
| 286 |
#if defined(OPENSSL_32_BIT) |
| 287 |
case 2: |
| 288 |
*out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32); |
| 289 |
return 1; |
| 290 |
#endif |
| 291 |
default: |
| 292 |
return 0; |
| 293 |
} |
| 294 |
} |