1 // SPDX-License-Identifier: GPL-2.0
3 * StarFive AES acceleration driver
5 * Copyright (c) 2022 StarFive Technology
8 #include <crypto/engine.h>
9 #include <crypto/gcm.h>
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/skcipher.h>
12 #include <crypto/scatterwalk.h>
13 #include "jh7110-cryp.h"
14 #include <linux/err.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
20 #define STARFIVE_AES_REGS_OFFSET 0x100
21 #define STARFIVE_AES_AESDIO0R (STARFIVE_AES_REGS_OFFSET + 0x0)
22 #define STARFIVE_AES_KEY0 (STARFIVE_AES_REGS_OFFSET + 0x4)
23 #define STARFIVE_AES_KEY1 (STARFIVE_AES_REGS_OFFSET + 0x8)
24 #define STARFIVE_AES_KEY2 (STARFIVE_AES_REGS_OFFSET + 0xC)
25 #define STARFIVE_AES_KEY3 (STARFIVE_AES_REGS_OFFSET + 0x10)
26 #define STARFIVE_AES_KEY4 (STARFIVE_AES_REGS_OFFSET + 0x14)
27 #define STARFIVE_AES_KEY5 (STARFIVE_AES_REGS_OFFSET + 0x18)
28 #define STARFIVE_AES_KEY6 (STARFIVE_AES_REGS_OFFSET + 0x1C)
29 #define STARFIVE_AES_KEY7 (STARFIVE_AES_REGS_OFFSET + 0x20)
30 #define STARFIVE_AES_CSR (STARFIVE_AES_REGS_OFFSET + 0x24)
31 #define STARFIVE_AES_IV0 (STARFIVE_AES_REGS_OFFSET + 0x28)
32 #define STARFIVE_AES_IV1 (STARFIVE_AES_REGS_OFFSET + 0x2C)
33 #define STARFIVE_AES_IV2 (STARFIVE_AES_REGS_OFFSET + 0x30)
34 #define STARFIVE_AES_IV3 (STARFIVE_AES_REGS_OFFSET + 0x34)
35 #define STARFIVE_AES_NONCE0 (STARFIVE_AES_REGS_OFFSET + 0x3C)
36 #define STARFIVE_AES_NONCE1 (STARFIVE_AES_REGS_OFFSET + 0x40)
37 #define STARFIVE_AES_NONCE2 (STARFIVE_AES_REGS_OFFSET + 0x44)
38 #define STARFIVE_AES_NONCE3 (STARFIVE_AES_REGS_OFFSET + 0x48)
39 #define STARFIVE_AES_ALEN0 (STARFIVE_AES_REGS_OFFSET + 0x4C)
40 #define STARFIVE_AES_ALEN1 (STARFIVE_AES_REGS_OFFSET + 0x50)
41 #define STARFIVE_AES_MLEN0 (STARFIVE_AES_REGS_OFFSET + 0x54)
42 #define STARFIVE_AES_MLEN1 (STARFIVE_AES_REGS_OFFSET + 0x58)
43 #define STARFIVE_AES_IVLEN (STARFIVE_AES_REGS_OFFSET + 0x5C)
45 #define FLG_MODE_MASK GENMASK(2, 0)
46 #define FLG_ENCRYPT BIT(4)
49 #define CCM_B0_ADATA 0x40
50 #define AES_BLOCK_32 (AES_BLOCK_SIZE / sizeof(u32))
52 static inline int starfive_aes_wait_busy(struct starfive_cryp_dev *cryp)
56 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
57 !(status & STARFIVE_AES_BUSY), 10, 100000);
60 static inline int starfive_aes_wait_keydone(struct starfive_cryp_dev *cryp)
64 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
65 (status & STARFIVE_AES_KEY_DONE), 10, 100000);
68 static inline int starfive_aes_wait_gcmdone(struct starfive_cryp_dev *cryp)
72 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
73 (status & STARFIVE_AES_GCM_DONE), 10, 100000);
76 static inline int is_gcm(struct starfive_cryp_dev *cryp)
78 return (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_GCM;
81 static inline int is_encrypt(struct starfive_cryp_dev *cryp)
83 return cryp->flags & FLG_ENCRYPT;
86 static void starfive_aes_aead_hw_start(struct starfive_cryp_ctx *ctx, u32 hw_mode)
88 struct starfive_cryp_dev *cryp = ctx->cryp;
92 case STARFIVE_AES_MODE_GCM:
93 value = readl(ctx->cryp->base + STARFIVE_AES_CSR);
94 value |= STARFIVE_AES_GCM_START;
95 writel(value, cryp->base + STARFIVE_AES_CSR);
96 starfive_aes_wait_gcmdone(cryp);
98 case STARFIVE_AES_MODE_CCM:
99 value = readl(ctx->cryp->base + STARFIVE_AES_CSR);
100 value |= STARFIVE_AES_CCM_START;
101 writel(value, cryp->base + STARFIVE_AES_CSR);
106 static inline void starfive_aes_set_ivlen(struct starfive_cryp_ctx *ctx)
108 struct starfive_cryp_dev *cryp = ctx->cryp;
111 writel(GCM_AES_IV_SIZE, cryp->base + STARFIVE_AES_IVLEN);
113 writel(AES_BLOCK_SIZE, cryp->base + STARFIVE_AES_IVLEN);
116 static inline void starfive_aes_set_alen(struct starfive_cryp_ctx *ctx)
118 struct starfive_cryp_dev *cryp = ctx->cryp;
120 writel(upper_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN0);
121 writel(lower_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN1);
124 static inline void starfive_aes_set_mlen(struct starfive_cryp_ctx *ctx)
126 struct starfive_cryp_dev *cryp = ctx->cryp;
128 writel(upper_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN0);
129 writel(lower_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN1);
132 static inline int starfive_aes_ccm_check_iv(const u8 *iv)
134 /* 2 <= L <= 8, so 1 <= L' <= 7. */
135 if (iv[0] < 1 || iv[0] > 7)
141 static int starfive_aes_write_iv(struct starfive_cryp_ctx *ctx, u32 *iv)
143 struct starfive_cryp_dev *cryp = ctx->cryp;
145 writel(iv[0], cryp->base + STARFIVE_AES_IV0);
146 writel(iv[1], cryp->base + STARFIVE_AES_IV1);
147 writel(iv[2], cryp->base + STARFIVE_AES_IV2);
150 if (starfive_aes_wait_gcmdone(cryp))
156 writel(iv[3], cryp->base + STARFIVE_AES_IV3);
161 static inline void starfive_aes_get_iv(struct starfive_cryp_dev *cryp, u32 *iv)
163 iv[0] = readl(cryp->base + STARFIVE_AES_IV0);
164 iv[1] = readl(cryp->base + STARFIVE_AES_IV1);
165 iv[2] = readl(cryp->base + STARFIVE_AES_IV2);
166 iv[3] = readl(cryp->base + STARFIVE_AES_IV3);
169 static inline void starfive_aes_write_nonce(struct starfive_cryp_ctx *ctx, u32 *nonce)
171 struct starfive_cryp_dev *cryp = ctx->cryp;
173 writel(nonce[0], cryp->base + STARFIVE_AES_NONCE0);
174 writel(nonce[1], cryp->base + STARFIVE_AES_NONCE1);
175 writel(nonce[2], cryp->base + STARFIVE_AES_NONCE2);
176 writel(nonce[3], cryp->base + STARFIVE_AES_NONCE3);
179 static int starfive_aes_write_key(struct starfive_cryp_ctx *ctx)
181 struct starfive_cryp_dev *cryp = ctx->cryp;
182 u32 *key = (u32 *)ctx->key;
184 if (ctx->keylen >= AES_KEYSIZE_128) {
185 writel(key[0], cryp->base + STARFIVE_AES_KEY0);
186 writel(key[1], cryp->base + STARFIVE_AES_KEY1);
187 writel(key[2], cryp->base + STARFIVE_AES_KEY2);
188 writel(key[3], cryp->base + STARFIVE_AES_KEY3);
191 if (ctx->keylen >= AES_KEYSIZE_192) {
192 writel(key[4], cryp->base + STARFIVE_AES_KEY4);
193 writel(key[5], cryp->base + STARFIVE_AES_KEY5);
196 if (ctx->keylen >= AES_KEYSIZE_256) {
197 writel(key[6], cryp->base + STARFIVE_AES_KEY6);
198 writel(key[7], cryp->base + STARFIVE_AES_KEY7);
201 if (starfive_aes_wait_keydone(cryp))
207 static int starfive_aes_ccm_init(struct starfive_cryp_ctx *ctx)
209 struct starfive_cryp_dev *cryp = ctx->cryp;
210 u8 iv[AES_BLOCK_SIZE], b0[AES_BLOCK_SIZE];
211 unsigned int textlen;
213 memcpy(iv, cryp->req.areq->iv, AES_BLOCK_SIZE);
214 memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
217 memcpy(b0, iv, AES_BLOCK_SIZE);
219 b0[0] |= (8 * ((cryp->authsize - 2) / 2));
222 b0[0] |= CCM_B0_ADATA;
224 textlen = cryp->total_in;
226 b0[AES_BLOCK_SIZE - 2] = textlen >> 8;
227 b0[AES_BLOCK_SIZE - 1] = textlen & 0xFF;
229 starfive_aes_write_nonce(ctx, (u32 *)b0);
234 static int starfive_aes_hw_init(struct starfive_cryp_ctx *ctx)
236 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
237 struct starfive_cryp_dev *cryp = ctx->cryp;
242 rctx->csr.aes.aesrst = 1;
243 writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR);
246 hw_mode = cryp->flags & FLG_MODE_MASK;
250 switch (ctx->keylen) {
251 case AES_KEYSIZE_128:
252 rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_128;
254 case AES_KEYSIZE_192:
255 rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_192;
257 case AES_KEYSIZE_256:
258 rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_256;
262 rctx->csr.aes.mode = hw_mode;
263 rctx->csr.aes.cmode = !is_encrypt(cryp);
264 rctx->csr.aes.ie = 1;
265 rctx->csr.aes.stmode = STARFIVE_AES_MODE_XFB_1;
267 if (cryp->side_chan) {
268 rctx->csr.aes.delay_aes = 1;
269 rctx->csr.aes.vaes_start = 1;
272 writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR);
274 cryp->err = starfive_aes_write_key(ctx);
279 case STARFIVE_AES_MODE_GCM:
280 starfive_aes_set_alen(ctx);
281 starfive_aes_set_mlen(ctx);
282 starfive_aes_set_ivlen(ctx);
283 starfive_aes_aead_hw_start(ctx, hw_mode);
284 starfive_aes_write_iv(ctx, (void *)cryp->req.areq->iv);
286 case STARFIVE_AES_MODE_CCM:
287 starfive_aes_set_alen(ctx);
288 starfive_aes_set_mlen(ctx);
289 starfive_aes_ccm_init(ctx);
290 starfive_aes_aead_hw_start(ctx, hw_mode);
292 case STARFIVE_AES_MODE_CBC:
293 case STARFIVE_AES_MODE_CTR:
294 starfive_aes_write_iv(ctx, (void *)cryp->req.sreq->iv);
303 static int starfive_aes_read_authtag(struct starfive_cryp_dev *cryp)
307 if (starfive_aes_wait_busy(cryp))
308 return dev_err_probe(cryp->dev, -ETIMEDOUT,
309 "Timeout waiting for tag generation.");
311 start_addr = STARFIVE_AES_NONCE0;
314 for (i = 0; i < AES_BLOCK_32; i++, start_addr += 4)
315 cryp->tag_out[i] = readl(cryp->base + start_addr);
317 for (i = 0; i < AES_BLOCK_32; i++)
318 cryp->tag_out[i] = readl(cryp->base + STARFIVE_AES_AESDIO0R);
320 if (is_encrypt(cryp)) {
321 scatterwalk_copychunks(cryp->tag_out, &cryp->out_walk, cryp->authsize, 1);
323 scatterwalk_copychunks(cryp->tag_in, &cryp->in_walk, cryp->authsize, 0);
325 if (crypto_memneq(cryp->tag_in, cryp->tag_out, cryp->authsize))
326 return dev_err_probe(cryp->dev, -EBADMSG, "Failed tag verification\n");
332 static void starfive_aes_finish_req(struct starfive_cryp_dev *cryp)
334 union starfive_aes_csr csr;
337 if (!err && cryp->authsize)
338 err = starfive_aes_read_authtag(cryp);
340 if (!err && ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC ||
341 (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CTR))
342 starfive_aes_get_iv(cryp, (void *)cryp->req.sreq->iv);
347 writel(csr.v, cryp->base + STARFIVE_AES_CSR);
350 crypto_finalize_aead_request(cryp->engine, cryp->req.areq, err);
352 crypto_finalize_skcipher_request(cryp->engine, cryp->req.sreq,
356 void starfive_aes_done_task(unsigned long param)
358 struct starfive_cryp_dev *cryp = (struct starfive_cryp_dev *)param;
359 u32 block[AES_BLOCK_32];
363 for (i = 0; i < AES_BLOCK_32; i++)
364 block[i] = readl(cryp->base + STARFIVE_AES_AESDIO0R);
366 scatterwalk_copychunks(block, &cryp->out_walk, min_t(size_t, AES_BLOCK_SIZE,
367 cryp->total_out), 1);
369 cryp->total_out -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_out);
371 if (!cryp->total_out) {
372 starfive_aes_finish_req(cryp);
376 memset(block, 0, AES_BLOCK_SIZE);
377 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, AES_BLOCK_SIZE,
379 cryp->total_in -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_in);
381 for (i = 0; i < AES_BLOCK_32; i++)
382 writel(block[i], cryp->base + STARFIVE_AES_AESDIO0R);
384 stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET);
385 stat &= ~STARFIVE_IE_MASK_AES_DONE;
386 writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET);
389 static int starfive_aes_gcm_write_adata(struct starfive_cryp_ctx *ctx)
391 struct starfive_cryp_dev *cryp = ctx->cryp;
392 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
396 total_len = ALIGN(cryp->assoclen, AES_BLOCK_SIZE) / sizeof(unsigned int);
397 buffer = (u32 *)rctx->adata;
399 for (loop = 0; loop < total_len; loop += 4) {
400 writel(*buffer, cryp->base + STARFIVE_AES_NONCE0);
402 writel(*buffer, cryp->base + STARFIVE_AES_NONCE1);
404 writel(*buffer, cryp->base + STARFIVE_AES_NONCE2);
406 writel(*buffer, cryp->base + STARFIVE_AES_NONCE3);
410 if (starfive_aes_wait_gcmdone(cryp))
411 return dev_err_probe(cryp->dev, -ETIMEDOUT,
412 "Timeout processing gcm aad block");
417 static int starfive_aes_ccm_write_adata(struct starfive_cryp_ctx *ctx)
419 struct starfive_cryp_dev *cryp = ctx->cryp;
420 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
425 total_len = cryp->assoclen;
428 writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R);
430 writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R);
435 for (loop = 0; loop < 3; loop++, buffer++)
436 writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R);
440 while (total_len > 0) {
441 for (loop = 0; loop < AES_BLOCK_32; loop++, buffer++)
442 writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R);
444 total_len -= AES_BLOCK_SIZE;
447 if (starfive_aes_wait_busy(cryp))
448 return dev_err_probe(cryp->dev, -ETIMEDOUT,
449 "Timeout processing ccm aad block");
454 static int starfive_aes_prepare_req(struct skcipher_request *req,
455 struct aead_request *areq)
457 struct starfive_cryp_ctx *ctx;
458 struct starfive_cryp_request_ctx *rctx;
459 struct starfive_cryp_dev *cryp;
464 ctx = req ? crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)) :
465 crypto_aead_ctx(crypto_aead_reqtfm(areq));
468 rctx = req ? skcipher_request_ctx(req) : aead_request_ctx(areq);
471 cryp->req.sreq = req;
472 cryp->total_in = req->cryptlen;
473 cryp->total_out = req->cryptlen;
477 cryp->req.areq = areq;
478 cryp->assoclen = areq->assoclen;
479 cryp->authsize = crypto_aead_authsize(crypto_aead_reqtfm(areq));
480 if (is_encrypt(cryp)) {
481 cryp->total_in = areq->cryptlen;
482 cryp->total_out = areq->cryptlen;
484 cryp->total_in = areq->cryptlen - cryp->authsize;
485 cryp->total_out = cryp->total_in;
489 rctx->in_sg = req ? req->src : areq->src;
490 scatterwalk_start(&cryp->in_walk, rctx->in_sg);
492 rctx->out_sg = req ? req->dst : areq->dst;
493 scatterwalk_start(&cryp->out_walk, rctx->out_sg);
495 if (cryp->assoclen) {
496 rctx->adata = kzalloc(cryp->assoclen + AES_BLOCK_SIZE, GFP_KERNEL);
498 return dev_err_probe(cryp->dev, -ENOMEM,
499 "Failed to alloc memory for adata");
501 scatterwalk_copychunks(rctx->adata, &cryp->in_walk, cryp->assoclen, 0);
502 scatterwalk_copychunks(NULL, &cryp->out_walk, cryp->assoclen, 2);
507 return starfive_aes_hw_init(ctx);
510 static int starfive_aes_do_one_req(struct crypto_engine *engine, void *areq)
512 struct skcipher_request *req =
513 container_of(areq, struct skcipher_request, base);
514 struct starfive_cryp_ctx *ctx =
515 crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
516 struct starfive_cryp_dev *cryp = ctx->cryp;
517 u32 block[AES_BLOCK_32];
522 err = starfive_aes_prepare_req(req, NULL);
527 * Write first plain/ciphertext block to start the module
528 * then let irq tasklet handle the rest of the data blocks.
530 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, AES_BLOCK_SIZE,
532 cryp->total_in -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_in);
534 for (i = 0; i < AES_BLOCK_32; i++)
535 writel(block[i], cryp->base + STARFIVE_AES_AESDIO0R);
537 stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET);
538 stat &= ~STARFIVE_IE_MASK_AES_DONE;
539 writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET);
544 static int starfive_aes_init_tfm(struct crypto_skcipher *tfm)
546 struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
548 ctx->cryp = starfive_cryp_find_dev(ctx);
552 crypto_skcipher_set_reqsize(tfm, sizeof(struct starfive_cryp_request_ctx) +
553 sizeof(struct skcipher_request));
558 static int starfive_aes_aead_do_one_req(struct crypto_engine *engine, void *areq)
560 struct aead_request *req =
561 container_of(areq, struct aead_request, base);
562 struct starfive_cryp_ctx *ctx =
563 crypto_aead_ctx(crypto_aead_reqtfm(req));
564 struct starfive_cryp_dev *cryp = ctx->cryp;
565 struct starfive_cryp_request_ctx *rctx;
566 u32 block[AES_BLOCK_32];
571 err = starfive_aes_prepare_req(NULL, req);
580 if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CCM)
581 cryp->err = starfive_aes_ccm_write_adata(ctx);
583 cryp->err = starfive_aes_gcm_write_adata(ctx);
595 * Write first plain/ciphertext block to start the module
596 * then let irq tasklet handle the rest of the data blocks.
598 scatterwalk_copychunks(block, &cryp->in_walk, min_t(size_t, AES_BLOCK_SIZE,
600 cryp->total_in -= min_t(size_t, AES_BLOCK_SIZE, cryp->total_in);
602 for (i = 0; i < AES_BLOCK_32; i++)
603 writel(block[i], cryp->base + STARFIVE_AES_AESDIO0R);
605 stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET);
606 stat &= ~STARFIVE_IE_MASK_AES_DONE;
607 writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET);
612 starfive_aes_finish_req(cryp);
616 static int starfive_aes_aead_init_tfm(struct crypto_aead *tfm)
618 struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
619 struct starfive_cryp_dev *cryp = ctx->cryp;
620 struct crypto_tfm *aead = crypto_aead_tfm(tfm);
621 struct crypto_alg *alg = aead->__crt_alg;
623 ctx->cryp = starfive_cryp_find_dev(ctx);
627 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
628 ctx->aead_fbk = crypto_alloc_aead(alg->cra_name, 0,
629 CRYPTO_ALG_NEED_FALLBACK);
630 if (IS_ERR(ctx->aead_fbk))
631 return dev_err_probe(cryp->dev, PTR_ERR(ctx->aead_fbk),
632 "%s() failed to allocate fallback for %s\n",
633 __func__, alg->cra_name);
636 crypto_aead_set_reqsize(tfm, sizeof(struct starfive_cryp_ctx) +
637 sizeof(struct aead_request));
642 static void starfive_aes_aead_exit_tfm(struct crypto_aead *tfm)
644 struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
646 crypto_free_aead(ctx->aead_fbk);
649 static int starfive_aes_crypt(struct skcipher_request *req, unsigned long flags)
651 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
652 struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
653 struct starfive_cryp_dev *cryp = ctx->cryp;
654 unsigned int blocksize_align = crypto_skcipher_blocksize(tfm) - 1;
658 if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_ECB ||
659 (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC)
660 if (req->cryptlen & blocksize_align)
663 return crypto_transfer_skcipher_request_to_engine(cryp->engine, req);
666 static int starfive_aes_aead_crypt(struct aead_request *req, unsigned long flags)
668 struct starfive_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
669 struct starfive_cryp_dev *cryp = ctx->cryp;
674 * HW engine could not perform CCM tag verification on
675 * non-blocksize aligned text, use fallback algo instead
677 if (ctx->aead_fbk && !is_encrypt(cryp)) {
678 struct aead_request *subreq = aead_request_ctx(req);
680 aead_request_set_tfm(subreq, ctx->aead_fbk);
681 aead_request_set_callback(subreq, req->base.flags,
682 req->base.complete, req->base.data);
683 aead_request_set_crypt(subreq, req->src,
684 req->dst, req->cryptlen, req->iv);
685 aead_request_set_ad(subreq, req->assoclen);
687 return crypto_aead_decrypt(subreq);
690 return crypto_transfer_aead_request_to_engine(cryp->engine, req);
693 static int starfive_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
696 struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
701 if (keylen != AES_KEYSIZE_128 &&
702 keylen != AES_KEYSIZE_192 &&
703 keylen != AES_KEYSIZE_256)
706 memcpy(ctx->key, key, keylen);
707 ctx->keylen = keylen;
712 static int starfive_aes_aead_setkey(struct crypto_aead *tfm, const u8 *key,
715 struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
720 if (keylen != AES_KEYSIZE_128 &&
721 keylen != AES_KEYSIZE_192 &&
722 keylen != AES_KEYSIZE_256)
725 memcpy(ctx->key, key, keylen);
726 ctx->keylen = keylen;
729 return crypto_aead_setkey(ctx->aead_fbk, key, keylen);
734 static int starfive_aes_gcm_setauthsize(struct crypto_aead *tfm,
735 unsigned int authsize)
737 return crypto_gcm_check_authsize(authsize);
740 static int starfive_aes_ccm_setauthsize(struct crypto_aead *tfm,
741 unsigned int authsize)
743 struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
758 return crypto_aead_setauthsize(ctx->aead_fbk, authsize);
761 static int starfive_aes_ecb_encrypt(struct skcipher_request *req)
763 return starfive_aes_crypt(req, STARFIVE_AES_MODE_ECB | FLG_ENCRYPT);
766 static int starfive_aes_ecb_decrypt(struct skcipher_request *req)
768 return starfive_aes_crypt(req, STARFIVE_AES_MODE_ECB);
771 static int starfive_aes_cbc_encrypt(struct skcipher_request *req)
773 return starfive_aes_crypt(req, STARFIVE_AES_MODE_CBC | FLG_ENCRYPT);
776 static int starfive_aes_cbc_decrypt(struct skcipher_request *req)
778 return starfive_aes_crypt(req, STARFIVE_AES_MODE_CBC);
781 static int starfive_aes_ctr_encrypt(struct skcipher_request *req)
783 return starfive_aes_crypt(req, STARFIVE_AES_MODE_CTR | FLG_ENCRYPT);
786 static int starfive_aes_ctr_decrypt(struct skcipher_request *req)
788 return starfive_aes_crypt(req, STARFIVE_AES_MODE_CTR);
791 static int starfive_aes_gcm_encrypt(struct aead_request *req)
793 return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_GCM | FLG_ENCRYPT);
796 static int starfive_aes_gcm_decrypt(struct aead_request *req)
798 return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_GCM);
801 static int starfive_aes_ccm_encrypt(struct aead_request *req)
805 ret = starfive_aes_ccm_check_iv(req->iv);
809 return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_CCM | FLG_ENCRYPT);
812 static int starfive_aes_ccm_decrypt(struct aead_request *req)
816 ret = starfive_aes_ccm_check_iv(req->iv);
820 return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_CCM);
823 static struct skcipher_engine_alg skcipher_algs[] = {
825 .base.init = starfive_aes_init_tfm,
826 .base.setkey = starfive_aes_setkey,
827 .base.encrypt = starfive_aes_ecb_encrypt,
828 .base.decrypt = starfive_aes_ecb_decrypt,
829 .base.min_keysize = AES_MIN_KEY_SIZE,
830 .base.max_keysize = AES_MAX_KEY_SIZE,
832 .cra_name = "ecb(aes)",
833 .cra_driver_name = "starfive-ecb-aes",
835 .cra_flags = CRYPTO_ALG_ASYNC,
836 .cra_blocksize = AES_BLOCK_SIZE,
837 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
838 .cra_alignmask = 0xf,
839 .cra_module = THIS_MODULE,
842 .do_one_request = starfive_aes_do_one_req,
845 .base.init = starfive_aes_init_tfm,
846 .base.setkey = starfive_aes_setkey,
847 .base.encrypt = starfive_aes_cbc_encrypt,
848 .base.decrypt = starfive_aes_cbc_decrypt,
849 .base.min_keysize = AES_MIN_KEY_SIZE,
850 .base.max_keysize = AES_MAX_KEY_SIZE,
851 .base.ivsize = AES_BLOCK_SIZE,
853 .cra_name = "cbc(aes)",
854 .cra_driver_name = "starfive-cbc-aes",
856 .cra_flags = CRYPTO_ALG_ASYNC,
857 .cra_blocksize = AES_BLOCK_SIZE,
858 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
859 .cra_alignmask = 0xf,
860 .cra_module = THIS_MODULE,
863 .do_one_request = starfive_aes_do_one_req,
866 .base.init = starfive_aes_init_tfm,
867 .base.setkey = starfive_aes_setkey,
868 .base.encrypt = starfive_aes_ctr_encrypt,
869 .base.decrypt = starfive_aes_ctr_decrypt,
870 .base.min_keysize = AES_MIN_KEY_SIZE,
871 .base.max_keysize = AES_MAX_KEY_SIZE,
872 .base.ivsize = AES_BLOCK_SIZE,
874 .cra_name = "ctr(aes)",
875 .cra_driver_name = "starfive-ctr-aes",
877 .cra_flags = CRYPTO_ALG_ASYNC,
879 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
880 .cra_alignmask = 0xf,
881 .cra_module = THIS_MODULE,
884 .do_one_request = starfive_aes_do_one_req,
889 static struct aead_engine_alg aead_algs[] = {
891 .base.setkey = starfive_aes_aead_setkey,
892 .base.setauthsize = starfive_aes_gcm_setauthsize,
893 .base.encrypt = starfive_aes_gcm_encrypt,
894 .base.decrypt = starfive_aes_gcm_decrypt,
895 .base.init = starfive_aes_aead_init_tfm,
896 .base.exit = starfive_aes_aead_exit_tfm,
897 .base.ivsize = GCM_AES_IV_SIZE,
898 .base.maxauthsize = AES_BLOCK_SIZE,
900 .cra_name = "gcm(aes)",
901 .cra_driver_name = "starfive-gcm-aes",
903 .cra_flags = CRYPTO_ALG_ASYNC,
905 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
906 .cra_alignmask = 0xf,
907 .cra_module = THIS_MODULE,
910 .do_one_request = starfive_aes_aead_do_one_req,
913 .base.setkey = starfive_aes_aead_setkey,
914 .base.setauthsize = starfive_aes_ccm_setauthsize,
915 .base.encrypt = starfive_aes_ccm_encrypt,
916 .base.decrypt = starfive_aes_ccm_decrypt,
917 .base.init = starfive_aes_aead_init_tfm,
918 .base.exit = starfive_aes_aead_exit_tfm,
919 .base.ivsize = AES_BLOCK_SIZE,
920 .base.maxauthsize = AES_BLOCK_SIZE,
922 .cra_name = "ccm(aes)",
923 .cra_driver_name = "starfive-ccm-aes",
925 .cra_flags = CRYPTO_ALG_ASYNC |
926 CRYPTO_ALG_NEED_FALLBACK,
928 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
929 .cra_alignmask = 0xf,
930 .cra_module = THIS_MODULE,
933 .do_one_request = starfive_aes_aead_do_one_req,
938 int starfive_aes_register_algs(void)
942 ret = crypto_engine_register_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
946 ret = crypto_engine_register_aeads(aead_algs, ARRAY_SIZE(aead_algs));
948 crypto_engine_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
953 void starfive_aes_unregister_algs(void)
955 crypto_engine_unregister_aeads(aead_algs, ARRAY_SIZE(aead_algs));
956 crypto_engine_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));