1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57 /* ====================================================================
58 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109 #ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H
110 #define OPENSSL_HEADER_CRYPTO_INTERNAL_H
111
112 #include <openssl/arm_arch.h>
113 #include <openssl/crypto.h>
114 #include <openssl/ex_data.h>
115 #include <openssl/stack.h>
116 #include <openssl/thread.h>
117
118 #include <assert.h>
119 #include <string.h>
120
121 #if defined(BORINGSSL_CONSTANT_TIME_VALIDATION)
122 #include <valgrind/memcheck.h>
123 #endif
124
125 #if defined(BORINGSSL_FIPS_BREAK_TESTS)
126 #include <stdlib.h>
127 #endif
128
129 #if defined(OPENSSL_THREADS) && \
130 (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__))
131 #include <pthread.h>
132 #define OPENSSL_PTHREADS
133 #endif
134
135 #if defined(OPENSSL_THREADS) && !defined(OPENSSL_PTHREADS) && \
136 defined(OPENSSL_WINDOWS)
137 #define OPENSSL_WINDOWS_THREADS
138 #endif
139
140 #if defined(OPENSSL_THREADS)
141 #include <atomic>
142 #endif
143
144 #if defined(OPENSSL_WINDOWS_THREADS)
145 OPENSSL_MSVC_PRAGMA(warning(push, 3))
146 #include <windows.h>
OPENSSL_MSVC_PRAGMA(warning (pop))147 OPENSSL_MSVC_PRAGMA(warning(pop))
148 #endif
149
150 #if defined(__cplusplus)
151 extern "C" {
152 #endif
153
154
155 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
156 (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
157 defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
158 // x86, x86_64, and the ARMs need to record the result of a cpuid/getauxval call
159 // for the asm to work correctly, unless compiled without asm code.
160 #define NEED_CPUID
161
162 // OPENSSL_cpuid_setup initializes the platform-specific feature cache. This
163 // function should not be called directly. Call |OPENSSL_init_cpuid| instead.
164 void OPENSSL_cpuid_setup(void);
165
166 // OPENSSL_init_cpuid initializes the platform-specific feature cache, if
167 // needed. This function is idempotent and may be called concurrently.
168 void OPENSSL_init_cpuid(void);
169 #else
170 OPENSSL_INLINE void OPENSSL_init_cpuid(void) {}
171 #endif
172
173 #if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \
174 !defined(OPENSSL_STATIC_ARMCAP)
175 // OPENSSL_get_armcap_pointer_for_test returns a pointer to |OPENSSL_armcap_P|
176 // for unit tests. Any modifications to the value must be made before any other
177 // function call in BoringSSL.
178 OPENSSL_EXPORT uint32_t *OPENSSL_get_armcap_pointer_for_test(void);
179 #endif
180
181
182 // On non-MSVC 64-bit targets, we expect __uint128_t support. This includes
183 // clang-cl, which defines both __clang__ and _MSC_VER.
184 #if (!defined(_MSC_VER) || defined(__clang__)) && defined(OPENSSL_64_BIT)
185 #define BORINGSSL_HAS_UINT128
186 typedef __int128_t int128_t;
187 typedef __uint128_t uint128_t;
188
189 // __uint128_t division depends on intrinsics in the compiler runtime. Those
190 // intrinsics are missing in clang-cl (https://crbug.com/787617) and nanolibc.
191 // These may be bugs in the toolchain definition, but just disable it for now.
192 // EDK2's toolchain is missing __udivti3 (b/339380897) so cannot support
193 // 128-bit division currently.
194 #if !defined(_MSC_VER) && !defined(OPENSSL_NANOLIBC) && \
195 !defined(__EDK2_BORINGSSL__)
196 #define BORINGSSL_CAN_DIVIDE_UINT128
197 #endif
198 #endif
199
200 #define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
201
202 #if defined(__clang__) && __clang_major__ >= 5
203 #if __has_attribute(fallthrough)
204 #define OPENSSL_CAN_USE_ATTR_FALLTHROUGH
205 #endif
206 #endif
207
208 // Have a generic fall-through for different versions of C/C++.
209 #if defined(__cplusplus) && __cplusplus >= 201703L
210 #define OPENSSL_FALLTHROUGH [[fallthrough]]
211 #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__clang__)
212 #define OPENSSL_FALLTHROUGH [[clang::fallthrough]]
213 #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__GNUC__)
214 #define OPENSSL_FALLTHROUGH [[gnu::fallthrough]]
215 #elif defined(__GNUC__)
216 #define OPENSSL_FALLTHROUGH __attribute__((fallthrough))
217 #elif defined(OPENSSL_CAN_USE_ATTR_FALLTHROUGH)
218 // Clang 3.5, at least, complains about "error: declaration does not declare
219 // anything", possibily because we put a semicolon after this macro in
220 // practice. Thus limit it to >= Clang 5, which does work.
221 #define OPENSSL_FALLTHROUGH __attribute__((fallthrough))
222 #else // all other cases
223 #define OPENSSL_FALLTHROUGH
224 #endif
225
226 // GCC-like compilers indicate SSE2 with |__SSE2__|. MSVC leaves the caller to
227 // know that x86_64 has SSE2, and uses _M_IX86_FP to indicate SSE2 on x86.
228 // https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170
229 #if defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || \
230 (defined(_M_IX86_FP) && _M_IX86_FP >= 2)
231 #define OPENSSL_SSE2
232 #endif
233
234 #if defined(OPENSSL_X86) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_SSE2)
235 #error \
236 "x86 assembly requires SSE2. Build with -msse2 (recommended), or disable assembly optimizations with -DOPENSSL_NO_ASM."
237 #endif
238
239 // For convenience in testing the fallback code, we allow disabling SSE2
240 // intrinsics via |OPENSSL_NO_SSE2_FOR_TESTING|. We require SSE2 on x86 and
241 // x86_64, so we would otherwise need to test such code on a non-x86 platform.
242 //
243 // This does not remove the above requirement for SSE2 support with assembly
244 // optimizations. It only disables some intrinsics-based optimizations so that
245 // we can test the fallback code on CI.
246 #if defined(OPENSSL_SSE2) && defined(OPENSSL_NO_SSE2_FOR_TESTING)
247 #undef OPENSSL_SSE2
248 #endif
249
250 #if defined(__GNUC__) || defined(__clang__)
251 #define OPENSSL_ATTR_CONST __attribute__((const))
252 #else
253 #define OPENSSL_ATTR_CONST
254 #endif
255
256 #if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
257 // OPENSSL_reset_malloc_counter_for_testing, when malloc testing is enabled,
258 // resets the internal malloc counter, to simulate further malloc failures. This
259 // should be called in between independent tests, at a point where failure from
260 // a previous test will not impact subsequent ones.
261 OPENSSL_EXPORT void OPENSSL_reset_malloc_counter_for_testing(void);
262
263 // OPENSSL_disable_malloc_failures_for_testing, when malloc testing is enabled,
264 // disables simulated malloc failures. Calls to |OPENSSL_malloc| will not
265 // increment the malloc counter or synthesize failures. This may be used to skip
266 // simulating malloc failures in some region of code.
267 OPENSSL_EXPORT void OPENSSL_disable_malloc_failures_for_testing(void);
268
269 // OPENSSL_enable_malloc_failures_for_testing, when malloc testing is enabled,
270 // re-enables simulated malloc failures.
271 OPENSSL_EXPORT void OPENSSL_enable_malloc_failures_for_testing(void);
272 #else
273 OPENSSL_INLINE void OPENSSL_reset_malloc_counter_for_testing(void) {}
274 OPENSSL_INLINE void OPENSSL_disable_malloc_failures_for_testing(void) {}
275 OPENSSL_INLINE void OPENSSL_enable_malloc_failures_for_testing(void) {}
276 #endif
277
278 #if defined(__has_builtin)
279 #define OPENSSL_HAS_BUILTIN(x) __has_builtin(x)
280 #else
281 #define OPENSSL_HAS_BUILTIN(x) 0
282 #endif
283
284
285 // Pointer utility functions.
286
287 // buffers_alias returns one if |a| and |b| alias and zero otherwise.
288 static inline int buffers_alias(const void *a, size_t a_bytes, const void *b,
289 size_t b_bytes) {
290 // Cast |a| and |b| to integers. In C, pointer comparisons between unrelated
291 // objects are undefined whereas pointer to integer conversions are merely
292 // implementation-defined. We assume the implementation defined it in a sane
293 // way.
294 uintptr_t a_u = (uintptr_t)a;
295 uintptr_t b_u = (uintptr_t)b;
296 return a_u + a_bytes > b_u && b_u + b_bytes > a_u;
297 }
298
299 // align_pointer returns |ptr|, advanced to |alignment|. |alignment| must be a
300 // power of two, and |ptr| must have at least |alignment - 1| bytes of scratch
301 // space.
302 static inline void *align_pointer(void *ptr, size_t alignment) {
303 // |alignment| must be a power of two.
304 assert(alignment != 0 && (alignment & (alignment - 1)) == 0);
305 // Instead of aligning |ptr| as a |uintptr_t| and casting back, compute the
306 // offset and advance in pointer space. C guarantees that casting from pointer
307 // to |uintptr_t| and back gives the same pointer, but general
308 // integer-to-pointer conversions are implementation-defined. GCC does define
309 // it in the useful way, but this makes fewer assumptions.
310 uintptr_t offset = (0u - (uintptr_t)ptr) & (alignment - 1);
311 ptr = (char *)ptr + offset;
312 assert(((uintptr_t)ptr & (alignment - 1)) == 0);
313 return ptr;
314 }
315
316
317 // Constant-time utility functions.
318 //
319 // The following methods return a bitmask of all ones (0xff...f) for true and 0
320 // for false. This is useful for choosing a value based on the result of a
321 // conditional in constant time. For example,
322 //
323 // if (a < b) {
324 // c = a;
325 // } else {
326 // c = b;
327 // }
328 //
329 // can be written as
330 //
331 // crypto_word_t lt = constant_time_lt_w(a, b);
332 // c = constant_time_select_w(lt, a, b);
333
334 // crypto_word_t is the type that most constant-time functions use. Ideally we
335 // would like it to be |size_t|, but NaCl builds in 64-bit mode with 32-bit
336 // pointers, which means that |size_t| can be 32 bits when |BN_ULONG| is 64
337 // bits. Since we want to be able to do constant-time operations on a
338 // |BN_ULONG|, |crypto_word_t| is defined as an unsigned value with the native
339 // word length.
340 #if defined(OPENSSL_64_BIT)
341 typedef uint64_t crypto_word_t;
342 #elif defined(OPENSSL_32_BIT)
343 typedef uint32_t crypto_word_t;
344 #else
345 #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
346 #endif
347
348 #define CONSTTIME_TRUE_W ~((crypto_word_t)0)
349 #define CONSTTIME_FALSE_W ((crypto_word_t)0)
350 #define CONSTTIME_TRUE_8 ((uint8_t)0xff)
351 #define CONSTTIME_FALSE_8 ((uint8_t)0)
352
353 // value_barrier_w returns |a|, but prevents GCC and Clang from reasoning about
354 // the returned value. This is used to mitigate compilers undoing constant-time
355 // code, until we can express our requirements directly in the language.
356 //
357 // Note the compiler is aware that |value_barrier_w| has no side effects and
358 // always has the same output for a given input. This allows it to eliminate
359 // dead code, move computations across loops, and vectorize.
360 static inline crypto_word_t value_barrier_w(crypto_word_t a) {
361 #if defined(__GNUC__) || defined(__clang__)
362 __asm__("" : "+r"(a) : /* no inputs */);
363 #endif
364 return a;
365 }
366
367 // value_barrier_u32 behaves like |value_barrier_w| but takes a |uint32_t|.
368 static inline uint32_t value_barrier_u32(uint32_t a) {
369 #if defined(__GNUC__) || defined(__clang__)
370 __asm__("" : "+r"(a) : /* no inputs */);
371 #endif
372 return a;
373 }
374
375 // value_barrier_u64 behaves like |value_barrier_w| but takes a |uint64_t|.
376 static inline uint64_t value_barrier_u64(uint64_t a) {
377 #if defined(__GNUC__) || defined(__clang__)
378 __asm__("" : "+r"(a) : /* no inputs */);
379 #endif
380 return a;
381 }
382
383 // |value_barrier_u8| could be defined as above, but compilers other than
384 // clang seem to still materialize 0x00..00MM instead of reusing 0x??..??MM.
385
386 // constant_time_msb_w returns the given value with the MSB copied to all the
387 // other bits.
388 static inline crypto_word_t constant_time_msb_w(crypto_word_t a) {
389 return 0u - (a >> (sizeof(a) * 8 - 1));
390 }
391
392 // constant_time_lt_w returns 0xff..f if a < b and 0 otherwise.
393 static inline crypto_word_t constant_time_lt_w(crypto_word_t a,
394 crypto_word_t b) {
395 // Consider the two cases of the problem:
396 // msb(a) == msb(b): a < b iff the MSB of a - b is set.
397 // msb(a) != msb(b): a < b iff the MSB of b is set.
398 //
399 // If msb(a) == msb(b) then the following evaluates as:
400 // msb(a^((a^b)|((a-b)^a))) ==
401 // msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
402 // msb(a^a^(a-b)) == (rearranging)
403 // msb(a-b) (because ∀x. x^x == 0)
404 //
405 // Else, if msb(a) != msb(b) then the following evaluates as:
406 // msb(a^((a^b)|((a-b)^a))) ==
407 // msb(a^( | ((a-b)^a))) == (because msb(a^b) == 1 and
408 // represents a value s.t. msb() = 1)
409 // msb(a^) == (because ORing with 1 results in 1)
410 // msb(b)
411 //
412 //
413 // Here is an SMT-LIB verification of this formula:
414 //
415 // (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
416 // (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
417 // )
418 //
419 // (declare-fun a () (_ BitVec 32))
420 // (declare-fun b () (_ BitVec 32))
421 //
422 // (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
423 // (check-sat)
424 // (get-model)
425 return constant_time_msb_w(a ^ ((a ^ b) | ((a - b) ^ a)));
426 }
427
428 // constant_time_lt_8 acts like |constant_time_lt_w| but returns an 8-bit
429 // mask.
430 static inline uint8_t constant_time_lt_8(crypto_word_t a, crypto_word_t b) {
431 return (uint8_t)(constant_time_lt_w(a, b));
432 }
433
434 // constant_time_ge_w returns 0xff..f if a >= b and 0 otherwise.
435 static inline crypto_word_t constant_time_ge_w(crypto_word_t a,
436 crypto_word_t b) {
437 return ~constant_time_lt_w(a, b);
438 }
439
440 // constant_time_ge_8 acts like |constant_time_ge_w| but returns an 8-bit
441 // mask.
442 static inline uint8_t constant_time_ge_8(crypto_word_t a, crypto_word_t b) {
443 return (uint8_t)(constant_time_ge_w(a, b));
444 }
445
446 // constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise.
447 static inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) {
448 // Here is an SMT-LIB verification of this formula:
449 //
450 // (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
451 // (bvand (bvnot a) (bvsub a #x00000001))
452 // )
453 //
454 // (declare-fun a () (_ BitVec 32))
455 //
456 // (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a
457 // #x00000000)))) (check-sat) (get-model)
458 return constant_time_msb_w(~a & (a - 1));
459 }
460
461 // constant_time_is_zero_8 acts like |constant_time_is_zero_w| but returns an
462 // 8-bit mask.
463 static inline uint8_t constant_time_is_zero_8(crypto_word_t a) {
464 return (uint8_t)(constant_time_is_zero_w(a));
465 }
466
467 // constant_time_eq_w returns 0xff..f if a == b and 0 otherwise.
468 static inline crypto_word_t constant_time_eq_w(crypto_word_t a,
469 crypto_word_t b) {
470 return constant_time_is_zero_w(a ^ b);
471 }
472
473 // constant_time_eq_8 acts like |constant_time_eq_w| but returns an 8-bit
474 // mask.
475 static inline uint8_t constant_time_eq_8(crypto_word_t a, crypto_word_t b) {
476 return (uint8_t)(constant_time_eq_w(a, b));
477 }
478
479 // constant_time_eq_int acts like |constant_time_eq_w| but works on int
480 // values.
481 static inline crypto_word_t constant_time_eq_int(int a, int b) {
482 return constant_time_eq_w((crypto_word_t)(a), (crypto_word_t)(b));
483 }
484
485 // constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
486 // mask.
487 static inline uint8_t constant_time_eq_int_8(int a, int b) {
488 return constant_time_eq_8((crypto_word_t)(a), (crypto_word_t)(b));
489 }
490
491 // constant_time_select_w returns (mask & a) | (~mask & b). When |mask| is all
492 // 1s or all 0s (as returned by the methods above), the select methods return
493 // either |a| (if |mask| is nonzero) or |b| (if |mask| is zero).
494 static inline crypto_word_t constant_time_select_w(crypto_word_t mask,
495 crypto_word_t a,
496 crypto_word_t b) {
497 // Clang recognizes this pattern as a select. While it usually transforms it
498 // to a cmov, it sometimes further transforms it into a branch, which we do
499 // not want.
500 //
501 // Hiding the value of the mask from the compiler evades this transformation.
502 mask = value_barrier_w(mask);
503 return (mask & a) | (~mask & b);
504 }
505
506 // constant_time_select_8 acts like |constant_time_select| but operates on
507 // 8-bit values.
508 static inline uint8_t constant_time_select_8(crypto_word_t mask, uint8_t a,
509 uint8_t b) {
510 // |mask| is a word instead of |uint8_t| to avoid materializing 0x000..0MM
511 // Making both |mask| and its value barrier |uint8_t| would allow the compiler
512 // to materialize 0x????..?MM instead, but only clang is that clever.
513 // However, vectorization of bitwise operations seems to work better on
514 // |uint8_t| than a mix of |uint64_t| and |uint8_t|, so |m| is cast to
515 // |uint8_t| after the value barrier but before the bitwise operations.
516 uint8_t m = value_barrier_w(mask);
517 return (m & a) | (~m & b);
518 }
519
520 // constant_time_select_int acts like |constant_time_select| but operates on
521 // ints.
522 static inline int constant_time_select_int(crypto_word_t mask, int a, int b) {
523 return (int)(constant_time_select_w(mask, (crypto_word_t)(a),
524 (crypto_word_t)(b)));
525 }
526
527 // constant_time_conditional_memcpy copies |n| bytes from |src| to |dst| if
528 // |mask| is 0xff..ff and does nothing if |mask| is 0. The |n|-byte memory
529 // ranges at |dst| and |src| must not overlap, as when calling |memcpy|.
530 static inline void constant_time_conditional_memcpy(void *dst, const void *src,
531 const size_t n,
532 const crypto_word_t mask) {
533 assert(!buffers_alias(dst, n, src, n));
534 uint8_t *out = (uint8_t *)dst;
535 const uint8_t *in = (const uint8_t *)src;
536 for (size_t i = 0; i < n; i++) {
537 out[i] = constant_time_select_8(mask, in[i], out[i]);
538 }
539 }
540
541 // constant_time_conditional_memxor xors |n| bytes from |src| to |dst| if
542 // |mask| is 0xff..ff and does nothing if |mask| is 0. The |n|-byte memory
543 // ranges at |dst| and |src| must not overlap, as when calling |memcpy|.
544 static inline void constant_time_conditional_memxor(void *dst, const void *src,
545 size_t n,
546 const crypto_word_t mask) {
547 assert(!buffers_alias(dst, n, src, n));
548 uint8_t *out = (uint8_t *)dst;
549 const uint8_t *in = (const uint8_t *)src;
550 #if defined(__GNUC__) && !defined(__clang__)
551 // gcc 13.2.0 doesn't automatically vectorize this loop regardless of barrier
552 typedef uint8_t v32u8 __attribute__((vector_size(32), aligned(1), may_alias));
553 size_t n_vec = n & ~(size_t)31;
554 v32u8 masks = ((uint8_t)mask - (v32u8){}); // broadcast
555 for (size_t i = 0; i < n_vec; i += 32) {
556 *(v32u8 *)&out[i] ^= masks & *(v32u8 *)&in[i];
557 }
558 out += n_vec;
559 n -= n_vec;
560 #endif
561 for (size_t i = 0; i < n; i++) {
562 out[i] ^= value_barrier_w(mask) & in[i];
563 }
564 }
565
566 #if defined(BORINGSSL_CONSTANT_TIME_VALIDATION)
567
568 // CONSTTIME_SECRET takes a pointer and a number of bytes and marks that region
569 // of memory as secret. Secret data is tracked as it flows to registers and
570 // other parts of a memory. If secret data is used as a condition for a branch,
571 // or as a memory index, it will trigger warnings in valgrind.
572 #define CONSTTIME_SECRET(ptr, len) VALGRIND_MAKE_MEM_UNDEFINED(ptr, len)
573
574 // CONSTTIME_DECLASSIFY takes a pointer and a number of bytes and marks that
575 // region of memory as public. Public data is not subject to constant-time
576 // rules.
577 #define CONSTTIME_DECLASSIFY(ptr, len) VALGRIND_MAKE_MEM_DEFINED(ptr, len)
578
579 #else
580
581 #define CONSTTIME_SECRET(ptr, len)
582 #define CONSTTIME_DECLASSIFY(ptr, len)
583
584 #endif // BORINGSSL_CONSTANT_TIME_VALIDATION
585
586 static inline crypto_word_t constant_time_declassify_w(crypto_word_t v) {
587 // Return |v| through a value barrier to be safe. Valgrind-based constant-time
588 // validation is partly to check the compiler has not undone any constant-time
589 // work. Any place |BORINGSSL_CONSTANT_TIME_VALIDATION| influences
590 // optimizations, this validation is inaccurate.
591 //
592 // However, by sending pointers through valgrind, we likely inhibit escape
593 // analysis. On local variables, particularly booleans, we likely
594 // significantly impact optimizations.
595 //
596 // Thus, to be safe, stick a value barrier, in hopes of comparably inhibiting
597 // compiler analysis.
598 CONSTTIME_DECLASSIFY(&v, sizeof(v));
599 return value_barrier_w(v);
600 }
601
602 static inline int constant_time_declassify_int(int v) {
603 static_assert(sizeof(uint32_t) == sizeof(int),
604 "int is not the same size as uint32_t");
605 // See comment above.
606 CONSTTIME_DECLASSIFY(&v, sizeof(v));
607 return value_barrier_u32(v);
608 }
609
610 // declassify_assert behaves like |assert| but declassifies the result of
611 // evaluating |expr|. This allows the assertion to branch on the (presumably
612 // public) result, but still ensures that values leading up to the computation
613 // were secret.
614 #define declassify_assert(expr) assert(constant_time_declassify_int(expr))
615
616
617 // Thread-safe initialisation.
618
619 #if !defined(OPENSSL_THREADS)
620 typedef uint32_t CRYPTO_once_t;
621 #define CRYPTO_ONCE_INIT 0
622 #elif defined(OPENSSL_WINDOWS_THREADS)
623 typedef INIT_ONCE CRYPTO_once_t;
624 #define CRYPTO_ONCE_INIT INIT_ONCE_STATIC_INIT
625 #elif defined(OPENSSL_PTHREADS)
626 typedef pthread_once_t CRYPTO_once_t;
627 #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
628 #else
629 #error "Unknown threading library"
630 #endif
631
632 // CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
633 // concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
634 // then they will block until |init| completes, but |init| will have only been
635 // called once.
636 //
637 // The |once| argument must be a |CRYPTO_once_t| that has been initialised with
638 // the value |CRYPTO_ONCE_INIT|.
639 OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
640
641
642 // Atomics.
643 //
644 // The following functions provide an API analogous to <stdatomic.h> from C11
645 // and abstract between a few variations on atomics we need to support.
646
647 #if defined(OPENSSL_THREADS)
648
649 using CRYPTO_atomic_u32 = std::atomic<uint32_t>;
650
651 static_assert(sizeof(CRYPTO_atomic_u32) == sizeof(uint32_t), "");
652
653 inline uint32_t CRYPTO_atomic_load_u32(const CRYPTO_atomic_u32 *val) {
654 return val->load(std::memory_order_seq_cst);
655 }
656
657 inline bool CRYPTO_atomic_compare_exchange_weak_u32(CRYPTO_atomic_u32 *val,
658 uint32_t *expected,
659 uint32_t desired) {
660 return val->compare_exchange_weak(
661 *expected, desired, std::memory_order_seq_cst, std::memory_order_seq_cst);
662 }
663
664 inline void CRYPTO_atomic_store_u32(CRYPTO_atomic_u32 *val, uint32_t desired) {
665 val->store(desired, std::memory_order_seq_cst);
666 }
667
668 #else
669
670 typedef uint32_t CRYPTO_atomic_u32;
671
672 OPENSSL_INLINE uint32_t CRYPTO_atomic_load_u32(CRYPTO_atomic_u32 *val) {
673 return *val;
674 }
675
676 OPENSSL_INLINE int CRYPTO_atomic_compare_exchange_weak_u32(
677 CRYPTO_atomic_u32 *val, uint32_t *expected, uint32_t desired) {
678 if (*val != *expected) {
679 *expected = *val;
680 return 0;
681 }
682 *val = desired;
683 return 1;
684 }
685
686 OPENSSL_INLINE void CRYPTO_atomic_store_u32(CRYPTO_atomic_u32 *val,
687 uint32_t desired) {
688 *val = desired;
689 }
690
691 #endif
692
693 // See the comment in the |__cplusplus| section above.
694 static_assert(sizeof(CRYPTO_atomic_u32) == sizeof(uint32_t),
695 "CRYPTO_atomic_u32 does not match uint32_t size");
696 static_assert(alignof(CRYPTO_atomic_u32) == alignof(uint32_t),
697 "CRYPTO_atomic_u32 does not match uint32_t alignment");
698
699
700 // Reference counting.
701
702 // CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates.
703 #define CRYPTO_REFCOUNT_MAX 0xffffffff
704
705 // CRYPTO_refcount_inc atomically increments the value at |*count| unless the
706 // value would overflow. It's safe for multiple threads to concurrently call
707 // this or |CRYPTO_refcount_dec_and_test_zero| on the same
708 // |CRYPTO_refcount_t|.
709 OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count);
710
711 // CRYPTO_refcount_dec_and_test_zero tests the value at |*count|:
712 // if it's zero, it crashes the address space.
713 // if it's the maximum value, it returns zero.
714 // otherwise, it atomically decrements it and returns one iff the resulting
715 // value is zero.
716 //
717 // It's safe for multiple threads to concurrently call this or
718 // |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|.
719 OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count);
720
721
722 // Locks.
723
724 #if !defined(OPENSSL_THREADS)
725 typedef struct crypto_mutex_st {
726 char padding; // Empty structs have different sizes in C and C++.
727 } CRYPTO_MUTEX;
728 #define CRYPTO_MUTEX_INIT \
729 { 0 }
730 #elif defined(OPENSSL_WINDOWS_THREADS)
731 typedef SRWLOCK CRYPTO_MUTEX;
732 #define CRYPTO_MUTEX_INIT SRWLOCK_INIT
733 #elif defined(OPENSSL_PTHREADS)
734 typedef pthread_rwlock_t CRYPTO_MUTEX;
735 #define CRYPTO_MUTEX_INIT PTHREAD_RWLOCK_INITIALIZER
736 #else
737 #error "Unknown threading library"
738 #endif
739
740 // CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
741 // |CRYPTO_MUTEX_INIT|.
742 OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
743
744 // CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
745 // read lock, but none may have a write lock.
746 OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
747
748 // CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
749 // of lock on it.
750 OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
751
752 // CRYPTO_MUTEX_unlock_read unlocks |lock| for reading.
753 OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock);
754
755 // CRYPTO_MUTEX_unlock_write unlocks |lock| for writing.
756 OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock);
757
758 // CRYPTO_MUTEX_cleanup releases all resources held by |lock|.
759 OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
760
761 #if defined(__cplusplus)
762 extern "C++" {
763
764 BSSL_NAMESPACE_BEGIN
765
766 namespace internal {
767
768 // MutexLockBase is a RAII helper for CRYPTO_MUTEX locking.
769 template <void (*LockFunc)(CRYPTO_MUTEX *), void (*ReleaseFunc)(CRYPTO_MUTEX *)>
770 class MutexLockBase {
771 public:
772 explicit MutexLockBase(CRYPTO_MUTEX *mu) : mu_(mu) {
773 assert(mu_ != nullptr);
774 LockFunc(mu_);
775 }
776 ~MutexLockBase() { ReleaseFunc(mu_); }
777 MutexLockBase(const MutexLockBase<LockFunc, ReleaseFunc> &) = delete;
778 MutexLockBase &operator=(const MutexLockBase<LockFunc, ReleaseFunc> &) =
779 delete;
780
781 private:
782 CRYPTO_MUTEX *const mu_;
783 };
784
785 } // namespace internal
786
787 using MutexWriteLock =
788 internal::MutexLockBase<CRYPTO_MUTEX_lock_write, CRYPTO_MUTEX_unlock_write>;
789 using MutexReadLock =
790 internal::MutexLockBase<CRYPTO_MUTEX_lock_read, CRYPTO_MUTEX_unlock_read>;
791
792 BSSL_NAMESPACE_END
793
794 } // extern "C++"
795 #endif // defined(__cplusplus)
796
797
798 // Thread local storage.
799
800 // thread_local_data_t enumerates the types of thread-local data that can be
801 // stored.
802 typedef enum {
803 OPENSSL_THREAD_LOCAL_ERR = 0,
804 OPENSSL_THREAD_LOCAL_RAND,
805 OPENSSL_THREAD_LOCAL_FIPS_COUNTERS,
806 OPENSSL_THREAD_LOCAL_FIPS_SERVICE_INDICATOR_STATE,
807 OPENSSL_THREAD_LOCAL_TEST,
808 NUM_OPENSSL_THREAD_LOCALS,
809 } thread_local_data_t;
810
811 // thread_local_destructor_t is the type of a destructor function that will be
812 // called when a thread exits and its thread-local storage needs to be freed.
813 typedef void (*thread_local_destructor_t)(void *);
814
815 // CRYPTO_get_thread_local gets the pointer value that is stored for the
816 // current thread for the given index, or NULL if none has been set.
817 OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
818
819 // CRYPTO_set_thread_local sets a pointer value for the current thread at the
820 // given index. This function should only be called once per thread for a given
821 // |index|: rather than update the pointer value itself, update the data that
822 // is pointed to.
823 //
824 // The destructor function will be called when a thread exits to free this
825 // thread-local data. All calls to |CRYPTO_set_thread_local| with the same
826 // |index| should have the same |destructor| argument. The destructor may be
827 // called with a NULL argument if a thread that never set a thread-local
828 // pointer for |index|, exits. The destructor may be called concurrently with
829 // different arguments.
830 //
831 // This function returns one on success or zero on error. If it returns zero
832 // then |destructor| has been called with |value| already.
833 OPENSSL_EXPORT int CRYPTO_set_thread_local(
834 thread_local_data_t index, void *value,
835 thread_local_destructor_t destructor);
836
837
838 // ex_data
839
840 typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
841
842 // CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
843 // supports ex_data. It should defined as a static global within the module
844 // which defines that type.
845 typedef struct {
846 CRYPTO_MUTEX lock;
847 // funcs is a linked list of |CRYPTO_EX_DATA_FUNCS| structures. It may be
848 // traversed without serialization only up to |num_funcs|. last points to the
849 // final entry of |funcs|, or NULL if empty.
850 CRYPTO_EX_DATA_FUNCS *funcs, *last;
851 // num_funcs is the number of entries in |funcs|.
852 CRYPTO_atomic_u32 num_funcs;
853 // num_reserved is one if the ex_data index zero is reserved for legacy
854 // |TYPE_get_app_data| functions.
855 uint8_t num_reserved;
856 } CRYPTO_EX_DATA_CLASS;
857
858 #define CRYPTO_EX_DATA_CLASS_INIT \
859 { CRYPTO_MUTEX_INIT, NULL, NULL, {}, 0 }
860 #define CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA \
861 { CRYPTO_MUTEX_INIT, NULL, NULL, {}, 1 }
862
863 // CRYPTO_get_ex_new_index_ex allocates a new index for |ex_data_class|. Each
864 // class of object should provide a wrapper function that uses the correct
865 // |CRYPTO_EX_DATA_CLASS|. It returns the new index on success and -1 on error.
866 OPENSSL_EXPORT int CRYPTO_get_ex_new_index_ex(
867 CRYPTO_EX_DATA_CLASS *ex_data_class, long argl, void *argp,
868 CRYPTO_EX_free *free_func);
869
870 // CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class
871 // of object should provide a wrapper function.
872 OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
873
874 // CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL
875 // if no such index exists. Each class of object should provide a wrapper
876 // function.
877 OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
878
879 // CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|.
880 OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad);
881
882 // CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
883 // object of the given class.
884 OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
885 void *obj, CRYPTO_EX_DATA *ad);
886
887
888 // Endianness conversions.
889
890 #if defined(__GNUC__) && __GNUC__ >= 2
891 static inline uint16_t CRYPTO_bswap2(uint16_t x) {
892 return __builtin_bswap16(x);
893 }
894
895 static inline uint32_t CRYPTO_bswap4(uint32_t x) {
896 return __builtin_bswap32(x);
897 }
898
899 static inline uint64_t CRYPTO_bswap8(uint64_t x) {
900 return __builtin_bswap64(x);
901 }
902 #elif defined(_MSC_VER)
903 OPENSSL_MSVC_PRAGMA(warning(push, 3))
904 #include <stdlib.h>
905 OPENSSL_MSVC_PRAGMA(warning(pop))
906 #pragma intrinsic(_byteswap_uint64, _byteswap_ulong, _byteswap_ushort)
907 static inline uint16_t CRYPTO_bswap2(uint16_t x) { return _byteswap_ushort(x); }
908
909 static inline uint32_t CRYPTO_bswap4(uint32_t x) { return _byteswap_ulong(x); }
910
911 static inline uint64_t CRYPTO_bswap8(uint64_t x) { return _byteswap_uint64(x); }
912 #else
913 static inline uint16_t CRYPTO_bswap2(uint16_t x) { return (x >> 8) | (x << 8); }
914
915 static inline uint32_t CRYPTO_bswap4(uint32_t x) {
916 x = (x >> 16) | (x << 16);
917 x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
918 return x;
919 }
920
921 static inline uint64_t CRYPTO_bswap8(uint64_t x) {
922 return CRYPTO_bswap4(x >> 32) | (((uint64_t)CRYPTO_bswap4(x)) << 32);
923 }
924 #endif
925
926
927 // Language bug workarounds.
928 //
929 // Most C standard library functions are undefined if passed NULL, even when the
930 // corresponding length is zero. This gives them (and, in turn, all functions
931 // which call them) surprising behavior on empty arrays. Some compilers will
932 // miscompile code due to this rule. See also
933 // https://www.imperialviolet.org/2016/06/26/nonnull.html
934 //
935 // These wrapper functions behave the same as the corresponding C standard
936 // functions, but behave as expected when passed NULL if the length is zero.
937 //
938 // Note |OPENSSL_memcmp| is a different function from |CRYPTO_memcmp|.
939
940 // C++ defines |memchr| as a const-correct overload.
941 #if defined(__cplusplus)
942 extern "C++" {
943
944 static inline const void *OPENSSL_memchr(const void *s, int c, size_t n) {
945 if (n == 0) {
946 return NULL;
947 }
948
949 return memchr(s, c, n);
950 }
951
952 static inline void *OPENSSL_memchr(void *s, int c, size_t n) {
953 if (n == 0) {
954 return NULL;
955 }
956
957 return memchr(s, c, n);
958 }
959
960 } // extern "C++"
961 #else // __cplusplus
962
963 static inline void *OPENSSL_memchr(const void *s, int c, size_t n) {
964 if (n == 0) {
965 return NULL;
966 }
967
968 return memchr(s, c, n);
969 }
970
971 #endif // __cplusplus
972
973 static inline int OPENSSL_memcmp(const void *s1, const void *s2, size_t n) {
974 if (n == 0) {
975 return 0;
976 }
977
978 return memcmp(s1, s2, n);
979 }
980
981 static inline void *OPENSSL_memcpy(void *dst, const void *src, size_t n) {
982 if (n == 0) {
983 return dst;
984 }
985
986 return memcpy(dst, src, n);
987 }
988
989 static inline void *OPENSSL_memmove(void *dst, const void *src, size_t n) {
990 if (n == 0) {
991 return dst;
992 }
993
994 return memmove(dst, src, n);
995 }
996
997 static inline void *OPENSSL_memset(void *dst, int c, size_t n) {
998 if (n == 0) {
999 return dst;
1000 }
1001
1002 return memset(dst, c, n);
1003 }
1004
1005
1006 // Loads and stores.
1007 //
1008 // The following functions load and store sized integers with the specified
1009 // endianness. They use |memcpy|, and so avoid alignment or strict aliasing
1010 // requirements on the input and output pointers.
1011
1012 static inline uint16_t CRYPTO_load_u16_be(const void *in) {
1013 uint16_t v;
1014 OPENSSL_memcpy(&v, in, sizeof(v));
1015 return CRYPTO_bswap2(v);
1016 }
1017
1018 static inline void CRYPTO_store_u16_be(void *out, uint16_t v) {
1019 v = CRYPTO_bswap2(v);
1020 OPENSSL_memcpy(out, &v, sizeof(v));
1021 }
1022
1023 static inline uint32_t CRYPTO_load_u32_le(const void *in) {
1024 uint32_t v;
1025 OPENSSL_memcpy(&v, in, sizeof(v));
1026 return v;
1027 }
1028
1029 static inline void CRYPTO_store_u32_le(void *out, uint32_t v) {
1030 OPENSSL_memcpy(out, &v, sizeof(v));
1031 }
1032
1033 static inline uint32_t CRYPTO_load_u32_be(const void *in) {
1034 uint32_t v;
1035 OPENSSL_memcpy(&v, in, sizeof(v));
1036 return CRYPTO_bswap4(v);
1037 }
1038
1039 static inline void CRYPTO_store_u32_be(void *out, uint32_t v) {
1040 v = CRYPTO_bswap4(v);
1041 OPENSSL_memcpy(out, &v, sizeof(v));
1042 }
1043
1044 static inline uint64_t CRYPTO_load_u64_le(const void *in) {
1045 uint64_t v;
1046 OPENSSL_memcpy(&v, in, sizeof(v));
1047 return v;
1048 }
1049
1050 static inline void CRYPTO_store_u64_le(void *out, uint64_t v) {
1051 OPENSSL_memcpy(out, &v, sizeof(v));
1052 }
1053
1054 static inline uint64_t CRYPTO_load_u64_be(const void *ptr) {
1055 uint64_t ret;
1056 OPENSSL_memcpy(&ret, ptr, sizeof(ret));
1057 return CRYPTO_bswap8(ret);
1058 }
1059
1060 static inline void CRYPTO_store_u64_be(void *out, uint64_t v) {
1061 v = CRYPTO_bswap8(v);
1062 OPENSSL_memcpy(out, &v, sizeof(v));
1063 }
1064
1065 static inline crypto_word_t CRYPTO_load_word_le(const void *in) {
1066 crypto_word_t v;
1067 OPENSSL_memcpy(&v, in, sizeof(v));
1068 return v;
1069 }
1070
1071 static inline void CRYPTO_store_word_le(void *out, crypto_word_t v) {
1072 OPENSSL_memcpy(out, &v, sizeof(v));
1073 }
1074
1075 static inline crypto_word_t CRYPTO_load_word_be(const void *in) {
1076 crypto_word_t v;
1077 OPENSSL_memcpy(&v, in, sizeof(v));
1078 #if defined(OPENSSL_64_BIT)
1079 static_assert(sizeof(v) == 8, "crypto_word_t has unexpected size");
1080 return CRYPTO_bswap8(v);
1081 #else
1082 static_assert(sizeof(v) == 4, "crypto_word_t has unexpected size");
1083 return CRYPTO_bswap4(v);
1084 #endif
1085 }
1086
1087
1088 // Bit rotation functions.
1089 //
1090 // Note these functions use |(-shift) & 31|, etc., because shifting by the bit
1091 // width is undefined. Both Clang and GCC recognize this pattern as a rotation,
1092 // but MSVC does not. Instead, we call MSVC's built-in functions.
1093
1094 static inline uint32_t CRYPTO_rotl_u32(uint32_t value, int shift) {
1095 #if defined(_MSC_VER)
1096 return _rotl(value, shift);
1097 #else
1098 return (value << shift) | (value >> ((-shift) & 31));
1099 #endif
1100 }
1101
1102 static inline uint32_t CRYPTO_rotr_u32(uint32_t value, int shift) {
1103 #if defined(_MSC_VER)
1104 return _rotr(value, shift);
1105 #else
1106 return (value >> shift) | (value << ((-shift) & 31));
1107 #endif
1108 }
1109
1110 static inline uint64_t CRYPTO_rotl_u64(uint64_t value, int shift) {
1111 #if defined(_MSC_VER)
1112 return _rotl64(value, shift);
1113 #else
1114 return (value << shift) | (value >> ((-shift) & 63));
1115 #endif
1116 }
1117
1118 static inline uint64_t CRYPTO_rotr_u64(uint64_t value, int shift) {
1119 #if defined(_MSC_VER)
1120 return _rotr64(value, shift);
1121 #else
1122 return (value >> shift) | (value << ((-shift) & 63));
1123 #endif
1124 }
1125
1126
1127 // FIPS functions.
1128
1129 #if defined(BORINGSSL_FIPS)
1130
1131 // BORINGSSL_FIPS_abort is called when a FIPS power-on or continuous test
1132 // fails. It prevents any further cryptographic operations by the current
1133 // process.
1134 void BORINGSSL_FIPS_abort(void) __attribute__((noreturn));
1135
1136 // boringssl_self_test_startup runs all startup self tests and returns one on
1137 // success or zero on error. Startup self tests do not include lazy tests.
1138 // Call |BORINGSSL_self_test| to run every self test.
1139 int boringssl_self_test_startup(void);
1140
1141 // boringssl_ensure_rsa_self_test checks whether the RSA self-test has been run
1142 // in this address space. If not, it runs it and crashes the address space if
1143 // unsuccessful.
1144 void boringssl_ensure_rsa_self_test(void);
1145
1146 // boringssl_ensure_ecc_self_test checks whether the ECDSA and ECDH self-test
1147 // has been run in this address space. If not, it runs it and crashes the
1148 // address space if unsuccessful.
1149 void boringssl_ensure_ecc_self_test(void);
1150
1151 // boringssl_ensure_ffdh_self_test checks whether the FFDH self-test has been
1152 // run in this address space. If not, it runs it and crashes the address space
1153 // if unsuccessful.
1154 void boringssl_ensure_ffdh_self_test(void);
1155
1156 #else
1157
1158 // Outside of FIPS mode, the lazy tests are no-ops.
1159
1160 OPENSSL_INLINE void boringssl_ensure_rsa_self_test(void) {}
1161 OPENSSL_INLINE void boringssl_ensure_ecc_self_test(void) {}
1162 OPENSSL_INLINE void boringssl_ensure_ffdh_self_test(void) {}
1163
1164 #endif // FIPS
1165
1166 // boringssl_self_test_sha256 performs a SHA-256 KAT.
1167 int boringssl_self_test_sha256(void);
1168
1169 // boringssl_self_test_sha512 performs a SHA-512 KAT.
1170 int boringssl_self_test_sha512(void);
1171
1172 // boringssl_self_test_hmac_sha256 performs an HMAC-SHA-256 KAT.
1173 int boringssl_self_test_hmac_sha256(void);
1174
1175 #if defined(BORINGSSL_FIPS_COUNTERS)
1176 void boringssl_fips_inc_counter(enum fips_counter_t counter);
1177 #else
1178 OPENSSL_INLINE void boringssl_fips_inc_counter(enum fips_counter_t counter) {}
1179 #endif
1180
1181 #if defined(BORINGSSL_FIPS_BREAK_TESTS)
1182 OPENSSL_INLINE int boringssl_fips_break_test(const char *test) {
1183 const char *const value = getenv("BORINGSSL_FIPS_BREAK_TEST");
1184 return value != NULL && strcmp(value, test) == 0;
1185 }
1186 #else
1187 OPENSSL_INLINE int boringssl_fips_break_test(const char *test) { return 0; }
1188 #endif // BORINGSSL_FIPS_BREAK_TESTS
1189
1190
1191 // Runtime CPU feature support
1192
1193 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
1194 // OPENSSL_ia32cap_P contains the Intel CPUID bits when running on an x86 or
1195 // x86-64 system.
1196 //
1197 // Index 0:
1198 // EDX for CPUID where EAX = 1
1199 // Bit 20 is always zero
1200 // Bit 28 is adjusted to reflect whether the data cache is shared between
1201 // multiple logical cores
1202 // Bit 30 is used to indicate an Intel CPU
1203 // Index 1:
1204 // ECX for CPUID where EAX = 1
1205 // Bit 11 is used to indicate AMD XOP support, not SDBG
1206 // Index 2:
1207 // EBX for CPUID where EAX = 7, ECX = 0
1208 // Bit 14 (for removed feature MPX) is used to indicate a preference for ymm
1209 // registers over zmm even when zmm registers are supported
1210 // Index 3:
1211 // ECX for CPUID where EAX = 7, ECX = 0
1212 //
1213 // Note: the CPUID bits are pre-adjusted for the OSXSAVE bit and the XMM, YMM,
1214 // and AVX512 bits in XCR0, so it is not necessary to check those. (WARNING: See
1215 // caveats in cpu_intel.c.)
1216 //
1217 // From C, this symbol should only be accessed with |OPENSSL_get_ia32cap|.
1218 extern uint32_t OPENSSL_ia32cap_P[4];
1219
1220 // OPENSSL_get_ia32cap initializes the library if needed and returns the |idx|th
1221 // entry of |OPENSSL_ia32cap_P|. It is marked as a const function so duplicate
1222 // calls can be merged by the compiler, at least when indices match.
1223 OPENSSL_ATTR_CONST uint32_t OPENSSL_get_ia32cap(int idx);
1224
1225 // See Intel manual, volume 2A, table 3-11.
1226
1227 OPENSSL_INLINE int CRYPTO_is_FXSR_capable(void) {
1228 #if defined(__FXSR__)
1229 return 1;
1230 #else
1231 return (OPENSSL_get_ia32cap(0) & (1u << 24)) != 0;
1232 #endif
1233 }
1234
1235 OPENSSL_INLINE int CRYPTO_is_intel_cpu(void) {
1236 // The reserved bit 30 is used to indicate an Intel CPU.
1237 return (OPENSSL_get_ia32cap(0) & (1u << 30)) != 0;
1238 }
1239
1240 // See Intel manual, volume 2A, table 3-10.
1241
1242 OPENSSL_INLINE int CRYPTO_is_PCLMUL_capable(void) {
1243 #if defined(__PCLMUL__)
1244 return 1;
1245 #else
1246 return (OPENSSL_get_ia32cap(1) & (1u << 1)) != 0;
1247 #endif
1248 }
1249
1250 OPENSSL_INLINE int CRYPTO_is_SSSE3_capable(void) {
1251 #if defined(__SSSE3__)
1252 return 1;
1253 #else
1254 return (OPENSSL_get_ia32cap(1) & (1u << 9)) != 0;
1255 #endif
1256 }
1257
1258 OPENSSL_INLINE int CRYPTO_is_SSE4_1_capable(void) {
1259 #if defined(__SSE4_1__)
1260 return 1;
1261 #else
1262 return (OPENSSL_get_ia32cap(1) & (1u << 19)) != 0;
1263 #endif
1264 }
1265
1266 OPENSSL_INLINE int CRYPTO_is_MOVBE_capable(void) {
1267 #if defined(__MOVBE__)
1268 return 1;
1269 #else
1270 return (OPENSSL_get_ia32cap(1) & (1u << 22)) != 0;
1271 #endif
1272 }
1273
1274 OPENSSL_INLINE int CRYPTO_is_AESNI_capable(void) {
1275 #if defined(__AES__)
1276 return 1;
1277 #else
1278 return (OPENSSL_get_ia32cap(1) & (1u << 25)) != 0;
1279 #endif
1280 }
1281
1282 // We intentionally avoid defining a |CRYPTO_is_XSAVE_capable| function. See
1283 // |CRYPTO_cpu_perf_is_like_silvermont|.
1284
1285 OPENSSL_INLINE int CRYPTO_is_AVX_capable(void) {
1286 #if defined(__AVX__)
1287 return 1;
1288 #else
1289 return (OPENSSL_get_ia32cap(1) & (1u << 28)) != 0;
1290 #endif
1291 }
1292
1293 OPENSSL_INLINE int CRYPTO_is_RDRAND_capable(void) {
1294 // We intentionally do not check |__RDRND__| here. On some AMD processors, we
1295 // will act as if the hardware is RDRAND-incapable, even it actually supports
1296 // it. See cpu_intel.c.
1297 return (OPENSSL_get_ia32cap(1) & (1u << 30)) != 0;
1298 }
1299
1300 // See Intel manual, volume 2A, table 3-8.
1301
1302 OPENSSL_INLINE int CRYPTO_is_BMI1_capable(void) {
1303 #if defined(__BMI__)
1304 return 1;
1305 #else
1306 return (OPENSSL_get_ia32cap(2) & (1u << 3)) != 0;
1307 #endif
1308 }
1309
1310 OPENSSL_INLINE int CRYPTO_is_AVX2_capable(void) {
1311 #if defined(__AVX2__)
1312 return 1;
1313 #else
1314 return (OPENSSL_get_ia32cap(2) & (1u << 5)) != 0;
1315 #endif
1316 }
1317
1318 OPENSSL_INLINE int CRYPTO_is_BMI2_capable(void) {
1319 #if defined(__BMI2__)
1320 return 1;
1321 #else
1322 return (OPENSSL_get_ia32cap(2) & (1u << 8)) != 0;
1323 #endif
1324 }
1325
1326 OPENSSL_INLINE int CRYPTO_is_ADX_capable(void) {
1327 #if defined(__ADX__)
1328 return 1;
1329 #else
1330 return (OPENSSL_get_ia32cap(2) & (1u << 19)) != 0;
1331 #endif
1332 }
1333
1334 // SHA-1 and SHA-256 are defined as a single extension.
1335 OPENSSL_INLINE int CRYPTO_is_x86_SHA_capable(void) {
1336 // We should check __SHA__ here, but for now we ignore it. We've run into a
1337 // few places where projects build with -march=goldmont, but need a build that
1338 // does not require SHA extensions:
1339 //
1340 // - Some CrOS toolchain definitions are incorrect and build with
1341 // -march=goldmont when targetting boards that are not Goldmont. b/320482539
1342 // tracks fixing this.
1343 //
1344 // - Sometimes projects build with -march=goldmont as a rough optimized
1345 // baseline. However, Intel CPU capabilities are not strictly linear, so
1346 // this does not quite work. Some combination of -mtune and
1347 // -march=x86-64-v{1,2,3,4} would be a better strategy here.
1348 //
1349 // - QEMU versions before 8.2 do not support SHA extensions and disable it
1350 // with a warning. Projects that target Goldmont and test on QEMU will
1351 // break. The long-term fix is to update to 8.2. A principled short-term fix
1352 // would be -march=goldmont -mno-sha, to reflect that the binary needs to
1353 // run on both QEMU-8.1-Goldmont and actual-Goldmont.
1354 //
1355 // TODO(b/320482539): Once the CrOS toolchain is fixed, try this again.
1356 return (OPENSSL_get_ia32cap(2) & (1u << 29)) != 0;
1357 }
1358
1359 // CRYPTO_cpu_perf_is_like_silvermont returns one if, based on a heuristic, the
1360 // CPU has Silvermont-like performance characteristics. It is often faster to
1361 // run different codepaths on these CPUs than the available instructions would
1362 // otherwise select. See chacha-x86_64.pl.
1363 //
1364 // Bonnell, Silvermont's predecessor in the Atom lineup, will also be matched by
1365 // this. Goldmont (Silvermont's successor in the Atom lineup) added XSAVE so it
1366 // isn't matched by this. Various sources indicate AMD first implemented MOVBE
1367 // and XSAVE at the same time in Jaguar, so it seems like AMD chips will not be
1368 // matched by this. That seems to be the case for other x86(-64) CPUs.
1369 OPENSSL_INLINE int CRYPTO_cpu_perf_is_like_silvermont(void) {
1370 // WARNING: This MUST NOT be used to guard the execution of the XSAVE
1371 // instruction. This is the "hardware supports XSAVE" bit, not the OSXSAVE bit
1372 // that indicates whether we can safely execute XSAVE. This bit may be set
1373 // even when XSAVE is disabled (by the operating system). See how the users of
1374 // this bit use it.
1375 //
1376 // Historically, the XSAVE bit was artificially cleared on Knights Landing
1377 // and Knights Mill chips, but as Intel has removed all support from GCC,
1378 // LLVM, and SDE, we assume they are no longer worth special-casing.
1379 int hardware_supports_xsave = (OPENSSL_get_ia32cap(1) & (1u << 26)) != 0;
1380 return !hardware_supports_xsave && CRYPTO_is_MOVBE_capable();
1381 }
1382
1383 OPENSSL_INLINE int CRYPTO_is_AVX512BW_capable(void) {
1384 #if defined(__AVX512BW__)
1385 return 1;
1386 #else
1387 return (OPENSSL_get_ia32cap(2) & (1u << 30)) != 0;
1388 #endif
1389 }
1390
1391 OPENSSL_INLINE int CRYPTO_is_AVX512VL_capable(void) {
1392 #if defined(__AVX512VL__)
1393 return 1;
1394 #else
1395 return (OPENSSL_get_ia32cap(2) & (1u << 31)) != 0;
1396 #endif
1397 }
1398
1399 // CRYPTO_cpu_avoid_zmm_registers returns 1 if zmm registers (512-bit vectors)
1400 // should not be used even if the CPU supports them.
1401 //
1402 // Note that this reuses the bit for the removed MPX feature.
1403 OPENSSL_INLINE int CRYPTO_cpu_avoid_zmm_registers(void) {
1404 return (OPENSSL_get_ia32cap(2) & (1u << 14)) != 0;
1405 }
1406
1407 OPENSSL_INLINE int CRYPTO_is_VAES_capable(void) {
1408 #if defined(__VAES__)
1409 return 1;
1410 #else
1411 return (OPENSSL_get_ia32cap(3) & (1u << 9)) != 0;
1412 #endif
1413 }
1414
1415 OPENSSL_INLINE int CRYPTO_is_VPCLMULQDQ_capable(void) {
1416 #if defined(__VPCLMULQDQ__)
1417 return 1;
1418 #else
1419 return (OPENSSL_get_ia32cap(3) & (1u << 10)) != 0;
1420 #endif
1421 }
1422
1423 #endif // OPENSSL_X86 || OPENSSL_X86_64
1424
1425 #if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
1426
1427 // OPENSSL_armcap_P contains ARM CPU capabilities. From C, this should only be
1428 // accessed with |OPENSSL_get_armcap|.
1429 extern uint32_t OPENSSL_armcap_P;
1430
1431 // OPENSSL_get_armcap initializes the library if needed and returns ARM CPU
1432 // capabilities. It is marked as a const function so duplicate calls can be
1433 // merged by the compiler.
1434 OPENSSL_ATTR_CONST uint32_t OPENSSL_get_armcap(void);
1435
1436 // We do not detect any features at runtime on several 32-bit Arm platforms.
1437 // Apple platforms and OpenBSD require NEON and moved to 64-bit to pick up Armv8
1438 // extensions. Android baremetal does not aim to support 32-bit Arm at all, but
1439 // it simplifies things to make it build.
1440 #if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP) && \
1441 (defined(OPENSSL_APPLE) || defined(OPENSSL_OPENBSD) || \
1442 defined(ANDROID_BAREMETAL))
1443 #define OPENSSL_STATIC_ARMCAP
1444 #endif
1445
1446 // Normalize some older feature flags to their modern ACLE values.
1447 // https://developer.arm.com/architectures/system-architectures/software-standards/acle
1448 #if defined(__ARM_NEON__) && !defined(__ARM_NEON)
1449 #define __ARM_NEON 1
1450 #endif
1451 #if defined(__ARM_FEATURE_CRYPTO)
1452 #if !defined(__ARM_FEATURE_AES)
1453 #define __ARM_FEATURE_AES 1
1454 #endif
1455 #if !defined(__ARM_FEATURE_SHA2)
1456 #define __ARM_FEATURE_SHA2 1
1457 #endif
1458 #endif
1459
1460 // CRYPTO_is_NEON_capable returns true if the current CPU has a NEON unit. If
1461 // this is known statically, it is a constant inline function.
1462 OPENSSL_INLINE int CRYPTO_is_NEON_capable(void) {
1463 #if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON)
1464 return 1;
1465 #elif defined(OPENSSL_STATIC_ARMCAP)
1466 return 0;
1467 #else
1468 return (OPENSSL_get_armcap() & ARMV7_NEON) != 0;
1469 #endif
1470 }
1471
1472 OPENSSL_INLINE int CRYPTO_is_ARMv8_AES_capable(void) {
1473 #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_AES)
1474 return 1;
1475 #elif defined(OPENSSL_STATIC_ARMCAP)
1476 return 0;
1477 #else
1478 return (OPENSSL_get_armcap() & ARMV8_AES) != 0;
1479 #endif
1480 }
1481
1482 OPENSSL_INLINE int CRYPTO_is_ARMv8_PMULL_capable(void) {
1483 #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_AES)
1484 return 1;
1485 #elif defined(OPENSSL_STATIC_ARMCAP)
1486 return 0;
1487 #else
1488 return (OPENSSL_get_armcap() & ARMV8_PMULL) != 0;
1489 #endif
1490 }
1491
1492 OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA1_capable(void) {
1493 // SHA-1 and SHA-2 (only) share |__ARM_FEATURE_SHA2| but otherwise
1494 // are dealt with independently.
1495 #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_SHA2)
1496 return 1;
1497 #elif defined(OPENSSL_STATIC_ARMCAP)
1498 return 0;
1499 #else
1500 return (OPENSSL_get_armcap() & ARMV8_SHA1) != 0;
1501 #endif
1502 }
1503
1504 OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA256_capable(void) {
1505 // SHA-1 and SHA-2 (only) share |__ARM_FEATURE_SHA2| but otherwise
1506 // are dealt with independently.
1507 #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_SHA2)
1508 return 1;
1509 #elif defined(OPENSSL_STATIC_ARMCAP)
1510 return 0;
1511 #else
1512 return (OPENSSL_get_armcap() & ARMV8_SHA256) != 0;
1513 #endif
1514 }
1515
1516 OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA512_capable(void) {
1517 // There is no |OPENSSL_STATIC_ARMCAP_SHA512|.
1518 #if defined(__ARM_FEATURE_SHA512)
1519 return 1;
1520 #elif defined(OPENSSL_STATIC_ARMCAP)
1521 return 0;
1522 #else
1523 return (OPENSSL_get_armcap() & ARMV8_SHA512) != 0;
1524 #endif
1525 }
1526
1527 #endif // OPENSSL_ARM || OPENSSL_AARCH64
1528
1529 #if defined(BORINGSSL_DISPATCH_TEST)
1530 // Runtime CPU dispatch testing support
1531
1532 // BORINGSSL_function_hit is an array of flags. The following functions will
1533 // set these flags if BORINGSSL_DISPATCH_TEST is defined.
1534 // 0: aes_hw_ctr32_encrypt_blocks
1535 // 1: aes_hw_encrypt
1536 // 2: aesni_gcm_encrypt
1537 // 3: aes_hw_set_encrypt_key
1538 // 4: vpaes_encrypt
1539 // 5: vpaes_set_encrypt_key
1540 extern uint8_t BORINGSSL_function_hit[7];
1541 #endif // BORINGSSL_DISPATCH_TEST
1542
1543 // OPENSSL_vasprintf_internal is just like |vasprintf(3)|. If |system_malloc| is
1544 // 0, memory will be allocated with |OPENSSL_malloc| and must be freed with
1545 // |OPENSSL_free|. Otherwise the system |malloc| function is used and the memory
1546 // must be freed with the system |free| function.
1547 OPENSSL_EXPORT int OPENSSL_vasprintf_internal(char **str, const char *format,
1548 va_list args, int system_malloc)
1549 OPENSSL_PRINTF_FORMAT_FUNC(2, 0);
1550
1551 #if defined(__cplusplus)
1552 } // extern C
1553 #endif
1554
1555 // Arithmetic functions.
1556
1557 // CRYPTO_addc_* returns |x + y + carry|, and sets |*out_carry| to the carry
1558 // bit. |carry| must be zero or one.
1559 #if OPENSSL_HAS_BUILTIN(__builtin_addc)
1560
CRYPTO_addc_impl(unsigned int x,unsigned int y,unsigned int carry,unsigned int * out_carry)1561 inline unsigned int CRYPTO_addc_impl(unsigned int x, unsigned int y,
1562 unsigned int carry,
1563 unsigned int *out_carry) {
1564 return __builtin_addc(x, y, carry, out_carry);
1565 }
1566
CRYPTO_addc_impl(unsigned long x,unsigned long y,unsigned long carry,unsigned long * out_carry)1567 inline unsigned long CRYPTO_addc_impl(unsigned long x, unsigned long y,
1568 unsigned long carry,
1569 unsigned long *out_carry) {
1570 return __builtin_addcl(x, y, carry, out_carry);
1571 }
1572
CRYPTO_addc_impl(unsigned long long x,unsigned long long y,unsigned long long carry,unsigned long long * out_carry)1573 inline unsigned long long CRYPTO_addc_impl(unsigned long long x,
1574 unsigned long long y,
1575 unsigned long long carry,
1576 unsigned long long *out_carry) {
1577 return __builtin_addcll(x, y, carry, out_carry);
1578 }
1579
CRYPTO_addc_u32(uint32_t x,uint32_t y,uint32_t carry,uint32_t * out_carry)1580 inline uint32_t CRYPTO_addc_u32(uint32_t x, uint32_t y, uint32_t carry,
1581 uint32_t *out_carry) {
1582 return CRYPTO_addc_impl(x, y, carry, out_carry);
1583 }
1584
CRYPTO_addc_u64(uint64_t x,uint64_t y,uint64_t carry,uint64_t * out_carry)1585 inline uint64_t CRYPTO_addc_u64(uint64_t x, uint64_t y, uint64_t carry,
1586 uint64_t *out_carry) {
1587 return CRYPTO_addc_impl(x, y, carry, out_carry);
1588 }
1589
1590 #else
1591
CRYPTO_addc_u32(uint32_t x,uint32_t y,uint32_t carry,uint32_t * out_carry)1592 static inline uint32_t CRYPTO_addc_u32(uint32_t x, uint32_t y, uint32_t carry,
1593 uint32_t *out_carry) {
1594 declassify_assert(carry <= 1);
1595 uint64_t ret = carry;
1596 ret += (uint64_t)x + y;
1597 *out_carry = (uint32_t)(ret >> 32);
1598 return (uint32_t)ret;
1599 }
1600
CRYPTO_addc_u64(uint64_t x,uint64_t y,uint64_t carry,uint64_t * out_carry)1601 static inline uint64_t CRYPTO_addc_u64(uint64_t x, uint64_t y, uint64_t carry,
1602 uint64_t *out_carry) {
1603 declassify_assert(carry <= 1);
1604 #if defined(BORINGSSL_HAS_UINT128)
1605 uint128_t ret = carry;
1606 ret += (uint128_t)x + y;
1607 *out_carry = (uint64_t)(ret >> 64);
1608 return (uint64_t)ret;
1609 #else
1610 x += carry;
1611 carry = x < carry;
1612 uint64_t ret = x + y;
1613 carry += ret < x;
1614 *out_carry = carry;
1615 return ret;
1616 #endif
1617 }
1618 #endif
1619
1620
1621 // CRYPTO_subc_* returns |x - y - borrow|, and sets |*out_borrow| to the borrow
1622 // bit. |borrow| must be zero or one.
1623 #if OPENSSL_HAS_BUILTIN(__builtin_subc)
1624
CRYPTO_subc_impl(unsigned int x,unsigned int y,unsigned int borrow,unsigned int * out_borrow)1625 inline unsigned int CRYPTO_subc_impl(unsigned int x, unsigned int y,
1626 unsigned int borrow,
1627 unsigned int *out_borrow) {
1628 return __builtin_subc(x, y, borrow, out_borrow);
1629 }
1630
CRYPTO_subc_impl(unsigned long x,unsigned long y,unsigned long borrow,unsigned long * out_borrow)1631 inline unsigned long CRYPTO_subc_impl(unsigned long x, unsigned long y,
1632 unsigned long borrow,
1633 unsigned long *out_borrow) {
1634 return __builtin_subcl(x, y, borrow, out_borrow);
1635 }
1636
CRYPTO_subc_impl(unsigned long long x,unsigned long long y,unsigned long long borrow,unsigned long long * out_borrow)1637 inline unsigned long long CRYPTO_subc_impl(unsigned long long x,
1638 unsigned long long y,
1639 unsigned long long borrow,
1640 unsigned long long *out_borrow) {
1641 return __builtin_subcll(x, y, borrow, out_borrow);
1642 }
1643
CRYPTO_subc_u32(uint32_t x,uint32_t y,uint32_t borrow,uint32_t * out_borrow)1644 inline uint32_t CRYPTO_subc_u32(uint32_t x, uint32_t y, uint32_t borrow,
1645 uint32_t *out_borrow) {
1646 return CRYPTO_subc_impl(x, y, borrow, out_borrow);
1647 }
1648
CRYPTO_subc_u64(uint64_t x,uint64_t y,uint64_t borrow,uint64_t * out_borrow)1649 inline uint64_t CRYPTO_subc_u64(uint64_t x, uint64_t y, uint64_t borrow,
1650 uint64_t *out_borrow) {
1651 return CRYPTO_subc_impl(x, y, borrow, out_borrow);
1652 }
1653
1654 #else
1655
CRYPTO_subc_u32(uint32_t x,uint32_t y,uint32_t borrow,uint32_t * out_borrow)1656 static inline uint32_t CRYPTO_subc_u32(uint32_t x, uint32_t y, uint32_t borrow,
1657 uint32_t *out_borrow) {
1658 declassify_assert(borrow <= 1);
1659 uint32_t ret = x - y - borrow;
1660 *out_borrow = (x < y) | ((x == y) & borrow);
1661 return ret;
1662 }
1663
CRYPTO_subc_u64(uint64_t x,uint64_t y,uint64_t borrow,uint64_t * out_borrow)1664 static inline uint64_t CRYPTO_subc_u64(uint64_t x, uint64_t y, uint64_t borrow,
1665 uint64_t *out_borrow) {
1666 declassify_assert(borrow <= 1);
1667 uint64_t ret = x - y - borrow;
1668 *out_borrow = (x < y) | ((x == y) & borrow);
1669 return ret;
1670 }
1671 #endif
1672
1673 #if defined(OPENSSL_64_BIT)
1674 #define CRYPTO_addc_w CRYPTO_addc_u64
1675 #define CRYPTO_subc_w CRYPTO_subc_u64
1676 #else
1677 #define CRYPTO_addc_w CRYPTO_addc_u32
1678 #define CRYPTO_subc_w CRYPTO_subc_u32
1679 #endif
1680
1681
1682 #endif // OPENSSL_HEADER_CRYPTO_INTERNAL_H
1683