6 #include "sw/device/lib/crypto/drivers/entropy.h"
7 #include "sw/device/lib/crypto/impl/integrity.h"
11 #include "sw/device/lib/testing/profile.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
16 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't')
19 kRsa2048NumBytes = 2048 / 8,
20 kRsa2048NumWords = kRsa2048NumBytes /
sizeof(uint32_t),
27 static uint32_t kTestModulus[kRsa2048NumWords] = {
28 0x40d984b1, 0x3611356d, 0x9eb2f35c, 0x031a892c, 0x16354662, 0x6a260bad,
29 0xb2b807d6, 0xb7de7ccb, 0x278492e0, 0x41adab06, 0x9e60110f, 0x1414eeff,
30 0x8b80e14e, 0x5eb5ae79, 0x0d98fa5b, 0x58bece1f, 0xcf6bdca8, 0x82f5611f,
31 0x351e3869, 0x075005d6, 0xe813fe23, 0xdd967a37, 0x682d1c41, 0x9fdd2d8c,
32 0x21bdd5fc, 0x4fc459c7, 0x508c9293, 0x1f9ac759, 0x55aacb04, 0x58389f05,
33 0x0d0b00fb, 0x59bb4141, 0x68f9e0bf, 0xc2f1a546, 0x0a71ad19, 0x9c400301,
34 0xa4f8ecb9, 0xcdf39538, 0xaabe9cb0, 0xd9f7b2dc, 0x0e8b292d, 0x8ef6c717,
35 0x720e9520, 0xb0c6a23e, 0xda1e92b1, 0x8b6b4800, 0x2f25082b, 0x7f2d6711,
36 0x426fc94f, 0x9926ba5a, 0x89bd4d2b, 0x977718d5, 0x5a8406be, 0x87d090f3,
37 0x639f9975, 0x5948488b, 0x1d3d9cd7, 0x28c7956b, 0xebb97a3e, 0x1edbf4e2,
38 0x105cc797, 0x924ec514, 0x146810df, 0xb1ab4a49,
40 static uint32_t kTestPrivateExponent[kRsa2048NumWords] = {
41 0x0b19915b, 0xa6a935e6, 0x426b2e10, 0xb4ff0629, 0x7322343b, 0x3f28c8d5,
42 0x190757ce, 0x87409d6b, 0xd88e282b, 0x01c13c2a, 0xebb79189, 0x74cbeab9,
43 0x93de5d54, 0xae1bc80a, 0x083a75f2, 0xd574d229, 0xeb46696e, 0x7648cfb6,
44 0xe7ad1b36, 0xbd0e81b2, 0x19c72703, 0xebea5085, 0xf8c7d152, 0x34dcf84d,
45 0xa437187f, 0x41e4f88e, 0xe4e35f9f, 0xcd8bc6f8, 0x7f98e2f2, 0xffdf75ca,
46 0x3698226e, 0x903f2a56, 0xbf21a6dc, 0x97cbf653, 0xe9d80cb3, 0x55dc1685,
47 0xe0ebae21, 0xc8171e18, 0x8e73d26d, 0xbbdbaac1, 0x886e8007, 0x673c9da4,
48 0xe2cb0698, 0xa9f1ba2d, 0xedab4f0a, 0x197e890c, 0x65e7e736, 0x1de28f24,
49 0x57cf5137, 0x631ff441, 0x22539942, 0xcee3fd41, 0xd22b5f8a, 0x995dd87a,
50 0xcaa6815c, 0x08ca0fd3, 0x8f996093, 0x30b7c446, 0xf69b11f7, 0xa298dd00,
51 0xfd4e8120, 0x059df602, 0x25feb268, 0x0f3f749e,
53 static uint32_t kTestPublicExponent = 65537;
56 static const unsigned char kTestMessage[] =
"Test message.";
57 static const size_t kTestMessageLen =
sizeof(kTestMessage) - 1;
60 static const unsigned char kTestLabel[] =
"Test label.";
61 static const size_t kTestLabelLen =
sizeof(kTestLabel) - 1;
65 static const size_t kTestHashModeDigestBytes = 256 / 8;
68 static const size_t kMaxPlaintextBytes =
69 kRsa2048NumBytes - 2 * kTestHashModeDigestBytes - 2;
73 static const uint32_t kValidCiphertext[kRsa2048NumWords] = {
74 0xfd4efa2a, 0x98502230, 0x8f40a23d, 0xf1bc68ec, 0x32c09a86, 0x31a34a7f,
75 0x4cc36d4d, 0xebde83bb, 0xd8641f7e, 0xedc26ed4, 0x8cd83ce6, 0xca3e0696,
76 0x5a425138, 0xd5d55a43, 0x4666b6eb, 0x7d031dee, 0xbc92a18d, 0xce7f14be,
77 0x768d170, 0xa3b26259, 0x668cf732, 0x72b44d0e, 0xd9f35df1, 0x67e194af,
78 0xf4a47c8a, 0x8c0be5ee, 0x3b132be9, 0x797cdeb, 0x5ac41ab2, 0x960bd1bb,
79 0x4d5f9c16, 0x1b40df52, 0x1cc85cae, 0x897f104f, 0xa6d56f86, 0x13d59592,
80 0x741b5a79, 0x15732dbb, 0xa792b600, 0x8a1a6ad8, 0x6192b34b, 0xd5516b1a,
81 0xab6c8133, 0x4b820cb3, 0xdec5f9b5, 0x9d479d3a, 0xd8e8109c, 0xe9e79346,
82 0x91e4c925, 0x730c258, 0x3ae71747, 0x50ab1e5e, 0x931bd40a, 0x351d2440,
83 0xb5e9273d, 0xd07a5e7b, 0x84487ef2, 0xfa2c3eae, 0x60a289db, 0x533d9a42,
84 0x3473ae8, 0x6b43b4a4, 0x4944f45f, 0x9588b044,
100 static status_t run_rsa_2048_encrypt(
const uint8_t *msg,
size_t msg_len,
101 const uint8_t *label,
size_t label_len,
102 uint32_t *ciphertext) {
105 .data = kTestModulus,
111 .key_mode = kOtcryptoKeyModeRsaEncryptOaep,
113 .key = public_key_data,
116 kTestPublicExponent, &public_key));
122 .len = kRsa2048NumWords,
124 uint64_t t_start = profile_start();
127 profile_end_and_print(t_start,
"RSA-2048 encryption");
149 static status_t run_rsa_2048_decrypt(
const uint8_t *label,
size_t label_len,
150 const uint32_t *ciphertext, uint8_t *msg,
154 .data = kTestPrivateExponent,
157 uint32_t share1[
ARRAYSIZE(kTestPrivateExponent)] = {0};
165 .version = kOtcryptoLibVersion1,
166 .key_mode = kOtcryptoKeyModeRsaEncryptOaep,
169 .security_level = kOtcryptoKeySecurityLevelLow,
171 size_t keyblob_words =
173 uint32_t keyblob[keyblob_words];
175 .config = private_key_config,
180 .data = kTestModulus,
184 kTestPublicExponent, d_share0,
185 d_share1, &private_key));
191 .len = kRsa2048NumWords,
193 uint64_t t_start = profile_start();
195 label_buf, plaintext_buf, msg_len));
196 profile_end_and_print(t_start,
"RSA-2048 decryption");
201 status_t oaep_decrypt_valid_test(
void) {
203 uint8_t actual_msg[kMaxPlaintextBytes];
204 size_t actual_msg_len;
205 TRY(run_rsa_2048_decrypt(kTestLabel, kTestLabelLen, kValidCiphertext,
206 actual_msg, &actual_msg_len));
209 TRY_CHECK(actual_msg_len == kTestMessageLen);
210 TRY_CHECK_ARRAYS_EQ(actual_msg, kTestMessage, actual_msg_len);
214 status_t oaep_encrypt_decrypt_test(
void) {
218 uint32_t ciphertext[kRsa2048NumWords];
219 TRY(run_rsa_2048_encrypt(kTestMessage, kTestMessageLen, kTestLabel,
220 kTestLabelLen, ciphertext));
223 uint8_t actual_msg[kMaxPlaintextBytes];
224 size_t actual_msg_len;
225 TRY(run_rsa_2048_decrypt(kTestLabel, kTestLabelLen, ciphertext, actual_msg,
229 TRY_CHECK(actual_msg_len == kTestMessageLen);
230 TRY_CHECK_ARRAYS_EQ(actual_msg, kTestMessage, actual_msg_len);
234 OTTF_DEFINE_TEST_CONFIG();
238 CHECK_STATUS_OK(entropy_complex_init());
241 return status_ok(test_result);