Skip to content

Commit 5f4210c

Browse files
nasahlpavogelpi
authored andcommitted
[cryptotest] Determine security level
Randomly select a security level for the cryptotests as for some security levels, the implementation might be different. Signed-off-by: Pascal Nasahl <[email protected]>
1 parent 3369368 commit 5f4210c

File tree

8 files changed

+87
-26
lines changed

8 files changed

+87
-26
lines changed

sw/device/tests/crypto/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ cc_library(
195195
"//sw/device/lib/runtime:log",
196196
"//sw/device/lib/testing:profile",
197197
"//sw/device/lib/testing/test_framework:check",
198+
"//sw/device/tests/crypto/lib:crypto_test_lib",
198199
],
199200
)
200201

sw/device/tests/crypto/aes_gcm_testutils.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "sw/device/lib/runtime/log.h"
1515
#include "sw/device/lib/testing/profile.h"
1616
#include "sw/device/lib/testing/test_framework/check.h"
17+
#include "sw/device/tests/crypto/lib/crypto_test_lib.h"
1718

1819
#define MODULE_ID MAKE_MODULE_ID('a', 'g', 't')
1920

@@ -123,13 +124,17 @@ static status_t stream_gcm(otcrypto_aes_gcm_context_t *ctx,
123124

124125
status_t aes_gcm_testutils_encrypt(const aes_gcm_test_t *test, bool streaming,
125126
uint32_t *cycles) {
127+
// Determine the security level.
128+
otcrypto_key_security_level_t sec_level;
129+
TRY(determine_security_level(&sec_level));
130+
126131
// Construct the blinded key configuration.
127132
otcrypto_key_config_t config = {
128133
.version = kOtcryptoLibVersion1,
129134
.key_mode = kOtcryptoKeyModeAesGcm,
130135
.key_length = test->key_len * sizeof(uint32_t),
131136
.hw_backed = kHardenedBoolFalse,
132-
.security_level = kOtcryptoKeySecurityLevelHigh,
137+
.security_level = sec_level,
133138
};
134139

135140
// Construct blinded key from the key and testing mask.
@@ -220,13 +225,17 @@ status_t aes_gcm_testutils_encrypt(const aes_gcm_test_t *test, bool streaming,
220225
status_t aes_gcm_testutils_decrypt(const aes_gcm_test_t *test,
221226
hardened_bool_t *tag_valid, bool streaming,
222227
uint32_t *cycles) {
228+
// Determine the security level.
229+
otcrypto_key_security_level_t sec_level;
230+
TRY(determine_security_level(&sec_level));
231+
223232
// Construct the blinded key configuration.
224233
otcrypto_key_config_t config = {
225234
.version = kOtcryptoLibVersion1,
226235
.key_mode = kOtcryptoKeyModeAesGcm,
227236
.key_length = test->key_len * sizeof(uint32_t),
228237
.hw_backed = kHardenedBoolFalse,
229-
.security_level = kOtcryptoKeySecurityLevelHigh,
238+
.security_level = sec_level,
230239
};
231240

232241
// Construct blinded key from the key and testing mask.

sw/device/tests/crypto/cryptotest/firmware/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ cc_library(
2222
"//sw/device/lib/crypto/impl:keyblob",
2323
"//sw/device/lib/crypto/include:datatypes",
2424
"//sw/device/lib/runtime:log",
25-
"//sw/device/lib/testing:rand_testutils",
2625
"//sw/device/lib/testing/test_framework:ujson_ottf",
2726
"//sw/device/lib/ujson",
2827
"//sw/device/tests/crypto/cryptotest/json:aes_commands",
28+
"//sw/device/tests/crypto/lib:crypto_test_lib",
2929
],
3030
)
3131

@@ -137,10 +137,10 @@ cc_library(
137137
"//sw/device/lib/crypto/impl:integrity",
138138
"//sw/device/lib/crypto/impl:key_transport",
139139
"//sw/device/lib/runtime:log",
140-
"//sw/device/lib/testing:rand_testutils",
141140
"//sw/device/lib/testing/test_framework:ujson_ottf",
142141
"//sw/device/lib/ujson",
143142
"//sw/device/tests/crypto/cryptotest/json:hmac_commands",
143+
"//sw/device/tests/crypto/lib:crypto_test_lib",
144144
],
145145
)
146146

sw/device/tests/crypto/cryptotest/firmware/aes.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include "sw/device/lib/crypto/impl/keyblob.h"
1111
#include "sw/device/lib/crypto/include/datatypes.h"
1212
#include "sw/device/lib/runtime/log.h"
13-
#include "sw/device/lib/testing/rand_testutils.h"
1413
#include "sw/device/lib/testing/test_framework/ujson_ottf.h"
1514
#include "sw/device/lib/ujson/ujson.h"
1615
#include "sw/device/tests/crypto/cryptotest/json/aes_commands.h"
16+
#include "sw/device/tests/crypto/lib/crypto_test_lib.h"
1717

1818
enum {
1919
kAesBlockBytes = 128 / 8,
@@ -28,13 +28,6 @@ static const uint32_t kKeyMask[8] = {
2828
0x8acdcb7e, 0x15e76440, 0x8459b2ce, 0xdc2110cc,
2929
};
3030

31-
// Available security levels. The test randomly chooses one.
32-
static const otcrypto_key_security_level_t security_level[3] = {
33-
kOtcryptoKeySecurityLevelLow,
34-
kOtcryptoKeySecurityLevelMedium,
35-
kOtcryptoKeySecurityLevelHigh,
36-
};
37-
3831
status_t handle_aes_block(ujson_t *uj) {
3932
cryptotest_aes_mode_t uj_mode;
4033
cryptotest_aes_operation_t uj_op;
@@ -117,15 +110,16 @@ status_t handle_aes_block(ujson_t *uj) {
117110
};
118111

119112
// Select a random security level.
120-
size_t sec_lvl_idx = rand_testutils_gen32_range(
121-
/*min=*/0, /*max=*/ARRAYSIZE(security_level) - 1);
113+
otcrypto_key_security_level_t sec_level;
114+
TRY(determine_security_level(&sec_level));
115+
122116
// Build the key configuration
123117
otcrypto_key_config_t config = {
124118
.version = kOtcryptoLibVersion1,
125119
.key_mode = key_mode,
126120
.key_length = uj_data.key_length,
127121
.hw_backed = kHardenedBoolFalse,
128-
.security_level = security_level[sec_lvl_idx],
122+
.security_level = sec_level,
129123
};
130124
// Create buffer to store key
131125
uint32_t key_buf[kAesMaxKeyWords];

sw/device/tests/crypto/cryptotest/firmware/hmac.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
#include "sw/device/lib/crypto/include/datatypes.h"
1212
#include "sw/device/lib/crypto/include/key_transport.h"
1313
#include "sw/device/lib/runtime/log.h"
14-
#include "sw/device/lib/testing/rand_testutils.h"
1514
#include "sw/device/lib/testing/test_framework/ujson_ottf.h"
1615
#include "sw/device/lib/ujson/ujson.h"
1716
#include "sw/device/tests/crypto/cryptotest/json/hmac_commands.h"
17+
#include "sw/device/tests/crypto/lib/crypto_test_lib.h"
1818

1919
const unsigned int kOtcryptoHmacTagBytesSha256 = 32;
2020
const unsigned int kOtcryptoHmacTagBytesSha384 = 48;
@@ -36,13 +36,6 @@ static const uint32_t kTestMask[48] = {
3636
0xA5D39BD2, 0xAB479AD5, 0x5786D029, 0x2E4B7CD7, 0xB77A3D76, 0xE2A09962,
3737
};
3838

39-
// Available security levels. The test randomly chooses one.
40-
static const otcrypto_key_security_level_t security_level[3] = {
41-
kOtcryptoKeySecurityLevelLow,
42-
kOtcryptoKeySecurityLevelMedium,
43-
kOtcryptoKeySecurityLevelHigh,
44-
};
45-
4639
status_t handle_hmac(ujson_t *uj) {
4740
// Declare test arguments
4841
cryptotest_hmac_hash_alg_t uj_hash_alg;
@@ -72,16 +65,18 @@ status_t handle_hmac(ujson_t *uj) {
7265
LOG_ERROR("Unsupported HMAC key mode: %d", uj_hash_alg);
7366
return INVALID_ARGUMENT();
7467
}
68+
7569
// Select a random security level.
76-
size_t sec_lvl_idx = rand_testutils_gen32_range(
77-
/*min=*/0, /*max=*/ARRAYSIZE(security_level) - 1);
70+
otcrypto_key_security_level_t sec_level;
71+
TRY(determine_security_level(&sec_level));
72+
7873
// Build the key configuration
7974
otcrypto_key_config_t config = {
8075
.version = kOtcryptoLibVersion1,
8176
.key_mode = key_mode,
8277
.key_length = uj_key.key_len,
8378
.hw_backed = kHardenedBoolFalse,
84-
.security_level = security_level[sec_lvl_idx],
79+
.security_level = sec_level,
8580
};
8681
// Create key shares.
8782
uint32_t key_buf[ceil_div(uj_key.key_len, sizeof(uint32_t))];

sw/device/tests/crypto/lib/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
package(default_visibility = ["//visibility:public"])
6+
7+
cc_library(
8+
name = "crypto_test_lib",
9+
srcs = ["crypto_test_lib.c"],
10+
hdrs = ["crypto_test_lib.h"],
11+
deps = [
12+
"//sw/device/lib/crypto/include:datatypes",
13+
"//sw/device/lib/testing:rand_testutils",
14+
],
15+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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/base/status.h"
6+
#include "sw/device/lib/crypto/include/datatypes.h"
7+
#include "sw/device/lib/testing/rand_testutils.h"
8+
9+
// Available security levels. The test randomly chooses one.
10+
static const otcrypto_key_security_level_t available_security_levels[3] = {
11+
kOtcryptoKeySecurityLevelLow,
12+
kOtcryptoKeySecurityLevelMedium,
13+
kOtcryptoKeySecurityLevelHigh,
14+
};
15+
16+
status_t determine_security_level(
17+
otcrypto_key_security_level_t *security_level) {
18+
uint32_t sec_lvl_idx = rand_testutils_gen32_range(
19+
/*min=*/0, /*max=*/ARRAYSIZE(available_security_levels) - 1);
20+
*security_level = available_security_levels[sec_lvl_idx];
21+
return OK_STATUS();
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#ifndef OPENTITAN_SW_DEVICE_TESTS_CRYPTO_LIB_CRYPTO_TEST_LIB_H_
6+
#define OPENTITAN_SW_DEVICE_TESTS_CRYPTO_LIB_CRYPTO_TEST_LIB_H_
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif // __cplusplus
11+
12+
/**
13+
* Determine a random security level.
14+
*
15+
* @param[out] security_level Cycle count for the decrypt() call
16+
* @return OK or error.
17+
*/
18+
status_t determine_security_level(
19+
otcrypto_key_security_level_t *security_level);
20+
21+
#ifdef __cplusplus
22+
} // extern "C"
23+
#endif // __cplusplus
24+
25+
#endif // OPENTITAN_SW_DEVICE_TESTS_CRYPTO_LIB_CRYPTO_TEST_LIB_H_

0 commit comments

Comments
 (0)