[c] Improved readability of cfich_chacha20.c and fixed bugs on encryption

This commit is contained in:
Afonso Franco 2024-02-22 02:45:34 +00:00
parent c05daa0f53
commit 53ba1d56ba
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys

View file

@ -28,35 +28,25 @@ close:
int encrypt(char *input_file, char *key_file) { int encrypt(char *input_file, char *key_file) {
FILE *fkey = fopen(key_file, "rb"); FILE *fkey = fopen(key_file, "rb");
FILE *finput = fopen(input_file, "rb");
// Create output file
char *output_file = malloc(strlen(input_file) + 5);
output_file = strcpy(output_file, input_file);
output_file = strcat(output_file, ".enc");
FILE *foutput = fopen(output_file, "wb");
if (fkey == NULL) { if (fkey == NULL) {
printf("Error opening key file\n"); printf("Error opening key file\n");
return 1; return 1;
} }
if (finput == NULL) {
printf("Error opening input file\n");
return 1;
}
if (foutput == NULL) {
printf("Error opening output file\n");
return 1;
}
unsigned char key_bytes[32]; unsigned char key_bytes[32];
if (fread(key_bytes, 1, 32, fkey) != 32) { if (fread(key_bytes, 1, 32, fkey) != 32) {
printf("Error reading key from file\n"); printf("Error reading key from file\n");
fclose(fkey); fclose(fkey);
fclose(finput);
fclose(foutput);
return 1; return 1;
} }
fclose(fkey);
FILE *finput = fopen(input_file, "rb");
if (finput == NULL) {
printf("Error opening input file\n");
return 1;
}
// Determining file size // Determining file size
fseek(finput, 0, SEEK_END); fseek(finput, 0, SEEK_END);
unsigned long fsize = ftell(finput); unsigned long fsize = ftell(finput);
@ -65,27 +55,21 @@ int encrypt(char *input_file, char *key_file) {
unsigned char *plaintext = malloc(fsize * sizeof(unsigned char)); unsigned char *plaintext = malloc(fsize * sizeof(unsigned char));
if (plaintext == NULL) { if (plaintext == NULL) {
printf("Error allocating memory\n"); printf("Error allocating memory\n");
fclose(fkey);
fclose(finput); fclose(finput);
fclose(foutput);
return 1; return 1;
} }
if (fread(plaintext, 1, fsize, finput) != fsize) { if (fread(plaintext, 1, fsize, finput) != fsize) {
printf("Error reading file\n"); printf("Error reading file\n");
fclose(fkey);
fclose(finput); fclose(finput);
fclose(foutput);
free(plaintext); free(plaintext);
return 1; return 1;
} }
fclose(finput);
unsigned char nonce[16]; unsigned char nonce[16];
if (RAND_bytes(nonce, 16) != 1) { if (RAND_bytes(nonce, 16) != 1) {
printf("Error generating nonce\n"); printf("Error generating nonce\n");
fclose(fkey);
fclose(finput);
fclose(foutput);
free(plaintext); free(plaintext);
return 1; return 1;
} }
@ -95,176 +79,167 @@ int encrypt(char *input_file, char *key_file) {
EVP_CIPHER_CTX *ctx; EVP_CIPHER_CTX *ctx;
if (!(ctx = EVP_CIPHER_CTX_new())) { if (!(ctx = EVP_CIPHER_CTX_new())) {
printf("Error creating context\n"); printf("Error creating context\n");
fclose(fkey);
fclose(finput);
fclose(foutput);
free(plaintext); free(plaintext);
return 1; return 1;
} }
if (EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, key_bytes, nonce) != 1) { if (EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, key_bytes, nonce) != 1) {
printf("Error initializing encryption\n"); printf("Error initializing encryption\n");
fclose(fkey);
fclose(finput);
fclose(foutput);
free(plaintext); free(plaintext);
EVP_CIPHER_CTX_free(ctx);
return 1; return 1;
} }
int len; int len;
if (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, sizeof(plaintext)) != 1) { if (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, fsize) != 1) {
printf("Error encrypting\n"); printf("Error encrypting\n");
fclose(fkey); EVP_CIPHER_CTX_free(ctx);
fclose(finput); free(plaintext);
fclose(foutput); return 1;
free(plaintext); }
return 1;
}
if (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1) { if (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1) {
printf("Error finalizing encryption\n"); printf("Error finalizing encryption\n");
fclose(fkey); EVP_CIPHER_CTX_free(ctx);
fclose(finput);
fclose(foutput);
free(plaintext); free(plaintext);
return 1; return 1;
} }
// Add the nonce to the beginning of the file // Add the nonce to the beginning of the file
char *nonce_and_ciphertext = malloc(16 + fsize); char *nonce_and_ciphertext = malloc(16 + (fsize * sizeof(unsigned char)));
nonce_and_ciphertext = memcpy(nonce_and_ciphertext, nonce, 16); memcpy(nonce_and_ciphertext, nonce, 16);
nonce_and_ciphertext = memcpy(nonce_and_ciphertext + 16, ciphertext, fsize); memcpy(nonce_and_ciphertext + 16, ciphertext, fsize);
if (fwrite(nonce_and_ciphertext, 1, 16 + fsize, foutput) != (16 + fsize)) { // Create output file
printf("Error writing to output file\n"); char *output_file = malloc(strlen(input_file) + 5);
fclose(fkey); strcpy(output_file, input_file);
fclose(finput); strcat(output_file, ".enc");
fclose(foutput); FILE *foutput = fopen(output_file, "wb");
free(plaintext); if (foutput == NULL) {
free(nonce_and_ciphertext); printf("Error opening output file\n");
EVP_CIPHER_CTX_free(ctx);
return 1;
}
if (fwrite(nonce_and_ciphertext, 1, 16 + fsize, foutput) != (16 + fsize)) {
printf("Error writing to output file\n");
fclose(foutput);
free(plaintext);
free(nonce_and_ciphertext);
free(output_file);
EVP_CIPHER_CTX_free(ctx);
return 1; return 1;
} }
fclose(fkey);
fclose(finput);
fclose(foutput); fclose(foutput);
free(plaintext); free(plaintext);
free(nonce_and_ciphertext); free(nonce_and_ciphertext);
free(output_file);
EVP_CIPHER_CTX_free(ctx);
return 0; return 0;
} }
int decrypt(char *input_file, char *key_file) { int decrypt(char *input_file, char *key_file) {
FILE *fkey = fopen(key_file, "rb"); FILE *fkey = fopen(key_file, "rb");
FILE *finput = fopen(input_file, "rb");
// Create output file
char *output_file = malloc(strlen(input_file) + 5);
output_file = strcpy(output_file, input_file);
output_file = strcat(output_file, ".dec");
FILE *foutput = fopen(output_file, "wb");
if (fkey == NULL) { if (fkey == NULL) {
printf("Error opening key file\n"); printf("Error opening key file\n");
return 1; return 1;
} }
if (finput == NULL) {
printf("Error opening input file\n");
return 1;
}
if (foutput == NULL) {
printf("Error opening output file\n");
return 1;
}
unsigned char key_bytes[32]; unsigned char key_bytes[32];
if (fread(key_bytes, 1, 32, fkey) != 32) { if (fread(key_bytes, 1, 32, fkey) != 32) {
printf("Error reading key from file\n"); printf("Error reading key from file\n");
fclose(fkey); fclose(fkey);
fclose(finput);
fclose(foutput);
return 1; return 1;
} }
fclose(fkey);
FILE *finput = fopen(input_file, "rb");
if (finput == NULL) {
printf("Error opening input file\n");
return 1;
}
// Determining file size // Determining file size
fseek(finput, 0, SEEK_END); fseek(finput, 0, SEEK_END);
unsigned long fsize = ftell(finput); unsigned long fsize = ftell(finput);
rewind(finput); rewind(finput);
unsigned char *nounce_and_ciphertext = malloc(fsize * sizeof(unsigned char)); unsigned char *nonce_and_ciphertext = malloc(fsize * sizeof(unsigned char));
if (nounce_and_ciphertext == NULL) { if (nonce_and_ciphertext == NULL) {
printf("Error allocating memory\n"); printf("Error allocating memory\n");
fclose(fkey);
fclose(finput); fclose(finput);
fclose(foutput);
return 1; return 1;
} }
if (fread(nounce_and_ciphertext, 1, fsize, finput) != fsize) { if (fread(nonce_and_ciphertext, 1, fsize, finput) != fsize) {
printf("Error reading file\n"); printf("Error reading file\n");
fclose(fkey);
fclose(finput); fclose(finput);
fclose(foutput); free(nonce_and_ciphertext);
free(nounce_and_ciphertext);
return 1; return 1;
} }
fclose(finput);
unsigned char nonce[16]; unsigned char nonce[16];
memcpy(nonce, nounce_and_ciphertext, 16); memcpy(nonce, nonce_and_ciphertext, 16);
unsigned char *ciphertext = malloc((fsize * sizeof(unsigned char)) - 16); unsigned char *ciphertext = malloc((fsize * sizeof(unsigned char)) - 16);
memcpy(ciphertext, nounce_and_ciphertext + 16, fsize - 16); memcpy(ciphertext, nonce_and_ciphertext + 16, fsize - 16);
// Decrypt the ciphertext using chacha20 // Decrypt the ciphertext using chacha20
unsigned char plaintext[fsize - 16]; unsigned char plaintext[fsize - 16];
EVP_CIPHER_CTX *ctx; EVP_CIPHER_CTX *ctx;
if (!(ctx = EVP_CIPHER_CTX_new())) { if (!(ctx = EVP_CIPHER_CTX_new())) {
printf("Error creating context\n"); printf("Error creating context\n");
fclose(fkey); free(nonce_and_ciphertext);
fclose(finput); free(ciphertext);
fclose(foutput);
free(nounce_and_ciphertext);
return 1; return 1;
} }
if (EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, key_bytes, nonce) != 1) { if (EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, key_bytes, nonce) != 1) {
printf("Error initializing decryption\n"); printf("Error initializing decryption\n");
fclose(fkey); free(nonce_and_ciphertext);
fclose(finput); free(ciphertext);
fclose(foutput);
free(nounce_and_ciphertext);
return 1; return 1;
} }
int len; int len;
if (EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, fsize - 16) != 1) { if (EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, fsize - 16) != 1) {
printf("Error decrypting\n"); printf("Error decrypting\n");
fclose(fkey); free(nonce_and_ciphertext);
fclose(finput); free(ciphertext);
fclose(foutput);
free(nounce_and_ciphertext);
return 1; return 1;
} }
if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1) { if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1) {
printf("Error finalizing decryption\n"); printf("Error finalizing decryption\n");
fclose(fkey); free(nonce_and_ciphertext);
fclose(finput); free(ciphertext);
fclose(foutput); return 1;
free(nounce_and_ciphertext); }
free(ciphertext);
// Create output file
char *output_file = malloc(strlen(input_file) + 5);
strcpy(output_file, input_file);
strcat(output_file, ".dec");
FILE *foutput = fopen(output_file, "wb");
if (foutput == NULL) {
printf("Error opening output file\n");
return 1; return 1;
} }
if (fwrite(plaintext, 1, fsize - 16, foutput) != (fsize - 16)) { if (fwrite(plaintext, 1, fsize - 16, foutput) != (fsize - 16)) {
printf("Error writing to output file\n"); printf("Error writing to output file\n");
fclose(fkey);
fclose(finput);
fclose(foutput); fclose(foutput);
free(nounce_and_ciphertext); free(nonce_and_ciphertext);
free(output_file);
return 1; return 1;
} }
fclose(fkey);
fclose(finput);
fclose(foutput); fclose(foutput);
free(nounce_and_ciphertext); free(nonce_and_ciphertext);
free(output_file);
return 0; return 0;
} }