Coverage Report

Created: 2025-12-31 06:58

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