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
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.
11 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
12 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
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(+)
20 --- a/drivers/crypto/amcc/crypto4xx_alg.c
21 +++ b/drivers/crypto/amcc/crypto4xx_alg.c
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);
34 +int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
35 + const u8 *key, unsigned int keylen)
37 + return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
38 + CRYPTO_FEEDBACK_MODE_128BIT_CFB);
41 +int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
42 + const u8 *key, unsigned int keylen)
44 + return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
45 + CRYPTO_FEEDBACK_MODE_NO_FB);
48 +int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
49 + const u8 *key, unsigned int keylen)
51 + return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
52 + CRYPTO_FEEDBACK_MODE_64BIT_OFB);
55 +int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
56 + const u8 *key, unsigned int keylen)
58 + struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
59 + struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
62 + rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
63 + CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
67 + memcpy(ctx->state_record,
68 + key + keylen - CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
73 +int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
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) };
79 + ctx->direction = DIR_OUTBOUND;
82 + return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
83 + req->nbytes, iv, AES_IV_SIZE);
86 +int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
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) };
92 + ctx->direction = DIR_INBOUND;
95 + return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
96 + req->nbytes, iv, AES_IV_SIZE);
100 * HASH SHA1 Functions
102 --- a/drivers/crypto/amcc/crypto4xx_core.c
103 +++ b/drivers/crypto/amcc/crypto4xx_core.c
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
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 |
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,
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,
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 |
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,
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,
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 |
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,
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,
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 |
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,
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,
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 {
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
247 #define CRYPTO_FEEDBACK_MODE_NO_FB 0
248 #define CRYPTO_FEEDBACK_MODE_64BIT_OFB 0