Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2017, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #ifndef OPENSSL_HEADER_FIPSMODULE_DELOCATE_H |
| 16 | #define OPENSSL_HEADER_FIPSMODULE_DELOCATE_H |
| 17 | |
| 18 | #include <openssl/base.h> |
| 19 | |
| 20 | #include "../internal.h" |
| 21 | |
| 22 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 23 | #if defined(BORINGSSL_FIPS) && !defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN) |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 24 | #define DEFINE_BSS_GET(type, name) \ |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 25 | static type name __attribute__((used)); \ |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 26 | type *name##_bss_get(void) __attribute__((const)); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 27 | // For FIPS builds we require that CRYPTO_ONCE_INIT be zero. |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 28 | #define DEFINE_STATIC_ONCE(name) DEFINE_BSS_GET(CRYPTO_once_t, name) |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 29 | // For FIPS builds we require that CRYPTO_STATIC_MUTEX_INIT be zero. |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 30 | #define DEFINE_STATIC_MUTEX(name) \ |
| 31 | DEFINE_BSS_GET(struct CRYPTO_STATIC_MUTEX, name) |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 32 | // For FIPS builds we require that CRYPTO_EX_DATA_CLASS_INIT be zero. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 33 | #define DEFINE_STATIC_EX_DATA_CLASS(name) \ |
| 34 | DEFINE_BSS_GET(CRYPTO_EX_DATA_CLASS, name) |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 35 | #else |
| 36 | #define DEFINE_BSS_GET(type, name) \ |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 37 | static type name; \ |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 38 | static type *name##_bss_get(void) { return &name; } |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 39 | #define DEFINE_STATIC_ONCE(name) \ |
| 40 | static CRYPTO_once_t name = CRYPTO_ONCE_INIT; \ |
| 41 | static CRYPTO_once_t *name##_bss_get(void) { return &name; } |
| 42 | #define DEFINE_STATIC_MUTEX(name) \ |
| 43 | static struct CRYPTO_STATIC_MUTEX name = CRYPTO_STATIC_MUTEX_INIT; \ |
| 44 | static struct CRYPTO_STATIC_MUTEX *name##_bss_get(void) { return &name; } |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 45 | #define DEFINE_STATIC_EX_DATA_CLASS(name) \ |
| 46 | static CRYPTO_EX_DATA_CLASS name = CRYPTO_EX_DATA_CLASS_INIT; \ |
| 47 | static CRYPTO_EX_DATA_CLASS *name##_bss_get(void) { return &name; } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 48 | #endif |
| 49 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 50 | #define DEFINE_DATA(type, name, accessor_decorations) \ |
| 51 | DEFINE_BSS_GET(type, name##_storage) \ |
| 52 | DEFINE_STATIC_ONCE(name##_once) \ |
| 53 | static void name##_do_init(type *out); \ |
| 54 | static void name##_init(void) { name##_do_init(name##_storage_bss_get()); } \ |
| 55 | accessor_decorations type *name(void) { \ |
| 56 | CRYPTO_once(name##_once_bss_get(), name##_init); \ |
| 57 | /* See http://c-faq.com/ansi/constmismatch.html for why the following \ |
| 58 | * cast is needed. */ \ |
| 59 | return (const type *)name##_storage_bss_get(); \ |
| 60 | } \ |
| 61 | static void name##_do_init(type *out) |
| 62 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 63 | // DEFINE_METHOD_FUNCTION defines a function named |name| which returns a |
| 64 | // method table of type const |type|*. In FIPS mode, to avoid rel.ro data, it |
| 65 | // is split into a CRYPTO_once_t-guarded initializer in the module and |
| 66 | // unhashed, non-module accessor functions to space reserved in the BSS. The |
| 67 | // method table is initialized by a caller-supplied function which takes a |
| 68 | // parameter named |out| of type |type|*. The caller should follow the macro |
| 69 | // invocation with the body of this function: |
| 70 | // |
| 71 | // DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md4) { |
| 72 | // out->type = NID_md4; |
| 73 | // out->md_size = MD4_DIGEST_LENGTH; |
| 74 | // out->flags = 0; |
| 75 | // out->init = md4_init; |
| 76 | // out->update = md4_update; |
| 77 | // out->final = md4_final; |
| 78 | // out->block_size = 64; |
| 79 | // out->ctx_size = sizeof(MD4_CTX); |
| 80 | // } |
| 81 | // |
| 82 | // This mechanism does not use a static initializer because their execution |
| 83 | // order is undefined. See FIPS.md for more details. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 84 | #define DEFINE_METHOD_FUNCTION(type, name) DEFINE_DATA(type, name, const) |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 85 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 86 | #define DEFINE_LOCAL_DATA(type, name) DEFINE_DATA(type, name, static const) |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 87 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 88 | #endif // OPENSSL_HEADER_FIPSMODULE_DELOCATE_H |