blob: 28ec3eeb8c754d6fde80021de8f2fcc8c4284b80 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H
110#define OPENSSL_HEADER_CRYPTO_INTERNAL_H
111
112#include <openssl/ex_data.h>
Robert Sloan8ff03552017-06-14 12:40:58 -0700113#include <openssl/stack.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700114#include <openssl/thread.h>
115
Robert Sloan69939df2017-01-09 10:53:07 -0800116#include <string.h>
117
David Benjamin4969cc92016-04-22 15:02:23 -0400118#if defined(_MSC_VER)
119#if !defined(__cplusplus) || _MSC_VER < 1900
120#define alignas(x) __declspec(align(x))
121#define alignof __alignof
122#endif
123#else
124#include <stdalign.h>
125#endif
126
David Benjamin7c0d06c2016-08-11 13:26:41 -0400127#if !defined(OPENSSL_NO_THREADS) && \
128 (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__))
129#include <pthread.h>
130#define OPENSSL_PTHREADS
131#endif
132
133#if !defined(OPENSSL_NO_THREADS) && !defined(OPENSSL_PTHREADS) && \
134 defined(OPENSSL_WINDOWS)
135#define OPENSSL_WINDOWS_THREADS
David Benjamin6e899c72016-06-09 18:02:18 -0400136OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langleye9ada862015-05-11 17:20:37 -0700137#include <windows.h>
David Benjamin6e899c72016-06-09 18:02:18 -0400138OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langleye9ada862015-05-11 17:20:37 -0700139#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140
141#if defined(__cplusplus)
142extern "C" {
143#endif
144
145
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400147 defined(OPENSSL_AARCH64) || defined(OPENSSL_PPC64LE)
148/* OPENSSL_cpuid_setup initializes the platform-specific feature cache. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149void OPENSSL_cpuid_setup(void);
150#endif
151
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152
David Benjamin4969cc92016-04-22 15:02:23 -0400153#if !defined(_MSC_VER) && defined(OPENSSL_64_BIT)
154typedef __int128_t int128_t;
155typedef __uint128_t uint128_t;
156#endif
157
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400158#define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
David Benjamin4969cc92016-04-22 15:02:23 -0400159
David Benjamin6e899c72016-06-09 18:02:18 -0400160/* buffers_alias returns one if |a| and |b| alias and zero otherwise. */
161static inline int buffers_alias(const uint8_t *a, size_t a_len,
162 const uint8_t *b, size_t b_len) {
163 /* Cast |a| and |b| to integers. In C, pointer comparisons between unrelated
164 * objects are undefined whereas pointer to integer conversions are merely
165 * implementation-defined. We assume the implementation defined it in a sane
166 * way. */
167 uintptr_t a_u = (uintptr_t)a;
168 uintptr_t b_u = (uintptr_t)b;
169 return a_u + a_len > b_u && b_u + b_len > a_u;
170}
171
172
Adam Langleyd9e397b2015-01-22 14:27:53 -0800173/* Constant-time utility functions.
174 *
175 * The following methods return a bitmask of all ones (0xff...f) for true and 0
176 * for false. This is useful for choosing a value based on the result of a
177 * conditional in constant time. For example,
178 *
179 * if (a < b) {
180 * c = a;
181 * } else {
182 * c = b;
183 * }
184 *
185 * can be written as
186 *
Robert Sloan9254e682017-04-24 09:42:06 -0700187 * crypto_word_t lt = constant_time_lt_w(a, b);
188 * c = constant_time_select_w(lt, a, b); */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800189
Robert Sloan9254e682017-04-24 09:42:06 -0700190/* crypto_word_t is the type that most constant-time functions use. Ideally we
191 * would like it to be |size_t|, but NaCl builds in 64-bit mode with 32-bit
192 * pointers, which means that |size_t| can be 32 bits when |BN_ULONG| is 64
193 * bits. Since we want to be able to do constant-time operations on a
194 * |BN_ULONG|, |crypto_word_t| is defined as an unsigned value with the native
195 * word length. */
196#if defined(OPENSSL_64_BIT)
197typedef uint64_t crypto_word_t;
198#elif defined(OPENSSL_32_BIT)
199typedef uint32_t crypto_word_t;
200#else
201#error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
202#endif
203
204#define CONSTTIME_TRUE_W ~((crypto_word_t)0)
205#define CONSTTIME_FALSE_W ((crypto_word_t)0)
206#define CONSTTIME_TRUE_8 ((uint8_t)0xff)
207
208#define CONSTTIME_TRUE_W ~((crypto_word_t)0)
209#define CONSTTIME_FALSE_W ((crypto_word_t)0)
Robert Sloan6f79a502017-04-03 09:16:40 -0700210#define CONSTTIME_TRUE_8 ((uint8_t)0xff)
211#define CONSTTIME_FALSE_8 ((uint8_t)0)
212
Robert Sloan9254e682017-04-24 09:42:06 -0700213/* constant_time_msb_w returns the given value with the MSB copied to all the
Adam Langleyd9e397b2015-01-22 14:27:53 -0800214 * other bits. */
Robert Sloan9254e682017-04-24 09:42:06 -0700215static inline crypto_word_t constant_time_msb_w(crypto_word_t a) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700216 return 0u - (a >> (sizeof(a) * 8 - 1));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217}
218
Robert Sloan9254e682017-04-24 09:42:06 -0700219/* constant_time_lt_w returns 0xff..f if a < b and 0 otherwise. */
220static inline crypto_word_t constant_time_lt_w(crypto_word_t a,
221 crypto_word_t b) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222 /* Consider the two cases of the problem:
223 * msb(a) == msb(b): a < b iff the MSB of a - b is set.
224 * msb(a) != msb(b): a < b iff the MSB of b is set.
225 *
226 * If msb(a) == msb(b) then the following evaluates as:
227 * msb(a^((a^b)|((a-b)^a))) ==
228 * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
229 * msb(a^a^(a-b)) == (rearranging)
230 * msb(a-b) (because ∀x. x^x == 0)
231 *
232 * Else, if msb(a) != msb(b) then the following evaluates as:
233 * msb(a^((a^b)|((a-b)^a))) ==
234 * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
235 * represents a value s.t. msb(𝟙) = 1)
236 * msb(a^𝟙) == (because ORing with 1 results in 1)
237 * msb(b)
238 *
239 *
240 * Here is an SMT-LIB verification of this formula:
241 *
242 * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
243 * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
244 * )
245 *
246 * (declare-fun a () (_ BitVec 32))
247 * (declare-fun b () (_ BitVec 32))
248 *
249 * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
250 * (check-sat)
251 * (get-model)
252 */
Robert Sloan9254e682017-04-24 09:42:06 -0700253 return constant_time_msb_w(a^((a^b)|((a-b)^a)));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254}
255
Robert Sloan9254e682017-04-24 09:42:06 -0700256/* constant_time_lt_8 acts like |constant_time_lt_w| but returns an 8-bit
Robert Sloan6f79a502017-04-03 09:16:40 -0700257 * mask. */
Robert Sloan9254e682017-04-24 09:42:06 -0700258static 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 Langleyd9e397b2015-01-22 14:27:53 -0800260}
261
Robert Sloan9254e682017-04-24 09:42:06 -0700262/* constant_time_ge_w returns 0xff..f if a >= b and 0 otherwise. */
263static 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);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266}
267
Robert Sloan9254e682017-04-24 09:42:06 -0700268/* constant_time_ge_8 acts like |constant_time_ge_w| but returns an 8-bit
Robert Sloan6f79a502017-04-03 09:16:40 -0700269 * mask. */
Robert Sloan9254e682017-04-24 09:42:06 -0700270static 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 Langleyd9e397b2015-01-22 14:27:53 -0800272}
273
274/* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
Robert Sloan9254e682017-04-24 09:42:06 -0700275static inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800276 /* 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)
287 */
Robert Sloan9254e682017-04-24 09:42:06 -0700288 return constant_time_msb_w(~a & (a - 1));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289}
290
Robert Sloan9254e682017-04-24 09:42:06 -0700291/* constant_time_is_zero_8 acts like |constant_time_is_zero_w| but returns an
Robert Sloan6f79a502017-04-03 09:16:40 -0700292 * 8-bit mask. */
Robert Sloan9254e682017-04-24 09:42:06 -0700293static inline uint8_t constant_time_is_zero_8(crypto_word_t a) {
294 return (uint8_t)(constant_time_is_zero_w(a));
Robert Sloan6f79a502017-04-03 09:16:40 -0700295}
296
Robert Sloan9254e682017-04-24 09:42:06 -0700297/* constant_time_eq_w returns 0xff..f if a == b and 0 otherwise. */
298static inline crypto_word_t constant_time_eq_w(crypto_word_t a,
299 crypto_word_t b) {
300 return constant_time_is_zero_w(a ^ b);
Robert Sloan6f79a502017-04-03 09:16:40 -0700301}
302
Robert Sloan9254e682017-04-24 09:42:06 -0700303/* constant_time_eq_8 acts like |constant_time_eq_w| but returns an 8-bit
Adam Langleyd9e397b2015-01-22 14:27:53 -0800304 * mask. */
Robert Sloan9254e682017-04-24 09:42:06 -0700305static inline uint8_t constant_time_eq_8(crypto_word_t a, crypto_word_t b) {
306 return (uint8_t)(constant_time_eq_w(a, b));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800307}
308
Robert Sloan9254e682017-04-24 09:42:06 -0700309/* constant_time_eq_int acts like |constant_time_eq_w| but works on int
Robert Sloan6f79a502017-04-03 09:16:40 -0700310 * values. */
Robert Sloan9254e682017-04-24 09:42:06 -0700311static inline crypto_word_t constant_time_eq_int(int a, int b) {
312 return constant_time_eq_w((crypto_word_t)(a), (crypto_word_t)(b));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800313}
314
315/* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
316 * mask. */
317static inline uint8_t constant_time_eq_int_8(int a, int b) {
Robert Sloan9254e682017-04-24 09:42:06 -0700318 return constant_time_eq_8((crypto_word_t)(a), (crypto_word_t)(b));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319}
320
Robert Sloan9254e682017-04-24 09:42:06 -0700321/* constant_time_select_w returns (mask & a) | (~mask & b). When |mask| is all
Robert Sloan6f79a502017-04-03 09:16:40 -0700322 * 1s or all 0s (as returned by the methods above), the select methods return
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323 * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
Robert Sloan9254e682017-04-24 09:42:06 -0700324static inline crypto_word_t constant_time_select_w(crypto_word_t mask,
325 crypto_word_t a,
326 crypto_word_t b) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327 return (mask & a) | (~mask & b);
328}
329
330/* constant_time_select_8 acts like |constant_time_select| but operates on
331 * 8-bit values. */
332static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
333 uint8_t b) {
Robert Sloan9254e682017-04-24 09:42:06 -0700334 return (uint8_t)(constant_time_select_w(mask, a, b));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800335}
336
337/* constant_time_select_int acts like |constant_time_select| but operates on
338 * ints. */
Robert Sloan9254e682017-04-24 09:42:06 -0700339static inline int constant_time_select_int(crypto_word_t mask, int a, int b) {
340 return (int)(constant_time_select_w(mask, (crypto_word_t)(a),
341 (crypto_word_t)(b)));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342}
343
344
Adam Langleye9ada862015-05-11 17:20:37 -0700345/* Thread-safe initialisation. */
346
347#if defined(OPENSSL_NO_THREADS)
348typedef uint32_t CRYPTO_once_t;
349#define CRYPTO_ONCE_INIT 0
David Benjamin7c0d06c2016-08-11 13:26:41 -0400350#elif defined(OPENSSL_WINDOWS_THREADS)
David Benjamin4969cc92016-04-22 15:02:23 -0400351typedef INIT_ONCE CRYPTO_once_t;
352#define CRYPTO_ONCE_INIT INIT_ONCE_STATIC_INIT
David Benjamin7c0d06c2016-08-11 13:26:41 -0400353#elif defined(OPENSSL_PTHREADS)
Adam Langleye9ada862015-05-11 17:20:37 -0700354typedef pthread_once_t CRYPTO_once_t;
355#define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
David Benjamin7c0d06c2016-08-11 13:26:41 -0400356#else
357#error "Unknown threading library"
Adam Langleye9ada862015-05-11 17:20:37 -0700358#endif
359
360/* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
361 * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
362 * then they will block until |init| completes, but |init| will have only been
363 * called once.
364 *
365 * The |once| argument must be a |CRYPTO_once_t| that has been initialised with
366 * the value |CRYPTO_ONCE_INIT|. */
367OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
368
369
Adam Langleyf4e42722015-06-04 17:45:09 -0700370/* Reference counting. */
371
372/* CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates. */
373#define CRYPTO_REFCOUNT_MAX 0xffffffff
374
375/* CRYPTO_refcount_inc atomically increments the value at |*count| unless the
376 * value would overflow. It's safe for multiple threads to concurrently call
377 * this or |CRYPTO_refcount_dec_and_test_zero| on the same
378 * |CRYPTO_refcount_t|. */
379OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count);
380
381/* CRYPTO_refcount_dec_and_test_zero tests the value at |*count|:
382 * if it's zero, it crashes the address space.
383 * if it's the maximum value, it returns zero.
384 * otherwise, it atomically decrements it and returns one iff the resulting
385 * value is zero.
386 *
387 * It's safe for multiple threads to concurrently call this or
388 * |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. */
389OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count);
390
391
Adam Langleye9ada862015-05-11 17:20:37 -0700392/* Locks.
393 *
394 * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in
395 * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as
396 * a global lock. A global lock must be initialised to the value
397 * |CRYPTO_STATIC_MUTEX_INIT|.
398 *
399 * |CRYPTO_MUTEX| can appear in public structures and so is defined in
David Benjamind316cba2016-06-02 16:17:39 -0400400 * thread.h as a structure large enough to fit the real type. The global lock is
401 * a different type so it may be initialized with platform initializer macros.*/
Adam Langleye9ada862015-05-11 17:20:37 -0700402
403#if defined(OPENSSL_NO_THREADS)
David Benjamin4969cc92016-04-22 15:02:23 -0400404struct CRYPTO_STATIC_MUTEX {
405 char padding; /* Empty structs have different sizes in C and C++. */
406};
407#define CRYPTO_STATIC_MUTEX_INIT { 0 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400408#elif defined(OPENSSL_WINDOWS_THREADS)
Adam Langleye9ada862015-05-11 17:20:37 -0700409struct CRYPTO_STATIC_MUTEX {
David Benjamind316cba2016-06-02 16:17:39 -0400410 SRWLOCK lock;
Adam Langleye9ada862015-05-11 17:20:37 -0700411};
David Benjamind316cba2016-06-02 16:17:39 -0400412#define CRYPTO_STATIC_MUTEX_INIT { SRWLOCK_INIT }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400413#elif defined(OPENSSL_PTHREADS)
Adam Langleye9ada862015-05-11 17:20:37 -0700414struct CRYPTO_STATIC_MUTEX {
415 pthread_rwlock_t lock;
416};
417#define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400418#else
419#error "Unknown threading library"
Adam Langleye9ada862015-05-11 17:20:37 -0700420#endif
421
422/* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
423 * |CRYPTO_STATIC_MUTEX|. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700424OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700425
426/* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
David Benjamind316cba2016-06-02 16:17:39 -0400427 * read lock, but none may have a write lock. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700428OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700429
430/* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
431 * of lock on it. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700432OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700433
David Benjamind316cba2016-06-02 16:17:39 -0400434/* CRYPTO_MUTEX_unlock_read unlocks |lock| for reading. */
435OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock);
436
437/* CRYPTO_MUTEX_unlock_write unlocks |lock| for writing. */
438OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700439
440/* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700441OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700442
443/* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also
444 * have a read lock, but none may have a write lock. The |lock| variable does
445 * not need to be initialised by any function, but must have been statically
446 * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700447OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_read(
448 struct CRYPTO_STATIC_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700449
450/* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has
451 * any type of lock on it. The |lock| variable does not need to be initialised
452 * by any function, but must have been statically initialised with
453 * |CRYPTO_STATIC_MUTEX_INIT|. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700454OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_write(
455 struct CRYPTO_STATIC_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700456
David Benjamind316cba2016-06-02 16:17:39 -0400457/* CRYPTO_STATIC_MUTEX_unlock_read unlocks |lock| for reading. */
458OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_read(
459 struct CRYPTO_STATIC_MUTEX *lock);
460
461/* CRYPTO_STATIC_MUTEX_unlock_write unlocks |lock| for writing. */
462OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_write(
Adam Langleyf4e42722015-06-04 17:45:09 -0700463 struct CRYPTO_STATIC_MUTEX *lock);
Adam Langleye9ada862015-05-11 17:20:37 -0700464
465
466/* Thread local storage. */
467
468/* thread_local_data_t enumerates the types of thread-local data that can be
469 * stored. */
470typedef enum {
471 OPENSSL_THREAD_LOCAL_ERR = 0,
472 OPENSSL_THREAD_LOCAL_RAND,
473 OPENSSL_THREAD_LOCAL_TEST,
474 NUM_OPENSSL_THREAD_LOCALS,
475} thread_local_data_t;
476
477/* thread_local_destructor_t is the type of a destructor function that will be
478 * called when a thread exits and its thread-local storage needs to be freed. */
479typedef void (*thread_local_destructor_t)(void *);
480
481/* CRYPTO_get_thread_local gets the pointer value that is stored for the
482 * current thread for the given index, or NULL if none has been set. */
483OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
484
485/* CRYPTO_set_thread_local sets a pointer value for the current thread at the
486 * given index. This function should only be called once per thread for a given
487 * |index|: rather than update the pointer value itself, update the data that
488 * is pointed to.
489 *
490 * The destructor function will be called when a thread exits to free this
491 * thread-local data. All calls to |CRYPTO_set_thread_local| with the same
492 * |index| should have the same |destructor| argument. The destructor may be
493 * called with a NULL argument if a thread that never set a thread-local
494 * pointer for |index|, exits. The destructor may be called concurrently with
495 * different arguments.
496 *
497 * This function returns one on success or zero on error. If it returns zero
498 * then |destructor| has been called with |value| already. */
499OPENSSL_EXPORT int CRYPTO_set_thread_local(
500 thread_local_data_t index, void *value,
501 thread_local_destructor_t destructor);
502
503
504/* ex_data */
505
506typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
507
Robert Sloan8ff03552017-06-14 12:40:58 -0700508DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
509
Adam Langleye9ada862015-05-11 17:20:37 -0700510/* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
511 * supports ex_data. It should defined as a static global within the module
512 * which defines that type. */
513typedef struct {
514 struct CRYPTO_STATIC_MUTEX lock;
515 STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
Kenny Rootb8494592015-09-25 02:29:14 +0000516 /* num_reserved is one if the ex_data index zero is reserved for legacy
517 * |TYPE_get_app_data| functions. */
518 uint8_t num_reserved;
Adam Langleye9ada862015-05-11 17:20:37 -0700519} CRYPTO_EX_DATA_CLASS;
520
Kenny Rootb8494592015-09-25 02:29:14 +0000521#define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL, 0}
522#define CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA \
523 {CRYPTO_STATIC_MUTEX_INIT, NULL, 1}
Adam Langleye9ada862015-05-11 17:20:37 -0700524
525/* CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes
526 * it to |*out_index|. Each class of object should provide a wrapper function
527 * that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and
528 * zero otherwise. */
529OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class,
530 int *out_index, long argl,
Robert Sloan8ff03552017-06-14 12:40:58 -0700531 void *argp,
Adam Langleye9ada862015-05-11 17:20:37 -0700532 CRYPTO_EX_free *free_func);
533
534/* CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class
535 * of object should provide a wrapper function. */
536OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
537
538/* CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL
539 * if no such index exists. Each class of object should provide a wrapper
540 * function. */
541OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
542
Adam Langley4139edb2016-01-13 15:00:54 -0800543/* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|. */
544OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad);
Adam Langleye9ada862015-05-11 17:20:37 -0700545
Adam Langleye9ada862015-05-11 17:20:37 -0700546/* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
547 * object of the given class. */
548OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
549 void *obj, CRYPTO_EX_DATA *ad);
550
551
Robert Sloan69939df2017-01-09 10:53:07 -0800552/* Language bug workarounds.
553 *
554 * Most C standard library functions are undefined if passed NULL, even when the
555 * corresponding length is zero. This gives them (and, in turn, all functions
556 * which call them) surprising behavior on empty arrays. Some compilers will
557 * miscompile code due to this rule. See also
558 * https://www.imperialviolet.org/2016/06/26/nonnull.html
559 *
560 * These wrapper functions behave the same as the corresponding C standard
561 * functions, but behave as expected when passed NULL if the length is zero.
562 *
563 * Note |OPENSSL_memcmp| is a different function from |CRYPTO_memcmp|. */
564
565/* C++ defines |memchr| as a const-correct overload. */
566#if defined(__cplusplus)
567extern "C++" {
568
569static inline const void *OPENSSL_memchr(const void *s, int c, size_t n) {
570 if (n == 0) {
571 return NULL;
572 }
573
574 return memchr(s, c, n);
575}
576
577static inline void *OPENSSL_memchr(void *s, int c, size_t n) {
578 if (n == 0) {
579 return NULL;
580 }
581
582 return memchr(s, c, n);
583}
584
585} /* extern "C++" */
586#else /* __cplusplus */
587
588static inline void *OPENSSL_memchr(const void *s, int c, size_t n) {
589 if (n == 0) {
590 return NULL;
591 }
592
593 return memchr(s, c, n);
594}
595
596#endif /* __cplusplus */
597
598static inline int OPENSSL_memcmp(const void *s1, const void *s2, size_t n) {
599 if (n == 0) {
600 return 0;
601 }
602
603 return memcmp(s1, s2, n);
604}
605
606static inline void *OPENSSL_memcpy(void *dst, const void *src, size_t n) {
607 if (n == 0) {
608 return dst;
609 }
610
611 return memcpy(dst, src, n);
612}
613
614static inline void *OPENSSL_memmove(void *dst, const void *src, size_t n) {
615 if (n == 0) {
616 return dst;
617 }
618
619 return memmove(dst, src, n);
620}
621
622static inline void *OPENSSL_memset(void *dst, int c, size_t n) {
623 if (n == 0) {
624 return dst;
625 }
626
627 return memset(dst, c, n);
628}
629
Robert Sloan8ff03552017-06-14 12:40:58 -0700630#if defined(BORINGSSL_FIPS)
631/* BORINGSSL_FIPS_abort is called when a FIPS power-on or continuous test
632 * fails. It prevents any further cryptographic operations by the current
633 * process. */
634void BORINGSSL_FIPS_abort(void) __attribute__((noreturn));
635#endif
Robert Sloan69939df2017-01-09 10:53:07 -0800636
Adam Langleyd9e397b2015-01-22 14:27:53 -0800637#if defined(__cplusplus)
638} /* extern C */
639#endif
640
641#endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */