test_ecp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* mbedTLS Elliptic Curve functionality tests
  2. *
  3. * Focus on testing functionality where we use ESP32 hardware
  4. * accelerated crypto features.
  5. *
  6. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. */
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdbool.h>
  13. #include <esp_random.h>
  14. /* ToDo - Remove this once appropriate solution is available.
  15. We need to define this for the file as ssl_misc.h uses private structures from mbedtls,
  16. which are undefined if the following flag is not defined */
  17. /* Many APIs in the file make use of this flag instead of `MBEDTLS_PRIVATE` */
  18. /* ToDo - Replace them with proper getter-setter once they are added */
  19. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  20. #include <mbedtls/entropy.h>
  21. #include <mbedtls/ctr_drbg.h>
  22. #include <mbedtls/ecdh.h>
  23. #include <mbedtls/ecdsa.h>
  24. #include <mbedtls/error.h>
  25. #include "unity.h"
  26. /* Note: negative value here so that assert message prints a grep-able
  27. error hex value (mbedTLS uses -N for error codes) */
  28. #define TEST_ASSERT_MBEDTLS_OK(X) TEST_ASSERT_EQUAL_HEX32(0, -(X))
  29. TEST_CASE("mbedtls ECDH Generate Key", "[mbedtls]")
  30. {
  31. mbedtls_ecdh_context ctx;
  32. mbedtls_entropy_context entropy;
  33. mbedtls_ctr_drbg_context ctr_drbg;
  34. mbedtls_ecdh_init(&ctx);
  35. mbedtls_ctr_drbg_init(&ctr_drbg);
  36. mbedtls_entropy_init(&entropy);
  37. TEST_ASSERT_MBEDTLS_OK( mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) );
  38. TEST_ASSERT_MBEDTLS_OK( mbedtls_ecp_group_load(&ctx.ctx.mbed_ecdh.grp, MBEDTLS_ECP_DP_CURVE25519) );
  39. TEST_ASSERT_MBEDTLS_OK( mbedtls_ecdh_gen_public(&ctx.ctx.mbed_ecdh.grp, &ctx.ctx.mbed_ecdh.d, &ctx.ctx.mbed_ecdh.Q,
  40. mbedtls_ctr_drbg_random, &ctr_drbg ) );
  41. mbedtls_ecdh_free(&ctx);
  42. mbedtls_ctr_drbg_free(&ctr_drbg);
  43. mbedtls_entropy_free(&entropy);
  44. }
  45. TEST_CASE("mbedtls ECP self-tests", "[mbedtls]")
  46. {
  47. TEST_ASSERT_EQUAL(0, mbedtls_ecp_self_test(1));
  48. }
  49. TEST_CASE("mbedtls ECP mul w/ koblitz", "[mbedtls]")
  50. {
  51. /* Test case code via https://github.com/espressif/esp-idf/issues/1556 */
  52. mbedtls_entropy_context ctxEntropy;
  53. mbedtls_ctr_drbg_context ctxRandom;
  54. mbedtls_ecdsa_context ctxECDSA;
  55. const char* pers = "myecdsa";
  56. mbedtls_entropy_init(&ctxEntropy);
  57. mbedtls_ctr_drbg_init(&ctxRandom);
  58. TEST_ASSERT_MBEDTLS_OK( mbedtls_ctr_drbg_seed(&ctxRandom, mbedtls_entropy_func, &ctxEntropy,
  59. (const unsigned char*) pers, strlen(pers)) );
  60. mbedtls_ecdsa_init(&ctxECDSA);
  61. TEST_ASSERT_MBEDTLS_OK( mbedtls_ecdsa_genkey(&ctxECDSA, MBEDTLS_ECP_DP_SECP256K1,
  62. mbedtls_ctr_drbg_random, &ctxRandom) );
  63. TEST_ASSERT_MBEDTLS_OK(mbedtls_ecp_mul(&ctxECDSA.grp, &ctxECDSA.Q, &ctxECDSA.d, &ctxECDSA.grp.G,
  64. mbedtls_ctr_drbg_random, &ctxRandom) );
  65. mbedtls_ecdsa_free(&ctxECDSA);
  66. mbedtls_ctr_drbg_free(&ctxRandom);
  67. mbedtls_entropy_free(&ctxEntropy);
  68. }
  69. #if CONFIG_MBEDTLS_HARDWARE_ECC
  70. /*
  71. * Coordinates and integers stored in big endian format
  72. */
  73. const uint8_t ecc_p192_point_x[] = {
  74. 0x18, 0x8D, 0xA8, 0x0E, 0xB0, 0x30, 0x90, 0xF6,
  75. 0x7C, 0xBF, 0x20, 0xEB, 0x43, 0xA1, 0x88, 0x00,
  76. 0xF4, 0xFF, 0x0A, 0xFD, 0x82, 0xFF, 0x10, 0x12
  77. };
  78. const uint8_t ecc_p192_point_y[] = {
  79. 0x07, 0x19, 0x2B, 0x95, 0xFF, 0xC8, 0xDA, 0x78,
  80. 0x63, 0x10, 0x11, 0xED, 0x6B, 0x24, 0xCD, 0xD5,
  81. 0x73, 0xF9, 0x77, 0xA1, 0x1E, 0x79, 0x48, 0x11
  82. };
  83. const uint8_t ecc_p192_scalar[] = {
  84. 0x6f, 0x18, 0x34, 0xeb, 0x16, 0xb7, 0xac, 0x9f,
  85. 0x3c, 0x77, 0x71, 0xb3, 0x02, 0x30, 0x70, 0x48,
  86. 0x75, 0x87, 0xbb, 0x6f, 0x80, 0x34, 0x8d, 0x5e
  87. };
  88. const uint8_t ecc_p192_mul_res_x[] = {
  89. 0x3F, 0xEE, 0x6F, 0x1F, 0x99, 0xDC, 0xCB, 0x78,
  90. 0xB7, 0x47, 0x1C, 0x2A, 0xF5, 0xA0, 0xAC, 0xE6,
  91. 0xEC, 0x24, 0x82, 0x37, 0x6C, 0xC0, 0x27, 0xC5,
  92. };
  93. const uint8_t ecc_p192_mul_res_y[] = {
  94. 0xDF, 0xF3, 0x9E, 0x76, 0x24, 0xF4, 0xF6, 0xB4,
  95. 0xF0, 0x0A, 0x18, 0xE1, 0x0B, 0xD2, 0xD9, 0x83,
  96. 0xE8, 0x29, 0x5E, 0xD9, 0x46, 0x54, 0xC3, 0xE1
  97. };
  98. const uint8_t ecc_p256_point_x[] = {
  99. 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
  100. 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
  101. 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
  102. 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96
  103. };
  104. const uint8_t ecc_p256_point_y[] = {
  105. 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
  106. 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
  107. 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
  108. 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5
  109. };
  110. const uint8_t ecc_p256_scalar[] = {
  111. 0xB2, 0xC5, 0x9E, 0x92, 0x64, 0xCD, 0x5F, 0x66,
  112. 0x9E, 0xC8, 0x83, 0x6D, 0x99, 0x61, 0x18, 0x72,
  113. 0xC8, 0x60, 0x83, 0x1E, 0xE5, 0x79, 0xCC, 0x73,
  114. 0xA9, 0xB4, 0x74, 0x85, 0x70, 0x11, 0x2D, 0xA2,
  115. };
  116. const uint8_t ecc_p256_mul_res_x[] = {
  117. 0x26, 0x1A, 0x0F, 0xBD, 0xA5, 0xE5, 0x1E, 0xE7,
  118. 0xB3, 0xC3, 0xB7, 0x09, 0xD1, 0x4A, 0x7A, 0x2A,
  119. 0x16, 0x69, 0x4B, 0xAF, 0x76, 0x5C, 0xD4, 0x0E,
  120. 0x93, 0x57, 0xB8, 0x67, 0xF9, 0xA1, 0xE5, 0xE8
  121. };
  122. const uint8_t ecc_p256_mul_res_y[] = {
  123. 0xA0, 0xF4, 0x2E, 0x62, 0x36, 0x25, 0x9F, 0xE0,
  124. 0xF2, 0xA0, 0x41, 0x42, 0xD2, 0x95, 0x89, 0x41,
  125. 0x38, 0xF0, 0xEB, 0x6E, 0xA7, 0x96, 0x29, 0x24,
  126. 0xC7, 0xD4, 0x0C, 0x90, 0xA1, 0xC9, 0xD3, 0x3A
  127. };
  128. static int rng_wrapper(void *ctx, unsigned char *buf, size_t len)
  129. {
  130. esp_fill_random(buf, len);
  131. return 0;
  132. }
  133. static void test_ecp_mul(mbedtls_ecp_group_id id, const uint8_t *x_coord, const uint8_t *y_coord, const uint8_t *scalar,
  134. const uint8_t *result_x_coord, const uint8_t *result_y_coord)
  135. {
  136. uint8_t x[32];
  137. uint8_t y[32];
  138. int size;
  139. int ret;
  140. mbedtls_ecp_group grp;
  141. mbedtls_ecp_point R;
  142. mbedtls_ecp_point P;
  143. mbedtls_mpi m;
  144. mbedtls_ecp_group_init(&grp);
  145. mbedtls_ecp_point_init(&R);
  146. mbedtls_ecp_point_init(&P);
  147. mbedtls_mpi_init(&m);
  148. mbedtls_ecp_group_load(&grp, id);
  149. size = grp.pbits / 8;
  150. mbedtls_mpi_read_binary(&m, scalar, size);
  151. mbedtls_mpi_read_binary(&P.X, x_coord, size);
  152. mbedtls_mpi_read_binary(&P.Y, y_coord, size);
  153. mbedtls_mpi_lset(&P.Z, 1);
  154. ret = mbedtls_ecp_mul(&grp, &R, &m, &P, rng_wrapper, NULL);
  155. TEST_ASSERT_EQUAL(0, ret);
  156. mbedtls_mpi_write_binary(&R.X, x, mbedtls_mpi_size(&R.X));
  157. mbedtls_mpi_write_binary(&R.Y, y, mbedtls_mpi_size(&R.Y));
  158. TEST_ASSERT_EQUAL(0, memcmp(x, result_x_coord, mbedtls_mpi_size(&R.X)));
  159. TEST_ASSERT_EQUAL(0, memcmp(y, result_y_coord, mbedtls_mpi_size(&R.Y)));
  160. mbedtls_ecp_point_free(&R);
  161. mbedtls_ecp_point_free(&P);
  162. mbedtls_mpi_free(&m);
  163. mbedtls_ecp_group_free(&grp);
  164. }
  165. TEST_CASE("mbedtls ECP point multiply with SECP192R1", "[mbedtls]")
  166. {
  167. test_ecp_mul(MBEDTLS_ECP_DP_SECP192R1, ecc_p192_point_x, ecc_p192_point_y, ecc_p192_scalar,
  168. ecc_p192_mul_res_x, ecc_p192_mul_res_y);
  169. }
  170. TEST_CASE("mbedtls ECP point multiply with SECP256R1", "[mbedtls]")
  171. {
  172. test_ecp_mul(MBEDTLS_ECP_DP_SECP256R1, ecc_p256_point_x, ecc_p256_point_y, ecc_p256_scalar,
  173. ecc_p256_mul_res_x, ecc_p256_mul_res_y);
  174. }
  175. static void test_ecp_verify(mbedtls_ecp_group_id id, const uint8_t *x_coord, const uint8_t *y_coord)
  176. {
  177. int size;
  178. int ret;
  179. mbedtls_ecp_group grp;
  180. mbedtls_ecp_point P;
  181. mbedtls_ecp_group_init(&grp);
  182. mbedtls_ecp_point_init(&P);
  183. mbedtls_ecp_group_load(&grp, id);
  184. size = grp.pbits / 8;
  185. mbedtls_mpi_read_binary(&P.X, x_coord, size);
  186. mbedtls_mpi_read_binary(&P.Y, y_coord, size);
  187. mbedtls_mpi_lset(&P.Z, 1);
  188. ret = mbedtls_ecp_check_pubkey(&grp, &P);
  189. TEST_ASSERT_EQUAL(0, ret);
  190. mbedtls_ecp_point_free(&P);
  191. mbedtls_ecp_group_free(&grp);
  192. }
  193. TEST_CASE("mbedtls ECP point verify with SECP192R1", "[mbedtls]")
  194. {
  195. test_ecp_verify(MBEDTLS_ECP_DP_SECP192R1, ecc_p192_mul_res_x, ecc_p192_mul_res_y);
  196. }
  197. TEST_CASE("mbedtls ECP point verify with SECP256R1", "[mbedtls]")
  198. {
  199. test_ecp_verify(MBEDTLS_ECP_DP_SECP256R1, ecc_p256_mul_res_x, ecc_p256_mul_res_y);
  200. }
  201. #endif /* CONFIG_MBEDTLS_HARDWARE_ECC */