hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/crypto/mediatek/mtk-aes.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Cryptographic API.
34 *
....@@ -5,15 +6,12 @@
56 *
67 * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
78 *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
11
- *
129 * Some ideas are from atmel-aes.c drivers.
1310 */
1411
1512 #include <crypto/aes.h>
1613 #include <crypto/gcm.h>
14
+#include <crypto/internal/skcipher.h>
1715 #include "mtk-platform.h"
1816
1917 #define AES_QUEUE_SIZE 512
....@@ -26,7 +24,7 @@
2624
2725 #define AES_CT_CTRL_HDR cpu_to_le32(0x00220000)
2826
29
-/* AES-CBC/ECB/CTR command token */
27
+/* AES-CBC/ECB/CTR/OFB/CFB command token */
3028 #define AES_CMD0 cpu_to_le32(0x05000000)
3129 #define AES_CMD1 cpu_to_le32(0x2d060000)
3230 #define AES_CMD2 cpu_to_le32(0xe4a63806)
....@@ -53,6 +51,8 @@
5351 /* AES transform information word 1 fields */
5452 #define AES_TFM_ECB cpu_to_le32(0x0 << 0)
5553 #define AES_TFM_CBC cpu_to_le32(0x1 << 0)
54
+#define AES_TFM_OFB cpu_to_le32(0x4 << 0)
55
+#define AES_TFM_CFB128 cpu_to_le32(0x5 << 0)
5656 #define AES_TFM_CTR_INIT cpu_to_le32(0x2 << 0) /* init counter to 1 */
5757 #define AES_TFM_CTR_LOAD cpu_to_le32(0x6 << 0) /* load/reuse counter */
5858 #define AES_TFM_3IV cpu_to_le32(0x7 << 5) /* using IV 0-2 */
....@@ -61,13 +61,15 @@
6161 #define AES_TFM_ENC_HASH cpu_to_le32(0x1 << 17)
6262
6363 /* AES flags */
64
-#define AES_FLAGS_CIPHER_MSK GENMASK(2, 0)
64
+#define AES_FLAGS_CIPHER_MSK GENMASK(4, 0)
6565 #define AES_FLAGS_ECB BIT(0)
6666 #define AES_FLAGS_CBC BIT(1)
6767 #define AES_FLAGS_CTR BIT(2)
68
-#define AES_FLAGS_GCM BIT(3)
69
-#define AES_FLAGS_ENCRYPT BIT(4)
70
-#define AES_FLAGS_BUSY BIT(5)
68
+#define AES_FLAGS_OFB BIT(3)
69
+#define AES_FLAGS_CFB128 BIT(4)
70
+#define AES_FLAGS_GCM BIT(5)
71
+#define AES_FLAGS_ENCRYPT BIT(6)
72
+#define AES_FLAGS_BUSY BIT(7)
7173
7274 #define AES_AUTH_TAG_ERR cpu_to_le32(BIT(26))
7375
....@@ -104,6 +106,7 @@
104106 struct mtk_aes_base_ctx {
105107 struct mtk_cryp *cryp;
106108 u32 keylen;
109
+ __le32 key[12];
107110 __le32 keymode;
108111
109112 mtk_aes_fn start;
....@@ -123,7 +126,7 @@
123126 struct mtk_aes_ctr_ctx {
124127 struct mtk_aes_base_ctx base;
125128
126
- u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
129
+ __be32 iv[AES_BLOCK_SIZE / sizeof(u32)];
127130 size_t offset;
128131 struct scatterlist src[2];
129132 struct scatterlist dst[2];
....@@ -134,8 +137,6 @@
134137
135138 u32 authsize;
136139 size_t textlen;
137
-
138
- struct crypto_skcipher *ctr;
139140 };
140141
141142 struct mtk_aes_drv {
....@@ -241,22 +242,6 @@
241242 sg->length += dma->remainder;
242243 }
243244
244
-static inline void mtk_aes_write_state_le(__le32 *dst, const u32 *src, u32 size)
245
-{
246
- int i;
247
-
248
- for (i = 0; i < SIZE_IN_WORDS(size); i++)
249
- dst[i] = cpu_to_le32(src[i]);
250
-}
251
-
252
-static inline void mtk_aes_write_state_be(__be32 *dst, const u32 *src, u32 size)
253
-{
254
- int i;
255
-
256
- for (i = 0; i < SIZE_IN_WORDS(size); i++)
257
- dst[i] = cpu_to_be32(src[i]);
258
-}
259
-
260245 static inline int mtk_aes_complete(struct mtk_cryp *cryp,
261246 struct mtk_aes_rec *aes,
262247 int err)
....@@ -320,7 +305,7 @@
320305
321306 /* Prepare enough space for authenticated tag */
322307 if (aes->flags & AES_FLAGS_GCM)
323
- res->hdr += AES_BLOCK_SIZE;
308
+ le32_add_cpu(&res->hdr, AES_BLOCK_SIZE);
324309
325310 /*
326311 * Make sure that all changes to the DMA ring are done before we
....@@ -408,11 +393,11 @@
408393 return mtk_aes_complete(cryp, aes, -EINVAL);
409394 }
410395
411
-/* Initialize transform information of CBC/ECB/CTR mode */
396
+/* Initialize transform information of CBC/ECB/CTR/OFB/CFB mode */
412397 static void mtk_aes_info_init(struct mtk_cryp *cryp, struct mtk_aes_rec *aes,
413398 size_t len)
414399 {
415
- struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
400
+ struct skcipher_request *req = skcipher_request_cast(aes->areq);
416401 struct mtk_aes_base_ctx *ctx = aes->ctx;
417402 struct mtk_aes_info *info = &ctx->info;
418403 u32 cnt = 0;
....@@ -437,16 +422,21 @@
437422 case AES_FLAGS_CTR:
438423 info->tfm[1] = AES_TFM_CTR_LOAD;
439424 goto ctr;
440
-
425
+ case AES_FLAGS_OFB:
426
+ info->tfm[1] = AES_TFM_OFB;
427
+ break;
428
+ case AES_FLAGS_CFB128:
429
+ info->tfm[1] = AES_TFM_CFB128;
430
+ break;
441431 default:
442432 /* Should not happen... */
443433 return;
444434 }
445435
446
- mtk_aes_write_state_le(info->state + ctx->keylen, req->info,
447
- AES_BLOCK_SIZE);
436
+ memcpy(info->state + ctx->keylen, req->iv, AES_BLOCK_SIZE);
448437 ctr:
449
- info->tfm[0] += AES_TFM_SIZE(SIZE_IN_WORDS(AES_BLOCK_SIZE));
438
+ le32_add_cpu(&info->tfm[0],
439
+ le32_to_cpu(AES_TFM_SIZE(SIZE_IN_WORDS(AES_BLOCK_SIZE))));
450440 info->tfm[1] |= AES_TFM_FULL_IV;
451441 info->cmd[cnt++] = AES_CMD2;
452442 ecb:
....@@ -528,6 +518,8 @@
528518 backlog->complete(backlog, -EINPROGRESS);
529519
530520 ctx = crypto_tfm_ctx(areq->tfm);
521
+ /* Write key into state buffer */
522
+ memcpy(ctx->info.state, ctx->key, sizeof(ctx->key));
531523
532524 aes->areq = areq;
533525 aes->ctx = ctx;
....@@ -543,13 +535,13 @@
543535
544536 static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
545537 {
546
- struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
547
- struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req);
538
+ struct skcipher_request *req = skcipher_request_cast(aes->areq);
539
+ struct mtk_aes_reqctx *rctx = skcipher_request_ctx(req);
548540
549541 mtk_aes_set_mode(aes, rctx);
550542 aes->resume = mtk_aes_transfer_complete;
551543
552
- return mtk_aes_dma(cryp, aes, req->src, req->dst, req->nbytes);
544
+ return mtk_aes_dma(cryp, aes, req->src, req->dst, req->cryptlen);
553545 }
554546
555547 static inline struct mtk_aes_ctr_ctx *
....@@ -562,7 +554,7 @@
562554 {
563555 struct mtk_aes_base_ctx *ctx = aes->ctx;
564556 struct mtk_aes_ctr_ctx *cctx = mtk_aes_ctr_ctx_cast(ctx);
565
- struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
557
+ struct skcipher_request *req = skcipher_request_cast(aes->areq);
566558 struct scatterlist *src, *dst;
567559 u32 start, end, ctr, blocks;
568560 size_t datalen;
....@@ -570,11 +562,11 @@
570562
571563 /* Check for transfer completion. */
572564 cctx->offset += aes->total;
573
- if (cctx->offset >= req->nbytes)
565
+ if (cctx->offset >= req->cryptlen)
574566 return mtk_aes_transfer_complete(cryp, aes);
575567
576568 /* Compute data length. */
577
- datalen = req->nbytes - cctx->offset;
569
+ datalen = req->cryptlen - cctx->offset;
578570 blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
579571 ctr = be32_to_cpu(cctx->iv[3]);
580572
....@@ -582,7 +574,7 @@
582574 start = ctr;
583575 end = start + blocks - 1;
584576 if (end < start) {
585
- ctr |= 0xffffffff;
577
+ ctr = 0xffffffff;
586578 datalen = AES_BLOCK_SIZE * -start;
587579 fragmented = true;
588580 }
....@@ -593,8 +585,7 @@
593585 scatterwalk_ffwd(cctx->dst, req->dst, cctx->offset));
594586
595587 /* Write IVs into transform state buffer. */
596
- mtk_aes_write_state_le(ctx->info.state + ctx->keylen, cctx->iv,
597
- AES_BLOCK_SIZE);
588
+ memcpy(ctx->info.state + ctx->keylen, cctx->iv, AES_BLOCK_SIZE);
598589
599590 if (unlikely(fragmented)) {
600591 /*
....@@ -611,12 +602,12 @@
611602 static int mtk_aes_ctr_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
612603 {
613604 struct mtk_aes_ctr_ctx *cctx = mtk_aes_ctr_ctx_cast(aes->ctx);
614
- struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
615
- struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req);
605
+ struct skcipher_request *req = skcipher_request_cast(aes->areq);
606
+ struct mtk_aes_reqctx *rctx = skcipher_request_ctx(req);
616607
617608 mtk_aes_set_mode(aes, rctx);
618609
619
- memcpy(cctx->iv, req->info, AES_BLOCK_SIZE);
610
+ memcpy(cctx->iv, req->iv, AES_BLOCK_SIZE);
620611 cctx->offset = 0;
621612 aes->total = 0;
622613 aes->resume = mtk_aes_ctr_transfer;
....@@ -625,10 +616,10 @@
625616 }
626617
627618 /* Check and set the AES key to transform state buffer */
628
-static int mtk_aes_setkey(struct crypto_ablkcipher *tfm,
619
+static int mtk_aes_setkey(struct crypto_skcipher *tfm,
629620 const u8 *key, u32 keylen)
630621 {
631
- struct mtk_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
622
+ struct mtk_aes_base_ctx *ctx = crypto_skcipher_ctx(tfm);
632623
633624 switch (keylen) {
634625 case AES_KEYSIZE_128:
....@@ -642,153 +633,188 @@
642633 break;
643634
644635 default:
645
- crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
646636 return -EINVAL;
647637 }
648638
649639 ctx->keylen = SIZE_IN_WORDS(keylen);
650
- mtk_aes_write_state_le(ctx->info.state, (const u32 *)key, keylen);
640
+ memcpy(ctx->key, key, keylen);
651641
652642 return 0;
653643 }
654644
655
-static int mtk_aes_crypt(struct ablkcipher_request *req, u64 mode)
645
+static int mtk_aes_crypt(struct skcipher_request *req, u64 mode)
656646 {
657
- struct mtk_aes_base_ctx *ctx;
647
+ struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
648
+ struct mtk_aes_base_ctx *ctx = crypto_skcipher_ctx(skcipher);
658649 struct mtk_aes_reqctx *rctx;
650
+ struct mtk_cryp *cryp;
659651
660
- ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
661
- rctx = ablkcipher_request_ctx(req);
652
+ cryp = mtk_aes_find_dev(ctx);
653
+ if (!cryp)
654
+ return -ENODEV;
655
+
656
+ rctx = skcipher_request_ctx(req);
662657 rctx->mode = mode;
663658
664
- return mtk_aes_handle_queue(ctx->cryp, !(mode & AES_FLAGS_ENCRYPT),
659
+ return mtk_aes_handle_queue(cryp, !(mode & AES_FLAGS_ENCRYPT),
665660 &req->base);
666661 }
667662
668
-static int mtk_aes_ecb_encrypt(struct ablkcipher_request *req)
663
+static int mtk_aes_ecb_encrypt(struct skcipher_request *req)
669664 {
670665 return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_ECB);
671666 }
672667
673
-static int mtk_aes_ecb_decrypt(struct ablkcipher_request *req)
668
+static int mtk_aes_ecb_decrypt(struct skcipher_request *req)
674669 {
675670 return mtk_aes_crypt(req, AES_FLAGS_ECB);
676671 }
677672
678
-static int mtk_aes_cbc_encrypt(struct ablkcipher_request *req)
673
+static int mtk_aes_cbc_encrypt(struct skcipher_request *req)
679674 {
680675 return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CBC);
681676 }
682677
683
-static int mtk_aes_cbc_decrypt(struct ablkcipher_request *req)
678
+static int mtk_aes_cbc_decrypt(struct skcipher_request *req)
684679 {
685680 return mtk_aes_crypt(req, AES_FLAGS_CBC);
686681 }
687682
688
-static int mtk_aes_ctr_encrypt(struct ablkcipher_request *req)
683
+static int mtk_aes_ctr_encrypt(struct skcipher_request *req)
689684 {
690685 return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CTR);
691686 }
692687
693
-static int mtk_aes_ctr_decrypt(struct ablkcipher_request *req)
688
+static int mtk_aes_ctr_decrypt(struct skcipher_request *req)
694689 {
695690 return mtk_aes_crypt(req, AES_FLAGS_CTR);
696691 }
697692
698
-static int mtk_aes_cra_init(struct crypto_tfm *tfm)
693
+static int mtk_aes_ofb_encrypt(struct skcipher_request *req)
699694 {
700
- struct mtk_aes_ctx *ctx = crypto_tfm_ctx(tfm);
701
- struct mtk_cryp *cryp = NULL;
695
+ return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_OFB);
696
+}
702697
703
- cryp = mtk_aes_find_dev(&ctx->base);
704
- if (!cryp) {
705
- pr_err("can't find crypto device\n");
706
- return -ENODEV;
707
- }
698
+static int mtk_aes_ofb_decrypt(struct skcipher_request *req)
699
+{
700
+ return mtk_aes_crypt(req, AES_FLAGS_OFB);
701
+}
708702
709
- tfm->crt_ablkcipher.reqsize = sizeof(struct mtk_aes_reqctx);
703
+static int mtk_aes_cfb_encrypt(struct skcipher_request *req)
704
+{
705
+ return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CFB128);
706
+}
707
+
708
+static int mtk_aes_cfb_decrypt(struct skcipher_request *req)
709
+{
710
+ return mtk_aes_crypt(req, AES_FLAGS_CFB128);
711
+}
712
+
713
+static int mtk_aes_init_tfm(struct crypto_skcipher *tfm)
714
+{
715
+ struct mtk_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
716
+
717
+ crypto_skcipher_set_reqsize(tfm, sizeof(struct mtk_aes_reqctx));
710718 ctx->base.start = mtk_aes_start;
711719 return 0;
712720 }
713721
714
-static int mtk_aes_ctr_cra_init(struct crypto_tfm *tfm)
722
+static int mtk_aes_ctr_init_tfm(struct crypto_skcipher *tfm)
715723 {
716
- struct mtk_aes_ctx *ctx = crypto_tfm_ctx(tfm);
717
- struct mtk_cryp *cryp = NULL;
724
+ struct mtk_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
718725
719
- cryp = mtk_aes_find_dev(&ctx->base);
720
- if (!cryp) {
721
- pr_err("can't find crypto device\n");
722
- return -ENODEV;
723
- }
724
-
725
- tfm->crt_ablkcipher.reqsize = sizeof(struct mtk_aes_reqctx);
726
+ crypto_skcipher_set_reqsize(tfm, sizeof(struct mtk_aes_reqctx));
726727 ctx->base.start = mtk_aes_ctr_start;
727728 return 0;
728729 }
729730
730
-static struct crypto_alg aes_algs[] = {
731
+static struct skcipher_alg aes_algs[] = {
731732 {
732
- .cra_name = "cbc(aes)",
733
- .cra_driver_name = "cbc-aes-mtk",
734
- .cra_priority = 400,
735
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
736
- CRYPTO_ALG_ASYNC,
737
- .cra_init = mtk_aes_cra_init,
738
- .cra_blocksize = AES_BLOCK_SIZE,
739
- .cra_ctxsize = sizeof(struct mtk_aes_ctx),
740
- .cra_alignmask = 0xf,
741
- .cra_type = &crypto_ablkcipher_type,
742
- .cra_module = THIS_MODULE,
743
- .cra_u.ablkcipher = {
744
- .min_keysize = AES_MIN_KEY_SIZE,
745
- .max_keysize = AES_MAX_KEY_SIZE,
746
- .setkey = mtk_aes_setkey,
747
- .encrypt = mtk_aes_cbc_encrypt,
748
- .decrypt = mtk_aes_cbc_decrypt,
749
- .ivsize = AES_BLOCK_SIZE,
750
- }
733
+ .base.cra_name = "cbc(aes)",
734
+ .base.cra_driver_name = "cbc-aes-mtk",
735
+ .base.cra_priority = 400,
736
+ .base.cra_flags = CRYPTO_ALG_ASYNC,
737
+ .base.cra_blocksize = AES_BLOCK_SIZE,
738
+ .base.cra_ctxsize = sizeof(struct mtk_aes_ctx),
739
+ .base.cra_alignmask = 0xf,
740
+ .base.cra_module = THIS_MODULE,
741
+
742
+ .min_keysize = AES_MIN_KEY_SIZE,
743
+ .max_keysize = AES_MAX_KEY_SIZE,
744
+ .setkey = mtk_aes_setkey,
745
+ .encrypt = mtk_aes_cbc_encrypt,
746
+ .decrypt = mtk_aes_cbc_decrypt,
747
+ .ivsize = AES_BLOCK_SIZE,
748
+ .init = mtk_aes_init_tfm,
751749 },
752750 {
753
- .cra_name = "ecb(aes)",
754
- .cra_driver_name = "ecb-aes-mtk",
755
- .cra_priority = 400,
756
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
757
- CRYPTO_ALG_ASYNC,
758
- .cra_init = mtk_aes_cra_init,
759
- .cra_blocksize = AES_BLOCK_SIZE,
760
- .cra_ctxsize = sizeof(struct mtk_aes_ctx),
761
- .cra_alignmask = 0xf,
762
- .cra_type = &crypto_ablkcipher_type,
763
- .cra_module = THIS_MODULE,
764
- .cra_u.ablkcipher = {
765
- .min_keysize = AES_MIN_KEY_SIZE,
766
- .max_keysize = AES_MAX_KEY_SIZE,
767
- .setkey = mtk_aes_setkey,
768
- .encrypt = mtk_aes_ecb_encrypt,
769
- .decrypt = mtk_aes_ecb_decrypt,
770
- }
751
+ .base.cra_name = "ecb(aes)",
752
+ .base.cra_driver_name = "ecb-aes-mtk",
753
+ .base.cra_priority = 400,
754
+ .base.cra_flags = CRYPTO_ALG_ASYNC,
755
+ .base.cra_blocksize = AES_BLOCK_SIZE,
756
+ .base.cra_ctxsize = sizeof(struct mtk_aes_ctx),
757
+ .base.cra_alignmask = 0xf,
758
+ .base.cra_module = THIS_MODULE,
759
+
760
+ .min_keysize = AES_MIN_KEY_SIZE,
761
+ .max_keysize = AES_MAX_KEY_SIZE,
762
+ .setkey = mtk_aes_setkey,
763
+ .encrypt = mtk_aes_ecb_encrypt,
764
+ .decrypt = mtk_aes_ecb_decrypt,
765
+ .init = mtk_aes_init_tfm,
771766 },
772767 {
773
- .cra_name = "ctr(aes)",
774
- .cra_driver_name = "ctr-aes-mtk",
775
- .cra_priority = 400,
776
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
777
- CRYPTO_ALG_ASYNC,
778
- .cra_init = mtk_aes_ctr_cra_init,
779
- .cra_blocksize = 1,
780
- .cra_ctxsize = sizeof(struct mtk_aes_ctr_ctx),
781
- .cra_alignmask = 0xf,
782
- .cra_type = &crypto_ablkcipher_type,
783
- .cra_module = THIS_MODULE,
784
- .cra_u.ablkcipher = {
785
- .min_keysize = AES_MIN_KEY_SIZE,
786
- .max_keysize = AES_MAX_KEY_SIZE,
787
- .ivsize = AES_BLOCK_SIZE,
788
- .setkey = mtk_aes_setkey,
789
- .encrypt = mtk_aes_ctr_encrypt,
790
- .decrypt = mtk_aes_ctr_decrypt,
791
- }
768
+ .base.cra_name = "ctr(aes)",
769
+ .base.cra_driver_name = "ctr-aes-mtk",
770
+ .base.cra_priority = 400,
771
+ .base.cra_flags = CRYPTO_ALG_ASYNC,
772
+ .base.cra_blocksize = 1,
773
+ .base.cra_ctxsize = sizeof(struct mtk_aes_ctx),
774
+ .base.cra_alignmask = 0xf,
775
+ .base.cra_module = THIS_MODULE,
776
+
777
+ .min_keysize = AES_MIN_KEY_SIZE,
778
+ .max_keysize = AES_MAX_KEY_SIZE,
779
+ .ivsize = AES_BLOCK_SIZE,
780
+ .setkey = mtk_aes_setkey,
781
+ .encrypt = mtk_aes_ctr_encrypt,
782
+ .decrypt = mtk_aes_ctr_decrypt,
783
+ .init = mtk_aes_ctr_init_tfm,
784
+},
785
+{
786
+ .base.cra_name = "ofb(aes)",
787
+ .base.cra_driver_name = "ofb-aes-mtk",
788
+ .base.cra_priority = 400,
789
+ .base.cra_flags = CRYPTO_ALG_ASYNC,
790
+ .base.cra_blocksize = AES_BLOCK_SIZE,
791
+ .base.cra_ctxsize = sizeof(struct mtk_aes_ctx),
792
+ .base.cra_alignmask = 0xf,
793
+ .base.cra_module = THIS_MODULE,
794
+
795
+ .min_keysize = AES_MIN_KEY_SIZE,
796
+ .max_keysize = AES_MAX_KEY_SIZE,
797
+ .ivsize = AES_BLOCK_SIZE,
798
+ .setkey = mtk_aes_setkey,
799
+ .encrypt = mtk_aes_ofb_encrypt,
800
+ .decrypt = mtk_aes_ofb_decrypt,
801
+},
802
+{
803
+ .base.cra_name = "cfb(aes)",
804
+ .base.cra_driver_name = "cfb-aes-mtk",
805
+ .base.cra_priority = 400,
806
+ .base.cra_flags = CRYPTO_ALG_ASYNC,
807
+ .base.cra_blocksize = 1,
808
+ .base.cra_ctxsize = sizeof(struct mtk_aes_ctx),
809
+ .base.cra_alignmask = 0xf,
810
+ .base.cra_module = THIS_MODULE,
811
+
812
+ .min_keysize = AES_MIN_KEY_SIZE,
813
+ .max_keysize = AES_MAX_KEY_SIZE,
814
+ .ivsize = AES_BLOCK_SIZE,
815
+ .setkey = mtk_aes_setkey,
816
+ .encrypt = mtk_aes_cfb_encrypt,
817
+ .decrypt = mtk_aes_cfb_decrypt,
792818 },
793819 };
794820
....@@ -805,7 +831,7 @@
805831 static int mtk_aes_gcm_tag_verify(struct mtk_cryp *cryp,
806832 struct mtk_aes_rec *aes)
807833 {
808
- u32 status = cryp->ring[aes->id]->res_prev->ct;
834
+ __le32 status = cryp->ring[aes->id]->res_prev->ct;
809835
810836 return mtk_aes_complete(cryp, aes, (status & AES_AUTH_TAG_ERR) ?
811837 -EBADMSG : 0);
....@@ -823,7 +849,7 @@
823849 u32 ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
824850 u32 cnt = 0;
825851
826
- ctx->ct_hdr = AES_CT_CTRL_HDR | len;
852
+ ctx->ct_hdr = AES_CT_CTRL_HDR | cpu_to_le32(len);
827853
828854 info->cmd[cnt++] = AES_GCM_CMD0 | cpu_to_le32(req->assoclen);
829855 info->cmd[cnt++] = AES_GCM_CMD1 | cpu_to_le32(req->assoclen);
....@@ -846,8 +872,8 @@
846872 info->tfm[1] = AES_TFM_CTR_INIT | AES_TFM_IV_CTR_MODE | AES_TFM_3IV |
847873 AES_TFM_ENC_HASH;
848874
849
- mtk_aes_write_state_le(info->state + ctx->keylen + SIZE_IN_WORDS(
850
- AES_BLOCK_SIZE), (const u32 *)req->iv, ivsize);
875
+ memcpy(info->state + ctx->keylen + SIZE_IN_WORDS(AES_BLOCK_SIZE),
876
+ req->iv, ivsize);
851877 }
852878
853879 static int mtk_aes_gcm_dma(struct mtk_cryp *cryp, struct mtk_aes_rec *aes,
....@@ -908,14 +934,11 @@
908934 aes->resume = mtk_aes_transfer_complete;
909935 /* Compute total process length. */
910936 aes->total = len + gctx->authsize;
911
- /* Compute text length. */
912
- gctx->textlen = req->cryptlen;
913937 /* Hardware will append authenticated tag to output buffer */
914938 scatterwalk_map_and_copy(tag, req->dst, len, gctx->authsize, 1);
915939 } else {
916940 aes->resume = mtk_aes_gcm_tag_verify;
917941 aes->total = len;
918
- gctx->textlen = req->cryptlen - gctx->authsize;
919942 }
920943
921944 return mtk_aes_gcm_dma(cryp, aes, req->src, req->dst, len);
....@@ -926,6 +949,15 @@
926949 struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
927950 struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx);
928951 struct mtk_aes_reqctx *rctx = aead_request_ctx(req);
952
+ struct mtk_cryp *cryp;
953
+ bool enc = !!(mode & AES_FLAGS_ENCRYPT);
954
+
955
+ cryp = mtk_aes_find_dev(ctx);
956
+ if (!cryp)
957
+ return -ENODEV;
958
+
959
+ /* Compute text length. */
960
+ gctx->textlen = req->cryptlen - (enc ? 0 : gctx->authsize);
929961
930962 /* Empty messages are not supported yet */
931963 if (!gctx->textlen && !req->assoclen)
....@@ -933,8 +965,7 @@
933965
934966 rctx->mode = AES_FLAGS_GCM | mode;
935967
936
- return mtk_aes_handle_queue(ctx->cryp, !!(mode & AES_FLAGS_ENCRYPT),
937
- &req->base);
968
+ return mtk_aes_handle_queue(cryp, enc, &req->base);
938969 }
939970
940971 /*
....@@ -946,18 +977,13 @@
946977 u32 keylen)
947978 {
948979 struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(aead);
949
- struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx);
950
- struct crypto_skcipher *ctr = gctx->ctr;
951
- struct {
952
- u32 hash[4];
953
- u8 iv[8];
954
-
955
- struct crypto_wait wait;
956
-
957
- struct scatterlist sg[1];
958
- struct skcipher_request req;
959
- } *data;
980
+ union {
981
+ u32 x32[SIZE_IN_WORDS(AES_BLOCK_SIZE)];
982
+ u8 x8[AES_BLOCK_SIZE];
983
+ } hash = {};
984
+ struct crypto_aes_ctx aes_ctx;
960985 int err;
986
+ int i;
961987
962988 switch (keylen) {
963989 case AES_KEYSIZE_128:
....@@ -971,49 +997,27 @@
971997 break;
972998
973999 default:
974
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
9751000 return -EINVAL;
9761001 }
9771002
9781003 ctx->keylen = SIZE_IN_WORDS(keylen);
9791004
980
- /* Same as crypto_gcm_setkey() from crypto/gcm.c */
981
- crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
982
- crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
983
- CRYPTO_TFM_REQ_MASK);
984
- err = crypto_skcipher_setkey(ctr, key, keylen);
985
- crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
986
- CRYPTO_TFM_RES_MASK);
1005
+ err = aes_expandkey(&aes_ctx, key, keylen);
9871006 if (err)
9881007 return err;
9891008
990
- data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
991
- GFP_KERNEL);
992
- if (!data)
993
- return -ENOMEM;
1009
+ aes_encrypt(&aes_ctx, hash.x8, hash.x8);
1010
+ memzero_explicit(&aes_ctx, sizeof(aes_ctx));
9941011
995
- crypto_init_wait(&data->wait);
996
- sg_init_one(data->sg, &data->hash, AES_BLOCK_SIZE);
997
- skcipher_request_set_tfm(&data->req, ctr);
998
- skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
999
- CRYPTO_TFM_REQ_MAY_BACKLOG,
1000
- crypto_req_done, &data->wait);
1001
- skcipher_request_set_crypt(&data->req, data->sg, data->sg,
1002
- AES_BLOCK_SIZE, data->iv);
1012
+ memcpy(ctx->key, key, keylen);
10031013
1004
- err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
1005
- &data->wait);
1006
- if (err)
1007
- goto out;
1014
+ /* Why do we need to do this? */
1015
+ for (i = 0; i < SIZE_IN_WORDS(AES_BLOCK_SIZE); i++)
1016
+ hash.x32[i] = swab32(hash.x32[i]);
10081017
1009
- /* Write key into state buffer */
1010
- mtk_aes_write_state_le(ctx->info.state, (const u32 *)key, keylen);
1011
- /* Write key(H) into state buffer */
1012
- mtk_aes_write_state_be(ctx->info.state + ctx->keylen, data->hash,
1013
- AES_BLOCK_SIZE);
1014
-out:
1015
- kzfree(data);
1016
- return err;
1018
+ memcpy(ctx->key + ctx->keylen, &hash, AES_BLOCK_SIZE);
1019
+
1020
+ return 0;
10171021 }
10181022
10191023 static int mtk_aes_gcm_setauthsize(struct crypto_aead *aead,
....@@ -1049,31 +1053,10 @@
10491053 static int mtk_aes_gcm_init(struct crypto_aead *aead)
10501054 {
10511055 struct mtk_aes_gcm_ctx *ctx = crypto_aead_ctx(aead);
1052
- struct mtk_cryp *cryp = NULL;
1053
-
1054
- cryp = mtk_aes_find_dev(&ctx->base);
1055
- if (!cryp) {
1056
- pr_err("can't find crypto device\n");
1057
- return -ENODEV;
1058
- }
1059
-
1060
- ctx->ctr = crypto_alloc_skcipher("ctr(aes)", 0,
1061
- CRYPTO_ALG_ASYNC);
1062
- if (IS_ERR(ctx->ctr)) {
1063
- pr_err("Error allocating ctr(aes)\n");
1064
- return PTR_ERR(ctx->ctr);
1065
- }
10661056
10671057 crypto_aead_set_reqsize(aead, sizeof(struct mtk_aes_reqctx));
10681058 ctx->base.start = mtk_aes_gcm_start;
10691059 return 0;
1070
-}
1071
-
1072
-static void mtk_aes_gcm_exit(struct crypto_aead *aead)
1073
-{
1074
- struct mtk_aes_gcm_ctx *ctx = crypto_aead_ctx(aead);
1075
-
1076
- crypto_free_skcipher(ctx->ctr);
10771060 }
10781061
10791062 static struct aead_alg aes_gcm_alg = {
....@@ -1082,7 +1065,6 @@
10821065 .encrypt = mtk_aes_gcm_encrypt,
10831066 .decrypt = mtk_aes_gcm_decrypt,
10841067 .init = mtk_aes_gcm_init,
1085
- .exit = mtk_aes_gcm_exit,
10861068 .ivsize = GCM_AES_IV_SIZE,
10871069 .maxauthsize = AES_BLOCK_SIZE,
10881070
....@@ -1201,7 +1183,7 @@
12011183 crypto_unregister_aead(&aes_gcm_alg);
12021184
12031185 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
1204
- crypto_unregister_alg(&aes_algs[i]);
1186
+ crypto_unregister_skcipher(&aes_algs[i]);
12051187 }
12061188
12071189 static int mtk_aes_register_algs(void)
....@@ -1209,7 +1191,7 @@
12091191 int err, i;
12101192
12111193 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
1212
- err = crypto_register_alg(&aes_algs[i]);
1194
+ err = crypto_register_skcipher(&aes_algs[i]);
12131195 if (err)
12141196 goto err_aes_algs;
12151197 }
....@@ -1222,7 +1204,7 @@
12221204
12231205 err_aes_algs:
12241206 for (; i--; )
1225
- crypto_unregister_alg(&aes_algs[i]);
1207
+ crypto_unregister_skcipher(&aes_algs[i]);
12261208
12271209 return err;
12281210 }