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