Skip to content
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

[ot] hw/opentitan: ot_aes: fix IV updates in CFB mode #107

Open
wants to merge 1 commit into
base: ot-earlgrey-9.2.0
Choose a base branch
from
Open
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
18 changes: 14 additions & 4 deletions hw/opentitan/ot_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* QEMU OpenTitan AES device
*
* Copyright (c) 2022-2024 Rivos, Inc.
* Copyright (c) 2025 lowRISC contributors.
*
* Author(s):
* Emmanuel Blot <[email protected]>
Expand Down Expand Up @@ -430,9 +431,11 @@ static void ot_aes_init_keyshare(OtAESState *s, bool randomize)
OtAESRegisters *r = s->regs;
OtAESContext *c = s->ctx;

trace_ot_aes_init("keyshare");
if (randomize) {
trace_ot_aes_init("keyshare init (randomize data)");
ot_aes_randomize(s, r->keyshare, ARRAY_SIZE(r->keyshare));
} else {
trace_ot_aes_init("keyshare init (data preserved)");
}
bitmap_zero(r->keyshare_bm, (int64_t)(PARAM_NUM_REGS_KEY * 2u));
c->key_ready = false;
Expand All @@ -443,9 +446,11 @@ static void ot_aes_init_iv(OtAESState *s, bool randomize)
OtAESRegisters *r = s->regs;
OtAESContext *c = s->ctx;

trace_ot_aes_init("iv");
if (randomize) {
trace_ot_aes_init("iv init (randomize data)");
ot_aes_randomize(s, r->iv, ARRAY_SIZE(r->iv));
} else {
trace_ot_aes_init("iv init (data preserved)");
}
bitmap_zero(r->iv_bm, PARAM_NUM_REGS_IV);
c->iv_ready = false;
Expand Down Expand Up @@ -873,13 +878,18 @@ static void ot_aes_process(OtAESState *s)
c->di_full = false;

if (rc == CRYPT_OK) {
/* IV registers are updated on each round */
/*
* IV registers are updated on each round. For details, see:
* https://opentitan.org/book/hw/ip/aes/doc/theory_of_operation.html
* https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
*/
switch (mode) {
case AES_CBC:
memcpy(c->iv, c->cbc.IV, sizeof(c->iv));
break;
case AES_CFB:
memcpy(c->iv, c->cfb.IV, sizeof(c->iv));
/* In CFB mode the next IV register value is the ciphertext */
memcpy(c->iv, encrypt ? c->dst : c->src, sizeof(c->iv));
break;
case AES_OFB:
memcpy(c->iv, c->ofb.IV, sizeof(c->iv));
Expand Down