4141
4242struct blufi_security {
4343#define DH_SELF_PUB_KEY_LEN 128
44- #define DH_SELF_PUB_KEY_BIT_LEN (DH_SELF_PUB_KEY_LEN * 8)
4544 uint8_t self_public_key [DH_SELF_PUB_KEY_LEN ];
4645#define SHARE_KEY_LEN 128
47- #define SHARE_KEY_BIT_LEN (SHARE_KEY_LEN * 8)
4846 uint8_t share_key [SHARE_KEY_LEN ];
4947 size_t share_len ;
5048#define PSK_LEN 16
@@ -91,6 +89,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
9189 }
9290 blufi_sec -> dh_param = (uint8_t * )malloc (blufi_sec -> dh_param_len );
9391 if (blufi_sec -> dh_param == NULL ) {
92+ blufi_sec -> dh_param_len = 0 ; /* Reset length to avoid using unallocated memory */
9493 btc_blufi_report_error (ESP_BLUFI_DH_MALLOC_ERROR );
9594 BLUFI_ERROR ("%s, malloc failed\n" , __func__ );
9695 return ;
@@ -125,9 +124,10 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
125124 if (dhm_len > DH_SELF_PUB_KEY_LEN ) {
126125 BLUFI_ERROR ("%s dhm len not support %d\n" , __func__ , dhm_len );
127126 btc_blufi_report_error (ESP_BLUFI_DH_PARAM_ERROR );
127+ return ;
128128 }
129129
130- ret = mbedtls_dhm_make_public (& blufi_sec -> dhm , dhm_len , blufi_sec -> self_public_key , dhm_len , myrand , NULL );
130+ ret = mbedtls_dhm_make_public (& blufi_sec -> dhm , dhm_len , blufi_sec -> self_public_key , DH_SELF_PUB_KEY_LEN , myrand , NULL );
131131 if (ret ) {
132132 BLUFI_ERROR ("%s make public failed %d\n" , __func__ , ret );
133133 btc_blufi_report_error (ESP_BLUFI_MAKE_PUBLIC_ERROR );
@@ -136,7 +136,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
136136
137137 ret = mbedtls_dhm_calc_secret ( & blufi_sec -> dhm ,
138138 blufi_sec -> share_key ,
139- SHARE_KEY_BIT_LEN ,
139+ SHARE_KEY_LEN ,
140140 & blufi_sec -> share_len ,
141141 myrand , NULL );
142142 if (ret ) {
@@ -153,7 +153,7 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
153153 return ;
154154 }
155155
156- mbedtls_aes_setkey_enc (& blufi_sec -> aes , blufi_sec -> psk , 128 );
156+ mbedtls_aes_setkey_enc (& blufi_sec -> aes , blufi_sec -> psk , PSK_LEN * 8 );
157157
158158 /* alloc output data */
159159 * output_data = & blufi_sec -> self_public_key [0 ];
@@ -177,6 +177,10 @@ int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len)
177177 size_t iv_offset = 0 ;
178178 uint8_t iv0 [16 ];
179179
180+ if (!blufi_sec ) {
181+ return -1 ;
182+ }
183+
180184 memcpy (iv0 , blufi_sec -> iv , sizeof (blufi_sec -> iv ));
181185 iv0 [0 ] = iv8 ; /* set iv8 as the iv0[0] */
182186
@@ -194,6 +198,10 @@ int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len)
194198 size_t iv_offset = 0 ;
195199 uint8_t iv0 [16 ];
196200
201+ if (!blufi_sec ) {
202+ return -1 ;
203+ }
204+
197205 memcpy (iv0 , blufi_sec -> iv , sizeof (blufi_sec -> iv ));
198206 iv0 [0 ] = iv8 ; /* set iv8 as the iv0[0] */
199207
@@ -223,7 +231,7 @@ esp_err_t blufi_security_init(void)
223231 mbedtls_dhm_init (& blufi_sec -> dhm );
224232 mbedtls_aes_init (& blufi_sec -> aes );
225233
226- memset (blufi_sec -> iv , 0x0 , 16 );
234+ memset (blufi_sec -> iv , 0x0 , sizeof ( blufi_sec -> iv ) );
227235 return 0 ;
228236}
229237
@@ -242,5 +250,5 @@ void blufi_security_deinit(void)
242250 memset (blufi_sec , 0x0 , sizeof (struct blufi_security ));
243251
244252 free (blufi_sec );
245- blufi_sec = NULL ;
253+ blufi_sec = NULL ;
246254}
0 commit comments