7070#define AES_FLAGS_ENCRYPT BIT(4)
7171#define AES_FLAGS_BUSY BIT(5)
7272
73+ #define AES_AUTH_TAG_ERR cpu_to_le32(BIT(26))
74+
7375/**
7476 * Command token(CT) is a set of hardware instructions that
7577 * are used to control engine's processing flow of AES.
@@ -306,6 +308,9 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
306308 }
307309 res -> hdr |= MTK_DESC_LAST ;
308310
311+ /* Pointer to current result descriptor */
312+ ring -> res_prev = res ;
313+
309314 /* Prepare enough space for authenticated tag */
310315 if (aes -> flags & AES_FLAGS_GCM )
311316 res -> hdr += AES_BLOCK_SIZE ;
@@ -799,6 +804,19 @@ mtk_aes_gcm_ctx_cast(struct mtk_aes_base_ctx *ctx)
799804 return container_of (ctx , struct mtk_aes_gcm_ctx , base );
800805}
801806
807+ /*
808+ * Engine will verify and compare tag automatically, so we just need
809+ * to check returned status which stored in the result descriptor.
810+ */
811+ static int mtk_aes_gcm_tag_verify (struct mtk_cryp * cryp ,
812+ struct mtk_aes_rec * aes )
813+ {
814+ u32 status = cryp -> ring [aes -> id ]-> res_prev -> ct ;
815+
816+ return mtk_aes_complete (cryp , aes , (status & AES_AUTH_TAG_ERR ) ?
817+ - EBADMSG : 0 );
818+ }
819+
802820/* Initialize transform information of GCM mode */
803821static void mtk_aes_gcm_info_init (struct mtk_cryp * cryp ,
804822 struct mtk_aes_rec * aes ,
@@ -902,17 +920,19 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
902920
903921 if (aes -> flags & AES_FLAGS_ENCRYPT ) {
904922 u32 tag [4 ];
923+
924+ aes -> resume = mtk_aes_transfer_complete ;
905925 /* Compute total process length. */
906926 aes -> total = len + gctx -> authsize ;
907927 /* Compute text length. */
908928 gctx -> textlen = req -> cryptlen ;
909929 /* Hardware will append authenticated tag to output buffer */
910930 scatterwalk_map_and_copy (tag , req -> dst , len , gctx -> authsize , 1 );
911931 } else {
932+ aes -> resume = mtk_aes_gcm_tag_verify ;
912933 aes -> total = len ;
913934 gctx -> textlen = req -> cryptlen - gctx -> authsize ;
914935 }
915- aes -> resume = mtk_aes_transfer_complete ;
916936
917937 return mtk_aes_gcm_dma (cryp , aes , req -> src , req -> dst , len );
918938}
@@ -925,7 +945,7 @@ static int mtk_aes_gcm_crypt(struct aead_request *req, u64 mode)
925945 rctx -> mode = AES_FLAGS_GCM | mode ;
926946
927947 return mtk_aes_handle_queue (ctx -> cryp , !!(mode & AES_FLAGS_ENCRYPT ),
928- & req -> base );
948+ & req -> base );
929949}
930950
931951static void mtk_gcm_setkey_done (struct crypto_async_request * req , int err )
0 commit comments