summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2025-06-28 12:39:10 +0000
committerjsing <>2025-06-28 12:39:10 +0000
commit26d929bd98e9326565e1dd597c5c0dcdabf7a0e0 (patch)
treea759230fee0803d4b63c471498b9c879151a7c88 /src
parent5e7b2abaf553d1159c6b708b34998bddf2938170 (diff)
downloadopenbsd-26d929bd98e9326565e1dd597c5c0dcdabf7a0e0.tar.gz
openbsd-26d929bd98e9326565e1dd597c5c0dcdabf7a0e0.tar.bz2
openbsd-26d929bd98e9326565e1dd597c5c0dcdabf7a0e0.zip
Rework gcm128 implementation selection for amd64/i386.
Provide gcm128_amd64.c and gcm128_i386.c, which contain the appropriate gcm128 initialisation and CPU feature tests for the respective platform. This allows for all of the #define spagetti to be removed from gcm128.c and removes one of the two remaining consumers of crypto_cpu_caps_ia32(). ok tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/arch/amd64/Makefile.inc6
-rw-r--r--src/lib/libcrypto/arch/amd64/crypto_arch.h4
-rw-r--r--src/lib/libcrypto/arch/i386/Makefile.inc7
-rw-r--r--src/lib/libcrypto/arch/i386/crypto_arch.h4
-rw-r--r--src/lib/libcrypto/modes/gcm128.c70
-rw-r--r--src/lib/libcrypto/modes/gcm128_amd64.c44
-rw-r--r--src/lib/libcrypto/modes/gcm128_i386.c56
7 files changed, 130 insertions, 61 deletions
diff --git a/src/lib/libcrypto/arch/amd64/Makefile.inc b/src/lib/libcrypto/arch/amd64/Makefile.inc
index 5ecf8f1390..649c507189 100644
--- a/src/lib/libcrypto/arch/amd64/Makefile.inc
+++ b/src/lib/libcrypto/arch/amd64/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.40 2025/06/15 15:11:50 jsing Exp $ 1# $OpenBSD: Makefile.inc,v 1.41 2025/06/28 12:39:10 jsing Exp $
2 2
3# amd64-specific libcrypto build rules 3# amd64-specific libcrypto build rules
4 4
@@ -38,11 +38,15 @@ SRCS += word_clz.S
38# md5 38# md5
39CFLAGS+= -DMD5_ASM 39CFLAGS+= -DMD5_ASM
40SRCS+= md5_amd64_generic.S 40SRCS+= md5_amd64_generic.S
41
41# modes 42# modes
42CFLAGS+= -DGHASH_ASM 43CFLAGS+= -DGHASH_ASM
43SSLASM+= modes ghash-x86_64 44SSLASM+= modes ghash-x86_64
45SRCS += gcm128_amd64.c
46
44# rc4 47# rc4
45SSLASM+= rc4 rc4-x86_64 48SSLASM+= rc4 rc4-x86_64
49
46# ripemd 50# ripemd
47# sha 51# sha
48SRCS+= sha1_amd64.c 52SRCS+= sha1_amd64.c
diff --git a/src/lib/libcrypto/arch/amd64/crypto_arch.h b/src/lib/libcrypto/arch/amd64/crypto_arch.h
index 213a5d903e..9f292cc530 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.8 2025/06/28 12:20:39 jsing Exp $ */ 1/* $OpenBSD: crypto_arch.h,v 1.9 2025/06/28 12:39:10 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -40,6 +40,8 @@ extern uint64_t crypto_cpu_caps_amd64;
40#define HAVE_AES_CBC_ENCRYPT_INTERNAL 40#define HAVE_AES_CBC_ENCRYPT_INTERNAL
41#define HAVE_AES_CTR32_ENCRYPT_INTERNAL 41#define HAVE_AES_CTR32_ENCRYPT_INTERNAL
42 42
43#define HAVE_GCM128_INIT
44
43#define HAVE_RC4_INTERNAL 45#define HAVE_RC4_INTERNAL
44#define HAVE_RC4_SET_KEY_INTERNAL 46#define HAVE_RC4_SET_KEY_INTERNAL
45 47
diff --git a/src/lib/libcrypto/arch/i386/Makefile.inc b/src/lib/libcrypto/arch/i386/Makefile.inc
index 8747d389ac..bfc701687e 100644
--- a/src/lib/libcrypto/arch/i386/Makefile.inc
+++ b/src/lib/libcrypto/arch/i386/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.30 2025/06/15 15:11:50 jsing Exp $ 1# $OpenBSD: Makefile.inc,v 1.31 2025/06/28 12:39:10 jsing Exp $
2 2
3# i386-specific libcrypto build rules 3# i386-specific libcrypto build rules
4 4
@@ -18,14 +18,19 @@ SSLASM+= bn bn-586
18SSLASM+= bn co-586 18SSLASM+= bn co-586
19CFLAGS+= -DOPENSSL_BN_ASM_MONT 19CFLAGS+= -DOPENSSL_BN_ASM_MONT
20SSLASM+= bn x86-mont 20SSLASM+= bn x86-mont
21
21# md5 22# md5
22CFLAGS+= -DMD5_ASM 23CFLAGS+= -DMD5_ASM
23SSLASM+= md5 md5-586 24SSLASM+= md5 md5-586
25
24# modes 26# modes
25CFLAGS+= -DGHASH_ASM 27CFLAGS+= -DGHASH_ASM
26SSLASM+= modes ghash-x86 28SSLASM+= modes ghash-x86
29SRCS += gcm128_i386.c
30
27# rc4 31# rc4
28SSLASM+= rc4 rc4-586 32SSLASM+= rc4 rc4-586
33
29# sha 34# sha
30SSLASM+= sha sha1-586 35SSLASM+= sha sha1-586
31SSLASM+= sha sha256-586 36SSLASM+= sha sha256-586
diff --git a/src/lib/libcrypto/arch/i386/crypto_arch.h b/src/lib/libcrypto/arch/i386/crypto_arch.h
index 9a377e56a9..95d4cc468b 100644
--- a/src/lib/libcrypto/arch/i386/crypto_arch.h
+++ b/src/lib/libcrypto/arch/i386/crypto_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_arch.h,v 1.7 2025/06/28 12:21:46 jsing Exp $ */ 1/* $OpenBSD: crypto_arch.h,v 1.8 2025/06/28 12:39:10 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -40,6 +40,8 @@ extern uint64_t crypto_cpu_caps_i386;
40#define HAVE_AES_CBC_ENCRYPT_INTERNAL 40#define HAVE_AES_CBC_ENCRYPT_INTERNAL
41#define HAVE_AES_CTR32_ENCRYPT_INTERNAL 41#define HAVE_AES_CTR32_ENCRYPT_INTERNAL
42 42
43#define HAVE_GCM128_INIT
44
43#define HAVE_RC4_INTERNAL 45#define HAVE_RC4_INTERNAL
44#define HAVE_RC4_SET_KEY_INTERNAL 46#define HAVE_RC4_SET_KEY_INTERNAL
45 47
diff --git a/src/lib/libcrypto/modes/gcm128.c b/src/lib/libcrypto/modes/gcm128.c
index b989915c4a..b6874296e0 100644
--- a/src/lib/libcrypto/modes/gcm128.c
+++ b/src/lib/libcrypto/modes/gcm128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gcm128.c,v 1.53 2025/06/28 12:32:27 jsing Exp $ */ 1/* $OpenBSD: gcm128.c,v 1.54 2025/06/28 12:39:10 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -55,7 +55,7 @@
55#include "crypto_internal.h" 55#include "crypto_internal.h"
56#include "modes_local.h" 56#include "modes_local.h"
57 57
58static void 58void
59gcm_init_4bit(u128 Htable[16], uint64_t H[2]) 59gcm_init_4bit(u128 Htable[16], uint64_t H[2])
60{ 60{
61 u128 V; 61 u128 V;
@@ -196,35 +196,17 @@ gcm_ghash(GCM128_CONTEXT *ctx, const uint8_t *in, size_t len)
196 ctx->ghash(ctx->Xi.u, ctx->Htable, in, len); 196 ctx->ghash(ctx->Xi.u, ctx->Htable, in, len);
197} 197}
198 198
199#if defined(GHASH_ASM) && \ 199#ifdef HAVE_GCM128_INIT
200 (defined(__i386) || defined(__i386__) || \ 200void gcm128_init(GCM128_CONTEXT *ctx);
201 defined(__x86_64) || defined(__x86_64__) || \
202 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
203#include "x86_arch.h"
204#endif
205
206#if defined(GHASH_ASM)
207# if (defined(__i386) || defined(__i386__) || \
208 defined(__x86_64) || defined(__x86_64__) || \
209 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
210# define GHASH_ASM_X86_OR_64
211 201
212void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]); 202#else
213void gcm_gmult_clmul(uint64_t Xi[2], const u128 Htable[16]); 203static void
214void gcm_ghash_clmul(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp, 204gcm128_init(GCM128_CONTEXT *ctx)
215 size_t len); 205{
216 206 gcm_init_4bit(ctx->Htable, ctx->H.u);
217# if defined(__i386) || defined(__i386__) || defined(_M_IX86) 207 ctx->gmult = gcm_gmult_4bit;
218# define GHASH_ASM_X86 208 ctx->ghash = gcm_ghash_4bit;
219void gcm_gmult_4bit_mmx(uint64_t Xi[2], const u128 Htable[16]); 209}
220void gcm_ghash_4bit_mmx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
221 size_t len);
222
223void gcm_gmult_4bit_x86(uint64_t Xi[2], const u128 Htable[16]);
224void gcm_ghash_4bit_x86(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
225 size_t len);
226# endif
227# endif
228#endif 210#endif
229 211
230void 212void
@@ -240,33 +222,7 @@ CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
240 ctx->H.u[0] = be64toh(ctx->H.u[0]); 222 ctx->H.u[0] = be64toh(ctx->H.u[0]);
241 ctx->H.u[1] = be64toh(ctx->H.u[1]); 223 ctx->H.u[1] = be64toh(ctx->H.u[1]);
242 224
243# if defined(GHASH_ASM_X86_OR_64) 225 gcm128_init(ctx);
244 /* check FXSR and PCLMULQDQ bits */
245 if ((crypto_cpu_caps_ia32() & (CPUCAP_MASK_FXSR | CPUCAP_MASK_PCLMUL)) ==
246 (CPUCAP_MASK_FXSR | CPUCAP_MASK_PCLMUL)) {
247 gcm_init_clmul(ctx->Htable, ctx->H.u);
248 ctx->gmult = gcm_gmult_clmul;
249 ctx->ghash = gcm_ghash_clmul;
250 return;
251 }
252 gcm_init_4bit(ctx->Htable, ctx->H.u);
253# if defined(GHASH_ASM_X86) /* x86 only */
254 if (crypto_cpu_caps_ia32() & CPUCAP_MASK_MMX) { /* check MMX bit */
255 ctx->gmult = gcm_gmult_4bit_mmx;
256 ctx->ghash = gcm_ghash_4bit_mmx;
257 } else {
258 ctx->gmult = gcm_gmult_4bit_x86;
259 ctx->ghash = gcm_ghash_4bit_x86;
260 }
261# else
262 ctx->gmult = gcm_gmult_4bit;
263 ctx->ghash = gcm_ghash_4bit;
264# endif
265# else
266 gcm_init_4bit(ctx->Htable, ctx->H.u);
267 ctx->gmult = gcm_gmult_4bit;
268 ctx->ghash = gcm_ghash_4bit;
269# endif
270} 226}
271LCRYPTO_ALIAS(CRYPTO_gcm128_init); 227LCRYPTO_ALIAS(CRYPTO_gcm128_init);
272 228
diff --git a/src/lib/libcrypto/modes/gcm128_amd64.c b/src/lib/libcrypto/modes/gcm128_amd64.c
new file mode 100644
index 0000000000..eaa66fb32f
--- /dev/null
+++ b/src/lib/libcrypto/modes/gcm128_amd64.c
@@ -0,0 +1,44 @@
1/* $OpenBSD: gcm128_amd64.c,v 1.1 2025/06/28 12:39:10 jsing Exp $ */
2/*
3 * Copyright (c) 2025 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "crypto_arch.h"
19#include "modes_local.h"
20
21void gcm_init_4bit(u128 Htable[16], uint64_t H[2]);
22void gcm_gmult_4bit(uint64_t Xi[2], const u128 Htable[16]);
23void gcm_ghash_4bit(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
24 size_t len);
25
26void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
27void gcm_gmult_clmul(uint64_t Xi[2], const u128 Htable[16]);
28void gcm_ghash_clmul(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
29 size_t len);
30
31void
32gcm128_init(GCM128_CONTEXT *ctx)
33{
34 if ((crypto_cpu_caps_amd64 & CRYPTO_CPU_CAPS_AMD64_CLMUL) != 0) {
35 gcm_init_clmul(ctx->Htable, ctx->H.u);
36 ctx->gmult = gcm_gmult_clmul;
37 ctx->ghash = gcm_ghash_clmul;
38 return;
39 }
40
41 gcm_init_4bit(ctx->Htable, ctx->H.u);
42 ctx->gmult = gcm_gmult_4bit;
43 ctx->ghash = gcm_ghash_4bit;
44}
diff --git a/src/lib/libcrypto/modes/gcm128_i386.c b/src/lib/libcrypto/modes/gcm128_i386.c
new file mode 100644
index 0000000000..ac517fdb04
--- /dev/null
+++ b/src/lib/libcrypto/modes/gcm128_i386.c
@@ -0,0 +1,56 @@
1/* $OpenBSD: gcm128_i386.c,v 1.1 2025/06/28 12:39:10 jsing Exp $ */
2/*
3 * Copyright (c) 2025 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "crypto_arch.h"
19#include "modes_local.h"
20
21void gcm_init_4bit(u128 Htable[16], uint64_t H[2]);
22
23void gcm_gmult_4bit_mmx(uint64_t Xi[2], const u128 Htable[16]);
24void gcm_ghash_4bit_mmx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
25 size_t len);
26
27void gcm_gmult_4bit_x86(uint64_t Xi[2], const u128 Htable[16]);
28void gcm_ghash_4bit_x86(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
29 size_t len);
30
31void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
32void gcm_gmult_clmul(uint64_t Xi[2], const u128 Htable[16]);
33void gcm_ghash_clmul(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
34 size_t len);
35
36void
37gcm128_init(GCM128_CONTEXT *ctx)
38{
39 if ((crypto_cpu_caps_i386 & CRYPTO_CPU_CAPS_I386_CLMUL) != 0) {
40 gcm_init_clmul(ctx->Htable, ctx->H.u);
41 ctx->gmult = gcm_gmult_clmul;
42 ctx->ghash = gcm_ghash_clmul;
43 return;
44 }
45
46 if ((crypto_cpu_caps_i386 & CRYPTO_CPU_CAPS_I386_MMX) != 0) {
47 gcm_init_4bit(ctx->Htable, ctx->H.u);
48 ctx->gmult = gcm_gmult_4bit_mmx;
49 ctx->ghash = gcm_ghash_4bit_mmx;
50 return;
51 }
52
53 gcm_init_4bit(ctx->Htable, ctx->H.u);
54 ctx->gmult = gcm_gmult_4bit_x86;
55 ctx->ghash = gcm_ghash_4bit_x86;
56}