Skip to content

Commit 41c7669

Browse files
toromanoSTMherbertx
authored andcommitted
crypto: stm32/cryp - fix CTR counter carry
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: bbb2832 ("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>
1 parent 029812a commit 41c7669

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

‎drivers/crypto/stm32/stm32-cryp.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ struct stm32_cryp {
163163
struct scatter_walk in_walk;
164164
struct scatter_walk out_walk;
165165

166-
u32 last_ctr[4];
166+
__be32 last_ctr[4];
167167
u32 gcm_ctr;
168168
};
169169

@@ -1217,27 +1217,26 @@ static void stm32_cryp_check_ctr_counter(struct stm32_cryp *cryp)
12171217
{
12181218
u32 cr;
12191219

1220-
if (unlikely(cryp->last_ctr[3] == 0xFFFFFFFF)) {
1221-
cryp->last_ctr[3] = 0;
1222-
cryp->last_ctr[2]++;
1223-
if (!cryp->last_ctr[2]) {
1224-
cryp->last_ctr[1]++;
1225-
if (!cryp->last_ctr[1])
1226-
cryp->last_ctr[0]++;
1227-
}
1220+
if (unlikely(cryp->last_ctr[3] == cpu_to_be32(0xFFFFFFFF))) {
1221+
/*
1222+
* In this case, we need to increment manually the ctr counter,
1223+
* as HW doesn't handle the U32 carry.
1224+
*/
1225+
crypto_inc((u8 *)cryp->last_ctr, sizeof(cryp->last_ctr));
12281226

12291227
cr = stm32_cryp_read(cryp, CRYP_CR);
12301228
stm32_cryp_write(cryp, CRYP_CR, cr & ~CR_CRYPEN);
12311229

1232-
stm32_cryp_hw_write_iv(cryp, (__be32 *)cryp->last_ctr);
1230+
stm32_cryp_hw_write_iv(cryp, cryp->last_ctr);
12331231

12341232
stm32_cryp_write(cryp, CRYP_CR, cr);
12351233
}
12361234

1237-
cryp->last_ctr[0] = stm32_cryp_read(cryp, CRYP_IV0LR);
1238-
cryp->last_ctr[1] = stm32_cryp_read(cryp, CRYP_IV0RR);
1239-
cryp->last_ctr[2] = stm32_cryp_read(cryp, CRYP_IV1LR);
1240-
cryp->last_ctr[3] = stm32_cryp_read(cryp, CRYP_IV1RR);
1235+
/* The IV registers are BE */
1236+
cryp->last_ctr[0] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV0LR));
1237+
cryp->last_ctr[1] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV0RR));
1238+
cryp->last_ctr[2] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV1LR));
1239+
cryp->last_ctr[3] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV1RR));
12411240
}
12421241

12431242
static bool stm32_cryp_irq_read_data(struct stm32_cryp *cryp)

0 commit comments

Comments
 (0)