ecdsa_example_main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * atecc608a_ecdsa example
  3. *
  4. * SPDX-FileCopyrightText: 2006-2016 ARM Limited, All Rights Reserved
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * SPDX-FileContributor: 2015-2021 Espressif Systems (Shanghai) CO LTD
  9. */
  10. /* This is mbedtls boilerplate for library configuration */
  11. #include "mbedtls/mbedtls_config.h"
  12. /* System Includes*/
  13. #include <stdio.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "esp_system.h"
  17. #include "spi_flash_mmap.h"
  18. #include "esp_log.h"
  19. /* Cryptoauthlib includes */
  20. #include "cryptoauthlib.h"
  21. #include "mbedtls/atca_mbedtls_wrap.h"
  22. /* mbedTLS includes */
  23. #include "mbedtls/platform.h"
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/ssl.h"
  26. #include "mbedtls/entropy.h"
  27. #include "mbedtls/ctr_drbg.h"
  28. #include "mbedtls/pk.h"
  29. static const char *TAG = "atecc_example";
  30. /* globals for mbedtls RNG */
  31. static mbedtls_entropy_context entropy;
  32. static mbedtls_ctr_drbg_context ctr_drbg;
  33. static int configure_mbedtls_rng(void)
  34. {
  35. int ret;
  36. const char * seed = "some random seed string";
  37. mbedtls_ctr_drbg_init(&ctr_drbg);
  38. ESP_LOGI(TAG, "Seeding the random number generator...");
  39. mbedtls_entropy_init(&entropy);
  40. ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  41. (const unsigned char *)seed, strlen(seed));
  42. if (ret != 0) {
  43. ESP_LOGI(TAG, " failed ! mbedtls_ctr_drbg_seed returned %d", ret);
  44. } else {
  45. ESP_LOGI(TAG, " ok");
  46. }
  47. return ret;
  48. }
  49. static void close_mbedtls_rng(void)
  50. {
  51. mbedtls_ctr_drbg_free(&ctr_drbg);
  52. mbedtls_entropy_free(&entropy);
  53. }
  54. /* An example hash */
  55. static unsigned char hash[32] = {
  56. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  57. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  58. };
  59. static const uint8_t public_key_x509_header[] = {
  60. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A,
  61. 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04
  62. };
  63. static void print_public_key(uint8_t *pubkey)
  64. {
  65. uint8_t buf[128];
  66. uint8_t * tmp;
  67. size_t buf_len = sizeof(buf);
  68. /* Calculate where the raw data will fit into the buffer */
  69. tmp = buf + sizeof(buf) - ATCA_PUB_KEY_SIZE - sizeof(public_key_x509_header);
  70. /* Copy the header */
  71. memcpy(tmp, public_key_x509_header, sizeof(public_key_x509_header));
  72. /* Copy the key bytes */
  73. memcpy(tmp + sizeof(public_key_x509_header), pubkey, ATCA_PUB_KEY_SIZE);
  74. /* Convert to base 64 */
  75. (void)atcab_base64encode(tmp, ATCA_PUB_KEY_SIZE + sizeof(public_key_x509_header), (char*)buf, &buf_len);
  76. /* Add a null terminator */
  77. buf[buf_len] = '\0';
  78. /* Print out the key */
  79. ESP_LOGI(TAG, "\r\n-----BEGIN PUBLIC KEY-----\r\n%s\r\n-----END PUBLIC KEY-----", buf);
  80. }
  81. static int atca_ecdsa_test(void)
  82. {
  83. mbedtls_pk_context pkey;
  84. int ret;
  85. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  86. size_t olen = 0;
  87. /* ECDSA Sign/Verify */
  88. #ifdef MBEDTLS_ECDSA_SIGN_ALT
  89. /* Convert to an mbedtls key */
  90. ESP_LOGI(TAG, " Using a hardware private key ..." );
  91. ret = atca_mbedtls_pk_init(&pkey, 0);
  92. if (ret != 0) {
  93. ESP_LOGI(TAG, " failed ! atca_mbedtls_pk_init returned %02x", ret);
  94. goto exit;
  95. }
  96. ESP_LOGI(TAG, " ok");
  97. #else
  98. ESP_LOGI(TAG, " Generating a software private key ..." );
  99. mbedtls_pk_init(&pkey);
  100. ret = mbedtls_pk_setup(&pkey,
  101. mbedtls_pk_info_from_type( MBEDTLS_PK_ECDSA ));
  102. if (ret != 0) {
  103. ESP_LOGI(TAG, " failed ! mbedtls_pk_setup returned -0x%04x", -ret );
  104. goto exit;
  105. }
  106. ret = mbedtls_ecp_gen_key( MBEDTLS_ECP_DP_SECP256R1,
  107. mbedtls_pk_ec( pkey ),
  108. mbedtls_ctr_drbg_random, &ctr_drbg );
  109. if (ret != 0) {
  110. ESP_LOGI(TAG, " failed ! mbedtls_ecp_gen_key returned -0x%04x", -ret );
  111. goto exit;
  112. }
  113. ESP_LOGI(TAG, " ok");
  114. #endif
  115. ESP_LOGI(TAG, " Generating ECDSA Signature...");
  116. ret = mbedtls_pk_sign(&pkey, MBEDTLS_MD_SHA256, hash, 0, buf, MBEDTLS_MPI_MAX_SIZE, &olen,
  117. mbedtls_ctr_drbg_random, &ctr_drbg);
  118. if (ret != 0) {
  119. ESP_LOGI(TAG, " failed ! mbedtls_pk_sign returned -0x%04x", -ret);
  120. goto exit;
  121. }
  122. ESP_LOGI(TAG, " ok");
  123. ESP_LOGI(TAG, " Verifying ECDSA Signature...");
  124. ret = mbedtls_pk_verify(&pkey, MBEDTLS_MD_SHA256, hash, 0,
  125. buf, olen);
  126. if (ret != 0) {
  127. ESP_LOGI(TAG, " failed ! mbedtls_pk_verify returned -0x%04x", -ret);
  128. goto exit;
  129. }
  130. ESP_LOGI(TAG, " ok");
  131. exit:
  132. fflush(stdout);
  133. return ret;
  134. }
  135. void app_main(void)
  136. {
  137. int ret = 0;
  138. bool lock;
  139. uint8_t buf[ATCA_ECC_CONFIG_SIZE];
  140. uint8_t pubkey[ATCA_PUB_KEY_SIZE];
  141. /* Initialize the mbedtls library */
  142. ret = configure_mbedtls_rng();
  143. #ifdef CONFIG_ATECC608A_TNG
  144. ESP_LOGI(TAG, " . Initialize the ATECC interface for Trust & GO ...");
  145. cfg_ateccx08a_i2c_default.atcai2c.address = 0x6A;
  146. #elif CONFIG_ATECC608A_TFLEX /* CONFIG_ATECC608A_TNGO */
  147. ESP_LOGI(TAG, " . Initialize the ATECC interface for TrustFlex ...");
  148. cfg_ateccx08a_i2c_default.atcai2c.address = 0x6C;
  149. #elif CONFIG_ATECC608A_TCUSTOM /* CONFIG_ATECC608A_TFLEX */
  150. ESP_LOGI(TAG, " . Initialize the ATECC interface for TrustCustom ...");
  151. /* Default slave address is same as that of TCUSTOM ATECC608A chips */
  152. #endif /* CONFIG_ATECC608A_TCUSTOM */
  153. ret = atcab_init(&cfg_ateccx08a_i2c_default);
  154. if (ret != 0) {
  155. ESP_LOGI(TAG, " failed ! atcab_init returned %02x", ret);
  156. goto exit;
  157. }
  158. ESP_LOGI(TAG, " ok");
  159. lock = 0;
  160. ESP_LOGI(TAG, " Check the data zone lock status...");
  161. ret = atcab_is_locked(LOCK_ZONE_DATA, &lock);
  162. if (ret != 0) {
  163. ESP_LOGI(TAG, " failed\n ! atcab_is_locked returned %02x", ret);
  164. goto exit;
  165. }
  166. if (lock) {
  167. ESP_LOGI(TAG, " ok: locked");
  168. } else {
  169. ESP_LOGE(TAG, "unlocked, please lock(configure) the ATECC608A chip with help of esp_cryptoauth_utility and try again");
  170. goto exit;
  171. }
  172. ESP_LOGI(TAG, " Get the device info (type)...");
  173. ret = atcab_info(buf);
  174. if (ret != 0) {
  175. ESP_LOGI(TAG, " failed\n ! atcab_info returned %02x", ret);
  176. goto exit;
  177. }
  178. ESP_LOGI(TAG, " ok: %02x %02x", buf[2], buf[3]);
  179. ESP_LOGI(TAG, " Get the public key...");
  180. ret = atcab_get_pubkey(0, pubkey);
  181. if (ret != 0) {
  182. ESP_LOGI(TAG, " failed\n ! atcab_get_pubkey returned %02x", ret);
  183. goto exit;
  184. }
  185. ESP_LOGI(TAG, " ok");
  186. print_public_key(pubkey);
  187. /* Perform a Sign/Verify Test */
  188. ret = atca_ecdsa_test();
  189. if (ret != 0) {
  190. ESP_LOGE(TAG, " ECDSA sign/verify failed");
  191. goto exit;
  192. }
  193. exit:
  194. fflush(stdout);
  195. close_mbedtls_rng();
  196. }