blob: 4e6026ffdd598a2ed13bfb332954ad001eff9f78 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Langley98ad22e2014-08-26 13:38:19 -0700112#include <openssl/ex_data.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -0700113#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114
David Benjamin17cf2cb2016-12-13 01:07:13 -0500115#include <string.h>
116
Brian Smith7cae9f52016-01-17 20:12:57 -1000117#if defined(_MSC_VER)
118#if !defined(__cplusplus) || _MSC_VER < 1900
119#define alignas(x) __declspec(align(x))
120#define alignof __alignof
121#endif
122#else
123#include <stdalign.h>
124#endif
125
David Benjamin84759bd2016-09-20 17:35:09 -0400126#if !defined(OPENSSL_NO_THREADS) && \
127 (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__))
128#include <pthread.h>
129#define OPENSSL_PTHREADS
130#endif
131
132#if !defined(OPENSSL_NO_THREADS) && !defined(OPENSSL_PTHREADS) && \
133 defined(OPENSSL_WINDOWS)
134#define OPENSSL_WINDOWS_THREADS
David Benjamina353cdb2016-06-09 16:48:33 -0400135OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langleydf1f5e72015-04-13 11:04:08 -0700136#include <windows.h>
David Benjamina353cdb2016-06-09 16:48:33 -0400137OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700138#endif
139
Adam Langleyb2cb0ec2014-09-02 14:28:49 -0700140#if defined(__cplusplus)
141extern "C" {
142#endif
143
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langley3e652652015-01-09 15:44:37 -0800145#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
Adam Langley4467e592016-09-23 12:47:24 -0700146 defined(OPENSSL_AARCH64) || defined(OPENSSL_PPC64LE)
147/* OPENSSL_cpuid_setup initializes the platform-specific feature cache. */
David Benjamina70c75c2014-09-11 19:11:15 -0400148void OPENSSL_cpuid_setup(void);
149#endif
Adam Langley98ad22e2014-08-26 13:38:19 -0700150
Adam Langleyb15d8132014-11-03 18:51:20 -0800151
David Benjamin647cd022016-01-27 17:26:48 -0500152#if !defined(_MSC_VER) && defined(OPENSSL_64_BIT)
Brian Smith24e42882016-01-26 15:16:37 -1000153typedef __int128_t int128_t;
154typedef __uint128_t uint128_t;
155#endif
156
Steven Valdezcb966542016-08-17 16:56:14 -0400157#define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
Brian Smith24e42882016-01-26 15:16:37 -1000158
David Benjamin1a01e1f2016-06-08 18:31:24 -0400159/* buffers_alias returns one if |a| and |b| alias and zero otherwise. */
160static inline int buffers_alias(const uint8_t *a, size_t a_len,
161 const uint8_t *b, size_t b_len) {
162 /* Cast |a| and |b| to integers. In C, pointer comparisons between unrelated
163 * objects are undefined whereas pointer to integer conversions are merely
164 * implementation-defined. We assume the implementation defined it in a sane
165 * way. */
166 uintptr_t a_u = (uintptr_t)a;
167 uintptr_t b_u = (uintptr_t)b;
168 return a_u + a_len > b_u && b_u + b_len > a_u;
169}
170
171
Adam Langleyb15d8132014-11-03 18:51:20 -0800172/* Constant-time utility functions.
173 *
174 * The following methods return a bitmask of all ones (0xff...f) for true and 0
175 * for false. This is useful for choosing a value based on the result of a
176 * conditional in constant time. For example,
177 *
178 * if (a < b) {
179 * c = a;
180 * } else {
181 * c = b;
182 * }
183 *
184 * can be written as
185 *
186 * unsigned int lt = constant_time_lt(a, b);
Adam Langleya952d962014-11-03 19:15:40 -0800187 * c = constant_time_select(lt, a, b); */
Adam Langleyb15d8132014-11-03 18:51:20 -0800188
David Benjamind1c0de62017-03-16 11:38:18 -0400189#define CONSTTIME_TRUE_S ~((size_t)0)
190#define CONSTTIME_FALSE_S ((size_t)0)
191#define CONSTTIME_TRUE ~0u
192#define CONSTTIME_FALSE 0u
193#define CONSTTIME_TRUE_8 ((uint8_t)0xff)
194#define CONSTTIME_FALSE_8 ((uint8_t)0)
195
196/* constant_time_msb_s returns the given value with the MSB copied to all the
197 * other bits. */
198static inline size_t constant_time_msb_s(size_t a) {
199 return 0u - (a >> (sizeof(a) * 8 - 1));
200}
201
Adam Langleyb15d8132014-11-03 18:51:20 -0800202/* constant_time_msb returns the given value with the MSB copied to all the
Adam Langley9ed9dae2014-11-04 11:22:01 -0800203 * other bits. */
Adam Langleyb15d8132014-11-03 18:51:20 -0800204static inline unsigned int constant_time_msb(unsigned int a) {
David Benjamind1c0de62017-03-16 11:38:18 -0400205 return 0u - (a >> (sizeof(a) * 8 - 1));
Adam Langleyb15d8132014-11-03 18:51:20 -0800206}
207
David Benjamind1c0de62017-03-16 11:38:18 -0400208/* constant_time_lt_s returns 0xff..f if a < b and 0 otherwise. */
209static inline size_t constant_time_lt_s(size_t a, size_t b) {
Adam Langley9ed9dae2014-11-04 11:22:01 -0800210 /* Consider the two cases of the problem:
211 * msb(a) == msb(b): a < b iff the MSB of a - b is set.
212 * msb(a) != msb(b): a < b iff the MSB of b is set.
213 *
214 * If msb(a) == msb(b) then the following evaluates as:
215 * msb(a^((a^b)|((a-b)^a))) ==
216 * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
217 * msb(a^a^(a-b)) == (rearranging)
218 * msb(a-b) (because ∀x. x^x == 0)
219 *
220 * Else, if msb(a) != msb(b) then the following evaluates as:
221 * msb(a^((a^b)|((a-b)^a))) ==
222 * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
223 * represents a value s.t. msb(𝟙) = 1)
224 * msb(a^𝟙) == (because ORing with 1 results in 1)
225 * msb(b)
226 *
227 *
228 * Here is an SMT-LIB verification of this formula:
229 *
230 * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
231 * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
232 * )
233 *
234 * (declare-fun a () (_ BitVec 32))
235 * (declare-fun b () (_ BitVec 32))
236 *
237 * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
238 * (check-sat)
239 * (get-model)
240 */
David Benjamind1c0de62017-03-16 11:38:18 -0400241 return constant_time_msb_s(a^((a^b)|((a-b)^a)));
Adam Langleyb15d8132014-11-03 18:51:20 -0800242}
243
David Benjamind1c0de62017-03-16 11:38:18 -0400244/* constant_time_lt acts like |constant_time_lt_s| but acts on |unsigned|. */
245static inline unsigned constant_time_lt(unsigned a, unsigned b) {
246 return (unsigned)constant_time_lt_s(a, b);
Adam Langleyb15d8132014-11-03 18:51:20 -0800247}
248
David Benjamind1c0de62017-03-16 11:38:18 -0400249/* constant_time_lt_8 acts like |constant_time_lt_s| but returns an 8-bit
250 * mask. */
251static inline uint8_t constant_time_lt_8(size_t a, size_t b) {
252 return (uint8_t)(constant_time_lt_s(a, b));
Adam Langleyb15d8132014-11-03 18:51:20 -0800253}
254
David Benjamind1c0de62017-03-16 11:38:18 -0400255/* constant_time_ge_s returns 0xff..f if a >= b and 0 otherwise. */
256static inline size_t constant_time_ge_s(size_t a, size_t b) {
257 return ~constant_time_lt_s(a, b);
258}
259
260/* constant_time_ge acts like |constant_time_ge_s| but acts on |unsigned|. */
261static inline unsigned constant_time_ge(unsigned a, unsigned b) {
262 return (unsigned)(constant_time_ge_s(a, b));
263}
264
265/* constant_time_ge_8 acts like |constant_time_ge_s| but returns an 8-bit
266 * mask. */
267static inline uint8_t constant_time_ge_8(size_t a, size_t b) {
268 return (uint8_t)(constant_time_ge_s(a, b));
Adam Langleyb15d8132014-11-03 18:51:20 -0800269}
270
271/* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
David Benjamind1c0de62017-03-16 11:38:18 -0400272static inline size_t constant_time_is_zero_s(size_t a) {
Adam Langley9ed9dae2014-11-04 11:22:01 -0800273 /* Here is an SMT-LIB verification of this formula:
274 *
275 * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
276 * (bvand (bvnot a) (bvsub a #x00000001))
277 * )
278 *
279 * (declare-fun a () (_ BitVec 32))
280 *
281 * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
282 * (check-sat)
283 * (get-model)
284 */
David Benjamind1c0de62017-03-16 11:38:18 -0400285 return constant_time_msb_s(~a & (a - 1));
Adam Langleyb15d8132014-11-03 18:51:20 -0800286}
287
David Benjamind1c0de62017-03-16 11:38:18 -0400288/* constant_time_is_zero acts like |constant_time_is_zero_s| but acts on
289 * |unsigned|. */
290static inline unsigned constant_time_is_zero(unsigned a) {
291 return (unsigned)constant_time_is_zero_s(a);
292}
293
294/* constant_time_is_zero_8 acts like |constant_time_is_zero_s| but returns an
295 * 8-bit mask. */
296static inline uint8_t constant_time_is_zero_8(size_t a) {
297 return (uint8_t)(constant_time_is_zero_s(a));
298}
299
300/* constant_time_eq_s returns 0xff..f if a == b and 0 otherwise. */
301static inline size_t constant_time_eq_s(size_t a, size_t b) {
302 return constant_time_is_zero_s(a ^ b);
303}
304
305/* constant_time_eq acts like |constant_time_eq_s| but acts on |unsigned|. */
306static inline unsigned constant_time_eq(unsigned a, unsigned b) {
307 return (unsigned)constant_time_eq_s(a, b);
308}
309
310/* constant_time_eq_8 acts like |constant_time_eq_s| but returns an 8-bit
Adam Langleyb15d8132014-11-03 18:51:20 -0800311 * mask. */
David Benjamind1c0de62017-03-16 11:38:18 -0400312static inline uint8_t constant_time_eq_8(size_t a, size_t b) {
313 return (uint8_t)(constant_time_eq_s(a, b));
Adam Langleyb15d8132014-11-03 18:51:20 -0800314}
315
David Benjamind1c0de62017-03-16 11:38:18 -0400316/* constant_time_eq_int acts like |constant_time_eq_s| but works on int
317 * values. */
318static inline size_t constant_time_eq_int(int a, int b) {
319 return constant_time_eq_s((size_t)(a), (size_t)(b));
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800320}
321
322/* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
323 * mask. */
324static inline uint8_t constant_time_eq_int_8(int a, int b) {
David Benjamind1c0de62017-03-16 11:38:18 -0400325 return constant_time_eq_8((size_t)(a), (size_t)(b));
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800326}
327
David Benjamind1c0de62017-03-16 11:38:18 -0400328/* constant_time_select_s returns (mask & a) | (~mask & b). When |mask| is all
329 * 1s or all 0s (as returned by the methods above), the select methods return
Adam Langleya952d962014-11-03 19:15:40 -0800330 * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
David Benjamind1c0de62017-03-16 11:38:18 -0400331static inline size_t constant_time_select_s(size_t mask, size_t a, size_t b) {
Adam Langleya952d962014-11-03 19:15:40 -0800332 return (mask & a) | (~mask & b);
333}
334
David Benjamind1c0de62017-03-16 11:38:18 -0400335static inline unsigned constant_time_select(unsigned mask, unsigned a,
336 unsigned b) {
337 return (unsigned)(constant_time_select_s(mask, a, b));
338}
339
Adam Langleya952d962014-11-03 19:15:40 -0800340/* constant_time_select_8 acts like |constant_time_select| but operates on
341 * 8-bit values. */
342static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
343 uint8_t b) {
David Benjamind1c0de62017-03-16 11:38:18 -0400344 return (uint8_t)(constant_time_select_s(mask, a, b));
Adam Langleya952d962014-11-03 19:15:40 -0800345}
346
347/* constant_time_select_int acts like |constant_time_select| but operates on
348 * ints. */
David Benjamind1c0de62017-03-16 11:38:18 -0400349static inline int constant_time_select_int(size_t mask, int a, int b) {
350 return (int)(constant_time_select_s(mask, (size_t)(a), (size_t)(b)));
Adam Langleya952d962014-11-03 19:15:40 -0800351}
352
Adam Langleyb15d8132014-11-03 18:51:20 -0800353
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700354/* Thread-safe initialisation. */
355
Adam Langley65a7e942015-05-07 18:28:27 -0700356#if defined(OPENSSL_NO_THREADS)
357typedef uint32_t CRYPTO_once_t;
358#define CRYPTO_ONCE_INIT 0
David Benjamin84759bd2016-09-20 17:35:09 -0400359#elif defined(OPENSSL_WINDOWS_THREADS)
David Benjamin9dadc3b2016-03-30 19:04:28 -0400360typedef INIT_ONCE CRYPTO_once_t;
361#define CRYPTO_ONCE_INIT INIT_ONCE_STATIC_INIT
David Benjamin84759bd2016-09-20 17:35:09 -0400362#elif defined(OPENSSL_PTHREADS)
Adam Langley65a7e942015-05-07 18:28:27 -0700363typedef pthread_once_t CRYPTO_once_t;
364#define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
David Benjamin84759bd2016-09-20 17:35:09 -0400365#else
366#error "Unknown threading library"
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700367#endif
368
369/* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
370 * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
371 * then they will block until |init| completes, but |init| will have only been
372 * called once.
373 *
374 * The |once| argument must be a |CRYPTO_once_t| that has been initialised with
375 * the value |CRYPTO_ONCE_INIT|. */
David Benjamin4d787182015-04-02 16:34:42 -0400376OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700377
378
Adam Langley6f2e7332015-05-15 12:01:29 -0700379/* Reference counting. */
380
Adam Langley6f2e7332015-05-15 12:01:29 -0700381/* CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates. */
382#define CRYPTO_REFCOUNT_MAX 0xffffffff
383
384/* CRYPTO_refcount_inc atomically increments the value at |*count| unless the
385 * value would overflow. It's safe for multiple threads to concurrently call
386 * this or |CRYPTO_refcount_dec_and_test_zero| on the same
387 * |CRYPTO_refcount_t|. */
388OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count);
389
390/* CRYPTO_refcount_dec_and_test_zero tests the value at |*count|:
391 * if it's zero, it crashes the address space.
392 * if it's the maximum value, it returns zero.
393 * otherwise, it atomically decrements it and returns one iff the resulting
394 * value is zero.
395 *
396 * It's safe for multiple threads to concurrently call this or
397 * |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. */
398OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count);
399
400
Adam Langleydf1f5e72015-04-13 11:04:08 -0700401/* Locks.
402 *
403 * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in
404 * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as
405 * a global lock. A global lock must be initialised to the value
406 * |CRYPTO_STATIC_MUTEX_INIT|.
407 *
408 * |CRYPTO_MUTEX| can appear in public structures and so is defined in
David Benjamin156edfe2016-05-24 15:41:11 +0000409 * thread.h as a structure large enough to fit the real type. The global lock is
410 * a different type so it may be initialized with platform initializer macros.*/
Adam Langleydf1f5e72015-04-13 11:04:08 -0700411
Adam Langley65a7e942015-05-07 18:28:27 -0700412#if defined(OPENSSL_NO_THREADS)
David Benjaminc25d2e62016-04-26 18:04:36 -0400413struct CRYPTO_STATIC_MUTEX {
414 char padding; /* Empty structs have different sizes in C and C++. */
415};
David Benjamin992c20a2016-04-28 12:56:35 -0400416#define CRYPTO_STATIC_MUTEX_INIT { 0 }
David Benjamin84759bd2016-09-20 17:35:09 -0400417#elif defined(OPENSSL_WINDOWS_THREADS)
Adam Langleydf1f5e72015-04-13 11:04:08 -0700418struct CRYPTO_STATIC_MUTEX {
David Benjamin156edfe2016-05-24 15:41:11 +0000419 SRWLOCK lock;
Adam Langleydf1f5e72015-04-13 11:04:08 -0700420};
David Benjamin156edfe2016-05-24 15:41:11 +0000421#define CRYPTO_STATIC_MUTEX_INIT { SRWLOCK_INIT }
David Benjamin84759bd2016-09-20 17:35:09 -0400422#elif defined(OPENSSL_PTHREADS)
Adam Langley65a7e942015-05-07 18:28:27 -0700423struct CRYPTO_STATIC_MUTEX {
424 pthread_rwlock_t lock;
425};
426#define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
David Benjamin84759bd2016-09-20 17:35:09 -0400427#else
428#error "Unknown threading library"
Adam Langleydf1f5e72015-04-13 11:04:08 -0700429#endif
430
431/* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
432 * |CRYPTO_STATIC_MUTEX|. */
Adam Langley04edcc82015-05-20 13:14:44 -0700433OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700434
435/* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
David Benjamin156edfe2016-05-24 15:41:11 +0000436 * read lock, but none may have a write lock. */
Adam Langley04edcc82015-05-20 13:14:44 -0700437OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700438
439/* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
440 * of lock on it. */
Adam Langley04edcc82015-05-20 13:14:44 -0700441OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700442
David Benjamin29270de2016-05-24 15:28:36 +0000443/* CRYPTO_MUTEX_unlock_read unlocks |lock| for reading. */
444OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock);
445
446/* CRYPTO_MUTEX_unlock_write unlocks |lock| for writing. */
447OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700448
449/* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */
Adam Langley04edcc82015-05-20 13:14:44 -0700450OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700451
452/* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also
453 * have a read lock, but none may have a write lock. The |lock| variable does
454 * not need to be initialised by any function, but must have been statically
455 * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */
Adam Langley04edcc82015-05-20 13:14:44 -0700456OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_read(
457 struct CRYPTO_STATIC_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700458
459/* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has
460 * any type of lock on it. The |lock| variable does not need to be initialised
461 * by any function, but must have been statically initialised with
462 * |CRYPTO_STATIC_MUTEX_INIT|. */
Adam Langley04edcc82015-05-20 13:14:44 -0700463OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_write(
464 struct CRYPTO_STATIC_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700465
David Benjamin29270de2016-05-24 15:28:36 +0000466/* CRYPTO_STATIC_MUTEX_unlock_read unlocks |lock| for reading. */
467OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_read(
468 struct CRYPTO_STATIC_MUTEX *lock);
469
470/* CRYPTO_STATIC_MUTEX_unlock_write unlocks |lock| for writing. */
471OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_write(
Adam Langley04edcc82015-05-20 13:14:44 -0700472 struct CRYPTO_STATIC_MUTEX *lock);
Adam Langleydf1f5e72015-04-13 11:04:08 -0700473
474
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700475/* Thread local storage. */
476
477/* thread_local_data_t enumerates the types of thread-local data that can be
478 * stored. */
479typedef enum {
480 OPENSSL_THREAD_LOCAL_ERR = 0,
Adam Langley310d4dd2015-04-13 11:04:21 -0700481 OPENSSL_THREAD_LOCAL_RAND,
Matt Braithwaitecc2c7aa2015-09-03 14:18:08 -0700482 OPENSSL_THREAD_LOCAL_URANDOM_BUF,
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700483 OPENSSL_THREAD_LOCAL_TEST,
484 NUM_OPENSSL_THREAD_LOCALS,
485} thread_local_data_t;
486
487/* thread_local_destructor_t is the type of a destructor function that will be
488 * called when a thread exits and its thread-local storage needs to be freed. */
489typedef void (*thread_local_destructor_t)(void *);
490
491/* CRYPTO_get_thread_local gets the pointer value that is stored for the
492 * current thread for the given index, or NULL if none has been set. */
David Benjamin4d787182015-04-02 16:34:42 -0400493OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700494
495/* CRYPTO_set_thread_local sets a pointer value for the current thread at the
496 * given index. This function should only be called once per thread for a given
497 * |index|: rather than update the pointer value itself, update the data that
498 * is pointed to.
499 *
500 * The destructor function will be called when a thread exits to free this
501 * thread-local data. All calls to |CRYPTO_set_thread_local| with the same
502 * |index| should have the same |destructor| argument. The destructor may be
503 * called with a NULL argument if a thread that never set a thread-local
504 * pointer for |index|, exits. The destructor may be called concurrently with
505 * different arguments.
506 *
507 * This function returns one on success or zero on error. If it returns zero
508 * then |destructor| has been called with |value| already. */
David Benjamin4d787182015-04-02 16:34:42 -0400509OPENSSL_EXPORT int CRYPTO_set_thread_local(
510 thread_local_data_t index, void *value,
511 thread_local_destructor_t destructor);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700512
513
David Benjamin546f1a52015-04-15 16:46:09 -0400514/* ex_data */
515
David Benjamin9f33fc62015-04-15 17:29:53 -0400516typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
517
518/* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
519 * supports ex_data. It should defined as a static global within the module
520 * which defines that type. */
521typedef struct {
522 struct CRYPTO_STATIC_MUTEX lock;
523 STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
David Benjaminaa585132015-06-29 23:36:17 -0400524 /* num_reserved is one if the ex_data index zero is reserved for legacy
525 * |TYPE_get_app_data| functions. */
526 uint8_t num_reserved;
David Benjamin9f33fc62015-04-15 17:29:53 -0400527} CRYPTO_EX_DATA_CLASS;
528
David Benjaminaa585132015-06-29 23:36:17 -0400529#define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL, 0}
530#define CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA \
531 {CRYPTO_STATIC_MUTEX_INIT, NULL, 1}
David Benjamin9f33fc62015-04-15 17:29:53 -0400532
533/* CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes
534 * it to |*out_index|. Each class of object should provide a wrapper function
535 * that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and
536 * zero otherwise. */
537OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class,
538 int *out_index, long argl,
David Benjamin8a589332015-12-04 23:14:35 -0500539 void *argp, CRYPTO_EX_dup *dup_func,
David Benjamin546f1a52015-04-15 16:46:09 -0400540 CRYPTO_EX_free *free_func);
541
David Benjamin9f33fc62015-04-15 17:29:53 -0400542/* CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class
543 * of object should provide a wrapper function. */
David Benjamin546f1a52015-04-15 16:46:09 -0400544OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
545
David Benjamin9f33fc62015-04-15 17:29:53 -0400546/* CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL
547 * if no such index exists. Each class of object should provide a wrapper
548 * function. */
David Benjamin546f1a52015-04-15 16:46:09 -0400549OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
550
David Benjamin8a589332015-12-04 23:14:35 -0500551/* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|. */
552OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad);
David Benjamin546f1a52015-04-15 16:46:09 -0400553
554/* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated
555 * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given
556 * class. It returns one on success and zero otherwise. */
David Benjamin9f33fc62015-04-15 17:29:53 -0400557OPENSSL_EXPORT int CRYPTO_dup_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
558 CRYPTO_EX_DATA *to,
David Benjamin546f1a52015-04-15 16:46:09 -0400559 const CRYPTO_EX_DATA *from);
560
561/* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
562 * object of the given class. */
David Benjamin9f33fc62015-04-15 17:29:53 -0400563OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
564 void *obj, CRYPTO_EX_DATA *ad);
David Benjamin546f1a52015-04-15 16:46:09 -0400565
566
David Benjamin17cf2cb2016-12-13 01:07:13 -0500567/* Language bug workarounds.
568 *
569 * Most C standard library functions are undefined if passed NULL, even when the
570 * corresponding length is zero. This gives them (and, in turn, all functions
571 * which call them) surprising behavior on empty arrays. Some compilers will
572 * miscompile code due to this rule. See also
573 * https://www.imperialviolet.org/2016/06/26/nonnull.html
574 *
David Benjaminec6b5302016-12-21 16:00:13 -0500575 * These wrapper functions behave the same as the corresponding C standard
576 * functions, but behave as expected when passed NULL if the length is zero.
David Benjamin17cf2cb2016-12-13 01:07:13 -0500577 *
578 * Note |OPENSSL_memcmp| is a different function from |CRYPTO_memcmp|. */
579
580/* C++ defines |memchr| as a const-correct overload. */
581#if defined(__cplusplus)
582extern "C++" {
583
584static inline const void *OPENSSL_memchr(const void *s, int c, size_t n) {
585 if (n == 0) {
586 return NULL;
587 }
588
589 return memchr(s, c, n);
590}
591
592static inline void *OPENSSL_memchr(void *s, int c, size_t n) {
593 if (n == 0) {
594 return NULL;
595 }
596
597 return memchr(s, c, n);
598}
599
600} /* extern "C++" */
601#else /* __cplusplus */
602
603static inline void *OPENSSL_memchr(const void *s, int c, size_t n) {
604 if (n == 0) {
605 return NULL;
606 }
607
608 return memchr(s, c, n);
609}
610
611#endif /* __cplusplus */
612
613static inline int OPENSSL_memcmp(const void *s1, const void *s2, size_t n) {
614 if (n == 0) {
615 return 0;
616 }
617
618 return memcmp(s1, s2, n);
619}
620
621static inline void *OPENSSL_memcpy(void *dst, const void *src, size_t n) {
622 if (n == 0) {
623 return dst;
624 }
625
626 return memcpy(dst, src, n);
627}
628
629static inline void *OPENSSL_memmove(void *dst, const void *src, size_t n) {
630 if (n == 0) {
631 return dst;
632 }
633
634 return memmove(dst, src, n);
635}
636
637static inline void *OPENSSL_memset(void *dst, int c, size_t n) {
638 if (n == 0) {
639 return dst;
640 }
641
642 return memset(dst, c, n);
643}
644
645
Adam Langley95c29f32014-06-20 12:00:00 -0700646#if defined(__cplusplus)
647} /* extern C */
648#endif
649
650#endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */