David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [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 | |
David Benjamin | 20c3731 | 2015-11-11 21:33:18 -0800 | [diff] [blame] | 17 | #include <openssl/cpu.h> |
| 18 | |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 19 | #include "internal.h" |
| 20 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 21 | |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 22 | #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \ |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 23 | (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \ |
Adam Langley | 4467e59 | 2016-09-23 12:47:24 -0700 | [diff] [blame] | 24 | defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \ |
| 25 | defined(OPENSSL_PPC64LE)) |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 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. |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 29 | #define NEED_CPUID |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 30 | |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 31 | #else |
| 32 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 33 | // Otherwise, don't emit a static initialiser. |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 34 | |
| 35 | #if !defined(BORINGSSL_NO_STATIC_INITIALIZER) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 36 | #define BORINGSSL_NO_STATIC_INITIALIZER |
| 37 | #endif |
| 38 | |
Adam Langley | c161571 | 2018-11-27 14:07:12 -0800 | [diff] [blame] | 39 | #endif // !NO_ASM && !STATIC_ARMCAP && |
| 40 | // (X86 || X86_64 || ARM || AARCH64 || PPC64LE) |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 41 | |
| 42 | |
David Benjamin | 38636ab | 2017-10-18 22:23:09 -0400 | [diff] [blame] | 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 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 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. |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 62 | |
David Benjamin | 0de64a7 | 2019-09-26 15:59:40 -0400 | [diff] [blame] | 63 | #if defined(BORINGSSL_DISPATCH_TEST) |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 64 | // This value must be explicitly initialised to zero in order to work around a |
| 65 | // bug in libtool or the linker on OS X. |
| 66 | // |
| 67 | // If not initialised then it becomes a "common symbol". When put into an |
| 68 | // archive, linking on OS X will fail to resolve common symbols. By |
| 69 | // initialising it to zero, it becomes a "data symbol", which isn't so |
| 70 | // affected. |
Adam Langley | c161571 | 2018-11-27 14:07:12 -0800 | [diff] [blame] | 71 | HIDDEN uint8_t BORINGSSL_function_hit[7] = {0}; |
| 72 | #endif |
| 73 | |
| 74 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 75 | |
| 76 | // This value must be explicitly initialized to zero. See similar comment above. |
David Benjamin | 38636ab | 2017-10-18 22:23:09 -0400 | [diff] [blame] | 77 | HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0}; |
Adam Langley | b18cb6a | 2017-04-04 11:08:28 -0700 | [diff] [blame] | 78 | |
| 79 | #elif defined(OPENSSL_PPC64LE) |
| 80 | |
David Benjamin | 38636ab | 2017-10-18 22:23:09 -0400 | [diff] [blame] | 81 | HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0; |
Adam Langley | b18cb6a | 2017-04-04 11:08:28 -0700 | [diff] [blame] | 82 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 83 | #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 84 | |
Adam Langley | 73415b6 | 2015-08-24 18:03:17 -0700 | [diff] [blame] | 85 | #include <openssl/arm_arch.h> |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 86 | |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 87 | #if defined(OPENSSL_STATIC_ARMCAP) |
| 88 | |
David Benjamin | 38636ab | 2017-10-18 22:23:09 -0400 | [diff] [blame] | 89 | HIDDEN uint32_t OPENSSL_armcap_P = |
Adam Langley | 2745ef9 | 2018-11-12 11:57:32 -0800 | [diff] [blame] | 90 | #if defined(OPENSSL_STATIC_ARMCAP_NEON) || \ |
| 91 | (defined(__ARM_NEON__) || defined(__ARM_NEON)) |
David Benjamin | 8500390 | 2016-02-23 18:04:15 -0500 | [diff] [blame] | 92 | ARMV7_NEON | |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 93 | #endif |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 94 | #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO) |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 95 | ARMV8_AES | |
| 96 | #endif |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 97 | #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO) |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 98 | ARMV8_SHA1 | |
| 99 | #endif |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 100 | #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO) |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 101 | ARMV8_SHA256 | |
| 102 | #endif |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 103 | #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO) |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 104 | ARMV8_PMULL | |
| 105 | #endif |
| 106 | 0; |
| 107 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 108 | #else |
David Benjamin | 38636ab | 2017-10-18 22:23:09 -0400 | [diff] [blame] | 109 | HIDDEN uint32_t OPENSSL_armcap_P = 0; |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 110 | |
| 111 | uint32_t *OPENSSL_get_armcap_pointer_for_test(void) { |
| 112 | return &OPENSSL_armcap_P; |
| 113 | } |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 114 | #endif |
| 115 | |
| 116 | #endif |
| 117 | |
Adam Langley | fd49993 | 2017-04-04 14:21:43 -0700 | [diff] [blame] | 118 | #if defined(BORINGSSL_FIPS) |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 119 | // In FIPS mode, the power-on self-test function calls |CRYPTO_library_init| |
| 120 | // because we have to ensure that CPUID detection occurs first. |
Adam Langley | fd49993 | 2017-04-04 14:21:43 -0700 | [diff] [blame] | 121 | #define BORINGSSL_NO_STATIC_INITIALIZER |
| 122 | #endif |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 123 | |
David Benjamin | 93a5b44 | 2015-11-19 11:39:46 -0500 | [diff] [blame] | 124 | #if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 125 | #define OPENSSL_CDECL __cdecl |
| 126 | #else |
| 127 | #define OPENSSL_CDECL |
| 128 | #endif |
| 129 | |
David Benjamin | 93a5b44 | 2015-11-19 11:39:46 -0500 | [diff] [blame] | 130 | #if defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 131 | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
David Benjamin | e2568c4 | 2017-08-16 15:25:27 -0400 | [diff] [blame] | 132 | #elif defined(_MSC_VER) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 133 | #pragma section(".CRT$XCU", read) |
| 134 | static void __cdecl do_library_init(void); |
| 135 | __declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) = |
| 136 | do_library_init; |
David Benjamin | 93a5b44 | 2015-11-19 11:39:46 -0500 | [diff] [blame] | 137 | #else |
| 138 | static void do_library_init(void) __attribute__ ((constructor)); |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 139 | #endif |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 140 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 141 | // do_library_init is the actual initialization function. If |
| 142 | // BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static |
| 143 | // initializer. Otherwise, it is called by CRYPTO_library_init. |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 144 | static void OPENSSL_CDECL do_library_init(void) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 145 | // WARNING: this function may only configure the capability variables. See the |
| 146 | // note above about the linker bug. |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 147 | #if defined(NEED_CPUID) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 148 | OPENSSL_cpuid_setup(); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | void CRYPTO_library_init(void) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 153 | // TODO(davidben): It would be tidier if this build knob could be replaced |
| 154 | // with an internal lazy-init mechanism that would handle things correctly |
| 155 | // in-library. https://crbug.com/542879 |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 156 | #if defined(BORINGSSL_NO_STATIC_INITIALIZER) |
David Benjamin | 93a5b44 | 2015-11-19 11:39:46 -0500 | [diff] [blame] | 157 | CRYPTO_once(&once, do_library_init); |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 158 | #endif |
| 159 | } |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 160 | |
Adam Langley | b83c680 | 2016-05-03 09:16:21 -0700 | [diff] [blame] | 161 | int CRYPTO_is_confidential_build(void) { |
| 162 | #if defined(BORINGSSL_CONFIDENTIAL) |
| 163 | return 1; |
| 164 | #else |
| 165 | return 0; |
| 166 | #endif |
| 167 | } |
| 168 | |
Adam Langley | 4fac8d0 | 2016-05-16 13:44:40 -0700 | [diff] [blame] | 169 | int CRYPTO_has_asm(void) { |
| 170 | #if defined(OPENSSL_NO_ASM) |
| 171 | return 0; |
| 172 | #else |
| 173 | return 1; |
| 174 | #endif |
| 175 | } |
| 176 | |
David Benjamin | 5d626b2 | 2018-05-08 16:07:00 -0400 | [diff] [blame] | 177 | const char *SSLeay_version(int which) { return OpenSSL_version(which); } |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 178 | |
David Benjamin | 5d626b2 | 2018-05-08 16:07:00 -0400 | [diff] [blame] | 179 | const char *OpenSSL_version(int which) { |
David Benjamin | d6e31f6 | 2018-05-15 13:37:53 -0400 | [diff] [blame] | 180 | switch (which) { |
| 181 | case OPENSSL_VERSION: |
| 182 | return "BoringSSL"; |
| 183 | case OPENSSL_CFLAGS: |
| 184 | return "compiler: n/a"; |
| 185 | case OPENSSL_BUILT_ON: |
| 186 | return "built on: n/a"; |
| 187 | case OPENSSL_PLATFORM: |
| 188 | return "platform: n/a"; |
| 189 | case OPENSSL_DIR: |
| 190 | return "OPENSSLDIR: n/a"; |
| 191 | default: |
| 192 | return "not available"; |
David Benjamin | 5d626b2 | 2018-05-08 16:07:00 -0400 | [diff] [blame] | 193 | } |
David Benjamin | 5d626b2 | 2018-05-08 16:07:00 -0400 | [diff] [blame] | 194 | } |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 195 | |
David Benjamin | 27e4c3b | 2018-04-12 18:31:36 -0400 | [diff] [blame] | 196 | unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; } |
Adam Langley | 05ee4fd | 2015-08-02 09:35:44 -0700 | [diff] [blame] | 197 | |
David Benjamin | 27e4c3b | 2018-04-12 18:31:36 -0400 | [diff] [blame] | 198 | unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; } |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 199 | |
David Benjamin | 27e4c3b | 2018-04-12 18:31:36 -0400 | [diff] [blame] | 200 | int CRYPTO_malloc_init(void) { return 1; } |
| 201 | |
| 202 | int OPENSSL_malloc_init(void) { return 1; } |
Adam Langley | 05ee4fd | 2015-08-02 09:35:44 -0700 | [diff] [blame] | 203 | |
| 204 | void ENGINE_load_builtin_engines(void) {} |
David Benjamin | e5aa791 | 2016-01-26 01:09:19 -0500 | [diff] [blame] | 205 | |
David Benjamin | 27e4c3b | 2018-04-12 18:31:36 -0400 | [diff] [blame] | 206 | int ENGINE_register_all_complete(void) { return 1; } |
Adam Langley | 27516f7 | 2016-07-12 10:26:56 -0700 | [diff] [blame] | 207 | |
David Benjamin | e5aa791 | 2016-01-26 01:09:19 -0500 | [diff] [blame] | 208 | void OPENSSL_load_builtin_modules(void) {} |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 209 | |
| 210 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
| 211 | CRYPTO_library_init(); |
| 212 | return 1; |
| 213 | } |
David Benjamin | bc3286b | 2018-08-13 17:52:48 -0500 | [diff] [blame] | 214 | |
| 215 | void OPENSSL_cleanup(void) {} |