Software APIs
hmac_testutils.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/testing/hmac_testutils.h"
6
7#include <stdbool.h>
8
11#include "sw/device/lib/testing/test_framework/check.h"
12
13#define MODULE_ID MAKE_MODULE_ID('h', 'm', 't')
14
15const char kHmacRefData[34] = "Sample message for keylen=blocklen";
16
17const uint8_t kHmacRefLongKey[100] = {
18 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
19 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
20 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
21 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
22 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
23 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
24 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53,
25 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
26 0x60, 0x61, 0x62, 0x63};
27
28/**
29 * Big endian representation of the hashed "long" key, which is used as the
30 * input key into the HMAC mode digest generation.
31 */
32const dif_hmac_digest_t kHmacRefExpectedLongKeyDigest = {
33 .digest =
34 {
35 0x5C954D52,
36 0x9E7F3392,
37 0x381052EE,
38 0x76E4BBF6,
39 0x61D04E43,
40 0x7469A30D,
41 0x9CF5AA6A,
42 0xBCE0AFF1,
43 },
44};
45
46/**
47 * Big endian representation of the final HMAC mode digest.
48 */
49const dif_hmac_digest_t kHmacRefExpectedDigest = {
50 .digest =
51 {
52 0x1B78378D,
53 0xC7A38A43,
54 0x0878DDB9,
55 0x41C63DBB,
56 0x86CB38CC,
57 0x00AE7683,
58 0x2DDEADB5,
59 0xBDCCB6C7,
60 },
61};
62
63status_t hmac_testutils_check_message_length(const dif_hmac_t *hmac,
64 uint64_t expected_sent_bits) {
65 uint64_t sent_bits;
66 TRY(dif_hmac_get_message_length(hmac, &sent_bits));
67
68 // 64bit formatting is not supported, so split into hi and lo hex 32bit
69 // values. These should appear as 64bit hex values in the debug output.
70 TRY_CHECK(expected_sent_bits == sent_bits,
71 "Message length mismatch. "
72 "Expected 0x%08x%08x bits but got 0x%08x%08x bits.",
73 (uint32_t)(expected_sent_bits >> 32), (uint32_t)expected_sent_bits,
74 (uint32_t)(sent_bits >> 32), (uint32_t)sent_bits);
75
76 return OK_STATUS();
77}
78
79/**
80 * Checks whether the HMAC FIFO is empty.
81 */
82static bool check_fifo_empty(const dif_hmac_t *hmac) {
83 uint32_t fifo_depth;
84 dif_result_t res = dif_hmac_fifo_count_entries(hmac, &fifo_depth);
85 return res == kDifOk || fifo_depth == 0;
86}
87
88status_t hmac_testutils_fifo_empty_polled(const dif_hmac_t *hmac) {
89 uint32_t usec;
90 TRY(compute_hmac_testutils_fifo_empty_usec(&usec));
91 IBEX_TRY_SPIN_FOR(check_fifo_empty(hmac), usec);
92 return OK_STATUS();
93}
94
95status_t hmac_testutils_finish_polled(const dif_hmac_t *hmac,
96 dif_hmac_digest_t *digest_out) {
97 uint32_t usec;
98 TRY(compute_hmac_testutils_finish_timeout_usec(&usec));
100 dif_hmac_finish(hmac, /*disable_after_done=*/true, digest_out) == kDifOk,
101 usec);
102 return OK_STATUS();
103}
104
105status_t hmac_testutils_finish_and_check_polled(
106 const dif_hmac_t *hmac, const dif_hmac_digest_t *expected) {
107 dif_hmac_digest_t digest;
108 TRY(hmac_testutils_finish_polled(hmac, &digest));
109 TRY_CHECK(memcmp(&digest.digest, &expected->digest, sizeof(digest.digest)) ==
110 0);
111 return OK_STATUS();
112}
113
114status_t hmac_testutils_push_message(const dif_hmac_t *hmac, const char *data,
115 size_t len) {
116 const char *dp = data;
117 size_t sent_bytes;
118
119 while (dp - data < len) {
120 const size_t offset = (size_t)(dp - data);
121 dif_result_t res = dif_hmac_fifo_push(hmac, dp, len - offset, &sent_bytes);
122 TRY_CHECK(res == kDifOk || res == kDifIpFifoFull, "HMAC error = %d", res);
123
124 // Wait until the FIFO is drained before pushing more data. This helps
125 // to prevent the undesirable back pressure condition.
126 if (res == kDifIpFifoFull) {
127 TRY(hmac_testutils_fifo_empty_polled(hmac));
128 }
129
130 dp += sent_bytes;
131 }
132 return OK_STATUS();
133}