f1035be52fc3c4e09d42af59fde920d9ba65bdc6
[oweals/openwrt.git] / target / linux / apm821xx / patches-4.14 / 022-0002-crypto-crypto4xx-performance-optimizations.patch
1 From a8d79d7bfb14f471914017103ee2329a74e5e89d Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Thu, 19 Apr 2018 18:41:51 +0200
4 Subject: crypto: crypto4xx - performance optimizations
5
6 This patch provides a cheap 2MiB/s+ (~ 6%) performance
7 improvement over the current code. This is because the
8 compiler can now optimize several endian swap memcpy.
9
10 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
11 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
12 ---
13  drivers/crypto/amcc/crypto4xx_alg.c  | 32 +++++++++++++++++++-------------
14  drivers/crypto/amcc/crypto4xx_core.c | 22 +++++++++++-----------
15  drivers/crypto/amcc/crypto4xx_core.h |  6 ++++--
16  3 files changed, 34 insertions(+), 26 deletions(-)
17
18 --- a/drivers/crypto/amcc/crypto4xx_alg.c
19 +++ b/drivers/crypto/amcc/crypto4xx_alg.c
20 @@ -74,32 +74,38 @@ static void set_dynamic_sa_command_1(str
21         sa->sa_command_1.bf.copy_hdr = cp_hdr;
22  }
23  
24 -int crypto4xx_encrypt(struct ablkcipher_request *req)
25 +static inline int crypto4xx_crypt(struct ablkcipher_request *req,
26 +                                 const unsigned int ivlen, bool decrypt)
27  {
28         struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
29 -       unsigned int ivlen = crypto_ablkcipher_ivsize(
30 -               crypto_ablkcipher_reqtfm(req));
31         __le32 iv[ivlen];
32  
33         if (ivlen)
34                 crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
35  
36         return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
37 -               req->nbytes, iv, ivlen, ctx->sa_out, ctx->sa_len, 0);
38 +               req->nbytes, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
39 +               ctx->sa_len, 0);
40  }
41  
42 -int crypto4xx_decrypt(struct ablkcipher_request *req)
43 +int crypto4xx_encrypt_noiv(struct ablkcipher_request *req)
44  {
45 -       struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
46 -       unsigned int ivlen = crypto_ablkcipher_ivsize(
47 -               crypto_ablkcipher_reqtfm(req));
48 -       __le32 iv[ivlen];
49 +       return crypto4xx_crypt(req, 0, false);
50 +}
51  
52 -       if (ivlen)
53 -               crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
54 +int crypto4xx_encrypt_iv(struct ablkcipher_request *req)
55 +{
56 +       return crypto4xx_crypt(req, AES_IV_SIZE, false);
57 +}
58  
59 -       return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
60 -               req->nbytes, iv, ivlen, ctx->sa_in, ctx->sa_len, 0);
61 +int crypto4xx_decrypt_noiv(struct ablkcipher_request *req)
62 +{
63 +       return crypto4xx_crypt(req, 0, true);
64 +}
65 +
66 +int crypto4xx_decrypt_iv(struct ablkcipher_request *req)
67 +{
68 +       return crypto4xx_crypt(req, AES_IV_SIZE, true);
69  }
70  
71  /**
72 --- a/drivers/crypto/amcc/crypto4xx_core.c
73 +++ b/drivers/crypto/amcc/crypto4xx_core.c
74 @@ -580,7 +580,7 @@ static void crypto4xx_aead_done(struct c
75         struct scatterlist *dst = pd_uinfo->dest_va;
76         size_t cp_len = crypto_aead_authsize(
77                 crypto_aead_reqtfm(aead_req));
78 -       u32 icv[cp_len];
79 +       u32 icv[AES_BLOCK_SIZE];
80         int err = 0;
81  
82         if (pd_uinfo->using_sd) {
83 @@ -595,7 +595,7 @@ static void crypto4xx_aead_done(struct c
84         if (pd_uinfo->sa_va->sa_command_0.bf.dir == DIR_OUTBOUND) {
85                 /* append icv at the end */
86                 crypto4xx_memcpy_from_le32(icv, pd_uinfo->sr_va->save_digest,
87 -                                          cp_len);
88 +                                          sizeof(icv));
89  
90                 scatterwalk_map_and_copy(icv, dst, aead_req->cryptlen,
91                                          cp_len, 1);
92 @@ -605,7 +605,7 @@ static void crypto4xx_aead_done(struct c
93                         aead_req->assoclen + aead_req->cryptlen -
94                         cp_len, cp_len, 0);
95  
96 -               crypto4xx_memcpy_from_le32(icv, icv, cp_len);
97 +               crypto4xx_memcpy_from_le32(icv, icv, sizeof(icv));
98  
99                 if (crypto_memneq(icv, pd_uinfo->sr_va->save_digest, cp_len))
100                         err = -EBADMSG;
101 @@ -1122,8 +1122,8 @@ static struct crypto4xx_alg_common crypt
102                                 .max_keysize    = AES_MAX_KEY_SIZE,
103                                 .ivsize         = AES_IV_SIZE,
104                                 .setkey         = crypto4xx_setkey_aes_cbc,
105 -                               .encrypt        = crypto4xx_encrypt,
106 -                               .decrypt        = crypto4xx_decrypt,
107 +                               .encrypt        = crypto4xx_encrypt_iv,
108 +                               .decrypt        = crypto4xx_decrypt_iv,
109                         }
110                 }
111         }},
112 @@ -1146,8 +1146,8 @@ static struct crypto4xx_alg_common crypt
113                                 .max_keysize    = AES_MAX_KEY_SIZE,
114                                 .ivsize         = AES_IV_SIZE,
115                                 .setkey         = crypto4xx_setkey_aes_cfb,
116 -                               .encrypt        = crypto4xx_encrypt,
117 -                               .decrypt        = crypto4xx_decrypt,
118 +                               .encrypt        = crypto4xx_encrypt_iv,
119 +                               .decrypt        = crypto4xx_decrypt_iv,
120                         }
121                 }
122         } },
123 @@ -1195,8 +1195,8 @@ static struct crypto4xx_alg_common crypt
124                                 .min_keysize    = AES_MIN_KEY_SIZE,
125                                 .max_keysize    = AES_MAX_KEY_SIZE,
126                                 .setkey         = crypto4xx_setkey_aes_ecb,
127 -                               .encrypt        = crypto4xx_encrypt,
128 -                               .decrypt        = crypto4xx_decrypt,
129 +                               .encrypt        = crypto4xx_encrypt_noiv,
130 +                               .decrypt        = crypto4xx_decrypt_noiv,
131                         }
132                 }
133         } },
134 @@ -1219,8 +1219,8 @@ static struct crypto4xx_alg_common crypt
135                                 .max_keysize    = AES_MAX_KEY_SIZE,
136                                 .ivsize         = AES_IV_SIZE,
137                                 .setkey         = crypto4xx_setkey_aes_ofb,
138 -                               .encrypt        = crypto4xx_encrypt,
139 -                               .decrypt        = crypto4xx_decrypt,
140 +                               .encrypt        = crypto4xx_encrypt_iv,
141 +                               .decrypt        = crypto4xx_decrypt_iv,
142                         }
143                 }
144         } },
145 --- a/drivers/crypto/amcc/crypto4xx_core.h
146 +++ b/drivers/crypto/amcc/crypto4xx_core.h
147 @@ -168,8 +168,10 @@ int crypto4xx_setkey_aes_ofb(struct cryp
148                              const u8 *key, unsigned int keylen);
149  int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
150                              const u8 *key, unsigned int keylen);
151 -int crypto4xx_encrypt(struct ablkcipher_request *req);
152 -int crypto4xx_decrypt(struct ablkcipher_request *req);
153 +int crypto4xx_encrypt_iv(struct ablkcipher_request *req);
154 +int crypto4xx_decrypt_iv(struct ablkcipher_request *req);
155 +int crypto4xx_encrypt_noiv(struct ablkcipher_request *req);
156 +int crypto4xx_decrypt_noiv(struct ablkcipher_request *req);
157  int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req);
158  int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req);
159  int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm);