Skip to content

ot_aes: fix IV and CTR issues (cherry-pick #98) #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions hw/opentitan/ot_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ REG32(STATUS, 0x84u)

#define OT_AES_DATA_SIZE (PARAM_NUM_REGS_DATA * sizeof(uint32_t))
#define OT_AES_KEY_SIZE (PARAM_NUM_REGS_KEY * sizeof(uint32_t))
#define OT_AES_IV_SIZE (PARAM_NUM_REGS_KEY * sizeof(uint32_t))
#define OT_AES_IV_SIZE (PARAM_NUM_REGS_IV * sizeof(uint32_t))

/* arbitrary value long enough to give back execution to vCPU */
#define OT_AES_RETARD_DELAY_NS 10000u /* 10 us */
Expand Down Expand Up @@ -754,6 +754,28 @@ static void ot_aes_finalize(OtAESState *s, enum OtAESMode mode)
c->do_full = false;
}

static void ot_aes_compute_ctr_iv(OtAESState *s, uint8_t *iv)
{
OtAESContext *c = s->ctx;
uint8_t liv[OT_AES_IV_SIZE];

unsigned long length = OT_AES_IV_SIZE;
ctr_getiv(liv, &length, &c->ctr);

g_assert(c->ctr.mode == CTR_COUNTER_BIG_ENDIAN);
g_assert(c->ctr.ctrlen == 0);

unsigned ix = OT_AES_IV_SIZE - 1u;
do {
liv[ix] = liv[ix] + 0x1u;
if (liv[ix] != 0) {
break;
}
} while (ix--);

memcpy(iv, liv, sizeof(liv));
}

static void ot_aes_pop(OtAESState *s)
{
OtAESRegisters *r = s->regs;
Expand Down Expand Up @@ -854,7 +876,7 @@ static void ot_aes_process(OtAESState *s)
memcpy(c->iv, c->ofb.IV, sizeof(c->iv));
break;
case AES_CTR:
memcpy(c->iv, c->ctr.ctr, sizeof(c->iv));
ot_aes_compute_ctr_iv(s, (uint8_t *)&c->iv[0]);
break;
default:
break;
Expand Down Expand Up @@ -1009,7 +1031,7 @@ static uint64_t ot_aes_read(void *opaque, hwaddr addr, unsigned size)
case R_IV_1:
case R_IV_2:
case R_IV_3:
val32 = r->keyshare[reg - R_IV_0];
val32 = r->iv[reg - R_IV_0];
break;
case R_DATA_OUT_0:
case R_DATA_OUT_1:
Expand Down
Loading