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