]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/info.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / crypto / info.c
1 /*
2 * Copyright 2019-2023 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 #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
19 # include "arm_arch.h"
20 # define CPU_INFO_STR_LEN 128
21 #elif defined(__s390__) || defined(__s390x__)
22 # include "s390x_arch.h"
23 # define CPU_INFO_STR_LEN 2048
24 #elif defined(__riscv)
25 # include "crypto/riscv_arch.h"
26 # define CPU_INFO_STR_LEN 2048
27 #else
28 # define CPU_INFO_STR_LEN 128
29 #endif
30
31 /* extern declaration to avoid warning */
32 extern char ossl_cpu_info_str[];
33
34 static char *seed_sources = NULL;
35
36 char ossl_cpu_info_str[CPU_INFO_STR_LEN] = "";
37 #define CPUINFO_PREFIX "CPUINFO: "
38
39 static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
40
41 DEFINE_RUN_ONCE_STATIC(init_info_strings)
42 {
43 #if defined(OPENSSL_CPUID_OBJ)
44 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
45 defined(__x86_64) || defined(__x86_64__) || \
46 defined(_M_AMD64) || defined(_M_X64)
47 const char *env;
48
49 BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
50 CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
51 (unsigned long long)OPENSSL_ia32cap_P[0] |
52 (unsigned long long)OPENSSL_ia32cap_P[1] << 32,
53 (unsigned long long)OPENSSL_ia32cap_P[2] |
54 (unsigned long long)OPENSSL_ia32cap_P[3] << 32);
55 if ((env = getenv("OPENSSL_ia32cap")) != NULL)
56 BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
57 sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
58 " env:%s", env);
59 # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
60 const char *env;
61
62 BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
63 CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
64 if ((env = getenv("OPENSSL_armcap")) != NULL)
65 BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
66 sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
67 " env:%s", env);
68 # elif defined(__s390__) || defined(__s390x__)
69 const char *env;
70
71 BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
72 CPUINFO_PREFIX "OPENSSL_s390xcap="
73 "stfle:0x%llx:0x%llx:0x%llx:0x%llx:"
74 "kimd:0x%llx:0x%llx:"
75 "klmd:0x%llx:0x%llx:"
76 "km:0x%llx:0x%llx:"
77 "kmc:0x%llx:0x%llx:"
78 "kmac:0x%llx:0x%llx:"
79 "kmctr:0x%llx:0x%llx:"
80 "kmo:0x%llx:0x%llx:"
81 "kmf:0x%llx:0x%llx:"
82 "prno:0x%llx:0x%llx:"
83 "kma:0x%llx:0x%llx:"
84 "pcc:0x%llx:0x%llx:"
85 "kdsa:0x%llx:0x%llx",
86 OPENSSL_s390xcap_P.stfle[0], OPENSSL_s390xcap_P.stfle[1],
87 OPENSSL_s390xcap_P.stfle[2], OPENSSL_s390xcap_P.stfle[3],
88 OPENSSL_s390xcap_P.kimd[0], OPENSSL_s390xcap_P.kimd[1],
89 OPENSSL_s390xcap_P.klmd[0], OPENSSL_s390xcap_P.klmd[1],
90 OPENSSL_s390xcap_P.km[0], OPENSSL_s390xcap_P.km[1],
91 OPENSSL_s390xcap_P.kmc[0], OPENSSL_s390xcap_P.kmc[1],
92 OPENSSL_s390xcap_P.kmac[0], OPENSSL_s390xcap_P.kmac[1],
93 OPENSSL_s390xcap_P.kmctr[0], OPENSSL_s390xcap_P.kmctr[1],
94 OPENSSL_s390xcap_P.kmo[0], OPENSSL_s390xcap_P.kmo[1],
95 OPENSSL_s390xcap_P.kmf[0], OPENSSL_s390xcap_P.kmf[1],
96 OPENSSL_s390xcap_P.prno[0], OPENSSL_s390xcap_P.prno[1],
97 OPENSSL_s390xcap_P.kma[0], OPENSSL_s390xcap_P.kma[1],
98 OPENSSL_s390xcap_P.pcc[0], OPENSSL_s390xcap_P.pcc[1],
99 OPENSSL_s390xcap_P.kdsa[0], OPENSSL_s390xcap_P.kdsa[1]);
100 if ((env = getenv("OPENSSL_s390xcap")) != NULL)
101 BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
102 sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
103 " env:%s", env);
104 # elif defined(__riscv)
105 const char *env;
106 char sep = '=';
107
108 BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
109 CPUINFO_PREFIX "OPENSSL_riscvcap");
110 for (size_t i = 0; i < kRISCVNumCaps; ++i) {
111 if (OPENSSL_riscvcap_P[RISCV_capabilities[i].index]
112 & (1 << RISCV_capabilities[i].bit_offset)) {
113 /* Match, display the name */
114 BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
115 sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
116 "%c%s", sep, RISCV_capabilities[i].name);
117 /* Only the first sep is '=' */
118 sep = '_';
119 }
120 }
121 /* If no capability is found, add back the = */
122 if (sep == '=') {
123 BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
124 sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
125 "%c", sep);
126 }
127 if ((env = getenv("OPENSSL_riscvcap")) != NULL)
128 BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
129 sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
130 " env:%s", env);
131 # endif
132 #endif
133
134 {
135 static char seeds[512] = "";
136
137 #define add_seeds_string(str) \
138 do { \
139 if (seeds[0] != '\0') \
140 OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
141 OPENSSL_strlcat(seeds, str, sizeof(seeds)); \
142 } while (0)
143 #define add_seeds_stringlist(label, strlist) \
144 do { \
145 add_seeds_string(label "("); \
146 { \
147 const char *dev[] = { strlist, NULL }; \
148 const char **p; \
149 int first = 1; \
150 \
151 for (p = dev; *p != NULL; p++) { \
152 if (!first) \
153 OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
154 first = 0; \
155 OPENSSL_strlcat(seeds, *p, sizeof(seeds)); \
156 } \
157 } \
158 OPENSSL_strlcat(seeds, ")", sizeof(seeds)); \
159 } while (0)
160
161 #ifdef OPENSSL_RAND_SEED_NONE
162 add_seeds_string("none");
163 #endif
164 #ifdef OPENSSL_RAND_SEED_RDTSC
165 add_seeds_string("rdtsc");
166 #endif
167 #ifdef OPENSSL_RAND_SEED_RDCPU
168 # ifdef __aarch64__
169 add_seeds_string("rndr ( rndrrs rndr )");
170 # else
171 add_seeds_string("rdrand ( rdseed rdrand )");
172 # endif
173 #endif
174 #ifdef OPENSSL_RAND_SEED_GETRANDOM
175 add_seeds_string("getrandom-syscall");
176 #endif
177 #ifdef OPENSSL_RAND_SEED_DEVRANDOM
178 add_seeds_stringlist("random-device", DEVRANDOM);
179 #endif
180 #ifdef OPENSSL_RAND_SEED_EGD
181 add_seeds_stringlist("EGD", DEVRANDOM_EGD);
182 #endif
183 #ifdef OPENSSL_RAND_SEED_OS
184 add_seeds_string("os-specific");
185 #endif
186 seed_sources = seeds;
187 }
188 return 1;
189 }
190
191 const char *OPENSSL_info(int t)
192 {
193 /*
194 * We don't care about the result. Worst case scenario, the strings
195 * won't be initialised, i.e. remain NULL, which means that the info
196 * isn't available anyway...
197 */
198 (void)RUN_ONCE(&init_info, init_info_strings);
199
200 switch (t) {
201 case OPENSSL_INFO_CONFIG_DIR:
202 return OPENSSLDIR;
203 case OPENSSL_INFO_ENGINES_DIR:
204 return ENGINESDIR;
205 case OPENSSL_INFO_MODULES_DIR:
206 return MODULESDIR;
207 case OPENSSL_INFO_DSO_EXTENSION:
208 return DSO_EXTENSION;
209 case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
210 #if defined(_WIN32)
211 return "\\";
212 #elif defined(__VMS)
213 return "";
214 #else /* Assume POSIX */
215 return "/";
216 #endif
217 case OPENSSL_INFO_LIST_SEPARATOR:
218 {
219 static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
220 return list_sep;
221 }
222 case OPENSSL_INFO_SEED_SOURCE:
223 return seed_sources;
224 case OPENSSL_INFO_CPU_SETTINGS:
225 /*
226 * If successfully initialized, ossl_cpu_info_str will start
227 * with CPUINFO_PREFIX, if failed it will be an empty string.
228 * Strip away the CPUINFO_PREFIX which we don't need here.
229 */
230 if (ossl_cpu_info_str[0] != '\0')
231 return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
232 break;
233 default:
234 break;
235 }
236 /* Not an error */
237 return NULL;
238 }