Coverage Report

Created: 2025-11-11 06:20

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