Skip to content

Commit 2518a4d

Browse files
siemen11nasahlpa
authored andcommitted
[crypto/tests] Test HMAC over all security levels
Adapt the hmac functests to test all security levels. Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
1 parent 6ade208 commit 2518a4d

4 files changed

Lines changed: 93 additions & 13 deletions

File tree

sw/device/tests/crypto/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ opentitan_test(
12511251
"//sw/device/lib/crypto/impl:hmac",
12521252
"//sw/device/lib/runtime:log",
12531253
"//sw/device/lib/testing/test_framework:ottf_main",
1254+
"//sw/device/tests/crypto/lib:crypto_test_lib",
12541255
],
12551256
)
12561257

@@ -1267,6 +1268,7 @@ opentitan_test(
12671268
"//sw/device/lib/crypto/impl:hmac",
12681269
"//sw/device/lib/runtime:log",
12691270
"//sw/device/lib/testing/test_framework:ottf_main",
1271+
"//sw/device/tests/crypto/lib:crypto_test_lib",
12701272
],
12711273
)
12721274

@@ -1283,6 +1285,7 @@ opentitan_test(
12831285
"//sw/device/lib/crypto/impl:hmac",
12841286
"//sw/device/lib/runtime:log",
12851287
"//sw/device/lib/testing/test_framework:ottf_main",
1288+
"//sw/device/tests/crypto/lib:crypto_test_lib",
12861289
],
12871290
)
12881291

sw/device/tests/crypto/hmac_sha256_functest.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "sw/device/lib/runtime/log.h"
1313
#include "sw/device/lib/testing/test_framework/check.h"
1414
#include "sw/device/lib/testing/test_framework/ottf_main.h"
15+
#include "sw/device/tests/crypto/lib/crypto_test_lib.h"
1516

