rtl838x: various fixes
[openwrt/openwrt.git] / target / linux / ipq40xx / patches-4.19 / 048-crypto-qce-use-AES-fallback-for-small-requests.patch
1 From 2d3b6fae7d1a2ad821769440daa91d7eec5c8250 Mon Sep 17 00:00:00 2001
2 From: Eneas U de Queiroz <cotequeiroz@gmail.com>
3 Date: Fri, 20 Dec 2019 09:41:44 -0300
4 Subject: [PATCH] crypto: qce - use AES fallback for small requests
5
6 Process small blocks using the fallback cipher, as a workaround for an
7 observed failure (DMA-related, apparently) when computing the GCM ghash
8 key. This brings a speed gain as well, since it avoids the latency of
9 using the hardware engine to process small blocks.
10
11 Using software for all 16-byte requests would be enough to make GCM
12 work, but to increase performance, a larger threshold would be better.
13 Measuring the performance of supported ciphers with openssl speed,
14 software matches hardware at around 768-1024 bytes.
15
16 Considering the 256-bit ciphers, software is 2-3 times faster than qce
17 at 256-bytes, 30% faster at 512, and about even at 768-bytes. With
18 128-bit keys, the break-even point would be around 1024-bytes.
19
20 This adds the 'aes_sw_max_len' parameter, to set the largest request
21 length processed by the software fallback. Its default is being set to
22 512 bytes, a little lower than the break-even point, to balance the cost
23 in CPU usage.
24
25 Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
26
27 --- a/drivers/crypto/Kconfig
28 +++ b/drivers/crypto/Kconfig
29 @@ -585,6 +585,29 @@ config CRYPTO_DEV_QCE
30 hardware. To compile this driver as a module, choose M here. The
31 module will be called qcrypto.
32
33 +config CRYPTO_DEV_QCE_SW_MAX_LEN
34 + int "Default maximum request size to use software for AES"
35 + depends on CRYPTO_DEV_QCE && CRYPTO_DEV_QCE_SKCIPHER
36 + default 512
37 + help
38 + This sets the default maximum request size to perform AES requests
39 + using software instead of the crypto engine. It can be changed by
40 + setting the aes_sw_max_len parameter.
41 +
42 + Small blocks are processed faster in software than hardware.
43 + Considering the 256-bit ciphers, software is 2-3 times faster than
44 + qce at 256-bytes, 30% faster at 512, and about even at 768-bytes.
45 + With 128-bit keys, the break-even point would be around 1024-bytes.
46 +
47 + The default is set a little lower, to 512 bytes, to balance the
48 + cost in CPU usage. The minimum recommended setting is 16-bytes
49 + (1 AES block), since AES-GCM will fail if you set it lower.
50 + Setting this to zero will send all requests to the hardware.
51 +
52 + Note that 192-bit keys are not supported by the hardware and are
53 + always processed by the software fallback, and all DES requests
54 + are done by the hardware.
55 +
56 config CRYPTO_DEV_QCOM_RNG
57 tristate "Qualcomm Random Number Generator Driver"
58 depends on ARCH_QCOM || COMPILE_TEST
59 --- a/drivers/crypto/qce/skcipher.c
60 +++ b/drivers/crypto/qce/skcipher.c
61 @@ -13,6 +13,7 @@
62
63 #include <linux/device.h>
64 #include <linux/interrupt.h>
65 +#include <linux/moduleparam.h>
66 #include <linux/types.h>
67 #include <crypto/aes.h>
68 #include <crypto/des.h>
69 @@ -20,6 +21,13 @@
70
71 #include "cipher.h"
72
73 +static unsigned int aes_sw_max_len = CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN;
74 +module_param(aes_sw_max_len, uint, 0644);
75 +MODULE_PARM_DESC(aes_sw_max_len,
76 + "Only use hardware for AES requests larger than this "
77 + "[0=always use hardware; anything <16 breaks AES-GCM; default="
78 + __stringify(CONFIG_CRYPTO_DEV_QCE_SOFT_THRESHOLD)"]");
79 +
80 static LIST_HEAD(skcipher_algs);
81
82 static void qce_skcipher_done(void *data)
83 @@ -170,15 +178,7 @@ static int qce_skcipher_setkey(struct cr
84 if (!key || !keylen)
85 return -EINVAL;
86
87 - if (IS_AES(flags)) {
88 - switch (IS_XTS(flags) ? keylen >> 1 : keylen) {
89 - case AES_KEYSIZE_128:
90 - case AES_KEYSIZE_256:
91 - break;
92 - default:
93 - goto fallback;
94 - }
95 - } else if (IS_DES(flags)) {
96 + if (IS_DES(flags)) {
97 u32 tmp[DES_EXPKEY_WORDS];
98
99 ret = des_ekey(tmp, key);
100 @@ -189,8 +189,8 @@ static int qce_skcipher_setkey(struct cr
101
102 ctx->enc_keylen = keylen;
103 memcpy(ctx->enc_key, key, keylen);
104 - return 0;
105 -fallback:
106 + if (!IS_AES(flags))
107 + return 0;
108 ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
109 if (!ret)
110 ctx->enc_keylen = keylen;
111 @@ -213,8 +213,9 @@ static int qce_skcipher_crypt(struct skc
112 rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
113 keylen = IS_XTS(rctx->flags) ? ctx->enc_keylen >> 1 : ctx->enc_keylen;
114
115 - if (IS_AES(rctx->flags) && keylen != AES_KEYSIZE_128 &&
116 - keylen != AES_KEYSIZE_256) {
117 + if (IS_AES(rctx->flags) &&
118 + ((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
119 + req->cryptlen <= aes_sw_max_len)) {
120 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
121
122 skcipher_request_set_tfm(subreq, ctx->fallback);