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