Coverage Report

Created: 2025-06-13 06:58

/src/openssl/crypto/cpuid.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1998-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 "internal/e_os.h"
11
#include "crypto/cryptlib.h"
12
13
#if     defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
14
        defined(__x86_64) || defined(__x86_64__) || \
15
        defined(_M_AMD64) || defined(_M_X64)
16
17
extern unsigned int OPENSSL_ia32cap_P[OPENSSL_IA32CAP_P_MAX_INDEXES];
18
19
# if defined(OPENSSL_CPUID_OBJ)
20
21
/*
22
 * Purpose of these minimalistic and character-type-agnostic subroutines
23
 * is to break dependency on MSVCRT (on Windows) and locale. This makes
24
 * OPENSSL_cpuid_setup safe to use as "constructor". "Character-type-
25
 * agnostic" means that they work with either wide or 8-bit characters,
26
 * exploiting the fact that first 127 characters can be simply casted
27
 * between the sets, while the rest would be simply rejected by ossl_is*
28
 * subroutines.
29
 */
30
#  ifdef _WIN32
31
typedef WCHAR variant_char;
32
#   define OPENSSL_IA32CAP_P_MAX_CHAR_SIZE 256
33
static variant_char *ossl_getenv(const char *name)
34
{
35
    /*
36
     * Since we pull only one environment variable, it's simpler to
37
     * just ignore |name| and use equivalent wide-char L-literal.
38
     * As well as to ignore excessively long values...
39
     */
40
    static WCHAR value[OPENSSL_IA32CAP_P_MAX_CHAR_SIZE];
41
    DWORD len = GetEnvironmentVariableW(L"OPENSSL_ia32cap", value, OPENSSL_IA32CAP_P_MAX_CHAR_SIZE);
42
43
    return (len > 0 && len < OPENSSL_IA32CAP_P_MAX_CHAR_SIZE) ? value : NULL;
44
}
45
#  else
46
typedef char variant_char;
47
53
#   define ossl_getenv getenv
48
#  endif
49
50
#  include "crypto/ctype.h"
51
52
static int todigit(variant_char c)
53
0
{
54
0
    if (ossl_isdigit(c))
55
0
        return c - '0';
56
0
    else if (ossl_isxdigit(c))
57
0
        return ossl_tolower(c) - 'a' + 10;
58
59
    /* return largest base value to make caller terminate the loop */
60
0
    return 16;
61
0
}
62
63
static uint64_t ossl_strtouint64(const variant_char *str)
64
0
{
65
0
    uint64_t ret = 0;
66
0
    unsigned int digit, base = 10;
67
68
0
    if (*str == '0') {
69
0
        base = 8, str++;
70
0
        if (ossl_tolower(*str) == 'x')
71
0
            base = 16, str++;
72
0
    }
73
74
0
    while ((digit = todigit(*str++)) < base)
75
0
        ret = ret * base + digit;
76
77
0
    return ret;
78
0
}
79
80
static variant_char *ossl_strchr(const variant_char *str, char srch)
81
0
{   variant_char c;
82
83
0
    while ((c = *str)) {
84
0
        if (c == srch)
85
0
            return (variant_char *)str;
86
0
        str++;
87
0
    }
88
89
0
    return NULL;
90
0
}
91
92
#  define OPENSSL_CPUID_SETUP
93
typedef uint64_t IA32CAP;
94
95
void OPENSSL_cpuid_setup(void)
96
53
{
97
53
    static int trigger = 0;
98
53
    IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
99
53
    IA32CAP vec;
100
53
    const variant_char *env;
101
53
    int index = 2;
102
103
53
    if (trigger)
104
0
        return;
105
106
53
    trigger = 1;
107
53
    if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) {
108
0
        int off = (env[0] == '~') ? 1 : 0;
109
110
0
        vec = ossl_strtouint64(env + off);
111
112
0
        if (off) {
113
0
            IA32CAP mask = vec;
114
0
            vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
115
0
            if (mask & (1<<24)) {
116
                /*
117
                 * User disables FXSR bit, mask even other capabilities
118
                 * that operate exclusively on XMM, so we don't have to
119
                 * double-check all the time. We mask PCLMULQDQ, AMD XOP,
120
                 * AES-NI and AVX. Formally speaking we don't have to
121
                 * do it in x86_64 case, but we can safely assume that
122
                 * x86_64 users won't actually flip this flag.
123
                 */
124
0
                vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
125
0
            }
126
0
        } else if (env[0] == ':') {
127
0
            vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
128
0
        }
129
130
        /* Processed indexes 0, 1 */
131
0
        if ((env = ossl_strchr(env, ':')) != NULL)
132
0
            env++;
133
0
        for (; index < OPENSSL_IA32CAP_P_MAX_INDEXES; index += 2) {
134
0
            if ((env != NULL) && (env[0] != '\0')) {
135
                /* if env[0] == ':' current index is skipped */
136
0
                if (env[0] != ':') {
137
0
                    IA32CAP vecx;
138
139
0
                    off = (env[0] == '~') ? 1 : 0;
140
0
                    vecx = ossl_strtouint64(env + off);
141
0
                    if (off) {
142
0
                        OPENSSL_ia32cap_P[index] &= ~(unsigned int)vecx;
143
0
                        OPENSSL_ia32cap_P[index + 1] &= ~(unsigned int)(vecx >> 32);
144
0
                    } else {
145
0
                        OPENSSL_ia32cap_P[index] = (unsigned int)vecx;
146
0
                        OPENSSL_ia32cap_P[index + 1] = (unsigned int)(vecx >> 32);
147
0
                    }
148
0
                }
149
                /* skip delimeter */
150
0
                if ((env = ossl_strchr(env, ':')) != NULL)
151
0
                    env++;
152
0
            } else { /* zeroize the next two indexes */
153
0
                OPENSSL_ia32cap_P[index] = 0;
154
0
                OPENSSL_ia32cap_P[index + 1] = 0;
155
0
            }
156
0
        }
157
158
        /* If AVX10 is disabled, zero out its detailed cap bits */
159
0
        if (!(OPENSSL_ia32cap_P[6] & (1 << 19)))
160
0
            OPENSSL_ia32cap_P[9] = 0;
161
53
    } else {
162
53
        vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
163
53
    }
