Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
| 109 | #ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H |
| 110 | #define OPENSSL_HEADER_CRYPTO_INTERNAL_H |
| 111 | |
Adam Langley | 98ad22e | 2014-08-26 13:38:19 -0700 | [diff] [blame] | 112 | #include <openssl/ex_data.h> |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 113 | #include <openssl/thread.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | |
Brian Smith | 7cae9f5 | 2016-01-17 20:12:57 -1000 | [diff] [blame] | 115 | #if defined(_MSC_VER) |
| 116 | #if !defined(__cplusplus) || _MSC_VER < 1900 |
| 117 | #define alignas(x) __declspec(align(x)) |
| 118 | #define alignof __alignof |
| 119 | #endif |
| 120 | #else |
| 121 | #include <stdalign.h> |
| 122 | #endif |
| 123 | |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 124 | #if defined(OPENSSL_NO_THREADS) |
| 125 | #elif defined(OPENSSL_WINDOWS) |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 126 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 127 | #include <windows.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 128 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 129 | #else |
| 130 | #include <pthread.h> |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 131 | #endif |
| 132 | |
Adam Langley | b2cb0ec | 2014-09-02 14:28:49 -0700 | [diff] [blame] | 133 | #if defined(__cplusplus) |
| 134 | extern "C" { |
| 135 | #endif |
| 136 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 138 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \ |
| 139 | defined(OPENSSL_AARCH64) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 140 | /* OPENSSL_cpuid_setup initializes OPENSSL_ia32cap_P. */ |
| 141 | void OPENSSL_cpuid_setup(void); |
| 142 | #endif |
Adam Langley | 98ad22e | 2014-08-26 13:38:19 -0700 | [diff] [blame] | 143 | |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 144 | |
David Benjamin | 647cd02 | 2016-01-27 17:26:48 -0500 | [diff] [blame] | 145 | #if !defined(_MSC_VER) && defined(OPENSSL_64_BIT) |
Brian Smith | 24e4288 | 2016-01-26 15:16:37 -1000 | [diff] [blame] | 146 | typedef __int128_t int128_t; |
| 147 | typedef __uint128_t uint128_t; |
| 148 | #endif |
| 149 | |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame^] | 150 | #define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) |
Brian Smith | 24e4288 | 2016-01-26 15:16:37 -1000 | [diff] [blame] | 151 | |
David Benjamin | 1a01e1f | 2016-06-08 18:31:24 -0400 | [diff] [blame] | 152 | /* buffers_alias returns one if |a| and |b| alias and zero otherwise. */ |
| 153 | static inline int buffers_alias(const uint8_t *a, size_t a_len, |
| 154 | const uint8_t *b, size_t b_len) { |
| 155 | /* Cast |a| and |b| to integers. In C, pointer comparisons between unrelated |
| 156 | * objects are undefined whereas pointer to integer conversions are merely |
| 157 | * implementation-defined. We assume the implementation defined it in a sane |
| 158 | * way. */ |
| 159 | uintptr_t a_u = (uintptr_t)a; |
| 160 | uintptr_t b_u = (uintptr_t)b; |
| 161 | return a_u + a_len > b_u && b_u + b_len > a_u; |
| 162 | } |
| 163 | |
| 164 | |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 165 | /* Constant-time utility functions. |
| 166 | * |
| 167 | * The following methods return a bitmask of all ones (0xff...f) for true and 0 |
| 168 | * for false. This is useful for choosing a value based on the result of a |
| 169 | * conditional in constant time. For example, |
| 170 | * |
| 171 | * if (a < b) { |
| 172 | * c = a; |
| 173 | * } else { |
| 174 | * c = b; |
| 175 | * } |
| 176 | * |
| 177 | * can be written as |
| 178 | * |
| 179 | * unsigned int lt = constant_time_lt(a, b); |
Adam Langley | a952d96 | 2014-11-03 19:15:40 -0800 | [diff] [blame] | 180 | * c = constant_time_select(lt, a, b); */ |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 181 | |
| 182 | /* constant_time_msb returns the given value with the MSB copied to all the |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 183 | * other bits. */ |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 184 | static inline unsigned int constant_time_msb(unsigned int a) { |
| 185 | return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1)); |
| 186 | } |
| 187 | |
| 188 | /* constant_time_lt returns 0xff..f if a < b and 0 otherwise. */ |
| 189 | static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) { |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 190 | /* Consider the two cases of the problem: |
| 191 | * msb(a) == msb(b): a < b iff the MSB of a - b is set. |
| 192 | * msb(a) != msb(b): a < b iff the MSB of b is set. |
| 193 | * |
| 194 | * If msb(a) == msb(b) then the following evaluates as: |
| 195 | * msb(a^((a^b)|((a-b)^a))) == |
| 196 | * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0) |
| 197 | * msb(a^a^(a-b)) == (rearranging) |
| 198 | * msb(a-b) (because ∀x. x^x == 0) |
| 199 | * |
| 200 | * Else, if msb(a) != msb(b) then the following evaluates as: |
| 201 | * msb(a^((a^b)|((a-b)^a))) == |
| 202 | * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙 |
| 203 | * represents a value s.t. msb(𝟙) = 1) |
| 204 | * msb(a^𝟙) == (because ORing with 1 results in 1) |
| 205 | * msb(b) |
| 206 | * |
| 207 | * |
| 208 | * Here is an SMT-LIB verification of this formula: |
| 209 | * |
| 210 | * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32) |
| 211 | * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a))) |
| 212 | * ) |
| 213 | * |
| 214 | * (declare-fun a () (_ BitVec 32)) |
| 215 | * (declare-fun b () (_ BitVec 32)) |
| 216 | * |
| 217 | * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b)))) |
| 218 | * (check-sat) |
| 219 | * (get-model) |
| 220 | */ |
| 221 | return constant_time_msb(a^((a^b)|((a-b)^a))); |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */ |
| 225 | static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) { |
| 226 | return (uint8_t)(constant_time_lt(a, b)); |
| 227 | } |
| 228 | |
| 229 | /* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */ |
| 230 | static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) { |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 231 | return ~constant_time_lt(a, b); |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */ |
| 235 | static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) { |
| 236 | return (uint8_t)(constant_time_ge(a, b)); |
| 237 | } |
| 238 | |
| 239 | /* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */ |
| 240 | static inline unsigned int constant_time_is_zero(unsigned int a) { |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 241 | /* Here is an SMT-LIB verification of this formula: |
| 242 | * |
| 243 | * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32) |
| 244 | * (bvand (bvnot a) (bvsub a #x00000001)) |
| 245 | * ) |
| 246 | * |
| 247 | * (declare-fun a () (_ BitVec 32)) |
| 248 | * |
| 249 | * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000)))) |
| 250 | * (check-sat) |
| 251 | * (get-model) |
| 252 | */ |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 253 | return constant_time_msb(~a & (a - 1)); |
| 254 | } |
| 255 | |
| 256 | /* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit |
| 257 | * mask. */ |
| 258 | static inline uint8_t constant_time_is_zero_8(unsigned int a) { |
| 259 | return (uint8_t)(constant_time_is_zero(a)); |
| 260 | } |
| 261 | |
| 262 | /* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */ |
| 263 | static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) { |
| 264 | return constant_time_is_zero(a ^ b); |
| 265 | } |
| 266 | |
Adam Langley | af6e45b | 2014-11-03 19:34:49 -0800 | [diff] [blame] | 267 | /* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */ |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 268 | static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) { |
| 269 | return (uint8_t)(constant_time_eq(a, b)); |
| 270 | } |
| 271 | |
Adam Langley | af6e45b | 2014-11-03 19:34:49 -0800 | [diff] [blame] | 272 | /* constant_time_eq_int acts like |constant_time_eq| but works on int values. */ |
| 273 | static inline unsigned int constant_time_eq_int(int a, int b) { |
| 274 | return constant_time_eq((unsigned)(a), (unsigned)(b)); |
| 275 | } |
| 276 | |
| 277 | /* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit |
| 278 | * mask. */ |
| 279 | static inline uint8_t constant_time_eq_int_8(int a, int b) { |
| 280 | return constant_time_eq_8((unsigned)(a), (unsigned)(b)); |
| 281 | } |
| 282 | |
Adam Langley | a952d96 | 2014-11-03 19:15:40 -0800 | [diff] [blame] | 283 | /* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s |
| 284 | * or all 0s (as returned by the methods above), the select methods return |
| 285 | * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */ |
| 286 | static inline unsigned int constant_time_select(unsigned int mask, |
| 287 | unsigned int a, unsigned int b) { |
| 288 | return (mask & a) | (~mask & b); |
| 289 | } |
| 290 | |
| 291 | /* constant_time_select_8 acts like |constant_time_select| but operates on |
| 292 | * 8-bit values. */ |
| 293 | static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a, |
| 294 | uint8_t b) { |
| 295 | return (uint8_t)(constant_time_select(mask, a, b)); |
| 296 | } |
| 297 | |
| 298 | /* constant_time_select_int acts like |constant_time_select| but operates on |
| 299 | * ints. */ |
| 300 | static inline int constant_time_select_int(unsigned int mask, int a, int b) { |
| 301 | return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b))); |
| 302 | } |
| 303 | |
Adam Langley | b15d813 | 2014-11-03 18:51:20 -0800 | [diff] [blame] | 304 | |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 305 | /* Thread-safe initialisation. */ |
| 306 | |
David Benjamin | a9959f2 | 2016-04-26 20:12:52 -0400 | [diff] [blame] | 307 | /* Android's mingw-w64 has some prototypes for INIT_ONCE, but is missing |
| 308 | * others. Work around the missing ones. |
| 309 | * |
| 310 | * TODO(davidben): Remove this once Android's mingw-w64 is upgraded. See |
| 311 | * b/26523949. */ |
| 312 | #if defined(__MINGW32__) && !defined(INIT_ONCE_STATIC_INIT) |
| 313 | typedef RTL_RUN_ONCE INIT_ONCE; |
| 314 | #define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT |
| 315 | #endif |
| 316 | |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 317 | #if defined(OPENSSL_NO_THREADS) |
| 318 | typedef uint32_t CRYPTO_once_t; |
| 319 | #define CRYPTO_ONCE_INIT 0 |
| 320 | #elif defined(OPENSSL_WINDOWS) |
David Benjamin | 9dadc3b | 2016-03-30 19:04:28 -0400 | [diff] [blame] | 321 | typedef INIT_ONCE CRYPTO_once_t; |
| 322 | #define CRYPTO_ONCE_INIT INIT_ONCE_STATIC_INIT |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 323 | #else |
| 324 | typedef pthread_once_t CRYPTO_once_t; |
| 325 | #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 326 | #endif |
| 327 | |
| 328 | /* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if |
| 329 | * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument |
| 330 | * then they will block until |init| completes, but |init| will have only been |
| 331 | * called once. |
| 332 | * |
| 333 | * The |once| argument must be a |CRYPTO_once_t| that has been initialised with |
| 334 | * the value |CRYPTO_ONCE_INIT|. */ |
David Benjamin | 4d78718 | 2015-04-02 16:34:42 -0400 | [diff] [blame] | 335 | OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)); |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 336 | |
| 337 | |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 338 | /* Reference counting. */ |
| 339 | |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 340 | /* CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates. */ |
| 341 | #define CRYPTO_REFCOUNT_MAX 0xffffffff |
| 342 | |
| 343 | /* CRYPTO_refcount_inc atomically increments the value at |*count| unless the |
| 344 | * value would overflow. It's safe for multiple threads to concurrently call |
| 345 | * this or |CRYPTO_refcount_dec_and_test_zero| on the same |
| 346 | * |CRYPTO_refcount_t|. */ |
| 347 | OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count); |
| 348 | |
| 349 | /* CRYPTO_refcount_dec_and_test_zero tests the value at |*count|: |
| 350 | * if it's zero, it crashes the address space. |
| 351 | * if it's the maximum value, it returns zero. |
| 352 | * otherwise, it atomically decrements it and returns one iff the resulting |
| 353 | * value is zero. |
| 354 | * |
| 355 | * It's safe for multiple threads to concurrently call this or |
| 356 | * |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. */ |
| 357 | OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count); |
| 358 | |
| 359 | |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 360 | /* Locks. |
| 361 | * |
| 362 | * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in |
| 363 | * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as |
| 364 | * a global lock. A global lock must be initialised to the value |
| 365 | * |CRYPTO_STATIC_MUTEX_INIT|. |
| 366 | * |
| 367 | * |CRYPTO_MUTEX| can appear in public structures and so is defined in |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 368 | * thread.h as a structure large enough to fit the real type. The global lock is |
| 369 | * a different type so it may be initialized with platform initializer macros.*/ |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 370 | |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 371 | #if defined(OPENSSL_NO_THREADS) |
David Benjamin | c25d2e6 | 2016-04-26 18:04:36 -0400 | [diff] [blame] | 372 | struct CRYPTO_STATIC_MUTEX { |
| 373 | char padding; /* Empty structs have different sizes in C and C++. */ |
| 374 | }; |
David Benjamin | 992c20a | 2016-04-28 12:56:35 -0400 | [diff] [blame] | 375 | #define CRYPTO_STATIC_MUTEX_INIT { 0 } |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 376 | #elif defined(OPENSSL_WINDOWS) |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 377 | struct CRYPTO_STATIC_MUTEX { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 378 | SRWLOCK lock; |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 379 | }; |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 380 | #define CRYPTO_STATIC_MUTEX_INIT { SRWLOCK_INIT } |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 381 | #else |
| 382 | struct CRYPTO_STATIC_MUTEX { |
| 383 | pthread_rwlock_t lock; |
| 384 | }; |
| 385 | #define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER } |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 386 | #endif |
| 387 | |
| 388 | /* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a |
| 389 | * |CRYPTO_STATIC_MUTEX|. */ |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 390 | OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 391 | |
| 392 | /* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 393 | * read lock, but none may have a write lock. */ |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 394 | OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 395 | |
| 396 | /* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type |
| 397 | * of lock on it. */ |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 398 | OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 399 | |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 400 | /* CRYPTO_MUTEX_unlock_read unlocks |lock| for reading. */ |
| 401 | OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock); |
| 402 | |
| 403 | /* CRYPTO_MUTEX_unlock_write unlocks |lock| for writing. */ |
| 404 | OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 405 | |
| 406 | /* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */ |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 407 | OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 408 | |
| 409 | /* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also |
| 410 | * have a read lock, but none may have a write lock. The |lock| variable does |
| 411 | * not need to be initialised by any function, but must have been statically |
| 412 | * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */ |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 413 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_read( |
| 414 | struct CRYPTO_STATIC_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 415 | |
| 416 | /* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has |
| 417 | * any type of lock on it. The |lock| variable does not need to be initialised |
| 418 | * by any function, but must have been statically initialised with |
| 419 | * |CRYPTO_STATIC_MUTEX_INIT|. */ |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 420 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_write( |
| 421 | struct CRYPTO_STATIC_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 422 | |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 423 | /* CRYPTO_STATIC_MUTEX_unlock_read unlocks |lock| for reading. */ |
| 424 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_read( |
| 425 | struct CRYPTO_STATIC_MUTEX *lock); |
| 426 | |
| 427 | /* CRYPTO_STATIC_MUTEX_unlock_write unlocks |lock| for writing. */ |
| 428 | OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_write( |
Adam Langley | 04edcc8 | 2015-05-20 13:14:44 -0700 | [diff] [blame] | 429 | struct CRYPTO_STATIC_MUTEX *lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 430 | |
| 431 | |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 432 | /* Thread local storage. */ |
| 433 | |
| 434 | /* thread_local_data_t enumerates the types of thread-local data that can be |
| 435 | * stored. */ |
| 436 | typedef enum { |
| 437 | OPENSSL_THREAD_LOCAL_ERR = 0, |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 438 | OPENSSL_THREAD_LOCAL_RAND, |
Matt Braithwaite | cc2c7aa | 2015-09-03 14:18:08 -0700 | [diff] [blame] | 439 | OPENSSL_THREAD_LOCAL_URANDOM_BUF, |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 440 | OPENSSL_THREAD_LOCAL_TEST, |
| 441 | NUM_OPENSSL_THREAD_LOCALS, |
| 442 | } thread_local_data_t; |
| 443 | |
| 444 | /* thread_local_destructor_t is the type of a destructor function that will be |
| 445 | * called when a thread exits and its thread-local storage needs to be freed. */ |
| 446 | typedef void (*thread_local_destructor_t)(void *); |
| 447 | |
| 448 | /* CRYPTO_get_thread_local gets the pointer value that is stored for the |
| 449 | * current thread for the given index, or NULL if none has been set. */ |
David Benjamin | 4d78718 | 2015-04-02 16:34:42 -0400 | [diff] [blame] | 450 | OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value); |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 451 | |
| 452 | /* CRYPTO_set_thread_local sets a pointer value for the current thread at the |
| 453 | * given index. This function should only be called once per thread for a given |
| 454 | * |index|: rather than update the pointer value itself, update the data that |
| 455 | * is pointed to. |
| 456 | * |
| 457 | * The destructor function will be called when a thread exits to free this |
| 458 | * thread-local data. All calls to |CRYPTO_set_thread_local| with the same |
| 459 | * |index| should have the same |destructor| argument. The destructor may be |
| 460 | * called with a NULL argument if a thread that never set a thread-local |
| 461 | * pointer for |index|, exits. The destructor may be called concurrently with |
| 462 | * different arguments. |
| 463 | * |
| 464 | * This function returns one on success or zero on error. If it returns zero |
| 465 | * then |destructor| has been called with |value| already. */ |
David Benjamin | 4d78718 | 2015-04-02 16:34:42 -0400 | [diff] [blame] | 466 | OPENSSL_EXPORT int CRYPTO_set_thread_local( |
| 467 | thread_local_data_t index, void *value, |
| 468 | thread_local_destructor_t destructor); |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 469 | |
| 470 | |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 471 | /* ex_data */ |
| 472 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 473 | typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS; |
| 474 | |
| 475 | /* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which |
| 476 | * supports ex_data. It should defined as a static global within the module |
| 477 | * which defines that type. */ |
| 478 | typedef struct { |
| 479 | struct CRYPTO_STATIC_MUTEX lock; |
| 480 | STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth; |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 481 | /* num_reserved is one if the ex_data index zero is reserved for legacy |
| 482 | * |TYPE_get_app_data| functions. */ |
| 483 | uint8_t num_reserved; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 484 | } CRYPTO_EX_DATA_CLASS; |
| 485 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 486 | #define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL, 0} |
| 487 | #define CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA \ |
| 488 | {CRYPTO_STATIC_MUTEX_INIT, NULL, 1} |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 489 | |
| 490 | /* CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes |
| 491 | * it to |*out_index|. Each class of object should provide a wrapper function |
| 492 | * that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and |
| 493 | * zero otherwise. */ |
| 494 | OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class, |
| 495 | int *out_index, long argl, |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 496 | void *argp, CRYPTO_EX_dup *dup_func, |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 497 | CRYPTO_EX_free *free_func); |
| 498 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 499 | /* CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class |
| 500 | * of object should provide a wrapper function. */ |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 501 | OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val); |
| 502 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 503 | /* CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL |
| 504 | * if no such index exists. Each class of object should provide a wrapper |
| 505 | * function. */ |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 506 | OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index); |
| 507 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 508 | /* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|. */ |
| 509 | OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad); |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 510 | |
| 511 | /* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated |
| 512 | * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given |
| 513 | * class. It returns one on success and zero otherwise. */ |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 514 | OPENSSL_EXPORT int CRYPTO_dup_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class, |
| 515 | CRYPTO_EX_DATA *to, |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 516 | const CRYPTO_EX_DATA *from); |
| 517 | |
| 518 | /* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an |
| 519 | * object of the given class. */ |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 520 | OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class, |
| 521 | void *obj, CRYPTO_EX_DATA *ad); |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 522 | |
| 523 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | #if defined(__cplusplus) |
| 525 | } /* extern C */ |
| 526 | #endif |
| 527 | |
| 528 | #endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */ |