7af13d4023809aa2cf4753da7b026aa14a5e0799
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0353-Assign-crypto-aliases-to-different-AES-implementatio.patch
1 From d4a2fa7b58d3891544c9b7e41e92f17774ad7d1f Mon Sep 17 00:00:00 2001
2 From: Ben Avison <bavison@riscosopen.org>
3 Date: Mon, 8 Mar 2021 15:32:25 +0000
4 Subject: [PATCH] Assign crypto aliases to different AES implementation
5 modules
6
7 The kernel modules aes-neon-blk and aes-neon-bs perform poorly, at least on
8 Cortex-A72 without crypto extensions. In fact, aes-arm64 outperforms them
9 on benchmarks, despite it being a simpler implementation (only accelerating
10 the single-block AES cipher).
11
12 For modes of operation where multiple cipher blocks can be processed in
13 parallel, aes-neon-bs outperforms aes-neon-blk by around 60-70% and aes-arm64
14 is another 10-20% faster still. But the difference is even more marked with
15 modes of operation with dependencies between neighbouring blocks, such as
16 CBC encryption, which defeat parallelism: in these cases, aes-arm64 is
17 typically around 250% faster than either aes-neon-blk or aes-neon-bs.
18
19 The key trade-off with aes-arm64 is that the look-up tables are situated in
20 RAM. This leaves them potentially open to cache timing attacks. The two other
21 modules, by contrast, load the look-up tables into NEON registers and so are
22 able to perform in constant time.
23
24 This patch aims to load aes-arm64 more often.
25
26 If none of the currently-loaded crypto modules implement a given algorithm,
27 a new one is typically selected for loading using a platform-neutral alias
28 describing the required algorithm. To enable users to still
29 load aes-neon-blk or aes-neon-bs if they really want them, while still
30 ensuring that aes-arm64 is usually selected, remove the aliases from
31 aes-neonbs-glue.c and aes-glue.c and apply them to aes-cipher-glue.c, but
32 still build the two NEON modules.
33
34 Since aes-glue.c can also be used to build aes-ce-blk, leave them enabled
35 if USE_V8_CRYPTO_EXTENSIONS is defined, to ensure they are selected if we
36 in future use a CPU which has the crypto extensions enabled.
37
38 Note that the algorithm priority specifiers are unchanged, so if
39 aes-neon-bs is loaded at the same time as aes-arm64, the former will be
40 used in preference. However, aes-neon-blk and aes-arm64 have tied priority,
41 so whichever module was loaded first will be used (assuming aes-neon-bs is
42 not loaded).
43
44 Signed-off-by: Ben Avison <bavison@riscosopen.org>
45 ---
46 arch/arm64/crypto/aes-cipher-glue.c | 10 ++++++++++
47 arch/arm64/crypto/aes-glue.c | 4 ++--
48 arch/arm64/crypto/aes-neonbs-glue.c | 5 -----
49 3 files changed, 12 insertions(+), 7 deletions(-)
50
51 --- a/arch/arm64/crypto/aes-cipher-glue.c
52 +++ b/arch/arm64/crypto/aes-cipher-glue.c
53 @@ -9,6 +9,16 @@
54 #include <linux/crypto.h>
55 #include <linux/module.h>
56
57 +MODULE_ALIAS_CRYPTO("ecb(aes)");
58 +MODULE_ALIAS_CRYPTO("cbc(aes)");
59 +MODULE_ALIAS_CRYPTO("ctr(aes)");
60 +MODULE_ALIAS_CRYPTO("xts(aes)");
61 +MODULE_ALIAS_CRYPTO("cts(cbc(aes))");
62 +MODULE_ALIAS_CRYPTO("essiv(cbc(aes),sha256)");
63 +MODULE_ALIAS_CRYPTO("cmac(aes)");
64 +MODULE_ALIAS_CRYPTO("xcbc(aes)");
65 +MODULE_ALIAS_CRYPTO("cbcmac(aes)");
66 +
67 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
68 asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
69
70 --- a/arch/arm64/crypto/aes-glue.c
71 +++ b/arch/arm64/crypto/aes-glue.c
72 @@ -57,17 +57,17 @@ MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS
73 #define aes_mac_update neon_aes_mac_update
74 MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 NEON");
75 #endif
76 -#if defined(USE_V8_CRYPTO_EXTENSIONS) || !IS_ENABLED(CONFIG_CRYPTO_AES_ARM64_BS)
77 +#if defined(USE_V8_CRYPTO_EXTENSIONS)
78 MODULE_ALIAS_CRYPTO("ecb(aes)");
79 MODULE_ALIAS_CRYPTO("cbc(aes)");
80 MODULE_ALIAS_CRYPTO("ctr(aes)");
81 MODULE_ALIAS_CRYPTO("xts(aes)");
82 -#endif
83 MODULE_ALIAS_CRYPTO("cts(cbc(aes))");
84 MODULE_ALIAS_CRYPTO("essiv(cbc(aes),sha256)");
85 MODULE_ALIAS_CRYPTO("cmac(aes)");
86 MODULE_ALIAS_CRYPTO("xcbc(aes)");
87 MODULE_ALIAS_CRYPTO("cbcmac(aes)");
88 +#endif
89
90 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
91 MODULE_LICENSE("GPL v2");
92 --- a/arch/arm64/crypto/aes-neonbs-glue.c
93 +++ b/arch/arm64/crypto/aes-neonbs-glue.c
94 @@ -18,11 +18,6 @@
95 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
96 MODULE_LICENSE("GPL v2");
97
98 -MODULE_ALIAS_CRYPTO("ecb(aes)");
99 -MODULE_ALIAS_CRYPTO("cbc(aes)");
100 -MODULE_ALIAS_CRYPTO("ctr(aes)");
101 -MODULE_ALIAS_CRYPTO("xts(aes)");
102 -
103 asmlinkage void aesbs_convert_key(u8 out[], u32 const rk[], int rounds);
104
105 asmlinkage void aesbs_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[],