Added exponential padding to increase security.
This commit is contained in:
285
src/crypto.c
285
src/crypto.c
@@ -401,18 +401,40 @@ int encrypt_text(const char* pad_identifier, const char* input_text) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if we have enough pad space
|
||||
struct stat pad_stat;
|
||||
if (stat(pad_path, &pad_stat) != 0) {
|
||||
printf("Error: Cannot get pad file size\n");
|
||||
// Calculate chunk size for padding (exponential bucketing)
|
||||
size_t chunk_size = calculate_chunk_size(input_len);
|
||||
|
||||
// Allocate buffer for padded message
|
||||
unsigned char* padded_buffer = malloc(chunk_size);
|
||||
if (!padded_buffer) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Copy message to buffer and apply padding
|
||||
memcpy(padded_buffer, text_buffer, input_len);
|
||||
if (apply_padme_padding(padded_buffer, input_len, chunk_size) != 0) {
|
||||
printf("Error: Failed to apply padding\n");
|
||||
free(padded_buffer);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (current_offset + input_len > (uint64_t)pad_stat.st_size) {
|
||||
// Check if we have enough pad space (now using chunk_size instead of input_len)
|
||||
struct stat pad_stat;
|
||||
if (stat(pad_path, &pad_stat) != 0) {
|
||||
printf("Error: Cannot get pad file size\n");
|
||||
free(padded_buffer);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (current_offset + chunk_size > (uint64_t)pad_stat.st_size) {
|
||||
printf("Error: Not enough pad space remaining\n");
|
||||
printf("Need: %lu bytes, Available: %lu bytes\n",
|
||||
input_len, (uint64_t)pad_stat.st_size - current_offset);
|
||||
chunk_size, (uint64_t)pad_stat.st_size - current_offset);
|
||||
free(padded_buffer);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
@@ -432,37 +454,40 @@ int encrypt_text(const char* pad_identifier, const char* input_text) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char* pad_data = malloc(input_len);
|
||||
if (fread(pad_data, 1, input_len, pad_file) != input_len) {
|
||||
unsigned char* pad_data = malloc(chunk_size);
|
||||
if (fread(pad_data, 1, chunk_size, pad_file) != chunk_size) {
|
||||
printf("Error: Cannot read pad data\n");
|
||||
free(pad_data);
|
||||
fclose(pad_file);
|
||||
free(padded_buffer);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
fclose(pad_file);
|
||||
|
||||
// Use universal XOR operation for encryption
|
||||
unsigned char* ciphertext = malloc(input_len);
|
||||
if (universal_xor_operation((const unsigned char*)text_buffer, input_len, pad_data, ciphertext) != 0) {
|
||||
// Use universal XOR operation for encryption (now with padded data)
|
||||
unsigned char* ciphertext = malloc(chunk_size);
|
||||
if (universal_xor_operation(padded_buffer, chunk_size, pad_data, ciphertext) != 0) {
|
||||
printf("Error: Encryption operation failed\n");
|
||||
free(pad_data);
|
||||
free(ciphertext);
|
||||
free(padded_buffer);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Update state offset
|
||||
if (write_state_offset(pad_chksum, current_offset + input_len) != 0) {
|
||||
// Update state offset (now using chunk_size)
|
||||
if (write_state_offset(pad_chksum, current_offset + chunk_size) != 0) {
|
||||
printf("Warning: Failed to update state file\n");
|
||||
}
|
||||
|
||||
// Use universal ASCII armor generator
|
||||
// Use universal ASCII armor generator (now with chunk_size)
|
||||
char* ascii_output;
|
||||
if (generate_ascii_armor(pad_chksum, current_offset, ciphertext, input_len, &ascii_output) != 0) {
|
||||
if (generate_ascii_armor(pad_chksum, current_offset, ciphertext, chunk_size, &ascii_output) != 0) {
|
||||
printf("Error: Failed to generate ASCII armor\n");
|
||||
free(pad_data);
|
||||
free(ciphertext);
|
||||
free(padded_buffer);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
@@ -479,6 +504,7 @@ int encrypt_text(const char* pad_identifier, const char* input_text) {
|
||||
// Cleanup
|
||||
free(pad_data);
|
||||
free(ciphertext);
|
||||
free(padded_buffer);
|
||||
free(ascii_output);
|
||||
free(pad_chksum);
|
||||
|
||||
@@ -632,6 +658,22 @@ int universal_decrypt(const char* input_data, const char* output_target, decrypt
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Remove padding to get actual message
|
||||
size_t actual_msg_len;
|
||||
if (remove_padme_padding(ciphertext, ciphertext_len, &actual_msg_len) != 0) {
|
||||
if (mode == DECRYPT_MODE_SILENT) {
|
||||
fprintf(stderr, "Error: Invalid padding - message may be corrupted\n");
|
||||
} else {
|
||||
printf("Error: Invalid padding - message may be corrupted\n");
|
||||
}
|
||||
free(ciphertext);
|
||||
free(pad_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Update ciphertext_len to actual message length
|
||||
ciphertext_len = actual_msg_len;
|
||||
|
||||
// Output based on mode
|
||||
if (mode == DECRYPT_MODE_FILE_TO_FILE) {
|
||||
// Write to output file
|
||||
@@ -740,6 +782,9 @@ int encrypt_file(const char* pad_identifier, const char* input_file, const char*
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Calculate chunk size for padding (exponential bucketing)
|
||||
size_t chunk_size = calculate_chunk_size(file_size);
|
||||
|
||||
// Check if pad file exists
|
||||
if (access(pad_path, R_OK) != 0) {
|
||||
printf("Error: Pad file %s not found\n", pad_path);
|
||||
@@ -774,10 +819,10 @@ int encrypt_file(const char* pad_identifier, const char* input_file, const char*
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (current_offset + file_size > (uint64_t)pad_stat.st_size) {
|
||||
if (current_offset + chunk_size > (uint64_t)pad_stat.st_size) {
|
||||
printf("Error: Not enough pad space remaining\n");
|
||||
printf("Need: %lu bytes, Available: %lu bytes\n",
|
||||
file_size, (uint64_t)pad_stat.st_size - current_offset);
|
||||
printf("Need: %lu bytes (file: %lu + padding), Available: %lu bytes\n",
|
||||
chunk_size, file_size, (uint64_t)pad_stat.st_size - current_offset);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
@@ -822,66 +867,93 @@ int encrypt_file(const char* pad_identifier, const char* input_file, const char*
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read and encrypt file
|
||||
unsigned char buffer[64 * 1024];
|
||||
unsigned char pad_buffer[64 * 1024];
|
||||
unsigned char* encrypted_data = malloc(file_size);
|
||||
uint64_t bytes_processed = 0;
|
||||
// Allocate buffer for padded file data
|
||||
unsigned char* file_data = malloc(file_size);
|
||||
if (!file_data) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
fclose(input_fp);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Encrypting %s...\n", input_file);
|
||||
|
||||
while (bytes_processed < file_size) {
|
||||
uint64_t chunk_size = sizeof(buffer);
|
||||
if (file_size - bytes_processed < chunk_size) {
|
||||
chunk_size = file_size - bytes_processed;
|
||||
}
|
||||
|
||||
// Read file data
|
||||
if (fread(buffer, 1, chunk_size, input_fp) != chunk_size) {
|
||||
printf("Error: Cannot read input file data\n");
|
||||
free(encrypted_data);
|
||||
fclose(input_fp);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read pad data
|
||||
if (fread(pad_buffer, 1, chunk_size, pad_file) != chunk_size) {
|
||||
printf("Error: Cannot read pad data\n");
|
||||
free(encrypted_data);
|
||||
fclose(input_fp);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Use universal XOR operation for encryption
|
||||
if (universal_xor_operation(buffer, chunk_size, pad_buffer, &encrypted_data[bytes_processed]) != 0) {
|
||||
printf("Error: Encryption operation failed\n");
|
||||
free(encrypted_data);
|
||||
fclose(input_fp);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bytes_processed += chunk_size;
|
||||
|
||||
// Show progress for large files (> 10MB)
|
||||
if (file_size > 10 * 1024 * 1024 && bytes_processed % (1024 * 1024) == 0) {
|
||||
// show_progress(bytes_processed, file_size, start_time); // MOVED TO src/util.c
|
||||
}
|
||||
// Read entire file
|
||||
if (fread(file_data, 1, file_size, input_fp) != file_size) {
|
||||
printf("Error: Cannot read input file\n");
|
||||
free(file_data);
|
||||
fclose(input_fp);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (file_size > 10 * 1024 * 1024) {
|
||||
// show_progress(file_size, file_size, start_time); // MOVED TO src/util.c
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
fclose(input_fp);
|
||||
|
||||
// Allocate buffer for padded data
|
||||
unsigned char* padded_data = malloc(chunk_size);
|
||||
if (!padded_data) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
free(file_data);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Copy file data and apply padding
|
||||
memcpy(padded_data, file_data, file_size);
|
||||
free(file_data);
|
||||
|
||||
if (apply_padme_padding(padded_data, file_size, chunk_size) != 0) {
|
||||
printf("Error: Failed to apply padding\n");
|
||||
free(padded_data);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read pad data
|
||||
unsigned char* pad_data = malloc(chunk_size);
|
||||
if (!pad_data) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
free(padded_data);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread(pad_data, 1, chunk_size, pad_file) != chunk_size) {
|
||||
printf("Error: Cannot read pad data\n");
|
||||
free(padded_data);
|
||||
free(pad_data);
|
||||
fclose(pad_file);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
fclose(pad_file);
|
||||
|
||||
// Encrypt padded data
|
||||
unsigned char* encrypted_data = malloc(chunk_size);
|
||||
if (!encrypted_data) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
free(padded_data);
|
||||
free(pad_data);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (universal_xor_operation(padded_data, chunk_size, pad_data, encrypted_data) != 0) {
|
||||
printf("Error: Encryption operation failed\n");
|
||||
free(padded_data);
|
||||
free(pad_data);
|
||||
free(encrypted_data);
|
||||
free(pad_chksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(padded_data);
|
||||
free(pad_data);
|
||||
|
||||
// Write output file
|
||||
if (ascii_armor) {
|
||||
// ASCII armored format - same as message format
|
||||
@@ -893,9 +965,9 @@ int encrypt_file(const char* pad_identifier, const char* input_file, const char*
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Use universal ASCII armor generator
|
||||
// Use universal ASCII armor generator (now with chunk_size)
|
||||
char* ascii_output;
|
||||
if (generate_ascii_armor(pad_chksum, current_offset, encrypted_data, file_size, &ascii_output) != 0) {
|
||||
if (generate_ascii_armor(pad_chksum, current_offset, encrypted_data, chunk_size, &ascii_output) != 0) {
|
||||
printf("Error: Failed to generate ASCII armor\n");
|
||||
fclose(output_fp);
|
||||
free(encrypted_data);
|
||||
@@ -941,17 +1013,17 @@ int encrypt_file(const char* pad_identifier, const char* input_file, const char*
|
||||
uint32_t file_mode = input_stat.st_mode;
|
||||
fwrite(&file_mode, sizeof(uint32_t), 1, output_fp);
|
||||
|
||||
// File size: 8 bytes
|
||||
// File size: 8 bytes (original file size, not padded)
|
||||
fwrite(&file_size, sizeof(uint64_t), 1, output_fp);
|
||||
|
||||
// Encrypted data
|
||||
fwrite(encrypted_data, 1, file_size, output_fp);
|
||||
// Encrypted data (padded)
|
||||
fwrite(encrypted_data, 1, chunk_size, output_fp);
|
||||
|
||||
fclose(output_fp);
|
||||
}
|
||||
|
||||
// Update state offset
|
||||
if (write_state_offset(pad_chksum, current_offset + file_size) != 0) {
|
||||
// Update state offset (now using chunk_size)
|
||||
if (write_state_offset(pad_chksum, current_offset + chunk_size) != 0) {
|
||||
printf("Warning: Failed to update state file\n");
|
||||
}
|
||||
|
||||
@@ -1012,14 +1084,14 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
||||
unsigned char pad_chksum_bin[32];
|
||||
uint64_t pad_offset;
|
||||
uint32_t file_mode;
|
||||
uint64_t file_size;
|
||||
uint64_t original_file_size;
|
||||
|
||||
if (fread(magic, 1, 4, input_fp) != 4 ||
|
||||
fread(&version, sizeof(uint16_t), 1, input_fp) != 1 ||
|
||||
fread(pad_chksum_bin, 1, 32, input_fp) != 32 ||
|
||||
fread(&pad_offset, sizeof(uint64_t), 1, input_fp) != 1 ||
|
||||
fread(&file_mode, sizeof(uint32_t), 1, input_fp) != 1 ||
|
||||
fread(&file_size, sizeof(uint64_t), 1, input_fp) != 1) {
|
||||
fread(&original_file_size, sizeof(uint64_t), 1, input_fp) != 1) {
|
||||
printf("Error: Cannot read binary header\n");
|
||||
fclose(input_fp);
|
||||
return 1;
|
||||
@@ -1039,7 +1111,7 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
||||
pad_chksum_hex[64] = '\0';
|
||||
|
||||
printf("Decrypting binary file...\n");
|
||||
printf("File size: %lu bytes\n", file_size);
|
||||
printf("Original file size: %lu bytes\n", original_file_size);
|
||||
|
||||
// Check if we have the required pad
|
||||
char pad_path[MAX_HASH_LENGTH + 20];
|
||||
@@ -1064,9 +1136,18 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
||||
output_file = default_output;
|
||||
}
|
||||
|
||||
// Read encrypted data
|
||||
unsigned char* encrypted_data = malloc(file_size);
|
||||
if (fread(encrypted_data, 1, file_size, input_fp) != file_size) {
|
||||
// Calculate chunk size (encrypted data is padded)
|
||||
size_t chunk_size = calculate_chunk_size(original_file_size);
|
||||
|
||||
// Read encrypted (padded) data
|
||||
unsigned char* encrypted_data = malloc(chunk_size);
|
||||
if (!encrypted_data) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
fclose(input_fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread(encrypted_data, 1, chunk_size, input_fp) != chunk_size) {
|
||||
printf("Error: Cannot read encrypted data\n");
|
||||
free(encrypted_data);
|
||||
fclose(input_fp);
|
||||
@@ -1089,8 +1170,15 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char* pad_data = malloc(file_size);
|
||||
if (fread(pad_data, 1, file_size, pad_file) != file_size) {
|
||||
unsigned char* pad_data = malloc(chunk_size);
|
||||
if (!pad_data) {
|
||||
printf("Error: Memory allocation failed\n");
|
||||
free(encrypted_data);
|
||||
fclose(pad_file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread(pad_data, 1, chunk_size, pad_file) != chunk_size) {
|
||||
printf("Error: Cannot read pad data\n");
|
||||
free(encrypted_data);
|
||||
free(pad_data);
|
||||
@@ -1100,26 +1188,40 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
||||
fclose(pad_file);
|
||||
|
||||
// Use universal XOR operation for decryption
|
||||
if (universal_xor_operation(encrypted_data, file_size, pad_data, encrypted_data) != 0) {
|
||||
if (universal_xor_operation(encrypted_data, chunk_size, pad_data, encrypted_data) != 0) {
|
||||
printf("Error: Decryption operation failed\n");
|
||||
free(encrypted_data);
|
||||
free(pad_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Write decrypted file
|
||||
free(pad_data);
|
||||
|
||||
// Remove padding to get original file
|
||||
size_t actual_file_size;
|
||||
if (remove_padme_padding(encrypted_data, chunk_size, &actual_file_size) != 0) {
|
||||
printf("Error: Invalid padding - file may be corrupted\n");
|
||||
free(encrypted_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Verify the actual size matches the stored original size
|
||||
if (actual_file_size != original_file_size) {
|
||||
printf("Warning: Decrypted size (%lu) doesn't match stored size (%lu)\n",
|
||||
actual_file_size, original_file_size);
|
||||
}
|
||||
|
||||
// Write decrypted file (using actual_file_size)
|
||||
FILE* output_fp = fopen(output_file, "wb");
|
||||
if (!output_fp) {
|
||||
printf("Error: Cannot create output file %s\n", output_file);
|
||||
free(encrypted_data);
|
||||
free(pad_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fwrite(encrypted_data, 1, file_size, output_fp) != file_size) {
|
||||
if (fwrite(encrypted_data, 1, actual_file_size, output_fp) != actual_file_size) {
|
||||
printf("Error: Cannot write decrypted data\n");
|
||||
free(encrypted_data);
|
||||
free(pad_data);
|
||||
fclose(output_fp);
|
||||
return 1;
|
||||
}
|
||||
@@ -1141,7 +1243,6 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
||||
|
||||
// Cleanup
|
||||
free(encrypted_data);
|
||||
free(pad_data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user