Skip to content

Commit 7e07c8b

Browse files
esnguyencfrantz
authored andcommitted
[drivers] Add hmac/kmac tests for unaligned inputs
Add tests that ensure the hmac and kmac drivers properly handle unaligned input messages. Signed-off-by: Ellis Sarza-Nguyen <[email protected]>
1 parent a7e4e03 commit 7e07c8b

File tree

3 files changed

+95
-15
lines changed

3 files changed

+95
-15
lines changed

sw/device/silicon_creator/lib/drivers/BUILD

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ load(
1414
)
1515
load(
1616
"//rules/opentitan:defs.bzl",
17+
"EARLGREY_CW340_TEST_ENVS",
1718
"EARLGREY_SILICON_OWNER_ROM_EXT_ENVS",
1819
"EARLGREY_TEST_ENVS",
1920
"fpga_params",
@@ -230,7 +231,10 @@ cc_test(
230231
opentitan_test(
231232
name = "hmac_functest",
232233
srcs = ["hmac_functest.c"],
233-
exec_env = EARLGREY_TEST_ENVS,
234+
exec_env = dicts.add(
235+
EARLGREY_TEST_ENVS,
236+
EARLGREY_CW340_TEST_ENVS,
237+
),
234238
verilator = verilator_params(
235239
timeout = "long",
236240
),
@@ -369,7 +373,10 @@ cc_test(
369373
opentitan_test(
370374
name = "kmac_functest",
371375
srcs = ["kmac_functest.c"],
372-
exec_env = EARLGREY_TEST_ENVS,
376+
exec_env = dicts.add(
377+
EARLGREY_TEST_ENVS,
378+
EARLGREY_CW340_TEST_ENVS,
379+
),
373380
verilator = verilator_params(
374381
timeout = "long",
375382
),

sw/device/silicon_creator/lib/drivers/hmac_functest.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
44

5+
#include <stdalign.h>
56
#include <stdbool.h>
67
#include <stdint.h>
78

@@ -19,17 +20,18 @@
1920

2021
#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
2122

22-
enum {
23-
kSha256BlockBits = 512,
24-
kSha256BlockBytes = kSha256BlockBits / 8,
25-
};
26-
2723
// From: http://www.abrahamlincolnonline.org/lincoln/speeches/gettysburg.htm
28-
static const char kGettysburgPrelude[] =
24+
static alignas(uint32_t) const char kGettysburgPrelude[] =
2925
"Four score and seven years ago our fathers brought forth on this "
3026
"continent, a new nation, conceived in Liberty, and dedicated to the "
3127
"proposition that all men are created equal.";
3228

29+
enum {
30+
kSha256BlockBits = 512,
31+
kSha256BlockBytes = kSha256BlockBits / 8,
32+
kGettysburgPreludeSize = sizeof(kGettysburgPrelude),
33+
};
34+
3335
// The following shell command will produce the sha256sum and convert the
3436
// digest into valid C hexadecimal constants:
3537
//
@@ -88,6 +90,21 @@ static const uint32_t kGettysburgHmacSha256Digest[] = {
8890
0xb54e0af5, 0x9d85e15b, 0xa9a3bafe, 0x9d115197,
8991
};
9092

93+
// The unaligned version of the kGettysburgPrelude is used to test
94+
typedef struct unalignedhash {
95+
char unused;
96+
const char payload[kGettysburgPreludeSize];
97+
} unalignedhash_t;
98+
99+
// This structure is used to ensure that the kGettysburgPrelude is not aligned
100+
// on a 4-byte boundary
101+
static const alignas(uint32_t) unalignedhash_t kGettysburgPreludeUnaligned = {
102+
.payload =
103+
"Four score and seven years ago our fathers brought forth on this "
104+
"continent, a new nation, conceived in Liberty, and dedicated to the "
105+
"proposition that all men are created equal.",
106+
};
107+
91108
rom_error_t hmac_hmac_sha256_test(void) {
92109
hmac_digest_t digest;
93110
hmac_hmac_sha256(kGettysburgPrelude, sizeof(kGettysburgPrelude) - 1, kHmacKey,
@@ -102,6 +119,21 @@ rom_error_t hmac_hmac_sha256_test(void) {
102119
return kErrorOk;
103120
}
104121

122+
rom_error_t hmac_hmac_sha256_unaligned_test(void) {
123+
hmac_digest_t digest;
124+
hmac_hmac_sha256(kGettysburgPreludeUnaligned.payload,
125+
sizeof(kGettysburgPreludeUnaligned.payload) - 1, kHmacKey,
126+
/*big_endian_digest=*/false, &digest);
127+
const size_t len = ARRAYSIZE(digest.digest);
128+
for (int i = 0; i < len; ++i) {
129+
LOG_INFO("word %d = 0x%08x", i, digest.digest[i]);
130+
if (digest.digest[i] != kGettysburgHmacSha256Digest[i]) {
131+
return kErrorUnknown;
132+
}
133+
}
134+
return kErrorOk;
135+
}
136+
105137
rom_error_t hmac_process_nowait_test(void) {
106138
hmac_digest_t digest;
107139
hmac_sha256_init();
@@ -291,6 +323,7 @@ OTTF_DEFINE_TEST_CONFIG();
291323
bool test_main(void) {
292324
status_t result = OK_STATUS();
293325
EXECUTE_TEST(result, hmac_sha256_test);
326+
EXECUTE_TEST(result, hmac_hmac_sha256_unaligned_test);
294327
EXECUTE_TEST(result, hmac_hmac_sha256_test);
295328
EXECUTE_TEST(result, hmac_process_nowait_test);
296329
EXECUTE_TEST(result, hmac_process_wait_test);

sw/device/silicon_creator/lib/drivers/kmac_functest.c

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
44

5+
#include <stdalign.h>
56
#include <stdbool.h>
67
#include <stdint.h>
78

@@ -19,12 +20,32 @@
1920

2021
OTTF_DEFINE_TEST_CONFIG();
2122

23+
enum {
24+
kShortDigestLen = 8,
25+
kLongDigestLen = 75,
26+
kShortMsgLen = 13,
27+
kLongMsgLen = 26 * 4,
28+
};
29+
30+
typedef struct unalignedhash {
31+
char unused;
32+
char short_payload[kShortMsgLen];
33+
char long_payload[kLongMsgLen];
34+
} unalignedhash_t;
35+
36+
static const alignas(uint32_t) unalignedhash_t kKmacTestData = {
37+
.short_payload = "Test message!",
38+
.long_payload =
39+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr"
40+
"stuv"
41+
"wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
42+
};
43+
2244
/**
2345
* Test data: short message with short output.
2446
*/
2547
static const char short_msg[] = "Test message!";
26-
static const size_t short_msg_len = 13;
27-
static const uint32_t short_msg_digest[8] = {
48+
static const uint32_t short_msg_digest[kShortDigestLen] = {
2849
0x84f1c984, 0x7a0316bb, 0xe404cfed, 0x83f9078a,
2950
0x21491adc, 0xd6c30988, 0xc6822ff6, 0x20b73405,
3051
};
@@ -35,8 +56,7 @@ static const uint32_t short_msg_digest[8] = {
3556
static const char long_msg[] =
3657
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv"
3758
"wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
38-
static const size_t long_msg_len = 26 * 4;
39-
static const uint32_t long_msg_digest[75] = {
59+
static const uint32_t long_msg_digest[kLongDigestLen] = {
4060
0x262992ca, 0xe4790cf1, 0x7681c77f, 0xa5366b52, 0x86490a2f, 0xf072d4c9,
4161
0xd4ea499a, 0x7a192fd2, 0xe1156b59, 0xb8f00ad5, 0x2ff4ba7c, 0xdec27032,
4262
0x33624f74, 0x88836d86, 0x4c3c6982, 0xb9e841e1, 0x78acb95a, 0x0bdbc7bc,
@@ -80,14 +100,33 @@ rom_error_t kmac_shake256_test(void) {
80100
RETURN_IF_ERROR(kmac_shake256_configure());
81101

82102
// Simple test.
83-
RETURN_IF_ERROR(shake256_test(short_msg_len, short_msg,
103+
RETURN_IF_ERROR(shake256_test(kShortMsgLen, short_msg,
84104
ARRAYSIZE(short_msg_digest), short_msg_digest));
85105

86106
// Test with long input, short output.
87-
RETURN_IF_ERROR(shake256_test(long_msg_len, long_msg, 1, long_msg_digest));
107+
RETURN_IF_ERROR(shake256_test(kLongMsgLen, long_msg, 1, long_msg_digest));
88108

89109
// Test with long input, long output.
90-
RETURN_IF_ERROR(shake256_test(long_msg_len, long_msg,
110+
RETURN_IF_ERROR(shake256_test(kLongMsgLen, long_msg,
111+
ARRAYSIZE(long_msg_digest), long_msg_digest));
112+
113+
return kErrorOk;
114+
}
115+
116+
rom_error_t kmac_shake256_unalign_test(void) {
117+
// Configure KMAC to run SHAKE-256.
118+
RETURN_IF_ERROR(kmac_shake256_configure());
119+
120+
// Simple test for unalign.
121+
RETURN_IF_ERROR(shake256_test(kShortMsgLen, kKmacTestData.short_payload,
122+
ARRAYSIZE(short_msg_digest), short_msg_digest));
123+
124+
// Test with unalign long input, short output.
125+
RETURN_IF_ERROR(shake256_test(kLongMsgLen, kKmacTestData.long_payload, 1,
126+
long_msg_digest));
127+
128+
// Test with unalign long input, long output.
129+
RETURN_IF_ERROR(shake256_test(kLongMsgLen, kKmacTestData.long_payload,
91130
ARRAYSIZE(long_msg_digest), long_msg_digest));
92131

93132
return kErrorOk;
@@ -176,6 +215,7 @@ bool test_main(void) {
176215

177216
status_t result = OK_STATUS();
178217
EXECUTE_TEST(result, kmac_shake256_test);
218+
EXECUTE_TEST(result, kmac_shake256_unalign_test);
179219
EXECUTE_TEST(result, kmac_kmac256_kat_1);
180220
EXECUTE_TEST(result, kmac_kmac256_kat_2);
181221
EXECUTE_TEST(result, kmac_kmac256_kat_3);

0 commit comments

Comments
 (0)