2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
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
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"
18 #ifndef OPENSSL_NO_JITTER
20 # include <jitterentropy.h>
23 #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
24 # include "arm_arch.h"
25 # define CPU_INFO_STR_LEN 128
26 #elif defined(__s390__) || defined(__s390x__)
27 # include "s390x_arch.h"
28 # define CPU_INFO_STR_LEN 2048
29 #elif defined(__riscv)
30 # include "crypto/riscv_arch.h"
31 # define CPU_INFO_STR_LEN 2048
33 # define CPU_INFO_STR_LEN 256
36 /* extern declaration to avoid warning */
37 extern char ossl_cpu_info_str
[];
39 static char *seed_sources
= NULL
;
41 char ossl_cpu_info_str
[CPU_INFO_STR_LEN
] = "";
42 #define CPUINFO_PREFIX "CPUINFO: "
44 static CRYPTO_ONCE init_info
= CRYPTO_ONCE_STATIC_INIT
;
46 DEFINE_RUN_ONCE_STATIC(init_info_strings
)
48 #if defined(OPENSSL_CPUID_OBJ)
49 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
50 defined(__x86_64) || defined(__x86_64__) || \
51 defined(_M_AMD64) || defined(_M_X64)
54 BIO_snprintf(ossl_cpu_info_str
, sizeof(ossl_cpu_info_str
),
55 CPUINFO_PREFIX
"OPENSSL_ia32cap=0x%.16llx:0x%.16llx:0x%.16llx:0x%.16llx:0x%.16llx",
56 (unsigned long long)OPENSSL_ia32cap_P
[0] |
57 (unsigned long long)OPENSSL_ia32cap_P
[1] << 32,
58 (unsigned long long)OPENSSL_ia32cap_P
[2] |
59 (unsigned long long)OPENSSL_ia32cap_P
[3] << 32,
60 (unsigned long long)OPENSSL_ia32cap_P
[4] |
61 (unsigned long long)OPENSSL_ia32cap_P
[5] << 32,
62 (unsigned long long)OPENSSL_ia32cap_P
[6] |
63 (unsigned long long)OPENSSL_ia32cap_P
[7] << 32,
64 (unsigned long long)OPENSSL_ia32cap_P
[8] |
65 (unsigned long long)OPENSSL_ia32cap_P
[9] << 32);
67 if ((env
= getenv("OPENSSL_ia32cap")) != NULL
)
68 BIO_snprintf(ossl_cpu_info_str
+ strlen(ossl_cpu_info_str
),
69 sizeof(ossl_cpu_info_str
) - strlen(ossl_cpu_info_str
),
71 # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
74 BIO_snprintf(ossl_cpu_info_str
, sizeof(ossl_cpu_info_str
),
75 CPUINFO_PREFIX
"OPENSSL_armcap=0x%x", OPENSSL_armcap_P
);
76 if ((env
= getenv("OPENSSL_armcap")) != NULL
)
77 BIO_snprintf(ossl_cpu_info_str
+ strlen(ossl_cpu_info_str
),
78 sizeof(ossl_cpu_info_str
) - strlen(ossl_cpu_info_str
),
80 # elif defined(__s390__) || defined(__s390x__)
83 BIO_snprintf(ossl_cpu_info_str
, sizeof(ossl_cpu_info_str
),
84 CPUINFO_PREFIX
"OPENSSL_s390xcap="
85 "stfle:0x%llx:0x%llx:0x%llx:0x%llx:"
91 "kmctr:0x%llx:0x%llx:"
98 OPENSSL_s390xcap_P
.stfle
[0], OPENSSL_s390xcap_P
.stfle
[1],
99 OPENSSL_s390xcap_P
.stfle
[2], OPENSSL_s390xcap_P
.stfle
[3],
100 OPENSSL_s390xcap_P
.kimd
[0], OPENSSL_s390xcap_P
.kimd
[1],
101 OPENSSL_s390xcap_P
.klmd
[0], OPENSSL_s390xcap_P
.klmd
[1],
102 OPENSSL_s390xcap_P
.km
[0], OPENSSL_s390xcap_P
.km
[1],
103 OPENSSL_s390xcap_P
.kmc
[0], OPENSSL_s390xcap_P
.kmc
[1],
104 OPENSSL_s390xcap_P
.kmac
[0], OPENSSL_s390xcap_P
.kmac
[1],
105 OPENSSL_s390xcap_P
.kmctr
[0], OPENSSL_s390xcap_P
.kmctr
[1],
106 OPENSSL_s390xcap_P
.kmo
[0], OPENSSL_s390xcap_P
.kmo
[1],
107 OPENSSL_s390xcap_P
.kmf
[0], OPENSSL_s390xcap_P
.kmf
[1],
108 OPENSSL_s390xcap_P
.prno
[0], OPENSSL_s390xcap_P
.prno
[1],
109 OPENSSL_s390xcap_P
.kma
[0], OPENSSL_s390xcap_P
.kma
[1],
110 OPENSSL_s390xcap_P
.pcc
[0], OPENSSL_s390xcap_P
.pcc
[1],
111 OPENSSL_s390xcap_P
.kdsa
[0], OPENSSL_s390xcap_P
.kdsa
[1]);
112 if ((env
= getenv("OPENSSL_s390xcap")) != NULL
)
113 BIO_snprintf(ossl_cpu_info_str
+ strlen(ossl_cpu_info_str
),
114 sizeof(ossl_cpu_info_str
) - strlen(ossl_cpu_info_str
),
116 # elif defined(__riscv)
120 BIO_snprintf(ossl_cpu_info_str
, sizeof(ossl_cpu_info_str
),
121 CPUINFO_PREFIX
"OPENSSL_riscvcap");
122 for (size_t i
= 0; i
< kRISCVNumCaps
; ++i
) {
123 if (OPENSSL_riscvcap_P
[RISCV_capabilities
[i
].index
]
124 & (1 << RISCV_capabilities
[i
].bit_offset
)) {
125 /* Match, display the name */
126 BIO_snprintf(ossl_cpu_info_str
+ strlen(ossl_cpu_info_str
),
127 sizeof(ossl_cpu_info_str
) - strlen(ossl_cpu_info_str
),
128 "%c%s", sep
, RISCV_capabilities
[i
].name
);
129 /* Only the first sep is '=' */
133 /* If no capability is found, add back the = */
135 BIO_snprintf(ossl_cpu_info_str
+ strlen(ossl_cpu_info_str
),
136 sizeof(ossl_cpu_info_str
) - strlen(ossl_cpu_info_str
),
139 if ((env
= getenv("OPENSSL_riscvcap")) != NULL
)
140 BIO_snprintf(ossl_cpu_info_str
+ strlen(ossl_cpu_info_str
),
141 sizeof(ossl_cpu_info_str
) - strlen(ossl_cpu_info_str
),
147 static char seeds
[512] = "";
149 #define add_seeds_string(str) \
151 if (seeds[0] != '\0') \
152 OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
153 OPENSSL_strlcat(seeds, str, sizeof(seeds)); \
155 #define add_seeds_stringlist(label, strlist) \
157 add_seeds_string(label "("); \
159 const char *dev[] = { strlist, NULL }; \
163 for (p = dev; *p != NULL; p++) { \
165 OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
167 OPENSSL_strlcat(seeds, *p, sizeof(seeds)); \
170 OPENSSL_strlcat(seeds, ")", sizeof(seeds)); \
173 #ifdef OPENSSL_RAND_SEED_NONE
174 add_seeds_string("none");
176 #ifdef OPENSSL_RAND_SEED_RDTSC
177 add_seeds_string("rdtsc");
179 #ifdef OPENSSL_RAND_SEED_RDCPU
181 add_seeds_string("rndr ( rndrrs rndr )");
183 add_seeds_string("rdrand ( rdseed rdrand )");
186 #ifdef OPENSSL_RAND_SEED_GETRANDOM
187 add_seeds_string("getrandom-syscall");
189 #ifdef OPENSSL_RAND_SEED_DEVRANDOM
190 add_seeds_stringlist("random-device", DEVRANDOM
);
192 #ifdef OPENSSL_RAND_SEED_EGD
193 add_seeds_stringlist("EGD", DEVRANDOM_EGD
);
195 #ifdef OPENSSL_RAND_SEED_OS
196 add_seeds_string("os-specific");
198 #ifndef OPENSSL_NO_JITTER
202 BIO_snprintf(buf
, sizeof(buf
), "JITTER (%d)", jent_version());
203 add_seeds_string(buf
);
206 seed_sources
= seeds
;
211 const char *OPENSSL_info(int t
)
214 * We don't care about the result. Worst case scenario, the strings
215 * won't be initialised, i.e. remain NULL, which means that the info
216 * isn't available anyway...
218 (void)RUN_ONCE(&init_info
, init_info_strings
);
221 case OPENSSL_INFO_CONFIG_DIR
:
222 return ossl_get_openssldir();
223 case OPENSSL_INFO_ENGINES_DIR
:
224 return ossl_get_enginesdir();
225 case OPENSSL_INFO_MODULES_DIR
:
226 return ossl_get_modulesdir();
227 case OPENSSL_INFO_DSO_EXTENSION
:
228 return DSO_EXTENSION
;
229 case OPENSSL_INFO_DIR_FILENAME_SEPARATOR
:
234 #else /* Assume POSIX */
237 case OPENSSL_INFO_LIST_SEPARATOR
:
239 static const char list_sep
[] = { LIST_SEPARATOR_CHAR
, '\0' };
242 case OPENSSL_INFO_SEED_SOURCE
:
244 case OPENSSL_INFO_CPU_SETTINGS
:
246 * If successfully initialized, ossl_cpu_info_str will start
247 * with CPUINFO_PREFIX, if failed it will be an empty string.
248 * Strip away the CPUINFO_PREFIX which we don't need here.
250 if (ossl_cpu_info_str
[0] != '\0')
251 return ossl_cpu_info_str
+ strlen(CPUINFO_PREFIX
);
253 case OPENSSL_INFO_WINDOWS_CONTEXT
:
254 return ossl_get_wininstallcontext();