Skip to content

Commit a00fa0c

Browse files
ebiggersherbertx
authored andcommitted
crypto: arm64/nhpoly1305 - add NEON-accelerated NHPoly1305
Add an ARM64 NEON implementation of NHPoly1305, an ε-almost-∆-universal hash function used in the Adiantum encryption mode. For now, only the NH portion is actually NEON-accelerated; the Poly1305 part is less performance-critical so is just implemented in C. Reviewed-by: Ard Biesheuvel <[email protected]> Tested-by: Ard Biesheuvel <[email protected]> # big-endian Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 88d905e commit a00fa0c

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

arch/arm64/crypto/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ config CRYPTO_CHACHA20_NEON
106106
select CRYPTO_BLKCIPHER
107107
select CRYPTO_CHACHA20
108108

109+
config CRYPTO_NHPOLY1305_NEON
110+
tristate "NHPoly1305 hash function using NEON instructions (for Adiantum)"
111+
depends on KERNEL_MODE_NEON
112+
select CRYPTO_NHPOLY1305
113+
109114
config CRYPTO_AES_ARM64_BS
110115
tristate "AES in ECB/CBC/CTR/XTS modes using bit-sliced NEON algorithm"
111116
depends on KERNEL_MODE_NEON

arch/arm64/crypto/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ sha512-arm64-y := sha512-glue.o sha512-core.o
5353
obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o
5454
chacha20-neon-y := chacha20-neon-core.o chacha20-neon-glue.o
5555

56+
obj-$(CONFIG_CRYPTO_NHPOLY1305_NEON) += nhpoly1305-neon.o
57+
nhpoly1305-neon-y := nh-neon-core.o nhpoly1305-neon-glue.o
58+
5659
obj-$(CONFIG_CRYPTO_AES_ARM64) += aes-arm64.o
5760
aes-arm64-y := aes-cipher-core.o aes-cipher-glue.o
5861

