Skip to content

Commit

Permalink
fix compiler warning
Browse files Browse the repository at this point in the history
  • Loading branch information
wuriyanto48 committed Aug 29, 2023
1 parent 761290b commit 83a9f5f
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions crypsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static int crypsi_digest(enum crypsi_digest_alg alg, const unsigned char* messag
EVP_MD* md;

int ret = -1;
int dst_len_tmp = 0;
unsigned int dst_len_tmp = 0;
unsigned char* dst_tmp;

switch (alg) {
Expand Down Expand Up @@ -286,7 +286,7 @@ static int crypsi_hmac(enum crypsi_digest_alg alg, const unsigned char* key,
size_t dst_len_tmp = 0;
unsigned char* dst_tmp;

if (strlen(key) < HMAC_KEY_MIN_SIZE) {
if (strlen((char*) key) < HMAC_KEY_MIN_SIZE) {
return ret;
}

Expand Down Expand Up @@ -314,7 +314,7 @@ static int crypsi_hmac(enum crypsi_digest_alg alg, const unsigned char* key,
goto cleanup;
}

if(!(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, strlen(key)))) {
if(!(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, strlen((char*) key)))) {
goto cleanup;
}

Expand Down Expand Up @@ -382,27 +382,31 @@ static int crypsi_aes_cbc_encrypt(enum crypsi_aes_key aes_key_size, const unsign
int ret = -1;
int dst_len_tmp = 0;
int ciphertext_len = 0;
int result_len_raw = 0;
unsigned char* dst_tmp_raw;
unsigned char* dst_tmp;
unsigned char iv[AES_BLOCK_SIZE];

// After padding and encrypting data, the size of the ciphertext is plaintext_size + (block_size - plaintext_size % block_size)
int raw_ciphertext_len = data_len + (AES_BLOCK_SIZE - data_len%AES_BLOCK_SIZE) + 1;

switch (aes_key_size) {
case CRYPSI_AES_128_KEY:
if (strlen(key) != CRYPSI_AES_128_KEY) {
if (strlen((char*) key) != CRYPSI_AES_128_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_128_cbc();
break;
case CRYPSI_AES_192_KEY:
if (strlen(key) != CRYPSI_AES_192_KEY) {
if (strlen((char*) key) != CRYPSI_AES_192_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_192_cbc();
break;
case CRYPSI_AES_256_KEY:
if (strlen(key) != CRYPSI_AES_256_KEY) {
if (strlen((char*) key) != CRYPSI_AES_256_KEY) {
return ret;
}

Expand All @@ -412,9 +416,6 @@ static int crypsi_aes_cbc_encrypt(enum crypsi_aes_key aes_key_size, const unsign
return ret;
}

// After padding and encrypting data, the size of the ciphertext is plaintext_size + (block_size - plaintext_size % block_size)
int raw_ciphertext_len = data_len + (AES_BLOCK_SIZE - data_len%AES_BLOCK_SIZE) + 1;

if((dst_tmp_raw = (unsigned char*) malloc(raw_ciphertext_len)) == NULL) {
goto cleanup;
}
Expand Down Expand Up @@ -444,7 +445,7 @@ static int crypsi_aes_cbc_encrypt(enum crypsi_aes_key aes_key_size, const unsign
ciphertext_len += dst_len_tmp;
dst_tmp_raw[raw_ciphertext_len-1] = 0x0;

int result_len_raw = ciphertext_len + sizeof(iv) + 1;
result_len_raw = ciphertext_len + sizeof(iv) + 1;

if((dst_tmp = (unsigned char*) malloc(result_len_raw)) == NULL) {
goto cleanup;
Expand Down Expand Up @@ -480,29 +481,30 @@ static int crypsi_aes_cbc_decrypt(enum crypsi_aes_key aes_key_size, const unsign
int ret = -1;
int dst_len_tmp = 0;
int plaintext_len = 0;
int raw_ciphertext_len = 0;
unsigned char* ciphertext_raw;
unsigned char* dst_tmp;
unsigned char iv[AES_BLOCK_SIZE];
unsigned char* dst_decode;
int dst_decode_len;
unsigned int dst_decode_len = 0;

switch (aes_key_size) {
case CRYPSI_AES_128_KEY:
if (strlen(key) != CRYPSI_AES_128_KEY) {
if (strlen((char*) key) != CRYPSI_AES_128_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_128_cbc();
break;
case CRYPSI_AES_192_KEY:
if (strlen(key) != CRYPSI_AES_192_KEY) {
if (strlen((char*) key) != CRYPSI_AES_192_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_192_cbc();
break;
case CRYPSI_AES_256_KEY:
if (strlen(key) != CRYPSI_AES_256_KEY) {
if (strlen((char*) key) != CRYPSI_AES_256_KEY) {
return ret;
}

Expand All @@ -519,7 +521,7 @@ static int crypsi_aes_cbc_decrypt(enum crypsi_aes_key aes_key_size, const unsign
memcpy(iv, dst_decode, sizeof(iv));

// After padding and encrypting data, the size of the ciphertext is plaintext_size + (block_size - plaintext_size % block_size)
int raw_ciphertext_len = dst_decode_len - sizeof(iv) + 1;
raw_ciphertext_len = dst_decode_len - sizeof(iv) + 1;

if((ciphertext_raw = (unsigned char*) malloc(raw_ciphertext_len)) == NULL) {
goto cleanup;
Expand Down Expand Up @@ -580,28 +582,32 @@ static int crypsi_aes_gcm_encrypt(enum crypsi_aes_key aes_key_size, const unsign
int ret = -1;
int dst_len_tmp = 0;
int ciphertext_len = 0;
int result_len_raw = 0;
unsigned char* dst_tmp_raw;
unsigned char* dst_tmp;
unsigned char iv[AES_GCM_IV_SIZE];
unsigned char tag[AES_GCM_TAG_SIZE];

// After padding and encrypting data, the size of the ciphertext is plaintext_size + (block_size - plaintext_size % block_size)
int raw_ciphertext_len = data_len + (AES_BLOCK_SIZE - data_len%AES_BLOCK_SIZE) + 1;

switch (aes_key_size) {
case CRYPSI_AES_128_KEY:
if (strlen(key) != CRYPSI_AES_128_KEY) {
if (strlen((char*) key) != CRYPSI_AES_128_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_128_gcm();
break;
case CRYPSI_AES_192_KEY:
if (strlen(key) != CRYPSI_AES_192_KEY) {
if (strlen((char*) key) != CRYPSI_AES_192_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_192_gcm();
break;
case CRYPSI_AES_256_KEY:
if (strlen(key) != CRYPSI_AES_256_KEY) {
if (strlen((char*) key) != CRYPSI_AES_256_KEY) {
return ret;
}

Expand All @@ -611,9 +617,6 @@ static int crypsi_aes_gcm_encrypt(enum crypsi_aes_key aes_key_size, const unsign
return ret;
}

// After padding and encrypting data, the size of the ciphertext is plaintext_size + (block_size - plaintext_size % block_size)
int raw_ciphertext_len = data_len + (AES_BLOCK_SIZE - data_len%AES_BLOCK_SIZE) + 1;

if((dst_tmp_raw = (unsigned char*) malloc(raw_ciphertext_len)) == NULL)
return -1;

Expand Down Expand Up @@ -652,7 +655,7 @@ static int crypsi_aes_gcm_encrypt(enum crypsi_aes_key aes_key_size, const unsign

dst_tmp_raw[ciphertext_len] = 0x0;

int result_len_raw = ciphertext_len + sizeof(iv) + sizeof(tag) + 1;
result_len_raw = ciphertext_len + sizeof(iv) + sizeof(tag) + 1;

if((dst_tmp = (unsigned char*) malloc(result_len_raw)) == NULL) {
goto cleanup;
Expand Down Expand Up @@ -689,30 +692,31 @@ static int crypsi_aes_gcm_decrypt(enum crypsi_aes_key aes_key_size, const unsign
int ret = -1;
int dst_len_tmp = 0;
int plaintext_len = 0;
int raw_ciphertext_len = 0;
unsigned char* ciphertext_raw;
unsigned char* dst_tmp;
unsigned char iv[AES_GCM_IV_SIZE];
unsigned char tag[AES_GCM_TAG_SIZE];
unsigned char* dst_decode;
int dst_decode_len;
unsigned int dst_decode_len = 0;

switch (aes_key_size) {
case CRYPSI_AES_128_KEY:
if (strlen(key) != CRYPSI_AES_128_KEY) {
if (strlen((char*) key) != CRYPSI_AES_128_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_128_gcm();
break;
case CRYPSI_AES_192_KEY:
if (strlen(key) != CRYPSI_AES_192_KEY) {
if (strlen((char*) key) != CRYPSI_AES_192_KEY) {
return ret;
}

cipher = (EVP_CIPHER*) EVP_aes_192_gcm();
break;
case CRYPSI_AES_256_KEY:
if (strlen(key) != CRYPSI_AES_256_KEY) {
if (strlen((char*) key) != CRYPSI_AES_256_KEY) {
return ret;
}

Expand All @@ -730,7 +734,7 @@ static int crypsi_aes_gcm_decrypt(enum crypsi_aes_key aes_key_size, const unsign
memcpy(iv, dst_decode, sizeof(iv));

// After padding and encrypting data, the size of the ciphertext is plaintext_size + (block_size - plaintext_size % block_size)
int raw_ciphertext_len = dst_decode_len - (sizeof(iv)+sizeof(tag)) + 1;
raw_ciphertext_len = dst_decode_len - (sizeof(iv)+sizeof(tag)) + 1;

if((ciphertext_raw = (unsigned char*) malloc(raw_ciphertext_len)) == NULL) {
goto cleanup;
Expand Down

0 comments on commit 83a9f5f

Please sign in to comment.