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