Software APIs
hmac.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/lib/crypto/drivers/hmac.h"
6 
11 #include "sw/device/lib/crypto/impl/status.h"
12 
13 #include "hmac_regs.h" // Generated.
15 
16 // Module ID for status codes.
17 #define MODULE_ID MAKE_MODULE_ID('d', 'h', 'm')
18 
19 OT_ASSERT_ENUM_VALUE(HMAC_KEY_1_REG_OFFSET, HMAC_KEY_0_REG_OFFSET + 4);
20 OT_ASSERT_ENUM_VALUE(HMAC_KEY_2_REG_OFFSET, HMAC_KEY_1_REG_OFFSET + 4);
21 OT_ASSERT_ENUM_VALUE(HMAC_KEY_3_REG_OFFSET, HMAC_KEY_2_REG_OFFSET + 4);
22 OT_ASSERT_ENUM_VALUE(HMAC_KEY_4_REG_OFFSET, HMAC_KEY_3_REG_OFFSET + 4);
23 OT_ASSERT_ENUM_VALUE(HMAC_KEY_5_REG_OFFSET, HMAC_KEY_4_REG_OFFSET + 4);
24 OT_ASSERT_ENUM_VALUE(HMAC_KEY_6_REG_OFFSET, HMAC_KEY_5_REG_OFFSET + 4);
25 OT_ASSERT_ENUM_VALUE(HMAC_KEY_7_REG_OFFSET, HMAC_KEY_6_REG_OFFSET + 4);
26 OT_ASSERT_ENUM_VALUE(HMAC_KEY_8_REG_OFFSET, HMAC_KEY_7_REG_OFFSET + 4);
27 OT_ASSERT_ENUM_VALUE(HMAC_KEY_9_REG_OFFSET, HMAC_KEY_8_REG_OFFSET + 4);
28 OT_ASSERT_ENUM_VALUE(HMAC_KEY_10_REG_OFFSET, HMAC_KEY_9_REG_OFFSET + 4);
29 OT_ASSERT_ENUM_VALUE(HMAC_KEY_11_REG_OFFSET, HMAC_KEY_10_REG_OFFSET + 4);
30 OT_ASSERT_ENUM_VALUE(HMAC_KEY_12_REG_OFFSET, HMAC_KEY_11_REG_OFFSET + 4);
31 OT_ASSERT_ENUM_VALUE(HMAC_KEY_13_REG_OFFSET, HMAC_KEY_12_REG_OFFSET + 4);
32 OT_ASSERT_ENUM_VALUE(HMAC_KEY_14_REG_OFFSET, HMAC_KEY_13_REG_OFFSET + 4);
33 OT_ASSERT_ENUM_VALUE(HMAC_KEY_15_REG_OFFSET, HMAC_KEY_14_REG_OFFSET + 4);
34 OT_ASSERT_ENUM_VALUE(HMAC_KEY_16_REG_OFFSET, HMAC_KEY_15_REG_OFFSET + 4);
35 OT_ASSERT_ENUM_VALUE(HMAC_KEY_17_REG_OFFSET, HMAC_KEY_16_REG_OFFSET + 4);
36 OT_ASSERT_ENUM_VALUE(HMAC_KEY_18_REG_OFFSET, HMAC_KEY_17_REG_OFFSET + 4);
37 OT_ASSERT_ENUM_VALUE(HMAC_KEY_19_REG_OFFSET, HMAC_KEY_18_REG_OFFSET + 4);
38 OT_ASSERT_ENUM_VALUE(HMAC_KEY_20_REG_OFFSET, HMAC_KEY_19_REG_OFFSET + 4);
39 OT_ASSERT_ENUM_VALUE(HMAC_KEY_21_REG_OFFSET, HMAC_KEY_20_REG_OFFSET + 4);
40 OT_ASSERT_ENUM_VALUE(HMAC_KEY_22_REG_OFFSET, HMAC_KEY_21_REG_OFFSET + 4);
41 OT_ASSERT_ENUM_VALUE(HMAC_KEY_23_REG_OFFSET, HMAC_KEY_22_REG_OFFSET + 4);
42 OT_ASSERT_ENUM_VALUE(HMAC_KEY_24_REG_OFFSET, HMAC_KEY_23_REG_OFFSET + 4);
43 OT_ASSERT_ENUM_VALUE(HMAC_KEY_25_REG_OFFSET, HMAC_KEY_24_REG_OFFSET + 4);
44 OT_ASSERT_ENUM_VALUE(HMAC_KEY_26_REG_OFFSET, HMAC_KEY_25_REG_OFFSET + 4);
45 OT_ASSERT_ENUM_VALUE(HMAC_KEY_27_REG_OFFSET, HMAC_KEY_26_REG_OFFSET + 4);
46 OT_ASSERT_ENUM_VALUE(HMAC_KEY_28_REG_OFFSET, HMAC_KEY_27_REG_OFFSET + 4);
47 OT_ASSERT_ENUM_VALUE(HMAC_KEY_29_REG_OFFSET, HMAC_KEY_28_REG_OFFSET + 4);
48 OT_ASSERT_ENUM_VALUE(HMAC_KEY_30_REG_OFFSET, HMAC_KEY_29_REG_OFFSET + 4);
49 OT_ASSERT_ENUM_VALUE(HMAC_KEY_31_REG_OFFSET, HMAC_KEY_30_REG_OFFSET + 4);
50 
51 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_1_REG_OFFSET, HMAC_DIGEST_0_REG_OFFSET + 4);
52 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_2_REG_OFFSET, HMAC_DIGEST_1_REG_OFFSET + 4);
53 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_3_REG_OFFSET, HMAC_DIGEST_2_REG_OFFSET + 4);
54 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_4_REG_OFFSET, HMAC_DIGEST_3_REG_OFFSET + 4);
55 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_5_REG_OFFSET, HMAC_DIGEST_4_REG_OFFSET + 4);
56 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_6_REG_OFFSET, HMAC_DIGEST_5_REG_OFFSET + 4);
57 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_7_REG_OFFSET, HMAC_DIGEST_6_REG_OFFSET + 4);
58 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_8_REG_OFFSET, HMAC_DIGEST_7_REG_OFFSET + 4);
59 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_9_REG_OFFSET, HMAC_DIGEST_8_REG_OFFSET + 4);
60 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_10_REG_OFFSET, HMAC_DIGEST_9_REG_OFFSET + 4);
61 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_11_REG_OFFSET, HMAC_DIGEST_10_REG_OFFSET + 4);
62 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_12_REG_OFFSET, HMAC_DIGEST_11_REG_OFFSET + 4);
63 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_13_REG_OFFSET, HMAC_DIGEST_12_REG_OFFSET + 4);
64 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_14_REG_OFFSET, HMAC_DIGEST_13_REG_OFFSET + 4);
65 OT_ASSERT_ENUM_VALUE(HMAC_DIGEST_15_REG_OFFSET, HMAC_DIGEST_14_REG_OFFSET + 4);
66 
67 enum {
68  /* The beginning of the address space of HMAC. */
69  kHmacBaseAddr = TOP_EARLGREY_HMAC_BASE_ADDR,
70  /* Timeout value for the polling
71  * As min 3 clock cycles are required to perform a read access to the STATUS
72  * hmac_idle register. And as we should observe 240 cycles (80 for the inner
73  * key, 80 for the outer key, and 80 for the result of the first round), plus
74  * 80 for msg itself -> 360 cycles in total as max (when HMAC is enabled).
75  * Which means, 360/3=120 loops before having the IDLE state.
76  * Let's take a large margin and consider that 200 loops are enough.
77  */
78  kNumIterTimeout = 200,
79 };
80 
81 /**
82  * Wait until HMAC becomes idle.
83  *
84  * It returns error if HMAC HWIP becomes idle without firing `hmac_done`
85  * interrupt.
86  *
87  * @return Result of the operation.
88  */
90 static status_t hmac_idle_wait(void) {
91  // Verify that HMAC HWIP is idle.
92  // Initialize `status_reg = 0` so that the loop starts with the assumption
93  // that HMAC HWIP is not idle.
94  uint32_t status_reg = 0;
95  uint32_t attempt_cnt = 0;
96  while (bitfield_bit32_read(status_reg, HMAC_STATUS_HMAC_IDLE_BIT) == 0) {
97  status_reg = abs_mmio_read32(kHmacBaseAddr + HMAC_STATUS_REG_OFFSET);
98  attempt_cnt++;
99  if (attempt_cnt >= kNumIterTimeout) {
100  return OTCRYPTO_FATAL_ERR;
101  }
102  }
103 
104  // Verify that HMAC HWIP raises `hmac_done` bit.
105  uint32_t intr_reg =
106  abs_mmio_read32(kHmacBaseAddr + HMAC_INTR_STATE_REG_OFFSET);
107  if (bitfield_bit32_read(intr_reg, HMAC_INTR_STATE_HMAC_DONE_BIT) == 0) {
108  return OTCRYPTO_FATAL_ERR;
109  }
110 
111  // Clear the interrupt by writing 1, because `INTR_STATE` is rw1c type.
112  abs_mmio_write32(kHmacBaseAddr + HMAC_INTR_STATE_REG_OFFSET, intr_reg);
113  return OTCRYPTO_OK;
114 }
115 
116 /**
117  * Clear the state of HMAC HWIP so that further driver calls can use it.
118  *
119  * This function cannot force stop HMAC HWIP, and ongoing operations will not
120  * simply stop by deasserting `sha_en` bit. Instead it should be used after
121  * HMAC HWIP indicates that it is idle (see `hmac_idle_wait`).
122  *
123  * It also clears the internal state of HMAC HWIP by overwriting sensitive
124  * values with 1s.
125  */
126 static void hmac_hwip_clear(void) {
127  // Do not clear the config yet, we just need to deassert sha_en, see #23014.
128  uint32_t cfg_reg = abs_mmio_read32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET);
129  cfg_reg = bitfield_bit32_write(cfg_reg, HMAC_CFG_SHA_EN_BIT, false);
130  abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, cfg_reg);
131 
132  // TODO(#23191): Use a random value from EDN to wipe.
133  abs_mmio_write32(kHmacBaseAddr + HMAC_WIPE_SECRET_REG_OFFSET, UINT32_MAX);
134 }
135 
136 /**
137  * Write given key to HMAC HWIP.
138  *
139  * This function does not return error, so it is the responsibility of the
140  * caller to check that `key` and `key_len` are correctly set. Moreover, the
141  * caller must ensure that HMAC HWIP is in an idle state that accepts writing
142  * key words.
143  *
144  * @param key The buffer that points to the key.
145  * @param key_wordlen The length of the key in words.
146  */
147 static void key_write(const uint32_t *key, size_t key_wordlen) {
148  for (size_t i = 0; i < key_wordlen; i++) {
149  abs_mmio_write32(kHmacBaseAddr + HMAC_KEY_0_REG_OFFSET + 4 * i, key[i]);
150  }
151 }
152 
153 /**
154  * Copy the digest result from HMAC HWIP to given `digest` buffer.
155  *
156  * This function does not return error, so it is the responsibility of the
157  * caller to check that `digest` and `digest_wordlen` are correctly set.
158  * Moreover, the caller must ensure that HMAC HWIP is in a state that permits
159  * reading the digest value, that is, either of stop or process commands is
160  * issued.
161  *
162  * @param[out] digest The digest buffer to copy to the result.
163  * @param digest_wordlen The length of the digest buffer in words.
164  */
165 static void digest_read(uint32_t *digest, size_t digest_wordlen) {
166  for (size_t i = 0; i < digest_wordlen; i++) {
167  digest[i] =
168  abs_mmio_read32(kHmacBaseAddr + HMAC_DIGEST_0_REG_OFFSET + 4 * i);
169  }
170 }
171 
172 /**
173  * Restore the internal state of HMAC HWIP from `ctx` struct, and resume the
174  * operation.
175  *
176  * The first HMAC HWIP operation requires the call of `start` instead of
177  * `continue`. Therefore, `ctx->hw_started` flag is used to distinguish
178  * the first call. This function also updated `ctx->hw_started` after
179  * the first such call.
180  *
181  * If this function is being called from `ctx` object with previously stored
182  * context (i.e. `ctx->hw_started = true`), then this state is restored.
183  *
184  * @param ctx Context from which values are written to CSRs.
185  */
186 static void context_restore(hmac_ctx_t *ctx) {
187  // Restore CFG register from `ctx->cfg_reg`.
188  abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, ctx->cfg_reg);
189 
190  // Write to KEY registers for HMAC operations. If the operation is SHA-2,
191  // `key_wordlen` is set to 0 during `ctx` initialization.
192  key_write(ctx->key, ctx->key_wordlen);
193 
194  uint32_t cmd_reg = HMAC_CMD_REG_RESVAL;
195  // Decide if we need to invoke `start` or `continue` command.
196  if (ctx->hw_started) {
197  cmd_reg = bitfield_bit32_write(cmd_reg, HMAC_CMD_HASH_CONTINUE_BIT, 1);
198 
199  // For SHA-256 and HMAC-256, we do not need to write to the second half of
200  // DIGEST registers, but we do it anyway to keep the driver simple.
201  for (size_t i = 0; i < kHmacMaxDigestWords; i++) {
202  abs_mmio_write32(kHmacBaseAddr + HMAC_DIGEST_0_REG_OFFSET + 4 * i,
203  ctx->H[i]);
204  }
205  abs_mmio_write32(kHmacBaseAddr + HMAC_MSG_LENGTH_LOWER_REG_OFFSET,
206  ctx->lower);
207  abs_mmio_write32(kHmacBaseAddr + HMAC_MSG_LENGTH_UPPER_REG_OFFSET,
208  ctx->upper);
209  } else {
210  cmd_reg = bitfield_bit32_write(cmd_reg, HMAC_CMD_HASH_START_BIT, 1);
211  ctx->hw_started = 1;
212  }
213 
214  // We could not set `sha_en` before updating context registers (see #23014).
215  // Now that context registers are restored, enable `sha_en`.
216  uint32_t cfg_reg = abs_mmio_read32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET);
217  cfg_reg = bitfield_bit32_write(cfg_reg, HMAC_CFG_SHA_EN_BIT, true);
218  abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, cfg_reg);
219 
220  // Now we can finally write the command to the register to actually issue
221  // `start` or `continue`.
222  abs_mmio_write32(kHmacBaseAddr + HMAC_CMD_REG_OFFSET, cmd_reg);
223 }
224 
225 /**
226  * Save the context from HMAC HWIP into `ctx` object.
227  *
228  * This function should be called only after `stop` command is invoked and
229  * HMAC HWIP is idle.
230  *
231  * @param[out] ctx Context to which values are written.
232  */
233 static void context_save(hmac_ctx_t *ctx) {
234  // For SHA-256 and HMAC-256, we do not need to save to the second half of
235  // DIGEST registers, but we do it anyway to keep the driver simple.
236  digest_read(ctx->H, kHmacMaxDigestWords);
237  ctx->lower =
238  abs_mmio_read32(kHmacBaseAddr + HMAC_MSG_LENGTH_LOWER_REG_OFFSET);
239  ctx->upper =
240  abs_mmio_read32(kHmacBaseAddr + HMAC_MSG_LENGTH_UPPER_REG_OFFSET);
241 }
242 
243 /**
244  * Write given byte array into the `MSG_FIFO`. This function should only be
245  * called when HMAC HWIP is already running and expecting further message bytes.
246  *
247  * @param message The incoming message buffer to be fed into HMAC_FIFO.
248  * @param message_len The length of `message` in bytes.
249  */
250 static void msg_fifo_write(const uint8_t *message, size_t message_len) {
251  // TODO(#23191): Should we handle backpressure here?
252  // Begin by writing a one byte at a time until the data is aligned.
253  size_t i = 0;
254  for (; misalignment32_of((uintptr_t)(&message[i])) > 0 && i < message_len;
255  i++) {
256  abs_mmio_write8(kHmacBaseAddr + HMAC_MSG_FIFO_REG_OFFSET, message[i]);
257  }
258 
259  // Write one word at a time as long as there is a full word available.
260  for (; i + sizeof(uint32_t) <= message_len; i += sizeof(uint32_t)) {
261  uint32_t next_word = read_32(&message[i]);
262  abs_mmio_write32(kHmacBaseAddr + HMAC_MSG_FIFO_REG_OFFSET, next_word);
263  }
264 
265  // For the last few bytes, we need to write one byte at a time again.
266  for (; i < message_len; i++) {
267  abs_mmio_write8(kHmacBaseAddr + HMAC_MSG_FIFO_REG_OFFSET, message[i]);
268  }
269 }
270 
271 /**
272  * For given `hmac_mode`, derive the matching CFG value and block/digest
273  * lengths.
274  *
275  * @param hmac_mode The input `hmac_mode_t` value that determines which
276  * SHA-2/HMAC algorithm is being used.
277  * @param[out] cfg_reg The value that needs to be set to HMAC HWIP for given
278  * `hmac_mode`.
279  * @param[out] msg_block_bytelen The internal block length associated with
280  * `hmac_mode` in bytes.
281  * @param[out] digest_wordlen The digest length associated with `hmac_mode` in
282  * words.
283  */
285 static status_t cfg_derive(hmac_mode_t hmac_mode, uint32_t *cfg_reg,
286  size_t *msg_block_bytelen, size_t *digest_wordlen) {
287  *cfg_reg = HMAC_CFG_REG_RESVAL;
288  // The endianness is fixed at driver level and not exposed to the caller.
289  // Digest should be big-endian to match the SHA-256 specification.
290  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_KEY_SWAP_BIT, true);
291  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_DIGEST_SWAP_BIT, true);
292  // Message should be little-endian to match Ibex's endianness.
293  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_ENDIAN_SWAP_BIT, false);
294 
295  // We need to keep `sha_en` low until context is restored, see #23014.
296  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_SHA_EN_BIT, false);
297 
298  // Default value for `hmac_en` is false, HMAC calls set it to true below.
299  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_HMAC_EN_BIT, false);
300 
301  switch (hmac_mode) {
302  case kHmacModeHmac256:
303  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_HMAC_EN_BIT, true);
304  *cfg_reg = bitfield_field32_write(*cfg_reg, HMAC_CFG_KEY_LENGTH_FIELD,
305  HMAC_CFG_KEY_LENGTH_VALUE_KEY_512);
307  case kHmacModeSha256:
308  *cfg_reg = bitfield_field32_write(*cfg_reg, HMAC_CFG_DIGEST_SIZE_FIELD,
309  HMAC_CFG_DIGEST_SIZE_VALUE_SHA2_256);
310  *msg_block_bytelen = kHmacSha256BlockBytes;
311  *digest_wordlen = kHmacSha256DigestWords;
312  break;
313  case kHmacModeHmac384:
314  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_HMAC_EN_BIT, true);
315  *cfg_reg = bitfield_field32_write(*cfg_reg, HMAC_CFG_KEY_LENGTH_FIELD,
316  HMAC_CFG_KEY_LENGTH_VALUE_KEY_1024);
318  case kHmacModeSha384:
319  *cfg_reg = bitfield_field32_write(*cfg_reg, HMAC_CFG_DIGEST_SIZE_FIELD,
320  HMAC_CFG_DIGEST_SIZE_VALUE_SHA2_384);
321  *msg_block_bytelen = kHmacSha512BlockBytes;
322  *digest_wordlen = kHmacSha384DigestWords;
323  break;
324  case kHmacModeHmac512:
325  *cfg_reg = bitfield_bit32_write(*cfg_reg, HMAC_CFG_HMAC_EN_BIT, true);
326  *cfg_reg = bitfield_field32_write(*cfg_reg, HMAC_CFG_KEY_LENGTH_FIELD,
327  HMAC_CFG_KEY_LENGTH_VALUE_KEY_1024);
329  case kHmacModeSha512:
330  *cfg_reg = bitfield_field32_write(*cfg_reg, HMAC_CFG_DIGEST_SIZE_FIELD,
331  HMAC_CFG_DIGEST_SIZE_VALUE_SHA2_512);
332  *msg_block_bytelen = kHmacSha512BlockBytes;
333  *digest_wordlen = kHmacSha512DigestWords;
334  break;
335  default:
336  return OTCRYPTO_BAD_ARGS;
337  }
338  return OTCRYPTO_OK;
339 }
340 
341 status_t hmac_init(hmac_ctx_t *ctx, const hmac_mode_t hmac_mode,
342  const uint32_t *key, size_t key_wordlen) {
343  if (ctx == NULL) {
344  return OTCRYPTO_BAD_ARGS;
345  }
346  // TODO(#23191) Zeroise or randomly populate ctx struct during init.
347 
348  HARDENED_TRY(cfg_derive(hmac_mode, &ctx->cfg_reg, &ctx->msg_block_bytelen,
349  &ctx->digest_wordlen));
350 
351  if (hmac_mode == kHmacModeHmac256 || hmac_mode == kHmacModeHmac384 ||
352  hmac_mode == kHmacModeHmac512) {
353  if (key == NULL) {
354  return OTCRYPTO_BAD_ARGS;
355  }
356  // Ensure that the key length matches the internal block size.
357  if (ctx->msg_block_bytelen != key_wordlen * sizeof(uint32_t)) {
358  return OTCRYPTO_BAD_ARGS;
359  }
360  ctx->key_wordlen = key_wordlen;
361  for (size_t i = 0; i < ctx->key_wordlen; i++) {
362  ctx->key[i] = key[i];
363  }
364  } else {
365  // Ensure that `key` is NULL for hashing operations.
366  if (key != NULL) {
367  return OTCRYPTO_BAD_ARGS;
368  }
369  // Set `key_wordlen` to 0, so that it is clear this is hash operation. This
370  // value is later used to skip writing to KEY registers.
371  ctx->key_wordlen = 0;
372  }
373 
374  ctx->hw_started = 0;
375  ctx->partial_block_len = 0;
376 
377  return OTCRYPTO_OK;
378 }
379 
380 status_t hmac_update(hmac_ctx_t *ctx, const uint8_t *data, size_t len) {
381  if (ctx == NULL || (data == NULL && len > 0)) {
382  return OTCRYPTO_BAD_ARGS;
383  }
384 
385  // Check if incoming message bits with `ctx->partial_block` together has
386  // enough bits to fill an internal SHA-2 block. Otherwise, this function just
387  // appends the incoming bits to partial_block and returns without invoking
388  // HMAC HWIP operation.
389  if (ctx->partial_block_len + len < ctx->msg_block_bytelen) {
390  memcpy(ctx->partial_block + ctx->partial_block_len, data, len);
391  ctx->partial_block_len += len;
392  return OTCRYPTO_OK;
393  }
394 
395  // `leftover` bits refers to the size of the next partial block, after we
396  // handle the current partial block and the incoming message bytes.
397  size_t leftover_len = (ctx->partial_block_len + len) % ctx->msg_block_bytelen;
398 
399  // The previous caller should have left it clean, but it doesn't hurt to
400  // clear again.
401  hmac_hwip_clear();
402  // Retore context will restore the context and also hit start or continue
403  // button as necessary.
404  context_restore(ctx);
405 
406  // Write `partial_block` to MSG_FIFO
407  msg_fifo_write(ctx->partial_block, ctx->partial_block_len);
408 
409  // Keep writing incoming bytes
410  msg_fifo_write(data, len - leftover_len);
411 
412  // Time to tell HMAC HWIP to stop, because we do not have enough message
413  // bytes for another round.
414  uint32_t cmd_reg =
415  bitfield_bit32_write(HMAC_CMD_REG_RESVAL, HMAC_CMD_HASH_STOP_BIT, 1);
416  abs_mmio_write32(kHmacBaseAddr + HMAC_CMD_REG_OFFSET, cmd_reg);
417 
418  // Wait for HMAC HWIP operation to be completed.
419  HARDENED_TRY(hmac_idle_wait());
420 
421  // Store context into `ctx`.
422  context_save(ctx);
423 
424  // Write leftover bytes to `partial_block`, so that future update/final call
425  // can feed them to HMAC HWIP.
426  memcpy(ctx->partial_block, data + len - leftover_len, leftover_len);
427  ctx->partial_block_len = leftover_len;
428 
429  // Clean up HMAC HWIP so it can be reused by other driver calls.
430  hmac_hwip_clear();
431  return OTCRYPTO_OK;
432 }
433 
434 status_t hmac_final(hmac_ctx_t *ctx, uint32_t *digest, size_t digest_wordlen) {
435  if (ctx == NULL || digest == NULL) {
436  return OTCRYPTO_BAD_ARGS;
437  }
438 
439  // Check that `digest_wordlen` matches the one from ctx.
440  if (ctx->digest_wordlen != digest_wordlen) {
441  return OTCRYPTO_BAD_ARGS;
442  }
443 
444  // The previous caller should have left it clean, but it doesn't hurt to
445  // clear again.
446  hmac_hwip_clear();
447 
448  // Retore context will restore the context and also hit start or continue
449  // button as necessary.
450  context_restore(ctx);
451 
452  // Feed the final leftover bytes to HMAC HWIP.
453  msg_fifo_write(ctx->partial_block, ctx->partial_block_len);
454 
455  // All message bytes are fed, now hit the process button.
456  uint32_t cmd_reg =
457  bitfield_bit32_write(HMAC_CMD_REG_RESVAL, HMAC_CMD_HASH_PROCESS_BIT, 1);
458  abs_mmio_write32(kHmacBaseAddr + HMAC_CMD_REG_OFFSET, cmd_reg);
459  HARDENED_TRY(hmac_idle_wait());
460 
461  digest_read(digest, ctx->digest_wordlen);
462 
463  // Clean up HMAC HWIP so it can be reused by other driver calls.
464  hmac_hwip_clear();
465 
466  // TODO(#23191): Destroy sensitive values in the ctx object.
467  return OTCRYPTO_OK;
468 }
469 
470 status_t hmac(const hmac_mode_t hmac_mode, const uint32_t *key,
471  size_t key_wordlen, const uint8_t *data, size_t len,
472  uint32_t *digest, size_t digest_wordlen) {
473  if (data == NULL && len > 0) {
474  return OTCRYPTO_BAD_ARGS;
475  }
476 
477  // The previous caller should have left it clean, but it doesn't hurt to
478  // clear again.
479  hmac_hwip_clear();
480 
481  uint32_t cfg_reg;
482  // Derived values below are only used for verifying their corresponding input
483  // arguments.
484  size_t derived_msg_block_bytelen;
485  size_t derived_digest_wordlen;
486 
487  HARDENED_TRY(cfg_derive(hmac_mode, &cfg_reg, &derived_msg_block_bytelen,
488  &derived_digest_wordlen));
489 
490  // We need to write CFG before key, because it includes `key_swap` endiannes
491  // option.
492  abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, cfg_reg);
493 
494  if (digest_wordlen != derived_digest_wordlen) {
495  return OTCRYPTO_BAD_ARGS;
496  }
497 
498  if (hmac_mode == kHmacModeHmac256 || hmac_mode == kHmacModeHmac384 ||
499  hmac_mode == kHmacModeHmac512) {
500  if (key == NULL ||
501  derived_msg_block_bytelen != key_wordlen * sizeof(uint32_t)) {
502  return OTCRYPTO_BAD_ARGS;
503  }
504  key_write(key, key_wordlen);
505  } else {
506  // Ensure that `key` is NULL and `key_wordlen = 0` for hashing operations.
507  if (key != NULL || key_wordlen != 0) {
508  return OTCRYPTO_BAD_ARGS;
509  }
510  }
511 
512  // `sha_en` is not set by `cfg_derive` so we need to explicity set it now.
513  cfg_reg = bitfield_bit32_write(cfg_reg, HMAC_CFG_SHA_EN_BIT, true);
514  abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, cfg_reg);
515 
516  uint32_t cmd_reg =
517  bitfield_bit32_write(HMAC_CMD_REG_RESVAL, HMAC_CMD_HASH_START_BIT, 1);
518  abs_mmio_write32(kHmacBaseAddr + HMAC_CMD_REG_OFFSET, cmd_reg);
519 
520  msg_fifo_write(data, len);
521 
522  cmd_reg =
523  bitfield_bit32_write(HMAC_CMD_REG_RESVAL, HMAC_CMD_HASH_PROCESS_BIT, 1);
524  abs_mmio_write32(kHmacBaseAddr + HMAC_CMD_REG_OFFSET, cmd_reg);
525 
526  // Wait for HMAC HWIP operation to be completed.
527  HARDENED_TRY(hmac_idle_wait());
528 
529  digest_read(digest, digest_wordlen);
530 
531  // Clean up HMAC HWIP so it can be reused by other driver calls.
532  hmac_hwip_clear();
533 
534  // TODO(#23191): Destroy sensitive values in the ctx object.
535  return OTCRYPTO_OK;
536 }