6 #include "sw/device/lib/crypto/drivers/entropy.h"
7 #include "sw/device/lib/crypto/impl/integrity.h"
8 #include "sw/device/lib/crypto/impl/keyblob.h"
11 #include "sw/device/lib/testing/test_framework/check.h"
13 #include "sw/device/tests/crypto/aes_testvectors.h"
16 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't')
19 static const uint32_t kKeyMask[8] = {
20 0x1b81540c, 0x220733c9, 0x8bf85383, 0x05ab50b4,
21 0x8acdcb7e, 0x15e76440, 0x8459b2ce, 0xdc2110cc,
28 kAesBlockBytes = 128 / 8,
29 kAesBlockWords = kAesBlockBytes /
sizeof(uint32_t),
35 case kOtcryptoAesModeEcb:
37 key_mode = kOtcryptoKeyModeAesEcb;
39 case kOtcryptoAesModeCbc:
41 key_mode = kOtcryptoKeyModeAesCbc;
43 case kOtcryptoAesModeCfb:
45 key_mode = kOtcryptoKeyModeAesCfb;
47 case kOtcryptoAesModeOfb:
49 key_mode = kOtcryptoKeyModeAesOfb;
51 case kOtcryptoAesModeCtr:
53 key_mode = kOtcryptoKeyModeAesCtr;
57 CHECK(
false,
"Invalid block cipher mode.");
61 .version = kOtcryptoLibVersion1,
63 .key_length =
test->key_len,
65 .security_level = kOtcryptoKeySecurityLevelLow,
83 uint32_t keyblob[keyblob_num_words(config)];
84 TRY(keyblob_from_key_and_mask(
test->key, kKeyMask, config, keyblob));
87 .keyblob_length =
sizeof(keyblob),
90 key.checksum = integrity_blinded_checksum(&key);
93 uint32_t iv_data[kAesBlockWords];
97 .len = kAesBlockWords,
101 size_t padded_len_bytes;
106 uint32_t ciphertext_data[padded_len_bytes /
sizeof(uint32_t)];
110 size_t plaintext_len =
test->plaintext_len;
111 size_t ciphertext_len =
sizeof(ciphertext_data);
112 const unsigned char *plaintext = (
const unsigned char *)
test->plaintext;
113 unsigned char *ciphertext = (
unsigned char *)ciphertext_data;
115 while (plaintext_len > kAesBlockBytes) {
117 .len = kAesBlockBytes};
119 .len = kAesBlockBytes};
121 plaintext_block, kOtcryptoAesPaddingNull,
123 plaintext += kAesBlockBytes;
124 ciphertext += kAesBlockBytes;
125 plaintext_len -= kAesBlockBytes;
126 ciphertext_len -= kAesBlockBytes;
132 .len = plaintext_len};
134 .len = ciphertext_len};
136 plaintext_buf,
test->padding, ciphertext_buf));
138 TRY_CHECK_ARRAYS_EQ(ciphertext_data,
test->exp_ciphertext,
157 uint32_t keyblob[keyblob_num_words(config)];
158 TRY(keyblob_from_key_and_mask(
test->key, kKeyMask, config, keyblob));
161 .keyblob_length =
sizeof(keyblob),
164 key.checksum = integrity_blinded_checksum(&key);
167 uint32_t iv_data[kAesBlockWords];
171 .len = kAesBlockWords,
175 size_t padded_len_bytes;
180 TRY_CHECK(padded_len_bytes %
sizeof(uint32_t) == 0);
181 size_t padded_len_words = padded_len_bytes /
sizeof(uint32_t);
182 uint32_t recovered_plaintext_data[padded_len_words];
183 memset(recovered_plaintext_data, 0,
sizeof(recovered_plaintext_data));
187 size_t len =
sizeof(recovered_plaintext_data);
188 const unsigned char *ciphertext = (
const unsigned char *)
test->exp_ciphertext;
189 unsigned char *recovered_plaintext =
190 (
unsigned char *)recovered_plaintext_data;
192 while (len > kAesBlockBytes) {
194 .len = kAesBlockBytes};
196 .data = recovered_plaintext, .len = kAesBlockBytes};
198 ciphertext_block, kOtcryptoAesPaddingNull,
199 recovered_plaintext_block));
200 ciphertext += kAesBlockBytes;
201 recovered_plaintext += kAesBlockBytes;
202 len -= kAesBlockBytes;
206 TRY_CHECK(len == kAesBlockBytes);
214 ciphertext_buf,
test->padding, recovered_plaintext_buf));
217 TRY_CHECK_ARRAYS_EQ((
unsigned char *)recovered_plaintext_data,
218 (
unsigned char *)
test->plaintext,
test->plaintext_len);
226 static status_t encrypt_test(
void) {
227 return run_encrypt(
test,
false);
233 static status_t decrypt_test(
void) {
234 return run_decrypt(
test,
false);
240 static status_t encrypt_streaming_test(
void) {
241 return run_encrypt(
test,
true);
247 static status_t decrypt_streaming_test(
void) {
248 return run_decrypt(
test,
true);
251 OTTF_DEFINE_TEST_CONFIG();
257 CHECK_STATUS_OK(entropy_complex_init());
259 for (
size_t i = 0; i <
ARRAYSIZE(kAesTests); i++) {
261 test = &kAesTests[i];
266 LOG_INFO(
"Finished AES test %d.", i + 1);
269 return status_ok(result);