crypto: stm32/cryp - fix CTR counter carry

[ Upstream commit 41c76690b0990efacd15d35cfb4e77318cd80ebb ]

STM32 CRYP hardware doesn't manage CTR counter bigger than max U32, as
a workaround, at each block the current IV is saved, if the saved IV
lower u32 is 0xFFFFFFFF, the full IV is manually incremented, and set
in hardware.
Fixes: bbb2832620 ("crypto: stm32 - Fix sparse warnings")

Signed-off-by: Nicolas Toromanoff <nicolas.toromanoff@foss.st.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Nicolas Toromanoff
2021-11-30 08:54:55 +01:00
committed by Greg Kroah-Hartman
parent c40b1bc851
commit e9e0dd5da8

View File

@@ -163,7 +163,7 @@ struct stm32_cryp {
struct scatter_walk in_walk; struct scatter_walk in_walk;
struct scatter_walk out_walk; struct scatter_walk out_walk;
u32 last_ctr[4]; __be32 last_ctr[4];
u32 gcm_ctr; u32 gcm_ctr;
}; };
@@ -1217,27 +1217,26 @@ static void stm32_cryp_check_ctr_counter(struct stm32_cryp *cryp)
{ {
u32 cr; u32 cr;
if (unlikely(cryp->last_ctr[3] == 0xFFFFFFFF)) { if (unlikely(cryp->last_ctr[3] == cpu_to_be32(0xFFFFFFFF))) {
cryp->last_ctr[3] = 0; /*
cryp->last_ctr[2]++; * In this case, we need to increment manually the ctr counter,
if (!cryp->last_ctr[2]) { * as HW doesn't handle the U32 carry.
cryp->last_ctr[1]++; */
if (!cryp->last_ctr[1]) crypto_inc((u8 *)cryp->last_ctr, sizeof(cryp->last_ctr));
cryp->last_ctr[0]++;
}
cr = stm32_cryp_read(cryp, CRYP_CR); cr = stm32_cryp_read(cryp, CRYP_CR);
stm32_cryp_write(cryp, CRYP_CR, cr & ~CR_CRYPEN); stm32_cryp_write(cryp, CRYP_CR, cr & ~CR_CRYPEN);
stm32_cryp_hw_write_iv(cryp, (__be32 *)cryp->last_ctr); stm32_cryp_hw_write_iv(cryp, cryp->last_ctr);
stm32_cryp_write(cryp, CRYP_CR, cr); stm32_cryp_write(cryp, CRYP_CR, cr);
} }
cryp->last_ctr[0] = stm32_cryp_read(cryp, CRYP_IV0LR); /* The IV registers are BE */
cryp->last_ctr[1] = stm32_cryp_read(cryp, CRYP_IV0RR); cryp->last_ctr[0] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV0LR));
cryp->last_ctr[2] = stm32_cryp_read(cryp, CRYP_IV1LR); cryp->last_ctr[1] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV0RR));
cryp->last_ctr[3] = stm32_cryp_read(cryp, CRYP_IV1RR); cryp->last_ctr[2] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV1LR));
cryp->last_ctr[3] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV1RR));
} }
static bool stm32_cryp_irq_read_data(struct stm32_cryp *cryp) static bool stm32_cryp_irq_read_data(struct stm32_cryp *cryp)