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