1 // Copyright 2014 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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[8] = {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     !defined(OPENSSL_STATIC_ARMCAP)
72 HIDDEN uint32_t OPENSSL_armcap_P = 0;
73 
OPENSSL_get_armcap_pointer_for_test(void)74 uint32_t *OPENSSL_get_armcap_pointer_for_test(void) {
75   OPENSSL_init_cpuid();
76   return &OPENSSL_armcap_P;
77 }
78 
OPENSSL_get_armcap(void)79 uint32_t OPENSSL_get_armcap(void) {
80   OPENSSL_init_cpuid();
81   return OPENSSL_armcap_P;
82 }
83 #endif
84 
85 #if defined(NEED_CPUID)
86 static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
OPENSSL_init_cpuid(void)87 void OPENSSL_init_cpuid(void) { CRYPTO_once(&once, OPENSSL_cpuid_setup); }
88 #endif
89 
CRYPTO_library_init(void)90 void CRYPTO_library_init(void) {}
91 
CRYPTO_is_confidential_build(void)92 int CRYPTO_is_confidential_build(void) {
93 #if defined(BORINGSSL_CONFIDENTIAL)
94   return 1;
95 #else
96   return 0;
97 #endif
98 }
99 
CRYPTO_pre_sandbox_init(void)100 void CRYPTO_pre_sandbox_init(void) {
101   // Read from /proc/cpuinfo if needed.
102   OPENSSL_init_cpuid();
103   // Open /dev/urandom if needed.
104   CRYPTO_init_sysrand();
105   // Set up MADV_WIPEONFORK state if needed.
106   CRYPTO_get_fork_generation();
107 }
108 
SSLeay_version(int which)109 const char *SSLeay_version(int which) { return OpenSSL_version(which); }
110 
OpenSSL_version(int which)111 const char *OpenSSL_version(int which) {
112   switch (which) {
113     case OPENSSL_VERSION:
114       return "BoringSSL";
115     case OPENSSL_CFLAGS:
116       return "compiler: n/a";
117     case OPENSSL_BUILT_ON:
118       return "built on: n/a";
119     case OPENSSL_PLATFORM:
120       return "platform: n/a";
121     case OPENSSL_DIR:
122       return "OPENSSLDIR: n/a";
123     default:
124       return "not available";
125   }
126 }
127 
SSLeay(void)128 unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; }
129 
OpenSSL_version_num(void)130 unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; }
131 
CRYPTO_malloc_init(void)132 int CRYPTO_malloc_init(void) { return 1; }
133 
OPENSSL_malloc_init(void)134 int OPENSSL_malloc_init(void) { return 1; }
135 
ENGINE_load_builtin_engines(void)136 void ENGINE_load_builtin_engines(void) {}
137 
ENGINE_register_all_complete(void)138 int ENGINE_register_all_complete(void) { return 1; }
139 
ENGINE_cleanup(void)140 void ENGINE_cleanup(void) {}
141 
OPENSSL_load_builtin_modules(void)142 void OPENSSL_load_builtin_modules(void) {}
143 
OPENSSL_init_crypto(uint64_t opts,const OPENSSL_INIT_SETTINGS * settings)144 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
145   return 1;
146 }
147 
OPENSSL_cleanup(void)148 void OPENSSL_cleanup(void) {}
149 
CRYPTO_get_stderr(void)150 FILE *CRYPTO_get_stderr(void) { return stderr; }
151