| 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/ex_data.h> |
| 113 | #include <openssl/stack.h> |
| 114 | #include <openssl/thread.h> |
| 115 |
| 116 | #include <assert.h> |
| 117 | #include <string.h> |
| 118 |
| 119 | #if !defined(__cplusplus) |
| 120 | #if defined(_MSC_VER) |
| 121 | #define alignas(x) __declspec(align(x)) |
| 122 | #define alignof __alignof |
| 123 | #else |
| 124 | #include <stdalign.h> |
| 125 | #endif |
| 126 | #endif |
| 127 |
| 128 | #if !defined(OPENSSL_NO_THREADS) && \ |
| 129 | (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__)) |
| 130 | #include <pthread.h> |
| 131 | #define OPENSSL_PTHREADS |
| 132 | #endif |
| 133 |
| 134 | #if !defined(OPENSSL_NO_THREADS) && !defined(OPENSSL_PTHREADS) && \ |
| 135 | defined(OPENSSL_WINDOWS) |
| 136 | #define OPENSSL_WINDOWS_THREADS |
| 137 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 138 | #include <windows.h> |
| 139 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 140 | #endif |
| 141 |
| 142 | #if defined(__cplusplus) |
| 143 | extern "C" { |
| 144 | #endif |
| 145 |
| 146 |
| 147 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \ |
| 148 | defined(OPENSSL_AARCH64) || defined(OPENSSL_PPC64LE) |
| 149 | // OPENSSL_cpuid_setup initializes the platform-specific feature cache. |
| 150 | void OPENSSL_cpuid_setup(void); |
| 151 | #endif |
| 152 |
| 153 |
| 154 | #if (!defined(_MSC_VER) || defined(__clang__)) && defined(OPENSSL_64_BIT) |
| 155 | #define BORINGSSL_HAS_UINT128 |
| 156 | typedef __int128_t int128_t; |
| 157 | typedef __uint128_t uint128_t; |
| 158 |
| 159 | // clang-cl supports __uint128_t but modulus and division don't work. |
| 160 | // https://crbug.com/787617. |
| 161 | #if !defined(_MSC_VER) || !defined(__clang__) |
| 162 | #define BORINGSSL_CAN_DIVIDE_UINT128 |
| 163 | #endif |
| 164 | #endif |
| 165 |
| 166 | #define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) |
| 167 |
| 168 | // buffers_alias returns one if |a| and |b| alias and zero otherwise. |
| 169 | static inline int buffers_alias(const uint8_t *a, size_t a_len, |
| 170 | const uint8_t *b, size_t b_len) { |
| 171 | // Cast |a| and |b| to integers. In C, pointer comparisons between unrelated |
| 172 | // objects are undefined whereas pointer to integer conversions are merely |
| 173 | // implementation-defined. We assume the implementation defined it in a sane |
| 174 | // way. |
| 175 | uintptr_t a_u = (uintptr_t)a; |
| 176 | uintptr_t b_u = (uintptr_t)b; |
| 177 | return a_u + a_len > b_u && b_u + b_len > a_u; |
| 178 | } |
| 179 |
| 180 |
| 181 | // Constant-time utility functions. |
| 182 | // |
| 183 | // The following methods return a bitmask of all ones (0xff...f) for true and 0 |
| 184 | // for false. This is useful for choosing a value based on the result of a |
| 185 | // conditional in constant time. For example, |
| 186 | // |
| 187 | // if (a < b) { |
| 188 | // c = a; |
| 189 | // } else { |
| 190 | // c = b; |
| 191 | // } |
| 192 | // |
| 193 | // can be written as |
| 194 | // |
| 195 | // crypto_word_t lt = constant_time_lt_w(a, b); |
| 196 | // c = constant_time_select_w(lt, a, b); |
| 197 |
| 198 | // crypto_word_t is the type that most constant-time functions use. Ideally we |
| 199 | // would like it to be |size_t|, but NaCl builds in 64-bit mode with 32-bit |
| 200 | // pointers, which means that |size_t| can be 32 bits when |BN_ULONG| is 64 |
| 201 | // bits. Since we want to be able to do constant-time operations on a |
| 202 | // |BN_ULONG|, |crypto_word_t| is defined as an unsigned value with the native |
| 203 | // word length. |
| 204 | #if defined(OPENSSL_64_BIT) |
| 205 | typedef uint64_t crypto_word_t; |
| 206 | #elif defined(OPENSSL_32_BIT) |
| 207 | typedef uint32_t crypto_word_t; |
| 208 | #else |
| 209 | #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT" |
| 210 | #endif |
| 211 |
| 212 | #define CONSTTIME_TRUE_W ~((crypto_word_t)0) |
| 213 | #define CONSTTIME_FALSE_W ((crypto_word_t)0) |
| 214 | #define CONSTTIME_TRUE_8 ((uint8_t)0xff) |
| 215 |
| 216 | #define CONSTTIME_TRUE_W ~((crypto_word_t)0) |
| 217 | #define CONSTTIME_FALSE_W ((crypto_word_t)0) |
| 218 | #define CONSTTIME_TRUE_8 ((uint8_t)0xff) |
| 219 | #define CONSTTIME_FALSE_8 ((uint8_t)0) |
| 220 |
| 221 | // constant_time_msb_w returns the given value with the MSB copied to all the |
| 222 | // other bits. |
| 223 | static inline crypto_word_t constant_time_msb_w(crypto_word_t a) { |
| 224 | return 0u - (a >> (sizeof(a) * 8 - 1)); |
| 225 | } |
| 226 |
| 227 | // constant_time_lt_w returns 0xff..f if a < b and 0 otherwise. |
| 228 | static inline crypto_word_t constant_time_lt_w(crypto_word_t a, |
| 229 | crypto_word_t b) { |
| 230 | // Consider the two cases of the problem: |
| 231 | // msb(a) == msb(b): a < b iff the MSB of a - b is set. |
| 232 | // msb(a) != msb(b): a < b iff the MSB of b is set. |
| 233 | // |
| 234 | // If msb(a) == msb(b) then the following evaluates as: |
| 235 | // msb(a^((a^b)|((a-b)^a))) == |
| 236 | // msb(a^((a-b) ^ a)) == (because msb(a^b) == 0) |
| 237 | // msb(a^a^(a-b)) == (rearranging) |
| 238 | // msb(a-b) (because ∀x. x^x == 0) |
| 239 | // |
| 240 | // Else, if msb(a) != msb(b) then the following evaluates as: |
| 241 | // msb(a^((a^b)|((a-b)^a))) == |
| 242 | // msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙 |
| 243 | // represents a value s.t. msb(𝟙) = 1) |
| 244 | // msb(a^𝟙) == (because ORing with 1 results in 1) |
| 245 | // msb(b) |
| 246 | // |
| 247 | // |
| 248 | // Here is an SMT-LIB verification of this formula: |
| 249 | // |
| 250 | // (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32) |
| 251 | // (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a))) |
| 252 | // ) |
| 253 | // |
| 254 | // (declare-fun a () (_ BitVec 32)) |
| 255 | // (declare-fun b () (_ BitVec 32)) |
| 256 | // |
| 257 | // (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b)))) |
| 258 | // (check-sat) |
| 259 | // (get-model) |
| 260 | return constant_time_msb_w(a^((a^b)|((a-b)^a))); |
| 261 | } |
| 262 |
| 263 | // constant_time_lt_8 acts like |constant_time_lt_w| but returns an 8-bit |
| 264 | // mask. |
| 265 | static inline uint8_t constant_time_lt_8(crypto_word_t a, crypto_word_t b) { |
| 266 | return (uint8_t)(constant_time_lt_w(a, b)); |
| 267 | } |
| 268 |
| 269 | // constant_time_ge_w returns 0xff..f if a >= b and 0 otherwise. |
| 270 | static inline crypto_word_t constant_time_ge_w(crypto_word_t a, |
| 271 | crypto_word_t b) { |
| 272 | return ~constant_time_lt_w(a, b); |
| 273 | } |
| 274 |
| 275 | // constant_time_ge_8 acts like |constant_time_ge_w| but returns an 8-bit |
| 276 | // mask. |
| 277 | static inline uint8_t constant_time_ge_8(crypto_word_t a, crypto_word_t b) { |
| 278 | return (uint8_t)(constant_time_ge_w(a, b)); |
| 279 | } |
| 280 |
| 281 | // constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. |
| 282 | static inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) { |
| 283 | // Here is an SMT-LIB verification of this formula: |
| 284 | // |
| 285 | // (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32) |
| 286 | // (bvand (bvnot a) (bvsub a #x00000001)) |
| 287 | // ) |
| 288 | // |
| 289 | // (declare-fun a () (_ BitVec 32)) |
| 290 | // |
| 291 | // (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000)))) |
| 292 | // (check-sat) |
| 293 | // (get-model) |
| 294 | return constant_time_msb_w(~a & (a - 1)); |
| 295 | } |
| 296 |
| 297 | // constant_time_is_zero_8 acts like |constant_time_is_zero_w| but returns an |
| 298 | // 8-bit mask. |
| 299 | static inline uint8_t constant_time_is_zero_8(crypto_word_t a) { |
| 300 | return (uint8_t)(constant_time_is_zero_w(a)); |
| 301 | } |
| 302 |
| 303 | // constant_time_eq_w returns 0xff..f if a == b and 0 otherwise. |
| 304 | static inline crypto_word_t constant_time_eq_w(crypto_word_t a, |
| 305 | crypto_word_t b) { |
| 306 | return constant_time_is_zero_w(a ^ b); |
| 307 | } |
| 308 |
| 309 | // constant_time_eq_8 acts like |constant_time_eq_w| but returns an 8-bit |
| 310 | // mask. |
| 311 | static inline uint8_t constant_time_eq_8(crypto_word_t a, crypto_word_t b) { |
| 312 | return (uint8_t)(constant_time_eq_w(a, b)); |
| 313 | } |
| 314 |
| 315 | // constant_time_eq_int acts like |constant_time_eq_w| but works on int |
| 316 | // values. |
| 317 | static inline crypto_word_t constant_time_eq_int(int a, int b) { |
| 318 | return constant_time_eq_w((crypto_word_t)(a), (crypto_word_t)(b)); |
| 319 | } |
| 320 |
| 321 | // constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit |
| 322 | // mask. |
| 323 | static inline uint8_t constant_time_eq_int_8(int a, int b) { |
| 324 | return constant_time_eq_8((crypto_word_t)(a), (crypto_word_t)(b)); |
| 325 | } |
| 326 |
| 327 | // constant_time_select_w returns (mask & a) | (~mask & b). When |mask| is all |
| 328 | // 1s or all 0s (as returned by the methods above), the select methods return |
| 329 | // either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). |
| 330 | static inline crypto_word_t constant_time_select_w(crypto_word_t mask, |
| 331 | crypto_word_t a, |
| 332 | crypto_word_t b) { |
| 333 | return (mask & a) | (~mask & b); |
| 334 | } |
| 335 |
| 336 | // constant_time_select_8 acts like |constant_time_select| but operates on |
| 337 | // 8-bit values. |
| 338 | static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a, |
| 339 | uint8_t b) { |
| 340 | return (uint8_t)(constant_time_select_w(mask, a, b)); |
| 341 | } |
| 342 |
| 343 | // constant_time_select_int acts like |constant_time_select| but operates on |
| 344 | // ints. |
| 345 | static inline int constant_time_select_int(crypto_word_t mask, int a, int b) { |
| 346 | return (int)(constant_time_select_w(mask, (crypto_word_t)(a), |
| 347 | (crypto_word_t)(b))); |
| 348 | } |
| 349 |
| 350 |
| 351 | // Thread-safe initialisation. |
| 352 |
| 353 | #if defined(OPENSSL_NO_THREADS) |
| 354 | typedef uint32_t CRYPTO_once_t; |
| 355 | #define CRYPTO_ONCE_INIT 0 |
| 356 | #elif defined(OPENSSL_WINDOWS_THREADS) |
| 357 | typedef INIT_ONCE CRYPTO_once_t; |
| 358 | #define CRYPTO_ONCE_INIT INIT_ONCE_STATIC_INIT |
| 359 | #elif defined(OPENSSL_PTHREADS) |
| 360 | typedef pthread_once_t CRYPTO_once_t; |
| 361 | #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT |
| 362 | #else |
| 363 | #error "Unknown threading library" |
| 364 | #endif |
| 365 |
| 366 | // CRYPTO_once calls |init| exactly once per process. This is thread-safe: if |
| 367 | // concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument |
| 368 | // then they will block until |init| completes, but |init| will have only been |
| 369 | // called once. |
| 370 | // |
| 371 | // The |once| argument must be a |CRYPTO_once_t| that has been initialised with |
| 372 | // the value |CRYPTO_ONCE_INIT|. |
| 373 | OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)); |
| 374 |
| 375 |
| 376 | // Reference counting. |
| 377 |
| 378 | // CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates. |
| 379 | #define CRYPTO_REFCOUNT_MAX 0xffffffff |
| 380 |
| 381 | // CRYPTO_refcount_inc atomically increments the value at |*count| unless the |
| 382 | // value would overflow. It's safe for multiple threads to concurrently call |
| 383 | // this or |CRYPTO_refcount_dec_and_test_zero| on the same |
| 384 | // |CRYPTO_refcount_t|. |
| 385 | OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count); |
| 386 |
| 387 | // CRYPTO_refcount_dec_and_test_zero tests the value at |*count|: |
| 388 | // if it's zero, it crashes the address space. |
| 389 | // if it's the maximum value, it returns zero. |
| 390 | // otherwise, it atomically decrements it and returns one iff the resulting |
| 391 | // value is zero. |
| 392 | // |
| 393 | // It's safe for multiple threads to concurrently call this or |
| 394 | // |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. |
| 395 | OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count); |
| 396 |
| 397 |
| 398 | // Locks. |
| 399 | // |
| 400 | // Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in |
| 401 | // structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as |
| 402 | // a global lock. A global lock must be initialised to the value |
| 403 | // |CRYPTO_STATIC_MUTEX_INIT|. |
| 404 | // |
| 405 | // |CRYPTO_MUTEX| can appear in public structures and so is defined in |
| 406 | // thread.h as a structure large enough to fit the real type. The global lock is |
| 407 | // a different type so it may be initialized with platform initializer macros. |
| 408 |
| 409 | #if defined(OPENSSL_NO_THREADS) |
| 410 | struct CRYPTO_STATIC_MUTEX { |
| 411 | char padding; // Empty structs have different sizes in C and C++. |
| 412 | }; |
| 413 | #define CRYPTO_STATIC_MUTEX_INIT { 0 } |
| 414 | #elif defined(OPENSSL_WINDOWS_THREADS) |
| 415 | struct CRYPTO_STATIC_MUTEX { |
| 416 | SRWLOCK lock; |
| 417 | }; |
| 418 | #define CRYPTO_STATIC_MUTEX_INIT { SRWLOCK_INIT } |
| 419 | #elif defined(OPENSSL_PTHREADS) |
| 420 | struct CRYPTO_STATIC_MUTEX { |
| 421 | pthread_rwlock_t lock; |
| 422 | }; |
| 423 | #define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER } |
| 424 | #else |
| 425 | #error "Unknown threading library" |
| 426 | #endif |
| 427 |
| 428 | // CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a |
| 429 | // |CRYPTO_STATIC_MUTEX|. |
| 430 | OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock); |
| 431 |
| 432 | // CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a |
| 433 | // read lock, but none may have a write lock. |
| 434 | OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock); |
| 435 |
| 436 | // CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type |
| 437 | // of lock on it. |
| 438 | OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock); |
| 439 |
| 440 | // CRYPTO_MUTEX_unlock_read unlocks |lock| for reading. |
| 441 | OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock); |
| 442 |
| 443 | // CRYPTO_MUTEX_unlock_write unlocks |lock| for writing. |
| 444 | OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock); |
| 445 |
| 446 | // CRYPTO_MUTEX_cleanup releases all resources held by |lock|. |
| 447 | OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock); |
| 448 |
| 449 | // CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also |
| 450 | // have a read lock, but none may have a write lock. The |lock| variable does |
| 451 | // not need to be initialised by any function, but must have been statically |
| 452 | // initialised with |CRYPTO_STATIC_MUTEX_INIT|. |
| 453 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_read( |
| 454 | struct CRYPTO_STATIC_MUTEX *lock); |
| 455 |
| 456 | // CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has |
| 457 | // any type of lock on it. The |lock| variable does not need to be initialised |
| 458 | // by any function, but must have been statically initialised with |
| 459 | // |CRYPTO_STATIC_MUTEX_INIT|. |
| 460 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_write( |
| 461 | struct CRYPTO_STATIC_MUTEX *lock); |
| 462 |
| 463 | // CRYPTO_STATIC_MUTEX_unlock_read unlocks |lock| for reading. |
| 464 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_read( |
| 465 | struct CRYPTO_STATIC_MUTEX *lock); |
| 466 |
| 467 | // CRYPTO_STATIC_MUTEX_unlock_write unlocks |lock| for writing. |
| 468 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_write( |
| 469 | struct CRYPTO_STATIC_MUTEX *lock); |
| 470 |
| 471 | #if defined(__cplusplus) |
| 472 | extern "C++" { |
| 473 |
| 474 | namespace bssl { |
| 475 |
| 476 | namespace internal { |
| 477 |
| 478 | // MutexLockBase is a RAII helper for CRYPTO_MUTEX locking. |
| 479 | template <void (*LockFunc)(CRYPTO_MUTEX *), void (*ReleaseFunc)(CRYPTO_MUTEX *)> |
| 480 | class MutexLockBase { |
| 481 | public: |
| 482 | explicit MutexLockBase(CRYPTO_MUTEX *mu) : mu_(mu) { |
| 483 | assert(mu_ != nullptr); |
| 484 | LockFunc(mu_); |
| 485 | } |
| 486 | ~MutexLockBase() { ReleaseFunc(mu_); } |
| 487 | MutexLockBase(const MutexLockBase<LockFunc, ReleaseFunc> &) = delete; |
| 488 | MutexLockBase &operator=(const MutexLockBase<LockFunc, ReleaseFunc> &) = |
| 489 | delete; |
| 490 |
| 491 | private: |
| 492 | CRYPTO_MUTEX *const mu_; |
| 493 | }; |
| 494 |
| 495 | } // namespace internal |
| 496 |
| 497 | using MutexWriteLock = |
| 498 | internal::MutexLockBase<CRYPTO_MUTEX_lock_write, CRYPTO_MUTEX_unlock_write>; |
| 499 | using MutexReadLock = |
| 500 | internal::MutexLockBase<CRYPTO_MUTEX_lock_read, CRYPTO_MUTEX_unlock_read>; |
| 501 |
| 502 | } // namespace bssl |
| 503 |
| 504 | } // extern "C++" |
| 505 | #endif // defined(__cplusplus) |
| 506 |
| 507 |
| 508 | // Thread local storage. |
| 509 |
| 510 | // thread_local_data_t enumerates the types of thread-local data that can be |
| 511 | // stored. |
| 512 | typedef enum { |
| 513 | OPENSSL_THREAD_LOCAL_ERR = 0, |
| 514 | OPENSSL_THREAD_LOCAL_RAND, |
| 515 | OPENSSL_THREAD_LOCAL_TEST, |
| 516 | NUM_OPENSSL_THREAD_LOCALS, |
| 517 | } thread_local_data_t; |
| 518 |
| 519 | // thread_local_destructor_t is the type of a destructor function that will be |
| 520 | // called when a thread exits and its thread-local storage needs to be freed. |
| 521 | typedef void (*thread_local_destructor_t)(void *); |
| 522 |
| 523 | // CRYPTO_get_thread_local gets the pointer value that is stored for the |
| 524 | // current thread for the given index, or NULL if none has been set. |
| 525 | OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value); |
| 526 |
| 527 | // CRYPTO_set_thread_local sets a pointer value for the current thread at the |
| 528 | // given index. This function should only be called once per thread for a given |
| 529 | // |index|: rather than update the pointer value itself, update the data that |
| 530 | // is pointed to. |
| 531 | // |
| 532 | // The destructor function will be called when a thread exits to free this |
| 533 | // thread-local data. All calls to |CRYPTO_set_thread_local| with the same |
| 534 | // |index| should have the same |destructor| argument. The destructor may be |
| 535 | // called with a NULL argument if a thread that never set a thread-local |
| 536 | // pointer for |index|, exits. The destructor may be called concurrently with |
| 537 | // different arguments. |
| 538 | // |
| 539 | // This function returns one on success or zero on error. If it returns zero |
| 540 | // then |destructor| has been called with |value| already. |
| 541 | OPENSSL_EXPORT int CRYPTO_set_thread_local( |
| 542 | thread_local_data_t index, void *value, |
| 543 | thread_local_destructor_t destructor); |
| 544 |
| 545 |
| 546 | // ex_data |
| 547 |
| 548 | typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS; |
| 549 |
| 550 | DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS) |
| 551 |
| 552 | // CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which |
| 553 | // supports ex_data. It should defined as a static global within the module |
| 554 | // which defines that type. |
| 555 | typedef struct { |
| 556 | struct CRYPTO_STATIC_MUTEX lock; |
| 557 | STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth; |
| 558 | // num_reserved is one if the ex_data index zero is reserved for legacy |
| 559 | // |TYPE_get_app_data| functions. |
| 560 | uint8_t num_reserved; |
| 561 | } CRYPTO_EX_DATA_CLASS; |
| 562 |
| 563 | #define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL, 0} |
| 564 | #define CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA \ |
| 565 | {CRYPTO_STATIC_MUTEX_INIT, NULL, 1} |
| 566 |
| 567 | // CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes |
| 568 | // it to |*out_index|. Each class of object should provide a wrapper function |
| 569 | // that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and |
| 570 | // zero otherwise. |
| 571 | OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class, |
| 572 | int *out_index, long argl, |
| 573 | void *argp, |
| 574 | CRYPTO_EX_free *free_func); |
| 575 |
| 576 | // CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class |
| 577 | // of object should provide a wrapper function. |
| 578 | OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val); |
| 579 |
| 580 | // CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL |
| 581 | // if no such index exists. Each class of object should provide a wrapper |
| 582 | // function. |
| 583 | OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index); |
| 584 |
| 585 | // CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|. |
| 586 | OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad); |
| 587 |
| 588 | // CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an |
| 589 | // object of the given class. |
| 590 | OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class, |
| 591 | void *obj, CRYPTO_EX_DATA *ad); |
| 592 |
| 593 |
| 594 | // Endianness conversions. |
| 595 |
| 596 | #if defined(__GNUC__) && __GNUC__ >= 2 |
| 597 | static inline uint32_t CRYPTO_bswap4(uint32_t x) { |
| 598 | return __builtin_bswap32(x); |
| 599 | } |
| 600 |
| 601 | static inline uint64_t CRYPTO_bswap8(uint64_t x) { |
| 602 | return __builtin_bswap64(x); |
| 603 | } |
| 604 | #elif defined(_MSC_VER) |
| 605 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 606 | #include <intrin.h> |
| 607 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 608 | #pragma intrinsic(_byteswap_uint64, _byteswap_ulong) |
| 609 | static inline uint32_t CRYPTO_bswap4(uint32_t x) { |
| 610 | return _byteswap_ulong(x); |
| 611 | } |
| 612 |
| 613 | static inline uint64_t CRYPTO_bswap8(uint64_t x) { |
| 614 | return _byteswap_uint64(x); |
| 615 | } |
| 616 | #else |
| 617 | static inline uint32_t CRYPTO_bswap4(uint32_t x) { |
| 618 | x = (x >> 16) | (x << 16); |
| 619 | x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8); |
| 620 | return x; |
| 621 | } |
| 622 |
| 623 | static inline uint64_t CRYPTO_bswap8(uint64_t x) { |
| 624 | return CRYPTO_bswap4(x >> 32) | (((uint64_t)CRYPTO_bswap4(x)) << 32); |
| 625 | } |
| 626 | #endif |
| 627 |
| 628 |
| 629 | // Language bug workarounds. |
| 630 | // |
| 631 | // Most C standard library functions are undefined if passed NULL, even when the |
| 632 | // corresponding length is zero. This gives them (and, in turn, all functions |
| 633 | // which call them) surprising behavior on empty arrays. Some compilers will |
| 634 | // miscompile code due to this rule. See also |
| 635 | // https://www.imperialviolet.org/2016/06/26/nonnull.html |
| 636 | // |
| 637 | // These wrapper functions behave the same as the corresponding C standard |
| 638 | // functions, but behave as expected when passed NULL if the length is zero. |
| 639 | // |
| 640 | // Note |OPENSSL_memcmp| is a different function from |CRYPTO_memcmp|. |
| 641 |
| 642 | // C++ defines |memchr| as a const-correct overload. |
| 643 | #if defined(__cplusplus) |
| 644 | extern "C++" { |
| 645 |
| 646 | static inline const void *OPENSSL_memchr(const void *s, int c, size_t n) { |
| 647 | if (n == 0) { |
| 648 | return NULL; |
| 649 | } |
| 650 |
| 651 | return memchr(s, c, n); |
| 652 | } |
| 653 |
| 654 | static inline void *OPENSSL_memchr(void *s, int c, size_t n) { |
| 655 | if (n == 0) { |
| 656 | return NULL; |
| 657 | } |
| 658 |
| 659 | return memchr(s, c, n); |
| 660 | } |
| 661 |
| 662 | } // extern "C++" |
| 663 | #else // __cplusplus |
| 664 |
| 665 | static inline void *OPENSSL_memchr(const void *s, int c, size_t n) { |
| 666 | if (n == 0) { |
| 667 | return NULL; |
| 668 | } |
| 669 |
| 670 | return memchr(s, c, n); |
| 671 | } |
| 672 |
| 673 | #endif // __cplusplus |
| 674 |
| 675 | static inline int OPENSSL_memcmp(const void *s1, const void *s2, size_t n) { |
| 676 | if (n == 0) { |
| 677 | return 0; |
| 678 | } |
| 679 |
| 680 | return memcmp(s1, s2, n); |
| 681 | } |
| 682 |
| 683 | static inline void *OPENSSL_memcpy(void *dst, const void *src, size_t n) { |
| 684 | if (n == 0) { |
| 685 | return dst; |
| 686 | } |
| 687 |
| 688 | return memcpy(dst, src, n); |
| 689 | } |
| 690 |
| 691 | static inline void *OPENSSL_memmove(void *dst, const void *src, size_t n) { |
| 692 | if (n == 0) { |
| 693 | return dst; |
| 694 | } |
| 695 |
| 696 | return memmove(dst, src, n); |
| 697 | } |
| 698 |
| 699 | static inline void *OPENSSL_memset(void *dst, int c, size_t n) { |
| 700 | if (n == 0) { |
| 701 | return dst; |
| 702 | } |
| 703 |
| 704 | return memset(dst, c, n); |
| 705 | } |
| 706 |
| 707 | #if defined(BORINGSSL_FIPS) |
| 708 | // BORINGSSL_FIPS_abort is called when a FIPS power-on or continuous test |
| 709 | // fails. It prevents any further cryptographic operations by the current |
| 710 | // process. |
| 711 | void BORINGSSL_FIPS_abort(void) __attribute__((noreturn)); |
| 712 | #endif |
| 713 |
| 714 | #if defined(__cplusplus) |
| 715 | } // extern C |
| 716 | #endif |
| 717 |
| 718 | #endif // OPENSSL_HEADER_CRYPTO_INTERNAL_H |
| 719 |