blob: 5d352892d692d98e40bf8ee01518aab3ff14e1b6 [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))
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 Langleyd9e397b2015-01-22 14:27:53 -080029#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/* 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. */
50
51#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
Robert Sloan572a4e22017-04-17 10:52:19 -070052
Adam Langleyd9e397b2015-01-22 14:27:53 -080053/* 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. */
60uint32_t OPENSSL_ia32cap_P[4] = {0};
Robert Sloan572a4e22017-04-17 10:52:19 -070061#if !defined(BORINGSSL_FIPS)
62uint32_t *OPENSSL_ia32cap_addr = OPENSSL_ia32cap_P;
63#endif
64
65#elif defined(OPENSSL_PPC64LE)
66
67unsigned long OPENSSL_ppc64le_hwcap2 = 0;
68
Adam Langleyd9e397b2015-01-22 14:27:53 -080069#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
70
Kenny Rootb8494592015-09-25 02:29:14 +000071#include <openssl/arm_arch.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080072
Kenny Roote99801b2015-11-06 15:31:15 -080073#if defined(OPENSSL_STATIC_ARMCAP)
74
75uint32_t OPENSSL_armcap_P =
76#if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON__)
David Benjamin4969cc92016-04-22 15:02:23 -040077 ARMV7_NEON |
Kenny Roote99801b2015-11-06 15:31:15 -080078#endif
79#if defined(OPENSSL_STATIC_ARMCAP_AES)
80 ARMV8_AES |
81#endif
82#if defined(OPENSSL_STATIC_ARMCAP_SHA1)
83 ARMV8_SHA1 |
84#endif
85#if defined(OPENSSL_STATIC_ARMCAP_SHA256)
86 ARMV8_SHA256 |
87#endif
88#if defined(OPENSSL_STATIC_ARMCAP_PMULL)
89 ARMV8_PMULL |
90#endif
91 0;
92
Adam Langleyd9e397b2015-01-22 14:27:53 -080093#else
David Benjamin4969cc92016-04-22 15:02:23 -040094uint32_t OPENSSL_armcap_P = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080095#endif
96
97#endif
98
Robert Sloan572a4e22017-04-17 10:52:19 -070099#if defined(BORINGSSL_FIPS)
100/* In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
101 * because we have to ensure that CPUID detection occurs first. */
102#define BORINGSSL_NO_STATIC_INITIALIZER
103#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800104
Adam Langley4139edb2016-01-13 15:00:54 -0800105#if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800106#define OPENSSL_CDECL __cdecl
107#else
108#define OPENSSL_CDECL
109#endif
110
Adam Langley4139edb2016-01-13 15:00:54 -0800111#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
112static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
113#elif defined(OPENSSL_WINDOWS)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114#pragma section(".CRT$XCU", read)
115static void __cdecl do_library_init(void);
116__declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
117 do_library_init;
Adam Langley4139edb2016-01-13 15:00:54 -0800118#else
119static void do_library_init(void) __attribute__ ((constructor));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800120#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121
122/* do_library_init is the actual initialization function. If
123 * BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
124 * initializer. Otherwise, it is called by CRYPTO_library_init. */
125static void OPENSSL_CDECL do_library_init(void) {
126 /* WARNING: this function may only configure the capability variables. See the
127 * note above about the linker bug. */
128#if defined(NEED_CPUID)
129 OPENSSL_cpuid_setup();
130#endif
131}
132
133void CRYPTO_library_init(void) {
134 /* TODO(davidben): It would be tidier if this build knob could be replaced
135 * with an internal lazy-init mechanism that would handle things correctly
Adam Langley4139edb2016-01-13 15:00:54 -0800136 * in-library. https://crbug.com/542879 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800137#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
Adam Langley4139edb2016-01-13 15:00:54 -0800138 CRYPTO_once(&once, do_library_init);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139#endif
140}
Adam Langleyf40f42d2015-03-24 18:25:20 -0700141
David Benjamin9aaebef2016-04-22 15:02:23 -0400142int CRYPTO_is_confidential_build(void) {
143#if defined(BORINGSSL_CONFIDENTIAL)
144 return 1;
145#else
146 return 0;
147#endif
148}
149
David Benjamind316cba2016-06-02 16:17:39 -0400150int CRYPTO_has_asm(void) {
151#if defined(OPENSSL_NO_ASM)
152 return 0;
153#else
154 return 1;
155#endif
156}
157
Adam Langleyf40f42d2015-03-24 18:25:20 -0700158const char *SSLeay_version(int unused) {
Adam Langley12addf82015-05-19 15:56:28 -0700159 return "BoringSSL";
Adam Langleyf40f42d2015-03-24 18:25:20 -0700160}
161
Adam Langley12addf82015-05-19 15:56:28 -0700162unsigned long SSLeay(void) {
163 return OPENSSL_VERSION_NUMBER;
Adam Langleyf40f42d2015-03-24 18:25:20 -0700164}
Kenny Roote99801b2015-11-06 15:31:15 -0800165
166int CRYPTO_malloc_init(void) {
167 return 1;
168}
169
170void ENGINE_load_builtin_engines(void) {}
David Benjamin4969cc92016-04-22 15:02:23 -0400171
David Benjaminc895d6b2016-08-11 13:26:41 -0400172int ENGINE_register_all_complete(void) {
173 return 1;
174}
175
David Benjamin4969cc92016-04-22 15:02:23 -0400176void OPENSSL_load_builtin_modules(void) {}