/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aws-lc-sys-0.40.0/aws-lc/crypto/crypto.c
Line | Count | Source |
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 | 8 | 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 | 8 | #if defined(NEED_CPUID) && !defined(BORINGSSL_FIPS) |
67 | 8 | OPENSSL_cpuid_setup(); |
68 | 8 | #endif |
69 | 8 | } |
70 | | |
71 | 0 | 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 | 0 | } |
79 | | |
80 | 0 | int CRYPTO_is_confidential_build(void) { |
81 | | #if defined(BORINGSSL_CONFIDENTIAL) |
82 | | return 1; |
83 | | #else |
84 | 0 | return 0; |
85 | 0 | #endif |
86 | 0 | } |
87 | | |
88 | 0 | int CRYPTO_has_asm(void) { |
89 | | #if defined(OPENSSL_NO_ASM) |
90 | | return 0; |
91 | | #else |
92 | 0 | return 1; |
93 | 0 | #endif |
94 | 0 | } |
95 | | |
96 | 0 | void CRYPTO_pre_sandbox_init(void) { |
97 | | // Read from /proc/cpuinfo if needed. |
98 | 0 | 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 | 0 | uint8_t buf[10]; |
105 | 0 | AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(buf, 10)); |
106 | 0 | } |
107 | | |
108 | 0 | const char *SSLeay_version(int which) { return OpenSSL_version(which); } |
109 | | |
110 | 0 | const char *OpenSSL_version(int which) { |
111 | 0 | switch (which) { |
112 | 0 | case OPENSSL_VERSION: |
113 | 0 | return AWSLC_VERSION_STRING; |
114 | 0 | case OPENSSL_CFLAGS: |
115 | 0 | return "compiler: n/a"; |
116 | 0 | case OPENSSL_BUILT_ON: |
117 | 0 | return "built on: n/a"; |
118 | 0 | case OPENSSL_PLATFORM: |
119 | 0 | return "platform: n/a"; |
120 | 0 | case OPENSSL_DIR: |
121 | 0 | return "OPENSSLDIR: n/a"; |
122 | 0 | default: |
123 | 0 | return "not available"; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | 0 | unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; } |
128 | | |
129 | 0 | unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; } |
130 | | |
131 | 0 | unsigned long awslc_api_version_num(void) { return AWSLC_API_VERSION; } |
132 | | |
133 | 0 | int CRYPTO_malloc_init(void) { return 1; } |
134 | | |
135 | 0 | int OPENSSL_malloc_init(void) { return 1; } |
136 | | |
137 | 0 | void ENGINE_load_builtin_engines(void) {} |
138 | | |
139 | 0 | void ENGINE_register_all_ciphers(void) {} |
140 | | |
141 | 0 | void ENGINE_register_all_digests(void) {} |
142 | | |
143 | 0 | int ENGINE_register_all_complete(void) { return 1; } |
144 | | |
145 | 0 | void OPENSSL_load_builtin_modules(void) {} |
146 | | |
147 | 0 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
148 | 0 | CRYPTO_library_init(); |
149 | 0 | return 1; |
150 | 0 | } |
151 | | |
152 | 0 | void OPENSSL_init(void) {} |
153 | | |
154 | 0 | void OPENSSL_cleanup(void) {} |