Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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). |
| 14 | * |
| 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. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 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)" |
| 39 | * |
| 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 |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 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.] */ |
| 56 | |
| 57 | #include <openssl/bn.h> |
| 58 | |
| 59 | #include <limits.h> |
| 60 | #include <string.h> |
| 61 | |
| 62 | #include <openssl/err.h> |
| 63 | #include <openssl/mem.h> |
| 64 | |
| 65 | #include "internal.h" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 66 | #include "../delocate.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 67 | |
| 68 | |
| 69 | BIGNUM *BN_new(void) { |
| 70 | BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM)); |
| 71 | |
| 72 | if (bn == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 73 | OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 77 | OPENSSL_memset(bn, 0, sizeof(BIGNUM)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 78 | bn->flags = BN_FLG_MALLOCED; |
| 79 | |
| 80 | return bn; |
| 81 | } |
| 82 | |
| 83 | void BN_init(BIGNUM *bn) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 84 | OPENSSL_memset(bn, 0, sizeof(BIGNUM)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void BN_free(BIGNUM *bn) { |
| 88 | if (bn == NULL) { |
| 89 | return; |
| 90 | } |
| 91 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 92 | if ((bn->flags & BN_FLG_STATIC_DATA) == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 93 | OPENSSL_free(bn->d); |
| 94 | } |
| 95 | |
| 96 | if (bn->flags & BN_FLG_MALLOCED) { |
| 97 | OPENSSL_free(bn); |
| 98 | } else { |
| 99 | bn->d = NULL; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void BN_clear_free(BIGNUM *bn) { |
| 104 | char should_free; |
| 105 | |
| 106 | if (bn == NULL) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (bn->d != NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 111 | if ((bn->flags & BN_FLG_STATIC_DATA) == 0) { |
| 112 | OPENSSL_free(bn->d); |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 113 | } else { |
| 114 | OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0])); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | should_free = (bn->flags & BN_FLG_MALLOCED) != 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | if (should_free) { |
| 120 | OPENSSL_free(bn); |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 121 | } else { |
| 122 | OPENSSL_cleanse(bn, sizeof(BIGNUM)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | BIGNUM *BN_dup(const BIGNUM *src) { |
| 127 | BIGNUM *copy; |
| 128 | |
| 129 | if (src == NULL) { |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | copy = BN_new(); |
| 134 | if (copy == NULL) { |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | if (!BN_copy(copy, src)) { |
| 139 | BN_free(copy); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | return copy; |
| 144 | } |
| 145 | |
| 146 | BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) { |
| 147 | if (src == dest) { |
| 148 | return dest; |
| 149 | } |
| 150 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 151 | if (!bn_wexpand(dest, src->width)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 152 | return NULL; |
| 153 | } |
| 154 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 155 | OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->width); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 156 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 157 | dest->width = src->width; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | dest->neg = src->neg; |
| 159 | return dest; |
| 160 | } |
| 161 | |
| 162 | void BN_clear(BIGNUM *bn) { |
| 163 | if (bn->d != NULL) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 164 | OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0])); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 167 | bn->width = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 168 | bn->neg = 0; |
| 169 | } |
| 170 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 171 | DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 172 | static const BN_ULONG kOneLimbs[1] = { 1 }; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 173 | out->d = (BN_ULONG*) kOneLimbs; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 174 | out->width = 1; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 175 | out->dmax = 1; |
| 176 | out->neg = 0; |
| 177 | out->flags = BN_FLG_STATIC_DATA; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 180 | // BN_num_bits_word returns the minimum number of bits needed to represent the |
| 181 | // value in |l|. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 182 | unsigned BN_num_bits_word(BN_ULONG l) { |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 183 | // |BN_num_bits| is often called on RSA prime factors. These have public bit |
| 184 | // lengths, but all bits beyond the high bit are secret, so count bits in |
| 185 | // constant time. |
| 186 | BN_ULONG x, mask; |
| 187 | int bits = (l != 0); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 188 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 189 | #if BN_BITS2 > 32 |
| 190 | x = l >> 32; |
| 191 | mask = 0u - x; |
| 192 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 193 | bits += 32 & mask; |
| 194 | l ^= (x ^ l) & mask; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 195 | #endif |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 196 | |
| 197 | x = l >> 16; |
| 198 | mask = 0u - x; |
| 199 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 200 | bits += 16 & mask; |
| 201 | l ^= (x ^ l) & mask; |
| 202 | |
| 203 | x = l >> 8; |
| 204 | mask = 0u - x; |
| 205 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 206 | bits += 8 & mask; |
| 207 | l ^= (x ^ l) & mask; |
| 208 | |
| 209 | x = l >> 4; |
| 210 | mask = 0u - x; |
| 211 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 212 | bits += 4 & mask; |
| 213 | l ^= (x ^ l) & mask; |
| 214 | |
| 215 | x = l >> 2; |
| 216 | mask = 0u - x; |
| 217 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 218 | bits += 2 & mask; |
| 219 | l ^= (x ^ l) & mask; |
| 220 | |
| 221 | x = l >> 1; |
| 222 | mask = 0u - x; |
| 223 | mask = (0u - (mask >> (BN_BITS2 - 1))); |
| 224 | bits += 1 & mask; |
| 225 | |
| 226 | return bits; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | unsigned BN_num_bits(const BIGNUM *bn) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 230 | const int width = bn_minimal_width(bn); |
| 231 | if (width == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 235 | return (width - 1) * BN_BITS2 + BN_num_bits_word(bn->d[width - 1]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | unsigned BN_num_bytes(const BIGNUM *bn) { |
| 239 | return (BN_num_bits(bn) + 7) / 8; |
| 240 | } |
| 241 | |
| 242 | void BN_zero(BIGNUM *bn) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 243 | bn->width = bn->neg = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int BN_one(BIGNUM *bn) { |
| 247 | return BN_set_word(bn, 1); |
| 248 | } |
| 249 | |
| 250 | int BN_set_word(BIGNUM *bn, BN_ULONG value) { |
| 251 | if (value == 0) { |
| 252 | BN_zero(bn); |
| 253 | return 1; |
| 254 | } |
| 255 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 256 | if (!bn_wexpand(bn, 1)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | bn->neg = 0; |
| 261 | bn->d[0] = value; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 262 | bn->width = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 266 | int BN_set_u64(BIGNUM *bn, uint64_t value) { |
| 267 | #if BN_BITS2 == 64 |
| 268 | return BN_set_word(bn, value); |
| 269 | #elif BN_BITS2 == 32 |
| 270 | if (value <= BN_MASK2) { |
| 271 | return BN_set_word(bn, (BN_ULONG)value); |
| 272 | } |
| 273 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 274 | if (!bn_wexpand(bn, 2)) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | bn->neg = 0; |
| 279 | bn->d[0] = (BN_ULONG)value; |
| 280 | bn->d[1] = (BN_ULONG)(value >> 32); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 281 | bn->width = 2; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 282 | return 1; |
| 283 | #else |
| 284 | #error "BN_BITS2 must be 32 or 64." |
| 285 | #endif |
| 286 | } |
| 287 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 288 | int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 289 | if (!bn_wexpand(bn, num)) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 292 | OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG)); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 293 | // |bn_wexpand| verified that |num| isn't too large. |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 294 | bn->width = (int)num; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 295 | bn->neg = 0; |
| 296 | return 1; |
| 297 | } |
| 298 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 299 | int bn_fits_in_words(const BIGNUM *bn, size_t num) { |
| 300 | // All words beyond |num| must be zero. |
| 301 | BN_ULONG mask = 0; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 302 | for (size_t i = num; i < (size_t)bn->width; i++) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 303 | mask |= bn->d[i]; |
| 304 | } |
| 305 | return mask == 0; |
| 306 | } |
| 307 | |
| 308 | int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn) { |
| 309 | if (bn->neg) { |
| 310 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 311 | return 0; |
| 312 | } |
| 313 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 314 | size_t width = (size_t)bn->width; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 315 | if (width > num) { |
| 316 | if (!bn_fits_in_words(bn, num)) { |
| 317 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 318 | return 0; |
| 319 | } |
| 320 | width = num; |
| 321 | } |
| 322 | |
| 323 | OPENSSL_memset(out, 0, sizeof(BN_ULONG) * num); |
| 324 | OPENSSL_memcpy(out, bn->d, sizeof(BN_ULONG) * width); |
| 325 | return 1; |
| 326 | } |
| 327 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 328 | int BN_is_negative(const BIGNUM *bn) { |
| 329 | return bn->neg != 0; |
| 330 | } |
| 331 | |
| 332 | void BN_set_negative(BIGNUM *bn, int sign) { |
| 333 | if (sign && !BN_is_zero(bn)) { |
| 334 | bn->neg = 1; |
| 335 | } else { |
| 336 | bn->neg = 0; |
| 337 | } |
| 338 | } |
| 339 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 340 | int bn_wexpand(BIGNUM *bn, size_t words) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 341 | BN_ULONG *a; |
| 342 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 343 | if (words <= (size_t)bn->dmax) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 344 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (words > (INT_MAX / (4 * BN_BITS2))) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 348 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 349 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (bn->flags & BN_FLG_STATIC_DATA) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 353 | OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 354 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 355 | } |
| 356 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 357 | a = OPENSSL_malloc(sizeof(BN_ULONG) * words); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 358 | if (a == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 359 | OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 360 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 363 | OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->width); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 364 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 365 | OPENSSL_free(bn->d); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 366 | bn->d = a; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 367 | bn->dmax = (int)words; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 368 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 369 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 372 | int bn_expand(BIGNUM *bn, size_t bits) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 373 | if (bits + BN_BITS2 - 1 < bits) { |
| 374 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 375 | return 0; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 376 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 377 | return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2); |
| 378 | } |
| 379 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 380 | int bn_resize_words(BIGNUM *bn, size_t words) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 381 | if ((size_t)bn->width <= words) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 382 | if (!bn_wexpand(bn, words)) { |
| 383 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 384 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 385 | OPENSSL_memset(bn->d + bn->width, 0, |
| 386 | (words - bn->width) * sizeof(BN_ULONG)); |
| 387 | bn->width = words; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 388 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 389 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 390 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 391 | // All words beyond the new width must be zero. |
| 392 | if (!bn_fits_in_words(bn, words)) { |
| 393 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 394 | return 0; |
| 395 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 396 | bn->width = words; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 400 | void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a, |
| 401 | const BN_ULONG *b, size_t num) { |
| 402 | for (size_t i = 0; i < num; i++) { |
| 403 | OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 404 | crypto_word_t_too_small); |
| 405 | r[i] = constant_time_select_w(mask, a[i], b[i]); |
| 406 | } |
| 407 | } |
| 408 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 409 | int bn_minimal_width(const BIGNUM *bn) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 410 | int ret = bn->width; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 411 | while (ret > 0 && bn->d[ret - 1] == 0) { |
| 412 | ret--; |
| 413 | } |
| 414 | return ret; |
| 415 | } |
| 416 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 417 | void bn_set_minimal_width(BIGNUM *bn) { |
| 418 | bn->width = bn_minimal_width(bn); |
| 419 | if (bn->width == 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 420 | bn->neg = 0; |
| 421 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 422 | } |