Skip to content

Commit 24c0f3f

Browse files
authored
Merge pull request #545 from tock/hmac-test-update
tests: hmac: add check for correctness
2 parents 3dee250 + d4062b8 commit 24c0f3f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

examples/tests/hmac/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ HMAC
22
====
33

44
This test performs a HMAC operation on a string and prints the output
5+
6+
Expected Output
7+
---------------
8+
9+
```
10+
[TEST] HMAC
11+
[HMAC] Computed Output: b5055553bd5e75d4fc96bf2559d655b77a58aea8b4c4076c52f774851bba06d8
12+
[HMAC] Expected Output: b5055553bd5e75d4fc96bf2559d655b77a58aea8b4c4076c52f774851bba06d8
13+
[HMAC] Matched!
14+
```

examples/tests/hmac/main.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
uint8_t key_buf[KEY_LEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xA0, 0xA1};
1212
uint8_t data_buf[DATA_LEN] = "A language empowering everyone to build reliable and efficient software.";
1313
uint8_t dest_buf[DEST_LEN];
14+
uint8_t expected_output[DEST_LEN] = {0xb5, 0x05, 0x55, 0x53, 0xbd, 0x5e, 0x75, 0xd4, 0xfc, 0x96, 0xbf, 0x25, 0x59, 0xd6,
15+
0x55, 0xb7, 0x7a, 0x58, 0xae, 0xa8, 0xb4, 0xc4, 0x07, 0x6c, 0x52, 0xf7, 0x74, 0x85, 0x1b, 0xba, 0x06, 0xd8};
1416

1517

1618

@@ -32,5 +34,30 @@ int main(void) {
3234
return -1;
3335
}
3436

37+
bool match = true;
38+
39+
printf("[HMAC] Computed Output: ");
40+
for (int i = 0; i < DEST_LEN; i++) {
41+
printf("%02x", dest_buf[i]);
42+
43+
if (dest_buf[i] != expected_output[i]) {
44+
match = false;
45+
}
46+
}
47+
printf("\n");
48+
49+
printf("[HMAC] Expected Output: ");
50+
for (int i = 0; i < DEST_LEN; i++) {
51+
printf("%02x", expected_output[i]);
52+
}
53+
printf("\n");
54+
55+
printf("[HMAC] ");
56+
if (match) {
57+
printf("Matched!\n");
58+
} else {
59+
printf("Error! Computation was incorrect!\n");
60+
}
61+
3562
return 0;
3663
}

0 commit comments

Comments
 (0)