blob: 2dab634e817ef3ce1ba30300678555a14340fac9 [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
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700115#if !defined(OPENSSL_WINDOWS)
116#include <pthread.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -0700117#else
118#pragma warning(push, 3)
119#include <windows.h>
120#pragma warning(pop)
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700121#endif
122
Adam Langleyb2cb0ec2014-09-02 14:28:49 -0700123#if defined(__cplusplus)
124extern "C" {
125#endif
126
Adam Langley95c29f32014-06-20 12:00:00 -0700127
Brian Smitha039d702015-01-29 15:03:18 -0800128/* MSVC's C4701 warning about the use of *potentially*--as opposed to
129 * *definitely*--uninitialized values sometimes has false positives. Usually
130 * the false positives can and should be worked around by simplifying the
131 * control flow. When that is not practical, annotate the function containing
132 * the code that triggers the warning with
133 * OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS after its parameters:
134 *
135 * void f() OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS {
136 * ...
137 * }
138 *
139 * Note that MSVC's control flow analysis seems to operate on a whole-function
140 * basis, so the annotation must be placed on the entire function, not just a
141 * block within the function. */
142#if defined(_MSC_VER)
143#define OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS \
144 __pragma(warning(suppress:4701))
145#else
146#define OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS
147#endif
148
Brian Smith1a9bc442015-03-16 19:27:05 -1000149/* MSVC will sometimes correctly detect unreachable code and issue a warning,
150 * which breaks the build since we treat errors as warnings, in some rare cases
151 * where we want to allow the dead code to continue to exist. In these
152 * situations, annotate the function containing the unreachable code with
153 * OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS after its parameters:
154 *
155 * void f() OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
156 * ...
157 * }
158 *
159 * Note that MSVC's reachability analysis seems to operate on a whole-function
160 * basis, so the annotation must be placed on the entire function, not just a
161 * block within the function. */
162#if defined(_MSC_VER)
163#define OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS \
164 __pragma(warning(suppress:4702))
165#else
166#define OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS
167#endif
168
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley46a7ca02015-01-05 16:29:28 -0800170#if defined(_MSC_VER)
Adam Langley98ad22e2014-08-26 13:38:19 -0700171#define OPENSSL_U64(x) x##UI64
172#else
173
174#if defined(OPENSSL_64_BIT)
175#define OPENSSL_U64(x) x##UL
176#else
177#define OPENSSL_U64(x) x##ULL
178#endif
179
Adam Langley46a7ca02015-01-05 16:29:28 -0800180#endif /* defined(_MSC_VER) */
Adam Langley98ad22e2014-08-26 13:38:19 -0700181
Adam Langley3e652652015-01-09 15:44:37 -0800182#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
183 defined(OPENSSL_AARCH64)
David Benjamina70c75c2014-09-11 19:11:15 -0400184/* OPENSSL_cpuid_setup initializes OPENSSL_ia32cap_P. */
185void OPENSSL_cpuid_setup(void);
186#endif
Adam Langley98ad22e2014-08-26 13:38:19 -0700187
Adam Langleyb15d8132014-11-03 18:51:20 -0800188#if !defined(inline)
189#define inline __inline
190#endif
191
192
193/* Constant-time utility functions.
194 *
195 * The following methods return a bitmask of all ones (0xff...f) for true and 0
196 * for false. This is useful for choosing a value based on the result of a
197 * conditional in constant time. For example,
198 *
199 * if (a < b) {
200 * c = a;
201 * } else {
202 * c = b;
203 * }
204 *
205 * can be written as
206 *
207 * unsigned int lt = constant_time_lt(a, b);
Adam Langleya952d962014-11-03 19:15:40 -0800208 * c = constant_time_select(lt, a, b); */
Adam Langleyb15d8132014-11-03 18:51:20 -0800209
210/* constant_time_msb returns the given value with the MSB copied to all the
Adam Langley9ed9dae2014-11-04 11:22:01 -0800211 * other bits. */
Adam Langleyb15d8132014-11-03 18:51:20 -0800212static inline unsigned int constant_time_msb(unsigned int a) {
213 return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1));
214}
215
216/* constant_time_lt returns 0xff..f if a < b and 0 otherwise. */
217static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) {
Adam Langley9ed9dae2014-11-04 11:22:01 -0800218 /* Consider the two cases of the problem:
219 * msb(a) == msb(b): a < b iff the MSB of a - b is set.
220 * msb(a) != msb(b): a < b iff the MSB of b is set.
221 *
222 * If msb(a) == msb(b) then the following evaluates as:
223 * msb(a^((a^b)|((a-b)^a))) ==
224 * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
225 * msb(a^a^(a-b)) == (rearranging)
226 * msb(a-b) (because ∀x. x^x == 0)
227 *
228 * Else, if msb(a) != msb(b) then the following evaluates as:
229 * msb(a^((a^b)|((a-b)^a))) ==
230 * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
231 * represents a value s.t. msb(𝟙) = 1)
232 * msb(a^𝟙) == (because ORing with 1 results in 1)
233 * msb(b)
234 *
235 *
236 * Here is an SMT-LIB verification of this formula:
237 *
238 * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
239 * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
240 * )
241 *
242 * (declare-fun a () (_ BitVec 32))
243 * (declare-fun b () (_ BitVec 32))
244 *
245 * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
246 * (check-sat)
247 * (get-model)
248 */
249 return constant_time_msb(a^((a^b)|((a-b)^a)));
Adam Langleyb15d8132014-11-03 18:51:20 -0800250}
251
252/* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */
253static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) {
254 return (uint8_t)(constant_time_lt(a, b));
255}
256
257/* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */
258static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) {
Adam Langley9ed9dae2014-11-04 11:22:01 -0800259 return ~constant_time_lt(a, b);
Adam Langleyb15d8132014-11-03 18:51:20 -0800260}
261
262/* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */
263static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) {
264 return (uint8_t)(constant_time_ge(a, b));
265}
266
267/* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
268static inline unsigned int constant_time_is_zero(unsigned int a) {
Adam Langley9ed9dae2014-11-04 11:22:01 -0800269 /* Here is an SMT-LIB verification of this formula:
270 *
271 * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
272 * (bvand (bvnot a) (bvsub a #x00000001))
273 * )
274 *
275 * (declare-fun a () (_ BitVec 32))
276 *
277 * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
278 * (check-sat)
279 * (get-model)
280 */
Adam Langleyb15d8132014-11-03 18:51:20 -0800281 return constant_time_msb(~a & (a - 1));
282}
283
284/* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit
285 * mask. */
286static inline uint8_t constant_time_is_zero_8(unsigned int a) {
287 return (uint8_t)(constant_time_is_zero(a));
288}
289
290/* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */
291static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) {
292 return constant_time_is_zero(a ^ b);
293}
294
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800295/* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */
Adam Langleyb15d8132014-11-03 18:51:20 -0800296static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) {
297 return (uint8_t)(constant_time_eq(a, b));
298}
299
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800300/* constant_time_eq_int acts like |constant_time_eq| but works on int values. */
301static inline unsigned int constant_time_eq_int(int a, int b) {
302 return constant_time_eq((unsigned)(a), (unsigned)(b));
303}
304
305/* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
306 * mask. */
307static inline uint8_t constant_time_eq_int_8(int a, int b) {
308 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
309}
310
Adam Langleya952d962014-11-03 19:15:40 -0800311/* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s
312 * or all 0s (as returned by the methods above), the select methods return
313 * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
314static inline unsigned int constant_time_select(unsigned int mask,
315 unsigned int a, unsigned int b) {
316 return (mask & a) | (~mask & b);
317}
318
319/* constant_time_select_8 acts like |constant_time_select| but operates on
320 * 8-bit values. */
321static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
322 uint8_t b) {
323 return (uint8_t)(constant_time_select(mask, a, b));
324}
325
326/* constant_time_select_int acts like |constant_time_select| but operates on
327 * ints. */
328static inline int constant_time_select_int(unsigned int mask, int a, int b) {
329 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
330}
331
Adam Langleyb15d8132014-11-03 18:51:20 -0800332
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700333/* Thread-safe initialisation. */
334
335#if !defined(OPENSSL_WINDOWS)
336typedef pthread_once_t CRYPTO_once_t;
337#define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
338#else
339typedef int32_t CRYPTO_once_t;
340#define CRYPTO_ONCE_INIT 0
341#endif
342
343/* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
344 * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
345 * then they will block until |init| completes, but |init| will have only been
346 * called once.
347 *
348 * The |once| argument must be a |CRYPTO_once_t| that has been initialised with
349 * the value |CRYPTO_ONCE_INIT|. */
David Benjamin4d787182015-04-02 16:34:42 -0400350OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700351
352
Adam Langleydf1f5e72015-04-13 11:04:08 -0700353/* Locks.
354 *
355 * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in
356 * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as
357 * a global lock. A global lock must be initialised to the value
358 * |CRYPTO_STATIC_MUTEX_INIT|.
359 *
360 * |CRYPTO_MUTEX| can appear in public structures and so is defined in
361 * thread.h.
362 *
363 * The global lock is a different type because there's no static initialiser
364 * value on Windows for locks, so global locks have to be coupled with a
365 * |CRYPTO_once_t| to ensure that the lock is setup before use. This is done
366 * automatically by |CRYPTO_STATIC_MUTEX_lock_*|. */
367
368#if !defined(OPENSSL_WINDOWS)
369struct CRYPTO_STATIC_MUTEX {
370 pthread_rwlock_t lock;
371};
372#define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
373#else
374struct CRYPTO_STATIC_MUTEX {
375 CRYPTO_once_t once;
376 CRITICAL_SECTION lock;
377};
378#define CRYPTO_STATIC_MUTEX_INIT { CRYPTO_ONCE_INIT, { 0 } }
379#endif
380
381/* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
382 * |CRYPTO_STATIC_MUTEX|. */
383void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
384
385/* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
386 * read lock, but none may have a write lock. (On Windows, read locks are
387 * actually fully exclusive.) */
388void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
389
390/* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
391 * of lock on it. */
392void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
393
394/* CRYPTO_MUTEX_unlock unlocks |lock|. */
395void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock);
396
397/* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */
398void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
399
400/* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also
401 * have a read lock, but none may have a write lock. The |lock| variable does
402 * not need to be initialised by any function, but must have been statically
403 * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */
404void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock);
405
406/* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has
407 * any type of lock on it. The |lock| variable does not need to be initialised
408 * by any function, but must have been statically initialised with
409 * |CRYPTO_STATIC_MUTEX_INIT|. */
410void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock);
411
412/* CRYPTO_STATIC_MUTEX_unlock unlocks |lock|. */
413void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock);
414
415
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700416/* Thread local storage. */
417
418/* thread_local_data_t enumerates the types of thread-local data that can be
419 * stored. */
420typedef enum {
421 OPENSSL_THREAD_LOCAL_ERR = 0,
Adam Langley310d4dd2015-04-13 11:04:21 -0700422 OPENSSL_THREAD_LOCAL_RAND,
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700423 OPENSSL_THREAD_LOCAL_TEST,
424 NUM_OPENSSL_THREAD_LOCALS,
425} thread_local_data_t;
426
427/* thread_local_destructor_t is the type of a destructor function that will be
428 * called when a thread exits and its thread-local storage needs to be freed. */
429typedef void (*thread_local_destructor_t)(void *);
430
431/* CRYPTO_get_thread_local gets the pointer value that is stored for the
432 * current thread for the given index, or NULL if none has been set. */
David Benjamin4d787182015-04-02 16:34:42 -0400433OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700434
435/* CRYPTO_set_thread_local sets a pointer value for the current thread at the
436 * given index. This function should only be called once per thread for a given
437 * |index|: rather than update the pointer value itself, update the data that
438 * is pointed to.
439 *
440 * The destructor function will be called when a thread exits to free this
441 * thread-local data. All calls to |CRYPTO_set_thread_local| with the same
442 * |index| should have the same |destructor| argument. The destructor may be
443 * called with a NULL argument if a thread that never set a thread-local
444 * pointer for |index|, exits. The destructor may be called concurrently with
445 * different arguments.
446 *
447 * This function returns one on success or zero on error. If it returns zero
448 * then |destructor| has been called with |value| already. */
David Benjamin4d787182015-04-02 16:34:42 -0400449OPENSSL_EXPORT int CRYPTO_set_thread_local(
450 thread_local_data_t index, void *value,
451 thread_local_destructor_t destructor);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700452
453
David Benjamin546f1a52015-04-15 16:46:09 -0400454/* ex_data */
455
456/* CRYPTO_get_ex_new_index allocates a new index for ex_data linked with
457 * objects of the given |class|. This should not be called directly, rather
458 * each class of object should provide a wrapper function that sets
459 * |class_value| correctly.
460 *
461 * The |class_value| argument should be one of |CRYPTO_EX_INDEX_*|.
462 *
463 * TODO(fork): replace the class_value with a pointer to EX_CLASS_ITEM. Saves
464 * having that hash table and some of the lock-bouncing. Maybe have every
465 * module have a private global EX_CLASS_ITEM somewhere and any direct callers
466 * of CRYPTO_{get,set}_ex_data{,_index} would have to always call the
467 * wrappers. */
468OPENSSL_EXPORT int CRYPTO_get_ex_new_index(int class_value, long argl,
469 void *argp, CRYPTO_EX_new *new_func,
470 CRYPTO_EX_dup *dup_func,
471 CRYPTO_EX_free *free_func);
472
473/* CRYPTO_set_ex_data sets an extra data pointer on a given object. This should
474 * not be called directly, rather each class of object should provide a wrapper
475 * function. */
476OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
477
478/* CRYPTO_set_ex_data return an extra data pointer for a given object, or NULL
479 * if no such index exists. This should not be called directly, rather each
480 * class of object should provide a wrapper function. */
481OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
482
483/* CRYPTO_EX_INDEX_* are the built-in classes of objects.
484 *
485 * TODO(fork): WARNING: these are called "INDEX", but they aren't! */
486enum {
487 CRYPTO_EX_INDEX_BIO,
488 CRYPTO_EX_INDEX_SSL,
489 CRYPTO_EX_INDEX_SSL_CTX,
490 CRYPTO_EX_INDEX_SSL_SESSION,
491 CRYPTO_EX_INDEX_X509_STORE,
492 CRYPTO_EX_INDEX_X509_STORE_CTX,
493 CRYPTO_EX_INDEX_RSA,
494 CRYPTO_EX_INDEX_DSA,
495 CRYPTO_EX_INDEX_DH,
496 CRYPTO_EX_INDEX_X509,
497 CRYPTO_EX_INDEX_EC_KEY,
498};
499
500/* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA| which is
501 * embedded inside of |obj| which is of class |class_value|. Returns one on
502 * success and zero otherwise. */
503OPENSSL_EXPORT int CRYPTO_new_ex_data(int class_value, void *obj,
504 CRYPTO_EX_DATA *ad);
505
506/* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated
507 * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given
508 * class. It returns one on success and zero otherwise. */
509OPENSSL_EXPORT int CRYPTO_dup_ex_data(int class_value, CRYPTO_EX_DATA *to,
510 const CRYPTO_EX_DATA *from);
511
512/* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
513 * object of the given class. */
514OPENSSL_EXPORT void CRYPTO_free_ex_data(int class_value, void *obj,
515 CRYPTO_EX_DATA *ad);
516
517
Adam Langley95c29f32014-06-20 12:00:00 -0700518#if defined(__cplusplus)
519} /* extern C */
520#endif
521
522#endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */