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 kRsa3072NumBytes = 3072 / 8,
20 kRsa3072NumWords = kRsa3072NumBytes /
sizeof(uint32_t),
34 static uint32_t kTestModulus[kRsa3072NumWords] = {
35 0x5248e8eb, 0x61422b39, 0xc2242ceb, 0x239b6fe8, 0xddf894ca, 0x5b29ea9c,
36 0x30ca1103, 0xb9c19f86, 0x0241a25d, 0x7b1f16bd, 0xee956a86, 0x154da24d,
37 0x2318ca24, 0xf941f899, 0x1299c298, 0xbcd26c2b, 0x1e9c1ce3, 0x5fcfd55f,
38 0x6258c90c, 0x55bed694, 0xd2412d32, 0x3d9bdfff, 0xdde93cc0, 0xdd1beacf,
39 0x54ce8e23, 0x62ec62aa, 0xd95d6ff6, 0xe04eb5af, 0x7b8bc3e8, 0xe300d474,
40 0x0d15ec18, 0xedd8bc92, 0xabc571eb, 0x91fcadfe, 0xebf8d88d, 0xe576ea64,
41 0x482def92, 0x22df77e1, 0x5c36a62b, 0x53b671a0, 0x434ccc48, 0x5bb1b180,
42 0x5dfe37bc, 0xab5c1657, 0x3e1dfa99, 0x64a074ff, 0xde3a683d, 0xddecb80b,
43 0xf7e63faf, 0xea044e20, 0xac776aa1, 0x26d660d3, 0x09435541, 0x1988b5df,
44 0xb82214b1, 0x1cf0109b, 0x8094aee1, 0x4b2dab39, 0x9d0a8e49, 0x37c377b3,
45 0xf9d614e6, 0xd446f2cb, 0x2f8c428b, 0x46b16dd8, 0xabca3029, 0x84807eb9,
46 0x3fc8e542, 0x2cd843c3, 0x6b8cfc8e, 0x6398c69c, 0xdcbafdb2, 0x4bed7f9e,
47 0xd1a7e998, 0x0b53c50a, 0x7fdec0a9, 0x141f23d0, 0x46dcfa85, 0x53cdc14e,
48 0x0922ea46, 0xe82d5c81, 0xcc4ee3b8, 0x5b973b2c, 0x065433ac, 0x075a427b,
49 0xa4b442f1, 0x8adb26e8, 0xe827fc8c, 0xc6057b0b, 0x1c9f5a53, 0x833bf326,
50 0x7520180f, 0x24ec935f, 0xa90cc23b, 0xafe74287, 0x31261907, 0xa60f30a2,
52 static uint32_t kTestPrivateExponent[kRsa3072NumWords] = {
53 0xf3cc45d9, 0x8341feca, 0x919be340, 0xf1624a49, 0x8702a398, 0xb332bc60,
54 0xbf748b23, 0xc52df173, 0x25b096ec, 0x8a71c552, 0x1c7f4bd8, 0x7863c805,
55 0x1f44a192, 0x421f557a, 0xc0944ac3, 0x1753730b, 0x44688498, 0x15dae580,
56 0x3c7a23fa, 0xfe49e5b6, 0x15ce0940, 0x04a3767a, 0x5ce77382, 0xf8a09cd5,
57 0x7fc735a1, 0xde3d3702, 0xe8f2bd3b, 0xf7a1e86a, 0xa7bd0a88, 0x52fc2a22,
58 0x621c40e7, 0x2a95830d, 0x098a4e15, 0x374bb038, 0x26066b83, 0xb8d11858,
59 0xc27647b3, 0xcdb00270, 0x42b3cba4, 0x6f433451, 0xa8133125, 0x5efe857e,
60 0x483a0ef6, 0x3e812025, 0x3c4dcc1e, 0xf8f5486c, 0x5e73b4c9, 0x546a4e70,
61 0xa5e8d6fc, 0xf1bee362, 0x3ad9ea8c, 0x4029e0b8, 0xb1457a1b, 0x74deb3c1,
62 0xd929e23c, 0x10c31fa1, 0x65072772, 0xcb95724f, 0x7af4d123, 0x824db164,
63 0x206695e4, 0x742886cd, 0x884979bc, 0x93e6b99f, 0xfc700b23, 0xbfcc1f26,
64 0xbe115398, 0x1bedb018, 0x33e873c8, 0x8e4583b9, 0x666f2c03, 0x280ede59,
65 0x6a76acdc, 0xb2cd4058, 0xa25ce17f, 0xc24789ff, 0xd69f0f98, 0xee4622f0,
66 0x6cb3bd85, 0xb8d5ccb1, 0xcdb605eb, 0x4de47a43, 0x7bda0c86, 0x8b83c71e,
67 0x31837e6a, 0xb02d0228, 0x9be7eb6e, 0xe1746942, 0xb6b48e76, 0xbf8c79d3,
68 0xd81b2383, 0x9e277621, 0x8c4ca670, 0xcfa2b928, 0xd60f0279, 0x2e1fdefe,
70 static uint32_t kTestPublicExponent = 65537;
73 static const unsigned char kTestMessage[] =
"Test message.";
74 static const size_t kTestMessageLen =
sizeof(kTestMessage) - 1;
77 static const unsigned char kTestLabel[] =
"Test label.";
78 static const size_t kTestLabelLen =
sizeof(kTestLabel) - 1;
82 static const size_t kTestHashModeDigestBytes = 512 / 8;
85 static const size_t kMaxPlaintextBytes =
86 kRsa3072NumBytes - 2 * kTestHashModeDigestBytes - 2;
90 static const uint32_t kValidCiphertext[kRsa3072NumWords] = {
91 0x2b57a10f, 0x26bbfdb9, 0x22f881b6, 0x3ea3735c, 0xb97896d8, 0x5ca9f2b9,
92 0xd70a4c1a, 0xaf1ead0d, 0xc4b0ae6d, 0x76ae6d9b, 0xcacf7b07, 0xb63f651f,
93 0x9fc70de5, 0x5f85a0c0, 0xda3b7882, 0x6c539d54, 0xe5566273, 0xeb461031,
94 0xb20242d6, 0x0cfcb276, 0x01b4e765, 0xe6ac1931, 0x82bf7c08, 0xb3aebe9c,
95 0x37d2f9cd, 0x0fe684a5, 0x9db2afd2, 0xa99b45e5, 0x17a8aff7, 0x22c56bae,
96 0x911f3c58, 0x645e7a67, 0x8833eaa5, 0x11e5d4f0, 0xafff8172, 0x40fc3c4c,
97 0x0a261c85, 0x6d623878, 0x30115176, 0x2853df91, 0x2bd02f85, 0xb1958b8e,
98 0x9288b3f9, 0x430f8ab2, 0x2556cf3a, 0xe485dbfb, 0xa644d055, 0xe0707828,
99 0xbd2aef6b, 0xfeb784a2, 0xe5ebbf8b, 0xa5f8b0fa, 0xb24c0f7b, 0xbfad7a83,
100 0x54903942, 0x56a20d38, 0xef1ad9a2, 0xbae1bc9b, 0x235c90c3, 0x3a9bd4ef,
101 0x2606ef34, 0x643add05, 0x57bc256c, 0xa45749dd, 0x1784134c, 0x3f553100,
102 0x46e3ae90, 0x44552576, 0x4feb281d, 0x4f83b747, 0xa890f513, 0x979afc28,
103 0x2addc3f2, 0x45a1064e, 0x9e1ade54, 0x551bf540, 0x4337b81c, 0x24259077,
104 0x6ba0c23c, 0xd6d81aab, 0xdf94cc28, 0x679ca168, 0xfc2b4700, 0x5018295b,
105 0xb8bf3cce, 0x3afdd992, 0x478989db, 0xfd095c8b, 0xd75b556b, 0x4672dd8c,
106 0x7b9dfe21, 0xb615edad, 0xf001b4d9, 0x9e6f9920, 0x58a92869, 0x15125194,
122 static status_t run_rsa_3072_encrypt(
const uint8_t *msg,
size_t msg_len,
123 const uint8_t *label,
size_t label_len,
124 uint32_t *ciphertext) {
127 .data = kTestModulus,
130 uint32_t public_key_data[
ceil_div(kOtcryptoRsa3072PublicKeyBytes,
133 .key_mode = kOtcryptoKeyModeRsaEncryptOaep,
134 .key_length = kOtcryptoRsa3072PublicKeyBytes,
135 .key = public_key_data,
138 kTestPublicExponent, &public_key));
144 .len = kRsa3072NumWords,
146 uint64_t t_start = profile_start();
149 profile_end_and_print(t_start,
"RSA-3072 encryption");
171 static status_t run_rsa_3072_decrypt(
const uint8_t *label,
size_t label_len,
172 const uint32_t *ciphertext, uint8_t *msg,
176 .data = kTestPrivateExponent,
179 uint32_t share1[
ARRAYSIZE(kTestPrivateExponent)] = {0};
187 .version = kOtcryptoLibVersion1,
188 .key_mode = kOtcryptoKeyModeRsaEncryptOaep,
189 .key_length = kOtcryptoRsa3072PrivateKeyBytes,
191 .security_level = kOtcryptoKeySecurityLevelLow,
193 size_t keyblob_words =
194 ceil_div(kOtcryptoRsa3072PrivateKeyblobBytes,
sizeof(uint32_t));
195 uint32_t keyblob[keyblob_words];
197 .config = private_key_config,
199 .keyblob_length = kOtcryptoRsa3072PrivateKeyblobBytes,
202 .data = kTestModulus,
206 kTestPublicExponent, d_share0,
207 d_share1, &private_key));
213 .len = kRsa3072NumWords,
215 uint64_t t_start = profile_start();
217 label_buf, plaintext_buf, msg_len));
218 profile_end_and_print(t_start,
"RSA-3072 decryption");
223 status_t oaep_decrypt_valid_test(
void) {
225 uint8_t actual_msg[kMaxPlaintextBytes];
226 size_t actual_msg_len;
227 TRY(run_rsa_3072_decrypt(kTestLabel, kTestLabelLen, kValidCiphertext,
228 actual_msg, &actual_msg_len));
231 TRY_CHECK(actual_msg_len == kTestMessageLen);
232 TRY_CHECK_ARRAYS_EQ(actual_msg, kTestMessage, actual_msg_len);
236 status_t oaep_encrypt_decrypt_test(
void) {
240 uint32_t ciphertext[kRsa3072NumWords];
241 TRY(run_rsa_3072_encrypt(kTestMessage, kTestMessageLen, kTestLabel,
242 kTestLabelLen, ciphertext));
245 uint8_t actual_msg[kMaxPlaintextBytes];
246 size_t actual_msg_len;
247 TRY(run_rsa_3072_decrypt(kTestLabel, kTestLabelLen, ciphertext, actual_msg,
251 TRY_CHECK(actual_msg_len == kTestMessageLen);
252 TRY_CHECK_ARRAYS_EQ(actual_msg, kTestMessage, actual_msg_len);
256 OTTF_DEFINE_TEST_CONFIG();
260 CHECK_STATUS_OK(entropy_complex_init());
263 return status_ok(test_result);