summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2024-11-16 13:05:35 +0000
committerjsing <>2024-11-16 13:05:35 +0000
commitfe7294a5c93bf90f080d28c3a7684b6e91757a35 (patch)
treee3abdc93f5a6fc1250a24940d0f3284fae1612aa /src/lib
parent39b7a3a9f20d985948b8d909c07ec87d5d1beb9e (diff)
downloadopenbsd-fe7294a5c93bf90f080d28c3a7684b6e91757a35.tar.gz
openbsd-fe7294a5c93bf90f080d28c3a7684b6e91757a35.tar.bz2
openbsd-fe7294a5c93bf90f080d28c3a7684b6e91757a35.zip
Add CPU capability detection for the Intel SHA extensions (aka SHA-NI).
This also provides a crypto_cpu_caps_amd64 variable that can be checked for CRYPTO_CPU_CAPS_AMD64_SHA. ok tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/arch/amd64/crypto_arch.h10
-rw-r--r--src/lib/libcrypto/arch/amd64/crypto_cpu_caps.c22
2 files changed, 27 insertions, 5 deletions
diff --git a/src/lib/libcrypto/arch/amd64/crypto_arch.h b/src/lib/libcrypto/arch/amd64/crypto_arch.h
index 64b2da587b..7546fb0dfd 100644
--- a/src/lib/libcrypto/arch/amd64/crypto_arch.h
+++ b/src/lib/libcrypto/arch/amd64/crypto_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_arch.h,v 1.3 2024/10/19 13:06:11 jsing Exp $ */ 1/* $OpenBSD: crypto_arch.h,v 1.4 2024/11/16 13:05:35 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -15,12 +15,20 @@
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */ 16 */
17 17
18#include <stdint.h>
19
18#ifndef HEADER_CRYPTO_ARCH_H 20#ifndef HEADER_CRYPTO_ARCH_H
19#define HEADER_CRYPTO_ARCH_H 21#define HEADER_CRYPTO_ARCH_H
20 22
21#define HAVE_CRYPTO_CPU_CAPS_INIT 23#define HAVE_CRYPTO_CPU_CAPS_INIT
22#define HAVE_CRYPTO_CPU_CAPS_IA32 24#define HAVE_CRYPTO_CPU_CAPS_IA32
23 25
26#ifndef __ASSEMBLER__
27extern uint64_t crypto_cpu_caps_amd64;
28#endif
29
30#define CRYPTO_CPU_CAPS_AMD64_SHA (1ULL << 0)
31
24#ifndef OPENSSL_NO_ASM 32#ifndef OPENSSL_NO_ASM
25 33
26#define HAVE_AES_CBC_ENCRYPT_INTERNAL 34#define HAVE_AES_CBC_ENCRYPT_INTERNAL
diff --git a/src/lib/libcrypto/arch/amd64/crypto_cpu_caps.c b/src/lib/libcrypto/arch/amd64/crypto_cpu_caps.c
index 6bb77411af..63b7b64cda 100644
--- a/src/lib/libcrypto/arch/amd64/crypto_cpu_caps.c
+++ b/src/lib/libcrypto/arch/amd64/crypto_cpu_caps.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_cpu_caps.c,v 1.3 2024/11/12 13:14:57 jsing Exp $ */ 1/* $OpenBSD: crypto_cpu_caps.c,v 1.4 2024/11/16 13:05:35 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -19,11 +19,15 @@
19 19
20#include <openssl/crypto.h> 20#include <openssl/crypto.h>
21 21
22#include "crypto_arch.h"
22#include "x86_arch.h" 23#include "x86_arch.h"
23 24
24/* Legacy architecture specific capabilities, used by perlasm. */ 25/* Legacy architecture specific capabilities, used by perlasm. */
25uint64_t OPENSSL_ia32cap_P; 26uint64_t OPENSSL_ia32cap_P;
26 27
28/* Machine dependent CPU capabilities. */
29uint64_t crypto_cpu_caps_amd64;
30
27/* Machine independent CPU capabilities. */ 31/* Machine independent CPU capabilities. */
28extern uint64_t crypto_cpu_caps; 32extern uint64_t crypto_cpu_caps;
29 33
@@ -67,19 +71,21 @@ xgetbv(uint32_t ecx, uint32_t *out_eax, uint32_t *out_edx)
67void 71void
68crypto_cpu_caps_init(void) 72crypto_cpu_caps_init(void)
69{ 73{
70 uint32_t eax, ebx, ecx, edx; 74 uint32_t eax, ebx, ecx, edx, max_cpuid;
71 uint64_t caps = 0; 75 uint64_t caps = 0;
72 76
73 cpuid(0, &eax, &ebx, &ecx, &edx); 77 cpuid(0, &eax, &ebx, &ecx, &edx);
74 78
79 max_cpuid = eax;
80
75 /* "GenuineIntel" in little endian. */ 81 /* "GenuineIntel" in little endian. */
76 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 82 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
77 caps |= CPUCAP_MASK_INTEL; 83 caps |= CPUCAP_MASK_INTEL;
78 84
79 if (eax < 1) 85 if (max_cpuid < 1)
80 return; 86 return;
81 87
82 cpuid(1, &eax, &ebx, &ecx, &edx); 88 cpuid(1, &eax, NULL, &ecx, &edx);
83 89
84 if ((edx & IA32CAP_MASK0_FXSR) != 0) 90 if ((edx & IA32CAP_MASK0_FXSR) != 0)
85 caps |= CPUCAP_MASK_FXSR; 91 caps |= CPUCAP_MASK_FXSR;
@@ -106,6 +112,14 @@ crypto_cpu_caps_init(void)
106 caps |= CPUCAP_MASK_AVX; 112 caps |= CPUCAP_MASK_AVX;
107 } 113 }
108 114
115 if (max_cpuid >= 7) {
116 cpuid(7, NULL, &ebx, NULL, NULL);
117
118 /* Intel SHA extensions feature bit - ebx[29]. */
119 if (((ebx >> 29) & 1) != 0)
120 crypto_cpu_caps_amd64 |= CRYPTO_CPU_CAPS_AMD64_SHA;
121 }
122
109 /* Set machine independent CPU capabilities. */ 123 /* Set machine independent CPU capabilities. */
110 if ((caps & CPUCAP_MASK_AESNI) != 0) 124 if ((caps & CPUCAP_MASK_AESNI) != 0)
111 crypto_cpu_caps |= CRYPTO_CPU_CAPS_ACCELERATED_AES; 125 crypto_cpu_caps |= CRYPTO_CPU_CAPS_ACCELERATED_AES;