test_ecp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <inttypes.h>
  14. #include <esp_random.h>
  15. #include <mbedtls/entropy.h>
  16. #include <mbedtls/ctr_drbg.h>
  17. #include <mbedtls/ecdh.h>
  18. #include <mbedtls/ecdsa.h>
  19. #include <mbedtls/error.h>
  20. #include "test_utils.h"
  21. #include "ccomp_timer.h"
  22. #include "unity.h"
  23. /* Note: negative value here so that assert message prints a grep-able
  24. error hex value (mbedTLS uses -N for error codes) */
  25. #define TEST_ASSERT_MBEDTLS_OK(X) TEST_ASSERT_EQUAL_HEX32(0, -(X))
  26. /* TODO: Currently MBEDTLS_ECDH_LEGACY_CONTEXT is enabled by default
  27. * when MBEDTLS_ECP_RESTARTABLE is enabled.
  28. * This is a temporary workaround to allow that.
  29. *
  30. * The legacy option is soon going to be removed in future mbedtls
  31. * versions and this workaround will be removed once the appropriate
  32. * solution is available.
  33. */
  34. #ifdef CONFIG_MBEDTLS_ECDH_LEGACY_CONTEXT
  35. #define ACCESS_ECDH(S, var) S.MBEDTLS_PRIVATE(var)
  36. #else
  37. #define ACCESS_ECDH(S, var) S.MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(mbed_ecdh).MBEDTLS_PRIVATE(var)
  38. #endif
  39. #if CONFIG_NEWLIB_NANO_FORMAT
  40. #define NEWLIB_NANO_COMPAT_FORMAT PRIu32
  41. #define NEWLIB_NANO_COMPAT_CAST(int64_t_var) (uint32_t)int64_t_var
  42. #else
  43. #define NEWLIB_NANO_COMPAT_FORMAT PRId64
  44. #define NEWLIB_NANO_COMPAT_CAST(int64_t_var) int64_t_var
  45. #endif
  46. TEST_CASE("mbedtls ECDH Generate Key", "[mbedtls]")
  47. {
  48. mbedtls_ecdh_context ctx;
  49. mbedtls_entropy_context entropy;
  50. mbedtls_ctr_drbg_context ctr_drbg;
  51. mbedtls_ecdh_init(&ctx);
  52. mbedtls_ctr_drbg_init(&ctr_drbg);
  53. mbedtls_entropy_init(&entropy);
  54. TEST_ASSERT_MBEDTLS_OK( mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) );
  55. TEST_ASSERT_MBEDTLS_OK( mbedtls_ecp_group_load(ACCESS_ECDH(&ctx, grp), MBEDTLS_ECP_DP_CURVE25519) );
  56. TEST_ASSERT_MBEDTLS_OK( mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q),
  57. mbedtls_ctr_drbg_random, &ctr_drbg ) );
  58. mbedtls_ecdh_free(&ctx);
  59. mbedtls_ctr_drbg_free(&ctr_drbg);
  60. mbedtls_entropy_free(&entropy);
  61. }
  62. TEST_CASE("mbedtls ECP self-tests", "[mbedtls]")
  63. {
  64. TEST_ASSERT_EQUAL(0, mbedtls_ecp_self_test(1));
  65. }
  66. TEST_CASE("mbedtls ECP mul w/ koblitz", "[mbedtls]")
  67. {
  68. /* Test case code via https://github.com/espressif/esp-idf/issues/1556 */
  69. mbedtls_entropy_context ctxEntropy;
  70. mbedtls_ctr_drbg_context ctxRandom;
  71. mbedtls_ecdsa_context ctxECDSA;
  72. const char* pers = "myecdsa";
  73. mbedtls_entropy_init(&ctxEntropy);
  74. mbedtls_ctr_drbg_init(&ctxRandom);
  75. TEST_ASSERT_MBEDTLS_OK( mbedtls_ctr_drbg_seed(&ctxRandom, mbedtls_entropy_func, &ctxEntropy,
  76. (const unsigned char*) pers, strlen(pers)) );
  77. mbedtls_ecdsa_init(&ctxECDSA);
  78. TEST_ASSERT_MBEDTLS_OK( mbedtls_ecdsa_genkey(&ctxECDSA, MBEDTLS_ECP_DP_SECP256K1,
  79. mbedtls_ctr_drbg_random, &ctxRandom) );
  80. TEST_ASSERT_MBEDTLS_OK(mbedtls_ecp_mul(&ctxECDSA.MBEDTLS_PRIVATE(grp), &ctxECDSA.MBEDTLS_PRIVATE(Q),
  81. &ctxECDSA.MBEDTLS_PRIVATE(d), &ctxECDSA.MBEDTLS_PRIVATE(grp).G,
  82. mbedtls_ctr_drbg_random, &ctxRandom) );
  83. mbedtls_ecdsa_free(&ctxECDSA);
  84. mbedtls_ctr_drbg_free(&ctxRandom);
  85. mbedtls_entropy_free(&ctxEntropy);
  86. }
  87. #if CONFIG_MBEDTLS_HARDWARE_ECC
  88. #define SMALL_SCALAR 127
  89. /*
  90. * Coordinates and integers stored in big endian format
  91. */
  92. const uint8_t ecc_p192_point_x[] = {
  93. 0x18, 0x8D, 0xA8, 0x0E, 0xB0, 0x30, 0x90, 0xF6,
  94. 0x7C, 0xBF, 0x20, 0xEB, 0x43, 0xA1, 0x88, 0x00,
  95. 0xF4, 0xFF, 0x0A, 0xFD, 0x82, 0xFF, 0x10, 0x12
  96. };
  97. const uint8_t ecc_p192_point_y[] = {
  98. 0x07, 0x19, 0x2B, 0x95, 0xFF, 0xC8, 0xDA, 0x78,
  99. 0x63, 0x10, 0x11, 0xED, 0x6B, 0x24, 0xCD, 0xD5,
  100. 0x73, 0xF9, 0x77, 0xA1, 0x1E, 0x79, 0x48, 0x11
  101. };
  102. const uint8_t ecc_p192_scalar[] = {
  103. 0x6f, 0x18, 0x34, 0xeb, 0x16, 0xb7, 0xac, 0x9f,
  104. 0x3c, 0x77, 0x71, 0xb3, 0x02, 0x30, 0x70, 0x48,
  105. 0x75, 0x87, 0xbb, 0x6f, 0x80, 0x34, 0x8d, 0x5e
  106. };
  107. const uint8_t ecc_p192_mul_res_x[] = {
  108. 0x3F, 0xEE, 0x6F, 0x1F, 0x99, 0xDC, 0xCB, 0x78,
  109. 0xB7, 0x47, 0x1C, 0x2A, 0xF5, 0xA0, 0xAC, 0xE6,
  110. 0xEC, 0x24, 0x82, 0x37, 0x6C, 0xC0, 0x27, 0xC5,
  111. };
  112. const uint8_t ecc_p192_mul_res_y[] = {
  113. 0xDF, 0xF3, 0x9E, 0x76, 0x24, 0xF4, 0xF6, 0xB4,
  114. 0xF0, 0x0A, 0x18, 0xE1, 0x0B, 0xD2, 0xD9, 0x83,
  115. 0xE8, 0x29, 0x5E, 0xD9, 0x46, 0x54, 0xC3, 0xE1
  116. };
  117. const uint8_t ecc_p192_small_mul_res_x[] = {
  118. 0x62, 0xBF, 0x33, 0xC1, 0x75, 0xB5, 0xEB, 0x1D,
  119. 0xBE, 0xC7, 0x15, 0x04, 0x03, 0xA7, 0xDD, 0x9D,
  120. 0x0B, 0x17, 0x9D, 0x3B, 0x06, 0x63, 0xFE, 0xD3
  121. };
  122. const uint8_t ecc_p192_small_mul_res_y[] = {
  123. 0xD4, 0xE9, 0x4E, 0x4D, 0x89, 0x4D, 0xB5, 0x99,
  124. 0x8A, 0xE1, 0x85, 0x81, 0x27, 0x38, 0x23, 0x32,
  125. 0x92, 0xCF, 0xE8, 0x38, 0xCA, 0x39, 0xF2, 0xE1
  126. };
  127. const uint8_t ecc_p256_point_x[] = {
  128. 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
  129. 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
  130. 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
  131. 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96
  132. };
  133. const uint8_t ecc_p256_point_y[] = {
  134. 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
  135. 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
  136. 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
  137. 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5
  138. };
  139. const uint8_t ecc_p256_scalar[] = {
  140. 0xB2, 0xC5, 0x9E, 0x92, 0x64, 0xCD, 0x5F, 0x66,
  141. 0x9E, 0xC8, 0x83, 0x6D, 0x99, 0x61, 0x18, 0x72,
  142. 0xC8, 0x60, 0x83, 0x1E, 0xE5, 0x79, 0xCC, 0x73,
  143. 0xA9, 0xB4, 0x74, 0x85, 0x70, 0x11, 0x2D, 0xA2,
  144. };
  145. const uint8_t ecc_p256_mul_res_x[] = {
  146. 0x26, 0x1A, 0x0F, 0xBD, 0xA5, 0xE5, 0x1E, 0xE7,
  147. 0xB3, 0xC3, 0xB7, 0x09, 0xD1, 0x4A, 0x7A, 0x2A,
  148. 0x16, 0x69, 0x4B, 0xAF, 0x76, 0x5C, 0xD4, 0x0E,
  149. 0x93, 0x57, 0xB8, 0x67, 0xF9, 0xA1, 0xE5, 0xE8
  150. };
  151. const uint8_t ecc_p256_mul_res_y[] = {
  152. 0xA0, 0xF4, 0x2E, 0x62, 0x36, 0x25, 0x9F, 0xE0,
  153. 0xF2, 0xA0, 0x41, 0x42, 0xD2, 0x95, 0x89, 0x41,
  154. 0x38, 0xF0, 0xEB, 0x6E, 0xA7, 0x96, 0x29, 0x24,
  155. 0xC7, 0xD4, 0x0C, 0x90, 0xA1, 0xC9, 0xD3, 0x3A
  156. };
  157. const uint8_t ecc_p256_small_mul_res_x[] = {
  158. 0x53, 0x4D, 0x45, 0xDB, 0x6B, 0xAC, 0xA8, 0xE2,
  159. 0xD2, 0xA5, 0xD0, 0xA7, 0x65, 0xF1, 0x60, 0x13,
  160. 0xA8, 0xD4, 0xEB, 0x58, 0xC6, 0xAA, 0xAD, 0x35,
  161. 0x67, 0xCE, 0xBD, 0xFA, 0xC4, 0x2D, 0x62, 0x3C
  162. };
  163. const uint8_t ecc_p256_small_mul_res_y[] = {
  164. 0xFA, 0xD6, 0x69, 0xC8, 0x9A, 0x2A, 0x54, 0xE4,
  165. 0x41, 0x54, 0x35, 0x7F, 0x99, 0x2C, 0xCE, 0xC8,
  166. 0xEE, 0xF0, 0x93, 0xE0, 0xF2, 0x3A, 0x63, 0x1D,
  167. 0x17, 0xFD, 0xF6, 0x64, 0x41, 0x9E, 0x50, 0x0C
  168. };
  169. static int rng_wrapper(void *ctx, unsigned char *buf, size_t len)
  170. {
  171. esp_fill_random(buf, len);
  172. return 0;
  173. }
  174. static void test_ecp_mul(mbedtls_ecp_group_id id, const uint8_t *x_coord, const uint8_t *y_coord, const uint8_t *scalar,
  175. const uint8_t *result_x_coord, const uint8_t *result_y_coord)
  176. {
  177. int64_t elapsed_time;
  178. uint8_t x[32];
  179. uint8_t y[32];
  180. int size;
  181. int ret;
  182. mbedtls_ecp_group grp;
  183. mbedtls_ecp_point R;
  184. mbedtls_ecp_point P;
  185. mbedtls_mpi m;
  186. mbedtls_ecp_group_init(&grp);
  187. mbedtls_ecp_point_init(&R);
  188. mbedtls_ecp_point_init(&P);
  189. mbedtls_mpi_init(&m);
  190. mbedtls_ecp_group_load(&grp, id);
  191. size = grp.pbits / 8;
  192. if (!scalar) {
  193. mbedtls_mpi_lset(&m, SMALL_SCALAR);
  194. } else {
  195. mbedtls_mpi_read_binary(&m, scalar, size);
  196. }
  197. mbedtls_mpi_read_binary(&P.MBEDTLS_PRIVATE(X), x_coord, size);
  198. mbedtls_mpi_read_binary(&P.MBEDTLS_PRIVATE(Y), y_coord, size);
  199. mbedtls_mpi_lset(&P.MBEDTLS_PRIVATE(Z), 1);
  200. ccomp_timer_start();
  201. ret = mbedtls_ecp_mul(&grp, &R, &m, &P, rng_wrapper, NULL);
  202. elapsed_time = ccomp_timer_stop();
  203. TEST_ASSERT_EQUAL(0, ret);
  204. mbedtls_mpi_write_binary(&R.MBEDTLS_PRIVATE(X), x, mbedtls_mpi_size(&R.MBEDTLS_PRIVATE(X)));
  205. mbedtls_mpi_write_binary(&R.MBEDTLS_PRIVATE(Y), y, mbedtls_mpi_size(&R.MBEDTLS_PRIVATE(Y)));
  206. TEST_ASSERT_EQUAL(0, memcmp(x, result_x_coord, mbedtls_mpi_size(&R.MBEDTLS_PRIVATE(X))));
  207. TEST_ASSERT_EQUAL(0, memcmp(y, result_y_coord, mbedtls_mpi_size(&R.MBEDTLS_PRIVATE(Y))));
  208. if (id == MBEDTLS_ECP_DP_SECP192R1) {
  209. TEST_PERFORMANCE_CCOMP_LESS_THAN(ECP_P192_POINT_MULTIPLY_OP, "%" NEWLIB_NANO_COMPAT_FORMAT" us", NEWLIB_NANO_COMPAT_CAST(elapsed_time));
  210. } else if (id == MBEDTLS_ECP_DP_SECP256R1) {
  211. TEST_PERFORMANCE_CCOMP_LESS_THAN(ECP_P256_POINT_MULTIPLY_OP, "%" NEWLIB_NANO_COMPAT_FORMAT" us", NEWLIB_NANO_COMPAT_CAST(elapsed_time));
  212. }
  213. mbedtls_ecp_point_free(&R);
  214. mbedtls_ecp_point_free(&P);
  215. mbedtls_mpi_free(&m);
  216. mbedtls_ecp_group_free(&grp);
  217. }
  218. TEST_CASE("mbedtls ECP point multiply with SECP192R1", "[mbedtls]")
  219. {
  220. test_ecp_mul(MBEDTLS_ECP_DP_SECP192R1, ecc_p192_point_x, ecc_p192_point_y, ecc_p192_scalar,
  221. ecc_p192_mul_res_x, ecc_p192_mul_res_y);
  222. test_ecp_mul(MBEDTLS_ECP_DP_SECP192R1, ecc_p192_point_x, ecc_p192_point_y, NULL,
  223. ecc_p192_small_mul_res_x, ecc_p192_small_mul_res_y);
  224. }
  225. TEST_CASE("mbedtls ECP point multiply with SECP256R1", "[mbedtls]")
  226. {
  227. test_ecp_mul(MBEDTLS_ECP_DP_SECP256R1, ecc_p256_point_x, ecc_p256_point_y, ecc_p256_scalar,
  228. ecc_p256_mul_res_x, ecc_p256_mul_res_y);
  229. test_ecp_mul(MBEDTLS_ECP_DP_SECP256R1, ecc_p256_point_x, ecc_p256_point_y, NULL,
  230. ecc_p256_small_mul_res_x, ecc_p256_small_mul_res_y);
  231. }
  232. static void test_ecp_verify(mbedtls_ecp_group_id id, const uint8_t *x_coord, const uint8_t *y_coord)
  233. {
  234. int64_t elapsed_time;
  235. int size;
  236. int ret;
  237. mbedtls_ecp_group grp;
  238. mbedtls_ecp_point P;
  239. mbedtls_ecp_group_init(&grp);
  240. mbedtls_ecp_point_init(&P);
  241. mbedtls_ecp_group_load(&grp, id);
  242. size = grp.pbits / 8;
  243. mbedtls_mpi_read_binary(&P.MBEDTLS_PRIVATE(X), x_coord, size);
  244. mbedtls_mpi_read_binary(&P.MBEDTLS_PRIVATE(Y), y_coord, size);
  245. mbedtls_mpi_lset(&P.MBEDTLS_PRIVATE(Z), 1);
  246. ccomp_timer_start();
  247. ret = mbedtls_ecp_check_pubkey(&grp, &P);
  248. elapsed_time = ccomp_timer_stop();
  249. TEST_ASSERT_EQUAL(0, ret);
  250. if (id == MBEDTLS_ECP_DP_SECP192R1) {
  251. TEST_PERFORMANCE_CCOMP_LESS_THAN(ECP_P192_POINT_VERIFY_OP, "%" NEWLIB_NANO_COMPAT_FORMAT" us", NEWLIB_NANO_COMPAT_CAST(elapsed_time));
  252. } else if (id == MBEDTLS_ECP_DP_SECP256R1) {
  253. TEST_PERFORMANCE_CCOMP_LESS_THAN(ECP_P256_POINT_VERIFY_OP, "%" NEWLIB_NANO_COMPAT_FORMAT" us", NEWLIB_NANO_COMPAT_CAST(elapsed_time));
  254. }
  255. mbedtls_ecp_point_free(&P);
  256. mbedtls_ecp_group_free(&grp);
  257. }
  258. TEST_CASE("mbedtls ECP point verify with SECP192R1", "[mbedtls]")
  259. {
  260. test_ecp_verify(MBEDTLS_ECP_DP_SECP192R1, ecc_p192_mul_res_x, ecc_p192_mul_res_y);
  261. }
  262. TEST_CASE("mbedtls ECP point verify with SECP256R1", "[mbedtls]")
  263. {
  264. test_ecp_verify(MBEDTLS_ECP_DP_SECP256R1, ecc_p256_mul_res_x, ecc_p256_mul_res_y);
  265. }
  266. #endif /* CONFIG_MBEDTLS_HARDWARE_ECC */