ecdsa_example_main.c 7.2 KB

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