| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 |
| 24 | #include "../delocate.h" |
| 25 | #include "internal.h" |
| 26 |
| 27 |
| 28 | // BN_MAX_WORDS is the maximum number of words allowed in a |BIGNUM|. It is |
| 29 | // sized so byte and bit counts of a |BIGNUM| always fit in |int|, with room to |
| 30 | // spare. |
| 31 | #define BN_MAX_WORDS (INT_MAX / (4 * BN_BITS2)) |
| 32 |
| 33 | BIGNUM *BN_new(void) { |
| 34 | BIGNUM *bn = reinterpret_cast<BIGNUM *>(OPENSSL_malloc(sizeof(BIGNUM))); |
| 35 |
| 36 | if (bn == NULL) { |
| 37 | return NULL; |
| 38 | } |
| 39 |
| 40 | OPENSSL_memset(bn, 0, sizeof(BIGNUM)); |
| 41 | bn->flags = BN_FLG_MALLOCED; |
| 42 |
| 43 | return bn; |
| 44 | } |
| 45 |
| 46 | BIGNUM *BN_secure_new(void) { return BN_new(); } |
| 47 |
| 48 | void BN_init(BIGNUM *bn) { OPENSSL_memset(bn, 0, sizeof(BIGNUM)); } |
| 49 |
| 50 | void BN_free(BIGNUM *bn) { |
| 51 | if (bn == NULL) { |
| 52 | return; |
| 53 | } |
| 54 |
| 55 | if ((bn->flags & BN_FLG_STATIC_DATA) == 0) { |
| 56 | OPENSSL_free(bn->d); |
| 57 | } |
| 58 |
| 59 | if (bn->flags & BN_FLG_MALLOCED) { |
| 60 | OPENSSL_free(bn); |
| 61 | } else { |
| 62 | bn->d = NULL; |
| 63 | } |
| 64 | } |
| 65 |
| 66 | void BN_clear_free(BIGNUM *bn) { BN_free(bn); } |
| 67 |
| 68 | BIGNUM *BN_dup(const BIGNUM *src) { |
| 69 | BIGNUM *copy; |
| 70 |
| 71 | if (src == NULL) { |
| 72 | return NULL; |
| 73 | } |
| 74 |
| 75 | copy = BN_new(); |
| 76 | if (copy == NULL) { |
| 77 | return NULL; |
| 78 | } |
| 79 |
| 80 | if (!BN_copy(copy, src)) { |
| 81 | BN_free(copy); |
| 82 | return NULL; |
| 83 | } |
| 84 |
| 85 | return copy; |
| 86 | } |
| 87 |
| 88 | BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) { |
| 89 | if (src == dest) { |
| 90 | return dest; |
| 91 | } |
| 92 |
| 93 | if (!bn_wexpand(dest, src->width)) { |
| 94 | return NULL; |
| 95 | } |
| 96 |
| 97 | OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->width); |
| 98 |
| 99 | dest->width = src->width; |
| 100 | dest->neg = src->neg; |
| 101 | return dest; |
| 102 | } |
| 103 |
| 104 | void BN_clear(BIGNUM *bn) { |
| 105 | if (bn->d != NULL) { |
| 106 | OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0])); |
| 107 | } |
| 108 |
| 109 | bn->width = 0; |
| 110 | bn->neg = 0; |
| 111 | } |
| 112 |
| 113 | DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) { |
| 114 | static const BN_ULONG kOneLimbs[1] = {1}; |
| 115 | out->d = (BN_ULONG *)kOneLimbs; |
| 116 | out->width = 1; |
| 117 | out->dmax = 1; |
| 118 | out->neg = 0; |
| 119 | out->flags = BN_FLG_STATIC_DATA; |
| 120 | } |
| 121 |
| 122 | // BN_num_bits_word returns the minimum number of bits needed to represent the |
| 123 | // value in |l|. |
| 124 | unsigned BN_num_bits_word(BN_ULONG l) { |
| 125 | // |BN_num_bits| is often called on RSA prime factors. These have public bit |
| 126 | // lengths, but all bits beyond the high bit are secret, so count bits in |
| 127 | // constant time. |
| 128 | BN_ULONG x, mask; |
| 129 | int bits = (l != 0); |
| 130 |
| 131 | #if BN_BITS2 > 32 |
| 132 | // Look at the upper half of |x|. |x| is at most 64 bits long. |
| 133 | x = l >> 32; |
| 134 | // Set |mask| to all ones if |x| (the top 32 bits of |l|) is non-zero and all |
| 135 | // all zeros otherwise. |
| 136 | mask = 0u - x; |
| 137 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 138 | // If |x| is non-zero, the lower half is included in the bit count in full, |
| 139 | // and we count the upper half. Otherwise, we count the lower half. |
| 140 | bits += 32 & mask; |
| 141 | l ^= (x ^ l) & mask; // |l| is |x| if |mask| and remains |l| otherwise. |
| 142 | #endif |
| 143 |
| 144 | // The remaining blocks are analogous iterations at lower powers of two. |
| 145 | x = l >> 16; |
| 146 | mask = 0u - x; |
| 147 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 148 | bits += 16 & mask; |
| 149 | l ^= (x ^ l) & mask; |
| 150 |
| 151 | x = l >> 8; |
| 152 | mask = 0u - x; |
| 153 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 154 | bits += 8 & mask; |
| 155 | l ^= (x ^ l) & mask; |
| 156 |
| 157 | x = l >> 4; |
| 158 | mask = 0u - x; |
| 159 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 160 | bits += 4 & mask; |
| 161 | l ^= (x ^ l) & mask; |
| 162 |
| 163 | x = l >> 2; |
| 164 | mask = 0u - x; |
| 165 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 166 | bits += 2 & mask; |
| 167 | l ^= (x ^ l) & mask; |
| 168 |
| 169 | x = l >> 1; |
| 170 | mask = 0u - x; |
| 171 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 172 | bits += 1 & mask; |
| 173 |
| 174 | return bits; |
| 175 | } |
| 176 |
| 177 | unsigned BN_num_bits(const BIGNUM *bn) { |
| 178 | const int width = bn_minimal_width(bn); |
| 179 | if (width == 0) { |
| 180 | return 0; |
| 181 | } |
| 182 |
| 183 | return (width - 1) * BN_BITS2 + BN_num_bits_word(bn->d[width - 1]); |
| 184 | } |
| 185 |
| 186 | unsigned BN_num_bytes(const BIGNUM *bn) { return (BN_num_bits(bn) + 7) / 8; } |
| 187 |
| 188 | void BN_zero(BIGNUM *bn) { bn->width = bn->neg = 0; } |
| 189 |
| 190 | int BN_one(BIGNUM *bn) { return BN_set_word(bn, 1); } |
| 191 |
| 192 | int BN_set_word(BIGNUM *bn, BN_ULONG value) { |
| 193 | if (value == 0) { |
| 194 | BN_zero(bn); |
| 195 | return 1; |
| 196 | } |
| 197 |
| 198 | if (!bn_wexpand(bn, 1)) { |
| 199 | return 0; |
| 200 | } |
| 201 |
| 202 | bn->neg = 0; |
| 203 | bn->d[0] = value; |
| 204 | bn->width = 1; |
| 205 | return 1; |
| 206 | } |
| 207 |
| 208 | int BN_set_u64(BIGNUM *bn, uint64_t value) { |
| 209 | #if BN_BITS2 == 64 |
| 210 | return BN_set_word(bn, value); |
| 211 | #elif BN_BITS2 == 32 |
| 212 | if (value <= BN_MASK2) { |
| 213 | return BN_set_word(bn, (BN_ULONG)value); |
| 214 | } |
| 215 |
| 216 | if (!bn_wexpand(bn, 2)) { |
| 217 | return 0; |
| 218 | } |
| 219 |
| 220 | bn->neg = 0; |
| 221 | bn->d[0] = (BN_ULONG)value; |
| 222 | bn->d[1] = (BN_ULONG)(value >> 32); |
| 223 | bn->width = 2; |
| 224 | return 1; |
| 225 | #else |
| 226 | #error "BN_BITS2 must be 32 or 64." |
| 227 | #endif |
| 228 | } |
| 229 |
| 230 | int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { |
| 231 | if (!bn_wexpand(bn, num)) { |
| 232 | return 0; |
| 233 | } |
| 234 | OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG)); |
| 235 | // |bn_wexpand| verified that |num| isn't too large. |
| 236 | bn->width = (int)num; |
| 237 | bn->neg = 0; |
| 238 | return 1; |
| 239 | } |
| 240 |
| 241 | void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { |
| 242 | if ((bn->flags & BN_FLG_STATIC_DATA) == 0) { |
| 243 | OPENSSL_free(bn->d); |
| 244 | } |
| 245 | bn->d = (BN_ULONG *)words; |
| 246 |
| 247 | assert(num <= BN_MAX_WORDS); |
| 248 | bn->width = (int)num; |
| 249 | bn->dmax = (int)num; |
| 250 | bn->neg = 0; |
| 251 | bn->flags |= BN_FLG_STATIC_DATA; |
| 252 | } |
| 253 |
| 254 | int bn_fits_in_words(const BIGNUM *bn, size_t num) { |
| 255 | // All words beyond |num| must be zero. |
| 256 | BN_ULONG mask = 0; |
| 257 | for (size_t i = num; i < (size_t)bn->width; i++) { |
| 258 | mask |= bn->d[i]; |
| 259 | } |
| 260 | return mask == 0; |
| 261 | } |
| 262 |
| 263 | int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn) { |
| 264 | if (bn->neg) { |
| 265 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 266 | return 0; |
| 267 | } |
| 268 |
| 269 | size_t width = (size_t)bn->width; |
| 270 | if (width > num) { |
| 271 | if (!bn_fits_in_words(bn, num)) { |
| 272 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 273 | return 0; |
| 274 | } |
| 275 | width = num; |
| 276 | } |
| 277 |
| 278 | OPENSSL_memset(out, 0, sizeof(BN_ULONG) * num); |
| 279 | OPENSSL_memcpy(out, bn->d, sizeof(BN_ULONG) * width); |
| 280 | return 1; |
| 281 | } |
| 282 |
| 283 | int BN_is_negative(const BIGNUM *bn) { return bn->neg != 0; } |
| 284 |
| 285 | void BN_set_negative(BIGNUM *bn, int sign) { |
| 286 | if (sign && !BN_is_zero(bn)) { |
| 287 | bn->neg = 1; |
| 288 | } else { |
| 289 | bn->neg = 0; |
| 290 | } |
| 291 | } |
| 292 |
| 293 | int bn_wexpand(BIGNUM *bn, size_t words) { |
| 294 | BN_ULONG *a; |
| 295 |
| 296 | if (words <= (size_t)bn->dmax) { |
| 297 | return 1; |
| 298 | } |
| 299 |
| 300 | if (words > BN_MAX_WORDS) { |
| 301 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 302 | return 0; |
| 303 | } |
| 304 |
| 305 | if (bn->flags & BN_FLG_STATIC_DATA) { |
| 306 | OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); |
| 307 | return 0; |
| 308 | } |
| 309 |
| 310 | a = reinterpret_cast<BN_ULONG *>(OPENSSL_calloc(words, sizeof(BN_ULONG))); |
| 311 | if (a == NULL) { |
| 312 | return 0; |
| 313 | } |
| 314 |
| 315 | OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->width); |
| 316 |
| 317 | OPENSSL_free(bn->d); |
| 318 | bn->d = a; |
| 319 | bn->dmax = (int)words; |
| 320 |
| 321 | return 1; |
| 322 | } |
| 323 |
| 324 | int bn_expand(BIGNUM *bn, size_t bits) { |
| 325 | if (bits + BN_BITS2 - 1 < bits) { |
| 326 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 327 | return 0; |
| 328 | } |
| 329 | return bn_wexpand(bn, (bits + BN_BITS2 - 1) / BN_BITS2); |
| 330 | } |
| 331 |
| 332 | int bn_resize_words(BIGNUM *bn, size_t words) { |
| 333 | if ((size_t)bn->width <= words) { |
| 334 | if (!bn_wexpand(bn, words)) { |
| 335 | return 0; |
| 336 | } |
| 337 | OPENSSL_memset(bn->d + bn->width, 0, |
| 338 | (words - bn->width) * sizeof(BN_ULONG)); |
| 339 | bn->width = (int)words; |
| 340 | return 1; |
| 341 | } |
| 342 |
| 343 | // All words beyond the new width must be zero. |
| 344 | if (!bn_fits_in_words(bn, words)) { |
| 345 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 346 | return 0; |
| 347 | } |
| 348 | bn->width = (int)words; |
| 349 | return 1; |
| 350 | } |
| 351 |
| 352 | void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a, |
| 353 | const BN_ULONG *b, size_t num) { |
| 354 | for (size_t i = 0; i < num; i++) { |
| 355 | static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 356 | "crypto_word_t is too small"); |
| 357 | r[i] = constant_time_select_w(mask, a[i], b[i]); |
| 358 | } |
| 359 | } |
| 360 |
| 361 | int bn_minimal_width(const BIGNUM *bn) { |
| 362 | int ret = bn->width; |
| 363 | while (ret > 0 && bn->d[ret - 1] == 0) { |
| 364 | ret--; |
| 365 | } |
| 366 | return ret; |
| 367 | } |
| 368 |
| 369 | void bn_set_minimal_width(BIGNUM *bn) { |
| 370 | bn->width = bn_minimal_width(bn); |
| 371 | if (bn->width == 0) { |
| 372 | bn->neg = 0; |
| 373 | } |
| 374 | } |
| 375 |