feat: review some code
This commit is contained in:
@@ -344,11 +344,11 @@ size_t additional_input_length;
|
|||||||
// 主要函数
|
// 主要函数
|
||||||
int ChaCha20_DRBG_Instantiate_function(uint8_t *personalization_string,
|
int ChaCha20_DRBG_Instantiate_function(uint8_t *personalization_string,
|
||||||
char *state);
|
char *state);
|
||||||
char *ChaCha20_DRBG_Reseed_function(int state_handle,
|
const char *ChaCha20_DRBG_Reseed_function(int state_handle,
|
||||||
uint8_t *additional_input);
|
uint8_t *additional_input);
|
||||||
void ChaCha20_DRBG_Update(const uint8_t *provided_data, uint8_t *V,
|
void ChaCha20_DRBG_Update(const uint8_t *provided_data, uint8_t *V,
|
||||||
uint8_t *Key);
|
uint8_t *Key);
|
||||||
char *ChaCha20_DRBG_Generate_function(int state_handle,
|
const char *ChaCha20_DRBG_Generate_function(int state_handle,
|
||||||
int requested_no_of_bits,
|
int requested_no_of_bits,
|
||||||
uint8_t *additional_input,
|
uint8_t *additional_input,
|
||||||
uint8_t **returned_bits);
|
uint8_t **returned_bits);
|
||||||
@@ -362,7 +362,7 @@ void ChaCha20_DRBG_Reseed_algorithm(uint8_t *V, uint8_t *Key,
|
|||||||
int *reseed_counter, int *reseed_time,
|
int *reseed_counter, int *reseed_time,
|
||||||
uint8_t *entropy_input,
|
uint8_t *entropy_input,
|
||||||
uint8_t *additional_input);
|
uint8_t *additional_input);
|
||||||
char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
|
const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
|
||||||
int *reseed_counter,
|
int *reseed_counter,
|
||||||
int requested_number_of_bits,
|
int requested_number_of_bits,
|
||||||
uint8_t *additional_input,
|
uint8_t *additional_input,
|
||||||
@@ -391,8 +391,7 @@ int Find_state_space() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void increment_nonce(uint8_t *nonce) {
|
void increment_nonce(uint8_t *nonce) {
|
||||||
int i = 0;
|
for (int i = 0; i < NONCELEN; i++) {
|
||||||
for (i; i < NONCELEN; i++) {
|
|
||||||
nonce[i]++;
|
nonce[i]++;
|
||||||
if (nonce[i] != 0) { // 检查是否有溢出
|
if (nonce[i] != 0) { // 检查是否有溢出
|
||||||
break; // 如果没有溢出,则不需要进位
|
break; // 如果没有溢出,则不需要进位
|
||||||
@@ -436,7 +435,7 @@ int ChaCha20_DRBG_Instantiate_function(uint8_t *personalization_string,
|
|||||||
return state_handle;
|
return state_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ChaCha20_DRBG_Reseed_function(int state_handle,
|
const char *ChaCha20_DRBG_Reseed_function(int state_handle,
|
||||||
uint8_t *additional_input) {
|
uint8_t *additional_input) {
|
||||||
// Check for the validity of state_handle.
|
// Check for the validity of state_handle.
|
||||||
if (!drbg_states[state_handle].used) {
|
if (!drbg_states[state_handle].used) {
|
||||||
@@ -490,14 +489,14 @@ void ChaCha20_DRBG_Update(const uint8_t *provided_data, uint8_t *Key,
|
|||||||
memcpy(V, temp + KEYLEN, COUNTERLEN);
|
memcpy(V, temp + KEYLEN, COUNTERLEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ChaCha20_DRBG_Generate_function(int state_handle,
|
const char *ChaCha20_DRBG_Generate_function(int state_handle,
|
||||||
int requested_no_of_bits,
|
int requested_no_of_bits,
|
||||||
uint8_t *additional_input,
|
uint8_t *additional_input,
|
||||||
uint8_t **returned_bits) {
|
uint8_t **returned_bits) {
|
||||||
uint8_t *V;
|
uint8_t *V;
|
||||||
uint8_t *Key;
|
uint8_t *Key;
|
||||||
int reseed_counter, reseed_time;
|
int reseed_counter, reseed_time;
|
||||||
char *status;
|
const char *status;
|
||||||
|
|
||||||
// Check the validity of state_handle
|
// Check the validity of state_handle
|
||||||
if (!drbg_states[state_handle].V || !drbg_states[state_handle].Key) {
|
if (!drbg_states[state_handle].V || !drbg_states[state_handle].Key) {
|
||||||
@@ -562,10 +561,9 @@ void ChaCha20_DRBG_Instantiate_algorithm(uint8_t *entropy_input, int nonce,
|
|||||||
size_t personalization_string_len = strlen((char *)personalization_string);
|
size_t personalization_string_len = strlen((char *)personalization_string);
|
||||||
size_t seed_material_size =
|
size_t seed_material_size =
|
||||||
entropy_input_len + sizeof(nonce) + personalization_string_len;
|
entropy_input_len + sizeof(nonce) + personalization_string_len;
|
||||||
uint8_t *seed_material = malloc(seed_material_size);
|
uint8_t *seed_material = (uint8_t *)malloc(seed_material_size);
|
||||||
uint8_t *requested_bits;
|
uint8_t *requested_bits;
|
||||||
time_t timep;
|
time_t timep;
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
memcpy(seed_material, entropy_input, entropy_input_len);
|
memcpy(seed_material, entropy_input, entropy_input_len);
|
||||||
memcpy(seed_material + entropy_input_len, &nonce, sizeof(nonce));
|
memcpy(seed_material + entropy_input_len, &nonce, sizeof(nonce));
|
||||||
@@ -592,7 +590,7 @@ void ChaCha20_DRBG_Reseed_algorithm(uint8_t *V, uint8_t *Key,
|
|||||||
// 1. seed_material = entropy_input || additional_input.
|
// 1. seed_material = entropy_input || additional_input.
|
||||||
size_t seed_material_size =
|
size_t seed_material_size =
|
||||||
min_entropy_input_length + additional_input_length;
|
min_entropy_input_length + additional_input_length;
|
||||||
uint8_t *seed_material = malloc(seed_material_size);
|
uint8_t *seed_material = (uint8_t *)malloc(seed_material_size);
|
||||||
memcpy(seed_material, entropy_input, min_entropy_input_length);
|
memcpy(seed_material, entropy_input, min_entropy_input_length);
|
||||||
memcpy(seed_material + min_entropy_input_length, additional_input,
|
memcpy(seed_material + min_entropy_input_length, additional_input,
|
||||||
additional_input_length);
|
additional_input_length);
|
||||||
@@ -612,7 +610,7 @@ void ChaCha20_DRBG_Reseed_algorithm(uint8_t *V, uint8_t *Key,
|
|||||||
*reseed_time = time(&timep);
|
*reseed_time = time(&timep);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
|
const char *ChaCha20_DRBG_Generate_algorithm(uint8_t *V, uint8_t *Key,
|
||||||
int *reseed_counter,
|
int *reseed_counter,
|
||||||
int requested_number_of_bits,
|
int requested_number_of_bits,
|
||||||
uint8_t *additional_input,
|
uint8_t *additional_input,
|
||||||
@@ -665,7 +663,7 @@ uint8_t *ChaCha20_df(uint8_t *V, uint8_t *input_string,
|
|||||||
// Pad S with zeros, if necessary.
|
// Pad S with zeros, if necessary.
|
||||||
int S_actual_size = S_init_size; // Taken the 0x80 into account
|
int S_actual_size = S_init_size; // Taken the 0x80 into account
|
||||||
while (S_actual_size % OUTLEN != 0) {
|
while (S_actual_size % OUTLEN != 0) {
|
||||||
S = realloc(S, S_actual_size + 1); // Increase size by one byte
|
S = (uint8_t *)realloc(S, S_actual_size + 1); // Increase size by one byte
|
||||||
S[S_actual_size++] =
|
S[S_actual_size++] =
|
||||||
0x00; // Add a zero at current end, and increase actual size by one
|
0x00; // Add a zero at current end, and increase actual size by one
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user