Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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) || \
53
0
     defined(__x86_64) || defined(__x86_64__) || \
54
0
     defined(_M_AMD64) || defined(_M_X64)
55
0
    const char *env;
56
57
0
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
58
0
                 CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
59
0
                 (unsigned long long)OPENSSL_ia32cap_P[0] |
60
0
                 (unsigned long long)OPENSSL_ia32cap_P[1] << 32,
61
0
                 (unsigned long long)OPENSSL_ia32cap_P[2] |
62
0
                 (unsigned long long)OPENSSL_ia32cap_P[3] << 32);
63
0
    if ((env = getenv("OPENSSL_ia32cap")) != NULL)
64
0
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
65
0
                     sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
66
0
                     " 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
0
#endif
177
178
0
    {
179
0
        static char seeds[512] = "";
180
181
0
#define add_seeds_string(str)                                           \
182
0
        do {                                                            \
183
0
            if (seeds[0] != '\0')                                       \
184
0
                OPENSSL_strlcat(seeds, " ", sizeof(seeds));             \
185
0
            OPENSSL_strlcat(seeds, str, sizeof(seeds));                 \
186
0
        } while (0)
187
0
#define add_seeds_stringlist(label, strlist)                            \
188
0
        do {                                                            \
189
0
            add_seeds_string(label "(");                                \
190
0
            {                                                           \
191
0
                const char *dev[] =  { strlist, NULL };                 \
192
0
                const char **p;                                         \
193
0
                int first = 1;                                          \
194
0
                                                                        \
195
0
                for (p = dev; *p != NULL; p++) {                        \
196
0
                    if (!first)                                         \
197
0
                        OPENSSL_strlcat(seeds, " ", sizeof(seeds));     \
198
0
                    first = 0;                                          \
199
0
                    OPENSSL_strlcat(seeds, *p, sizeof(seeds));          \
200
0
                }                                                       \
201
0
            }                                                           \
202
0
            OPENSSL_strlcat(seeds, ")", sizeof(seeds));                 \
203
0
        } 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
0
#ifdef OPENSSL_RAND_SEED_OS
228
0
        add_seeds_string("os-specific");
229
0
#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
0
        seed_sources = seeds;
239
0
    }
240
0
    return 1;
241
0
}
242
243
const char *OPENSSL_info(int t)
244
0
{
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
0
    (void)RUN_ONCE(&init_info, init_info_strings);
251
252
0
    switch (t) {
253
0
    case OPENSSL_INFO_CONFIG_DIR:
254
0
        return ossl_get_openssldir();
255
0
    case OPENSSL_INFO_ENGINES_DIR:
256
0
        return ossl_get_enginesdir();
257
0
    case OPENSSL_INFO_MODULES_DIR:
258
0
        return ossl_get_modulesdir();
259
0
    case OPENSSL_INFO_DSO_EXTENSION:
260
0
        return DSO_EXTENSION;
261
0
    case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
262
#if defined(_WIN32)
263
        return "\\";
264
#elif defined(__VMS)
265
        return "";
266
#else  /* Assume POSIX */
267
0
        return "/";
268
0
#endif
269
0
    case OPENSSL_INFO_LIST_SEPARATOR:
270
0
        {
271
0
            static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
272
0
            return list_sep;
273
0
        }
274
0
    case OPENSSL_INFO_SEED_SOURCE:
275
0
        return seed_sources;
276
0
    case OPENSSL_INFO_CPU_SETTINGS:
277
        /*
278
         * If successfully initialized, ossl_cpu_info_str will start
279
         * with CPUINFO_PREFIX, if failed it will be an empty string.
280
         * Strip away the CPUINFO_PREFIX which we don't need here.
281
         */
282
0
        if (ossl_cpu_info_str[0] != '\0')
283
0
            return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
284
0
        break;
285
0
    case OPENSSL_INFO_WINDOWS_CONTEXT:
286
0
        return ossl_get_wininstallcontext();
287
0
    default:
288
0
        break;
289
0
    }
290
    /* Not an error */
291
0
    return NULL;
292
0
}