11 #include "sw/device/lib/base/status.h"
15 #include "sw/device/lib/testing/hexstr.h"
17 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
18 #include "sw/device/silicon_creator/lib/error.h"
23 kSha256BlockBits = 512,
24 kSha256BlockBytes = kSha256BlockBits / 8,
28 static const char kGettysburgPrelude[] =
29 "Four score and seven years ago our fathers brought forth on this "
30 "continent, a new nation, conceived in Liberty, and dedicated to the "
31 "proposition that all men are created equal.";
41 static const uint32_t kGettysburgDigest[] = {
42 0x8b8cc7ba, 0xe29f6ac0, 0xeb3dd433, 0x420ec587,
43 0x96c324ed, 0x775708a3, 0x0f9034cd, 0x1e6fd403,
56 static const char kGettysburgDigestBigEndian[] =
57 "1e6fd4030f9034cd775708a396c324ed420ec587eb3dd433e29f6ac08b8cc7ba";
59 rom_error_t hmac_test(
void) {
61 hmac_sha256(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1, &digest);
63 const size_t len =
ARRAYSIZE(digest.digest);
64 for (
int i = 0; i < len; ++i) {
65 LOG_INFO(
"word %d = 0x%08x", i, digest.digest[i]);
66 if (digest.digest[i] != kGettysburgDigest[i]) {
73 rom_error_t hmac_process_nowait_test(
void) {
76 hmac_sha256_update(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1);
77 hmac_sha256_process();
78 hmac_sha256_final(&digest);
80 const size_t len =
ARRAYSIZE(digest.digest);
81 for (
int i = 0; i < len; ++i) {
82 LOG_INFO(
"word %d = 0x%08x", i, digest.digest[i]);
83 if (digest.digest[i] != kGettysburgDigest[i]) {
90 rom_error_t hmac_process_wait_test(
void) {
93 hmac_sha256_update(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1);
94 hmac_sha256_process();
97 hmac_sha256_final(&digest);
99 const size_t len =
ARRAYSIZE(digest.digest);
100 for (
int i = 0; i < len; ++i) {
101 LOG_INFO(
"word %d = 0x%08x", i, digest.digest[i]);
102 if (digest.digest[i] != kGettysburgDigest[i]) {
103 return kErrorUnknown;
109 rom_error_t hmac_truncated_test(
void) {
112 hmac_sha256_update(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1);
113 hmac_sha256_process();
114 hmac_sha256_final_truncated(digest,
ARRAYSIZE(digest));
117 for (
int i = 0; i < len; ++i) {
118 LOG_INFO(
"word %d = 0x%08x", i, digest[i]);
119 if (digest[i] != kGettysburgDigest[i]) {
120 return kErrorUnknown;
126 rom_error_t hmac_bigendian_test(
void) {
128 hexstr_decode(&result,
sizeof(result), kGettysburgDigestBigEndian);
131 hmac_sha256_configure(
true);
133 hmac_sha256_update(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1);
134 hmac_sha256_process();
135 hmac_sha256_final(&digest);
137 const size_t len =
ARRAYSIZE(digest.digest);
138 for (
int i = 0; i < len; ++i) {
139 LOG_INFO(
"word %d = 0x%08x", i, digest.digest[i]);
140 if (digest.digest[i] != result[i]) {
141 return kErrorUnknown;
147 rom_error_t hmac_bigendian_truncated_test(
void) {
149 hexstr_decode(&result,
sizeof(result), kGettysburgDigestBigEndian);
151 hmac_sha256_configure(
true);
153 hmac_sha256_update(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1);
154 hmac_sha256_process();
156 hmac_sha256_final_truncated(digest,
ARRAYSIZE(digest));
159 for (
int i = 0; i < len; ++i) {
160 LOG_INFO(
"word %d = 0x%08x", i, digest[i]);
161 if (digest[i] != result[i]) {
162 return kErrorUnknown;
168 static_assert(
sizeof(kGettysburgPrelude) - 1 > kSha256BlockBytes,
169 "Test assumes that the test message is longer than a SHA-256 "
171 rom_error_t hmac_save_restore_test(
void) {
173 unsigned char *input = (
unsigned char *)kGettysburgPrelude;
174 size_t remaining_input_len =
sizeof(kGettysburgPrelude) - 1;
176 hmac_sha256_update(input, kSha256BlockBytes);
177 input += kSha256BlockBytes;
178 remaining_input_len -= kSha256BlockBytes;
182 hmac_sha256_save(&ctx);
185 hmac_sha256_configure(
true);
187 hmac_sha256_update(kGettysburgPrelude,
sizeof(kGettysburgPrelude) - 1);
188 hmac_sha256_process();
190 hmac_sha256_final(&digest_be);
193 hmac_sha256_configure(
false);
194 hmac_sha256_restore(&ctx);
195 hmac_sha256_update(input, remaining_input_len);
196 hmac_sha256_process();
198 hmac_sha256_final(&digest_le);
201 uint32_t expected_digest_be[8];
202 hexstr_decode(&expected_digest_be,
sizeof(expected_digest_be),
203 kGettysburgDigestBigEndian);
204 for (
int i = 0; i <
ARRAYSIZE(digest_be.digest); ++i) {
205 LOG_INFO(
"word %d = 0x%08x", i, digest_be.digest[i]);
206 if (digest_be.digest[i] != expected_digest_be[i]) {
207 return kErrorUnknown;
212 for (
int i = 0; i <
ARRAYSIZE(digest_le.digest); ++i) {
213 LOG_INFO(
"word %d = 0x%08x", i, digest_le.digest[i]);
214 if (digest_le.digest[i] != kGettysburgDigest[i]) {
215 return kErrorUnknown;
221 static_assert(
sizeof(kGettysburgPrelude) - 1 > 2 * kSha256BlockBytes,
222 "Test assumes that the test message is more than 2x longer than "
223 "a SHA-256 message block.");
224 rom_error_t hmac_save_restore_repeated_test(
void) {
226 unsigned char *input = (
unsigned char *)kGettysburgPrelude;
227 size_t remaining_input_len =
sizeof(kGettysburgPrelude) - 1;
229 hmac_sha256_update(input, kSha256BlockBytes);
230 input += kSha256BlockBytes;
231 remaining_input_len -= kSha256BlockBytes;
235 hmac_sha256_save(&ctx);
236 hmac_sha256_restore(&ctx);
237 hmac_sha256_update(input, kSha256BlockBytes);
238 input += kSha256BlockBytes;
239 remaining_input_len -= kSha256BlockBytes;
240 hmac_sha256_save(&ctx);
241 hmac_sha256_restore(&ctx);
242 hmac_sha256_update(input, remaining_input_len);
243 hmac_sha256_process();
245 hmac_sha256_final(&digest_le);
248 for (
int i = 0; i <
ARRAYSIZE(digest_le.digest); ++i) {
249 LOG_INFO(
"word %d = 0x%08x", i, digest_le.digest[i]);
250 if (digest_le.digest[i] != kGettysburgDigest[i]) {
251 return kErrorUnknown;
257 OTTF_DEFINE_TEST_CONFIG();
269 return status_ok(result);