Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
#if defined(__arm__) || defined(__arm) || defined(__aarch64__)
19
#include "arm_arch.h"
20
#define CPU_INFO_STR_LEN 128
21
#elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)
22
#include "crypto/ppc_arch.h"
23
#define CPU_INFO_STR_LEN 128
24
#elif defined(__s390__) || defined(__s390x__)
25
#include "s390x_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
0
#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
0
{
43
0
#if defined(OPENSSL_CPUID_OBJ)
44
0
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
45
0
    const char *env;
46
47
0
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
48
0
        CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
49
0
        (unsigned long long)OPENSSL_ia32cap_P[0] | (unsigned long long)OPENSSL_ia32cap_P[1] << 32,
50
0
        (unsigned long long)OPENSSL_ia32cap_P[2] | (unsigned long long)OPENSSL_ia32cap_P[3] << 32);
51
0
    if ((env = getenv("OPENSSL_ia32cap")) != NULL)
52
0
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
53
0
            sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
54
0
            " env:%s", env);
55
#elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
56
    const char *env;
57
58
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
59
        CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
60
    if ((env = getenv("OPENSSL_armcap")) != NULL)
61
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
62
            sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
63
            " env:%s", env);
64
#elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)
65
    const char *env;
66
67
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
68
        CPUINFO_PREFIX "OPENSSL_ppccap=0x%x", OPENSSL_ppccap_P);
69
    if ((env = getenv("OPENSSL_ppccap")) != NULL)
70
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
71
            sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
72
            " env:%s", env);
73
#elif defined(__s390__) || defined(__s390x__)
74
    const char *env;
75
76
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
77
        CPUINFO_PREFIX "OPENSSL_s390xcap="
78
                       "stfle:0x%llx:0x%llx:0x%llx:0x%llx:"
79
                       "kimd:0x%llx:0x%llx:"
80
                       "klmd:0x%llx:0x%llx:"
81
                       "km:0x%llx:0x%llx:"
82
                       "kmc:0x%llx:0x%llx:"
83
                       "kmac:0x%llx:0x%llx:"
84
                       "kmctr:0x%llx:0x%llx:"
85
                       "kmo:0x%llx:0x%llx:"
86
                       "kmf:0x%llx:0x%llx:"
87
                       "prno:0x%llx:0x%llx:"
88
                       "kma:0x%llx:0x%llx:"
89
                       "pcc:0x%llx:0x%llx:"
90
                       "kdsa:0x%llx:0x%llx",
91
        OPENSSL_s390xcap_P.stfle[0], OPENSSL_s390xcap_P.stfle[1],
92
        OPENSSL_s390xcap_P.stfle[2], OPENSSL_s390xcap_P.stfle[3],
93
        OPENSSL_s390xcap_P.kimd[0], OPENSSL_s390xcap_P.kimd[1],
94
        OPENSSL_s390xcap_P.klmd[0], OPENSSL_s390xcap_P.klmd[1],
95
        OPENSSL_s390xcap_P.km[0], OPENSSL_s390xcap_P.km[1],
96
        OPENSSL_s390xcap_P.kmc[0], OPENSSL_s390xcap_P.kmc[1],
97
        OPENSSL_s390xcap_P.kmac[0], OPENSSL_s390xcap_P.kmac[1],
98
        OPENSSL_s390xcap_P.kmctr[0], OPENSSL_s390xcap_P.kmctr[1],
99
        OPENSSL_s390xcap_P.kmo[0], OPENSSL_s390xcap_P.kmo[1],
100
        OPENSSL_s390xcap_P.kmf[0], OPENSSL_s390xcap_P.kmf[1],
101
        OPENSSL_s390xcap_P.prno[0], OPENSSL_s390xcap_P.prno[1],
102
        OPENSSL_s390xcap_P.kma[0], OPENSSL_s390xcap_P.kma[1],
103
        OPENSSL_s390xcap_P.pcc[0], OPENSSL_s390xcap_P.pcc[1],
104
        OPENSSL_s390xcap_P.kdsa[0], OPENSSL_s390xcap_P.kdsa[1]);
105
    if ((env = getenv("OPENSSL_s390xcap")) != NULL)
106
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
107
            sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
108
            " env:%s", env);
109
#endif
110
0
#endif
111
112
0
    {
113
0
        static char seeds[512] = "";
114
115
0
#define add_seeds_string(str)                           \
116
0
    do {                                                \
117
0
        if (seeds[0] != '\0')                           \
118
0
            OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
119
0
        OPENSSL_strlcat(seeds, str, sizeof(seeds));     \
120
0
    } while (0)
121
0
#define add_seeds_stringlist(label, strlist)                    \
122
0
    do {                                                        \
123
0
        add_seeds_string(label "(");                            \
124
0
        {                                                       \
125
0
            const char *dev[] = { strlist, NULL };              \
126
0
            const char **p;                                     \
127
0
            int first = 1;                                      \
128
0
                                                                \
129
0
            for (p = dev; *p != NULL; p++) {                    \
130
0
                if (!first)                                     \
131
0
                    OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
132
0
                first = 0;                                      \
133
0
                OPENSSL_strlcat(seeds, *p, sizeof(seeds));      \
134
0
            }                                                   \
135
0
        }                                                       \
136
0
        OPENSSL_strlcat(seeds, ")", sizeof(seeds));             \
137
0
    } while (0)
138
139
#ifdef OPENSSL_RAND_SEED_NONE
140
        add_seeds_string("none");
141
#endif
142
#ifdef OPENSSL_RAND_SEED_RDTSC
143
        add_seeds_string("rdtsc");
144
#endif
145
#ifdef OPENSSL_RAND_SEED_RDCPU
146
#ifdef __aarch64__
147
        add_seeds_string("rndr ( rndrrs rndr )");
148
#else
149
        add_seeds_string("rdrand ( rdseed rdrand )");
150
#endif
151
#endif
152
#ifdef OPENSSL_RAND_SEED_LIBRANDOM
153
        add_seeds_string("C-library-random");
154
#endif
155
#ifdef OPENSSL_RAND_SEED_GETRANDOM
156
        add_seeds_string("getrandom-syscall");
157
#endif
158
#ifdef OPENSSL_RAND_SEED_DEVRANDOM
159
        add_seeds_stringlist("random-device", DEVRANDOM);
160
#endif
161
#ifdef OPENSSL_RAND_SEED_EGD
162
        add_seeds_stringlist("EGD", DEVRANDOM_EGD);
163
#endif
164
0
#ifdef OPENSSL_RAND_SEED_OS
165
0
        add_seeds_string("os-specific");
166
0
#endif
167
0
        seed_sources = seeds;
168
0
    }
169
0
    return 1;
170
0
}
171
172
const char *OPENSSL_info(int t)
173
0
{
174
    /*
175
     * We don't care about the result.  Worst case scenario, the strings
176
     * won't be initialised, i.e. remain NULL, which means that the info
177
     * isn't available anyway...
178
     */
179
0
    (void)RUN_ONCE(&init_info, init_info_strings);
180
181
0
    switch (t) {
182
0
    case OPENSSL_INFO_CONFIG_DIR:
183
0
        return OPENSSLDIR;
184
0
    case OPENSSL_INFO_ENGINES_DIR:
185
0
        return ENGINESDIR;
186
0
    case OPENSSL_INFO_MODULES_DIR:
187
0
        return MODULESDIR;
188
0
    case OPENSSL_INFO_DSO_EXTENSION:
189
0
        return DSO_EXTENSION;
190
0
    case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
191
#if defined(_WIN32)
192
        return "\\";
193
#elif defined(__VMS)
194
        return "";
195
#else /* Assume POSIX */
196
0
        return "/";
197
0
#endif
198
0
    case OPENSSL_INFO_LIST_SEPARATOR: {
199
0
        static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
200
0
        return list_sep;
201
0
    }
202
0
    case OPENSSL_INFO_SEED_SOURCE:
203
0
        return seed_sources;
204
0
    case OPENSSL_INFO_CPU_SETTINGS:
205
        /*
206
         * If successfully initialized, ossl_cpu_info_str will start
207
         * with CPUINFO_PREFIX, if failed it will be an empty string.
208
         * Strip away the CPUINFO_PREFIX which we don't need here.
209
         */
210
0
        if (ossl_cpu_info_str[0] != '\0')
211
0
            return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
212
0
        break;
213
0
    default:
214
0
        break;
215
0
    }
216
    /* Not an error */
217
0
    return NULL;
218
0
}