6 #include "sw/device/lib/crypto/drivers/otbn.h" 
    7 #include "sw/device/lib/crypto/impl/rsa/rsa_datatypes.h" 
   12 #include "sw/device/lib/testing/entropy_testutils.h" 
   13 #include "sw/device/lib/testing/test_framework/check.h" 
   17 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't') 
   20 static const unsigned char kTestMessage[] = 
"Test message.";
 
   21 static const size_t kTestMessageLen = 
sizeof(kTestMessage) - 1;
 
   26 status_t keygen_then_sign_test(
void) {
 
   28   uint32_t public_key_data[
ceil_div(kOtcryptoRsa4096PublicKeyBytes,
 
   30   memset(public_key_data, 0, 
sizeof(public_key_data));
 
   32       .key_mode = kTestKeyMode,
 
   33       .key_length = kOtcryptoRsa4096PublicKeyBytes,
 
   34       .key = public_key_data,
 
   38   size_t keyblob_words =
 
   39       ceil_div(kOtcryptoRsa4096PrivateKeyblobBytes, 
sizeof(uint32_t));
 
   40   uint32_t keyblob[keyblob_words];
 
   41   memset(keyblob, 0, 
sizeof(keyblob));
 
   45               .version = kOtcryptoLibVersion1,
 
   46               .key_mode = kTestKeyMode,
 
   47               .key_length = kOtcryptoRsa4096PrivateKeyBytes,
 
   49               .security_level = kOtcryptoKeySecurityLevelLow,
 
   51       .keyblob_length = kOtcryptoRsa4096PrivateKeyblobBytes,
 
   56   LOG_INFO(
"Starting keypair generation...");
 
   58   LOG_INFO(
"Keypair generation complete.");
 
   59   LOG_INFO(
"OTBN instruction count: %u", otbn_instruction_count_get());
 
   70   TRY_CHECK(pk->e == 65537);
 
   74   TRY_CHECK_ARRAYS_EQ(pk->n.data, sk->n.data, 
ARRAYSIZE(pk->n.data));
 
   78   bool d_large_enough = 
false;
 
   79   for (
size_t i = kRsa4096NumWords / 2; i < kRsa4096NumWords; i++) {
 
   80     if (sk->d.data[i] != 0) {
 
   81       d_large_enough = 
true;
 
   84   TRY_CHECK(d_large_enough);
 
   88       .len = kTestMessageLen,
 
   91   uint32_t msg_digest_data[kSha512DigestWords];
 
   93       .data = msg_digest_data,
 
   95       .mode = kOtcryptoHashModeSha512,
 
   99   uint32_t sig[kRsa4096NumWords];
 
  102       .len = kRsa4096NumWords,
 
  106       .len = kRsa4096NumWords,
 
  110   LOG_INFO(
"Starting signature generation...");
 
  113   LOG_INFO(
"Signature generation complete.");
 
  114   LOG_INFO(
"OTBN instruction count: %u", otbn_instruction_count_get());
 
  118   LOG_INFO(
"Starting signature verification...");
 
  121                           const_sig_buf, &verification_result));
 
  122   LOG_INFO(
"Signature verification complete.");
 
  123   LOG_INFO(
"OTBN instruction count: %u", otbn_instruction_count_get());
 
  130 OTTF_DEFINE_TEST_CONFIG();
 
  133   CHECK_STATUS_OK(entropy_testutils_auto_mode_init());
 
  137   return status_ok(test_result);