Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/crypto.c

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/crypto.h>
5 #include <openssl/rand.h>
6
7 #include <assert.h>
8
9 #include "fipsmodule/cpucap/internal.h"
10 #include "internal.h"
11
12
13 OPENSSL_STATIC_ASSERT(sizeof(ossl_ssize_t) == sizeof(size_t),
14 ossl_ssize_t_should_be_the_same_size_as_size_t)
15
16 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
17 (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
18 defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
19 defined(OPENSSL_PPC64LE))
20 // x86, x86_64, the ARMs and ppc64le need to record the result of a
21 // cpuid/getauxval call for the asm to work correctly, unless compiled without
22 // asm code.
23 #define NEED_CPUID
24
25 #else
26
27 // Otherwise, don't emit a static initialiser.
28
29 #if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
30 #define BORINGSSL_NO_STATIC_INITIALIZER
31 #endif
32
33 #endif // !NO_ASM && !STATIC_ARMCAP &&
34 // (X86 || X86_64 || ARM || AARCH64 || PPC64LE)
35
36 #if defined(BORINGSSL_FIPS)
37 // In FIPS mode, the power-on self-test function calls |OPENSSL_cpuid_setup|
38 // because we have to ensure that CPUID detection occurs first.
39 #define BORINGSSL_NO_STATIC_INITIALIZER
40 #endif
41
42 #if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
43 #define OPENSSL_CDECL __cdecl
44 #else
45 #define OPENSSL_CDECL
46 #endif
47
48 #if defined(BORINGSSL_NO_STATIC_INITIALIZER)
49 static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
50 #elif defined(_MSC_VER)
51 #pragma section(".CRT$XCU", read)
52 static void __cdecl do_library_init(void);
53 __declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
54 do_library_init;
55 #else
56 static void do_library_init(void) __attribute__ ((constructor));
57 #endif
58
59 // do_library_init is the actual initialization function. If
60 // BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
61 // initializer. Otherwise, it is called by CRYPTO_library_init.
62 static void OPENSSL_CDECL do_library_init(void) {
63 // WARNING: this function may only configure the capability variables. See the
64 // note above about the linker bug.
65 // In the FIPS build the module itself has to call |OPENSSL_cpuid_setup|.
66 #if defined(NEED_CPUID) && !defined(BORINGSSL_FIPS)
67 OPENSSL_cpuid_setup();
68 #endif
69 }
70
71 void CRYPTO_library_init(void) {
72 // TODO(davidben): It would be tidier if this build knob could be replaced
73 // with an internal lazy-init mechanism that would handle things correctly
74 // in-library. https://crbug.com/542879
75 #if defined(BORINGSSL_NO_STATIC_INITIALIZER)
76 CRYPTO_once(&once, do_library_init);
77 #endif
78 }
79
80 int CRYPTO_is_confidential_build(void) {
81 #if defined(BORINGSSL_CONFIDENTIAL)
82 return 1;
83 #else
84 return 0;
85 #endif
86 }
87
88 int CRYPTO_has_asm(void) {
89 #if defined(OPENSSL_NO_ASM)
90 return 0;
91 #else
92 return 1;
93 #endif
94 }
95
96 void CRYPTO_pre_sandbox_init(void) {
97 // Read from /proc/cpuinfo if needed.
98 CRYPTO_library_init();
99
100 // The randomness generation subsystem has a few kernel touch points that
101 // can be blocked when sandboxed. For example, /dev/urandom, MADV_WIPEONFORK
102 // tagged state, and VM UBE allocated state. All this is implemented lazily.
103 // Invoke the top-level function that will kick off the lazy work pre-sandbox.
104 uint8_t buf[10];
105 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(buf, 10));
106 }
107
108 const char *SSLeay_version(int which) { return OpenSSL_version(which); }
109
110 const char *OpenSSL_version(int which) {
111 switch (which) {
112 case OPENSSL_VERSION:
113 return AWSLC_VERSION_STRING;
114 case OPENSSL_CFLAGS:
115 return "compiler: n/a";
116 case OPENSSL_BUILT_ON:
117 return "built on: n/a";
118 case OPENSSL_PLATFORM:
119 return "platform: n/a";
120 case OPENSSL_DIR:
121 return "OPENSSLDIR: n/a";
122 default:
123 return "not available";
124 }
125 }
126
127 unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; }
128
129 unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; }
130
131 unsigned long awslc_api_version_num(void) { return AWSLC_API_VERSION; }
132
133 int CRYPTO_malloc_init(void) { return 1; }
134
135 int OPENSSL_malloc_init(void) { return 1; }
136
137 void ENGINE_load_builtin_engines(void) {}
138
139 void ENGINE_register_all_ciphers(void) {}
140
141 void ENGINE_register_all_digests(void) {}
142
143 int ENGINE_register_all_complete(void) { return 1; }
144
145 void OPENSSL_load_builtin_modules(void) {}
146
147 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
148 CRYPTO_library_init();
149 return 1;
150 }
151
152 void OPENSSL_init(void) {}
153
154 void OPENSSL_cleanup(void) {}