6 #include "sw/device/lib/crypto/drivers/aes.h"
7 #include "sw/device/lib/crypto/drivers/entropy.h"
8 #include "sw/device/lib/crypto/drivers/keymgr.h"
9 #include "sw/device/lib/crypto/impl/integrity.h"
10 #include "sw/device/lib/crypto/impl/status.h"
14 #include "sw/device/lib/testing/keymgr_testutils.h"
15 #include "sw/device/lib/testing/test_framework/check.h"
19 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't')
22 static const uint32_t kKeyVersion = 0x0;
25 static const uint32_t kKeySalt1[7] = {
26 0x00112233, 0x44556677, 0x8899aabb, 0xccddeeff,
27 0x00010203, 0x04050607, 0x08090a0b,
29 static const uint32_t kKeySalt2[7] = {
30 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
31 0x00112233, 0x44556677, 0x8899aabb,
36 .version = kOtcryptoLibVersion1,
37 .key_mode = kOtcryptoKeyModeAesCtr,
38 .key_length = 256 / 8,
40 .security_level = kOtcryptoKeySecurityLevelLow,
44 static const uint32_t kAesIv[4] = {
52 static const uint32_t kAesPlaintextBlock[4] = {0};
67 const uint32_t salt[7],
const uint32_t *input,
72 .config = kAesKeyConfig,
73 .keyblob_length =
sizeof(keyblob),
80 memcpy(iv_data, kAesIv,
sizeof(kAesIv));
88 .data = (
const unsigned char *)input,
89 .len =
sizeof(kAesPlaintextBlock),
94 .data = (
unsigned char *)output,
95 .len =
sizeof(kAesPlaintextBlock),
98 return otcrypto_aes(&key, iv, kOtcryptoAesModeCtr, operation, input_buf,
99 kOtcryptoAesPaddingNull, output_buf);
112 static status_t encrypt(
const uint32_t *salt, uint32_t *ciphertext) {
113 return run_aes(kOtcryptoAesOperationEncrypt, salt, kAesPlaintextBlock,
128 static status_t decrypt(
const uint32_t *salt,
const uint32_t *ciphertext,
129 uint32_t *plaintext) {
130 return run_aes(kOtcryptoAesOperationDecrypt, salt, ciphertext, plaintext);
146 TRY(keymgr_testutils_try_startup(&keymgr, &kmac, &keymgr_state));
149 TRY(keymgr_testutils_advance_state(&keymgr, &kOwnerIntParams));
150 TRY(keymgr_testutils_advance_state(&keymgr, &kOwnerRootKeyParams));
152 TRY(keymgr_testutils_advance_state(&keymgr, &kOwnerRootKeyParams));
160 return entropy_complex_init();
168 status_t basic_encrypt_decrypt_test(
void) {
170 uint32_t ciphertext1[
ARRAYSIZE(kAesPlaintextBlock)];
171 TRY(encrypt(kKeySalt1, ciphertext1));
174 uint32_t recovered_plaintext[
ARRAYSIZE(kAesPlaintextBlock)];
175 TRY(decrypt(kKeySalt1, ciphertext1, recovered_plaintext));
178 TRY_CHECK_ARRAYS_EQ(recovered_plaintext, kAesPlaintextBlock,
190 status_t sideload_update_test(
void) {
192 uint32_t ciphertext1[
ARRAYSIZE(kAesPlaintextBlock)];
193 TRY(encrypt(kKeySalt1, ciphertext1));
196 uint32_t ciphertext2[
ARRAYSIZE(kAesPlaintextBlock)];
197 TRY(encrypt(kKeySalt2, ciphertext2));
200 TRY_CHECK_ARRAYS_NE(ciphertext1, ciphertext2,
ARRAYSIZE(ciphertext1));
203 uint32_t ciphertext3[
ARRAYSIZE(kAesPlaintextBlock)];
204 TRY(encrypt(kKeySalt1, ciphertext3));
207 TRY_CHECK_ARRAYS_EQ(ciphertext1, ciphertext3,
ARRAYSIZE(ciphertext1));
217 status_t sideload_clear_test(
void) {
219 uint32_t ciphertext1[
ARRAYSIZE(kAesPlaintextBlock)];
220 TRY(encrypt(kKeySalt1, ciphertext1));
223 uint32_t share0[256 / 32] = {0};
224 uint32_t share1[256 / 32] = {0};
226 .
mode = kAesCipherModeCtr,
228 .key_len = kAesKeyConfig.key_length,
229 .key_shares = {share0, share1},
234 memcpy(iv.data, kAesIv,
sizeof(kAesIv));
238 TRY_CHECK(!status_ok(aes_encrypt_begin(key, &iv)));
243 OTTF_DEFINE_TEST_CONFIG();
253 return status_ok(result);