5 #include "hw/ip/aes/model/aes_modes.h"
8 #include "sw/device/lib/base/multibits.h"
11 #include "sw/device/lib/dif/dif_csrng_shared.h"
14 #include "sw/device/lib/dif/dif_rv_core_ibex.h"
17 #include "sw/device/lib/testing/csrng_testutils.h"
18 #include "sw/device/lib/testing/entropy_testutils.h"
19 #include "sw/device/lib/testing/rv_core_ibex_testutils.h"
20 #include "sw/device/lib/testing/test_framework/check.h"
26 OTTF_DEFINE_TEST_CONFIG();
37 uint32_t kAesNumBlocks;
39 uint32_t kDifAesBlockNumBytes;
40 uint32_t disable_entropy_after_block;
41 uint32_t enable_entropy_at_block;
49 .kDifAesBlockNumBytes = 4,
50 .disable_entropy_after_block = 32,
51 .enable_entropy_at_block = 63,
56 .kAesNumBlocks = 8400,
57 .kDifAesBlockNumBytes = 4,
58 .disable_entropy_after_block = 41,
59 .enable_entropy_at_block = 8191,
63 static const uint8_t kKeyShare1[16] = {
64 0x0f, 0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x7f,
65 0x8f, 0x9f, 0xaf, 0xbf, 0xcf, 0xdf, 0xef, 0xff,
68 static dif_edn_t edn0, edn1;
69 static dif_csrng_t csrng;
70 static dif_entropy_src_t entropy_src;
72 status_t disable_entropy_complex(
void) {
75 TRY(entropy_testutils_stop_all());
76 LOG_INFO(
"The entire entropy complex is stopped");
81 status_t enable_entropy_complex(
void) {
84 TRY(entropy_testutils_auto_mode_init());
85 LOG_INFO(
"The entire entropy complex is enabled");
91 uint8_t new_key_share0[
sizeof(kAesModesKey128)];
93 for (
size_t i = 0; i <
sizeof(kAesModesKey128); ++i) {
94 new_key_share0[i] = kAesModesKey128[i] ^ kKeyShare1[i] * (uint8_t)(index);
97 memcpy(key->share0, new_key_share0,
sizeof(key->share0));
98 memcpy(key->share1, kKeyShare1,
sizeof(key->share1));
103 bool aes_idle =
false;
104 bool input_ready =
false;
105 bool output_valid =
false;
108 "Testing AES PRNG Reseed Test with number of blocks: %d (Block 0 to %d), "
109 "and disabling entropy after block number: %d",
110 config->kAesNumBlocks, config->kAesNumBlocks - 1,
111 config->disable_entropy_after_block - 1);
120 TRY(dif_entropy_src_init(
124 generate_new_key(&key, 0);
132 .mask_reseeding = config->mask_reseeding,
134 .reseed_on_key_change =
true,
135 .force_masks =
false,
136 .ctrl_aux_lock =
false,
149 uint32_t total_blocks = config->kAesNumBlocks;
151 uint32_t blocks_processed = 0;
155 dif_rv_core_ibex_t rv_core_ibex;
156 TRY(dif_rv_core_ibex_init(
159 for (uint32_t i = 0; i <
ARRAYSIZE(plain_text); ++i) {
160 for (uint32_t j = 0; j < config->kDifAesBlockNumBytes; ++j) {
162 TRY(rv_core_ibex_testutils_get_rnd_data(&rv_core_ibex, 2000, &rand));
163 plain_text[i].data[j] = rand;
168 while (blocks_processed < total_blocks) {
169 uint32_t blocks_to_process = total_blocks - blocks_processed;
170 if (blocks_to_process > kMaxChunkSize) {
171 blocks_to_process = kMaxChunkSize;
176 for (uint32_t i = 0; i < blocks_to_process; ++i) {
177 uint32_t block_index = blocks_processed + i;
183 }
while (!input_ready);
189 if (block_index == config->disable_entropy_after_block) {
190 LOG_INFO(
"Disabling entropy complex to induce AES halt at block %d",
192 CHECK_STATUS_OK(disable_entropy_complex());
197 if (block_index == config->enable_entropy_at_block) {
198 output_valid =
false;
201 "ERROR: AES encryption did not halt when entropy was disabled at "
204 LOG_INFO(
"AES encryption is halted as expected at block %d",
208 LOG_INFO(
"Re-enabling entropy complex at block %d", block_index);
209 CHECK_STATUS_OK(enable_entropy_complex());
214 output_valid =
false;
217 }
while (!output_valid);
223 blocks_processed += blocks_to_process;
227 LOG_INFO(
"End AES encryption operation");
245 for (
size_t i = 0; i < kNumTestConfigs; ++i) {
249 LOG_INFO(
"Starting AES PRNG Reseed Test with mask_reseeding: %d",
250 config->mask_reseeding);
253 CHECK_STATUS_OK(execute_test(config));