fix: free mem after malloc

This commit is contained in:
2024-01-09 14:07:18 +08:00
parent 8971b91a68
commit ba36f81f65

View File

@@ -581,6 +581,7 @@ void ChaCha20_DRBG_Instantiate_algorithm(uint8_t *entropy_input, int nonce,
*reseed_counter = 1; *reseed_counter = 1;
*reseed_time = time(&timep); *reseed_time = time(&timep);
free(seed_material);
} }
void ChaCha20_DRBG_Reseed_algorithm(uint8_t *V, uint8_t *Key, void ChaCha20_DRBG_Reseed_algorithm(uint8_t *V, uint8_t *Key,
@@ -608,6 +609,7 @@ void ChaCha20_DRBG_Reseed_algorithm(uint8_t *V, uint8_t *Key,
time_t timep; time_t timep;
*reseed_counter = 1; *reseed_counter = 1;
*reseed_time = time(&timep); *reseed_time = time(&timep);
free(seed_material);
} }
const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key, const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
@@ -629,7 +631,8 @@ const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
memset(additional_input, 0, SEEDLEN); memset(additional_input, 0, SEEDLEN);
} }
uint8_t temp[requested_number_of_bits / 8]; uint8_t *temp =
(uint8_t *)malloc(sizeof(uint8_t) * (requested_number_of_bits / 8));
ChaCha20_encrypt(chacha_state, V, temp, requested_number_of_bits / 8, true); ChaCha20_encrypt(chacha_state, V, temp, requested_number_of_bits / 8, true);
@@ -640,6 +643,9 @@ const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
(*reseed_counter)++; (*reseed_counter)++;
free(additional_input);
free(temp);
free(returned_bits);
return "Success"; return "Success";
} }
@@ -685,11 +691,13 @@ uint8_t *ChaCha20_df(uint8_t *V, uint8_t *input_string,
IV[OUTLEN - 4] = i; // 32-bit integer representation of i padded with zeros IV[OUTLEN - 4] = i; // 32-bit integer representation of i padded with zeros
// to OUTLEN bits. // to OUTLEN bits.
uint8_t data_to_be_encrypted[OUTLEN + S_actual_size]; uint8_t *data_to_be_encrypted =
(uint8_t *)malloc(sizeof(uint8_t) * OUTLEN + S_actual_size);
memcpy(data_to_be_encrypted, IV, OUTLEN); memcpy(data_to_be_encrypted, IV, OUTLEN);
memcpy(data_to_be_encrypted + OUTLEN, S, S_actual_size); memcpy(data_to_be_encrypted + OUTLEN, S, S_actual_size);
uint8_t data_encrypted_result[OUTLEN + S_actual_size]; uint8_t *data_encrypted_result =
(uint8_t *)malloc(sizeof(uint8_t) * OUTLEN + S_actual_size);
ChaCha20_encrypt(chacha_state, data_to_be_encrypted, data_encrypted_result, ChaCha20_encrypt(chacha_state, data_to_be_encrypted, data_encrypted_result,
temp_len, true); temp_len, true);
@@ -803,12 +811,14 @@ int main() {
} while (!pass); } while (!pass);
// 将二进制数据转为十六进制后写入文件 // 将二进制数据转为十六进制后写入文件
char hex_str[returned_bits_len / 8 * 2 + 1]; char *hex_str =
(char *)malloc(sizeof(char) * (returned_bits_len / 8 * 2 + 1));
for (int i = 0; i < returned_bits_len / 8; ++i) { for (int i = 0; i < returned_bits_len / 8; ++i) {
sprintf(&hex_str[i * 2], "%02x", returned_bits[i]); sprintf(&hex_str[i * 2], "%02x", returned_bits[i]);
} }
hex_str[returned_bits_len / 8 * 2] = '\0'; hex_str[returned_bits_len / 8 * 2] = '\0';
fprintf(file, "%s\n", hex_str); // 写入带有换行符的十六进制字符串 fprintf(file, "%s\n", hex_str); // 写入带有换行符的十六进制字符串
free(hex_str);
} }
clock_t end_time = clock(); clock_t end_time = clock();