1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "s2n_pq.h"
static bool sikep434r2_asm_enabled = false;
#if defined(S2N_CPUID_AVAILABLE)
/* https://en.wikipedia.org/wiki/CPUID */
#include <cpuid.h>
#define EXTENDED_FEATURES_LEAF 7
#define EXTENDED_FEATURES_SUBLEAF_ZERO 0
/* The cpuid.h header included with older versions of gcc and
* clang doesn't include definitions for bit_ADX, bit_BMI2, or
* __get_cpuid_count(). */
#if !defined(bit_ADX)
#define bit_ADX (1 << 19)
#endif
#if !defined(bit_BMI2)
#define bit_BMI2 (1 << 8)
#endif
bool s2n_get_cpuid_count(uint32_t leaf, uint32_t sub_leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) {
/* 0x80000000 probes for extended cpuid info */
uint32_t max_level = __get_cpuid_max(leaf & 0x80000000, 0);
if (max_level == 0 || max_level < leaf) {
return false;
}
__cpuid_count(leaf, sub_leaf, *eax, *ebx, *ecx, *edx);
return true;
}
/* https://en.wikipedia.org/wiki/Bit_manipulation_instruction_set#BMI2_(Bit_Manipulation_Instruction_Set_2) */
bool s2n_cpu_supports_bmi2() {
uint32_t eax, ebx, ecx, edx;
if (!s2n_get_cpuid_count(EXTENDED_FEATURES_LEAF, EXTENDED_FEATURES_SUBLEAF_ZERO, &eax, &ebx, &ecx, &edx)) {
return false;
}
return (ebx & bit_BMI2);
}
/* https://en.wikipedia.org/wiki/Intel_ADX */
bool s2n_cpu_supports_adx() {
uint32_t eax, ebx, ecx, edx;
if (!s2n_get_cpuid_count(EXTENDED_FEATURES_LEAF, EXTENDED_FEATURES_SUBLEAF_ZERO, &eax, &ebx, &ecx, &edx)) {
return false;
}
return (ebx & bit_ADX);
}
bool s2n_cpu_supports_sikep434r2_asm() {
#if defined(S2N_SIKEP434R2_ASM)
/* The sikep434r2 assembly code always requires BMI2. If the assembly
* was compiled with support for ADX, we also require ADX at runtime. */
#if defined(S2N_ADX)
return s2n_cpu_supports_bmi2() && s2n_cpu_supports_adx();
#else
return s2n_cpu_supports_bmi2();
#endif
#else
/* sikep434r2 assembly was not supported at compile time */
return false;
#endif /* defined(S2N_SIKEP434R2_ASM) */
}
#else /* defined(S2N_CPUID_AVAILABLE) */
/* If CPUID is not available, we cannot perform necessary run-time checks. */
bool s2n_cpu_supports_sikep434r2_asm() {
return false;
}
#endif /* defined(S2N_CPUID_AVAILABLE) */
bool s2n_sikep434r2_asm_is_enabled() {
return sikep434r2_asm_enabled;
}
bool s2n_pq_is_enabled() {
#if defined(S2N_NO_PQ)
return false;
#else
return !s2n_is_in_fips_mode();
#endif
}
S2N_RESULT s2n_disable_sikep434r2_asm() {
sikep434r2_asm_enabled = false;
return S2N_RESULT_OK;
}
S2N_RESULT s2n_try_enable_sikep434r2_asm() {
if (s2n_pq_is_enabled() && s2n_cpu_supports_sikep434r2_asm()) {
sikep434r2_asm_enabled = true;
}
return S2N_RESULT_OK;
}
S2N_RESULT s2n_pq_init() {
ENSURE_OK(s2n_try_enable_sikep434r2_asm(), S2N_ERR_SAFETY);
return S2N_RESULT_OK;
}
|