Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014, 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 | #include <openssl/crypto.h> |
| 16 | |
| 17 | #include <openssl/cpu.h> |
| 18 | |
| 19 | #include "internal.h" |
| 20 | |
| 21 | |
Andrew Top | 193dc3d | 2019-01-23 09:57:23 -0800 | [diff] [blame] | 22 | #if defined(OPENSSL_MSAN) && !defined(OPENSSL_NO_ASM) |
| 23 | // MSan works by instrumenting memory accesses in the compiler. Accesses from |
| 24 | // uninstrumented code, such as assembly, are invisible to it. MSan will |
| 25 | // incorrectly report reads from assembly-initialized memory as uninitialized. |
| 26 | // If building BoringSSL with MSan, exclude assembly files from the build and |
| 27 | // define OPENSSL_NO_ASM. |
| 28 | // |
| 29 | // This is checked here rather than in a header because the consumer might not |
| 30 | // define OPENSSL_NO_ASM. It is only necessary for BoringSSL source files to be |
| 31 | // built with it. |
| 32 | #error "BoringSSL must be built with assembly disabled to use MSan." |
| 33 | #endif |
| 34 | |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 35 | #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \ |
| 36 | (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \ |
| 37 | defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \ |
| 38 | defined(OPENSSL_PPC64LE)) |
| 39 | // x86, x86_64, the ARMs and ppc64le need to record the result of a |
| 40 | // cpuid/getauxval call for the asm to work correctly, unless compiled without |
| 41 | // asm code. |
| 42 | #define NEED_CPUID |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | // Otherwise, don't emit a static initialiser. |
| 47 | |
| 48 | #if !defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 49 | #define BORINGSSL_NO_STATIC_INITIALIZER |
| 50 | #endif |
| 51 | |
| 52 | #endif /* !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64 || |
| 53 | OPENSSL_ARM || OPENSSL_AARCH64) */ |
| 54 | |
| 55 | |
| 56 | // Our assembly does not use the GOT to reference symbols, which means |
| 57 | // references to visible symbols will often require a TEXTREL. This is |
| 58 | // undesirable, so all assembly-referenced symbols should be hidden. CPU |
| 59 | // capabilities are the only such symbols defined in C. Explicitly hide them, |
| 60 | // rather than rely on being built with -fvisibility=hidden. |
| 61 | #if defined(OPENSSL_WINDOWS) |
| 62 | #define HIDDEN |
| 63 | #else |
| 64 | #define HIDDEN __attribute__((visibility("hidden"))) |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | // The capability variables are defined in this file in order to work around a |
| 69 | // linker bug. When linking with a .a, if no symbols in a .o are referenced |
| 70 | // then the .o is discarded, even if it has constructor functions. |
| 71 | // |
| 72 | // This still means that any binaries that don't include some functionality |
| 73 | // that tests the capability values will still skip the constructor but, so |
| 74 | // far, the init constructor function only sets the capability variables. |
| 75 | |
| 76 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 77 | |
| 78 | // This value must be explicitly initialised to zero in order to work around a |
| 79 | // bug in libtool or the linker on OS X. |
| 80 | // |
| 81 | // If not initialised then it becomes a "common symbol". When put into an |
| 82 | // archive, linking on OS X will fail to resolve common symbols. By |
| 83 | // initialising it to zero, it becomes a "data symbol", which isn't so |
| 84 | // affected. |
| 85 | HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0}; |
| 86 | |
| 87 | #elif defined(OPENSSL_PPC64LE) |
| 88 | |
| 89 | HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0; |
| 90 | |
| 91 | #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 92 | |
| 93 | #include <openssl/arm_arch.h> |
| 94 | |
| 95 | #if defined(OPENSSL_STATIC_ARMCAP) |
| 96 | |
| 97 | HIDDEN uint32_t OPENSSL_armcap_P = |
| 98 | #if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON__) |
| 99 | ARMV7_NEON | |
| 100 | #endif |
| 101 | #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO) |
| 102 | ARMV8_AES | |
| 103 | #endif |
| 104 | #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO) |
| 105 | ARMV8_SHA1 | |
| 106 | #endif |
| 107 | #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO) |
| 108 | ARMV8_SHA256 | |
| 109 | #endif |
| 110 | #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO) |
| 111 | ARMV8_PMULL | |
| 112 | #endif |
| 113 | 0; |
| 114 | |
| 115 | #else |
| 116 | HIDDEN uint32_t OPENSSL_armcap_P = 0; |
| 117 | #endif |
| 118 | |
| 119 | #endif |
| 120 | |
| 121 | #if defined(BORINGSSL_FIPS) |
| 122 | // In FIPS mode, the power-on self-test function calls |CRYPTO_library_init| |
| 123 | // because we have to ensure that CPUID detection occurs first. |
| 124 | #define BORINGSSL_NO_STATIC_INITIALIZER |
| 125 | #endif |
| 126 | |
| 127 | #if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 128 | #define OPENSSL_CDECL __cdecl |
| 129 | #else |
| 130 | #define OPENSSL_CDECL |
| 131 | #endif |
| 132 | |
| 133 | #if defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 134 | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
| 135 | #elif defined(_MSC_VER) |
| 136 | #pragma section(".CRT$XCU", read) |
| 137 | static void __cdecl do_library_init(void); |
| 138 | __declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) = |
| 139 | do_library_init; |
| 140 | #else |
| 141 | static void do_library_init(void) __attribute__ ((constructor)); |
| 142 | #endif |
| 143 | |
| 144 | // do_library_init is the actual initialization function. If |
| 145 | // BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static |
| 146 | // initializer. Otherwise, it is called by CRYPTO_library_init. |
| 147 | static void OPENSSL_CDECL do_library_init(void) { |
| 148 | // WARNING: this function may only configure the capability variables. See the |
| 149 | // note above about the linker bug. |
| 150 | #if defined(NEED_CPUID) |
Kaido Kert | f309f9a | 2021-04-30 12:09:15 -0700 | [diff] [blame] | 151 | #if defined(STARBOARD) |
Kaido Kert | b1d4450 | 2019-07-29 09:58:25 -0700 | [diff] [blame] | 152 | OPENSSL_cpuid_setup_starboard(); |
| 153 | #else |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 154 | OPENSSL_cpuid_setup(); |
| 155 | #endif |
Kaido Kert | b1d4450 | 2019-07-29 09:58:25 -0700 | [diff] [blame] | 156 | #endif |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void CRYPTO_library_init(void) { |
| 160 | // TODO(davidben): It would be tidier if this build knob could be replaced |
| 161 | // with an internal lazy-init mechanism that would handle things correctly |
| 162 | // in-library. https://crbug.com/542879 |
| 163 | #if defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 164 | CRYPTO_once(&once, do_library_init); |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | int CRYPTO_is_confidential_build(void) { |
| 169 | #if defined(BORINGSSL_CONFIDENTIAL) |
| 170 | return 1; |
| 171 | #else |
| 172 | return 0; |
| 173 | #endif |
| 174 | } |
| 175 | |
| 176 | int CRYPTO_has_asm(void) { |
| 177 | #if defined(OPENSSL_NO_ASM) |
| 178 | return 0; |
| 179 | #else |
| 180 | return 1; |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | const char *SSLeay_version(int which) { return OpenSSL_version(which); } |
| 185 | |
| 186 | const char *OpenSSL_version(int which) { |
| 187 | switch (which) { |
| 188 | case OPENSSL_VERSION: |
| 189 | return "BoringSSL"; |
| 190 | case OPENSSL_CFLAGS: |
| 191 | return "compiler: n/a"; |
| 192 | case OPENSSL_BUILT_ON: |
| 193 | return "built on: n/a"; |
| 194 | case OPENSSL_PLATFORM: |
| 195 | return "platform: n/a"; |
| 196 | case OPENSSL_DIR: |
| 197 | return "OPENSSLDIR: n/a"; |
| 198 | default: |
| 199 | return "not available"; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; } |
| 204 | |
| 205 | unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; } |
| 206 | |
| 207 | int CRYPTO_malloc_init(void) { return 1; } |
| 208 | |
| 209 | int OPENSSL_malloc_init(void) { return 1; } |
| 210 | |
| 211 | void ENGINE_load_builtin_engines(void) {} |
| 212 | |
| 213 | int ENGINE_register_all_complete(void) { return 1; } |
| 214 | |
| 215 | void OPENSSL_load_builtin_modules(void) {} |
| 216 | |
| 217 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
| 218 | CRYPTO_library_init(); |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | void OPENSSL_cleanup(void) {} |