blob: aee35210bcd8c5ec491ae87a55cb11c4e587d416 [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
39#endif /* !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64 ||
40 OPENSSL_ARM || OPENSSL_AARCH64) */
41
42
Robert Sloan8f860b12017-08-28 07:37:06 -070043// The capability variables are defined in this file in order to work around a
44// linker bug. When linking with a .a, if no symbols in a .o are referenced
45// then the .o is discarded, even if it has constructor functions.
46//
47// This still means that any binaries that don't include some functionality
48// that tests the capability values will still skip the constructor but, so
49// far, the init constructor function only sets the capability variables.
Adam Langleyd9e397b2015-01-22 14:27:53 -080050
51#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
Robert Sloan572a4e22017-04-17 10:52:19 -070052
Robert Sloan8f860b12017-08-28 07:37:06 -070053// This value must be explicitly initialised to zero in order to work around a
54// bug in libtool or the linker on OS X.
55//
56// If not initialised then it becomes a "common symbol". When put into an
57// archive, linking on OS X will fail to resolve common symbols. By
58// initialising it to zero, it becomes a "data symbol", which isn't so
59// affected.
Adam Langleyd9e397b2015-01-22 14:27:53 -080060uint32_t OPENSSL_ia32cap_P[4] = {0};
Robert Sloan572a4e22017-04-17 10:52:19 -070061
62#elif defined(OPENSSL_PPC64LE)
63
64unsigned long OPENSSL_ppc64le_hwcap2 = 0;
65
Adam Langleyd9e397b2015-01-22 14:27:53 -080066#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
67
Kenny Rootb8494592015-09-25 02:29:14 +000068#include <openssl/arm_arch.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080069
Kenny Roote99801b2015-11-06 15:31:15 -080070#if defined(OPENSSL_STATIC_ARMCAP)
71
72uint32_t OPENSSL_armcap_P =
73#if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON__)
David Benjamin4969cc92016-04-22 15:02:23 -040074 ARMV7_NEON |
Kenny Roote99801b2015-11-06 15:31:15 -080075#endif
Robert Sloan8ff03552017-06-14 12:40:58 -070076#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -080077 ARMV8_AES |
78#endif
Robert Sloan8ff03552017-06-14 12:40:58 -070079#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -080080 ARMV8_SHA1 |
81#endif
Robert Sloan8ff03552017-06-14 12:40:58 -070082#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -080083 ARMV8_SHA256 |
84#endif
Robert Sloan8ff03552017-06-14 12:40:58 -070085#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO)
Kenny Roote99801b2015-11-06 15:31:15 -080086 ARMV8_PMULL |
87#endif
88 0;
89
Adam Langleyd9e397b2015-01-22 14:27:53 -080090#else
David Benjamin4969cc92016-04-22 15:02:23 -040091uint32_t OPENSSL_armcap_P = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080092#endif
93
94#endif
95
Robert Sloan572a4e22017-04-17 10:52:19 -070096#if defined(BORINGSSL_FIPS)
Robert Sloan8f860b12017-08-28 07:37:06 -070097// In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
98// because we have to ensure that CPUID detection occurs first.
Robert Sloan572a4e22017-04-17 10:52:19 -070099#define BORINGSSL_NO_STATIC_INITIALIZER
100#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800101
Adam Langley4139edb2016-01-13 15:00:54 -0800102#if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800103#define OPENSSL_CDECL __cdecl
104#else
105#define OPENSSL_CDECL
106#endif
107
Adam Langley4139edb2016-01-13 15:00:54 -0800108#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
109static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
Robert Sloan8f860b12017-08-28 07:37:06 -0700110#elif defined(_MSC_VER)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111#pragma section(".CRT$XCU", read)
112static void __cdecl do_library_init(void);
113__declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
114 do_library_init;
Adam Langley4139edb2016-01-13 15:00:54 -0800115#else
116static void do_library_init(void) __attribute__ ((constructor));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800117#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800118
Robert Sloan8f860b12017-08-28 07:37:06 -0700119// do_library_init is the actual initialization function. If
120// BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
121// initializer. Otherwise, it is called by CRYPTO_library_init.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800122static void OPENSSL_CDECL do_library_init(void) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700123 // WARNING: this function may only configure the capability variables. See the
124 // note above about the linker bug.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125#if defined(NEED_CPUID)
126 OPENSSL_cpuid_setup();
127#endif
128}
129
130void CRYPTO_library_init(void) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700131 // TODO(davidben): It would be tidier if this build knob could be replaced
132 // with an internal lazy-init mechanism that would handle things correctly
133 // in-library. https://crbug.com/542879
Adam Langleyd9e397b2015-01-22 14:27:53 -0800134#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
Adam Langley4139edb2016-01-13 15:00:54 -0800135 CRYPTO_once(&once, do_library_init);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136#endif
137}
Adam Langleyf40f42d2015-03-24 18:25:20 -0700138
David Benjamin9aaebef2016-04-22 15:02:23 -0400139int CRYPTO_is_confidential_build(void) {
140#if defined(BORINGSSL_CONFIDENTIAL)
141 return 1;
142#else
143 return 0;
144#endif
145}
146
David Benjamind316cba2016-06-02 16:17:39 -0400147int CRYPTO_has_asm(void) {
148#if defined(OPENSSL_NO_ASM)
149 return 0;
150#else
151 return 1;
152#endif
153}
154
Adam Langleyf40f42d2015-03-24 18:25:20 -0700155const char *SSLeay_version(int unused) {
Adam Langley12addf82015-05-19 15:56:28 -0700156 return "BoringSSL";
Adam Langleyf40f42d2015-03-24 18:25:20 -0700157}
158
Adam Langley12addf82015-05-19 15:56:28 -0700159unsigned long SSLeay(void) {
160 return OPENSSL_VERSION_NUMBER;
Adam Langleyf40f42d2015-03-24 18:25:20 -0700161}
Kenny Roote99801b2015-11-06 15:31:15 -0800162
163int CRYPTO_malloc_init(void) {
164 return 1;
165}
166
167void ENGINE_load_builtin_engines(void) {}
David Benjamin4969cc92016-04-22 15:02:23 -0400168
David Benjaminc895d6b2016-08-11 13:26:41 -0400169int ENGINE_register_all_complete(void) {
170 return 1;
171}
172
David Benjamin4969cc92016-04-22 15:02:23 -0400173void OPENSSL_load_builtin_modules(void) {}