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