Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

642 righe
24 KiB

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