Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bn/internal.h

raw
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_INTERNAL_H
12 #define OPENSSL_HEADER_BN_INTERNAL_H
13
14 #include <openssl/bn.h>
15 #include <openssl/rand.h>
16
17 #if defined(OPENSSL_X86_64) && defined(_MSC_VER)
18 OPENSSL_MSVC_PRAGMA(warning(push, 3))
19 #include <intrin.h>
20 OPENSSL_MSVC_PRAGMA(warning(pop))
21 #pragma intrinsic(__umulh, _umul128)
22 #endif
23
24 #include "../../internal.h"
25 #include "../cpucap/internal.h"
26
27 #if defined(__cplusplus)
28 extern "C" {
29 #endif
30
31 #if defined(OPENSSL_64_BIT)
32
33 #if defined(BORINGSSL_HAS_UINT128)
34 // MSVC doesn't support two-word integers on 64-bit.
35 #define BN_ULLONG uint128_t
36 #if defined(BORINGSSL_CAN_DIVIDE_UINT128)
37 #define BN_CAN_DIVIDE_ULLONG
38 #endif
39 #endif
40
41 #define BN_BITS2 64
42 #define BN_BITS2_LG 6
43 #define BN_BYTES 8
44 #define BN_BITS4 32
45 #define BN_MASK2 (0xffffffffffffffffUL)
46 #define BN_MASK2l (0xffffffffUL)
47 #define BN_MASK2h (0xffffffff00000000UL)
48 #define BN_MASK2h1 (0xffffffff80000000UL)
49 #define BN_MONT_CTX_N0_LIMBS 1
50 #define BN_DEC_CONV (10000000000000000000UL)
51 #define BN_DEC_NUM 19
52 #define TOBN(hi, lo) ((BN_ULONG)(hi) << 32 | (lo))
53
54 #elif defined(OPENSSL_32_BIT)
55
56 #define BN_ULLONG uint64_t
57 #define BN_CAN_DIVIDE_ULLONG
58 #define BN_BITS2 32
59 #define BN_BITS2_LG 5
60 #define BN_BYTES 4
61 #define BN_BITS4 16
62 #define BN_MASK2 (0xffffffffUL)
63 #define BN_MASK2l (0xffffUL)
64 #define BN_MASK2h1 (0xffff8000UL)
65 #define BN_MASK2h (0xffff0000UL)
66 // On some 32-bit platforms, Montgomery multiplication is done using 64-bit
67 // arithmetic with SIMD instructions. On such platforms, |BN_MONT_CTX::n0|
68 // needs to be two words long. Only certain 32-bit platforms actually make use
69 // of n0[1] and shorter R value would suffice for the others. However,
70 // currently only the assembly files know which is which.
71 #define BN_MONT_CTX_N0_LIMBS 2
72 #define BN_DEC_CONV (1000000000UL)
73 #define BN_DEC_NUM 9
74 #define TOBN(hi, lo) (lo), (hi)
75
76 #else
77 #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
78 #endif
79
80 #if !defined(OPENSSL_NO_ASM) && (defined(__GNUC__) || defined(__clang__))
81 #define BN_CAN_USE_INLINE_ASM
82 #endif
83
84 // MOD_EXP_CTIME_ALIGN is the alignment needed for |BN_mod_exp_mont_consttime|'s
85 // tables.
86 //
87 // TODO(davidben): Historically, this alignment came from cache line
88 // assumptions, which we've since removed. Is 64-byte alignment still necessary
89 // or ideal? The true alignment requirement seems to now be 32 bytes, coming
90 // from RSAZ's use of VMOVDQA to a YMM register. Non-x86_64 has even fewer
91 // requirements.
92 #define MOD_EXP_CTIME_ALIGN 64
93
94 // MOD_EXP_CTIME_STORAGE_LEN is the number of |BN_ULONG|s needed for the
95 // |BN_mod_exp_mont_consttime| stack-allocated storage buffer. The buffer is
96 // just the right size for the RSAZ and is about ~1KB larger than what's
97 // necessary (4480 bytes) for 1024-bit inputs.
98 #define MOD_EXP_CTIME_STORAGE_LEN \
99 (((320u * 3u) + (32u * 9u * 16u)) / sizeof(BN_ULONG))
100
101 #define STATIC_BIGNUM(x) \
102 { \
103 (BN_ULONG *)(x), sizeof(x) / sizeof(BN_ULONG), \
104 sizeof(x) / sizeof(BN_ULONG), 0, BN_FLG_STATIC_DATA \
105 }
106
107 #if defined(BN_ULLONG)
108 #define Lw(t) ((BN_ULONG)(t))
109 #define Hw(t) ((BN_ULONG)((t) >> BN_BITS2))
110 #endif
111
112 #define BN_GENCB_UNSET 0
113 #define BN_GENCB_NEW_STYLE 1
114 #define BN_GENCB_OLD_STYLE 2
115
116 // bn_minimal_width returns the minimal number of words needed to represent
117 // |bn|.
118 int bn_minimal_width(const BIGNUM *bn);
119
120 // bn_set_minimal_width sets |bn->width| to |bn_minimal_width(bn)|. If |bn| is
121 // zero, |bn->neg| is set to zero.
122 void bn_set_minimal_width(BIGNUM *bn);
123
124 // bn_wexpand ensures that |bn| has at least |words| works of space without
125 // altering its value. It returns one on success or zero on allocation
126 // failure.
127 int bn_wexpand(BIGNUM *bn, size_t words);
128
129 // bn_expand acts the same as |bn_wexpand|, but takes a number of bits rather
130 // than a number of words.
131 int bn_expand(BIGNUM *bn, size_t bits);
132
133 // bn_resize_words adjusts |bn->width| to be |words|. It returns one on success
134 // and zero on allocation error or if |bn|'s value is too large.
135 OPENSSL_EXPORT int bn_resize_words(BIGNUM *bn, size_t words);
136
137 // bn_select_words sets |r| to |a| if |mask| is all ones or |b| if |mask| is
138 // all zeros.
139 void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a,
140 const BN_ULONG *b, size_t num);
141
142 // bn_set_words sets |bn| to the value encoded in the |num| words in |words|,
143 // least significant word first.
144 int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
145
146 // bn_set_static_words acts like |bn_set_words|, but doesn't copy the data. A
147 // flag is set on |bn| so that |BN_free| won't attempt to free the data.
148 //
149 // The |STATIC_BIGNUM| macro is probably a better solution for this outside of
150 // the FIPS module. Inside of the FIPS module that macro generates rel.ro data,
151 // which doesn't work with FIPS requirements.
152 void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
153
154 // bn_fits_in_words returns one if |bn| may be represented in |num| words, plus
155 // a sign bit, and zero otherwise.
156 int bn_fits_in_words(const BIGNUM *bn, size_t num);
157
158 // bn_copy_words copies the value of |bn| to |out| and returns one if the value
159 // is representable in |num| words. Otherwise, it returns zero.
160 int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn);
161
162 // bn_assert_fits_in_bytes asserts that |bn| fits in |num| bytes. This is a
163 // no-op in release builds, but triggers an assert in debug builds, and
164 // declassifies all bytes which are therefore known to be zero in constant-time
165 // validation.
166 OPENSSL_EXPORT void bn_assert_fits_in_bytes(const BIGNUM *bn, size_t num);
167
168 // bn_secret marks |bn|'s contents, but not its width or sign, as secret. See
169 // |CONSTTIME_SECRET| for details.
170 OPENSSL_INLINE void bn_secret(BIGNUM *bn) {
171 CONSTTIME_SECRET(bn->d, bn->width * sizeof(BN_ULONG));
172 }
173
174 // bn_declassify marks |bn|'s value as public. See |CONSTTIME_DECLASSIFY| for
175 // details.
176 OPENSSL_INLINE void bn_declassify(BIGNUM *bn) {
177 CONSTTIME_DECLASSIFY(bn->d, bn->width * sizeof(BN_ULONG));
178 }
179
180 // bn_mul_add_words multiples |ap| by |w|, adds the result to |rp|, and places
181 // the result in |rp|. |ap| and |rp| must both be |num| words long. It returns
182 // the carry word of the operation. |ap| and |rp| may be equal but otherwise may
183 // not alias.
184 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num,
185 BN_ULONG w);
186
187 // bn_mul_words multiples |ap| by |w| and places the result in |rp|. |ap| and
188 // |rp| must both be |num| words long. It returns the carry word of the
189 // operation. |ap| and |rp| may be equal but otherwise may not alias.
190 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w);
191
192 // bn_sqr_words sets |rp[2*i]| and |rp[2*i+1]| to |ap[i]|'s square, for all |i|
193 // up to |num|. |ap| is an array of |num| words and |rp| an array of |2*num|
194 // words. |ap| and |rp| may not alias.
195 //
196 // This gives the contribution of the |ap[i]*ap[i]| terms when squaring |ap|.
197 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num);
198
199 // bn_add_words adds |ap| to |bp| and places the result in |rp|, each of which
200 // are |num| words long. It returns the carry bit, which is one if the operation
201 // overflowed and zero otherwise. Any pair of |ap|, |bp|, and |rp| may be equal
202 // to each other but otherwise may not alias.
203 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
204 size_t num);
205
206 // bn_sub_words subtracts |bp| from |ap| and places the result in |rp|. It
207 // returns the borrow bit, which is one if the computation underflowed and zero
208 // otherwise. Any pair of |ap|, |bp|, and |rp| may be equal to each other but
209 // otherwise may not alias.
210 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
211 size_t num);
212
213 // bn_mul_comba4 sets |r| to the product of |a| and |b|.
214 void bn_mul_comba4(BN_ULONG r[8], const BN_ULONG a[4], const BN_ULONG b[4]);
215
216 // bn_mul_comba8 sets |r| to the product of |a| and |b|.
217 void bn_mul_comba8(BN_ULONG r[16], const BN_ULONG a[8], const BN_ULONG b[8]);
218
219 // bn_sqr_comba8 sets |r| to |a|^2.
220 void bn_sqr_comba8(BN_ULONG r[16], const BN_ULONG a[8]);
221
222 // bn_sqr_comba4 sets |r| to |a|^2.
223 void bn_sqr_comba4(BN_ULONG r[8], const BN_ULONG a[4]);
224
225 // bn_less_than_words returns one if |a| < |b| and zero otherwise, where |a|
226 // and |b| both are |len| words long. It runs in constant time.
227 int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len);
228
229 // bn_in_range_words returns one if |min_inclusive| <= |a| < |max_exclusive|,
230 // where |a| and |max_exclusive| both are |len| words long. |a| and
231 // |max_exclusive| are treated as secret.
232 int bn_in_range_words(const BN_ULONG *a, BN_ULONG min_inclusive,
233 const BN_ULONG *max_exclusive, size_t len);
234
235 // bn_rand_range_words sets |out| to a uniformly distributed random number from
236 // |min_inclusive| to |max_exclusive|. Both |out| and |max_exclusive| are |len|
237 // words long.
238 //
239 // This function runs in time independent of the result, but |min_inclusive| and
240 // |max_exclusive| are public data. (Information about the range is unavoidably
241 // leaked by how many iterations it took to select a number.)
242 int bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive,
243 const BN_ULONG *max_exclusive, size_t len,
244 const uint8_t additional_data[RAND_PRED_RESISTANCE_LEN]);
245
246 // bn_range_secret_range behaves like |BN_rand_range_ex|, but treats
247 // |max_exclusive| as secret. Because of this constraint, the distribution of
248 // values returned is more complex.
249 //
250 // Rather than repeatedly generating values until one is in range, which would
251 // leak information, it generates one value. If the value is in range, it sets
252 // |*out_is_uniform| to one. Otherwise, it sets |*out_is_uniform| to zero,
253 // fixing up the value to force it in range.
254 //
255 // The subset of calls to |bn_rand_secret_range| which set |*out_is_uniform| to
256 // one are uniformly distributed in the target range. Calls overall are not.
257 // This function is intended for use in situations where the extra values are
258 // still usable and where the number of iterations needed to reach the target
259 // number of uniform outputs may be blinded for negligible probabilities of
260 // timing leaks.
261 //
262 // Although this function treats |max_exclusive| as secret, it treats the number
263 // of bits in |max_exclusive| as public.
264 int bn_rand_secret_range(BIGNUM *r, int *out_is_uniform, BN_ULONG min_inclusive,
265 const BIGNUM *max_exclusive);
266
267 // BN_MONTGOMERY_MAX_WORDS is the maximum numer of words allowed in a |BIGNUM|
268 // used with Montgomery reduction. Ideally this limit would be applied to all
269 // |BIGNUM|s, in |bn_wexpand|, but the exactfloat library needs to create 8 MiB
270 // values for other operations.
271 #define BN_MONTGOMERY_MAX_WORDS (8 * 1024 / sizeof(BN_ULONG))
272
273 #if !defined(OPENSSL_NO_ASM) && \
274 (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
275 defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
276 #define OPENSSL_BN_ASM_MONT
277 // bn_mul_mont writes |ap| * |bp| mod |np| to |rp|, each |num| words
278 // long. Inputs and outputs are in Montgomery form. |n0| is a pointer to the
279 // corresponding field in |BN_MONT_CTX|. It returns one if |bn_mul_mont| handles
280 // inputs of this size and zero otherwise.
281 //
282 // If at least one of |ap| or |bp| is fully reduced, |rp| will be fully reduced.
283 // If neither is fully-reduced, the output may not be either.
284 //
285 // This function allocates |num| words on the stack, so |num| should be at most
286 // |BN_MONTGOMERY_MAX_WORDS|.
287 //
288 // TODO(davidben): The x86_64 implementation expects a 32-bit input and masks
289 // off upper bits. The aarch64 implementation expects a 64-bit input and does
290 // not. |size_t| is the safer option but not strictly correct for x86_64. But
291 // the |BN_MONTGOMERY_MAX_WORDS| bound makes this moot.
292 //
293 // See also discussion in |ToWord| in abi_test.h for notes on smaller-than-word
294 // inputs.
295 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
296 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
297
298 #if defined(OPENSSL_X86_64)
299 OPENSSL_INLINE int bn_mulx_adx_capable(void) {
300 // MULX is in BMI2.
301 return CRYPTO_is_BMI2_capable() && CRYPTO_is_ADX_capable();
302 }
303 int bn_mul_mont_nohw(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
304 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
305 OPENSSL_INLINE int bn_mul4x_mont_capable(size_t num) {
306 return (num >= 8) && ((num & 3) == 0);
307 }
308 int bn_mul4x_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
309 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
310 #if !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
311 OPENSSL_INLINE int bn_mulx4x_mont_capable(size_t num) {
312 return bn_mul4x_mont_capable(num) && bn_mulx_adx_capable();
313 }
314 int bn_mulx4x_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
315 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
316 OPENSSL_INLINE int bn_sqr8x_mont_capable(size_t num) {
317 return (num >= 8) && ((num & 7) == 0);
318 }
319 int bn_sqr8x_mont(BN_ULONG *rp, const BN_ULONG *ap, BN_ULONG mulx_adx_capable,
320 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
321 #endif // !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
322 #elif defined(OPENSSL_ARM)
323 OPENSSL_INLINE int bn_mul8x_mont_neon_capable(size_t num) {
324 return (num & 7) == 0 && CRYPTO_is_NEON_capable();
325 }
326 int bn_mul8x_mont_neon(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
327 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
328 int bn_mul_mont_nohw(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
329 const BN_ULONG *np, const BN_ULONG *n0, size_t num);
330 #endif // defined(OPENSSL_X86_64)
331
332 #endif
333
334 #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
335 #define OPENSSL_BN_ASM_MONT5
336
337 // bn_mul_mont_gather5 multiples loads index |power| of |table|, multiplies it
338 // by |ap| modulo |np|, and stores the result in |rp|. The values are |num|
339 // words long and represented in Montgomery form. |n0| is a pointer to the
340 // corresponding field in |BN_MONT_CTX|. |table| must be aligned to at least
341 // 16 bytes. |power| must be less than 32 and is treated as secret.
342 //
343 // WARNING: This function implements Almost Montgomery Multiplication from
344 // https://eprint.iacr.org/2011/239. The inputs do not need to be fully reduced.
345 // However, even if they are fully reduced, the output may not be.
346 void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,
347 const BN_ULONG *table, const BN_ULONG *np,
348 const BN_ULONG *n0, int num, int power);
349
350 // bn_scatter5 stores |inp| to index |power| of |table|. |inp| and each entry of
351 // |table| are |num| words long. |power| must be less than 32 and is treated as
352 // public. |table| must be 32*|num| words long. |table| must be aligned to at
353 // least 16 bytes.
354 void bn_scatter5(const BN_ULONG *inp, size_t num, BN_ULONG *table,
355 size_t power);
356
357 // bn_gather5 loads index |power| of |table| and stores it in |out|. |out| and
358 // each entry of |table| are |num| words long. |power| must be less than 32 and
359 // is treated as secret. |table| must be aligned to at least 16 bytes.
360 void bn_gather5(BN_ULONG *out, size_t num, const BN_ULONG *table, size_t power);
361
362 // bn_power5 squares |ap| five times and multiplies it by the value stored at
363 // index |power| of |table|, modulo |np|. It stores the result in |rp|. The
364 // values are |num| words long and represented in Montgomery form. |n0| is a
365 // pointer to the corresponding field in |BN_MONT_CTX|. |num| must be divisible
366 // by 8. |power| must be less than 32 and is treated as secret.
367 //
368 // WARNING: This function implements Almost Montgomery Multiplication from
369 // https://eprint.iacr.org/2011/239. The inputs do not need to be fully reduced.
370 // However, even if they are fully reduced, the output may not be.
371 void bn_power5(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *table,
372 const BN_ULONG *np, const BN_ULONG *n0, int num, int power);
373 #endif // !OPENSSL_NO_ASM && OPENSSL_X86_64
374
375 uint64_t bn_mont_n0(const BIGNUM *n);
376
377 // bn_mont_ctx_set_RR_consttime initializes |mont->RR|. It returns one on
378 // success and zero on error. |mont->N| and |mont->n0| must have been
379 // initialized already. The bit width of |mont->N| is assumed public, but
380 // |mont->N| is otherwise treated as secret.
381 int bn_mont_ctx_set_RR_consttime(BN_MONT_CTX *mont, BN_CTX *ctx);
382
383 #if defined(_MSC_VER)
384 #if defined(OPENSSL_X86_64)
385 #define BN_UMULT_LOHI(low, high, a, b) ((low) = _umul128((a), (b), &(high)))
386 #elif defined(OPENSSL_AARCH64)
387 #define BN_UMULT_LOHI(low, high, a, b) \
388 do { \
389 const BN_ULONG _a = (a); \
390 const BN_ULONG _b = (b); \
391 (low) = _a * _b; \
392 (high) = __umulh(_a, _b); \
393 } while (0)
394 #endif
395 #endif // _MSC_VER
396
397 #if !defined(BN_ULLONG) && !defined(BN_UMULT_LOHI)
398 #error "Either BN_ULLONG or BN_UMULT_LOHI must be defined on every platform."
399 #endif
400
401 // bn_jacobi returns the Jacobi symbol of |a| and |b| (which is -1, 0 or 1), or
402 // -2 on error.
403 int bn_jacobi(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
404
405 // bn_is_bit_set_words returns one if bit |bit| is set in |a| and zero
406 // otherwise.
407 int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit);
408
409 // bn_one_to_montgomery sets |r| to one in Montgomery form. It returns one on
410 // success and zero on error. This function treats the bit width of the modulus
411 // as public.
412 int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx);
413
414 // bn_less_than_montgomery_R returns one if |bn| is less than the Montgomery R
415 // value for |mont| and zero otherwise.
416 int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont);
417
418 // bn_mod_u16_consttime returns |bn| mod |d|, ignoring |bn|'s sign bit. It runs
419 // in time independent of the value of |bn|, but it treats |d| as public.
420 OPENSSL_EXPORT uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d);
421
422 // bn_odd_number_is_obviously_composite returns one if |bn| is divisible by one
423 // of the first several odd primes and zero otherwise.
424 int bn_odd_number_is_obviously_composite(const BIGNUM *bn);
425
426 // A BN_MILLER_RABIN stores state common to each Miller-Rabin iteration. It is
427 // initialized within an existing |BN_CTX| scope and may not be used after
428 // that scope is released with |BN_CTX_end|. Field names match those in FIPS
429 // 186-4, section C.3.1.
430 typedef struct {
431 // w1 is w-1.
432 BIGNUM *w1;
433 // m is (w-1)/2^a.
434 BIGNUM *m;
435 // one_mont is 1 (mod w) in Montgomery form.
436 BIGNUM *one_mont;
437 // w1_mont is w-1 (mod w) in Montgomery form.
438 BIGNUM *w1_mont;
439 // w_bits is BN_num_bits(w).
440 int w_bits;
441 // a is the largest integer such that 2^a divides w-1.
442 int a;
443 } BN_MILLER_RABIN;
444
445 // bn_miller_rabin_init initializes |miller_rabin| for testing if |mont->N| is
446 // prime. It returns one on success and zero on error.
447 OPENSSL_EXPORT int bn_miller_rabin_init(BN_MILLER_RABIN *miller_rabin,
448 const BN_MONT_CTX *mont, BN_CTX *ctx);
449
450 // bn_miller_rabin_iteration performs one Miller-Rabin iteration, checking if
451 // |b| is a composite witness for |mont->N|. |miller_rabin| must have been
452 // initialized with |bn_miller_rabin_setup|. On success, it returns one and sets
453 // |*out_is_possibly_prime| to one if |mont->N| may still be prime or zero if
454 // |b| shows it is composite. On allocation or internal failure, it returns
455 // zero.
456 OPENSSL_EXPORT int bn_miller_rabin_iteration(
457 const BN_MILLER_RABIN *miller_rabin, int *out_is_possibly_prime,
458 const BIGNUM *b, const BN_MONT_CTX *mont, BN_CTX *ctx);
459
460 // bn_rshift1_words sets |r| to |a| >> 1, where both arrays are |num| bits wide.
461 void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num);
462
463 // bn_rshift_words sets |r| to |a| >> |shift|, where both arrays are |num| bits
464 // wide.
465 void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift,
466 size_t num);
467
468 // bn_rshift_secret_shift behaves like |BN_rshift| but runs in time independent
469 // of both |a| and |n|.
470 OPENSSL_EXPORT int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a,
471 unsigned n, BN_CTX *ctx);
472
473 // bn_reduce_once sets |r| to |a| mod |m| where 0 <= |a| < 2*|m|. It returns
474 // zero if |a| < |m| and a mask of all ones if |a| >= |m|. Each array is |num|
475 // words long, but |a| has an additional word specified by |carry|. |carry| must
476 // be zero or one, as implied by the bounds on |a|.
477 //
478 // |r|, |a|, and |m| may not alias. Use |bn_reduce_once_in_place| if |r| and |a|
479 // must alias.
480 BN_ULONG bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry,
481 const BN_ULONG *m, size_t num);
482
483 // bn_reduce_once_in_place behaves like |bn_reduce_once| but acts in-place on
484 // |r|, using |tmp| as scratch space. |r|, |tmp|, and |m| may not alias.
485 BN_ULONG bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m,
486 BN_ULONG *tmp, size_t num);
487
488
489 // Constant-time non-modular arithmetic.
490 //
491 // The following functions implement non-modular arithmetic in constant-time
492 // and pessimally set |r->width| to the largest possible word size.
493 //
494 // Note this means that, e.g., repeatedly multiplying by one will cause widths
495 // to increase without bound. The corresponding public API functions minimize
496 // their outputs to avoid regressing calculator consumers.
497
498 // bn_uadd_consttime behaves like |BN_uadd|, but it pessimally sets
499 // |r->width| = |a->width| + |b->width| + 1.
500 int bn_uadd_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
501
502 // bn_usub_consttime behaves like |BN_usub|, but it pessimally sets
503 // |r->width| = |a->width|.
504 int bn_usub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
505
506 // bn_abs_sub_consttime sets |r| to the absolute value of |a| - |b|, treating
507 // both inputs as secret. It returns one on success and zero on error.
508 OPENSSL_EXPORT int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a,
509 const BIGNUM *b, BN_CTX *ctx);
510
511 // bn_mul_consttime behaves like |BN_mul|, but it rejects negative inputs and
512 // pessimally sets |r->width| to |a->width| + |b->width|, to avoid leaking
513 // information about |a| and |b|.
514 int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
515
516 // bn_sqrt_consttime behaves like |BN_sqrt|, but it pessimally sets |r->width|
517 // to 2*|a->width|, to avoid leaking information about |a| and |b|.
518 int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
519
520 // bn_div_consttime behaves like |BN_div|, but it rejects negative inputs and
521 // treats both inputs, including their magnitudes, as secret. It is, as a
522 // result, much slower than |BN_div| and should only be used for rare operations
523 // where Montgomery reduction is not available. |divisor_min_bits| is a
524 // public lower bound for |BN_num_bits(divisor)|. When |divisor|'s bit width is
525 // public, this can speed up the operation.
526 //
527 // Note that |quotient->width| will be set pessimally to |numerator->width|.
528 OPENSSL_EXPORT int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
529 const BIGNUM *numerator,
530 const BIGNUM *divisor,
531 unsigned divisor_min_bits, BN_CTX *ctx);
532
533 // bn_is_relatively_prime checks whether GCD(|x|, |y|) is one. On success, it
534 // returns one and sets |*out_relatively_prime| to one if the GCD was one and
535 // zero otherwise. On error, it returns zero.
536 OPENSSL_EXPORT int bn_is_relatively_prime(int *out_relatively_prime,
537 const BIGNUM *x, const BIGNUM *y,
538 BN_CTX *ctx);
539
540 // bn_lcm_consttime sets |r| to LCM(|a|, |b|). It returns one and success and
541 // zero on error. |a| and |b| are both treated as secret.
542 OPENSSL_EXPORT int bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
543 BN_CTX *ctx);
544
545 // bn_mont_ctx_init zero-initialies |mont|.
546 void bn_mont_ctx_init(BN_MONT_CTX *mont);
547
548 // bn_mont_ctx_cleanup releases memory associated with |mont|, without freeing
549 // |mont| itself.
550 void bn_mont_ctx_cleanup(BN_MONT_CTX *mont);
551
552
553 // Constant-time modular arithmetic.
554 //
555 // The following functions implement basic constant-time modular arithmetic.
556
557 // bn_mod_add_words sets |r| to |a| + |b| (mod |m|), using |tmp| as scratch
558 // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
559 // |r|, |a|, and |b| may alias.
560 void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
561 const BN_ULONG *m, BN_ULONG *tmp, size_t num);
562
563 // bn_mod_add_consttime acts like |BN_mod_add_quick| but takes a |BN_CTX|.
564 int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
565 const BIGNUM *m, BN_CTX *ctx);
566
567 // bn_mod_sub_words sets |r| to |a| - |b| (mod |m|), using |tmp| as scratch
568 // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
569 // |r|, |a|, and |b| may alias.
570 void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
571 const BN_ULONG *m, BN_ULONG *tmp, size_t num);
572
573 // bn_mod_sub_consttime acts like |BN_mod_sub_quick| but takes a |BN_CTX|.
574 int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
575 const BIGNUM *m, BN_CTX *ctx);
576
577 // bn_mod_lshift1_consttime acts like |BN_mod_lshift1_quick| but takes a
578 // |BN_CTX|.
579 int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
580 BN_CTX *ctx);
581
582 // bn_mod_lshift_consttime acts like |BN_mod_lshift_quick| but takes a |BN_CTX|.
583 int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
584 BN_CTX *ctx);
585
586 // bn_mod_inverse_consttime sets |r| to |a|^-1, mod |n|. |a| must be non-
587 // negative and less than |n|. It returns one on success and zero on error. On
588 // failure, if the failure was caused by |a| having no inverse mod |n| then
589 // |*out_no_inverse| will be set to one; otherwise it will be set to zero.
590 //
591 // This function treats both |a| and |n| as secret, provided they are both non-
592 // zero and the inverse exists. It should only be used for even moduli where
593 // none of the less general implementations are applicable.
594 OPENSSL_EXPORT int bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse,
595 const BIGNUM *a, const BIGNUM *n,
596 BN_CTX *ctx);
597
598 // bn_mod_inverse_prime sets |out| to the modular inverse of |a| modulo |p|,
599 // computed with Fermat's Little Theorem. It returns one on success and zero on
600 // error. If |mont_p| is NULL, one will be computed temporarily.
601 int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
602 BN_CTX *ctx, const BN_MONT_CTX *mont_p);
603
604 // bn_mod_inverse_secret_prime behaves like |bn_mod_inverse_prime| but uses
605 // |BN_mod_exp_mont_consttime| instead of |BN_mod_exp_mont| in hopes of
606 // protecting the exponent.
607 int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
608 BN_CTX *ctx, const BN_MONT_CTX *mont_p);
609
610 // BN_MONT_CTX_set_locked takes |lock| and checks whether |*pmont| is NULL. If
611 // so, it creates a new |BN_MONT_CTX| and sets the modulus for it to |mod|. It
612 // then stores it as |*pmont|. It returns one on success and zero on error. Note
613 // this function assumes |mod| is public.
614 //
615 // If |*pmont| is already non-NULL then it does nothing and returns one.
616 int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
617 const BIGNUM *mod, BN_CTX *bn_ctx);
618
619
620 // Low-level operations for small numbers.
621 //
622 // The following functions implement algorithms suitable for use with scalars
623 // and field elements in elliptic curves. They rely on the number being small
624 // both to stack-allocate various temporaries and because they do not implement
625 // optimizations useful for the larger values used in RSA.
626
627 // BN_SMALL_MAX_WORDS is the largest size input these functions handle. This
628 // limit allows temporaries to be more easily stack-allocated. This limit is set
629 // to accommodate P-521.
630 #if defined(OPENSSL_32_BIT)
631 #define BN_SMALL_MAX_WORDS 17
632 #else
633 #define BN_SMALL_MAX_WORDS 9
634 #endif
635
636 // bn_mul_small sets |r| to |a|*|b|. |num_r| must be |num_a| + |num_b|. |r| may
637 // not alias with |a| or |b|.
638 void bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
639 const BN_ULONG *b, size_t num_b);
640
641 // bn_sqr_small sets |r| to |a|^2. |num_a| must be at most |BN_SMALL_MAX_WORDS|.
642 // |num_r| must be |num_a|*2. |r| and |a| may not alias.
643 void bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a);
644
645 // In the following functions, the modulus must be at most |BN_SMALL_MAX_WORDS|
646 // words long.
647
648 // bn_to_montgomery_small sets |r| to |a| translated to the Montgomery domain.
649 // |r| and |a| are |num| words long, which must be |mont->N.width|. |a| must be
650 // fully reduced and may alias |r|.
651 void bn_to_montgomery_small(BN_ULONG *r, const BN_ULONG *a, size_t num,
652 const BN_MONT_CTX *mont);
653
654 // bn_from_montgomery_small sets |r| to |a| translated out of the Montgomery
655 // domain. |r| and |a| are |num_r| and |num_a| words long, respectively. |num_r|
656 // must be |mont->N.width|. |a| must be at most |mont->N|^2 and may alias |r|.
657 //
658 // Unlike most of these functions, only |num_r| is bounded by
659 // |BN_SMALL_MAX_WORDS|. |num_a| may exceed it, but must be at most 2 * |num_r|.
660 void bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
661 size_t num_a, const BN_MONT_CTX *mont);
662
663 // bn_mod_mul_montgomery_small sets |r| to |a| * |b| mod |mont->N|. Both inputs
664 // and outputs are in the Montgomery domain. Each array is |num| words long,
665 // which must be |mont->N.width|. Any two of |r|, |a|, and |b| may alias. |a|
666 // and |b| must be reduced on input.
667 void bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a,
668 const BN_ULONG *b, size_t num,
669 const BN_MONT_CTX *mont);
670
671 // bn_mod_exp_mont_small sets |r| to |a|^|p| mod |mont->N|. It returns one on
672 // success and zero on programmer or internal error. Both inputs and outputs are
673 // in the Montgomery domain. |r| and |a| are |num| words long, which must be
674 // |mont->N.width| and at most |BN_SMALL_MAX_WORDS|. |num_p|, measured in bits,
675 // must fit in |size_t|. |a| must be fully-reduced. This function runs in time
676 // independent of |a|, but |p| and |mont->N| are public values. |a| must be
677 // fully-reduced and may alias with |r|.
678 //
679 // Note this function differs from |BN_mod_exp_mont| which uses Montgomery
680 // reduction but takes input and output outside the Montgomery domain. Combine
681 // this function with |bn_from_montgomery_small| and |bn_to_montgomery_small|
682 // if necessary.
683 void bn_mod_exp_mont_small(BN_ULONG *r, const BN_ULONG *a, size_t num,
684 const BN_ULONG *p, size_t num_p,
685 const BN_MONT_CTX *mont);
686
687 // bn_mod_inverse0_prime_mont_small sets |r| to |a|^-1 mod |mont->N|. If |a| is
688 // zero, |r| is set to zero. |mont->N| must be a prime. |r| and |a| are |num|
689 // words long, which must be |mont->N.width| and at most |BN_SMALL_MAX_WORDS|.
690 // |a| must be fully-reduced and may alias |r|. This function runs in time
691 // independent of |a|, but |mont->N| is a public value.
692 void bn_mod_inverse0_prime_mont_small(BN_ULONG *r, const BN_ULONG *a,
693 size_t num, const BN_MONT_CTX *mont);
694
695
696 // Word-based byte conversion functions.
697
698 // bn_big_endian_to_words interprets |in_len| bytes from |in| as a big-endian,
699 // unsigned integer and writes the result to |out_len| words in |out|. The output
700 // is in little-endian word order with |out[0]| being the least-significant word.
701 // |out_len| must be large enough to represent any |in_len|-byte value. That is,
702 // |in_len| must be at most |BN_BYTES * out_len|.
703 void bn_big_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in,
704 size_t in_len);
705
706 // bn_words_to_big_endian represents |in_len| words from |in| (in little-endian
707 // word order) as a big-endian, unsigned integer in |out_len| bytes. It writes
708 // the result to |out|. |out_len| must be large enough to represent |in| without
709 // truncation.
710 //
711 // Note |out_len| may be less than |BN_BYTES * in_len| if |in| is known to have
712 // leading zeros.
713 void bn_words_to_big_endian(uint8_t *out, size_t out_len, const BN_ULONG *in,
714 size_t in_len);
715
716 // bn_little_endian_to_words interprets |in_len| bytes from |in| as a little-endian,
717 // unsigned integer and writes the result to |out_len| words in |out|. The output
718 // is in little-endian word order with |out[0]| being the least-significant word.
719 // |out_len| must be large enough to represent any |in_len|-byte value. That is,
720 // |out_len| must be at least |BN_BYTES * in_len|.
721 void bn_little_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in, const size_t in_len);
722
723 // bn_words_to_little_endian represents |in_len| words from |in| (in little-endian
724 // word order) as a little-endian, unsigned integer in |out_len| bytes. It
725 // writes the result to |out|. |out_len| must be large enough to represent |in|
726 // without truncation.
727 //
728 // Note |out_len| may be less than |BN_BYTES * in_len| if |in| is known to have
729 // leading zeros.
730 void bn_words_to_little_endian(uint8_t *out, size_t out_len, const BN_ULONG *in, const size_t in_len);
731
732 #if defined(__cplusplus)
733 } // extern C
734 #endif
735
736 #endif // OPENSSL_HEADER_BN_INTERNAL_H