blob: c86206a000a80590c7923bf88b18c272cc6365b8 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Adam Langley4139edb2016-01-13 15:00:54 -080017#include <openssl/cpu.h>
18
Adam Langleyd9e397b2015-01-22 14:27:53 -080019#include "internal.h"
20
21
Kenny Roote99801b2015-11-06 15:31:15 -080022#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
Adam Langleyd9e397b2015-01-22 14:27:53 -080023 (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
Steven Valdezbb1ceac2016-10-07 10:34:51 -040024 defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
25 defined(OPENSSL_PPC64LE))
Robert Sloan8f860b12017-08-28 07:37:06 -070026// 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 Langleyd9e397b2015-01-22 14:27:53 -080029#define NEED_CPUID
30
31#else
32
Robert Sloan8f860b12017-08-28 07:37:06 -070033// Otherwise, don't emit a static initialiser.
Adam Langleyd9e397b2015-01-22 14:27:53 -080034
35#if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
36#define BORINGSSL_NO_STATIC_INITIALIZER
37#endif
38
Robert Sloan4c22c5f2019-03-01 15:53:37 -080039#endif // !NO_ASM && !STATIC_ARMCAP &&
40 // (X86 || X86_64 || ARM || AARCH64 || PPC64LE)
Adam Langleyd9e397b2015-01-22 14:27:53 -080041
42
Robert Sloan29c1d2c2017-10-30 14:10:28 -070043// 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
Robert Sloan8f860b12017-08-28 07:37:06 -070055// 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 Langleyd9e397b2015-01-22 14:27:53 -080062
Robert Sloan4c22c5f2019-03-01 15:53:37 -080063#if !defined(NDEBUG) && !defined(BORINGSSL_FIPS)
Robert Sloan8f860b12017-08-28 07:37:06 -070064// 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.
Robert Sloan4c22c5f2019-03-01 15:53:37 -080071HIDDEN 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.
Robert Sloan29c1d2c2017-10-30 14:10:28 -070077HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0};
Robert Sloan572a4e22017-04-17 10:52:19 -070078
79#elif defined(OPENSSL_PPC64LE)
80
Robert Sloan29c1d2c2017-10-30 14:10:28 -070081HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0;
Robert Sloan572a4e22017-04-17 10:52:19 -070082
Adam Langleyd9e397b2015-01-22 14:27:53 -080083#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
84
Kenny Rootb8494592015-09-25 02:29:14 +000085#include <openssl/arm_arch.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080086
Kenny Roote99801b2015-11-06 15:31:15 -080087#if defined(OPENSSL_STATIC_ARMCAP)
88
Robert Sloan29c1d2c2017-10-30 14:10:28 -070089HIDDEN uint32_t OPENSSL_armcap_P =
Robert Sloanc9abfe42018-11-26 12:19:07 -080090#if defined(OPENSSL_STATIC_ARMCAP_NEON) || \
91 (defined(__ARM_NEON__) || defined(__ARM_NEON))
David Benjamin4969cc92016-04-22 15:02:23 -040092 ARMV7_NEON |
Kenny Roote99801b2015-11-06 15:31:15 -080093#endif
Robert Sloan8ff03552017-06-14 12:40:58 -070094#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -080095 ARMV8_AES |
96#endif
Robert Sloan8ff03552017-06-14 12:40:58 -070097#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -080098 ARMV8_SHA1 |
99#endif
Robert Sloan8ff03552017-06-14 12:40:58 -0700100#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -0800101 ARMV8_SHA256 |
102#endif
Robert Sloan8ff03552017-06-14 12:40:58 -0700103#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -0800104 ARMV8_PMULL |
105#endif
106 0;
107
Adam Langleyd9e397b2015-01-22 14:27:53 -0800108#else
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700109HIDDEN uint32_t OPENSSL_armcap_P = 0;
Robert Sloanc9abfe42018-11-26 12:19:07 -0800110
111uint32_t *OPENSSL_get_armcap_pointer_for_test(void) {
112 return &OPENSSL_armcap_P;
113}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114#endif
115
116#endif
117
Robert Sloan572a4e22017-04-17 10:52:19 -0700118#if defined(BORINGSSL_FIPS)
Robert Sloan8f860b12017-08-28 07:37:06 -0700119// In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
120// because we have to ensure that CPUID detection occurs first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700121#define BORINGSSL_NO_STATIC_INITIALIZER
122#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123
Adam Langley4139edb2016-01-13 15:00:54 -0800124#if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125#define OPENSSL_CDECL __cdecl
126#else
127#define OPENSSL_CDECL
128#endif
129
Adam Langley4139edb2016-01-13 15:00:54 -0800130#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
131static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
Robert Sloan8f860b12017-08-28 07:37:06 -0700132#elif defined(_MSC_VER)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800133#pragma section(".CRT$XCU", read)
134static void __cdecl do_library_init(void);
135__declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
136 do_library_init;
Adam Langley4139edb2016-01-13 15:00:54 -0800137#else
138static void do_library_init(void) __attribute__ ((constructor));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140
Robert Sloan8f860b12017-08-28 07:37:06 -0700141// 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.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144static void OPENSSL_CDECL do_library_init(void) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700145 // WARNING: this function may only configure the capability variables. See the
146 // note above about the linker bug.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800147#if defined(NEED_CPUID)
148 OPENSSL_cpuid_setup();
149#endif
150}
151
152void CRYPTO_library_init(void) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700153 // 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
Adam Langleyd9e397b2015-01-22 14:27:53 -0800156#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
Adam Langley4139edb2016-01-13 15:00:54 -0800157 CRYPTO_once(&once, do_library_init);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800158#endif
159}
Adam Langleyf40f42d2015-03-24 18:25:20 -0700160
David Benjamin9aaebef2016-04-22 15:02:23 -0400161int CRYPTO_is_confidential_build(void) {
162#if defined(BORINGSSL_CONFIDENTIAL)
163 return 1;
164#else
165 return 0;
166#endif
167}
168
David Benjamind316cba2016-06-02 16:17:39 -0400169int CRYPTO_has_asm(void) {
170#if defined(OPENSSL_NO_ASM)
171 return 0;
172#else
173 return 1;
174#endif
175}
176
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100177const char *SSLeay_version(int which) { return OpenSSL_version(which); }
Adam Langleyf40f42d2015-03-24 18:25:20 -0700178
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100179const char *OpenSSL_version(int which) {
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";
193 }
194}
Robert Sloan4562e9d2017-10-02 10:26:51 -0700195
Robert Sloan15c0b352018-04-16 08:36:46 -0700196unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; }
Kenny Roote99801b2015-11-06 15:31:15 -0800197
Robert Sloan15c0b352018-04-16 08:36:46 -0700198unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; }
Robert Sloan4562e9d2017-10-02 10:26:51 -0700199
Robert Sloan15c0b352018-04-16 08:36:46 -0700200int CRYPTO_malloc_init(void) { return 1; }
201
202int OPENSSL_malloc_init(void) { return 1; }
Kenny Roote99801b2015-11-06 15:31:15 -0800203
204void ENGINE_load_builtin_engines(void) {}
David Benjamin4969cc92016-04-22 15:02:23 -0400205
Robert Sloan15c0b352018-04-16 08:36:46 -0700206int ENGINE_register_all_complete(void) { return 1; }
David Benjaminc895d6b2016-08-11 13:26:41 -0400207
David Benjamin4969cc92016-04-22 15:02:23 -0400208void OPENSSL_load_builtin_modules(void) {}
Robert Sloan4562e9d2017-10-02 10:26:51 -0700209
210int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
211 CRYPTO_library_init();
212 return 1;
213}
Robert Sloand9e572d2018-08-27 12:27:00 -0700214
215void OPENSSL_cleanup(void) {}