test_rsa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* mbedTLS RSA functionality tests
  2. Focus on testing functionality where we use ESP32 hardware
  3. accelerated crypto features.
  4. */
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include <esp_system.h>
  9. #include "mbedtls/rsa.h"
  10. #include "mbedtls/pk.h"
  11. #include "mbedtls/x509_crt.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/semphr.h"
  15. #include "unity.h"
  16. #include "sdkconfig.h"
  17. /* Taken from openssl s_client -connect api.gigafive.com:443 -showcerts
  18. */
  19. static const char *rsa4096_cert = "-----BEGIN CERTIFICATE-----\n"\
  20. "MIIExzCCA6+gAwIBAgIBAzANBgkqhkiG9w0BAQsFADCBkjELMAkGA1UEBhMCVVMx\n"\
  21. "CzAJBgNVBAgMAkNBMRQwEgYDVQQHDAtTYW50YSBDbGFyYTElMCMGA1UECgwcR2ln\n"\
  22. "YWZpdmUgVGVjaG5vbG9neSBQYXJ0bmVyczEZMBcGA1UEAwwQR2lnYWZpdmUgUm9v\n"\
  23. "dCBDQTEeMBwGCSqGSIb3DQEJARYPY2FAZ2lnYWZpdmUuY29tMB4XDTE2MDgyNzE2\n"\
  24. "NDYyM1oXDTI2MDgyNTE2NDYyM1owgZcxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJD\n"\
  25. "QTEUMBIGA1UEBwwLU2FudGEgQ2xhcmExKTAnBgNVBAoMIEdpZ2FmaXZlIFRlY2hu\n"\
  26. "b2xvZ3kgUGFydG5lcnMgTExDMRkwFwYDVQQDDBBhcGku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L26hnKVspyvCxw7ebI5ZJgjtbrD1t+0D8yrgIZzr7AWGA9Hj\n"\
  42. "WIHqDHGDxwkmfjVVPmuO3l5RtJmL6KV6kVL2bOvVI6gECpFLddmOTtg+iXDfSw3x\n"\
  43. "0+ueMYKr8QLF+TCxfzQTHvTHvOJtcZHecc1n7PYbRmI2p7tV6RoBpV69oM6NAVUV\n"\
  44. "i2QoSxm0pYzDzavOaxwhEPHT34Tpg6fwXy1QokFD9OtxRFtdpTjL3bMWpatZE+ba\n"\
  45. "cjvvf0utMW5fNjTTxu1nnpuxZM3ifTCqZJ+9\n"\
  46. "-----END CERTIFICATE-----\n";
  47. /* Root cert from openssl s_client -connect google.com:443 -showcerts
  48. */
  49. static const char *rsa2048_cert = "-----BEGIN CERTIFICATE-----\n"\
  50. "MIIDfTCCAuagAwIBAgIDErvmMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT\n"\
  51. "MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0\n"\
  52. "aWZpY2F0ZSBBdXRob3JpdHkwHhcNMDIwNTIxMDQwMDAwWhcNMTgwODIxMDQwMDAw\n"\
  53. "WjBCMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UE\n"\
  54. "AxMSR2VvVHJ1c3QgR2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n"\
  55. "CgKCAQEA2swYYzD99BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9m\n"\
  56. "OSm9BXiLnTjoBbdqfnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIu\n"\
  57. "T8rxh0PBFpVXLVDviS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6c\n"\
  58. "JmTM386DGXHKTubU1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmR\n"\
  59. "Cw7+OC7RHQWa9k0+bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5asz\n"\
  60. "PeE4uwc2hGKceeoWMPRfwCvocWvk+QIDAQABo4HwMIHtMB8GA1UdIwQYMBaAFEjm\n"\
  61. "aPkr0rKV10fYIyAQTzOYkJ/UMB0GA1UdDgQWBBTAephojYn7qwVkDBF9qn1luMrM\n"\
  62. "TjAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBBjA6BgNVHR8EMzAxMC+g\n"\
  63. "LaArhilodHRwOi8vY3JsLmdlb3RydXN0LmNvbS9jcmxzL3NlY3VyZWNhLmNybDBO\n"\
  64. "BgNVHSAERzBFMEMGBFUdIAAwOzA5BggrBgEFBQcCARYtaHR0cHM6Ly93d3cuZ2Vv\n"\
  65. "dHJ1c3QuY29tL3Jlc291cmNlcy9yZXBvc2l0b3J5MA0GCSqGSIb3DQEBBQUAA4GB\n"\
  66. "AHbhEm5OSxYShjAGsoEIz/AIx8dxfmbuwu3UOx//8PDITtZDOLC5MH0Y0FWDomrL\n"\
  67. "NhGc6Ehmo21/uBPUR/6LWlxz/K7ZGzIZOKuXNBSqltLroxwUCEm2u+WR74M26x1W\n"\
  68. "b8ravHNjkOR/ez4iyz0H7V84dJzjA1BOoa+Y7mHyhD8S\n"\
  69. "-----END CERTIFICATE-----\n";
  70. /* Some random input bytes to public key encrypt */
  71. static const uint8_t pki_input[4096/8] = {
  72. 0, 1, 4, 6, 7, 9, 33, 103, 49, 11, 56, 211, 67, 92 };
  73. /* Result of an RSA4096 operation using cert's public key
  74. (raw PKI, no padding/etc) */
  75. static const uint8_t pki_rsa4096_output[] = {
  76. 0x91, 0x87, 0xcd, 0x04, 0x80, 0x7c, 0x8b, 0x0b,
  77. 0x0c, 0xc0, 0x38, 0x37, 0x7a, 0xe3, 0x2c, 0x94,
  78. 0xea, 0xc4, 0xcb, 0x83, 0x2c, 0x77, 0x71, 0x14,
  79. 0x11, 0x85, 0x16, 0x61, 0xd3, 0x64, 0x2a, 0x0f,
  80. 0xf9, 0x6b, 0x45, 0x04, 0x66, 0x5d, 0x15, 0xf1,
  81. 0xcf, 0x69, 0x77, 0x90, 0xb9, 0x41, 0x68, 0xa9,
  82. 0xa6, 0xfd, 0x94, 0xdc, 0x6a, 0xce, 0xc7, 0xb6,
  83. 0x41, 0xd9, 0x44, 0x3c, 0x02, 0xb6, 0xc7, 0x26,
  84. 0xce, 0xec, 0x66, 0x21, 0xa8, 0xe8, 0xf4, 0xa9,
  85. 0x33, 0x4a, 0x6c, 0x28, 0x0f, 0x50, 0x30, 0x32,
  86. 0x28, 0x00, 0xbb, 0x2c, 0xc3, 0x44, 0x72, 0x31,
  87. 0x93, 0xd4, 0xde, 0x29, 0x6b, 0xfa, 0x31, 0xfd,
  88. 0x3a, 0x05, 0xc6, 0xb1, 0x28, 0x43, 0x57, 0x20,
  89. 0xf7, 0xf8, 0x13, 0x0c, 0x4a, 0x80, 0x00, 0xab,
  90. 0x1f, 0xe8, 0x88, 0xad, 0x56, 0xf2, 0xda, 0x5a,
  91. 0x50, 0xe9, 0x02, 0x09, 0x21, 0x2a, 0xfc, 0x82,
  92. 0x68, 0x34, 0xf9, 0x04, 0xa3, 0x25, 0xe1, 0x0f,
  93. 0xa8, 0x77, 0x29, 0x94, 0xb6, 0x9d, 0x5a, 0x08,
  94. 0x33, 0x8d, 0x27, 0x6a, 0xc0, 0x3b, 0xad, 0x91,
  95. 0x8a, 0x83, 0xa9, 0x2e, 0x48, 0xcd, 0x67, 0xa3,
  96. 0x3a, 0x35, 0x41, 0x85, 0xfa, 0x3f, 0x61, 0x1f,
  97. 0x80, 0xeb, 0xcd, 0x5a, 0xc5, 0x14, 0x7b, 0xab,
  98. 0x9c, 0x45, 0x11, 0xd2, 0x25, 0x9a, 0x16, 0xeb,
  99. 0x9c, 0xfa, 0xbe, 0x73, 0x18, 0xbd, 0x25, 0x8e,
  100. 0x99, 0x6d, 0xb3, 0xbc, 0xac, 0x2d, 0xa2, 0x53,
  101. 0xe8, 0x7c, 0x38, 0x1b, 0x7a, 0x75, 0xff, 0x76,
  102. 0x4f, 0x48, 0x5b, 0x39, 0x20, 0x5a, 0x7b, 0x82,
  103. 0xd3, 0x33, 0x33, 0x2a, 0xab, 0x6a, 0x7a, 0x42,
  104. 0x1d, 0x1f, 0xd1, 0x61, 0x58, 0xd7, 0x38, 0x52,
  105. 0xdf, 0xb0, 0x61, 0x98, 0x63, 0xb7, 0xa1, 0x4e,
  106. 0xdb, 0x9b, 0xcb, 0xb7, 0x85, 0xc4, 0x3e, 0x03,
  107. 0xe5, 0x59, 0x50, 0x28, 0x5a, 0x4d, 0x7f, 0x53,
  108. 0x2e, 0x99, 0x1d, 0x6d, 0x85, 0x27, 0x78, 0x34,
  109. 0x5e, 0xae, 0xc9, 0x1b, 0x37, 0x96, 0xde, 0x40,
  110. 0x87, 0x35, 0x3c, 0x1f, 0xe0, 0x8f, 0xfb, 0x3a,
  111. 0x58, 0x0e, 0x60, 0xe9, 0x06, 0xbd, 0x83, 0x03,
  112. 0x92, 0xde, 0x5e, 0x69, 0x28, 0xb1, 0x00, 0xeb,
  113. 0x44, 0xca, 0x3c, 0x49, 0x03, 0x10, 0xa8, 0x84,
  114. 0xa6, 0xbb, 0xd5, 0xda, 0x98, 0x8c, 0x6f, 0xa3,
  115. 0x0f, 0x39, 0xf3, 0xa7, 0x7d, 0xd5, 0x3b, 0xe2,
  116. 0x85, 0x12, 0xda, 0xa4, 0x4d, 0x80, 0x97, 0xcb,
  117. 0x11, 0xe0, 0x89, 0x90, 0xff, 0x5b, 0x72, 0x19,
  118. 0x59, 0xd1, 0x39, 0x23, 0x9f, 0xb0, 0x00, 0xe2,
  119. 0x45, 0x72, 0xc6, 0x9a, 0xbc, 0xe1, 0xd1, 0x51,
  120. 0x6b, 0x35, 0xd2, 0x49, 0xbf, 0xb6, 0xfe, 0xab,
  121. 0x09, 0xf7, 0x9d, 0xa4, 0x6e, 0x69, 0xb6, 0xf9,
  122. 0xde, 0xe3, 0x57, 0x0c, 0x1a, 0x96, 0xf1, 0xcc,
  123. 0x1c, 0x92, 0xdb, 0x44, 0xf4, 0x45, 0xfa, 0x8f,
  124. 0x87, 0xcf, 0xf4, 0xd2, 0xa1, 0xf8, 0x69, 0x18,
  125. 0xcf, 0xdc, 0xa0, 0x1f, 0xb0, 0x26, 0xad, 0x81,
  126. 0xab, 0xdf, 0x78, 0x18, 0xa2, 0x74, 0xba, 0x2f,
  127. 0xec, 0x70, 0xa2, 0x1f, 0x56, 0xee, 0xff, 0xc9,
  128. 0xfe, 0xb1, 0xe1, 0x9b, 0xea, 0x0e, 0x33, 0x14,
  129. 0x5f, 0x6e, 0xca, 0xee, 0x02, 0x56, 0x5a, 0x67,
  130. 0x42, 0x9a, 0xbf, 0x55, 0xc0, 0x0f, 0x8e, 0x01,
  131. 0x67, 0x63, 0x6e, 0xd1, 0x57, 0xf7, 0xf1, 0xc6,
  132. 0x92, 0x9e, 0xb5, 0x45, 0xe1, 0x50, 0x58, 0x94,
  133. 0x20, 0x90, 0x6a, 0x29, 0x2d, 0x4b, 0xd1, 0xb5,
  134. 0x68, 0x63, 0xb5, 0xe6, 0xd8, 0x6e, 0x84, 0x80,
  135. 0xad, 0xe6, 0x03, 0x1e, 0x51, 0xc2, 0xa8, 0x6d,
  136. 0x84, 0xec, 0x2d, 0x7c, 0x61, 0x02, 0xd1, 0xda,
  137. 0xf5, 0x94, 0xfa, 0x2d, 0xa6, 0xed, 0x89, 0x6a,
  138. 0x6a, 0xda, 0x07, 0x5d, 0x83, 0xfc, 0x43, 0x76,
  139. 0x7c, 0xca, 0x8c, 0x00, 0xfc, 0xb9, 0x2c, 0x23,
  140. };
  141. static const uint8_t pki_rsa2048_output[] = {
  142. 0x47, 0x0b, 0xe5, 0x8a, 0xcd, 0x2f, 0x78, 0x07,
  143. 0x69, 0x69, 0x70, 0xff, 0x81, 0xdf, 0x96, 0xf0,
  144. 0xed, 0x82, 0x3a, 0x3d, 0x46, 0xab, 0xe9, 0xc3,
  145. 0xb5, 0xd9, 0xca, 0xa2, 0x05, 0xa9, 0xf6, 0x6e,
  146. 0xad, 0x6c, 0xe0, 0xd1, 0xa2, 0xb4, 0xf2, 0x78,
  147. 0x4a, 0x93, 0xfc, 0x45, 0xe1, 0x9b, 0xdd, 0x62,
  148. 0xf9, 0x66, 0x2a, 0x14, 0x38, 0x12, 0xb6, 0x50,
  149. 0x0b, 0xe3, 0x53, 0x9c, 0x12, 0x56, 0xf1, 0xb7,
  150. 0x83, 0xd5, 0xf3, 0x24, 0x81, 0xcc, 0x5a, 0xeb,
  151. 0xec, 0xac, 0x68, 0xa8, 0x0c, 0xd7, 0x84, 0x7a,
  152. 0xbb, 0x77, 0x7b, 0xd5, 0x5b, 0xcf, 0x7b, 0x25,
  153. 0xd0, 0x75, 0x80, 0x21, 0x12, 0x97, 0x6b, 0xe1,
  154. 0xb6, 0x51, 0x12, 0x52, 0x6e, 0x01, 0x92, 0xb7,
  155. 0xcc, 0x70, 0x4b, 0x46, 0x11, 0x98, 0x5a, 0x84,
  156. 0x1c, 0x90, 0x45, 0x0f, 0x15, 0x77, 0xdb, 0x79,
  157. 0xe8, 0xff, 0x1f, 0xaa, 0x58, 0x95, 0xce, 0x3c,
  158. 0x65, 0x0c, 0x66, 0x29, 0xe1, 0x9c, 0x41, 0xbb,
  159. 0xde, 0x65, 0xb8, 0x29, 0x36, 0x94, 0xbd, 0x87,
  160. 0x93, 0x39, 0xc5, 0xeb, 0x49, 0x21, 0xc1, 0xeb,
  161. 0x48, 0xbd, 0x19, 0x13, 0x4d, 0x40, 0x90, 0x88,
  162. 0xc6, 0x12, 0xd9, 0xf7, 0xdd, 0xc8, 0x4f, 0x89,
  163. 0xc0, 0x91, 0xf8, 0xeb, 0xcf, 0xe3, 0x12, 0x17,
  164. 0x88, 0x9c, 0x88, 0xf4, 0xf5, 0xae, 0xf4, 0x15,
  165. 0xfe, 0x17, 0xf6, 0xa4, 0x74, 0x49, 0x02, 0x05,
  166. 0x11, 0x3b, 0x92, 0x25, 0x39, 0x2c, 0x4b, 0x08,
  167. 0x19, 0x76, 0x13, 0x8d, 0xf9, 0xda, 0xae, 0xdf,
  168. 0x30, 0xda, 0xcc, 0xbb, 0x3f, 0xb9, 0xb0, 0xd6,
  169. 0x5c, 0x78, 0x4b, 0x2b, 0x35, 0x51, 0x17, 0x48,
  170. 0xf5, 0xd4, 0x39, 0x7e, 0x05, 0x83, 0x68, 0x86,
  171. 0x44, 0x5f, 0x56, 0x1d, 0x2c, 0x53, 0xd3, 0x64,
  172. 0x3a, 0xb2, 0x0c, 0x4a, 0x85, 0xd6, 0x5b, 0x7e,
  173. 0xf9, 0xe9, 0x50, 0x29, 0x5d, 0x4f, 0xcc, 0xc9,
  174. };
  175. _Static_assert(sizeof(pki_rsa2048_output) == 2048/8, "rsa2048 output is wrong size");
  176. _Static_assert(sizeof(pki_rsa4096_output) == 4096/8, "rsa4096 output is wrong size");
  177. static void test_cert(const char *cert, const uint8_t *expected_output, size_t output_len);
  178. TEST_CASE("mbedtls RSA4096 cert", "[mbedtls]")
  179. {
  180. test_cert(rsa4096_cert, pki_rsa4096_output, 4096/8);
  181. }
  182. TEST_CASE("mbedtls RSA2048 cert", "[mbedtls]")
  183. {
  184. test_cert(rsa2048_cert, pki_rsa2048_output, 2048/8);
  185. }
  186. static void test_cert(const char *cert, const uint8_t *expected_output, size_t output_len)
  187. {
  188. mbedtls_x509_crt crt;
  189. mbedtls_rsa_context *rsa;
  190. char buf[output_len];
  191. bzero(buf, output_len);
  192. mbedtls_x509_crt_init(&crt);
  193. TEST_ASSERT_EQUAL_HEX16_MESSAGE(0,
  194. -mbedtls_x509_crt_parse(&crt,
  195. (const uint8_t *)cert,
  196. strlen(cert)+1),
  197. "parse cert");
  198. rsa = mbedtls_pk_rsa(crt.pk);
  199. TEST_ASSERT_NOT_NULL(rsa);
  200. TEST_ASSERT_EQUAL_HEX16_MESSAGE(0,
  201. -mbedtls_rsa_check_pubkey(rsa),
  202. "check cert pubkey");
  203. mbedtls_x509_crt_info(buf, sizeof(buf), "", &crt);
  204. puts(buf);
  205. TEST_ASSERT_EQUAL_HEX16_MESSAGE(0,
  206. -mbedtls_rsa_public(rsa, pki_input, (uint8_t *)buf),
  207. "RSA PK operation");
  208. /*
  209. // Dump buffer for debugging
  210. for(int i = 0; i < output_len; i++) {
  211. printf("0x%02x, ", buf[i]);
  212. }
  213. printf("\n");
  214. */
  215. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_output, buf, output_len);
  216. mbedtls_x509_crt_free(&crt);
  217. }