Skip to content

Commit 464bc30

Browse files
committed
[ot] hw/opentitan: Add digest write-back to HMAC SHA256 calls.
Digest write-back allows the state of a partial HMAC SHA256 operation to be read from the DIGEST registers, supporting the addition of STOP/CONTINUE commands to save and restore partial states. Also extracts the `sha256_...` calls into separate `ot_hmac_...` functions to more modular functions which reduce repeated logic, and will better allow for expansion to support different key length sizes (SHA2-384/SHA2-512) in the future. Signed-off-by: Alex Jones <alex.jones@lowrisc.org>
1 parent 77322d9 commit 464bc30

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

hw/opentitan/ot_hmac.c

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,27 +282,60 @@ static void ot_hmac_report_error(OtHMACState *s, uint32_t error)
282282
ot_hmac_update_irqs(s);
283283
}
284284

285+
static void ot_hmac_writeback_digest_state(OtHMACState *s)
286+
{
287+
/* copy intermediary digest to mock HMAC operation for stop/continue behaviour. */
288+
/* TODO: add support for SHA2-384 and SHA2-512 */
289+
for (uint8_t i = 0; i < 8; i++) {
290+
STORE32H(s->ctx->state.sha256.state[i], s->regs->digest + i);
291+
}
292+
}
293+
294+
static void ot_hmac_sha_init(OtHMACState *s, bool write_back)
295+
{
296+
/* TODO: add support for SHA2-384 and SHA2-512 */
297+
sha256_init(&s->ctx->state);
298+
if (write_back) {
299+
ot_hmac_writeback_digest_state(s);
300+
}
301+
}
302+
303+
static void ot_hmac_sha_process(OtHMACState *s, const uint8_t *in, size_t inlen,
304+
bool write_back)
305+
{
306+
/* TODO: add support for SHA2-384 and SHA2-512 */
307+
sha256_process(&s->ctx->state, in, inlen);
308+
if (write_back) {
309+
ot_hmac_writeback_digest_state(s);
310+
}
311+
}
312+
313+
static void ot_hmac_sha_done(OtHMACState *s)
314+
{
315+
/* TODO: add support for SHA2-384 and SHA2-512 */
316+
sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest);
317+
}
318+
285319
static void ot_hmac_compute_digest(OtHMACState *s)
286320
{
287321
trace_ot_hmac_debug(s->ot_id, __func__);
288322

289323
/* HMAC mode, perform outer hash */
290324
if (s->regs->cfg & R_CFG_HMAC_EN_MASK) {
291-
sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest);
325+
ot_hmac_sha_done(s);
292326

293327
uint64_t opad[8u];
294328
memset(opad, 0, sizeof(opad));
295329
memcpy(opad, s->regs->key, sizeof(s->regs->key));
296330
for (unsigned i = 0; i < ARRAY_SIZE(opad); i++) {
297331
opad[i] ^= 0x5c5c5c5c5c5c5c5cull;
298332
}
299-
sha256_init(&s->ctx->state);
300-
sha256_process(&s->ctx->state, (const uint8_t *)opad, sizeof(opad));
301-
sha256_process(&s->ctx->state, (const uint8_t *)s->regs->digest,
302-
sizeof(s->regs->digest));
333+
ot_hmac_sha_init(s, false);
334+
ot_hmac_sha_process(s, (const uint8_t *)opad, sizeof(opad), false);
335+
ot_hmac_sha_process(s, (const uint8_t *)s->regs->digest,
336+
sizeof(s->regs->digest), true);
303337
}
304-
305-
sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest);
338+
ot_hmac_sha_done(s);
306339
}
307340

308341
static void ot_hmac_process_fifo(OtHMACState *s)
@@ -312,7 +345,7 @@ static void ot_hmac_process_fifo(OtHMACState *s)
312345
if (!fifo8_is_empty(&s->input_fifo)) {
313346
while (!fifo8_is_empty(&s->input_fifo)) {
314347
uint8_t value = fifo8_pop(&s->input_fifo);
315-
sha256_process(&s->ctx->state, &value, 1);
348+
ot_hmac_sha_process(s, &value, 1, false);
316349
}
317350

318351
/* assert FIFO Empty IRQ */
@@ -582,7 +615,7 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
582615

583616
ibex_irq_set(&s->clkmgr, true);
584617

585-
sha256_init(&s->ctx->state);
618+
ot_hmac_sha_init(s, true);
586619

587620
/* HMAC mode, process input padding */
588621
if (s->regs->cfg & R_CFG_HMAC_EN_MASK) {
@@ -592,8 +625,8 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
592625
for (unsigned i = 0; i < ARRAY_SIZE(ipad); i++) {
593626
ipad[i] ^= 0x3636363636363636u;
594627
}
595-
sha256_process(&s->ctx->state, (const uint8_t *)ipad,
596-
sizeof(ipad));
628+
ot_hmac_sha_process(s, (const uint8_t *)ipad, sizeof(ipad),
629+
true);
597630
}
598631
}
599632

0 commit comments

Comments
 (0)