164
165
    /*
166
     * |(1<<10) sets a reserved bit to signal that variable
167
     * was initialized already... This is to avoid interference
168
     * with cpuid snippets in ELF .init segment.
169
     */
170
53
    OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
171
53
    OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
172
53
}
173
# else
174
unsigned int OPENSSL_ia32cap_P[OPENSSL_IA32CAP_P_MAX_INDEXES];
175
# endif
176
#endif
177
178
#ifndef OPENSSL_CPUID_OBJ
179
# ifndef OPENSSL_CPUID_SETUP
180
void OPENSSL_cpuid_setup(void)
181
{
182
}
183
# endif
184
185
/*
186
 * The rest are functions that are defined in the same assembler files as
187
 * the CPUID functionality.
188
 */
189
190
/*
191
 * The volatile is used to ensure that the compiler generates code that reads
192
 * all values from the array and doesn't try to optimize this away. The standard
193
 * doesn't actually require this behavior if the original data pointed to is
194
 * not volatile, but compilers do this in practice anyway.
195
 *
196
 * There are also assembler versions of this function.
197
 */
198
# undef CRYPTO_memcmp
199
int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
200
{
201
    size_t i;
202
    const volatile unsigned char *a = in_a;
203
    const volatile unsigned char *b = in_b;
204
    unsigned char x = 0;
205
206
    for (i = 0; i < len; i++)
207
        x |= a[i] ^ b[i];
208
209
    return x;
210
}
211
212
/*
213
 * For systems that don't provide an instruction counter register or equivalent.
214
 */
215
uint32_t OPENSSL_rdtsc(void)
216
{
217
    return 0;
218
}
219
220
size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
221
{
222
    return 0;
223
}
224
225
size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
226
{
227
    return 0;
228
}
229
#endif