Skip to content

Commit 012c823

Browse files
ebiggersherbertx
authored andcommitted
crypto: x86/nhpoly1305 - add SSE2 accelerated NHPoly1305
Add a 64-bit SSE2 implementation of NHPoly1305, an ε-almost-∆-universal hash function used in the Adiantum encryption mode. For now, only the NH portion is actually SSE2-accelerated; the Poly1305 part is less performance-critical so is just implemented in C. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent b299362 commit 012c823

4 files changed

Lines changed: 211 additions & 0 deletions

File tree

‎arch/x86/crypto/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ obj-$(CONFIG_CRYPTO_MORUS1280_GLUE) += morus1280_glue.o
4747
obj-$(CONFIG_CRYPTO_MORUS640_SSE2) += morus640-sse2.o
4848
obj-$(CONFIG_CRYPTO_MORUS1280_SSE2) += morus1280-sse2.o
4949

50+
obj-$(CONFIG_CRYPTO_NHPOLY1305_SSE2) += nhpoly1305-sse2.o
51+
5052
# These modules require assembler to support AVX.
5153
ifeq ($(avx_supported),yes)
5254
obj-$(CONFIG_CRYPTO_CAMELLIA_AESNI_AVX_X86_64) += \
@@ -85,6 +87,8 @@ aegis256-aesni-y := aegis256-aesni-asm.o aegis256-aesni-glue.o
8587
morus640-sse2-y := morus640-sse2-asm.o morus640-sse2-glue.o
8688
morus1280-sse2-y := morus1280-sse2-asm.o morus1280-sse2-glue.o
8789

90+
nhpoly1305-sse2-y := nh-sse2-x86_64.o nhpoly1305-sse2-glue.o
91+
8892
ifeq ($(avx_supported),yes)
8993
camellia-aesni-avx-x86_64-y := camellia-aesni-avx-asm_64.o \
9094
camellia_aesni_avx_glue.o

