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