Skip to content

Commit 5524365

Browse files
committed
[ot] hw/opentitan: ot_aes: compute next round CTR/IV value.
OT HW assumes the IV register in CTR mode exposes the IV of the next iteration once a round has been completed, while libtomcrypt only updates the CTR value when a new round is triggered. Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
1 parent 9185a7a commit 5524365

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

hw/opentitan/ot_aes.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,28 @@ static void ot_aes_finalize(OtAESState *s, enum OtAESMode mode)
754754
c->do_full = false;
755755
}
756756

757+
static void ot_aes_compute_ctr_iv(OtAESState *s, uint8_t *iv)
758+
{
759+
OtAESContext *c = s->ctx;
760+
uint8_t liv[OT_AES_IV_SIZE];
761+
762+
unsigned long length = OT_AES_IV_SIZE;
763+
ctr_getiv(liv, &length, &c->ctr);
764+
765+
g_assert(c->ctr.mode == CTR_COUNTER_BIG_ENDIAN);
766+
g_assert(c->ctr.ctrlen == 0);
767+
768+
unsigned ix = OT_AES_IV_SIZE - 1u;
769+
do {
770+
liv[ix] = liv[ix] + 0x1u;
771+
if (liv[ix] != 0) {
772+
break;
773+
}
774+
} while (ix--);
775+
776+
memcpy(iv, liv, sizeof(liv));
777+
}
778+
757779
static void ot_aes_pop(OtAESState *s)
758780
{
759781
OtAESRegisters *r = s->regs;
@@ -854,7 +876,7 @@ static void ot_aes_process(OtAESState *s)
854876
memcpy(c->iv, c->ofb.IV, sizeof(c->iv));
855877
break;
856878
case AES_CTR:
857-
memcpy(c->iv, c->ctr.ctr, sizeof(c->iv));
879+
ot_aes_compute_ctr_iv(s, (uint8_t *)&c->iv[0]);
858880
break;
859881
default:
860882
break;

0 commit comments

Comments
 (0)