[c] Improved readability of cfich_chacha20.c and fixed bugs on encryption
This commit is contained in:
parent
c05daa0f53
commit
53ba1d56ba
1 changed files with 75 additions and 100 deletions
|
@ -28,35 +28,25 @@ close:
|
|||
|
||||
int encrypt(char *input_file, char *key_file) {
|
||||
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) {
|
||||
printf("Error opening key file\n");
|
||||
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];
|
||||
if (fread(key_bytes, 1, 32, fkey) != 32) {
|
||||
printf("Error reading key from file\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
return 1;
|
||||
}
|
||||
fclose(fkey);
|
||||
|
||||
FILE *finput = fopen(input_file, "rb");
|
||||
if (finput == NULL) {
|
||||
printf("Error opening input file\n");
|
||||
return 1;
|
||||
}
|
||||
// Determining file size
|
||||
fseek(finput, 0, SEEK_END);
|
||||
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));
|
||||
if (plaintext == NULL) {
|
||||
printf("Error allocating memory\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread(plaintext, 1, fsize, finput) != fsize) {
|
||||
printf("Error reading file\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
return 1;
|
||||
}
|
||||
fclose(finput);
|
||||
|
||||
unsigned char nonce[16];
|
||||
if (RAND_bytes(nonce, 16) != 1) {
|
||||
printf("Error generating nonce\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
return 1;
|
||||
}
|
||||
|
@ -95,176 +79,167 @@ int encrypt(char *input_file, char *key_file) {
|
|||
EVP_CIPHER_CTX *ctx;
|
||||
if (!(ctx = EVP_CIPHER_CTX_new())) {
|
||||
printf("Error creating context\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, key_bytes, nonce) != 1) {
|
||||
printf("Error initializing encryption\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int len;
|
||||
if (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, sizeof(plaintext)) != 1) {
|
||||
printf("Error encrypting\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
return 1;
|
||||
}
|
||||
if (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, fsize) != 1) {
|
||||
printf("Error encrypting\n");
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
free(plaintext);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1) {
|
||||
printf("Error finalizing encryption\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
free(plaintext);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Add the nonce to the beginning of the file
|
||||
char *nonce_and_ciphertext = malloc(16 + fsize);
|
||||
nonce_and_ciphertext = memcpy(nonce_and_ciphertext, nonce, 16);
|
||||
nonce_and_ciphertext = memcpy(nonce_and_ciphertext + 16, ciphertext, fsize);
|
||||
char *nonce_and_ciphertext = malloc(16 + (fsize * sizeof(unsigned char)));
|
||||
memcpy(nonce_and_ciphertext, nonce, 16);
|
||||
memcpy(nonce_and_ciphertext + 16, ciphertext, fsize);
|
||||
|
||||
if (fwrite(nonce_and_ciphertext, 1, 16 + fsize, foutput) != (16 + fsize)) {
|
||||
printf("Error writing to output file\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
free(nonce_and_ciphertext);
|
||||
// Create output file
|
||||
char *output_file = malloc(strlen(input_file) + 5);
|
||||
strcpy(output_file, input_file);
|
||||
strcat(output_file, ".enc");
|
||||
FILE *foutput = fopen(output_file, "wb");
|
||||
if (foutput == NULL) {
|
||||
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;
|
||||
}
|
||||
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(plaintext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(output_file);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int decrypt(char *input_file, char *key_file) {
|
||||
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) {
|
||||
printf("Error opening key file\n");
|
||||
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];
|
||||
if (fread(key_bytes, 1, 32, fkey) != 32) {
|
||||
printf("Error reading key from file\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
return 1;
|
||||
}
|
||||
fclose(fkey);
|
||||
|
||||
FILE *finput = fopen(input_file, "rb");
|
||||
if (finput == NULL) {
|
||||
printf("Error opening input file\n");
|
||||
return 1;
|
||||
}
|
||||
// Determining file size
|
||||
fseek(finput, 0, SEEK_END);
|
||||
unsigned long fsize = ftell(finput);
|
||||
rewind(finput);
|
||||
|
||||
unsigned char *nounce_and_ciphertext = malloc(fsize * sizeof(unsigned char));
|
||||
if (nounce_and_ciphertext == NULL) {
|
||||
unsigned char *nonce_and_ciphertext = malloc(fsize * sizeof(unsigned char));
|
||||
if (nonce_and_ciphertext == NULL) {
|
||||
printf("Error allocating memory\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
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");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
return 1;
|
||||
}
|
||||
fclose(finput);
|
||||
|
||||
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);
|
||||
memcpy(ciphertext, nounce_and_ciphertext + 16, fsize - 16);
|
||||
memcpy(ciphertext, nonce_and_ciphertext + 16, fsize - 16);
|
||||
|
||||
// Decrypt the ciphertext using chacha20
|
||||
unsigned char plaintext[fsize - 16];
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
if (!(ctx = EVP_CIPHER_CTX_new())) {
|
||||
printf("Error creating context\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(ciphertext);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, key_bytes, nonce) != 1) {
|
||||
printf("Error initializing decryption\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(ciphertext);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int len;
|
||||
if (EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, fsize - 16) != 1) {
|
||||
printf("Error decrypting\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(ciphertext);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1) {
|
||||
printf("Error finalizing decryption\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(ciphertext);
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
if (fwrite(plaintext, 1, fsize - 16, foutput) != (fsize - 16)) {
|
||||
printf("Error writing to output file\n");
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(output_file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fclose(fkey);
|
||||
fclose(finput);
|
||||
fclose(foutput);
|
||||
free(nounce_and_ciphertext);
|
||||
free(nonce_and_ciphertext);
|
||||
free(output_file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue