Skip to content

Commit a439763

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: aegis128 - provide a SIMD implementation based on NEON intrinsics
Provide an accelerated implementation of aegis128 by wiring up the SIMD hooks in the generic driver to an implementation based on NEON intrinsics, which can be compiled to both ARM and arm64 code. This results in a performance of 2.2 cycles per byte on Cortex-A53, which is a performance increase of ~11x compared to the generic code. Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent cf3d41a commit a439763

4 files changed

Lines changed: 207 additions & 0 deletions

File tree

‎crypto/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ config CRYPTO_AEGIS128
306306
help
307307
Support for the AEGIS-128 dedicated AEAD algorithm.
308308

309+
config CRYPTO_AEGIS128_SIMD
310+
bool "Support SIMD acceleration for AEGIS-128"
311+
depends on CRYPTO_AEGIS128 && ((ARM || ARM64) && KERNEL_MODE_NEON)
312+
default y
313+
309314
config CRYPTO_AEGIS128_AESNI_SSE2
310315
tristate "AEGIS-128 AEAD algorithm (x86_64 AESNI+SSE2 implementation)"
311316
depends on X86 && 64BIT

‎crypto/Makefile‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ obj-$(CONFIG_CRYPTO_CCM) += ccm.o
9191
obj-$(CONFIG_CRYPTO_CHACHA20POLY1305) += chacha20poly1305.o
9292
obj-$(CONFIG_CRYPTO_AEGIS128) += aegis128.o
9393
aegis128-y := aegis128-core.o
94+
95+
ifeq ($(ARCH),arm)
96+
CFLAGS_aegis128-neon-inner.o += -ffreestanding -march=armv7-a -mfloat-abi=softfp
97+
CFLAGS_aegis128-neon-inner.o += -mfpu=crypto-neon-fp-armv8
98+
aegis128-$(CONFIG_CRYPTO_AEGIS128_SIMD) += aegis128-neon.o aegis128-neon-inner.o
99+
endif
100+
ifeq ($(ARCH),arm64)
101+
CFLAGS_aegis128-neon-inner.o += -ffreestanding -mcpu=generic+crypto
102+
CFLAGS_REMOVE_aegis128-neon-inner.o += -mgeneral-regs-only
103+
aegis128-$(CONFIG_CRYPTO_AEGIS128_SIMD) += aegis128-neon.o aegis128-neon-inner.o
104+
endif
105+
94106
obj-$(CONFIG_CRYPTO_PCRYPT) += pcrypt.o
95107
obj-$(CONFIG_CRYPTO_CRYPTD) += cryptd.o
96108
obj-$(CONFIG_CRYPTO_DES) += des_generic.o

‎crypto/aegis128-neon-inner.c‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
4+
*/
5+
6+
#ifdef CONFIG_ARM64
7+
#include <asm/neon-intrinsics.h>
8+
9+
#define AES_ROUND "aese %0.16b, %1.16b \n\t aesmc %0.16b, %0.16b"
10+
#else
11+
#include <arm_neon.h>
12+
13+
#define AES_ROUND "aese.8 %q0, %q1 \n\t aesmc.8 %q0, %q0"
14+
#endif
15+
16+
#define AEGIS_BLOCK_SIZE 16
17+
18+
#include <stddef.h>
19+
20+
void *memcpy(void *dest, const void *src, size_t n);
21+
void *memset(void *s, int c, size_t n);
22+
23+
struct aegis128_state {
24+
uint8x16_t v[5];
25+
};
26+
27+
static struct aegis128_state aegis128_load_state_neon(const void *state)
28+
{
29+
return (struct aegis128_state){ {
30+
vld1q_u8(state),
31+
vld1q_u8(state + 16),
32+
vld1q_u8(state + 32),
33+
vld1q_u8(state + 48),
34+
vld1q_u8(state + 64)
35+
} };
36+
}
37+
38+
static void aegis128_save_state_neon(struct aegis128_state st, void *state)
39+
{
40+
vst1q_u8(state, st.v[0]);
41+
vst1q_u8(state + 16, st.v[1]);
42+
vst1q_u8(state + 32, st.v[2]);
43+
vst1q_u8(state + 48, st.v[3]);
44+
vst1q_u8(state + 64, st.v[4]);
45+
}
46+
47+
static inline __attribute__((always_inline))
48+
uint8x16_t aegis_aes_round(uint8x16_t w)
49+
{
50+
uint8x16_t z = {};
51+
52+
/*
53+
* We use inline asm here instead of the vaeseq_u8/vaesmcq_u8 intrinsics
54+
* to force the compiler to issue the aese/aesmc instructions in pairs.
55+
* This is much faster on many cores, where the instruction pair can
56+
* execute in a single cycle.
57+
*/
58+
asm(AES_ROUND : "+w"(w) : "w"(z));
59+
return w;
60+
}
61+
62+
static inline __attribute__((always_inline))
63+
struct aegis128_state aegis128_update_neon(struct aegis128_state st,
64+
uint8x16_t m)
65+
{
66+
m ^= aegis_aes_round(st.v[4]);
67+
st.v[4] ^= aegis_aes_round(st.v[3]);
68+
st.v[3] ^= aegis_aes_round(st.v[2]);
69+
st.v[2] ^= aegis_aes_round(st.v[1]);
70+
st.v[1] ^= aegis_aes_round(st.v[0]);
71+
st.v[0] ^= m;
72+
73+
return st;
74+
}
75+
76+
void crypto_aegis128_update_neon(void *state, const void *msg)
77+
{
78+
struct aegis128_state st = aegis128_load_state_neon(state);
79+
80+
st = aegis128_update_neon(st, vld1q_u8(msg));
81+
82+
aegis128_save_state_neon(st, state);
83+
}
84+
85+
void crypto_aegis128_encrypt_chunk_neon(void *state, void *dst, const void *src,
86+
unsigned int size)
87+
{
88+
struct aegis128_state st = aegis128_load_state_neon(state);
89+
uint8x16_t msg;
90+
91+
while (size >= AEGIS_BLOCK_SIZE) {
92+
uint8x16_t s = st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
93+
94+
msg = vld1q_u8(src);
95+
st = aegis128_update_neon(st, msg);
96+
vst1q_u8(dst, msg ^ s);
97+
98+
size -= AEGIS_BLOCK_SIZE;
99+
src += AEGIS_BLOCK_SIZE;
100+
dst += AEGIS_BLOCK_SIZE;
101+
}
102+
103+
if (size > 0) {
104+
uint8x16_t s = st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
105+
uint8_t buf[AEGIS_BLOCK_SIZE] = {};
106+
107+
memcpy(buf, src, size);
108+
msg = vld1q_u8(buf);
109+
st = aegis128_update_neon(st, msg);
110+
vst1q_u8(buf, msg ^ s);
111+
memcpy(dst, buf, size);
112+
}
113+
114+
aegis128_save_state_neon(st, state);
115+
}
116+
117+
void crypto_aegis128_decrypt_chunk_neon(void *state, void *dst, const void *src,
118+
unsigned int size)
119+
{
120+
struct aegis128_state st = aegis128_load_state_neon(state);
121+
uint8x16_t msg;
122+
123+
while (size >= AEGIS_BLOCK_SIZE) {
124+
msg = vld1q_u8(src) ^ st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
125+
st = aegis128_update_neon(st, msg);
126+
vst1q_u8(dst, msg);
127+
128+
size -= AEGIS_BLOCK_SIZE;
129+
src += AEGIS_BLOCK_SIZE;
130+
dst += AEGIS_BLOCK_SIZE;
131+
}
132+
133+
if (size > 0) {
134+
uint8x16_t s = st.v[1] ^ (st.v[2] & st.v[3]) ^ st.v[4];
135+
uint8_t buf[AEGIS_BLOCK_SIZE];
136+
137+
vst1q_u8(buf, s);
138+
memcpy(buf, src, size);
139+
msg = vld1q_u8(buf) ^ s;
140+
vst1q_u8(buf, msg);
141+
memcpy(dst, buf, size);
142+
143+
st = aegis128_update_neon(st, msg);
144+
}
145+
146+
aegis128_save_state_neon(st, state);
147+
}

‎crypto/aegis128-neon.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2019 Linaro Ltd <ard.biesheuvel@linaro.org>
4+
*/
5+
6+
#include <asm/cpufeature.h>
7+
#include <asm/neon.h>
8+
9+
#include "aegis.h"
10+
11+
void crypto_aegis128_update_neon(void *state, const void *msg);
12+
void crypto_aegis128_encrypt_chunk_neon(void *state, void *dst, const void *src,
13+
unsigned int size);
14+
void crypto_aegis128_decrypt_chunk_neon(void *state, void *dst, const void *src,
15+
unsigned int size);
16+
17+
bool crypto_aegis128_have_simd(void)
18+
{
19+
return cpu_have_feature(cpu_feature(AES));
20+
}
21+
22+
void crypto_aegis128_update_simd(union aegis_block *state, const void *msg)
23+
{
24+
kernel_neon_begin();
25+
crypto_aegis128_update_neon(state, msg);
26+
kernel_neon_end();
27+
}
28+
29+
void crypto_aegis128_encrypt_chunk_simd(union aegis_block *state, u8 *dst,
30+
const u8 *src, unsigned int size)
31+
{
32+
kernel_neon_begin();
33+
crypto_aegis128_encrypt_chunk_neon(state, dst, src, size);
34+
kernel_neon_end();
35+
}
36+
37+
void crypto_aegis128_decrypt_chunk_simd(union aegis_block *state, u8 *dst,
38+
const u8 *src, unsigned int size)
39+
{
40+
kernel_neon_begin();
41+
crypto_aegis128_decrypt_chunk_neon(state, dst, src, size);
42+
kernel_neon_end();
43+
}

0 commit comments

Comments
 (0)