xref: /aosp_15_r20/external/boringssl/src/crypto/crypto.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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 
17 #include <assert.h>
18 
19 #include "fipsmodule/rand/fork_detect.h"
20 #include "fipsmodule/rand/internal.h"
21 #include "internal.h"
22 
23 
24 static_assert(sizeof(ossl_ssize_t) == sizeof(size_t),
25               "ossl_ssize_t should be the same size as size_t");
26 
27 
28 // Our assembly does not use the GOT to reference symbols, which means
29 // references to visible symbols will often require a TEXTREL. This is
30 // undesirable, so all assembly-referenced symbols should be hidden. CPU
31 // capabilities are the only such symbols defined in C. Explicitly hide them,
32 // rather than rely on being built with -fvisibility=hidden.
33 #if defined(OPENSSL_WINDOWS)
34 #define HIDDEN
35 #else
36 #define HIDDEN __attribute__((visibility("hidden")))
37 #endif
38 
39 
40 // The capability variables are defined in this file in order to work around a
41 // linker bug. When linking with a .a, if no symbols in a .o are referenced
42 // then the .o is discarded, even if it has constructor functions.
43 //
44 // This still means that any binaries that don't include some functionality
45 // that tests the capability values will still skip the constructor but, so
46 // far, the init constructor function only sets the capability variables.
47 
48 #if defined(BORINGSSL_DISPATCH_TEST)
49 // This value must be explicitly initialised to zero in order to work around a
50 // bug in libtool or the linker on OS X.
51 //
52 // If not initialised then it becomes a "common symbol". When put into an
53 // archive, linking on OS X will fail to resolve common symbols. By
54 // initialising it to zero, it becomes a "data symbol", which isn't so
55 // affected.
56 HIDDEN uint8_t BORINGSSL_function_hit[7] = {0};
57 #endif
58 
59 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
60 
61 // This value must be explicitly initialized to zero. See similar comment above.
62 HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0};
63 
OPENSSL_get_ia32cap(int idx)64 uint32_t OPENSSL_get_ia32cap(int idx) {
65   OPENSSL_init_cpuid();
66   return OPENSSL_ia32cap_P[idx];
67 }
68 
69 #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
70 
71 #include <openssl/arm_arch.h>
72 
73 #if defined(OPENSSL_STATIC_ARMCAP)
74 
75 // See ARM ACLE for the definitions of these macros. Note |__ARM_FEATURE_AES|
76 // covers both AES and PMULL and |__ARM_FEATURE_SHA2| covers SHA-1 and SHA-256.
77 // https://developer.arm.com/architectures/system-architectures/software-standards/acle
78 // https://github.com/ARM-software/acle/issues/152
79 //
80 // TODO(davidben): Do we still need |OPENSSL_STATIC_ARMCAP_*| or are the
81 // standard flags and -march sufficient?
82 HIDDEN uint32_t OPENSSL_armcap_P =
83 #if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON)
84     ARMV7_NEON |
85 #endif
86 #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_AES)
87     ARMV8_AES |
88 #endif
89 #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_AES)
90     ARMV8_PMULL |
91 #endif
92 #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_SHA2)
93     ARMV8_SHA1 |
94 #endif
95 #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_SHA2)
96     ARMV8_SHA256 |
97 #endif
98 #if defined(__ARM_FEATURE_SHA512)
99     ARMV8_SHA512 |
100 #endif
101     0;
102 
103 #else
104 HIDDEN uint32_t OPENSSL_armcap_P = 0;
105 
OPENSSL_get_armcap_pointer_for_test(void)106 uint32_t *OPENSSL_get_armcap_pointer_for_test(void) {
107   OPENSSL_init_cpuid();
108   return &OPENSSL_armcap_P;
109 }
110 #endif
111 
OPENSSL_get_armcap(void)112 uint32_t OPENSSL_get_armcap(void) {
113   OPENSSL_init_cpuid();
114   return OPENSSL_armcap_P;
115 }
116 
117 #endif
118 
119 #if defined(NEED_CPUID)
120 static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
OPENSSL_init_cpuid(void)121 void OPENSSL_init_cpuid(void) { CRYPTO_once(&once, OPENSSL_cpuid_setup); }
122 #endif
123 
CRYPTO_library_init(void)124 void CRYPTO_library_init(void) {}
125 
CRYPTO_is_confidential_build(void)126 int CRYPTO_is_confidential_build(void) {
127 #if defined(BORINGSSL_CONFIDENTIAL)
128   return 1;
129 #else
130   return 0;
131 #endif
132 }
133 
CRYPTO_has_asm(void)134 int CRYPTO_has_asm(void) {
135 #if defined(OPENSSL_NO_ASM)
136   return 0;
137 #else
138   return 1;
139 #endif
140 }
141 
CRYPTO_pre_sandbox_init(void)142 void CRYPTO_pre_sandbox_init(void) {
143   // Read from /proc/cpuinfo if needed.
144   OPENSSL_init_cpuid();
145   // Open /dev/urandom if needed.
146   CRYPTO_init_sysrand();
147   // Set up MADV_WIPEONFORK state if needed.
148   CRYPTO_get_fork_generation();
149 }
150 
SSLeay_version(int which)151 const char *SSLeay_version(int which) { return OpenSSL_version(which); }
152 
OpenSSL_version(int which)153 const char *OpenSSL_version(int which) {
154   switch (which) {
155     case OPENSSL_VERSION:
156       return "BoringSSL";
157     case OPENSSL_CFLAGS:
158       return "compiler: n/a";
159     case OPENSSL_BUILT_ON:
160       return "built on: n/a";
161     case OPENSSL_PLATFORM:
162       return "platform: n/a";
163     case OPENSSL_DIR:
164       return "OPENSSLDIR: n/a";
165     default:
166       return "not available";
167   }
168 }
169 
SSLeay(void)170 unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; }
171 
OpenSSL_version_num(void)172 unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; }
173 
CRYPTO_malloc_init(void)174 int CRYPTO_malloc_init(void) { return 1; }
175 
OPENSSL_malloc_init(void)176 int OPENSSL_malloc_init(void) { return 1; }
177 
ENGINE_load_builtin_engines(void)178 void ENGINE_load_builtin_engines(void) {}
179 
ENGINE_register_all_complete(void)180 int ENGINE_register_all_complete(void) { return 1; }
181 
OPENSSL_load_builtin_modules(void)182 void OPENSSL_load_builtin_modules(void) {}
183 
OPENSSL_init_crypto(uint64_t opts,const OPENSSL_INIT_SETTINGS * settings)184 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
185   return 1;
186 }
187 
OPENSSL_cleanup(void)188 void OPENSSL_cleanup(void) {}
189