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