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