| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_FIPSMODULE_DELOCATE_H |
| 5 |
#define OPENSSL_HEADER_FIPSMODULE_DELOCATE_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include "../internal.h" |
| 10 |
|
| 11 |
|
| 12 |
#if !defined(BORINGSSL_SHARED_LIBRARY) && defined(BORINGSSL_FIPS) && \ |
| 13 |
!defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN) |
| 14 |
#define DEFINE_BSS_GET(type, name) \ |
| 15 |
static type name __attribute__((used)); \ |
| 16 |
type *name##_bss_get(void) __attribute__((const)); |
| 17 |
// For FIPS builds we require that CRYPTO_ONCE_INIT be zero. |
| 18 |
#define DEFINE_STATIC_ONCE(name) DEFINE_BSS_GET(CRYPTO_once_t, name) |
| 19 |
// For FIPS builds we require that CRYPTO_STATIC_MUTEX_INIT be zero. |
| 20 |
#define DEFINE_STATIC_MUTEX(name) \ |
| 21 |
DEFINE_BSS_GET(struct CRYPTO_STATIC_MUTEX, name) |
| 22 |
// For FIPS builds we require that CRYPTO_EX_DATA_CLASS_INIT be zero. |
| 23 |
#define DEFINE_STATIC_EX_DATA_CLASS(name) \ |
| 24 |
DEFINE_BSS_GET(CRYPTO_EX_DATA_CLASS, name) |
| 25 |
#else |
| 26 |
#define DEFINE_BSS_GET(type, name) \ |
| 27 |
static type name; \ |
| 28 |
static type *name##_bss_get(void) { return &name; } |
| 29 |
#define DEFINE_STATIC_ONCE(name) \ |
| 30 |
static CRYPTO_once_t name = CRYPTO_ONCE_INIT; \ |
| 31 |
static CRYPTO_once_t *name##_bss_get(void) { return &name; } |
| 32 |
#define DEFINE_STATIC_MUTEX(name) \ |
| 33 |
static struct CRYPTO_STATIC_MUTEX name = CRYPTO_STATIC_MUTEX_INIT; \ |
| 34 |
static struct CRYPTO_STATIC_MUTEX *name##_bss_get(void) { return &name; } |
| 35 |
#define DEFINE_STATIC_EX_DATA_CLASS(name) \ |
| 36 |
static CRYPTO_EX_DATA_CLASS name = CRYPTO_EX_DATA_CLASS_INIT; \ |
| 37 |
static CRYPTO_EX_DATA_CLASS *name##_bss_get(void) { return &name; } |
| 38 |
#endif |
| 39 |
|
| 40 |
#define DEFINE_DATA(type, name, accessor_decorations) \ |
| 41 |
DEFINE_BSS_GET(type, name##_storage) \ |
| 42 |
DEFINE_STATIC_ONCE(name##_once) \ |
| 43 |
static void name##_do_init(type *out); \ |
| 44 |
static void name##_init(void) { name##_do_init(name##_storage_bss_get()); } \ |
| 45 |
accessor_decorations type *name(void) { \ |
| 46 |
CRYPTO_once(name##_once_bss_get(), name##_init); \ |
| 47 |
/* See http://c-faq.com/ansi/constmismatch.html for why the following \ |
| 48 |
* cast is needed. */ \ |
| 49 |
return (const type *)name##_storage_bss_get(); \ |
| 50 |
} \ |
| 51 |
static void name##_do_init(type *out) |
| 52 |
|
| 53 |
// DEFINE_METHOD_FUNCTION defines a function named |name| which returns a |
| 54 |
// method table of type const |type|*. In FIPS mode, to avoid rel.ro data, it |
| 55 |
// is split into a CRYPTO_once_t-guarded initializer in the module and |
| 56 |
// unhashed, non-module accessor functions to space reserved in the BSS. The |
| 57 |
// method table is initialized by a caller-supplied function which takes a |
| 58 |
// parameter named |out| of type |type|*. The caller should follow the macro |
| 59 |
// invocation with the body of this function: |
| 60 |
// |
| 61 |
// DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md4) { |
| 62 |
// out->type = NID_md4; |
| 63 |
// out->md_size = MD4_DIGEST_LENGTH; |
| 64 |
// out->flags = 0; |
| 65 |
// out->init = md4_init; |
| 66 |
// out->update = md4_update; |
| 67 |
// out->final = md4_final; |
| 68 |
// out->block_size = 64; |
| 69 |
// out->ctx_size = sizeof(MD4_CTX); |
| 70 |
// } |
| 71 |
// |
| 72 |
// This mechanism does not use a static initializer because their execution |
| 73 |
// order is undefined. See FIPS.md for more details. |
| 74 |
#define DEFINE_METHOD_FUNCTION(type, name) DEFINE_DATA(type, name, const) |
| 75 |
|
| 76 |
#define DEFINE_LOCAL_DATA(type, name) DEFINE_DATA(type, name, static const) |
| 77 |
|
| 78 |
#endif // OPENSSL_HEADER_FIPSMODULE_DELOCATE_H |