Skip to content

Commit 98b1023

Browse files
ryderlee1110herbertx
authored andcommitted
crypto: mediatek - add mtk_aes_gcm_tag_verify()
This patch adds mtk_aes_gcm_tag_verify() which is used to compare authenticated tag. Signed-off-by: Ryder Lee <ryder.lee@mediatek.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 5041d71 commit 98b1023

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎drivers/crypto/mediatek/mtk-aes.c‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
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 */
803821
static 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

931951
static void mtk_gcm_setkey_done(struct crypto_async_request *req, int err)

‎drivers/crypto/mediatek/mtk-platform.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct mtk_desc {
8888
* @cmd_dma: DMA address of command descriptor ring
8989
* @res_base: pointer to result descriptor ring base
9090
* @res_next: pointer to the next result descriptor
91+
* @res_prev: pointer to the previous result descriptor
9192
* @res_dma: DMA address of result descriptor ring
9293
*
9394
* A descriptor ring is a circular buffer that is used to manage
@@ -100,6 +101,7 @@ struct mtk_ring {
100101
dma_addr_t cmd_dma;
101102
struct mtk_desc *res_base;
102103
struct mtk_desc *res_next;
104+
struct mtk_desc *res_prev;
103105
dma_addr_t res_dma;
104106
};
105107

0 commit comments

Comments
 (0)