blufi_security.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "freertos/event_groups.h"
  19. #include "esp_system.h"
  20. #include "esp_wifi.h"
  21. #include "esp_event_loop.h"
  22. #include "esp_log.h"
  23. #include "nvs_flash.h"
  24. #include "bt.h"
  25. #include "esp_blufi_api.h"
  26. #include "esp_bt_defs.h"
  27. #include "esp_gap_ble_api.h"
  28. #include "esp_bt_main.h"
  29. #include "blufi_example.h"
  30. #include "mbedtls/aes.h"
  31. #include "mbedtls/dhm.h"
  32. #include "mbedtls/md5.h"
  33. #include "rom/crc.h"
  34. /*
  35. The SEC_TYPE_xxx is for self-defined packet data type in the procedure of "BLUFI negotiate key"
  36. If user use other negotiation procedure to exchange(or generate) key, should redefine the type by yourself.
  37. */
  38. #define SEC_TYPE_DH_PARAM_LEN 0x00
  39. #define SEC_TYPE_DH_PARAM_DATA 0x01
  40. #define SEC_TYPE_DH_P 0x02
  41. #define SEC_TYPE_DH_G 0x03
  42. #define SEC_TYPE_DH_PUBLIC 0x04
  43. struct blufi_security {
  44. #define DH_SELF_PUB_KEY_LEN 128
  45. #define DH_SELF_PUB_KEY_BIT_LEN (DH_SELF_PUB_KEY_LEN * 8)
  46. uint8_t self_public_key[DH_SELF_PUB_KEY_LEN];
  47. #define SHARE_KEY_LEN 128
  48. #define SHARE_KEY_BIT_LEN (SHARE_KEY_LEN * 8)
  49. uint8_t share_key[SHARE_KEY_LEN];
  50. size_t share_len;
  51. #define PSK_LEN 16
  52. uint8_t psk[PSK_LEN];
  53. uint8_t *dh_param;
  54. int dh_param_len;
  55. uint8_t iv[16];
  56. mbedtls_dhm_context dhm;
  57. mbedtls_aes_context aes;
  58. };
  59. static struct blufi_security *blufi_sec;
  60. static int myrand( void *rng_state, unsigned char *output, size_t len )
  61. {
  62. size_t i;
  63. for( i = 0; i < len; ++i )
  64. output[i] = esp_random();
  65. return( 0 );
  66. }
  67. void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free)
  68. {
  69. int ret;
  70. uint8_t type = data[0];
  71. if (blufi_sec == NULL) {
  72. BLUFI_ERROR("BLUFI Security is not initialized");
  73. return;
  74. }
  75. switch (type) {
  76. case SEC_TYPE_DH_PARAM_LEN:
  77. blufi_sec->dh_param_len = ((data[1]<<8)|data[2]);
  78. if (blufi_sec->dh_param) {
  79. free(blufi_sec->dh_param);
  80. }
  81. blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len);
  82. if (blufi_sec->dh_param == NULL) {
  83. return;
  84. }
  85. break;
  86. case SEC_TYPE_DH_PARAM_DATA:
  87. memcpy(blufi_sec->dh_param, &data[1], blufi_sec->dh_param_len);
  88. ret = mbedtls_dhm_read_params(&blufi_sec->dhm, &blufi_sec->dh_param, &blufi_sec->dh_param[blufi_sec->dh_param_len]);
  89. if (ret) {
  90. BLUFI_ERROR("%s read param failed %d\n", __func__, ret);
  91. return;
  92. }
  93. ret = mbedtls_dhm_make_public(&blufi_sec->dhm, (int) mbedtls_mpi_size( &blufi_sec->dhm.P ), blufi_sec->self_public_key, blufi_sec->dhm.len, myrand, NULL);
  94. if (ret) {
  95. BLUFI_ERROR("%s make public failed %d\n", __func__, ret);
  96. return;
  97. }
  98. mbedtls_dhm_calc_secret( &blufi_sec->dhm,
  99. blufi_sec->share_key,
  100. SHARE_KEY_BIT_LEN,
  101. &blufi_sec->share_len,
  102. NULL, NULL);
  103. mbedtls_md5(blufi_sec->share_key, blufi_sec->share_len, blufi_sec->psk);
  104. mbedtls_aes_setkey_enc(&blufi_sec->aes, blufi_sec->psk, 128);
  105. mbedtls_aes_setkey_dec(&blufi_sec->aes, blufi_sec->psk, 128);
  106. /* alloc output data */
  107. *output_data = &blufi_sec->self_public_key[0];
  108. *output_len = blufi_sec->dhm.len;
  109. *need_free = false;
  110. break;
  111. case SEC_TYPE_DH_P:
  112. break;
  113. case SEC_TYPE_DH_G:
  114. break;
  115. case SEC_TYPE_DH_PUBLIC:
  116. break;
  117. }
  118. }
  119. int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len)
  120. {
  121. int ret;
  122. size_t iv_offset = 0;
  123. uint8_t iv0[16];
  124. memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv));
  125. iv0[0] = iv8; /* set iv8 as the iv0[0] */
  126. ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_ENCRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data);
  127. if (ret) {
  128. return -1;
  129. }
  130. return crypt_len;
  131. }
  132. int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len)
  133. {
  134. int ret;
  135. size_t iv_offset = 0;
  136. uint8_t iv0[16];
  137. memcpy(iv0, blufi_sec->iv, sizeof(blufi_sec->iv));
  138. iv0[0] = iv8; /* set iv8 as the iv0[0] */
  139. ret = mbedtls_aes_crypt_cfb128(&blufi_sec->aes, MBEDTLS_AES_DECRYPT, crypt_len, &iv_offset, iv0, crypt_data, crypt_data);
  140. if (ret) {
  141. return -1;
  142. }
  143. return crypt_len;
  144. }
  145. uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len)
  146. {
  147. /* This iv8 ignore, not used */
  148. return crc16_be(0, data, len);
  149. }
  150. esp_err_t blufi_security_init(void)
  151. {
  152. blufi_sec = (struct blufi_security *)malloc(sizeof(struct blufi_security));
  153. if (blufi_sec == NULL) {
  154. return ESP_FAIL;
  155. }
  156. memset(blufi_sec, 0x0, sizeof(struct blufi_security));
  157. mbedtls_dhm_init(&blufi_sec->dhm);
  158. mbedtls_aes_init(&blufi_sec->aes);
  159. memset(blufi_sec->iv, 0x0, 16);
  160. return 0;
  161. }
  162. void blufi_security_deinit(void)
  163. {
  164. mbedtls_dhm_free(&blufi_sec->dhm);
  165. mbedtls_aes_free(&blufi_sec->aes);
  166. memset(blufi_sec, 0x0, sizeof(struct blufi_security));
  167. free(blufi_sec);
  168. blufi_sec = NULL;
  169. }