arch/arm64/crypto/nh-neon-core.S

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* NH - ε-almost-universal hash function, ARM64 NEON accelerated version
4+
*
5+
* Copyright 2018 Google LLC
6+
*
7+
* Author: Eric Biggers <[email protected]>
8+
*/
9+
10+
#include <linux/linkage.h>
11+
12+
KEY .req x0
13+
MESSAGE .req x1
14+
MESSAGE_LEN .req x2
15+
HASH .req x3
16+
17+
PASS0_SUMS .req v0
18+
PASS1_SUMS .req v1
19+
PASS2_SUMS .req v2
20+
PASS3_SUMS .req v3
21+
K0 .req v4
22+
K1 .req v5
23+
K2 .req v6
24+
K3 .req v7
25+
T0 .req v8
26+
T1 .req v9
27+
T2 .req v10
28+
T3 .req v11
29+
T4 .req v12
30+
T5 .req v13
31+
T6 .req v14
32+
T7 .req v15
33+
34+
.macro _nh_stride k0, k1, k2, k3
35+
36+
// Load next message stride
37+
ld1 {T3.16b}, [MESSAGE], #16
38+
39+
// Load next key stride
40+
ld1 {\k3\().4s}, [KEY], #16
41+
42+
// Add message words to key words
43+
add T0.4s, T3.4s, \k0\().4s
44+
add T1.4s, T3.4s, \k1\().4s
45+
add T2.4s, T3.4s, \k2\().4s
46+
add T3.4s, T3.4s, \k3\().4s
47+
48+
// Multiply 32x32 => 64 and accumulate
49+
mov T4.d[0], T0.d[1]
50+
mov T5.d[0], T1.d[1]
51+
mov T6.d[0], T2.d[1]
52+
mov T7.d[0], T3.d[1]
53+
umlal PASS0_SUMS.2d, T0.2s, T4.2s
54+
umlal PASS1_SUMS.2d, T1.2s, T5.2s
55+
umlal PASS2_SUMS.2d, T2.2s, T6.2s
56+
umlal PASS3_SUMS.2d, T3.2s, T7.2s
57+
.endm
58+
59+
/*
60+
* void nh_neon(const u32 *key, const u8 *message, size_t message_len,
61+
* u8 hash[NH_HASH_BYTES])
62+
*
63+
* It's guaranteed that message_len % 16 == 0.
64+
*/
65+
ENTRY(nh_neon)
66+
67+
ld1 {K0.4s,K1.4s}, [KEY], #32
68+
movi PASS0_SUMS.2d, #0
69+
movi PASS1_SUMS.2d, #0
70+
ld1 {K2.4s}, [KEY], #16
71+
movi PASS2_SUMS.2d, #0
72+
movi PASS3_SUMS.2d, #0
73+
74+
subs MESSAGE_LEN, MESSAGE_LEN, #64
75+
blt .Lloop4_done
76+
.Lloop4:
77+
_nh_stride K0, K1, K2, K3
78+
_nh_stride K1, K2, K3, K0
79+
_nh_stride K2, K3, K0, K1
80+
_nh_stride K3, K0, K1, K2
81+
subs MESSAGE_LEN, MESSAGE_LEN, #64
82+
bge .Lloop4
83+
84+
.Lloop4_done:
85+
ands MESSAGE_LEN, MESSAGE_LEN, #63
86+
beq .Ldone
87+
_nh_stride K0, K1, K2, K3
88+
89+
subs MESSAGE_LEN, MESSAGE_LEN, #16
90+
beq .Ldone
91+
_nh_stride K1, K2, K3, K0
92+
93+
subs MESSAGE_LEN, MESSAGE_LEN, #16
94+
beq .Ldone
95+
_nh_stride K2, K3, K0, K1
96+
97+
.Ldone:
98+
// Sum the accumulators for each pass, then store the sums to 'hash'
99+
addp T0.2d, PASS0_SUMS.2d, PASS1_SUMS.2d
100+
addp T1.2d, PASS2_SUMS.2d, PASS3_SUMS.2d
101+
st1 {T0.16b,T1.16b}, [HASH]
102+
ret
103+
ENDPROC(nh_neon)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
4+
* (ARM64 NEON accelerated version)
5+
*
6+
* Copyright 2018 Google LLC
7+
*/
8+
9+
#include <asm/neon.h>
10+
#include <asm/simd.h>
11+
#include <crypto/internal/hash.h>
12+
#include <crypto/nhpoly1305.h>
13+
#include <linux/module.h>
14+
15+
asmlinkage void nh_neon(const u32 *key, const u8 *message, size_t message_len,
16+
u8 hash[NH_HASH_BYTES]);
17+
18+
/* wrapper to avoid indirect call to assembly, which doesn't work with CFI */
19+
static void _nh_neon(const u32 *key, const u8 *message, size_t message_len,
20+
__le64 hash[NH_NUM_PASSES])
21+
{
22+
nh_neon(key, message, message_len, (u8 *)hash);
23+
}
24+
25+
static int nhpoly1305_neon_update(struct shash_desc *desc,
26+
const u8 *src, unsigned int srclen)
27+
{
28+
if (srclen < 64 || !may_use_simd())
29+
return crypto_nhpoly1305_update(desc, src, srclen);
30+
31+
do {
32+
unsigned int n = min_t(unsigned int, srclen, PAGE_SIZE);
33+
34+
kernel_neon_begin();
35+
crypto_nhpoly1305_update_helper(desc, src, n, _nh_neon);
36+
kernel_neon_end();
37+
src += n;
38+
srclen -= n;
39+
} while (srclen);
40+
return 0;
41+
}
42+
43+
static struct shash_alg nhpoly1305_alg = {
44+
.base.cra_name = "nhpoly1305",
45+
.base.cra_driver_name = "nhpoly1305-neon",
46+
.base.cra_priority = 200,
47+
.base.cra_ctxsize = sizeof(struct nhpoly1305_key),
48+
.base.cra_module = THIS_MODULE,
49+
.digestsize = POLY1305_DIGEST_SIZE,
50+
.init = crypto_nhpoly1305_init,
51+
.update = nhpoly1305_neon_update,
52+
.final = crypto_nhpoly1305_final,
53+
.setkey = crypto_nhpoly1305_setkey,
54+
.descsize = sizeof(struct nhpoly1305_state),
55+
};
56+
57+
static int __init nhpoly1305_mod_init(void)
58+
{
59+
if (!(elf_hwcap & HWCAP_ASIMD))
60+
return -ENODEV;
61+
62+
return crypto_register_shash(&nhpoly1305_alg);
63+
}
64+
65+
static void __exit nhpoly1305_mod_exit(void)
66+
{
67+
crypto_unregister_shash(&nhpoly1305_alg);
68+
}
69+
70+
module_init(nhpoly1305_mod_init);
71+
module_exit(nhpoly1305_mod_exit);
72+
73+
MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (NEON-accelerated)");
74+
MODULE_LICENSE("GPL v2");
75+
MODULE_AUTHOR("Eric Biggers <[email protected]>");
76+
MODULE_ALIAS_CRYPTO("nhpoly1305");
77+
MODULE_ALIAS_CRYPTO("nhpoly1305-neon");

0 commit comments

Comments
 (0)