1 /* mbedTLS RSA functionality tests
2
3 Focus on testing functionality where we use ESP32 hardware
4 accelerated crypto features.
5
6 */
7 #include <string.h>
8 #include <stdbool.h>
9 #include <esp_system.h>
10 #include "mbedtls/rsa.h"
11 #include "mbedtls/pk.h"
12 #include "mbedtls/x509_crt.h"
13 #include "mbedtls/entropy_poll.h"
14 #include "freertos/FreeRTOS.h"
15 #include "unity.h"
16 #include "test_utils.h"
17 #include "ccomp_timer.h"
18
19 #define PRINT_DEBUG_INFO
20
21 /* Taken from openssl s_client -connect api.gigafive.com:443 -showcerts
22 */
23 static const char *rsa4096_cert = "-----BEGIN CERTIFICATE-----\n"\
24 "MIIExzCCA6+gAwIBAgIBAzANBgkqhkiG9w0BAQsFADCBkjELMAkGA1UEBhMCVVMx\n"\
25 "CzAJBgNVBAgMAkNBMRQwEgYDVQQHDAtTYW50YSBDbGFyYTElMCMGA1UECgwcR2ln\n"\
26 "YWZpdmUgVGVjaG5vbG9neSBQYXJ0bmVyczEZMBcGA1UEAwwQR2lnYWZpdmUgUm9v\n"\
27 "dCBDQTEeMBwGCSqGSIb3DQEJARYPY2FAZ2lnYWZpdmUuY29tMB4XDTE2MDgyNzE2\n"\
28 "NDYyM1oXDTI2MDgyNTE2NDYyM1owgZcxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJD\n"\
29 "QTEUMBIGA1UEBwwLU2FudGEgQ2xhcmExKTAnBgNVBAoMIEdpZ2FmaXZlIFRlY2hu\n"\
30 "b2xvZ3kgUGFydG5lcnMgTExDMRkwFwYDVQQDDBBhcGkuZ2lnYWZpdmUuY29tMR8w\n"\
31 "HQYJKoZIhvcNAQkBFhBjcmxAZ2lnYWZpdmUuY29tMIICIjANBgkqhkiG9w0BAQEF\n"\
32 "AAOCAg8AMIICCgKCAgEAof82VrEpXMpsI/ddW6RLeTeSYtxiXZZkRbDKN6otYgEk\n"\
33 "vA8yRbzei2cO2A/8+Erhe9beYLAMXWF+bjoUAFwnuIcbmufgHprOYzX/7CYXCs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"\
66 "KmnFD1ZZx3R29/7IZ5OVvQL1hgWbm3cL4JADOc8PQKcqCzBE9JDdAVoa228ESaJ/\n"\
67 "n4b63qaqfgBnoaFzCEruEcXj5nuXBxlk19WWtgY1tZtAgoA8hTWxxH0CAwEAAaNT\n"\
68 "MFEwHQYDVR0OBBYEFPlwrvgkde/r+F8VRMMtpDUIxAtgMB8GA1UdIwQYMBaAFPlw\n"\
69 "rvgkde/r+F8VRMMtpDUIxAtgMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEL\n"\
70 "BQADggGBAH9nBaEP+FWyaZnmxCblKhs8eIEYXzjxbnRUPo5b3uL/PAv1XD1kEUwY\n"\
71 "GWnJ7Z5HOSCdVMgo1opmKGLWuiVP6Vlt9QuA/tWh0bGScL4QfriPXuA7aXAcLbW/\n"\
72 "BqHNJ9Z+H2Fq09XktkZE4Nfnv3iTMMqfNCchM3t3iWZRf2sRVYIdd5OjhM+CLLUK\n"\
73 "kYNiseAgbcBX0/kqTdHlC6OS8Mcu9btJ/663DZy8tndf+PH+EB6fexQd9T31jWoj\n"\
74 "OkEkJ4vDRZP+0LceK7kNcMOcLx8DnF9LwUyHQitW7NMFServoTfxy8A0yep7nIOH\n"\
75 "M/ndECzirQ6WkR9jMG3cw0Jm5mZvA9IAvnLhUO45AyZGC8mShJ0AaXtqejqPg9ng\n"\
76 "//5VIpzoqwVkrMYlMA7ZrccQiRsd2nlBHr+64PRwRCp7y5FOxIzhGzsJibXUpO/V\n"\
77 "FNwuPz+VcnPvJE7r4gB1oRViiGYojMDQV3G+jbgvpTHKUKP6zzavSAKs+FlfEAmh\n"\
78 "EtmuT/beDA==\n"\
79 "-----END CERTIFICATE-----\n";
80
81 /* Root cert from openssl s_client -connect google.com:443 -showcerts
82 */
83 static const char *rsa2048_cert = "-----BEGIN CERTIFICATE-----\n"\
84 "MIIDfTCCAuagAwIBAgIDErvmMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT\n"\
85 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DQEBBQUAA4GB\n"\
100 "AHbhEm5OSxYShjAGsoEIz/AIx8dxfmbuwu3UOx//8PDITtZDOLC5MH0Y0FWDomrL\n"\
101 "NhGc6Ehmo21/uBPUR/6LWlxz/K7ZGzIZOKuXNBSqltLroxwUCEm2u+WR74M26x1W\n"\
102 "b8ravHNjkOR/ez4iyz0H7V84dJzjA1BOoa+Y7mHyhD8S\n"\
103 "-----END CERTIFICATE-----\n";
104
105 /* Some random input bytes to public key encrypt */
106 static const uint8_t pki_input[4096/8] = {
107 0, 1, 4, 6, 7, 9, 33, 103, 49, 11, 56, 211, 67, 92 };
108
109 /* Result of an RSA4096 operation using cert's public key
110 (raw PKI, no padding/etc) */
111 static const uint8_t pki_rsa4096_output[] = {
112 0x91, 0x87, 0xcd, 0x04, 0x80, 0x7c, 0x8b, 0x0b,
113 0x0c, 0xc0, 0x38, 0x37, 0x7a, 0xe3, 0x2c, 0x94,
114 0xea, 0xc4, 0xcb, 0x83, 0x2c, 0x77, 0x71, 0x14,
115 0x11, 0x85, 0x16, 0x61, 0xd3, 0x64, 0x2a, 0x0f,
116 0xf9, 0x6b, 0x45, 0x04, 0x66, 0x5d, 0x15, 0xf1,
117 0xcf, 0x69, 0x77, 0x90, 0xb9, 0x41, 0x68, 0xa9,
118 0xa6, 0xfd, 0x94, 0xdc, 0x6a, 0xce, 0xc7, 0xb6,
119 0x41, 0xd9, 0x44, 0x3c, 0x02, 0xb6, 0xc7, 0x26,
120 0xce, 0xec, 0x66, 0x21, 0xa8, 0xe8, 0xf4, 0xa9,
121 0x33, 0x4a, 0x6c, 0x28, 0x0f, 0x50, 0x30, 0x32,
122 0x28, 0x00, 0xbb, 0x2c, 0xc3, 0x44, 0x72, 0x31,
123 0x93, 0xd4, 0xde, 0x29, 0x6b, 0xfa, 0x31, 0xfd,
124 0x3a, 0x05, 0xc6, 0xb1, 0x28, 0x43, 0x57, 0x20,
125 0xf7, 0xf8, 0x13, 0x0c, 0x4a, 0x80, 0x00, 0xab,
126 0x1f, 0xe8, 0x88, 0xad, 0x56, 0xf2, 0xda, 0x5a,
127 0x50, 0xe9, 0x02, 0x09, 0x21, 0x2a, 0xfc, 0x82,
128 0x68, 0x34, 0xf9, 0x04, 0xa3, 0x25, 0xe1, 0x0f,
129 0xa8, 0x77, 0x29, 0x94, 0xb6, 0x9d, 0x5a, 0x08,
130 0x33, 0x8d, 0x27, 0x6a, 0xc0, 0x3b, 0xad, 0x91,
131 0x8a, 0x83, 0xa9, 0x2e, 0x48, 0xcd, 0x67, 0xa3,
132 0x3a, 0x35, 0x41, 0x85, 0xfa, 0x3f, 0x61, 0x1f,
133 0x80, 0xeb, 0xcd, 0x5a, 0xc5, 0x14, 0x7b, 0xab,
134 0x9c, 0x45, 0x11, 0xd2, 0x25, 0x9a, 0x16, 0xeb,
135 0x9c, 0xfa, 0xbe, 0x73, 0x18, 0xbd, 0x25, 0x8e,
136 0x99, 0x6d, 0xb3, 0xbc, 0xac, 0x2d, 0xa2, 0x53,
137 0xe8, 0x7c, 0x38, 0x1b, 0x7a, 0x75, 0xff, 0x76,
138 0x4f, 0x48, 0x5b, 0x39, 0x20, 0x5a, 0x7b, 0x82,
139 0xd3, 0x33, 0x33, 0x2a, 0xab, 0x6a, 0x7a, 0x42,
140 0x1d, 0x1f, 0xd1, 0x61, 0x58, 0xd7, 0x38, 0x52,
141 0xdf, 0xb0, 0x61, 0x98, 0x63, 0xb7, 0xa1, 0x4e,
142 0xdb, 0x9b, 0xcb, 0xb7, 0x85, 0xc4, 0x3e, 0x03,
143 0xe5, 0x59, 0x50, 0x28, 0x5a, 0x4d, 0x7f, 0x53,
144 0x2e, 0x99, 0x1d, 0x6d, 0x85, 0x27, 0x78, 0x34,
145 0x5e, 0xae, 0xc9, 0x1b, 0x37, 0x96, 0xde, 0x40,
146 0x87, 0x35, 0x3c, 0x1f, 0xe0, 0x8f, 0xfb, 0x3a,
147 0x58, 0x0e, 0x60, 0xe9, 0x06, 0xbd, 0x83, 0x03,
148 0x92, 0xde, 0x5e, 0x69, 0x28, 0xb1, 0x00, 0xeb,
149 0x44, 0xca, 0x3c, 0x49, 0x03, 0x10, 0xa8, 0x84,
150 0xa6, 0xbb, 0xd5, 0xda, 0x98, 0x8c, 0x6f, 0xa3,
151 0x0f, 0x39, 0xf3, 0xa7, 0x7d, 0xd5, 0x3b, 0xe2,
152 0x85, 0x12, 0xda, 0xa4, 0x4d, 0x80, 0x97, 0xcb,
153 0x11, 0xe0, 0x89, 0x90, 0xff, 0x5b, 0x72, 0x19,
154 0x59, 0xd1, 0x39, 0x23, 0x9f, 0xb0, 0x00, 0xe2,
155 0x45, 0x72, 0xc6, 0x9a, 0xbc, 0xe1, 0xd1, 0x51,
156 0x6b, 0x35, 0xd2, 0x49, 0xbf, 0xb6, 0xfe, 0xab,
157 0x09, 0xf7, 0x9d, 0xa4, 0x6e, 0x69, 0xb6, 0xf9,
158 0xde, 0xe3, 0x57, 0x0c, 0x1a, 0x96, 0xf1, 0xcc,
159 0x1c, 0x92, 0xdb, 0x44, 0xf4, 0x45, 0xfa, 0x8f,
160 0x87, 0xcf, 0xf4, 0xd2, 0xa1, 0xf8, 0x69, 0x18,
161 0xcf, 0xdc, 0xa0, 0x1f, 0xb0, 0x26, 0xad, 0x81,
162 0xab, 0xdf, 0x78, 0x18, 0xa2, 0x74, 0xba, 0x2f,
163 0xec, 0x70, 0xa2, 0x1f, 0x56, 0xee, 0xff, 0xc9,
164 0xfe, 0xb1, 0xe1, 0x9b, 0xea, 0x0e, 0x33, 0x14,
165 0x5f, 0x6e, 0xca, 0xee, 0x02, 0x56, 0x5a, 0x67,
166 0x42, 0x9a, 0xbf, 0x55, 0xc0, 0x0f, 0x8e, 0x01,
167 0x67, 0x63, 0x6e, 0xd1, 0x57, 0xf7, 0xf1, 0xc6,
168 0x92, 0x9e, 0xb5, 0x45, 0xe1, 0x50, 0x58, 0x94,
169 0x20, 0x90, 0x6a, 0x29, 0x2d, 0x4b, 0xd1, 0xb5,
170 0x68, 0x63, 0xb5, 0xe6, 0xd8, 0x6e, 0x84, 0x80,
171 0xad, 0xe6, 0x03, 0x1e, 0x51, 0xc2, 0xa8, 0x6d,
172 0x84, 0xec, 0x2d, 0x7c, 0x61, 0x02, 0xd1, 0xda,
173 0xf5, 0x94, 0xfa, 0x2d, 0xa6, 0xed, 0x89, 0x6a,
174 0x6a, 0xda, 0x07, 0x5d, 0x83, 0xfc, 0x43, 0x76,
175 0x7c, 0xca, 0x8c, 0x00, 0xfc, 0xb9, 0x2c, 0x23,
176 };
177
178 static const uint8_t pki_rsa3072_output[] = {
179 0x86, 0xc0, 0xe4, 0xa5, 0x4b, 0x45, 0xe4, 0xd4, 0x0f, 0xb7, 0xe3, 0x10, 0x4f, 0xea, 0x88, 0x91,
180 0x3d, 0xad, 0x43, 0x86, 0x90, 0xf0, 0xd8, 0xf0, 0x29, 0x21, 0xc7, 0x5c, 0x75, 0x49, 0x91, 0xce,
181 0xf8, 0x34, 0x91, 0xbd, 0x89, 0x61, 0xcf, 0x47, 0x0e, 0x4d, 0x3f, 0x29, 0xd1, 0x02, 0xa7, 0xa8,
182 0x8f, 0x6a, 0xda, 0x1a, 0xf2, 0xf1, 0x18, 0x92, 0x35, 0xf6, 0x0c, 0x07, 0x5a, 0x84, 0xfa, 0x65,
183 0xd3, 0x02, 0xe0, 0x53, 0x17, 0x5d, 0xf7, 0x45, 0x26, 0xcc, 0xf9, 0x26, 0xf5, 0x6a, 0x66, 0xbb,
184 0xef, 0x33, 0xcb, 0x03, 0x6e, 0x6a, 0x93, 0x6c, 0x2a, 0x27, 0xa7, 0xf7, 0x2c, 0xdc, 0x00, 0xdd,
185 0x98, 0x52, 0xfb, 0xce, 0x31, 0xe2, 0x96, 0x20, 0x98, 0x0a, 0xf4, 0x19, 0x0f, 0xbf, 0x22, 0xed,
186 0x37, 0xb2, 0x14, 0x10, 0x88, 0xa3, 0x6a, 0x43, 0x26, 0xb8, 0x54, 0xf1, 0xb8, 0xc6, 0x56, 0xb7,
187 0x89, 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 0x15, 0x57, 0xc8, 0x39, 0x14, 0xff, 0x8d, 0x2b, 0x12, 0xf5, 0x5b,
199 0xaf, 0x78, 0x2e, 0x0b, 0xcd, 0x27, 0x83, 0xdb, 0x4e, 0xe1, 0x5d, 0xa5, 0xbd, 0xfe, 0x2b, 0x6e,
200 0x8b, 0x54, 0x7d, 0x14, 0x6f, 0x4d, 0xe1, 0x14, 0xc8, 0x30, 0x0e, 0x10, 0x23, 0x2a, 0xe1, 0xe5,
201 0xee, 0xa3, 0x69, 0x8d, 0xe2, 0x9a, 0xed, 0x0c, 0x23, 0x16, 0x8e, 0x95, 0xae, 0x1a, 0xa2, 0x28,
202 0x61, 0x25, 0xa2, 0x15, 0x74, 0xc4, 0xec, 0x6b, 0x73, 0xb2, 0x8c, 0xd2, 0x64, 0xfd, 0x2b, 0x92,
203 };
204
205 static const uint8_t pki_rsa2048_output[] = {
206 0x47, 0x0b, 0xe5, 0x8a, 0xcd, 0x2f, 0x78, 0x07,
207 0x69, 0x69, 0x70, 0xff, 0x81, 0xdf, 0x96, 0xf0,
208 0xed, 0x82, 0x3a, 0x3d, 0x46, 0xab, 0xe9, 0xc3,
209 0xb5, 0xd9, 0xca, 0xa2, 0x05, 0xa9, 0xf6, 0x6e,
210 0xad, 0x6c, 0xe0, 0xd1, 0xa2, 0xb4, 0xf2, 0x78,
211 0x4a, 0x93, 0xfc, 0x45, 0xe1, 0x9b, 0xdd, 0x62,
212 0xf9, 0x66, 0x2a, 0x14, 0x38, 0x12, 0xb6, 0x50,
213 0x0b, 0xe3, 0x53, 0x9c, 0x12, 0x56, 0xf1, 0xb7,
214 0x83, 0xd5, 0xf3, 0x24, 0x81, 0xcc, 0x5a, 0xeb,
215 0xec, 0xac, 0x68, 0xa8, 0x0c, 0xd7, 0x84, 0x7a,
216 0xbb, 0x77, 0x7b, 0xd5, 0x5b, 0xcf, 0x7b, 0x25,
217 0xd0, 0x75, 0x80, 0x21, 0x12, 0x97, 0x6b, 0xe1,
218 0xb6, 0x51, 0x12, 0x52, 0x6e, 0x01, 0x92, 0xb7,
219 0xcc, 0x70, 0x4b, 0x46, 0x11, 0x98, 0x5a, 0x84,
220 0x1c, 0x90, 0x45, 0x0f, 0x15, 0x77, 0xdb, 0x79,
221 0xe8, 0xff, 0x1f, 0xaa, 0x58, 0x95, 0xce, 0x3c,
222 0x65, 0x0c, 0x66, 0x29, 0xe1, 0x9c, 0x41, 0xbb,
223 0xde, 0x65, 0xb8, 0x29, 0x36, 0x94, 0xbd, 0x87,
224 0x93, 0x39, 0xc5, 0xeb, 0x49, 0x21, 0xc1, 0xeb,
225 0x48, 0xbd, 0x19, 0x13, 0x4d, 0x40, 0x90, 0x88,
226 0xc6, 0x12, 0xd9, 0xf7, 0xdd, 0xc8, 0x4f, 0x89,
227 0xc0, 0x91, 0xf8, 0xeb, 0xcf, 0xe3, 0x12, 0x17,
228 0x88, 0x9c, 0x88, 0xf4, 0xf5, 0xae, 0xf4, 0x15,
229 0xfe, 0x17, 0xf6, 0xa4, 0x74, 0x49, 0x02, 0x05,
230 0x11, 0x3b, 0x92, 0x25, 0x39, 0x2c, 0x4b, 0x08,
231 0x19, 0x76, 0x13, 0x8d, 0xf9, 0xda, 0xae, 0xdf,
232 0x30, 0xda, 0xcc, 0xbb, 0x3f, 0xb9, 0xb0, 0xd6,
233 0x5c, 0x78, 0x4b, 0x2b, 0x35, 0x51, 0x17, 0x48,
234 0xf5, 0xd4, 0x39, 0x7e, 0x05, 0x83, 0x68, 0x86,
235 0x44, 0x5f, 0x56, 0x1d, 0x2c, 0x53, 0xd3, 0x64,
236 0x3a, 0xb2, 0x0c, 0x4a, 0x85, 0xd6, 0x5b, 0x7e,
237 0xf9, 0xe9, 0x50, 0x29, 0x5d, 0x4f, 0xcc, 0xc9,
238 };
239
240 #ifdef CONFIG_MBEDTLS_HARDWARE_MPI
241 /* Pregenerated RSA 4096 size keys using openssl */
242 static const char privkey_4096_buf[] = "-----BEGIN RSA PRIVATE KEY-----\n"
243 "MIIJ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espyrPRiEdmjNPxHXiISt8AK9OcB+GwVVsphERygI35Rz5aoWv3VhUPJqNrBKXwYdO06Q3/ILIz5oprU1wIuER9BSU+ZiUFxnXRHEZIAN7Yj5Piyh5hqNCBHTQK17dlbcFdNokxHdUKmYth/l8wyFYnvA21lt+4XOY8x+aQ/xjde+ZvnSozlTGbVNWHxBqI61MsfzDDStQVrhpniIqWJh6PwXM4CIII9z2mgqfR7NqKmTptAoIBAQDTYQOigmZbFvyrayoXVi8XtTLAnv3jByxR5pY7OtvSbagJ3J1w5CYim4iYq39M6TKP4KkMApy5rWl/tFQabPeRcS0gsxc0TBmFEaMTme7fGgrxcFZ6+koubHZCUN5k0sWmIeWQiKlNaY2uf7vf49TBSMXFuGtTclCjlybCnnlmZMPJuhCDqFsUyNelm15+f5pPyWXM5NiFooEc7WIZj996Zb4uSo1EKruVWONzzqe814s9AOp60SCkuoiv97uVRxbLZNItPRSmXNktQmSx/CEl0AuYPYwvJ9HbZQncfTBH9ExlDyidernjyr4uyHGMZyJN614ICy0gncsZv9ZtAd1FAoIBAA4toGPU/VcKFmK92zgO05jsg5vJzw5xeoxRWKrLg7iby6Su6BuNgaVwfYWeZuOhnXakid7FvFXKH6x44o9gyFm5bKqFhaXDzAnxzqcLeM5V+gititOsstpZCbVOoKQOhgTHyxpFNVX3E/nB8EunydWyhQMxKme//NsRroFm1vWljQKyL3zER82AzyseEpEYZoB/6g0n5uF2lR7KllxeBlINsceQ8g3JkmJTdS1hoXcyUSsZ+EgrRbCykNB5aVC5G3/W1OSZsFHbbMrYHCMnaYKwMqLmOkb11o6nOrJJ4pgHj8CVcp2TNjfy3y0Ru6RZ42b0Q+3LktJBGu9r5d04FgI=\n"
244 "-----END RSA PRIVATE KEY-----";
245
246 static const char privkey_2048_buf[] = "-----BEGIN RSA PRIVATE KEY-----\r\n"
247 "MIIEowIBAAKCAQEA8N8hdkemvj6Tpk975/OWhv9BrTsCBCu+ZYfDb5VI7U2meKBg\r\n"
248 "3dAkyyhRl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b/P/aBt6gYXcaBn\r\n"
263 "cjepx+sE81mnC7UrHb4TjG4hO5t3ZTc6X28gyCUCgYAMZn9lSisecrO5SCJUp0M4\r\n"
264 "NH3stq6XdGqIKBbQnG0J2u9WLh1PUIjbGKdRx1f/bPCGXe0gCRL5yse7/IA7d+51\r\n"
265 "9ZnpDAI8EE+bDgXkWWD5MB/alHjGstdsURSICSR47L2f4g6/T8GlGr3vAg/r53My\r\n"
266 "xv1IXOkFdu1NtbeBKbxaSQKBgENDmw5mAVmIcXiFAEICn4ahp4EoYT6g9T2BhQKu\r\n"
267 "s6BKnU2qUj7Lr5ETOp8dzqGpx3B9Yux/q3cGotmFmd3S2x8SzJ5MlAoqbyy9aRSR\r\n"
268 "DeZeKNL9CuV+YcA7lOz1ZWOOe7AZbHwB38NLPBNb3CheI769iTkfAuLtNvabw8go\r\n"
269 "VokdAoGBALyvBhW+Squ5tx8NOEgAisakhAVOnT6jcoeKy6FyjcvKaWagmCOCC7Gz\r\n"
270 "QB9Yf1tJ+3di+aLtWWdmU494iKJHBtPMhfrYltCpxHHQGlUc/GLPY3Z5bBYYYWpb\r\n"
271 "Wzw4ZvDraKlAs7a9CRwS5cpktk5ptK4rc5noSXkvV+yOT75zXat2\r\n"
272 "-----END RSA PRIVATE KEY-----\r\n";
273
274 static const char privkey_3072_buf[] = "-----BEGIN RSA PRIVATE KEY-----\r\n"
275 "MIIG4wIBAAKCAYEAoMPuYRnHVPP49qiPACIsYBLVuj8xH4XqAuXmurOyPPFfKSch\r\n"
276 "52dn97sXvfXQw6hj+iPBeMSzbSAompjx4mUHtwn2+EvyXjqUe8qtI0y12uzXgOr8\r\n"
277 "vdwNLJO1kTmUWxQIa/e6dZpiKcEYYZ6qWNUGVH9IiMB9HdIFLNIdCAAC+gsK+Q0w\r\n"
278 "OT2CwnGOoZ/PzOXHyfte9pJTDk6nQJDKVTBoOLgVcJoCLwctGf7VJ9YI9+YXJKvW\r\n"
279 "1ZYq8PXM8KAVE7KHN7KiskJx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UUYd4UfDW76e8SjvhRQ7nkpR3f4+LEBCqaJ\r\n"
309 "LDJDhg+6AColwKaWRWV9M1GXHhVD4vaTM46JAvH9wbhmJDUORHq8viyHlwO9QKpK\r\n"
310 "iHE/MtcYb5QBGP5md5wc8LY1lcQazDsJMLlcYNk6ZICNWWrcc2loG4VeOERpHU02\r\n"
311 "6AsKaaMGqBp/T9wYwFPUzk1i+jWCu66xfCYKvEubNdxT/R5juXrd\r\n"
312 "-----END RSA PRIVATE KEY-----\r\n";
313
314 #endif
315
316 _Static_assert(sizeof(pki_rsa2048_output) == 2048/8, "rsa2048 output is wrong size");
317 _Static_assert(sizeof(pki_rsa3072_output) == 3072/8, "rsa3072 output is wrong size");
318 _Static_assert(sizeof(pki_rsa4096_output) == 4096/8, "rsa4096 output is wrong size");
319
320 static void test_cert(const char *cert, const uint8_t *expected_output, size_t output_len);
321 void mbedtls_mpi_printf(const char *name, const mbedtls_mpi *X);
322
323
324 TEST_CASE("mbedtls RSA4096 cert", "[mbedtls]")
325 {
326
327 test_cert(rsa4096_cert, pki_rsa4096_output, 4096/8);
328 }
329
330 TEST_CASE("mbedtls RSA3072 cert", "[mbedtls]")
331 {
332
333 test_cert(rsa3072_cert, pki_rsa3072_output, 3072/8);
334 }
335
336 TEST_CASE("mbedtls RSA2048 cert", "[mbedtls]")
337 {
338 test_cert(rsa2048_cert, pki_rsa2048_output, 2048/8);
339 }
340
test_cert(const char * cert,const uint8_t * expected_output,size_t output_len)341 static void test_cert(const char *cert, const uint8_t *expected_output, size_t output_len)
342 {
343 mbedtls_x509_crt crt;
344 mbedtls_rsa_context *rsa;
345 char buf[output_len];
346 int res;
347
348 bzero(buf, output_len);
349
350 mbedtls_x509_crt_init(&crt);
351
352 TEST_ASSERT_EQUAL_HEX16_MESSAGE(0,
353 -mbedtls_x509_crt_parse(&crt,
354 (const uint8_t *)cert,
355 strlen(cert)+1),
356 "parse cert");
357
358 rsa = mbedtls_pk_rsa(crt.pk);
359 TEST_ASSERT_NOT_NULL(rsa);
360
361 res = mbedtls_rsa_check_pubkey(rsa);
362 TEST_ASSERT_EQUAL_HEX16_MESSAGE(0,
363 -res,
364 "check cert pubkey");
365
366 mbedtls_x509_crt_info(buf, sizeof(buf), "", &crt);
367 puts(buf);
368
369 res = mbedtls_rsa_public(rsa, pki_input, (uint8_t *)buf);
370 if (res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE + MBEDTLS_ERR_RSA_PUBLIC_FAILED) {
371 mbedtls_x509_crt_free(&crt);
372 TEST_IGNORE_MESSAGE("Hardware does not support this key length");
373 }
374
375 TEST_ASSERT_EQUAL_HEX16_MESSAGE(0,
376 -res,
377 "RSA PK operation");
378
379 /*
380 // Dump buffer for debugging
381 for(int i = 0; i < output_len; i++) {
382 printf("0x%02x, ", buf[i]);
383 }
384 printf("\n");
385 */
386
387 TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_output, buf, output_len);
388
389 mbedtls_x509_crt_free(&crt);
390 }
391
392 #ifdef CONFIG_MBEDTLS_HARDWARE_MPI
393 static void rsa_key_operations(int keysize, bool check_performance, bool use_blinding, bool generate_new_rsa);
394
myrand(void * rng_state,unsigned char * output,size_t len)395 static int myrand(void *rng_state, unsigned char *output, size_t len)
396 {
397 size_t olen;
398 return mbedtls_hardware_poll(rng_state, output, len, &olen);
399 }
400
401 #ifdef PRINT_DEBUG_INFO
print_rsa_details(mbedtls_rsa_context * rsa)402 static void print_rsa_details(mbedtls_rsa_context *rsa)
403 {
404 mbedtls_mpi X[5];
405 for (int i=0; i<5; ++i) {
406 mbedtls_mpi_init( &X[i] );
407 }
408
409 if (0 == mbedtls_rsa_export(rsa, &X[0], &X[1], &X[2], &X[3], &X[4])) {
410 for (int i=0; i<5; ++i) {
411 mbedtls_mpi_printf((char*)"N\0P\0Q\0D\0E" + 2*i, &X[i]);
412 mbedtls_mpi_free( &X[i] );
413 }
414 }
415 }
416 #endif
417
418 TEST_CASE("test performance RSA key operations", "[bignum]")
419 {
420 for (int keysize = 2048; keysize <= SOC_RSA_MAX_BIT_LEN; keysize += 1024) {
421 rsa_key_operations(keysize, true, false, false);
422 }
423 }
424
425 TEST_CASE("test RSA-3072 calculations", "[bignum]")
426 {
427 // use pre-genrated keys to make the test run a bit faster
428 rsa_key_operations(3072, false, true, false);
429 }
430
431 TEST_CASE("test RSA-2048 calculations", "[bignum]")
432 {
433 // use pre-genrated keys to make the test run a bit faster
434 rsa_key_operations(2048, false, true, false);
435 }
436
437
438 TEST_CASE("test RSA-4096 calculations", "[bignum]")
439 {
440 // use pre-genrated keys to make the test run a bit faster
441 rsa_key_operations(4096, false, true, false);
442 }
443
444
rsa_key_operations(int keysize,bool check_performance,bool use_blinding,bool generate_new_rsa)445 static void rsa_key_operations(int keysize, bool check_performance, bool use_blinding, bool generate_new_rsa)
446 {
447 mbedtls_pk_context clientkey;
448 mbedtls_rsa_context rsa;
449 unsigned char orig_buf[4096 / 8];
450 unsigned char encrypted_buf[4096 / 8];
451 unsigned char decrypted_buf[4096 / 8];
452 int public_perf, private_perf;
453 int res = 0;
454
455 printf("First, orig_buf is encrypted by the public key, and then decrypted by the private key\n");
456 printf("keysize=%d check_performance=%d use_blinding=%d generate_new_rsa=%d\n", keysize, check_performance, use_blinding, generate_new_rsa);
457
458 memset(orig_buf, 0xAA, sizeof(orig_buf));
459 orig_buf[0] = 0; // Ensure that orig_buf is smaller than rsa.N
460 if (generate_new_rsa) {
461 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PRIVATE, 0);
462 TEST_ASSERT_EQUAL(0, mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537));
463 } else {
464 mbedtls_pk_init(&clientkey);
465
466 switch(keysize) {
467 case 4096:
468 res = mbedtls_pk_parse_key(&clientkey, (const uint8_t *)privkey_4096_buf, sizeof(privkey_4096_buf), NULL, 0);
469 break;
470 case 3072:
471 res = mbedtls_pk_parse_key(&clientkey, (const uint8_t *)privkey_3072_buf, sizeof(privkey_3072_buf), NULL, 0);
472 break;
473 case 2048:
474 res = mbedtls_pk_parse_key(&clientkey, (const uint8_t *)privkey_2048_buf, sizeof(privkey_2048_buf), NULL, 0);
475 break;
476 default:
477 TEST_FAIL_MESSAGE("unsupported keysize, pass generate_new_rsa=true or update test");
478 }
479
480 TEST_ASSERT_EQUAL_HEX16(0, -res);
481
482 memcpy(&rsa, mbedtls_pk_rsa(clientkey), sizeof(mbedtls_rsa_context));
483 }
484
485 #ifdef PRINT_DEBUG_INFO
486 print_rsa_details(&rsa);
487 #endif
488
489 TEST_ASSERT_EQUAL(keysize, (int)rsa.len * 8);
490 TEST_ASSERT_EQUAL(keysize, (int)rsa.D.n * sizeof(mbedtls_mpi_uint) * 8); // The private exponent
491
492 ccomp_timer_start();
493 res = mbedtls_rsa_public(&rsa, orig_buf, encrypted_buf);
494 public_perf = ccomp_timer_stop();
495
496 if (res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE + MBEDTLS_ERR_RSA_PUBLIC_FAILED) {
497 mbedtls_rsa_free(&rsa);
498 TEST_IGNORE_MESSAGE("Hardware does not support this key length");
499 }
500 TEST_ASSERT_EQUAL_HEX16(0, -res);
501
502 ccomp_timer_start();
503 res = mbedtls_rsa_private(&rsa, use_blinding?myrand:NULL, NULL, encrypted_buf, decrypted_buf);
504 private_perf = ccomp_timer_stop();
505 TEST_ASSERT_EQUAL_HEX16(0, -res);
506
507 private_perf = ccomp_timer_stop();
508
509 if (check_performance && keysize == 2048) {
510 TEST_PERFORMANCE_CCOMP_LESS_THAN(RSA_2048KEY_PUBLIC_OP, "public operations %d us", public_perf);
511 TEST_PERFORMANCE_CCOMP_LESS_THAN(RSA_2048KEY_PRIVATE_OP, "private operations %d us", private_perf);
512 } else if (check_performance && keysize == 4096) {
513 TEST_PERFORMANCE_CCOMP_LESS_THAN(RSA_4096KEY_PUBLIC_OP, "public operations %d us", public_perf);
514 TEST_PERFORMANCE_CCOMP_LESS_THAN(RSA_4096KEY_PRIVATE_OP, "private operations %d us", private_perf);
515 }
516
517 TEST_ASSERT_EQUAL_MEMORY_MESSAGE(orig_buf, decrypted_buf, keysize / 8, "RSA operation");
518
519 mbedtls_rsa_free(&rsa);
520 }
521
522 #endif // CONFIG_MBEDTLS_HARDWARE_MPI
523