1617
enum {
1718
/**
@@ -43,6 +44,9 @@ static const uint32_t kTestMask[ARRAYSIZE(kLongTestKey)] = {
4344
0xa7ebc3e3, 0x04b2a1b9, 0x764a9630, 0x78b8f9c5, 0x3f2a1d8e,
4445
};
4546

47+
static otcrypto_key_security_level_t current_sec_level =
48+
kOtcryptoKeySecurityLevelLow;
49+
4650
/**
4751
* Call the `otcrypto_mac` API and check the resulting tag.
4852
*
@@ -62,7 +66,7 @@ static status_t run_test(const uint32_t *key, size_t key_len,
6266
.key_length = key_len,
6367
.hw_backed = kHardenedBoolFalse,
6468
.exportable = kHardenedBoolFalse,
65-
.security_level = kOtcryptoKeySecurityLevelLow,
69+
.security_level = current_sec_level,
6670
};
6771

6872
uint32_t keyblob[keyblob_num_words(config)];
@@ -159,7 +163,7 @@ static status_t streaming_test(void) {
159163
.key_length = sizeof(kBasicTestKey),
160164
.hw_backed = kHardenedBoolFalse,
161165
.exportable = kHardenedBoolFalse,
162-
.security_level = kOtcryptoKeySecurityLevelLow,
166+
.security_level = current_sec_level,
163167
};
164168

165169
uint32_t keyblob[keyblob_num_words(config)];
@@ -214,10 +218,27 @@ static volatile status_t test_result;
214218

215219
bool test_main(void) {
216220
test_result = OK_STATUS();
221+
222+
// Testing overall cryptolib low security, i.e., no jittery clock or dummy
223+
// instructions
217224
CHECK_STATUS_OK(otcrypto_init(kOtcryptoKeySecurityLevelLow));
218-
EXECUTE_TEST(test_result, simple_test);
219-
EXECUTE_TEST(test_result, empty_test);
220-
EXECUTE_TEST(test_result, long_key_test);
225+
226+
// Streaming test only works on low security
227+
current_sec_level = kOtcryptoKeySecurityLevelLow;
221228
EXECUTE_TEST(test_result, streaming_test);
229+
230+
for (size_t i = 0; i < ARRAYSIZE(available_security_levels); ++i) {
231+
current_sec_level = available_security_levels[i];
232+
LOG_INFO("Running HMAC tests with security level: %d", current_sec_level);
233+
234+
EXECUTE_TEST(test_result, simple_test);
235+
EXECUTE_TEST(test_result, empty_test);
236+
EXECUTE_TEST(test_result, long_key_test);
237+
238+
if (status_err(test_result)) {
239+
break;
240+
}
241+
}
242+
222243
return status_ok(test_result);
223244
}

sw/device/tests/crypto/hmac_sha384_functest.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "sw/device/lib/runtime/log.h"
1313
#include "sw/device/lib/testing/test_framework/check.h"
1414
#include "sw/device/lib/testing/test_framework/ottf_main.h"
15+
#include "sw/device/tests/crypto/lib/crypto_test_lib.h"
1516

1617
enum {
1718
/**
@@ -48,6 +49,9 @@ static const uint32_t kTestMask[ARRAYSIZE(kLongTestKey)] = {
4849
0x04b2a1b9, 0x764a9630, 0x78b8f9c5, 0x3f2a1d8e,
4950
};
5051

52+
static otcrypto_key_security_level_t current_sec_level =
53+
kOtcryptoKeySecurityLevelLow;
54+
5155
/**
5256
* Call the `otcrypto_mac` API and check the resulting tag.
5357
*
@@ -67,7 +71,7 @@ static status_t run_test(const uint32_t *key, size_t key_len,
6771
.key_length = key_len,
6872
.hw_backed = kHardenedBoolFalse,
6973
.exportable = kHardenedBoolFalse,
70-
.security_level = kOtcryptoKeySecurityLevelLow,
74+
.security_level = current_sec_level,
7175
};
7276

7377
uint32_t keyblob[keyblob_num_words(config)];
@@ -151,9 +155,33 @@ static volatile status_t test_result;
151155

152156
bool test_main(void) {
153157
test_result = OK_STATUS();
158+
159+
const otcrypto_key_security_level_t security_levels[] = {
160+
kOtcryptoKeySecurityLevelLow,
161+
kOtcryptoKeySecurityLevelMedium,
162+
kOtcryptoKeySecurityLevelHigh,
163+
};
164+
165+
// Testing overall cryptolib low security, i.e., no jittery clock or dummy
166+
// instructions
154167
CHECK_STATUS_OK(otcrypto_init(kOtcryptoKeySecurityLevelLow));
155-
EXECUTE_TEST(test_result, empty_test);
156-
EXECUTE_TEST(test_result, simple_test);
157-
EXECUTE_TEST(test_result, long_key_test);
168+
169+
for (size_t i = 0; i < ARRAYSIZE(available_security_levels); ++i) {
170+
current_sec_level = available_security_levels[i];
171+
LOG_INFO("Running HMAC-SHA384 tests with security level: %d",
172+
current_sec_level);
173+
174+
// Initialize hardware for the current security level
175+
CHECK_STATUS_OK(otcrypto_init(current_sec_level));
176+
177+
EXECUTE_TEST(test_result, empty_test);
178+
EXECUTE_TEST(test_result, simple_test);
179+
EXECUTE_TEST(test_result, long_key_test);
180+
181+
if (status_err(test_result)) {
182+
break;
183+
}
184+
}
185+
158186
return status_ok(test_result);
159187
}

sw/device/tests/crypto/hmac_sha512_functest.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "sw/device/lib/runtime/log.h"
1313
#include "sw/device/lib/testing/test_framework/check.h"
1414
#include "sw/device/lib/testing/test_framework/ottf_main.h"
15+
#include "sw/device/tests/crypto/lib/crypto_test_lib.h"
1516

1617
enum {
1718
/**
@@ -51,6 +52,9 @@ static const uint32_t kTestMask[ARRAYSIZE(kLongTestKey)] = {
5152
0x04b2a1b9, 0x764a9630, 0x78b8f9c5, 0x3f2a1d8e,
5253
};
5354

55+
static otcrypto_key_security_level_t current_sec_level =
56+
kOtcryptoKeySecurityLevelLow;
57+
5458
/**
5559
* Call the `otcrypto_mac` API and check the resulting tag.
5660
*
@@ -70,7 +74,7 @@ static status_t run_test(const uint32_t *key, size_t key_len,
7074
.key_length = key_len,
7175
.hw_backed = kHardenedBoolFalse,
7276
.exportable = kHardenedBoolFalse,
73-
.security_level = kOtcryptoKeySecurityLevelLow,
77+
.security_level = current_sec_level,
7478
};
7579

7680
uint32_t keyblob[keyblob_num_words(config)];
@@ -160,9 +164,33 @@ static volatile status_t test_result;
160164

161165
bool test_main(void) {
162166
test_result = OK_STATUS();
167+
168+
const otcrypto_key_security_level_t security_levels[] = {
169+
kOtcryptoKeySecurityLevelLow,
170+
kOtcryptoKeySecurityLevelMedium,
171+
kOtcryptoKeySecurityLevelHigh,
172+
};
173+
174+
// Testing overall cryptolib low security, i.e., no jittery clock or dummy
175+
// instructions
163176
CHECK_STATUS_OK(otcrypto_init(kOtcryptoKeySecurityLevelLow));
164-
EXECUTE_TEST(test_result, empty_test);
165-
EXECUTE_TEST(test_result, simple_test);
166-
EXECUTE_TEST(test_result, long_key_test);
177+
178+
for (size_t i = 0; i < ARRAYSIZE(available_security_levels); ++i) {
179+
current_sec_level = available_security_levels[i];
180+
LOG_INFO("Running HMAC-SHA512 tests with security level: %d",
181+
current_sec_level);
182+
183+
// Initialize hardware for the current security level
184+
CHECK_STATUS_OK(otcrypto_init(current_sec_level));
185+
186+
EXECUTE_TEST(test_result, empty_test);
187+
EXECUTE_TEST(test_result, simple_test);
188+
EXECUTE_TEST(test_result, long_key_test);
189+
190+
if (status_err(test_result)) {
191+
break;
192+
}
193+
}
194+
167195
return status_ok(test_result);
168196
}

0 commit comments

Comments
 (0)