/src/openssl/crypto/info.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/crypto.h> |
11 | | #include "crypto/rand.h" |
12 | | #include "crypto/dso_conf.h" |
13 | | #include "internal/thread_once.h" |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/e_os.h" |
16 | | #include "buildinf.h" |
17 | | |
18 | | #ifndef OPENSSL_NO_JITTER |
19 | | #include <stdio.h> |
20 | | #include <jitterentropy.h> |
21 | | #endif |
22 | | |
23 | | #if defined(__arm__) || defined(__arm) || defined(__aarch64__) |
24 | | #include "arm_arch.h" |
25 | | #define CPU_INFO_STR_LEN 128 |
26 | | #elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC) |
27 | | #include "crypto/ppc_arch.h" |
28 | | #define CPU_INFO_STR_LEN 128 |
29 | | #elif defined(__sparcv9) || defined(__sparcv9__) |
30 | | #include "crypto/sparc_arch.h" |
31 | | #define CPU_INFO_STR_LEN 128 |
32 | | #elif defined(__s390__) || defined(__s390x__) |
33 | | #include "s390x_arch.h" |
34 | | #define CPU_INFO_STR_LEN 2048 |
35 | | #elif defined(__riscv) |
36 | | #include "crypto/riscv_arch.h" |
37 | | #define CPU_INFO_STR_LEN 2048 |
38 | | #else |
39 | | #define CPU_INFO_STR_LEN 256 |
40 | | #endif |
41 | | |
42 | | /* extern declaration to avoid warning */ |
43 | | extern char ossl_cpu_info_str[]; |
44 | | |
45 | | static char *seed_sources = NULL; |
46 | | |
47 | | char ossl_cpu_info_str[CPU_INFO_STR_LEN] = ""; |
48 | 0 | #define CPUINFO_PREFIX "CPUINFO: " |
49 | | |
50 | | static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT; |
51 | | |
52 | | DEFINE_RUN_ONCE_STATIC(init_info_strings) |
53 | 0 | { |
54 | | #if defined(OPENSSL_CPUID_OBJ) |
55 | | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) |
56 | | const char *env; |
57 | | |
58 | | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), |
59 | | CPUINFO_PREFIX "OPENSSL_ia32cap=0x%.16llx:0x%.16llx:0x%.16llx:0x%.16llx:0x%.16llx", |
60 | | (unsigned long long)OPENSSL_ia32cap_P[0] | (unsigned long long)OPENSSL_ia32cap_P[1] << 32, |
61 | | (unsigned long long)OPENSSL_ia32cap_P[2] | (unsigned long long)OPENSSL_ia32cap_P[3] << 32, |
62 | | (unsigned long long)OPENSSL_ia32cap_P[4] | (unsigned long long)OPENSSL_ia32cap_P[5] << 32, |
63 | | (unsigned long long)OPENSSL_ia32cap_P[6] | (unsigned long long)OPENSSL_ia32cap_P[7] << 32, |
64 | | (unsigned long long)OPENSSL_ia32cap_P[8] | (unsigned long long)OPENSSL_ia32cap_P[9] << 32); |
65 | | |
66 | | if ((env = getenv("OPENSSL_ia32cap")) != NULL) |
67 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
68 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
69 | | " env:%s", env); |
70 | | #elif defined(__arm__) || defined(__arm) || defined(__aarch64__) |
71 | | const char *env; |
72 | | |
73 | | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), |
74 | | CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P); |
75 | | if ((env = getenv("OPENSSL_armcap")) != NULL) |
76 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
77 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
78 | | " env:%s", env); |
79 | | #elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC) |
80 | | const char *env; |
81 | | |
82 | | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), |
83 | | CPUINFO_PREFIX "OPENSSL_ppccap=0x%x", OPENSSL_ppccap_P); |
84 | | if ((env = getenv("OPENSSL_ppccap")) != NULL) |
85 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
86 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
87 | | " env:%s", env); |
88 | | #elif defined(__sparcv9) || defined(__sparcv9__) |
89 | | const char *env; |
90 | | |
91 | | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), |
92 | | CPUINFO_PREFIX "OPENSSL_sparcv9cap=0x%x:0x%x", |
93 | | OPENSSL_sparcv9cap_P[0], OPENSSL_sparcv9cap_P[1]); |
94 | | if ((env = getenv("OPENSSL_sparcv9cap")) != NULL) |
95 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
96 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
97 | | " env:%s", env); |
98 | | #elif defined(__s390__) || defined(__s390x__) |
99 | | const char *env; |
100 | | |
101 | | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), |
102 | | CPUINFO_PREFIX "OPENSSL_s390xcap=" |
103 | | "stfle:0x%llx:0x%llx:0x%llx:0x%llx:" |
104 | | "kimd:0x%llx:0x%llx:" |
105 | | "klmd:0x%llx:0x%llx:" |
106 | | "km:0x%llx:0x%llx:" |
107 | | "kmc:0x%llx:0x%llx:" |
108 | | "kmac:0x%llx:0x%llx:" |
109 | | "kmctr:0x%llx:0x%llx:" |
110 | | "kmo:0x%llx:0x%llx:" |
111 | | "kmf:0x%llx:0x%llx:" |
112 | | "prno:0x%llx:0x%llx:" |
113 | | "kma:0x%llx:0x%llx:" |
114 | | "pcc:0x%llx:0x%llx:" |
115 | | "kdsa:0x%llx:0x%llx", |
116 | | OPENSSL_s390xcap_P.stfle[0], OPENSSL_s390xcap_P.stfle[1], |
117 | | OPENSSL_s390xcap_P.stfle[2], OPENSSL_s390xcap_P.stfle[3], |
118 | | OPENSSL_s390xcap_P.kimd[0], OPENSSL_s390xcap_P.kimd[1], |
119 | | OPENSSL_s390xcap_P.klmd[0], OPENSSL_s390xcap_P.klmd[1], |
120 | | OPENSSL_s390xcap_P.km[0], OPENSSL_s390xcap_P.km[1], |
121 | | OPENSSL_s390xcap_P.kmc[0], OPENSSL_s390xcap_P.kmc[1], |
122 | | OPENSSL_s390xcap_P.kmac[0], OPENSSL_s390xcap_P.kmac[1], |
123 | | OPENSSL_s390xcap_P.kmctr[0], OPENSSL_s390xcap_P.kmctr[1], |
124 | | OPENSSL_s390xcap_P.kmo[0], OPENSSL_s390xcap_P.kmo[1], |
125 | | OPENSSL_s390xcap_P.kmf[0], OPENSSL_s390xcap_P.kmf[1], |
126 | | OPENSSL_s390xcap_P.prno[0], OPENSSL_s390xcap_P.prno[1], |
127 | | OPENSSL_s390xcap_P.kma[0], OPENSSL_s390xcap_P.kma[1], |
128 | | OPENSSL_s390xcap_P.pcc[0], OPENSSL_s390xcap_P.pcc[1], |
129 | | OPENSSL_s390xcap_P.kdsa[0], OPENSSL_s390xcap_P.kdsa[1]); |
130 | | if ((env = getenv("OPENSSL_s390xcap")) != NULL) |
131 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
132 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
133 | | " env:%s", env); |
134 | | #elif defined(__riscv) |
135 | | const char *env; |
136 | | size_t i; |
137 | | |
138 | | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), |
139 | | CPUINFO_PREFIX "OPENSSL_riscvcap=RV" |
140 | | #if __riscv_xlen == 32 |
141 | | "32" |
142 | | #elif __riscv_xlen == 64 |
143 | | "64" |
144 | | #elif __riscv_xlen == 128 |
145 | | "128" |
146 | | #endif |
147 | | #if defined(__riscv_i) && defined(__riscv_m) && defined(__riscv_a) \ |
148 | | && defined(__riscv_f) && defined(__riscv_d) \ |
149 | | && defined(__riscv_zicsr) && defined(__riscv_zifencei) |
150 | | "G" /* shorthand for IMAFD_Zicsr_Zifencei */ |
151 | | #else |
152 | | #ifdef __riscv_i |
153 | | "I" |
154 | | #endif |
155 | | #ifdef __riscv_m |
156 | | "M" |
157 | | #endif |
158 | | #ifdef __riscv_a |
159 | | "A" |
160 | | #endif |
161 | | #ifdef __riscv_f |
162 | | "F" |
163 | | #endif |
164 | | #ifdef __riscv_d |
165 | | "D" |
166 | | #endif |
167 | | #endif |
168 | | #ifdef __riscv_c |
169 | | "C" |
170 | | #endif |
171 | | ); |
172 | | for (i = 0; i < kRISCVNumCaps; i++) { |
173 | | if (OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |
174 | | & (1 << RISCV_capabilities[i].bit_offset)) |
175 | | /* Match, display the name */ |
176 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
177 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
178 | | "_%s", RISCV_capabilities[i].name); |
179 | | } |
180 | | if (RISCV_HAS_V()) |
181 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
182 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
183 | | " vlen:%lu", riscv_vlen()); |
184 | | if ((env = getenv("OPENSSL_riscvcap")) != NULL) |
185 | | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), |
186 | | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), |
187 | | " env:%s", env); |
188 | | #endif |
189 | | #endif |
190 | |
|
191 | 0 | { |
192 | 0 | static char seeds[512] = ""; |
193 | |
|
194 | 0 | #define add_seeds_string(str) \ |
195 | 0 | do { \ |
196 | 0 | if (seeds[0] != '\0') \ |
197 | 0 | OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \ |
198 | 0 | OPENSSL_strlcat(seeds, str, sizeof(seeds)); \ |
199 | 0 | } while (0) |
200 | 0 | #define add_seeds_stringlist(label, strlist) \ |
201 | 0 | do { \ |
202 | 0 | add_seeds_string(label "("); \ |
203 | 0 | { \ |
204 | 0 | const char *dev[] = { strlist, NULL }; \ |
205 | 0 | const char **p; \ |
206 | 0 | int first = 1; \ |
207 | 0 | \ |
208 | 0 | for (p = dev; *p != NULL; p++) { \ |
209 | 0 | if (!first) \ |
210 | 0 | OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \ |
211 | 0 | first = 0; \ |
212 | 0 | OPENSSL_strlcat(seeds, *p, sizeof(seeds)); \ |
213 | 0 | } \ |
214 | 0 | } \ |
215 | 0 | OPENSSL_strlcat(seeds, ")", sizeof(seeds)); \ |
216 | 0 | } while (0) |
217 | |
|
218 | | #ifdef OPENSSL_RAND_SEED_NONE |
219 | | add_seeds_string("none"); |
220 | | #endif |
221 | | #ifdef OPENSSL_RAND_SEED_RDTSC |
222 | | add_seeds_string("rdtsc"); |
223 | | #endif |
224 | | #ifdef OPENSSL_RAND_SEED_RDCPU |
225 | | #ifdef __aarch64__ |
226 | | add_seeds_string("rndr ( rndrrs rndr )"); |
227 | | #else |
228 | | add_seeds_string("rdrand ( rdseed rdrand )"); |
229 | | #endif |
230 | | #endif |
231 | | #ifdef OPENSSL_RAND_SEED_GETRANDOM |
232 | | add_seeds_string("getrandom-syscall"); |
233 | | #endif |
234 | | #ifdef OPENSSL_RAND_SEED_DEVRANDOM |
235 | | add_seeds_stringlist("random-device", DEVRANDOM); |
236 | | #endif |
237 | | #ifdef OPENSSL_RAND_SEED_EGD |
238 | | add_seeds_stringlist("EGD", DEVRANDOM_EGD); |
239 | | #endif |
240 | 0 | #ifdef OPENSSL_RAND_SEED_OS |
241 | 0 | add_seeds_string("os-specific"); |
242 | 0 | #endif |
243 | | #ifndef OPENSSL_NO_JITTER |
244 | | { |
245 | | char buf[32]; |
246 | | |
247 | | BIO_snprintf(buf, sizeof(buf), "JITTER (%d)", jent_version()); |
248 | | add_seeds_string(buf); |
249 | | } |
250 | | #endif |
251 | 0 | seed_sources = seeds; |
252 | 0 | } |
253 | 0 | return 1; |
254 | 0 | } |
255 | | |
256 | | const char *OPENSSL_info(int t) |
257 | 0 | { |
258 | | /* |
259 | | * We don't care about the result. Worst case scenario, the strings |
260 | | * won't be initialised, i.e. remain NULL, which means that the info |
261 | | * isn't available anyway... |
262 | | */ |
263 | 0 | (void)RUN_ONCE(&init_info, init_info_strings); |
264 | |
|
265 | 0 | switch (t) { |
266 | 0 | case OPENSSL_INFO_CONFIG_DIR: |
267 | 0 | return ossl_get_openssldir(); |
268 | 0 | case OPENSSL_INFO_ENGINES_DIR: |
269 | 0 | return NULL; |
270 | 0 | case OPENSSL_INFO_MODULES_DIR: |
271 | 0 | return ossl_get_modulesdir(); |
272 | 0 | case OPENSSL_INFO_DSO_EXTENSION: |
273 | 0 | return DSO_EXTENSION; |
274 | 0 | case OPENSSL_INFO_DIR_FILENAME_SEPARATOR: |
275 | | #if defined(_WIN32) |
276 | | return "\\"; |
277 | | #elif defined(__VMS) |
278 | | return ""; |
279 | | #else /* Assume POSIX */ |
280 | 0 | return "/"; |
281 | 0 | #endif |
282 | 0 | case OPENSSL_INFO_LIST_SEPARATOR: { |
283 | 0 | static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' }; |
284 | 0 | return list_sep; |
285 | 0 | } |
286 | 0 | case OPENSSL_INFO_SEED_SOURCE: |
287 | 0 | return seed_sources; |
288 | 0 | case OPENSSL_INFO_CPU_SETTINGS: |
289 | | /* |
290 | | * If successfully initialized, ossl_cpu_info_str will start |
291 | | * with CPUINFO_PREFIX, if failed it will be an empty string. |
292 | | * Strip away the CPUINFO_PREFIX which we don't need here. |
293 | | */ |
294 | 0 | if (ossl_cpu_info_str[0] != '\0') |
295 | 0 | return ossl_cpu_info_str + strlen(CPUINFO_PREFIX); |
296 | 0 | break; |
297 | 0 | case OPENSSL_INFO_WINDOWS_CONTEXT: |
298 | 0 | return ossl_get_wininstallcontext(); |
299 | 0 | default: |
300 | 0 | break; |
301 | 0 | } |
302 | | /* Not an error */ |
303 | 0 | return NULL; |
304 | 0 | } |