blufi_security.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/event_groups.h"
  12. #include "esp_system.h"
  13. #include "esp_wifi.h"
  14. #include "esp_event.h"
  15. #include "esp_log.h"
  16. #include "nvs_flash.h"
  17. #include "esp_random.h"
  18. #include "esp_bt.h"
  19. #include "esp_blufi_api.h"
  20. #include "blufi_example.h"
  21. #include "mbedtls/aes.h"
  22. #include "mbedtls/dhm.h"
  23. #include "mbedtls/md5.h"
  24. #include "esp_crc.h"
  25. /*
  26. The SEC_TYPE_xxx is for self-defined packet data type in the procedure of "BLUFI negotiate key"
  27. If user use other negotiation procedure to exchange(or generate) key, should redefine the type by yourself.
  28. */
  29. #define SEC_TYPE_DH_PARAM_LEN 0x00
  30. #define SEC_TYPE_DH_PARAM_DATA 0x01
  31. #define SEC_TYPE_DH_P 0x02
  32. #define SEC_TYPE_DH_G 0x03
  33. #define SEC_TYPE_DH_PUBLIC 0x04
  34. struct blufi_security {
  35. #define DH_SELF_PUB_KEY_LEN 128
  36. #define DH_SELF_PUB_KEY_BIT_LEN (DH_SELF_PUB_KEY_LEN * 8)
  37. uint8_t self_public_key[DH_SELF_PUB_KEY_LEN];
  38. #define SHARE_KEY_LEN 128
  39. #define SHARE_KEY_BIT_LEN (SHARE_KEY_LEN * 8)
  40. uint8_t share_key[SHARE_KEY_LEN];
  41. size_t share_len;
  42. #define PSK_LEN 16
  43. uint8_t psk[PSK_LEN];
  44. uint8_t *dh_param;
  45. int dh_param_len;
  46. uint8_t iv[16];
  47. mbedtls_dhm_context dhm;
  48. mbedtls_aes_context aes;
  49. };
  50. static struct blufi_security *blufi_sec;
  51. static int myrand( void *rng_state, unsigned char *output, size_t len )
  52. {
  53. esp_fill_random(output, len);
  54. return( 0 );
  55. }
  56. extern void btc_blufi_report_error(esp_blufi_error_state_t state);
  57. void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free)
  58. {
  59. int ret;
  60. uint8_t type = data[0];
  61. if (blufi_sec == NULL) {
  62. BLUFI_ERROR("BLUFI Security is not initialized");
  63. btc_blufi_report_error(ESP_BLUFI_INIT_SECURITY_ERROR);
  64. return;
  65. }
  66. switch (type) {
  67. case SEC_TYPE_DH_PARAM_LEN:
  68. blufi_sec->dh_param_len = ((data[1]<<8)|data[2]);
  69. if (blufi_sec->dh_param) {
  70. free(blufi_sec->dh_param);
  71. blufi_sec->dh_param = NULL;
  72. }
  73. blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len);
  74. if (blufi_sec->dh_param == NULL) {
  75. btc_blufi_report_error(ESP_BLUFI_DH_MALLOC_ERROR);
  76. BLUFI_ERROR("%s, malloc failed\n", __func__);
  77. return;
  78. }
  79. break;
  80. case SEC_TYPE_DH_PARAM_DATA:{
  81. if (blufi_sec->dh_param == NULL) {
  82. BLUFI_ERROR("%s, blufi_sec->dh_param == NULL\n", __func__);
  83. btc_blufi_report_error(ESP_BLUFI_DH_PARAM_ERROR);
  84. return;
  85. }
  86. uint8_t *param = blufi_sec->dh_param;
  87. memcpy(blufi_sec->dh_param, &data[1], blufi_sec->dh_param_len);
  88. ret = mbedtls_dhm_read_params(&blufi_sec->dhm, &param, &param[blufi_sec->dh_param_len]);
  89. if (ret) {
  90. BLUFI_ERROR("%s read param failed %d\n", __func__, ret);
  91. btc_blufi_report_error(ESP_BLUFI_READ_PARAM_ERROR);
  92. return;
  93. }
  94. free(blufi_sec->dh_param);
  95. blufi_sec->dh_param = NULL;
  96. const int dhm_len = mbedtls_dhm_get_len(&blufi_sec->dhm);
  97. ret = mbedtls_dhm_make_public(&blufi_sec->dhm, dhm_len, blufi_sec->self_public_key, dhm_len, myrand, NULL);
  98. if (ret) {
  99. BLUFI_ERROR("%s make public failed %d\n", __func__, ret);
  100. btc_blufi_report_error(ESP_BLUFI_MAKE_PUBLIC_ERROR);
  101. return;
  102. }
  103. ret = mbedtls_dhm_calc_secret( &blufi_sec->dhm,
  104. blufi_sec->share_key,
  105. SHARE_KEY_BIT_LEN,
  106. &blufi_sec->share_len,
  107. myrand, NULL);
  108. if (ret) {
  109. BLUFI_ERROR("%s mbedtls_dhm_calc_secret failed %d\n", __func__, ret);
  110. btc_blufi_report_error(ESP_BLUFI_DH_PARAM_ERROR);
  111. return;
  112. }
  113. ret = mbedtls_md5(blufi_sec->share_key, blufi_sec->share_len, blufi_sec->psk);
  114. if (ret) {
  115. BLUFI_ERROR("%s mbedtls_md5 failed %d\n", __func__, ret);
  116. btc_blufi_report_error(ESP_BLUFI_CALC_MD5_ERROR);
  117. return;
  118. }
  119. mbedtls_aes_setkey_enc(&blufi_sec->aes, blufi_sec->psk, 128);
  120. /* alloc output data */
  121. *output_data = &blufi_sec->self_public_key[0];
  122. *output_len = dhm_len;
  123. *need_free = false;
  124. }
  125. break;
  126. case SEC_TYPE_DH_P:
  127. break;
  128. case SEC_TYPE_DH_G:
  129. break;
  130. case SEC_TYPE_DH_PUBLIC:
  131. break;
  132. }
  133. }
  134. int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len)
  135. {
  136. int ret;
  137. size_t iv_offset = 0;
  138. uint8_t iv0[16];
  139. memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv));
  140. iv0[0] = iv8; /* set iv8 as the iv0[0] */
  141. ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_ENCRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data);
  142. if (ret) {
  143. return -1;
  144. }
  145. return crypt_len;
  146. }
  147. int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len)
  148. {
  149. int ret;
  150. size_t iv_offset = 0;
  151. uint8_t iv0[16];
  152. memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv));
  153. iv0[0] = iv8; /* set iv8 as the iv0[0] */
  154. ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_DECRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data);
  155. if (ret) {
  156. return -1;
  157. }
  158. return crypt_len;
  159. }
  160. uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len)
  161. {
  162. /* This iv8 ignore, not used */
  163. return esp_crc16_be(0, data, len);
  164. }
  165. esp_err_t blufi_security_init(void)
  166. {
  167. blufi_sec = (struct blufi_security *)malloc(sizeof(struct blufi_security));
  168. if (blufi_sec == NULL) {
  169. return ESP_FAIL;
  170. }
  171. memset(blufi_sec, 0x0, sizeof(struct blufi_security));
  172. mbedtls_dhm_init(&blufi_sec->dhm);
  173. mbedtls_aes_init(&blufi_sec->aes);
  174. memset(blufi_sec->iv, 0x0, 16);
  175. return 0;
  176. }
  177. void blufi_security_deinit(void)
  178. {
  179. if (blufi_sec == NULL) {
  180. return;
  181. }
  182. if (blufi_sec->dh_param){
  183. free(blufi_sec->dh_param);
  184. blufi_sec->dh_param = NULL;
  185. }
  186. mbedtls_dhm_free(&blufi_sec->dhm);
  187. mbedtls_aes_free(&blufi_sec->aes);
  188. memset(blufi_sec, 0x0, sizeof(struct blufi_security));
  189. free(blufi_sec);
  190. blufi_sec = NULL;
  191. }