| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 |
| 16 | #ifndef OPENSSL_HEADER_BN_H |
| 17 | #define OPENSSL_HEADER_BN_H |
| 18 |
| 19 | #include <openssl/base.h> // IWYU pragma: export |
| 20 | #include <openssl/thread.h> |
| 21 |
| 22 | #include <inttypes.h> // for PRIu64 and friends |
| 23 | #include <stdio.h> // for FILE* |
| 24 |
| 25 | #if defined(__cplusplus) |
| 26 | extern "C" { |
| 27 | #endif |
| 28 |
| 29 |
| 30 | // BN provides support for working with arbitrary sized integers. For example, |
| 31 | // although the largest integer supported by the compiler might be 64 bits, BN |
| 32 | // will allow you to work with much larger numbers. |
| 33 | // |
| 34 | // This library is developed for use inside BoringSSL, and uses implementation |
| 35 | // strategies that may not be ideal for other applications. Non-cryptographic |
| 36 | // uses should use a more general-purpose integer library, especially if |
| 37 | // performance-sensitive. |
| 38 | // |
| 39 | // Many functions in BN scale quadratically or higher in the bit length of their |
| 40 | // input. Callers at this layer are assumed to have capped input sizes within |
| 41 | // their performance tolerances. |
| 42 |
| 43 |
| 44 | // BN_ULONG is the native word size when working with big integers. |
| 45 | // |
| 46 | // Note: on some platforms, inttypes.h does not define print format macros in |
| 47 | // C++ unless |__STDC_FORMAT_MACROS| defined. This is due to text in C99 which |
| 48 | // was never adopted in any C++ standard and explicitly overruled in C++11. As |
| 49 | // this is a public header, bn.h does not define |__STDC_FORMAT_MACROS| itself. |
| 50 | // Projects which use |BN_*_FMT*| with outdated C headers may need to define it |
| 51 | // externally. |
| 52 | #if defined(OPENSSL_64_BIT) |
| 53 | typedef uint64_t BN_ULONG; |
| 54 | #define BN_BITS2 64 |
| 55 | #define BN_DEC_FMT1 "%" PRIu64 |
| 56 | #define BN_HEX_FMT1 "%" PRIx64 |
| 57 | #define BN_HEX_FMT2 "%016" PRIx64 |
| 58 | #elif defined(OPENSSL_32_BIT) |
| 59 | typedef uint32_t BN_ULONG; |
| 60 | #define BN_BITS2 32 |
| 61 | #define BN_DEC_FMT1 "%" PRIu32 |
| 62 | #define BN_HEX_FMT1 "%" PRIx32 |
| 63 | #define BN_HEX_FMT2 "%08" PRIx32 |
| 64 | #else |
| 65 | #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT" |
| 66 | #endif |
| 67 |
| 68 |
| 69 | // Allocation and freeing. |
| 70 |
| 71 | // BN_new creates a new, allocated BIGNUM and initialises it. |
| 72 | OPENSSL_EXPORT BIGNUM *BN_new(void); |
| 73 |
| 74 | // BN_init initialises a stack allocated |BIGNUM|. |
| 75 | OPENSSL_EXPORT void BN_init(BIGNUM *bn); |
| 76 |
| 77 | // BN_free frees the data referenced by |bn| and, if |bn| was originally |
| 78 | // allocated on the heap, frees |bn| also. |
| 79 | OPENSSL_EXPORT void BN_free(BIGNUM *bn); |
| 80 |
| 81 | // BN_clear_free erases and frees the data referenced by |bn| and, if |bn| was |
| 82 | // originally allocated on the heap, frees |bn| also. |
| 83 | OPENSSL_EXPORT void BN_clear_free(BIGNUM *bn); |
| 84 |
| 85 | // BN_dup allocates a new BIGNUM and sets it equal to |src|. It returns the |
| 86 | // allocated BIGNUM on success or NULL otherwise. |
| 87 | OPENSSL_EXPORT BIGNUM *BN_dup(const BIGNUM *src); |
| 88 |
| 89 | // BN_copy sets |dest| equal to |src| and returns |dest| or NULL on allocation |
| 90 | // failure. |
| 91 | OPENSSL_EXPORT BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src); |
| 92 |
| 93 | // BN_clear sets |bn| to zero and erases the old data. |
| 94 | OPENSSL_EXPORT void BN_clear(BIGNUM *bn); |
| 95 |
| 96 | // BN_value_one returns a static BIGNUM with value 1. |
| 97 | OPENSSL_EXPORT const BIGNUM *BN_value_one(void); |
| 98 |
| 99 |
| 100 | // Basic functions. |
| 101 |
| 102 | // BN_num_bits returns the minimum number of bits needed to represent the |
| 103 | // absolute value of |bn|. |
| 104 | OPENSSL_EXPORT unsigned BN_num_bits(const BIGNUM *bn); |
| 105 |
| 106 | // BN_num_bytes returns the minimum number of bytes needed to represent the |
| 107 | // absolute value of |bn|. |
| 108 | // |
| 109 | // While |size_t| is the preferred type for byte counts, callers can assume that |
| 110 | // |BIGNUM|s are bounded such that this value, and its corresponding bit count, |
| 111 | // will always fit in |int|. |
| 112 | OPENSSL_EXPORT unsigned BN_num_bytes(const BIGNUM *bn); |
| 113 |
| 114 | // BN_zero sets |bn| to zero. |
| 115 | OPENSSL_EXPORT void BN_zero(BIGNUM *bn); |
| 116 |
| 117 | // BN_one sets |bn| to one. It returns one on success or zero on allocation |
| 118 | // failure. |
| 119 | OPENSSL_EXPORT int BN_one(BIGNUM *bn); |
| 120 |
| 121 | // BN_set_word sets |bn| to |value|. It returns one on success or zero on |
| 122 | // allocation failure. |
| 123 | OPENSSL_EXPORT int BN_set_word(BIGNUM *bn, BN_ULONG value); |
| 124 |
| 125 | // BN_set_u64 sets |bn| to |value|. It returns one on success or zero on |
| 126 | // allocation failure. |
| 127 | OPENSSL_EXPORT int BN_set_u64(BIGNUM *bn, uint64_t value); |
| 128 |
| 129 | // BN_set_negative sets the sign of |bn|. |
| 130 | OPENSSL_EXPORT void BN_set_negative(BIGNUM *bn, int sign); |
| 131 |
| 132 | // BN_is_negative returns one if |bn| is negative and zero otherwise. |
| 133 | OPENSSL_EXPORT int BN_is_negative(const BIGNUM *bn); |
| 134 |
| 135 |
| 136 | // Conversion functions. |
| 137 |
| 138 | // BN_bin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as |
| 139 | // a big-endian number, and returns |ret|. If |ret| is NULL then a fresh |
| 140 | // |BIGNUM| is allocated and returned. It returns NULL on allocation |
| 141 | // failure. |
| 142 | OPENSSL_EXPORT BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret); |
| 143 |
| 144 | // BN_bn2bin serialises the absolute value of |in| to |out| as a big-endian |
| 145 | // integer, which must have |BN_num_bytes| of space available. It returns the |
| 146 | // number of bytes written. Note this function leaks the magnitude of |in|. If |
| 147 | // |in| is secret, use |BN_bn2bin_padded| instead. |
| 148 | OPENSSL_EXPORT size_t BN_bn2bin(const BIGNUM *in, uint8_t *out); |
| 149 |
| 150 | // BN_lebin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as |
| 151 | // a little-endian number, and returns |ret|. If |ret| is NULL then a fresh |
| 152 | // |BIGNUM| is allocated and returned. It returns NULL on allocation |
| 153 | // failure. |
| 154 | OPENSSL_EXPORT BIGNUM *BN_lebin2bn(const uint8_t *in, size_t len, BIGNUM *ret); |
| 155 |
| 156 | // BN_bn2le_padded serialises the absolute value of |in| to |out| as a |
| 157 | // little-endian integer, which must have |len| of space available, padding |
| 158 | // out the remainder of out with zeros. If |len| is smaller than |BN_num_bytes|, |
| 159 | // the function fails and returns 0. Otherwise, it returns 1. |
| 160 | OPENSSL_EXPORT int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in); |
| 161 |
| 162 | // BN_bn2bin_padded serialises the absolute value of |in| to |out| as a |
| 163 | // big-endian integer. The integer is padded with leading zeros up to size |
| 164 | // |len|. If |len| is smaller than |BN_num_bytes|, the function fails and |
| 165 | // returns 0. Otherwise, it returns 1. |
| 166 | OPENSSL_EXPORT int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in); |
| 167 |
| 168 | // BN_bn2cbb_padded behaves like |BN_bn2bin_padded| but writes to a |CBB|. |
| 169 | OPENSSL_EXPORT int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in); |
| 170 |
| 171 | // BN_bn2hex returns an allocated string that contains a NUL-terminated, hex |
| 172 | // representation of |bn|. If |bn| is negative, the first char in the resulting |
| 173 | // string will be '-'. Returns NULL on allocation failure. |
| 174 | OPENSSL_EXPORT char *BN_bn2hex(const BIGNUM *bn); |
| 175 |
| 176 | // BN_hex2bn parses the leading hex number from |in|, which may be proceeded by |
| 177 | // a '-' to indicate a negative number and may contain trailing, non-hex data. |
| 178 | // If |outp| is not NULL, it constructs a BIGNUM equal to the hex number and |
| 179 | // stores it in |*outp|. If |*outp| is NULL then it allocates a new BIGNUM and |
| 180 | // updates |*outp|. It returns the number of bytes of |in| processed or zero on |
| 181 | // error. |
| 182 | OPENSSL_EXPORT int BN_hex2bn(BIGNUM **outp, const char *in); |
| 183 |
| 184 | // BN_bn2dec returns an allocated string that contains a NUL-terminated, |
| 185 | // decimal representation of |bn|. If |bn| is negative, the first char in the |
| 186 | // resulting string will be '-'. Returns NULL on allocation failure. |
| 187 | // |
| 188 | // Converting an arbitrarily large integer to decimal is quadratic in the bit |
| 189 | // length of |a|. This function assumes the caller has capped the input within |
| 190 | // performance tolerances. |
| 191 | OPENSSL_EXPORT char *BN_bn2dec(const BIGNUM *a); |
| 192 |
| 193 | // BN_dec2bn parses the leading decimal number from |in|, which may be |
| 194 | // proceeded by a '-' to indicate a negative number and may contain trailing, |
| 195 | // non-decimal data. If |outp| is not NULL, it constructs a BIGNUM equal to the |
| 196 | // decimal number and stores it in |*outp|. If |*outp| is NULL then it |
| 197 | // allocates a new BIGNUM and updates |*outp|. It returns the number of bytes |
| 198 | // of |in| processed or zero on error. |
| 199 | // |
| 200 | // Converting an arbitrarily large integer to decimal is quadratic in the bit |
| 201 | // length of |a|. This function assumes the caller has capped the input within |
| 202 | // performance tolerances. |
| 203 | OPENSSL_EXPORT int BN_dec2bn(BIGNUM **outp, const char *in); |
| 204 |
| 205 | // BN_asc2bn acts like |BN_dec2bn| or |BN_hex2bn| depending on whether |in| |
| 206 | // begins with "0X" or "0x" (indicating hex) or not (indicating decimal). A |
| 207 | // leading '-' is still permitted and comes before the optional 0X/0x. It |
| 208 | // returns one on success or zero on error. |
| 209 | OPENSSL_EXPORT int BN_asc2bn(BIGNUM **outp, const char *in); |
| 210 |
| 211 | // BN_print writes a hex encoding of |a| to |bio|. It returns one on success |
| 212 | // and zero on error. |
| 213 | OPENSSL_EXPORT int BN_print(BIO *bio, const BIGNUM *a); |
| 214 |
| 215 | // BN_print_fp acts like |BIO_print|, but wraps |fp| in a |BIO| first. |
| 216 | OPENSSL_EXPORT int BN_print_fp(FILE *fp, const BIGNUM *a); |
| 217 |
| 218 | // BN_get_word returns the absolute value of |bn| as a single word. If |bn| is |
| 219 | // too large to be represented as a single word, the maximum possible value |
| 220 | // will be returned. |
| 221 | OPENSSL_EXPORT BN_ULONG BN_get_word(const BIGNUM *bn); |
| 222 |
| 223 | // BN_get_u64 sets |*out| to the absolute value of |bn| as a |uint64_t| and |
| 224 | // returns one. If |bn| is too large to be represented as a |uint64_t|, it |
| 225 | // returns zero. |
| 226 | OPENSSL_EXPORT int BN_get_u64(const BIGNUM *bn, uint64_t *out); |
| 227 |
| 228 |
| 229 | // ASN.1 functions. |
| 230 |
| 231 | // BN_parse_asn1_unsigned parses a non-negative DER INTEGER from |cbs| writes |
| 232 | // the result to |ret|. It returns one on success and zero on failure. |
| 233 | OPENSSL_EXPORT int BN_parse_asn1_unsigned(CBS *cbs, BIGNUM *ret); |
| 234 |
| 235 | // BN_marshal_asn1 marshals |bn| as a non-negative DER INTEGER and appends the |
| 236 | // result to |cbb|. It returns one on success and zero on failure. |
| 237 | OPENSSL_EXPORT int BN_marshal_asn1(CBB *cbb, const BIGNUM *bn); |
| 238 |
| 239 |
| 240 | // BIGNUM pools. |
| 241 | // |
| 242 | // Certain BIGNUM operations need to use many temporary variables and |
| 243 | // allocating and freeing them can be quite slow. Thus such operations typically |
| 244 | // take a |BN_CTX| parameter, which contains a pool of |BIGNUMs|. The |ctx| |
| 245 | // argument to a public function may be NULL, in which case a local |BN_CTX| |
| 246 | // will be created just for the lifetime of that call. |
| 247 | // |
| 248 | // A function must call |BN_CTX_start| first. Then, |BN_CTX_get| may be called |
| 249 | // repeatedly to obtain temporary |BIGNUM|s. All |BN_CTX_get| calls must be made |
| 250 | // before calling any other functions that use the |ctx| as an argument. |
| 251 | // |
| 252 | // Finally, |BN_CTX_end| must be called before returning from the function. |
| 253 | // When |BN_CTX_end| is called, the |BIGNUM| pointers obtained from |
| 254 | // |BN_CTX_get| become invalid. |
| 255 |
| 256 | // BN_CTX_new returns a new, empty BN_CTX or NULL on allocation failure. |
| 257 | OPENSSL_EXPORT BN_CTX *BN_CTX_new(void); |
| 258 |
| 259 | // BN_CTX_free frees all BIGNUMs contained in |ctx| and then frees |ctx| |
| 260 | // itself. |
| 261 | OPENSSL_EXPORT void BN_CTX_free(BN_CTX *ctx); |
| 262 |
| 263 | // BN_CTX_start "pushes" a new entry onto the |ctx| stack and allows future |
| 264 | // calls to |BN_CTX_get|. |
| 265 | OPENSSL_EXPORT void BN_CTX_start(BN_CTX *ctx); |
| 266 |
| 267 | // BN_CTX_get returns a new |BIGNUM|, or NULL on allocation failure. Once |
| 268 | // |BN_CTX_get| has returned NULL, all future calls will also return NULL until |
| 269 | // |BN_CTX_end| is called. |
| 270 | OPENSSL_EXPORT BIGNUM *BN_CTX_get(BN_CTX *ctx); |
| 271 |
| 272 | // BN_CTX_end invalidates all |BIGNUM|s returned from |BN_CTX_get| since the |
| 273 | // matching |BN_CTX_start| call. |
| 274 | OPENSSL_EXPORT void BN_CTX_end(BN_CTX *ctx); |
| 275 |
| 276 |
| 277 | // Simple arithmetic |
| 278 |
| 279 | // BN_add sets |r| = |a| + |b|, where |r| may be the same pointer as either |a| |
| 280 | // or |b|. It returns one on success and zero on allocation failure. |
| 281 | OPENSSL_EXPORT int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); |
| 282 |
| 283 | // BN_uadd sets |r| = |a| + |b|, considering only the absolute values of |a| and |
| 284 | // |b|. |r| may be the same pointer as either |a| or |b|. It returns one on |
| 285 | // success and zero on allocation failure. |
| 286 | OPENSSL_EXPORT int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); |
| 287 |
| 288 | // BN_add_word adds |w| to |a|. It returns one on success and zero otherwise. |
| 289 | OPENSSL_EXPORT int BN_add_word(BIGNUM *a, BN_ULONG w); |
| 290 |
| 291 | // BN_sub sets |r| = |a| - |b|, where |r| may be the same pointer as either |a| |
| 292 | // or |b|. It returns one on success and zero on allocation failure. |
| 293 | OPENSSL_EXPORT int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); |
| 294 |
| 295 | // BN_usub sets |r| = |a| - |b|, considering only the absolute values of |a| and |
| 296 | // |b|. The result must be non-negative, i.e. |b| <= |a|. |r| may be the same |
| 297 | // pointer as either |a| or |b|. It returns one on success and zero on error. |
| 298 | OPENSSL_EXPORT int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); |
| 299 |
| 300 | // BN_sub_word subtracts |w| from |a|. It returns one on success and zero on |
| 301 | // allocation failure. |
| 302 | OPENSSL_EXPORT int BN_sub_word(BIGNUM *a, BN_ULONG w); |
| 303 |
| 304 | // BN_mul sets |r| = |a| * |b|, where |r| may be the same pointer as |a| or |
| 305 | // |b|. Returns one on success and zero otherwise. |
| 306 | OPENSSL_EXPORT int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 307 | BN_CTX *ctx); |
| 308 |
| 309 | // BN_mul_word sets |bn| = |bn| * |w|. It returns one on success or zero on |
| 310 | // allocation failure. |
| 311 | OPENSSL_EXPORT int BN_mul_word(BIGNUM *bn, BN_ULONG w); |
| 312 |
| 313 | // BN_sqr sets |r| = |a|^2 (i.e. squares), where |r| may be the same pointer as |
| 314 | // |a|. Returns one on success and zero otherwise. This is more efficient than |
| 315 | // BN_mul(r, a, a, ctx). |
| 316 | OPENSSL_EXPORT int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); |
| 317 |
| 318 | // BN_div divides |numerator| by |divisor| and places the result in |quotient| |
| 319 | // and the remainder in |rem|. Either of |quotient| or |rem| may be NULL, in |
| 320 | // which case the respective value is not returned. It returns one on success or |
| 321 | // zero on error. It is an error condition if |divisor| is zero. |
| 322 | // |
| 323 | // The outputs will be such that |quotient| * |divisor| + |rem| = |numerator|, |
| 324 | // with the quotient rounded towards zero. Thus, if |numerator| is negative, |
| 325 | // |rem| will be zero or negative. If |divisor| is negative, the sign of |
| 326 | // |quotient| will be flipped to compensate but otherwise rounding will be as if |
| 327 | // |divisor| were its absolute value. |
| 328 | OPENSSL_EXPORT int BN_div(BIGNUM *quotient, BIGNUM *rem, |
| 329 | const BIGNUM *numerator, const BIGNUM *divisor, |
| 330 | BN_CTX *ctx); |
| 331 |
| 332 | // BN_div_word sets |numerator| = |numerator|/|divisor| and returns the |
| 333 | // remainder or (BN_ULONG)-1 on error. |
| 334 | OPENSSL_EXPORT BN_ULONG BN_div_word(BIGNUM *numerator, BN_ULONG divisor); |
| 335 |
| 336 | // BN_sqrt sets |*out_sqrt| (which may be the same |BIGNUM| as |in|) to the |
| 337 | // square root of |in|, using |ctx|. It returns one on success or zero on |
| 338 | // error. Negative numbers and non-square numbers will result in an error with |
| 339 | // appropriate errors on the error queue. |
| 340 | OPENSSL_EXPORT int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx); |
| 341 |
| 342 |
| 343 | // Comparison functions |
| 344 |
| 345 | // BN_cmp returns a value less than, equal to or greater than zero if |a| is |
| 346 | // less than, equal to or greater than |b|, respectively. |
| 347 | OPENSSL_EXPORT int BN_cmp(const BIGNUM *a, const BIGNUM *b); |
| 348 |
| 349 | // BN_cmp_word is like |BN_cmp| except it takes its second argument as a |
| 350 | // |BN_ULONG| instead of a |BIGNUM|. |
| 351 | OPENSSL_EXPORT int BN_cmp_word(const BIGNUM *a, BN_ULONG b); |
| 352 |
| 353 | // BN_ucmp returns a value less than, equal to or greater than zero if the |
| 354 | // absolute value of |a| is less than, equal to or greater than the absolute |
| 355 | // value of |b|, respectively. |
| 356 | OPENSSL_EXPORT int BN_ucmp(const BIGNUM *a, const BIGNUM *b); |
| 357 |
| 358 | // BN_equal_consttime returns one if |a| is equal to |b|, and zero otherwise. |
| 359 | // It takes an amount of time dependent on the sizes of |a| and |b|, but |
| 360 | // independent of the contents (including the signs) of |a| and |b|. |
| 361 | OPENSSL_EXPORT int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b); |
| 362 |
| 363 | // BN_abs_is_word returns one if the absolute value of |bn| equals |w| and zero |
| 364 | // otherwise. |
| 365 | OPENSSL_EXPORT int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w); |
| 366 |
| 367 | // BN_is_zero returns one if |bn| is zero and zero otherwise. |
| 368 | OPENSSL_EXPORT int BN_is_zero(const BIGNUM *bn); |
| 369 |
| 370 | // BN_is_one returns one if |bn| equals one and zero otherwise. |
| 371 | OPENSSL_EXPORT int BN_is_one(const BIGNUM *bn); |
| 372 |
| 373 | // BN_is_word returns one if |bn| is exactly |w| and zero otherwise. |
| 374 | OPENSSL_EXPORT int BN_is_word(const BIGNUM *bn, BN_ULONG w); |
| 375 |
| 376 | // BN_is_odd returns one if |bn| is odd and zero otherwise. |
| 377 | OPENSSL_EXPORT int BN_is_odd(const BIGNUM *bn); |
| 378 |
| 379 | // BN_is_pow2 returns 1 if |a| is a power of two, and 0 otherwise. |
| 380 | OPENSSL_EXPORT int BN_is_pow2(const BIGNUM *a); |
| 381 |
| 382 |
| 383 | // Bitwise operations. |
| 384 |
| 385 | // BN_lshift sets |r| equal to |a| << n. The |a| and |r| arguments may be the |
| 386 | // same |BIGNUM|. It returns one on success and zero on allocation failure. |
| 387 | OPENSSL_EXPORT int BN_lshift(BIGNUM *r, const BIGNUM *a, int n); |
| 388 |
| 389 | // BN_lshift1 sets |r| equal to |a| << 1, where |r| and |a| may be the same |
| 390 | // pointer. It returns one on success and zero on allocation failure. |
| 391 | OPENSSL_EXPORT int BN_lshift1(BIGNUM *r, const BIGNUM *a); |
| 392 |
| 393 | // BN_rshift sets |r| equal to |a| >> n, where |r| and |a| may be the same |
| 394 | // pointer. It returns one on success and zero on allocation failure. |
| 395 | OPENSSL_EXPORT int BN_rshift(BIGNUM *r, const BIGNUM *a, int n); |
| 396 |
| 397 | // BN_rshift1 sets |r| equal to |a| >> 1, where |r| and |a| may be the same |
| 398 | // pointer. It returns one on success and zero on allocation failure. |
| 399 | OPENSSL_EXPORT int BN_rshift1(BIGNUM *r, const BIGNUM *a); |
| 400 |
| 401 | // BN_set_bit sets the |n|th, least-significant bit in |a|. For example, if |a| |
| 402 | // is 2 then setting bit zero will make it 3. It returns one on success or zero |
| 403 | // on allocation failure. |
| 404 | OPENSSL_EXPORT int BN_set_bit(BIGNUM *a, int n); |
| 405 |
| 406 | // BN_clear_bit clears the |n|th, least-significant bit in |a|. For example, if |
| 407 | // |a| is 3, clearing bit zero will make it two. It returns one on success or |
| 408 | // zero on allocation failure. |
| 409 | OPENSSL_EXPORT int BN_clear_bit(BIGNUM *a, int n); |
| 410 |
| 411 | // BN_is_bit_set returns one if the |n|th least-significant bit in |a| exists |
| 412 | // and is set. Otherwise, it returns zero. |
| 413 | OPENSSL_EXPORT int BN_is_bit_set(const BIGNUM *a, int n); |
| 414 |
| 415 | // BN_mask_bits truncates |a| so that it is only |n| bits long. It returns one |
| 416 | // on success or zero if |n| is negative. |
| 417 | // |
| 418 | // This differs from OpenSSL which additionally returns zero if |a|'s word |
| 419 | // length is less than or equal to |n|, rounded down to a number of words. Note |
| 420 | // word size is platform-dependent, so this behavior is also difficult to rely |
| 421 | // on in OpenSSL and not very useful. |
| 422 | OPENSSL_EXPORT int BN_mask_bits(BIGNUM *a, int n); |
| 423 |
| 424 | // BN_count_low_zero_bits returns the number of low-order zero bits in |bn|, or |
| 425 | // the number of factors of two which divide it. It returns zero if |bn| is |
| 426 | // zero. |
| 427 | OPENSSL_EXPORT int BN_count_low_zero_bits(const BIGNUM *bn); |
| 428 |
| 429 |
| 430 | // Modulo arithmetic. |
| 431 |
| 432 | // BN_mod_word returns |a| mod |w| or (BN_ULONG)-1 on error. |
| 433 | OPENSSL_EXPORT BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); |
| 434 |
| 435 | // BN_mod_pow2 sets |r| = |a| mod 2^|e|. It returns 1 on success and |
| 436 | // 0 on error. |
| 437 | OPENSSL_EXPORT int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e); |
| 438 |
| 439 | // BN_nnmod_pow2 sets |r| = |a| mod 2^|e| where |r| is always positive. |
| 440 | // It returns 1 on success and 0 on error. |
| 441 | OPENSSL_EXPORT int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e); |
| 442 |
| 443 | // BN_mod is a helper macro that calls |BN_div| and discards the quotient. |
| 444 | #define BN_mod(rem, numerator, divisor, ctx) \ |
| 445 | BN_div(NULL, (rem), (numerator), (divisor), (ctx)) |
| 446 |
| 447 | // BN_nnmod is a non-negative modulo function. It acts like |BN_mod|, but 0 <= |
| 448 | // |rem| < |divisor| is always true. It returns one on success and zero on |
| 449 | // error. |
| 450 | OPENSSL_EXPORT int BN_nnmod(BIGNUM *rem, const BIGNUM *numerator, |
| 451 | const BIGNUM *divisor, BN_CTX *ctx); |
| 452 |
| 453 | // BN_mod_add sets |r| = |a| + |b| mod |m|. It returns one on success and zero |
| 454 | // on error. |
| 455 | OPENSSL_EXPORT int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 456 | const BIGNUM *m, BN_CTX *ctx); |
| 457 |
| 458 | // BN_mod_add_quick acts like |BN_mod_add| but requires that |a| and |b| be |
| 459 | // non-negative and less than |m|. |
| 460 | OPENSSL_EXPORT int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 461 | const BIGNUM *m); |
| 462 |
| 463 | // BN_mod_sub sets |r| = |a| - |b| mod |m|. It returns one on success and zero |
| 464 | // on error. |
| 465 | OPENSSL_EXPORT int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 466 | const BIGNUM *m, BN_CTX *ctx); |
| 467 |
| 468 | // BN_mod_sub_quick acts like |BN_mod_sub| but requires that |a| and |b| be |
| 469 | // non-negative and less than |m|. |
| 470 | OPENSSL_EXPORT int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 471 | const BIGNUM *m); |
| 472 |
| 473 | // BN_mod_mul sets |r| = |a|*|b| mod |m|. It returns one on success and zero |
| 474 | // on error. |
| 475 | OPENSSL_EXPORT int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 476 | const BIGNUM *m, BN_CTX *ctx); |
| 477 |
| 478 | // BN_mod_sqr sets |r| = |a|^2 mod |m|. It returns one on success and zero |
| 479 | // on error. |
| 480 | OPENSSL_EXPORT int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, |
| 481 | BN_CTX *ctx); |
| 482 |
| 483 | // BN_mod_lshift sets |r| = (|a| << n) mod |m|, where |r| and |a| may be the |
| 484 | // same pointer. It returns one on success and zero on error. |
| 485 | OPENSSL_EXPORT int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, |
| 486 | const BIGNUM *m, BN_CTX *ctx); |
| 487 |
| 488 | // BN_mod_lshift_quick acts like |BN_mod_lshift| but requires that |a| be |
| 489 | // non-negative and less than |m|. |
| 490 | OPENSSL_EXPORT int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, |
| 491 | const BIGNUM *m); |
| 492 |
| 493 | // BN_mod_lshift1 sets |r| = (|a| << 1) mod |m|, where |r| and |a| may be the |
| 494 | // same pointer. It returns one on success and zero on error. |
| 495 | OPENSSL_EXPORT int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, |
| 496 | BN_CTX *ctx); |
| 497 |
| 498 | // BN_mod_lshift1_quick acts like |BN_mod_lshift1| but requires that |a| be |
| 499 | // non-negative and less than |m|. |
| 500 | OPENSSL_EXPORT int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, |
| 501 | const BIGNUM *m); |
| 502 |
| 503 | // BN_mod_sqrt returns a newly-allocated |BIGNUM|, r, such that |
| 504 | // r^2 == a (mod p). It returns NULL on error or if |a| is not a square mod |p|. |
| 505 | // In the latter case, it will add |BN_R_NOT_A_SQUARE| to the error queue. |
| 506 | // If |a| is a square and |p| > 2, there are two possible square roots. This |
| 507 | // function may return either and may even select one non-deterministically. |
| 508 | // |
| 509 | // This function only works if |p| is a prime. If |p| is composite, it may fail |
| 510 | // or return an arbitrary value. Callers should not pass attacker-controlled |
| 511 | // values of |p|. |
| 512 | OPENSSL_EXPORT BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, |
| 513 | BN_CTX *ctx); |
| 514 |
| 515 |
| 516 | // Random and prime number generation. |
| 517 |
| 518 | // The following are values for the |top| parameter of |BN_rand|. |
| 519 | #define BN_RAND_TOP_ANY (-1) |
| 520 | #define BN_RAND_TOP_ONE 0 |
| 521 | #define BN_RAND_TOP_TWO 1 |
| 522 |
| 523 | // The following are values for the |bottom| parameter of |BN_rand|. |
| 524 | #define BN_RAND_BOTTOM_ANY 0 |
| 525 | #define BN_RAND_BOTTOM_ODD 1 |
| 526 |
| 527 | // BN_rand sets |rnd| to a random number of length |bits|. It returns one on |
| 528 | // success and zero otherwise. |
| 529 | // |
| 530 | // |top| must be one of the |BN_RAND_TOP_*| values. If |BN_RAND_TOP_ONE|, the |
| 531 | // most-significant bit, if any, will be set. If |BN_RAND_TOP_TWO|, the two |
| 532 | // most significant bits, if any, will be set. If |BN_RAND_TOP_ANY|, no extra |
| 533 | // action will be taken and |BN_num_bits(rnd)| may not equal |bits| if the most |
| 534 | // significant bits randomly ended up as zeros. |
| 535 | // |
| 536 | // |bottom| must be one of the |BN_RAND_BOTTOM_*| values. If |
| 537 | // |BN_RAND_BOTTOM_ODD|, the least-significant bit, if any, will be set. If |
| 538 | // |BN_RAND_BOTTOM_ANY|, no extra action will be taken. |
| 539 | OPENSSL_EXPORT int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); |
| 540 |
| 541 | // BN_pseudo_rand is an alias for |BN_rand|. |
| 542 | OPENSSL_EXPORT int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); |
| 543 |
| 544 | // BN_rand_range is equivalent to |BN_rand_range_ex| with |min_inclusive| set |
| 545 | // to zero and |max_exclusive| set to |range|. |
| 546 | OPENSSL_EXPORT int BN_rand_range(BIGNUM *rnd, const BIGNUM *range); |
| 547 |
| 548 | // BN_rand_range_ex sets |rnd| to a random value in |
| 549 | // [min_inclusive..max_exclusive). It returns one on success and zero |
| 550 | // otherwise. |
| 551 | OPENSSL_EXPORT int BN_rand_range_ex(BIGNUM *r, BN_ULONG min_inclusive, |
| 552 | const BIGNUM *max_exclusive); |
| 553 |
| 554 | // BN_pseudo_rand_range is an alias for BN_rand_range. |
| 555 | OPENSSL_EXPORT int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range); |
| 556 |
| 557 | #define BN_GENCB_GENERATED 0 |
| 558 | #define BN_GENCB_PRIME_TEST 1 |
| 559 |
| 560 | // bn_gencb_st, or |BN_GENCB|, holds a callback function that is used by |
| 561 | // generation functions that can take a very long time to complete. Use |
| 562 | // |BN_GENCB_set| to initialise a |BN_GENCB| structure. |
| 563 | // |
| 564 | // The callback receives the address of that |BN_GENCB| structure as its last |
| 565 | // argument and the user is free to put an arbitrary pointer in |arg|. The other |
| 566 | // arguments are set as follows: |
| 567 | // - event=BN_GENCB_GENERATED, n=i: after generating the i'th possible prime |
| 568 | // number. |
| 569 | // - event=BN_GENCB_PRIME_TEST, n=-1: when finished trial division primality |
| 570 | // checks. |
| 571 | // - event=BN_GENCB_PRIME_TEST, n=i: when the i'th primality test has finished. |
| 572 | // |
| 573 | // The callback can return zero to abort the generation progress or one to |
| 574 | // allow it to continue. |
| 575 | // |
| 576 | // When other code needs to call a BN generation function it will often take a |
| 577 | // BN_GENCB argument and may call the function with other argument values. |
| 578 | struct bn_gencb_st { |
| 579 | void *arg; // callback-specific data |
| 580 | int (*callback)(int event, int n, struct bn_gencb_st *); |
| 581 | }; |
| 582 |
| 583 | // BN_GENCB_new returns a newly-allocated |BN_GENCB| object, or NULL on |
| 584 | // allocation failure. The result must be released with |BN_GENCB_free| when |
| 585 | // done. |
| 586 | OPENSSL_EXPORT BN_GENCB *BN_GENCB_new(void); |
| 587 |
| 588 | // BN_GENCB_free releases memory associated with |callback|. |
| 589 | OPENSSL_EXPORT void BN_GENCB_free(BN_GENCB *callback); |
| 590 |
| 591 | // BN_GENCB_set configures |callback| to call |f| and sets |callout->arg| to |
| 592 | // |arg|. |
| 593 | OPENSSL_EXPORT void BN_GENCB_set(BN_GENCB *callback, |
| 594 | int (*f)(int event, int n, BN_GENCB *), |
| 595 | void *arg); |
| 596 |
| 597 | // BN_GENCB_call calls |callback|, if not NULL, and returns the return value of |
| 598 | // the callback, or 1 if |callback| is NULL. |
| 599 | OPENSSL_EXPORT int BN_GENCB_call(BN_GENCB *callback, int event, int n); |
| 600 |
| 601 | // BN_GENCB_get_arg returns |callback->arg|. |
| 602 | OPENSSL_EXPORT void *BN_GENCB_get_arg(const BN_GENCB *callback); |
| 603 |
| 604 | // BN_generate_prime_ex sets |ret| to a prime number of |bits| length. If safe |
| 605 | // is non-zero then the prime will be such that (ret-1)/2 is also a prime. |
| 606 | // (This is needed for Diffie-Hellman groups to ensure that the only subgroups |
| 607 | // are of size 2 and (p-1)/2.). |
| 608 | // |
| 609 | // If |add| is not NULL, the prime will fulfill the condition |ret| % |add| == |
| 610 | // |rem| in order to suit a given generator. (If |rem| is NULL then |ret| % |
| 611 | // |add| == 1.) |
| 612 | // |
| 613 | // If |cb| is not NULL, it will be called during processing to give an |
| 614 | // indication of progress. See the comments for |BN_GENCB|. It returns one on |
| 615 | // success and zero otherwise. |
| 616 | OPENSSL_EXPORT int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, |
| 617 | const BIGNUM *add, const BIGNUM *rem, |
| 618 | BN_GENCB *cb); |
| 619 |
| 620 | // BN_prime_checks_for_validation can be used as the |checks| argument to the |
| 621 | // primarily testing functions when validating an externally-supplied candidate |
| 622 | // prime. It gives a false positive rate of at most 2^{-128}. (The worst case |
| 623 | // false positive rate for a single iteration is 1/4 per |
| 624 | // https://eprint.iacr.org/2018/749. (1/4)^64 = 2^{-128}.) |
| 625 | #define BN_prime_checks_for_validation 64 |
| 626 |
| 627 | // BN_prime_checks_for_generation can be used as the |checks| argument to the |
| 628 | // primality testing functions when generating random primes. It gives a false |
| 629 | // positive rate at most the security level of the corresponding RSA key size. |
| 630 | // |
| 631 | // Note this value only performs enough checks if the candidate prime was |
| 632 | // selected randomly. If validating an externally-supplied candidate, especially |
| 633 | // one that may be selected adversarially, use |BN_prime_checks_for_validation| |
| 634 | // instead. |
| 635 | #define BN_prime_checks_for_generation 0 |
| 636 |
| 637 | // bn_primality_result_t enumerates the outcomes of primality-testing. |
| 638 | enum bn_primality_result_t { |
| 639 | bn_probably_prime, |
| 640 | bn_composite, |
| 641 | bn_non_prime_power_composite, |
| 642 | }; |
| 643 |
| 644 | // BN_enhanced_miller_rabin_primality_test tests whether |w| is probably a prime |
| 645 | // number using the Enhanced Miller-Rabin Test (FIPS 186-4 C.3.2) with |
| 646 | // |checks| iterations and returns the result in |out_result|. Enhanced |
| 647 | // Miller-Rabin tests primality for odd integers greater than 3, returning |
| 648 | // |bn_probably_prime| if the number is probably prime, |
| 649 | // |bn_non_prime_power_composite| if the number is a composite that is not the |
| 650 | // power of a single prime, and |bn_composite| otherwise. It returns one on |
| 651 | // success and zero on failure. If |cb| is not NULL, then it is called during |
| 652 | // each iteration of the primality test. |
| 653 | // |
| 654 | // See |BN_prime_checks_for_validation| and |BN_prime_checks_for_generation| for |
| 655 | // recommended values of |checks|. |
| 656 | OPENSSL_EXPORT int BN_enhanced_miller_rabin_primality_test( |
| 657 | enum bn_primality_result_t *out_result, const BIGNUM *w, int checks, |
| 658 | BN_CTX *ctx, BN_GENCB *cb); |
| 659 |
| 660 | // BN_primality_test sets |*is_probably_prime| to one if |candidate| is |
| 661 | // probably a prime number by the Miller-Rabin test or zero if it's certainly |
| 662 | // not. |
| 663 | // |
| 664 | // If |do_trial_division| is non-zero then |candidate| will be tested against a |
| 665 | // list of small primes before Miller-Rabin tests. The probability of this |
| 666 | // function returning a false positive is at most 2^{2*checks}. See |
| 667 | // |BN_prime_checks_for_validation| and |BN_prime_checks_for_generation| for |
| 668 | // recommended values of |checks|. |
| 669 | // |
| 670 | // If |cb| is not NULL then it is called during the checking process. See the |
| 671 | // comment above |BN_GENCB|. |
| 672 | // |
| 673 | // The function returns one on success and zero on error. |
| 674 | OPENSSL_EXPORT int BN_primality_test(int *is_probably_prime, |
| 675 | const BIGNUM *candidate, int checks, |
| 676 | BN_CTX *ctx, int do_trial_division, |
| 677 | BN_GENCB *cb); |
| 678 |
| 679 | // BN_is_prime_fasttest_ex returns one if |candidate| is probably a prime |
| 680 | // number by the Miller-Rabin test, zero if it's certainly not and -1 on error. |
| 681 | // |
| 682 | // If |do_trial_division| is non-zero then |candidate| will be tested against a |
| 683 | // list of small primes before Miller-Rabin tests. The probability of this |
| 684 | // function returning one when |candidate| is composite is at most 2^{2*checks}. |
| 685 | // See |BN_prime_checks_for_validation| and |BN_prime_checks_for_generation| for |
| 686 | // recommended values of |checks|. |
| 687 | // |
| 688 | // If |cb| is not NULL then it is called during the checking process. See the |
| 689 | // comment above |BN_GENCB|. |
| 690 | // |
| 691 | // WARNING: deprecated. Use |BN_primality_test|. |
| 692 | OPENSSL_EXPORT int BN_is_prime_fasttest_ex(const BIGNUM *candidate, int checks, |
| 693 | BN_CTX *ctx, int do_trial_division, |
| 694 | BN_GENCB *cb); |
| 695 |
| 696 | // BN_is_prime_ex acts the same as |BN_is_prime_fasttest_ex| with |
| 697 | // |do_trial_division| set to zero. |
| 698 | // |
| 699 | // WARNING: deprecated: Use |BN_primality_test|. |
| 700 | OPENSSL_EXPORT int BN_is_prime_ex(const BIGNUM *candidate, int checks, |
| 701 | BN_CTX *ctx, BN_GENCB *cb); |
| 702 |
| 703 |
| 704 | // Number theory functions |
| 705 |
| 706 | // BN_gcd sets |r| = gcd(|a|, |b|). It returns one on success and zero |
| 707 | // otherwise. |
| 708 | OPENSSL_EXPORT int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 709 | BN_CTX *ctx); |
| 710 |
| 711 | // BN_mod_inverse sets |out| equal to |a|^-1, mod |n|. If |out| is NULL, a |
| 712 | // fresh BIGNUM is allocated. It returns the result or NULL on error. |
| 713 | // |
| 714 | // If |n| is even then the operation is performed using an algorithm that avoids |
| 715 | // some branches but which isn't constant-time. This function shouldn't be used |
| 716 | // for secret values; use |BN_mod_inverse_blinded| instead. Or, if |n| is |
| 717 | // guaranteed to be prime, use |
| 718 | // |BN_mod_exp_mont_consttime(out, a, m_minus_2, m, ctx, m_mont)|, taking |
| 719 | // advantage of Fermat's Little Theorem. |
| 720 | OPENSSL_EXPORT BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, |
| 721 | const BIGNUM *n, BN_CTX *ctx); |
| 722 |
| 723 | // BN_mod_inverse_blinded sets |out| equal to |a|^-1, mod |n|, where |n| is the |
| 724 | // Montgomery modulus for |mont|. |a| must be non-negative and must be less |
| 725 | // than |n|. |n| must be greater than 1. |a| is blinded (masked by a random |
| 726 | // value) to protect it against side-channel attacks. On failure, if the failure |
| 727 | // was caused by |a| having no inverse mod |n| then |*out_no_inverse| will be |
| 728 | // set to one; otherwise it will be set to zero. |
| 729 | // |
| 730 | // Note this function may incorrectly report |a| has no inverse if the random |
| 731 | // blinding value has no inverse. It should only be used when |n| has few |
| 732 | // non-invertible elements, such as an RSA modulus. |
| 733 | OPENSSL_EXPORT int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, |
| 734 | const BIGNUM *a, |
| 735 | const BN_MONT_CTX *mont, BN_CTX *ctx); |
| 736 |
| 737 | // BN_mod_inverse_odd sets |out| equal to |a|^-1, mod |n|. |a| must be |
| 738 | // non-negative and must be less than |n|. |n| must be odd. This function |
| 739 | // shouldn't be used for secret values; use |BN_mod_inverse_blinded| instead. |
| 740 | // Or, if |n| is guaranteed to be prime, use |
| 741 | // |BN_mod_exp_mont_consttime(out, a, m_minus_2, m, ctx, m_mont)|, taking |
| 742 | // advantage of Fermat's Little Theorem. It returns one on success or zero on |
| 743 | // failure. On failure, if the failure was caused by |a| having no inverse mod |
| 744 | // |n| then |*out_no_inverse| will be set to one; otherwise it will be set to |
| 745 | // zero. |
| 746 | int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, |
| 747 | const BIGNUM *n, BN_CTX *ctx); |
| 748 |
| 749 |
| 750 | // Montgomery arithmetic. |
| 751 |
| 752 | // BN_MONT_CTX contains the precomputed values needed to work in a specific |
| 753 | // Montgomery domain. |
| 754 |
| 755 | // BN_MONT_CTX_new_for_modulus returns a fresh |BN_MONT_CTX| given the modulus, |
| 756 | // |mod| or NULL on error. Note this function assumes |mod| is public. |
| 757 | OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, |
| 758 | BN_CTX *ctx); |
| 759 |
| 760 | // BN_MONT_CTX_new_consttime behaves like |BN_MONT_CTX_new_for_modulus| but |
| 761 | // treats |mod| as secret. |
| 762 | OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new_consttime(const BIGNUM *mod, |
| 763 | BN_CTX *ctx); |
| 764 |
| 765 | // BN_MONT_CTX_free frees memory associated with |mont|. |
| 766 | OPENSSL_EXPORT void BN_MONT_CTX_free(BN_MONT_CTX *mont); |
| 767 |
| 768 | // BN_MONT_CTX_copy sets |to| equal to |from|. It returns |to| on success or |
| 769 | // NULL on error. |
| 770 | OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, |
| 771 | const BN_MONT_CTX *from); |
| 772 |
| 773 | // BN_to_montgomery sets |ret| equal to |a| in the Montgomery domain. |a| is |
| 774 | // assumed to be in the range [0, n), where |n| is the Montgomery modulus. It |
| 775 | // returns one on success or zero on error. |
| 776 | OPENSSL_EXPORT int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, |
| 777 | const BN_MONT_CTX *mont, BN_CTX *ctx); |
| 778 |
| 779 | // BN_from_montgomery sets |ret| equal to |a| * R^-1, i.e. translates values out |
| 780 | // of the Montgomery domain. |a| is assumed to be in the range [0, n*R), where |
| 781 | // |n| is the Montgomery modulus. Note n < R, so inputs in the range [0, n*n) |
| 782 | // are valid. This function returns one on success or zero on error. |
| 783 | OPENSSL_EXPORT int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, |
| 784 | const BN_MONT_CTX *mont, BN_CTX *ctx); |
| 785 |
| 786 | // BN_mod_mul_montgomery set |r| equal to |a| * |b|, in the Montgomery domain. |
| 787 | // Both |a| and |b| must already be in the Montgomery domain (by |
| 788 | // |BN_to_montgomery|). In particular, |a| and |b| are assumed to be in the |
| 789 | // range [0, n), where |n| is the Montgomery modulus. It returns one on success |
| 790 | // or zero on error. |
| 791 | OPENSSL_EXPORT int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, |
| 792 | const BIGNUM *b, |
| 793 | const BN_MONT_CTX *mont, BN_CTX *ctx); |
| 794 |
| 795 |
| 796 | // Exponentiation. |
| 797 |
| 798 | // BN_exp sets |r| equal to |a|^{|p|}. It does so with a square-and-multiply |
| 799 | // algorithm that leaks side-channel information. It returns one on success or |
| 800 | // zero otherwise. |
| 801 | OPENSSL_EXPORT int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
| 802 | BN_CTX *ctx); |
| 803 |
| 804 | // BN_mod_exp sets |r| equal to |a|^{|p|} mod |m|. It does so with the best |
| 805 | // algorithm for the values provided. It returns one on success or zero |
| 806 | // otherwise. The |BN_mod_exp_mont_consttime| variant must be used if the |
| 807 | // exponent is secret. |
| 808 | OPENSSL_EXPORT int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
| 809 | const BIGNUM *m, BN_CTX *ctx); |
| 810 |
| 811 | // BN_mod_exp_mont behaves like |BN_mod_exp| but treats |a| as secret and |
| 812 | // requires 0 <= |a| < |m|. |
| 813 | OPENSSL_EXPORT int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
| 814 | const BIGNUM *m, BN_CTX *ctx, |
| 815 | const BN_MONT_CTX *mont); |
| 816 |
| 817 | // BN_mod_exp_mont_consttime behaves like |BN_mod_exp| but treats |a|, |p|, and |
| 818 | // |m| as secret and requires 0 <= |a| < |m|. |
| 819 | OPENSSL_EXPORT int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, |
| 820 | const BIGNUM *p, const BIGNUM *m, |
| 821 | BN_CTX *ctx, |
| 822 | const BN_MONT_CTX *mont); |
| 823 |
| 824 |
| 825 | // Deprecated functions |
| 826 |
| 827 | // BN_bn2mpi serialises the value of |in| to |out|, using a format that consists |
| 828 | // of the number's length in bytes represented as a 4-byte big-endian number, |
| 829 | // and the number itself in big-endian format, where the most significant bit |
| 830 | // signals a negative number. (The representation of numbers with the MSB set is |
| 831 | // prefixed with null byte). |out| must have sufficient space available; to |
| 832 | // find the needed amount of space, call the function with |out| set to NULL. |
| 833 | OPENSSL_EXPORT size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out); |
| 834 |
| 835 | // BN_mpi2bn parses |len| bytes from |in| and returns the resulting value. The |
| 836 | // bytes at |in| are expected to be in the format emitted by |BN_bn2mpi|. |
| 837 | // |
| 838 | // If |out| is NULL then a fresh |BIGNUM| is allocated and returned, otherwise |
| 839 | // |out| is reused and returned. On error, NULL is returned and the error queue |
| 840 | // is updated. |
| 841 | OPENSSL_EXPORT BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out); |
| 842 |
| 843 | // BN_mod_exp_mont_word is like |BN_mod_exp_mont| except that the base |a| is |
| 844 | // given as a |BN_ULONG| instead of a |BIGNUM *|. It returns one on success |
| 845 | // or zero otherwise. |
| 846 | OPENSSL_EXPORT int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, |
| 847 | const BIGNUM *m, BN_CTX *ctx, |
| 848 | const BN_MONT_CTX *mont); |
| 849 |
| 850 | // BN_mod_exp2_mont calculates (a1^p1) * (a2^p2) mod m. It returns 1 on success |
| 851 | // or zero otherwise. |
| 852 | OPENSSL_EXPORT int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, |
| 853 | const BIGNUM *p1, const BIGNUM *a2, |
| 854 | const BIGNUM *p2, const BIGNUM *m, |
| 855 | BN_CTX *ctx, const BN_MONT_CTX *mont); |
| 856 |
| 857 | // BN_MONT_CTX_new returns a fresh |BN_MONT_CTX| or NULL on allocation failure. |
| 858 | // Use |BN_MONT_CTX_new_for_modulus| instead. |
| 859 | OPENSSL_EXPORT BN_MONT_CTX *BN_MONT_CTX_new(void); |
| 860 |
| 861 | // BN_MONT_CTX_set sets up a Montgomery context given the modulus, |mod|. It |
| 862 | // returns one on success and zero on error. Use |BN_MONT_CTX_new_for_modulus| |
| 863 | // instead. |
| 864 | OPENSSL_EXPORT int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, |
| 865 | BN_CTX *ctx); |
| 866 |
| 867 | // BN_bn2binpad behaves like |BN_bn2bin_padded|, but it returns |len| on success |
| 868 | // and -1 on error. |
| 869 | // |
| 870 | // Use |BN_bn2bin_padded| instead. It is |size_t|-clean. |
| 871 | OPENSSL_EXPORT int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len); |
| 872 |
| 873 | // BN_bn2lebinpad behaves like |BN_bn2le_padded|, but it returns |len| on |
| 874 | // success and -1 on error. |
| 875 | // |
| 876 | // Use |BN_bn2le_padded| instead. It is |size_t|-clean. |
| 877 | OPENSSL_EXPORT int BN_bn2lebinpad(const BIGNUM *in, uint8_t *out, int len); |
| 878 |
| 879 | // BN_prime_checks is a deprecated alias for |BN_prime_checks_for_validation|. |
| 880 | // Use |BN_prime_checks_for_generation| or |BN_prime_checks_for_validation| |
| 881 | // instead. (This defaults to the |_for_validation| value in order to be |
| 882 | // conservative.) |
| 883 | #define BN_prime_checks BN_prime_checks_for_validation |
| 884 |
| 885 | // BN_secure_new calls |BN_new|. |
| 886 | OPENSSL_EXPORT BIGNUM *BN_secure_new(void); |
| 887 |
| 888 | // BN_le2bn calls |BN_lebin2bn|. |
| 889 | OPENSSL_EXPORT BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret); |
| 890 |
| 891 |
| 892 | // Private functions |
| 893 |
| 894 | struct bignum_st { |
| 895 | // d is a pointer to an array of |width| |BN_BITS2|-bit chunks in |
| 896 | // little-endian order. This stores the absolute value of the number. |
| 897 | BN_ULONG *d; |
| 898 | // width is the number of elements of |d| which are valid. This value is not |
| 899 | // necessarily minimal; the most-significant words of |d| may be zero. |
| 900 | // |width| determines a potentially loose upper-bound on the absolute value |
| 901 | // of the |BIGNUM|. |
| 902 | // |
| 903 | // Functions taking |BIGNUM| inputs must compute the same answer for all |
| 904 | // possible widths. |bn_minimal_width|, |bn_set_minimal_width|, and other |
| 905 | // helpers may be used to recover the minimal width, provided it is not |
| 906 | // secret. If it is secret, use a different algorithm. Functions may output |
| 907 | // minimal or non-minimal |BIGNUM|s depending on secrecy requirements, but |
| 908 | // those which cause widths to unboundedly grow beyond the minimal value |
| 909 | // should be documented such. |
| 910 | // |
| 911 | // Note this is different from historical |BIGNUM| semantics. |
| 912 | int width; |
| 913 | // dmax is number of elements of |d| which are allocated. |
| 914 | int dmax; |
| 915 | // neg is one if the number if negative and zero otherwise. |
| 916 | int neg; |
| 917 | // flags is a bitmask of |BN_FLG_*| values |
| 918 | int flags; |
| 919 | }; |
| 920 |
| 921 | struct bn_mont_ctx_st { |
| 922 | // RR is R^2, reduced modulo |N|. It is used to convert to Montgomery form. It |
| 923 | // is guaranteed to have the same width as |N|. |
| 924 | BIGNUM RR; |
| 925 | // N is the modulus. It is always stored in minimal form, so |N.width| |
| 926 | // determines R. |
| 927 | BIGNUM N; |
| 928 | BN_ULONG n0[2]; // least significant words of (R*Ri-1)/N |
| 929 | }; |
| 930 |
| 931 | OPENSSL_EXPORT unsigned BN_num_bits_word(BN_ULONG l); |
| 932 |
| 933 | #define BN_FLG_MALLOCED 0x01 |
| 934 | #define BN_FLG_STATIC_DATA 0x02 |
| 935 | // |BN_FLG_CONSTTIME| has been removed and intentionally omitted so code relying |
| 936 | // on it will not compile. Consumers outside BoringSSL should use the |
| 937 | // higher-level cryptographic algorithms exposed by other modules. Consumers |
| 938 | // within the library should call the appropriate timing-sensitive algorithm |
| 939 | // directly. |
| 940 |
| 941 |
| 942 | #if defined(__cplusplus) |
| 943 | } // extern C |
| 944 |
| 945 | #if !defined(BORINGSSL_NO_CXX) |
| 946 | extern "C++" { |
| 947 |
| 948 | BSSL_NAMESPACE_BEGIN |
| 949 |
| 950 | BORINGSSL_MAKE_DELETER(BIGNUM, BN_free) |
| 951 | BORINGSSL_MAKE_DELETER(BN_CTX, BN_CTX_free) |
| 952 | BORINGSSL_MAKE_DELETER(BN_MONT_CTX, BN_MONT_CTX_free) |
| 953 |
| 954 | class BN_CTXScope { |
| 955 | public: |
| 956 | BN_CTXScope(BN_CTX *ctx) : ctx_(ctx) { BN_CTX_start(ctx_); } |
| 957 | ~BN_CTXScope() { BN_CTX_end(ctx_); } |
| 958 |
| 959 | private: |
| 960 | BN_CTX *ctx_; |
| 961 |
| 962 | BN_CTXScope(BN_CTXScope &) = delete; |
| 963 | BN_CTXScope &operator=(BN_CTXScope &) = delete; |
| 964 | }; |
| 965 |
| 966 | BSSL_NAMESPACE_END |
| 967 |
| 968 | } // extern C++ |
| 969 | #endif |
| 970 |
| 971 | #endif |
| 972 |
| 973 | #define BN_R_ARG2_LT_ARG3 100 |
| 974 | #define BN_R_BAD_RECIPROCAL 101 |
| 975 | #define BN_R_BIGNUM_TOO_LONG 102 |
| 976 | #define BN_R_BITS_TOO_SMALL 103 |
| 977 | #define BN_R_CALLED_WITH_EVEN_MODULUS 104 |
| 978 | #define BN_R_DIV_BY_ZERO 105 |
| 979 | #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 106 |
| 980 | #define BN_R_INPUT_NOT_REDUCED 107 |
| 981 | #define BN_R_INVALID_RANGE 108 |
| 982 | #define BN_R_NEGATIVE_NUMBER 109 |
| 983 | #define BN_R_NOT_A_SQUARE 110 |
| 984 | #define BN_R_NOT_INITIALIZED 111 |
| 985 | #define BN_R_NO_INVERSE 112 |
| 986 | #define BN_R_PRIVATE_KEY_TOO_LARGE 113 |
| 987 | #define BN_R_P_IS_NOT_PRIME 114 |
| 988 | #define BN_R_TOO_MANY_ITERATIONS 115 |
| 989 | #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 116 |
| 990 | #define BN_R_BAD_ENCODING 117 |
| 991 | #define BN_R_ENCODE_ERROR 118 |
| 992 | #define BN_R_INVALID_INPUT 119 |
| 993 |
| 994 | #endif // OPENSSL_HEADER_BN_H |
| 995 |