Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/info.c

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