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