internal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. #ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H
  109. #define OPENSSL_HEADER_CRYPTO_INTERNAL_H
  110. #include <openssl/ex_data.h>
  111. #include <openssl/thread.h>
  112. #include <string.h>
  113. #if defined(_MSC_VER)
  114. #if !defined(__cplusplus) || _MSC_VER < 1900
  115. #define alignas(x) __declspec(align(x))
  116. #define alignof __alignof
  117. #endif
  118. #else
  119. #include <stdalign.h>
  120. #endif
  121. #if !defined(OPENSSL_NO_THREADS) && \
  122. (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__))
  123. #include <pthread.h>
  124. #define OPENSSL_PTHREADS
  125. #endif
  126. #if !defined(OPENSSL_NO_THREADS) && !defined(OPENSSL_PTHREADS) && \
  127. defined(OPENSSL_WINDOWS)
  128. #define OPENSSL_WINDOWS_THREADS
  129. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  130. #include <windows.h>
  131. OPENSSL_MSVC_PRAGMA(warning(pop))
  132. #endif
  133. #if defined(__cplusplus)
  134. extern "C" {
  135. #endif
  136. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
  137. defined(OPENSSL_AARCH64) || defined(OPENSSL_PPC64LE)
  138. /* OPENSSL_cpuid_setup initializes the platform-specific feature cache. */
  139. void OPENSSL_cpuid_setup(void);
  140. #endif
  141. #if !defined(_MSC_VER) && defined(OPENSSL_64_BIT)
  142. typedef __int128_t int128_t;
  143. typedef __uint128_t uint128_t;
  144. #endif
  145. #define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  146. /* buffers_alias returns one if |a| and |b| alias and zero otherwise. */
  147. static inline int buffers_alias(const uint8_t *a, size_t a_len,
  148. const uint8_t *b, size_t b_len) {
  149. /* Cast |a| and |b| to integers. In C, pointer comparisons between unrelated
  150. * objects are undefined whereas pointer to integer conversions are merely
  151. * implementation-defined. We assume the implementation defined it in a sane
  152. * way. */
  153. uintptr_t a_u = (uintptr_t)a;
  154. uintptr_t b_u = (uintptr_t)b;
  155. return a_u + a_len > b_u && b_u + b_len > a_u;
  156. }
  157. /* Constant-time utility functions.
  158. *
  159. * The following methods return a bitmask of all ones (0xff...f) for true and 0
  160. * for false. This is useful for choosing a value based on the result of a
  161. * conditional in constant time. For example,
  162. *
  163. * if (a < b) {
  164. * c = a;
  165. * } else {
  166. * c = b;
  167. * }
  168. *
  169. * can be written as
  170. *
  171. * unsigned int lt = constant_time_lt(a, b);
  172. * c = constant_time_select(lt, a, b); */
  173. /* constant_time_msb returns the given value with the MSB copied to all the
  174. * other bits. */
  175. static inline unsigned int constant_time_msb(unsigned int a) {
  176. return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1));
  177. }
  178. /* constant_time_lt returns 0xff..f if a < b and 0 otherwise. */
  179. static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) {
  180. /* Consider the two cases of the problem:
  181. * msb(a) == msb(b): a < b iff the MSB of a - b is set.
  182. * msb(a) != msb(b): a < b iff the MSB of b is set.
  183. *
  184. * If msb(a) == msb(b) then the following evaluates as:
  185. * msb(a^((a^b)|((a-b)^a))) ==
  186. * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
  187. * msb(a^a^(a-b)) == (rearranging)
  188. * msb(a-b) (because ∀x. x^x == 0)
  189. *
  190. * Else, if msb(a) != msb(b) then the following evaluates as:
  191. * msb(a^((a^b)|((a-b)^a))) ==
  192. * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
  193. * represents a value s.t. msb(𝟙) = 1)
  194. * msb(a^𝟙) == (because ORing with 1 results in 1)
  195. * msb(b)
  196. *
  197. *
  198. * Here is an SMT-LIB verification of this formula:
  199. *
  200. * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
  201. * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
  202. * )
  203. *
  204. * (declare-fun a () (_ BitVec 32))
  205. * (declare-fun b () (_ BitVec 32))
  206. *
  207. * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
  208. * (check-sat)
  209. * (get-model)
  210. */
  211. return constant_time_msb(a^((a^b)|((a-b)^a)));
  212. }
  213. /* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */
  214. static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) {
  215. return (uint8_t)(constant_time_lt(a, b));
  216. }
  217. /* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */
  218. static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) {
  219. return ~constant_time_lt(a, b);
  220. }
  221. /* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */
  222. static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) {
  223. return (uint8_t)(constant_time_ge(a, b));
  224. }
  225. /* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
  226. static inline unsigned int constant_time_is_zero(unsigned int a) {
  227. /* Here is an SMT-LIB verification of this formula:
  228. *
  229. * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
  230. * (bvand (bvnot a) (bvsub a #x00000001))
  231. * )
  232. *
  233. * (declare-fun a () (_ BitVec 32))
  234. *
  235. * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
  236. * (check-sat)
  237. * (get-model)
  238. */
  239. return constant_time_msb(~a & (a - 1));
  240. }
  241. /* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit
  242. * mask. */
  243. static inline uint8_t constant_time_is_zero_8(unsigned int a) {
  244. return (uint8_t)(constant_time_is_zero(a));
  245. }
  246. /* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */
  247. static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) {
  248. return constant_time_is_zero(a ^ b);
  249. }
  250. /* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */
  251. static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) {
  252. return (uint8_t)(constant_time_eq(a, b));
  253. }
  254. /* constant_time_eq_int acts like |constant_time_eq| but works on int values. */
  255. static inline unsigned int constant_time_eq_int(int a, int b) {
  256. return constant_time_eq((unsigned)(a), (unsigned)(b));
  257. }
  258. /* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
  259. * mask. */
  260. static inline uint8_t constant_time_eq_int_8(int a, int b) {
  261. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  262. }
  263. /* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s
  264. * or all 0s (as returned by the methods above), the select methods return
  265. * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
  266. static inline unsigned int constant_time_select(unsigned int mask,
  267. unsigned int a, unsigned int b) {
  268. return (mask & a) | (~mask & b);
  269. }
  270. /* constant_time_select_8 acts like |constant_time_select| but operates on
  271. * 8-bit values. */
  272. static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
  273. uint8_t b) {
  274. return (uint8_t)(constant_time_select(mask, a, b));
  275. }
  276. /* constant_time_select_int acts like |constant_time_select| but operates on
  277. * ints. */
  278. static inline int constant_time_select_int(unsigned int mask, int a, int b) {
  279. return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
  280. }
  281. /* Thread-safe initialisation. */
  282. #if defined(OPENSSL_NO_THREADS)
  283. typedef uint32_t CRYPTO_once_t;
  284. #define CRYPTO_ONCE_INIT 0
  285. #elif defined(OPENSSL_WINDOWS_THREADS)
  286. typedef INIT_ONCE CRYPTO_once_t;
  287. #define CRYPTO_ONCE_INIT INIT_ONCE_STATIC_INIT
  288. #elif defined(OPENSSL_PTHREADS)
  289. typedef pthread_once_t CRYPTO_once_t;
  290. #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
  291. #else
  292. #error "Unknown threading library"
  293. #endif
  294. /* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
  295. * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
  296. * then they will block until |init| completes, but |init| will have only been
  297. * called once.
  298. *
  299. * The |once| argument must be a |CRYPTO_once_t| that has been initialised with
  300. * the value |CRYPTO_ONCE_INIT|. */
  301. OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
  302. /* Reference counting. */
  303. /* CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates. */
  304. #define CRYPTO_REFCOUNT_MAX 0xffffffff
  305. /* CRYPTO_refcount_inc atomically increments the value at |*count| unless the
  306. * value would overflow. It's safe for multiple threads to concurrently call
  307. * this or |CRYPTO_refcount_dec_and_test_zero| on the same
  308. * |CRYPTO_refcount_t|. */
  309. OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count);
  310. /* CRYPTO_refcount_dec_and_test_zero tests the value at |*count|:
  311. * if it's zero, it crashes the address space.
  312. * if it's the maximum value, it returns zero.
  313. * otherwise, it atomically decrements it and returns one iff the resulting
  314. * value is zero.
  315. *
  316. * It's safe for multiple threads to concurrently call this or
  317. * |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. */
  318. OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count);
  319. /* Locks.
  320. *
  321. * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in
  322. * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as
  323. * a global lock. A global lock must be initialised to the value
  324. * |CRYPTO_STATIC_MUTEX_INIT|.
  325. *
  326. * |CRYPTO_MUTEX| can appear in public structures and so is defined in
  327. * thread.h as a structure large enough to fit the real type. The global lock is
  328. * a different type so it may be initialized with platform initializer macros.*/
  329. #if defined(OPENSSL_NO_THREADS)
  330. struct CRYPTO_STATIC_MUTEX {
  331. char padding; /* Empty structs have different sizes in C and C++. */
  332. };
  333. #define CRYPTO_STATIC_MUTEX_INIT { 0 }
  334. #elif defined(OPENSSL_WINDOWS_THREADS)
  335. struct CRYPTO_STATIC_MUTEX {
  336. SRWLOCK lock;
  337. };
  338. #define CRYPTO_STATIC_MUTEX_INIT { SRWLOCK_INIT }
  339. #elif defined(OPENSSL_PTHREADS)
  340. struct CRYPTO_STATIC_MUTEX {
  341. pthread_rwlock_t lock;
  342. };
  343. #define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
  344. #else
  345. #error "Unknown threading library"
  346. #endif
  347. /* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
  348. * |CRYPTO_STATIC_MUTEX|. */
  349. OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
  350. /* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
  351. * read lock, but none may have a write lock. */
  352. OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
  353. /* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
  354. * of lock on it. */
  355. OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
  356. /* CRYPTO_MUTEX_unlock_read unlocks |lock| for reading. */
  357. OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock);
  358. /* CRYPTO_MUTEX_unlock_write unlocks |lock| for writing. */
  359. OPENSSL_EXPORT void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock);
  360. /* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */
  361. OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
  362. /* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also
  363. * have a read lock, but none may have a write lock. The |lock| variable does
  364. * not need to be initialised by any function, but must have been statically
  365. * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */
  366. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_read(
  367. struct CRYPTO_STATIC_MUTEX *lock);
  368. /* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has
  369. * any type of lock on it. The |lock| variable does not need to be initialised
  370. * by any function, but must have been statically initialised with
  371. * |CRYPTO_STATIC_MUTEX_INIT|. */
  372. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_write(
  373. struct CRYPTO_STATIC_MUTEX *lock);
  374. /* CRYPTO_STATIC_MUTEX_unlock_read unlocks |lock| for reading. */
  375. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_read(
  376. struct CRYPTO_STATIC_MUTEX *lock);
  377. /* CRYPTO_STATIC_MUTEX_unlock_write unlocks |lock| for writing. */
  378. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_write(
  379. struct CRYPTO_STATIC_MUTEX *lock);
  380. /* Thread local storage. */
  381. /* thread_local_data_t enumerates the types of thread-local data that can be
  382. * stored. */
  383. typedef enum {
  384. OPENSSL_THREAD_LOCAL_ERR = 0,
  385. OPENSSL_THREAD_LOCAL_RAND,
  386. OPENSSL_THREAD_LOCAL_URANDOM_BUF,
  387. OPENSSL_THREAD_LOCAL_TEST,
  388. NUM_OPENSSL_THREAD_LOCALS,
  389. } thread_local_data_t;
  390. /* thread_local_destructor_t is the type of a destructor function that will be
  391. * called when a thread exits and its thread-local storage needs to be freed. */
  392. typedef void (*thread_local_destructor_t)(void *);
  393. /* CRYPTO_get_thread_local gets the pointer value that is stored for the
  394. * current thread for the given index, or NULL if none has been set. */
  395. OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
  396. /* CRYPTO_set_thread_local sets a pointer value for the current thread at the
  397. * given index. This function should only be called once per thread for a given
  398. * |index|: rather than update the pointer value itself, update the data that
  399. * is pointed to.
  400. *
  401. * The destructor function will be called when a thread exits to free this
  402. * thread-local data. All calls to |CRYPTO_set_thread_local| with the same
  403. * |index| should have the same |destructor| argument. The destructor may be
  404. * called with a NULL argument if a thread that never set a thread-local
  405. * pointer for |index|, exits. The destructor may be called concurrently with
  406. * different arguments.
  407. *
  408. * This function returns one on success or zero on error. If it returns zero
  409. * then |destructor| has been called with |value| already. */
  410. OPENSSL_EXPORT int CRYPTO_set_thread_local(
  411. thread_local_data_t index, void *value,
  412. thread_local_destructor_t destructor);
  413. /* ex_data */
  414. typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
  415. /* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
  416. * supports ex_data. It should defined as a static global within the module
  417. * which defines that type. */
  418. typedef struct {
  419. struct CRYPTO_STATIC_MUTEX lock;
  420. STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
  421. /* num_reserved is one if the ex_data index zero is reserved for legacy
  422. * |TYPE_get_app_data| functions. */
  423. uint8_t num_reserved;
  424. } CRYPTO_EX_DATA_CLASS;
  425. #define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL, 0}
  426. #define CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA \
  427. {CRYPTO_STATIC_MUTEX_INIT, NULL, 1}
  428. /* CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes
  429. * it to |*out_index|. Each class of object should provide a wrapper function
  430. * that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and
  431. * zero otherwise. */
  432. OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class,
  433. int *out_index, long argl,
  434. void *argp, CRYPTO_EX_dup *dup_func,
  435. CRYPTO_EX_free *free_func);
  436. /* CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class
  437. * of object should provide a wrapper function. */
  438. OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
  439. /* CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL
  440. * if no such index exists. Each class of object should provide a wrapper
  441. * function. */
  442. OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
  443. /* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA|. */
  444. OPENSSL_EXPORT void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad);
  445. /* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated
  446. * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given
  447. * class. It returns one on success and zero otherwise. */
  448. OPENSSL_EXPORT int CRYPTO_dup_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  449. CRYPTO_EX_DATA *to,
  450. const CRYPTO_EX_DATA *from);
  451. /* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
  452. * object of the given class. */
  453. OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  454. void *obj, CRYPTO_EX_DATA *ad);
  455. /* Language bug workarounds.
  456. *
  457. * Most C standard library functions are undefined if passed NULL, even when the
  458. * corresponding length is zero. This gives them (and, in turn, all functions
  459. * which call them) surprising behavior on empty arrays. Some compilers will
  460. * miscompile code due to this rule. See also
  461. * https://www.imperialviolet.org/2016/06/26/nonnull.html
  462. *
  463. * These wrapper functions behave the same as the corresponding C standard
  464. * functions, but behave as expected when passed NULL if the length is zero.
  465. *
  466. * Note |OPENSSL_memcmp| is a different function from |CRYPTO_memcmp|. */
  467. /* C++ defines |memchr| as a const-correct overload. */
  468. #if defined(__cplusplus)
  469. extern "C++" {
  470. static inline const void *OPENSSL_memchr(const void *s, int c, size_t n) {
  471. if (n == 0) {
  472. return NULL;
  473. }
  474. return memchr(s, c, n);
  475. }
  476. static inline void *OPENSSL_memchr(void *s, int c, size_t n) {
  477. if (n == 0) {
  478. return NULL;
  479. }
  480. return memchr(s, c, n);
  481. }
  482. } /* extern "C++" */
  483. #else /* __cplusplus */
  484. static inline void *OPENSSL_memchr(const void *s, int c, size_t n) {
  485. if (n == 0) {
  486. return NULL;
  487. }
  488. return memchr(s, c, n);
  489. }
  490. #endif /* __cplusplus */
  491. static inline int OPENSSL_memcmp(const void *s1, const void *s2, size_t n) {
  492. if (n == 0) {
  493. return 0;
  494. }
  495. return memcmp(s1, s2, n);
  496. }
  497. static inline void *OPENSSL_memcpy(void *dst, const void *src, size_t n) {
  498. if (n == 0) {
  499. return dst;
  500. }
  501. return memcpy(dst, src, n);
  502. }
  503. static inline void *OPENSSL_memmove(void *dst, const void *src, size_t n) {
  504. if (n == 0) {
  505. return dst;
  506. }
  507. return memmove(dst, src, n);
  508. }
  509. static inline void *OPENSSL_memset(void *dst, int c, size_t n) {
  510. if (n == 0) {
  511. return dst;
  512. }
  513. return memset(dst, c, n);
  514. }
  515. #if defined(__cplusplus)
  516. } /* extern C */
  517. #endif
  518. #endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */