Skip to content

Commit 2c5f13d

Browse files
committed
[ot] hw/opentitan: ot_hmac: 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 <[email protected]>
1 parent cb37589 commit 2c5f13d

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

hw/opentitan/ot_hmac.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,27 +282,62 @@ 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
288+
behaviour. */
289+
/* TODO: add support for SHA2-384 and SHA2-512 */
290+
unsigned digest_length = OT_HMAC_DIGEST_LENGTH / sizeof(uint32_t);
291+
for (unsigned i = 0; i < digest_length; i++) {
292+
STORE32H(s->ctx->state.sha256.state[i], s->regs->digest + i);
293+
}
294+
}
295+
296+
static void ot_hmac_sha_init(OtHMACState *s, bool write_back)
297+
{
298+
/* TODO: add support for SHA2-384 and SHA2-512 */
299+
sha256_init(&s->ctx->state);
300+
if (write_back) {
301+
ot_hmac_writeback_digest_state(s);
302+
}
303+
}
304+
305+
static void ot_hmac_sha_process(OtHMACState *s, const uint8_t *in, size_t inlen,
306+
bool write_back)
307+
{
308+
/* TODO: add support for SHA2-384 and SHA2-512 */
309+
sha256_process(&s->ctx->state, in, inlen);
310+
if (write_back) {
311+
ot_hmac_writeback_digest_state(s);
312+
}
313+
}
314+
315+
static void ot_hmac_sha_done(OtHMACState *s)
316+
{
317+
/* TODO: add support for SHA2-384 and SHA2-512 */
318+
sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest);
319+
}
320+
285321
static void ot_hmac_compute_digest(OtHMACState *s)
286322
{
287323
trace_ot_hmac_debug(s->ot_id, __func__);
288324

289325
/* HMAC mode, perform outer hash */
290326
if (s->regs->cfg & R_CFG_HMAC_EN_MASK) {
291-
sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest);
327+
ot_hmac_sha_done(s);
292328

293329
uint64_t opad[8u];
294330
memset(opad, 0, sizeof(opad));
295331
memcpy(opad, s->regs->key, sizeof(s->regs->key));
296332
for (unsigned i = 0; i < ARRAY_SIZE(opad); i++) {
297333
opad[i] ^= 0x5c5c5c5c5c5c5c5cull;
298334
}
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));
335+
ot_hmac_sha_init(s, false);
336+
ot_hmac_sha_process(s, (const uint8_t *)opad, sizeof(opad), false);
337+
ot_hmac_sha_process(s, (const uint8_t *)s->regs->digest,
338+
sizeof(s->regs->digest), true);
303339
}
304-
305-
sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest);
340+
ot_hmac_sha_done(s);
306341
}
307342

308343
static void ot_hmac_process_fifo(OtHMACState *s)
@@ -312,7 +347,7 @@ static void ot_hmac_process_fifo(OtHMACState *s)
312347
if (!fifo8_is_empty(&s->input_fifo)) {
313348
while (!fifo8_is_empty(&s->input_fifo)) {
314349
uint8_t value = fifo8_pop(&s->input_fifo);
315-
sha256_process(&s->ctx->state, &value, 1);
350+
ot_hmac_sha_process(s, &value, 1u, false);
316351
}
317352

318353
/* assert FIFO Empty IRQ */
@@ -582,7 +617,7 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
582617

583618
ibex_irq_set(&s->clkmgr, true);
584619

585-
sha256_init(&s->ctx->state);
620+
ot_hmac_sha_init(s, true);
586621

587622
/* HMAC mode, process input padding */
588623
if (s->regs->cfg & R_CFG_HMAC_EN_MASK) {
@@ -592,8 +627,8 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
592627
for (unsigned i = 0; i < ARRAY_SIZE(ipad); i++) {
593628
ipad[i] ^= 0x3636363636363636u;
594629
}
595-
sha256_process(&s->ctx->state, (const uint8_t *)ipad,
596-
sizeof(ipad));
630+
ot_hmac_sha_process(s, (const uint8_t *)ipad, sizeof(ipad),
631+
true);
597632
}
598633
}
599634

0 commit comments

Comments
 (0)