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