8 #include "sw/device/lib/testing/test_framework/check.h"
10 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
11 #include "sw/device/sca/lib/aes.h"
14 #include "sw/device/tests/penetrationtests/firmware/lib/pentest_lib.h"
16 #ifndef OPENTITAN_IS_ENGLISHBREAKFAST
17 #include "sw/device/lib/testing/aes_testutils.h"
46 OTTF_DEFINE_TEST_CONFIG();
49 kAesKeyLengthMax = 32,
59 kIbexAesSleepCycles = 680,
70 kNumBatchOpsMax = 256,
83 uint8_t batch_keys[kNumBatchOpsMax][kAesKeyLength];
88 uint8_t batch_plaintexts[kNumBatchOpsMax][kAesTextLength];
93 bool sample_fixed =
true;
104 static const uint32_t kKeyGenRoundKeys[(kAesKeyLength / 4) * 11] = {
105 0xab239a12, 0xcd45bc34, 0xe067de56, 0xf089f178, 0xbc1734ae, 0xe12c69d5,
106 0x836304da, 0x9262eb1a, 0xcb776054, 0x9d7c5039, 0x71f29195, 0x64f6947f,
107 0xd2196e0e, 0x2bb6ca9a, 0xc4b547d6, 0x6602f460, 0x528099f7, 0xd1fa4c86,
108 0xd317a2e5, 0x452321d5, 0x92c040d9, 0x8756ace0, 0xed3e298b, 0x92d7f4d5,
109 0xfc6eaeee, 0xc84f19b5, 0x3ed3edc4, 0x2bb96e9a, 0x7a86e846, 0x99511e07,
110 0x350bd835, 0xd6fd442a, 0x3c46c028, 0x47de8f91, 0x25101bc3, 0x9f49b4f0,
111 0x29155393, 0xb8ff21ae, 0x36130318, 0x79e6af1b, 0xa68f9ac9, 0xcd758aab,
112 0x88beadae, 0x8ef711be};
117 static uint8_t plaintext_fixed[kAesTextLength] = {
118 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
119 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
123 static uint8_t key_fixed[kAesTextLength] = {0x81, 0x1E, 0x37, 0x31, 0xB0, 0x12,
124 0x0A, 0x78, 0x42, 0x78, 0x1E, 0x22,
125 0xB2, 0x5C, 0xDD, 0xF9};
129 static uint8_t plaintext_random[kAesTextLength] = {
130 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
131 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc};
135 static uint8_t key_random[kAesTextLength] = {0x53, 0x53, 0x53, 0x53, 0x53, 0x53,
136 0x53, 0x53, 0x53, 0x53, 0x53, 0x53,
137 0x53, 0x53, 0x53, 0x53};
141 static uint8_t ciphertext_temp[kAesTextLength];
146 static uint8_t batch_plaintext[kAesTextLength];
152 static uint32_t block_ctr;
154 static dif_aes_t aes;
163 .reseed_on_key_change =
false,
164 .force_masks =
false,
165 .ctrl_aux_lock =
false,
172 static void aes_sca_wait_for_idle(
void) {
191 static void aes_sca_load_fixed_seed(
void) {
192 aes_sca_wait_for_idle();
196 aes_sca_wait_for_idle();
210 static void aes_key_mask_and_config(
const uint8_t *key,
size_t key_len) {
214 for (
int i = 0; i < key_len / 4; ++i) {
215 key_shares.
share1[i] = pentest_non_linear_layer(
216 pentest_linear_layer(pentest_next_lfsr(1, kPentestLfsrMasking)));
217 key_shares.
share0[i] = *((uint32_t *)key + i) ^ key_shares.
share1[i];
220 for (
size_t i = key_len / 4; i < kAesKeyLengthMax / 4; ++i) {
222 pentest_non_linear_layer(pentest_next_lfsr(1, kPentestLfsrMasking));
224 pentest_non_linear_layer(pentest_next_lfsr(1, kPentestLfsrMasking));
226 #ifndef OPENTITAN_IS_ENGLISHBREAKFAST
227 const dif_csrng_t csrng = {
229 const dif_edn_t edn0 = {
232 CHECK_STATUS_OK(aes_testutils_masking_prng_zero_output_seed(&csrng, &edn0));
236 #ifndef OPENTITAN_IS_ENGLISHBREAKFAST
239 aes_sca_load_fixed_seed();
247 static void aes_manual_trigger(
void) {
262 static void aes_serial_key_set(
const uint8_t *key,
size_t key_len) {
264 memcpy(key_fixed, key, key_len);
265 aes_key_mask_and_config(key_fixed, key_len);
279 static void aes_encrypt(
const uint8_t *plaintext,
size_t plaintext_len) {
286 SS_CHECK(plaintext_len ==
sizeof(data.data));
287 memcpy(data.data, plaintext, plaintext_len);
294 pentest_set_trigger_high();
295 pentest_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles,
false,
false);
296 pentest_set_trigger_low();
305 static void aes_send_ciphertext(
bool only_first_word) {
314 if (only_first_word) {
331 static void aes_serial_single_encrypt(
const uint8_t *plaintext,
332 size_t plaintext_len) {
333 SS_CHECK(plaintext_len == kAesTextLength);
339 if (block_ctr > kBlockCtrMax) {
340 aes_key_mask_and_config(key_fixed, kAesKeyLength);
344 aes_encrypt(plaintext, plaintext_len);
346 aes_send_ciphertext(
false);
355 static void aes_serial_advance_fixed(
void) {
356 aes_sw_encrypt_block(plaintext_fixed, kKeyGenRoundKeys, ciphertext_temp);
357 memcpy(plaintext_fixed, ciphertext_temp, kAesTextLength);
366 static void aes_serial_advance_random(
void) {
367 aes_sw_encrypt_block(plaintext_random, kKeyGenRoundKeys, ciphertext_temp);
368 memcpy(plaintext_random, ciphertext_temp, kAesTextLength);
369 aes_sw_encrypt_block(key_random, kKeyGenRoundKeys, ciphertext_temp);
370 memcpy(key_random, ciphertext_temp, kAesTextLength);
379 static void aes_serial_advance_random_data(
void) {
380 aes_sw_encrypt_block(plaintext_random, kKeyGenRoundKeys, ciphertext_temp);
381 memcpy(plaintext_random, ciphertext_temp, kAesTextLength);
409 static void aes_serial_batch_encrypt(
const uint8_t *data,
size_t data_len) {
410 uint32_t num_encryptions = 0;
411 SS_CHECK(data_len ==
sizeof(num_encryptions));
412 num_encryptions =
read_32(data);
414 block_ctr += num_encryptions;
418 if (block_ctr > kBlockCtrMax) {
419 aes_key_mask_and_config(key_fixed, kAesKeyLength);
420 block_ctr = num_encryptions;
423 for (uint32_t i = 0; i < num_encryptions; ++i) {
424 aes_encrypt(plaintext_random, kAesTextLength);
425 aes_serial_advance_random();
428 aes_send_ciphertext(
true);
452 static void aes_serial_batch_alternative_encrypt(
const uint8_t *data,
455 uint32_t num_encryptions = 0;
456 SS_CHECK(data_len ==
sizeof(num_encryptions));
457 num_encryptions =
read_32(data);
460 block_ctr += num_encryptions;
464 if (block_ctr > kBlockCtrMax) {
465 aes_key_mask_and_config(key_fixed, kAesKeyLength);
466 block_ctr = num_encryptions;
475 for (uint32_t i = 0; i < num_encryptions; ++i) {
477 aes_encrypt(batch_plaintext, kAesTextLength);
488 memcpy(batch_plaintext, ciphertext.data, kAesTextLength);
507 static void aes_serial_batch_plaintext_set(
const uint8_t *plaintext,
510 memcpy(batch_plaintext, plaintext, len);
524 static void aes_serial_fvsr_key_set(
const uint8_t *key,
size_t key_len) {
526 memcpy(key_fixed, key, key_len);
554 static void aes_serial_fvsr_key_batch_generate(
const uint8_t *data,
556 uint32_t num_encryptions = 0;
557 SS_CHECK(data_len ==
sizeof(num_encryptions));
558 num_encryptions =
read_32(data);
559 SS_CHECK(num_encryptions <= kNumBatchOpsMax);
561 for (uint32_t i = 0; i < num_encryptions; ++i) {
563 memcpy(batch_keys[i], key_fixed, kAesKeyLength);
564 memcpy(batch_plaintexts[i], plaintext_fixed, kAesKeyLength);
565 aes_serial_advance_fixed();
567 memcpy(batch_keys[i], key_random, kAesKeyLength);
568 memcpy(batch_plaintexts[i], plaintext_random, kAesKeyLength);
569 aes_serial_advance_random();
571 sample_fixed = batch_plaintexts[i][0] & 0x1;
601 static void aes_serial_fvsr_key_batch_encrypt(
const uint8_t *data,
603 uint32_t num_encryptions = 0;
604 SS_CHECK(data_len ==
sizeof(num_encryptions));
605 num_encryptions =
read_32(data);
606 SS_CHECK(num_encryptions <= kNumBatchOpsMax);
608 for (uint32_t i = 0; i < num_encryptions; ++i) {
609 aes_key_mask_and_config(batch_keys[i], kAesKeyLength);
610 aes_encrypt(batch_plaintexts[i], kAesTextLength);
616 aes_send_ciphertext(
false);
620 aes_serial_fvsr_key_batch_generate(data, data_len);
649 static void aes_serial_fvsr_data_batch_encrypt(
const uint8_t *data,
651 uint32_t num_encryptions = 0;
652 SS_CHECK(data_len ==
sizeof(num_encryptions));
653 num_encryptions =
read_32(data);
654 SS_CHECK(num_encryptions <= kNumBatchOpsMax);
656 for (uint32_t i = 0; i < num_encryptions; ++i) {
658 memcpy(batch_keys[i], key_fixed, kAesKeyLength);
660 memcpy(batch_plaintexts[i], plaintext_fixed, kAesKeyLength);
662 memcpy(batch_plaintexts[i], plaintext_random, kAesKeyLength);
663 aes_serial_advance_random_data();
665 sample_fixed = pentest_next_lfsr(1, kPentestLfsrOrder) & 0x1;
668 for (uint32_t i = 0; i < num_encryptions; ++i) {
669 aes_key_mask_and_config(batch_keys[i], kAesKeyLength);
670 aes_encrypt(batch_plaintexts[i], kAesTextLength);
676 aes_send_ciphertext(
false);
687 static void aes_serial_seed_lfsr(
const uint8_t *seed,
size_t seed_len) {
688 SS_CHECK(seed_len ==
sizeof(uint32_t));
689 uint32_t seed_local =
read_32(seed);
690 if (seed_local == 0) {
697 pentest_seed_lfsr(seed_local, kPentestLfsrMasking);
709 static void aes_serial_seed_lfsr_order(
const uint8_t *seed,
size_t seed_len) {
710 SS_CHECK(seed_len ==
sizeof(uint32_t));
711 uint32_t seed_local =
read_32(seed);
712 pentest_seed_lfsr(seed_local, kPentestLfsrOrder);
724 static void aes_serial_set_default_values(
const uint8_t *data,
726 SS_CHECK(data_len ==
sizeof(uint32_t));
727 uint32_t command = 0;
730 static const uint8_t kPlaintextFixedStartFvsrKey[kAesTextLength] = {
731 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
732 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
733 static const uint8_t kKeyFixedStartFvsrKey[kAesTextLength] = {
734 0x81, 0x1E, 0x37, 0x31, 0xB0, 0x12, 0x0A, 0x78,
735 0x42, 0x78, 0x1E, 0x22, 0xB2, 0x5C, 0xDD, 0xF9};
736 static const uint8_t kPlaintextRandomStartFvsrKey[kAesTextLength] = {
737 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
738 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc};
739 static const uint8_t kKeyRandomStartFvsrKey[kAesTextLength] = {
740 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53,
741 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53};
743 static const uint8_t kPlaintextFixedStartFvsrData[kAesTextLength] = {
744 0xDA, 0x39, 0xA3, 0xEE, 0x5E, 0x6B, 0x4B, 0x0D,
745 0x32, 0x55, 0xBF, 0xEF, 0x95, 0x60, 0x18, 0x90};
746 static const uint8_t kPlaintextRandomStartFvsrData[kAesTextLength] = {
747 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
748 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
749 static const uint8_t kKeyStartFvsrData[kAesTextLength] = {
750 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
751 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0};
754 static const uint32_t kPrngInitialState = 0x99999999;
758 memcpy(plaintext_fixed, kPlaintextFixedStartFvsrKey, kAesTextLength);
759 memcpy(key_fixed, kKeyFixedStartFvsrKey, kAesKeyLength);
760 memcpy(plaintext_random, kPlaintextRandomStartFvsrKey, kAesTextLength);
761 memcpy(key_random, kKeyRandomStartFvsrKey, kAesKeyLength);
766 memcpy(plaintext_fixed, kPlaintextFixedStartFvsrData, kAesTextLength);
767 memcpy(key_fixed, kKeyStartFvsrData, kAesKeyLength);
768 memcpy(plaintext_random, kPlaintextRandomStartFvsrData, kAesTextLength);
771 pentest_seed_lfsr(kPrngInitialState, kPentestLfsrOrder);
777 static void init_aes(
void) {
790 pentest_init(kPentestTriggerSourceAes,
791 kPentestPeripheralIoDiv4 | kPentestPeripheralAes);
795 LOG_INFO(
"Initializing simple serial interface to capture board.");
813 #ifndef OPENTITAN_IS_ENGLISHBREAKFAST
815 LOG_INFO(
"Initializing entropy complex.");
816 const dif_csrng_t csrng = {
818 const dif_edn_t edn0 = {
821 CHECK_STATUS_OK(aes_testutils_masking_prng_zero_output_seed(&csrng, &edn0));
822 aes_sca_load_fixed_seed();
827 LOG_INFO(
"Starting simple serial packet handling.");