9977c60d26cb6ab9c60dddb99e1d6b96d7f837ec
[oweals/openwrt.git] / target / linux / apm821xx / patches-4.14 / 020-0008-crypto-crypto4xx-enable-AES-RFC3686-ECB-CFB-and-OFB-.patch
1 From f2a13e7cba9e2b16f4888fbd9cf2bc25b95945be Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@googlemail.com>
3 Date: Fri, 25 Aug 2017 15:47:21 +0200
4 Subject: [PATCH 08/25] crypto: crypto4xx - enable AES RFC3686, ECB, CFB and
5  OFB offloads
6
7 The crypto engine supports more than just aes-cbc. This patch
8 enables the remaining AES block cipher modes that pass the
9 testmanager's test vectors.
10
11 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
12 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
13 ---
14  drivers/crypto/amcc/crypto4xx_alg.c  | 66 ++++++++++++++++++++++++
15  drivers/crypto/amcc/crypto4xx_core.c | 98 ++++++++++++++++++++++++++++++++++++
16  drivers/crypto/amcc/crypto4xx_core.h | 10 ++++
17  drivers/crypto/amcc/crypto4xx_sa.h   |  3 ++
18  4 files changed, 177 insertions(+)
19
20 --- a/drivers/crypto/amcc/crypto4xx_alg.c
21 +++ b/drivers/crypto/amcc/crypto4xx_alg.c
22 @@ -28,6 +28,7 @@
23  #include <crypto/algapi.h>
24  #include <crypto/aes.h>
25  #include <crypto/sha.h>
26 +#include <crypto/ctr.h>
27  #include "crypto4xx_reg_def.h"
28  #include "crypto4xx_core.h"
29  #include "crypto4xx_sa.h"
30 @@ -171,6 +172,71 @@ int crypto4xx_setkey_aes_cbc(struct cryp
31                                     CRYPTO_FEEDBACK_MODE_NO_FB);
32  }
33  
34 +int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
35 +                            const u8 *key, unsigned int keylen)
36 +{
37 +       return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
38 +                                   CRYPTO_FEEDBACK_MODE_128BIT_CFB);
39 +}
40 +
41 +int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
42 +                            const u8 *key, unsigned int keylen)
43 +{
44 +       return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
45 +                                   CRYPTO_FEEDBACK_MODE_NO_FB);
46 +}
47 +
48 +int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
49 +                            const u8 *key, unsigned int keylen)
50 +{
51 +       return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
52 +                                   CRYPTO_FEEDBACK_MODE_64BIT_OFB);
53 +}
54 +
55 +int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
56 +                            const u8 *key, unsigned int keylen)
57 +{
58 +       struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
59 +       struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
60 +       int rc;
61 +
62 +       rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
63 +               CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
64 +       if (rc)
65 +               return rc;
66 +
67 +       memcpy(ctx->state_record,
68 +               key + keylen - CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
69 +
70 +       return 0;
71 +}
72 +
73 +int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
74 +{
75 +       struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
76 +       __be32 iv[AES_IV_SIZE / 4] = { *(u32 *)ctx->state_record,
77 +               *(u32 *) req->info, *(u32 *) (req->info + 4), cpu_to_be32(1) };
78 +
79 +       ctx->direction = DIR_OUTBOUND;
80 +       ctx->pd_ctl = 1;
81 +
82 +       return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
83 +                                 req->nbytes, iv, AES_IV_SIZE);
84 +}
85 +
86 +int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
87 +{
88 +       struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
89 +       __be32 iv[AES_IV_SIZE / 4] = { *(u32 *)ctx->state_record,
90 +               *(u32 *) req->info, *(u32 *) (req->info + 4), cpu_to_be32(1) };
91 +
92 +       ctx->direction = DIR_INBOUND;
93 +       ctx->pd_ctl = 1;
94 +
95 +       return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
96 +                                 req->nbytes, iv, AES_IV_SIZE);
97 +}
98 +
99  /**
100   * HASH SHA1 Functions
101   */
102 --- a/drivers/crypto/amcc/crypto4xx_core.c
103 +++ b/drivers/crypto/amcc/crypto4xx_core.c
104 @@ -36,6 +36,7 @@
105  #include <asm/dcr-regs.h>
106  #include <asm/cacheflush.h>
107  #include <crypto/aes.h>
108 +#include <crypto/ctr.h>
109  #include <crypto/sha.h>
110  #include "crypto4xx_reg_def.h"
111  #include "crypto4xx_core.h"
112 @@ -1133,6 +1134,103 @@ struct crypto4xx_alg_common crypto4xx_al
113                         }
114                 }
115         }},
116 +       { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, .u.cipher = {
117 +               .cra_name       = "cfb(aes)",
118 +               .cra_driver_name = "cfb-aes-ppc4xx",
119 +               .cra_priority   = CRYPTO4XX_CRYPTO_PRIORITY,
120 +               .cra_flags      = CRYPTO_ALG_TYPE_ABLKCIPHER |
121 +                                 CRYPTO_ALG_ASYNC |
122 +                                 CRYPTO_ALG_KERN_DRIVER_ONLY,
123 +               .cra_blocksize  = AES_BLOCK_SIZE,
124 +               .cra_ctxsize    = sizeof(struct crypto4xx_ctx),
125 +               .cra_type       = &crypto_ablkcipher_type,
126 +               .cra_init       = crypto4xx_alg_init,
127 +               .cra_exit       = crypto4xx_alg_exit,
128 +               .cra_module     = THIS_MODULE,
129 +               .cra_u          = {
130 +                       .ablkcipher = {
131 +                               .min_keysize    = AES_MIN_KEY_SIZE,
132 +                               .max_keysize    = AES_MAX_KEY_SIZE,
133 +                               .ivsize         = AES_IV_SIZE,
134 +                               .setkey         = crypto4xx_setkey_aes_cfb,
135 +                               .encrypt        = crypto4xx_encrypt,
136 +                               .decrypt        = crypto4xx_decrypt,
137 +                       }
138 +               }
139 +       } },
140 +       { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, .u.cipher = {
141 +               .cra_name       = "rfc3686(ctr(aes))",
142 +               .cra_driver_name = "rfc3686-ctr-aes-ppc4xx",
143 +               .cra_priority   = CRYPTO4XX_CRYPTO_PRIORITY,
144 +               .cra_flags      = CRYPTO_ALG_TYPE_ABLKCIPHER |
145 +                                 CRYPTO_ALG_ASYNC |
146 +                                 CRYPTO_ALG_KERN_DRIVER_ONLY,
147 +               .cra_blocksize  = AES_BLOCK_SIZE,
148 +               .cra_ctxsize    = sizeof(struct crypto4xx_ctx),
149 +               .cra_type       = &crypto_ablkcipher_type,
150 +               .cra_init       = crypto4xx_alg_init,
151 +               .cra_exit       = crypto4xx_alg_exit,
152 +               .cra_module     = THIS_MODULE,
153 +               .cra_u          = {
154 +                       .ablkcipher = {
155 +                               .min_keysize    = AES_MIN_KEY_SIZE +
156 +                                                 CTR_RFC3686_NONCE_SIZE,
157 +                               .max_keysize    = AES_MAX_KEY_SIZE +
158 +                                                 CTR_RFC3686_NONCE_SIZE,
159 +                               .ivsize         = CTR_RFC3686_IV_SIZE,
160 +                               .setkey         = crypto4xx_setkey_rfc3686,
161 +                               .encrypt        = crypto4xx_rfc3686_encrypt,
162 +                               .decrypt        = crypto4xx_rfc3686_decrypt,
163 +                       }
164 +               }
165 +       } },
166 +       { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, .u.cipher = {
167 +               .cra_name       = "ecb(aes)",
168 +               .cra_driver_name = "ecb-aes-ppc4xx",
169 +               .cra_priority   = CRYPTO4XX_CRYPTO_PRIORITY,
170 +               .cra_flags      = CRYPTO_ALG_TYPE_ABLKCIPHER |
171 +                                 CRYPTO_ALG_ASYNC |
172 +                                 CRYPTO_ALG_KERN_DRIVER_ONLY,
173 +               .cra_blocksize  = AES_BLOCK_SIZE,
174 +               .cra_ctxsize    = sizeof(struct crypto4xx_ctx),
175 +               .cra_type       = &crypto_ablkcipher_type,
176 +               .cra_init       = crypto4xx_alg_init,
177 +               .cra_exit       = crypto4xx_alg_exit,
178 +               .cra_module     = THIS_MODULE,
179 +               .cra_u          = {
180 +                       .ablkcipher = {
181 +                               .min_keysize    = AES_MIN_KEY_SIZE,
182 +                               .max_keysize    = AES_MAX_KEY_SIZE,
183 +                               .setkey         = crypto4xx_setkey_aes_ecb,
184 +                               .encrypt        = crypto4xx_encrypt,
185 +                               .decrypt        = crypto4xx_decrypt,
186 +                       }
187 +               }
188 +       } },
189 +       { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, .u.cipher = {
190 +               .cra_name       = "ofb(aes)",
191 +               .cra_driver_name = "ofb-aes-ppc4xx",
192 +               .cra_priority   = CRYPTO4XX_CRYPTO_PRIORITY,
193 +               .cra_flags      = CRYPTO_ALG_TYPE_ABLKCIPHER |
194 +                                 CRYPTO_ALG_ASYNC |
195 +                                 CRYPTO_ALG_KERN_DRIVER_ONLY,
196 +               .cra_blocksize  = AES_BLOCK_SIZE,
197 +               .cra_ctxsize    = sizeof(struct crypto4xx_ctx),
198 +               .cra_type       = &crypto_ablkcipher_type,
199 +               .cra_init       = crypto4xx_alg_init,
200 +               .cra_exit       = crypto4xx_alg_exit,
201 +               .cra_module     = THIS_MODULE,
202 +               .cra_u          = {
203 +                       .ablkcipher = {
204 +                               .min_keysize    = AES_MIN_KEY_SIZE,
205 +                               .max_keysize    = AES_MAX_KEY_SIZE,
206 +                               .ivsize         = AES_IV_SIZE,
207 +                               .setkey         = crypto4xx_setkey_aes_cbc,
208 +                               .encrypt        = crypto4xx_encrypt,
209 +                               .decrypt        = crypto4xx_decrypt,
210 +                       }
211 +               }
212 +       } },
213  };
214  
215  /**
216 --- a/drivers/crypto/amcc/crypto4xx_core.h
217 +++ b/drivers/crypto/amcc/crypto4xx_core.h
218 @@ -171,8 +171,18 @@ u32 crypto4xx_build_pd(struct crypto_asy
219                        void *iv, u32 iv_len);
220  int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
221                              const u8 *key, unsigned int keylen);
222 +int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
223 +                            const u8 *key, unsigned int keylen);
224 +int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
225 +                            const u8 *key, unsigned int keylen);
226 +int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
227 +                            const u8 *key, unsigned int keylen);
228 +int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
229 +                            const u8 *key, unsigned int keylen);
230  int crypto4xx_encrypt(struct ablkcipher_request *req);
231  int crypto4xx_decrypt(struct ablkcipher_request *req);
232 +int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req);
233 +int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req);
234  int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm);
235  int crypto4xx_hash_digest(struct ahash_request *req);
236  int crypto4xx_hash_final(struct ahash_request *req);
237 --- a/drivers/crypto/amcc/crypto4xx_sa.h
238 +++ b/drivers/crypto/amcc/crypto4xx_sa.h
239 @@ -112,6 +112,9 @@ union sa_command_0 {
240  
241  #define CRYPTO_MODE_ECB                                0
242  #define CRYPTO_MODE_CBC                                1
243 +#define CRYPTO_MODE_OFB                                2
244 +#define CRYPTO_MODE_CFB                                3
245 +#define CRYPTO_MODE_CTR                                4
246  
247  #define CRYPTO_FEEDBACK_MODE_NO_FB             0
248  #define CRYPTO_FEEDBACK_MODE_64BIT_OFB         0