‎arch/x86/crypto/nh-sse2-x86_64.S‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* NH - ε-almost-universal hash function, x86_64 SSE2 accelerated
4+
*
5+
* Copyright 2018 Google LLC
6+
*
7+
* Author: Eric Biggers <ebiggers@google.com>
8+
*/
9+
10+
#include <linux/linkage.h>
11+
12+
#define PASS0_SUMS %xmm0
13+
#define PASS1_SUMS %xmm1
14+
#define PASS2_SUMS %xmm2
15+
#define PASS3_SUMS %xmm3
16+
#define K0 %xmm4
17+
#define K1 %xmm5
18+
#define K2 %xmm6
19+
#define K3 %xmm7
20+
#define T0 %xmm8
21+
#define T1 %xmm9
22+
#define T2 %xmm10
23+
#define T3 %xmm11
24+
#define T4 %xmm12
25+
#define T5 %xmm13
26+
#define T6 %xmm14
27+
#define T7 %xmm15
28+
#define KEY %rdi
29+
#define MESSAGE %rsi
30+
#define MESSAGE_LEN %rdx
31+
#define HASH %rcx
32+
33+
.macro _nh_stride k0, k1, k2, k3, offset
34+
35+
// Load next message stride
36+
movdqu \offset(MESSAGE), T1
37+
38+
// Load next key stride
39+
movdqu \offset(KEY), \k3
40+
41+
// Add message words to key words
42+
movdqa T1, T2
43+
movdqa T1, T3
44+
paddd T1, \k0 // reuse k0 to avoid a move
45+
paddd \k1, T1
46+
paddd \k2, T2
47+
paddd \k3, T3
48+
49+
// Multiply 32x32 => 64 and accumulate
50+
pshufd $0x10, \k0, T4
51+
pshufd $0x32, \k0, \k0
52+
pshufd $0x10, T1, T5
53+
pshufd $0x32, T1, T1
54+
pshufd $0x10, T2, T6
55+
pshufd $0x32, T2, T2
56+
pshufd $0x10, T3, T7
57+
pshufd $0x32, T3, T3
58+
pmuludq T4, \k0
59+
pmuludq T5, T1
60+
pmuludq T6, T2
61+
pmuludq T7, T3
62+
paddq \k0, PASS0_SUMS
63+
paddq T1, PASS1_SUMS
64+
paddq T2, PASS2_SUMS
65+
paddq T3, PASS3_SUMS
66+
.endm
67+
68+
/*
69+
* void nh_sse2(const u32 *key, const u8 *message, size_t message_len,
70+
* u8 hash[NH_HASH_BYTES])
71+
*
72+
* It's guaranteed that message_len % 16 == 0.
73+
*/
74+
ENTRY(nh_sse2)
75+
76+
movdqu 0x00(KEY), K0
77+
movdqu 0x10(KEY), K1
78+
movdqu 0x20(KEY), K2
79+
add $0x30, KEY
80+
pxor PASS0_SUMS, PASS0_SUMS
81+
pxor PASS1_SUMS, PASS1_SUMS
82+
pxor PASS2_SUMS, PASS2_SUMS
83+
pxor PASS3_SUMS, PASS3_SUMS
84+
85+
sub $0x40, MESSAGE_LEN
86+
jl .Lloop4_done
87+
.Lloop4:
88+
_nh_stride K0, K1, K2, K3, 0x00
89+
_nh_stride K1, K2, K3, K0, 0x10
90+
_nh_stride K2, K3, K0, K1, 0x20
91+
_nh_stride K3, K0, K1, K2, 0x30
92+
add $0x40, KEY
93+
add $0x40, MESSAGE
94+
sub $0x40, MESSAGE_LEN
95+
jge .Lloop4
96+
97+
.Lloop4_done:
98+
and $0x3f, MESSAGE_LEN
99+
jz .Ldone
100+
_nh_stride K0, K1, K2, K3, 0x00
101+
102+
sub $0x10, MESSAGE_LEN
103+
jz .Ldone
104+
_nh_stride K1, K2, K3, K0, 0x10
105+
106+
sub $0x10, MESSAGE_LEN
107+
jz .Ldone
108+
_nh_stride K2, K3, K0, K1, 0x20
109+
110+
.Ldone:
111+
// Sum the accumulators for each pass, then store the sums to 'hash'
112+
movdqa PASS0_SUMS, T0
113+
movdqa PASS2_SUMS, T1
114+
punpcklqdq PASS1_SUMS, T0 // => (PASS0_SUM_A PASS1_SUM_A)
115+
punpcklqdq PASS3_SUMS, T1 // => (PASS2_SUM_A PASS3_SUM_A)
116+
punpckhqdq PASS1_SUMS, PASS0_SUMS // => (PASS0_SUM_B PASS1_SUM_B)
117+
punpckhqdq PASS3_SUMS, PASS2_SUMS // => (PASS2_SUM_B PASS3_SUM_B)
118+
paddq PASS0_SUMS, T0
119+
paddq PASS2_SUMS, T1
120+
movdqu T0, 0x00(HASH)
121+
movdqu T1, 0x10(HASH)
122+
ret
123+
ENDPROC(nh_sse2)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
4+
* (SSE2 accelerated version)
5+
*
6+
* Copyright 2018 Google LLC
7+
*/
8+
9+
#include <crypto/internal/hash.h>
10+
#include <crypto/nhpoly1305.h>
11+
#include <linux/module.h>
12+
#include <asm/fpu/api.h>
13+
14+
asmlinkage void nh_sse2(const u32 *key, const u8 *message, size_t message_len,
15+
u8 hash[NH_HASH_BYTES]);
16+
17+
/* wrapper to avoid indirect call to assembly, which doesn't work with CFI */
18+
static void _nh_sse2(const u32 *key, const u8 *message, size_t message_len,
19+
__le64 hash[NH_NUM_PASSES])
20+
{
21+
nh_sse2(key, message, message_len, (u8 *)hash);
22+
}
23+
24+
static int nhpoly1305_sse2_update(struct shash_desc *desc,
25+
const u8 *src, unsigned int srclen)
26+
{
27+
if (srclen < 64 || !irq_fpu_usable())
28+
return crypto_nhpoly1305_update(desc, src, srclen);
29+
30+
do {
31+
unsigned int n = min_t(unsigned int, srclen, PAGE_SIZE);
32+
33+
kernel_fpu_begin();
34+
crypto_nhpoly1305_update_helper(desc, src, n, _nh_sse2);
35+
kernel_fpu_end();
36+
src += n;
37+
srclen -= n;
38+
} while (srclen);
39+
return 0;
40+
}
41+
42+
static struct shash_alg nhpoly1305_alg = {
43+
.base.cra_name = "nhpoly1305",
44+
.base.cra_driver_name = "nhpoly1305-sse2",
45+
.base.cra_priority = 200,
46+
.base.cra_ctxsize = sizeof(struct nhpoly1305_key),
47+
.base.cra_module = THIS_MODULE,
48+
.digestsize = POLY1305_DIGEST_SIZE,
49+
.init = crypto_nhpoly1305_init,
50+
.update = nhpoly1305_sse2_update,
51+
.final = crypto_nhpoly1305_final,
52+
.setkey = crypto_nhpoly1305_setkey,
53+
.descsize = sizeof(struct nhpoly1305_state),
54+
};
55+
56+
static int __init nhpoly1305_mod_init(void)
57+
{
58+
if (!boot_cpu_has(X86_FEATURE_XMM2))
59+
return -ENODEV;
60+
61+
return crypto_register_shash(&nhpoly1305_alg);
62+
}
63+
64+
static void __exit nhpoly1305_mod_exit(void)
65+
{
66+
crypto_unregister_shash(&nhpoly1305_alg);
67+
}
68+
69+
module_init(nhpoly1305_mod_init);
70+
module_exit(nhpoly1305_mod_exit);
71+
72+
MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (SSE2-accelerated)");
73+
MODULE_LICENSE("GPL v2");
74+
MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
75+
MODULE_ALIAS_CRYPTO("nhpoly1305");
76+
MODULE_ALIAS_CRYPTO("nhpoly1305-sse2");

‎crypto/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,14 @@ config CRYPTO_NHPOLY1305
501501
select CRYPTO_HASH
502502
select CRYPTO_POLY1305
503503

504+
config CRYPTO_NHPOLY1305_SSE2
505+
tristate "NHPoly1305 hash function (x86_64 SSE2 implementation)"
506+
depends on X86 && 64BIT
507+
select CRYPTO_NHPOLY1305
508+
help
509+
SSE2 optimized implementation of the hash function used by the
510+
Adiantum encryption mode.
511+
504512
config CRYPTO_ADIANTUM
505513
tristate "Adiantum support"
506514
select CRYPTO_CHACHA20

0 commit comments

Comments
 (0)