From ba36f81f65329d9a0f2ffccc26dffe7700a472e5 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 9 Jan 2024 14:07:18 +0800 Subject: [PATCH] fix: free mem after malloc --- ChaCha20_DRBG.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/ChaCha20_DRBG.c b/ChaCha20_DRBG.c index c6c891e..abc0631 100755 --- a/ChaCha20_DRBG.c +++ b/ChaCha20_DRBG.c @@ -581,6 +581,7 @@ void ChaCha20_DRBG_Instantiate_algorithm(uint8_t *entropy_input, int nonce, *reseed_counter = 1; *reseed_time = time(&timep); + free(seed_material); } 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; *reseed_counter = 1; *reseed_time = time(&timep); + free(seed_material); } 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); } - 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); @@ -640,6 +643,9 @@ const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key, (*reseed_counter)++; + free(additional_input); + free(temp); + free(returned_bits); 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 // 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 + 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, temp_len, true); @@ -803,12 +811,14 @@ int main() { } 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) { sprintf(&hex_str[i * 2], "%02x", returned_bits[i]); } hex_str[returned_bits_len / 8 * 2] = '\0'; fprintf(file, "%s\n", hex_str); // 写入带有换行符的十六进制字符串 + free(hex_str); } clock_t end_time = clock();