2323/* AES command token size */
2424#define AES_CT_SIZE_ECB 2
2525#define AES_CT_SIZE_CBC 3
26+ #define AES_CT_SIZE_CTR 3
2627#define AES_CT_CTRL_HDR cpu_to_le32(0x00220000)
27- /* AES-CBC/ECB command token */
28+
29+ /* AES-CBC/ECB/CTR command token */
2830#define AES_CMD0 cpu_to_le32(0x05000000)
2931#define AES_CMD1 cpu_to_le32(0x2d060000)
3032#define AES_CMD2 cpu_to_le32(0xe4a63806)
3941/* AES transform information word 1 fields */
4042#define AES_TFM_ECB cpu_to_le32(0x0 << 0)
4143#define AES_TFM_CBC cpu_to_le32(0x1 << 0)
42- #define AES_TFM_FULL_IV cpu_to_le32(0xf << 5)
44+ #define AES_TFM_CTR_LOAD cpu_to_le32(0x6 << 0) /* load/reuse counter */
45+ #define AES_TFM_FULL_IV cpu_to_le32(0xf << 5) /* using IV 0-3 */
4346
4447/* AES flags */
4548#define AES_FLAGS_ECB BIT(0)
4649#define AES_FLAGS_CBC BIT(1)
47- #define AES_FLAGS_ENCRYPT BIT(2)
48- #define AES_FLAGS_BUSY BIT(3)
50+ #define AES_FLAGS_CTR BIT(2)
51+ #define AES_FLAGS_ENCRYPT BIT(3)
52+ #define AES_FLAGS_BUSY BIT(4)
4953
5054/**
5155 * Command token(CT) is a set of hardware instructions that
@@ -90,6 +94,15 @@ struct mtk_aes_ctx {
9094 struct mtk_aes_base_ctx base ;
9195};
9296
97+ struct mtk_aes_ctr_ctx {
98+ struct mtk_aes_base_ctx base ;
99+
100+ u32 iv [AES_BLOCK_SIZE / sizeof (u32 )];
101+ size_t offset ;
102+ struct scatterlist src [2 ];
103+ struct scatterlist dst [2 ];
104+ };
105+
93106struct mtk_aes_drv {
94107 struct list_head dev_list ;
95108 /* Device list lock */
@@ -332,7 +345,7 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
332345 return - EINVAL ;
333346}
334347
335- /* Initialize transform information of CBC/ECB mode */
348+ /* Initialize transform information of CBC/ECB/CTR mode */
336349static void mtk_aes_info_init (struct mtk_cryp * cryp , struct mtk_aes_rec * aes ,
337350 size_t len )
338351{
@@ -374,6 +387,13 @@ static void mtk_aes_info_init(struct mtk_cryp *cryp, struct mtk_aes_rec *aes,
374387 ctx -> tfm .ctrl [1 ] = AES_TFM_ECB ;
375388
376389 ctx -> ct_size = AES_CT_SIZE_ECB ;
390+ } else if (aes -> flags & AES_FLAGS_CTR ) {
391+ ctx -> tfm .ctrl [0 ] |= AES_TFM_SIZE (ctx -> keylen +
392+ SIZE_IN_WORDS (AES_BLOCK_SIZE ));
393+ ctx -> tfm .ctrl [1 ] = AES_TFM_CTR_LOAD | AES_TFM_FULL_IV ;
394+
395+ ctx -> ct .cmd [2 ] = AES_CMD2 ;
396+ ctx -> ct_size = AES_CT_SIZE_CTR ;
377397 }
378398}
379399
@@ -479,6 +499,80 @@ static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
479499 return mtk_aes_dma (cryp , aes , req -> src , req -> dst , req -> nbytes );
480500}
481501
502+ static inline struct mtk_aes_ctr_ctx *
503+ mtk_aes_ctr_ctx_cast (struct mtk_aes_base_ctx * ctx )
504+ {
505+ return container_of (ctx , struct mtk_aes_ctr_ctx , base );
506+ }
507+
508+ static int mtk_aes_ctr_transfer (struct mtk_cryp * cryp , struct mtk_aes_rec * aes )
509+ {
510+ struct mtk_aes_base_ctx * ctx = aes -> ctx ;
511+ struct mtk_aes_ctr_ctx * cctx = mtk_aes_ctr_ctx_cast (ctx );
512+ struct ablkcipher_request * req = ablkcipher_request_cast (aes -> areq );
513+ struct scatterlist * src , * dst ;
514+ int i ;
515+ u32 start , end , ctr , blocks , * iv_state ;
516+ size_t datalen ;
517+ bool fragmented = false;
518+
519+ /* Check for transfer completion. */
520+ cctx -> offset += aes -> total ;
521+ if (cctx -> offset >= req -> nbytes )
522+ return mtk_aes_complete (cryp , aes );
523+
524+ /* Compute data length. */
525+ datalen = req -> nbytes - cctx -> offset ;
526+ blocks = DIV_ROUND_UP (datalen , AES_BLOCK_SIZE );
527+ ctr = be32_to_cpu (cctx -> iv [3 ]);
528+
529+ /* Check 32bit counter overflow. */
530+ start = ctr ;
531+ end = start + blocks - 1 ;
532+ if (end < start ) {
533+ ctr |= 0xffffffff ;
534+ datalen = AES_BLOCK_SIZE * - start ;
535+ fragmented = true;
536+ }
537+
538+ /* Jump to offset. */
539+ src = scatterwalk_ffwd (cctx -> src , req -> src , cctx -> offset );
540+ dst = ((req -> src == req -> dst ) ? src :
541+ scatterwalk_ffwd (cctx -> dst , req -> dst , cctx -> offset ));
542+
543+ /* Write IVs into transform state buffer. */
544+ iv_state = ctx -> tfm .state + ctx -> keylen ;
545+ for (i = 0 ; i < SIZE_IN_WORDS (AES_BLOCK_SIZE ); i ++ )
546+ iv_state [i ] = cpu_to_le32 (cctx -> iv [i ]);
547+
548+ if (unlikely (fragmented )) {
549+ /*
550+ * Increment the counter manually to cope with the hardware
551+ * counter overflow.
552+ */
553+ cctx -> iv [3 ] = cpu_to_be32 (ctr );
554+ crypto_inc ((u8 * )cctx -> iv , AES_BLOCK_SIZE );
555+ }
556+ aes -> resume = mtk_aes_ctr_transfer ;
557+
558+ return mtk_aes_dma (cryp , aes , src , dst , datalen );
559+ }
560+
561+ static int mtk_aes_ctr_start (struct mtk_cryp * cryp , struct mtk_aes_rec * aes )
562+ {
563+ struct mtk_aes_ctr_ctx * cctx = mtk_aes_ctr_ctx_cast (aes -> ctx );
564+ struct ablkcipher_request * req = ablkcipher_request_cast (aes -> areq );
565+ struct mtk_aes_reqctx * rctx = ablkcipher_request_ctx (req );
566+
567+ mtk_aes_set_mode (aes , rctx );
568+
569+ memcpy (cctx -> iv , req -> info , AES_BLOCK_SIZE );
570+ cctx -> offset = 0 ;
571+ aes -> total = 0 ;
572+
573+ return mtk_aes_ctr_transfer (cryp , aes );
574+ }
575+
482576/* Check and set the AES key to transform state buffer */
483577static int mtk_aes_setkey (struct crypto_ablkcipher * tfm ,
484578 const u8 * key , u32 keylen )
@@ -536,6 +630,16 @@ static int mtk_aes_cbc_decrypt(struct ablkcipher_request *req)
536630 return mtk_aes_crypt (req , AES_FLAGS_CBC );
537631}
538632
633+ static int mtk_aes_ctr_encrypt (struct ablkcipher_request * req )
634+ {
635+ return mtk_aes_crypt (req , AES_FLAGS_ENCRYPT | AES_FLAGS_CTR );
636+ }
637+
638+ static int mtk_aes_ctr_decrypt (struct ablkcipher_request * req )
639+ {
640+ return mtk_aes_crypt (req , AES_FLAGS_CTR );
641+ }
642+
539643static int mtk_aes_cra_init (struct crypto_tfm * tfm )
540644{
541645 struct mtk_aes_ctx * ctx = crypto_tfm_ctx (tfm );
@@ -552,6 +656,22 @@ static int mtk_aes_cra_init(struct crypto_tfm *tfm)
552656 return 0 ;
553657}
554658
659+ static int mtk_aes_ctr_cra_init (struct crypto_tfm * tfm )
660+ {
661+ struct mtk_aes_ctx * ctx = crypto_tfm_ctx (tfm );
662+ struct mtk_cryp * cryp = NULL ;
663+
664+ cryp = mtk_aes_find_dev (& ctx -> base );
665+ if (!cryp ) {
666+ pr_err ("can't find crypto device\n" );
667+ return - ENODEV ;
668+ }
669+
670+ tfm -> crt_ablkcipher .reqsize = sizeof (struct mtk_aes_reqctx );
671+ ctx -> base .start = mtk_aes_ctr_start ;
672+ return 0 ;
673+ }
674+
555675static struct crypto_alg aes_algs [] = {
556676{
557677 .cra_name = "cbc(aes)" ,
@@ -594,6 +714,27 @@ static struct crypto_alg aes_algs[] = {
594714 .decrypt = mtk_aes_ecb_decrypt ,
595715 }
596716},
717+ {
718+ .cra_name = "ctr(aes)" ,
719+ .cra_driver_name = "ctr-aes-mtk" ,
720+ .cra_priority = 400 ,
721+ .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
722+ CRYPTO_ALG_ASYNC ,
723+ .cra_init = mtk_aes_ctr_cra_init ,
724+ .cra_blocksize = 1 ,
725+ .cra_ctxsize = sizeof (struct mtk_aes_ctr_ctx ),
726+ .cra_alignmask = 0xf ,
727+ .cra_type = & crypto_ablkcipher_type ,
728+ .cra_module = THIS_MODULE ,
729+ .cra_u .ablkcipher = {
730+ .min_keysize = AES_MIN_KEY_SIZE ,
731+ .max_keysize = AES_MAX_KEY_SIZE ,
732+ .ivsize = AES_BLOCK_SIZE ,
733+ .setkey = mtk_aes_setkey ,
734+ .encrypt = mtk_aes_ctr_encrypt ,
735+ .decrypt = mtk_aes_ctr_decrypt ,
736+ }
737+ },
597738};
598739
599740static void mtk_aes_enc_task (unsigned long data )
0 commit comments