Version v0.3.21 - Fixing errors with adding entropy to file
This commit is contained in:
79
src/pads.c
79
src/pads.c
@@ -83,13 +83,37 @@ int generate_pad(uint64_t size_bytes, int display_progress) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check available disk space before starting
|
||||
const char* pads_dir = get_current_pads_dir();
|
||||
struct statvfs stat;
|
||||
if (statvfs(pads_dir, &stat) == 0) {
|
||||
uint64_t available_bytes = stat.f_bavail * stat.f_frsize;
|
||||
double available_gb = (double)available_bytes / (1024.0 * 1024.0 * 1024.0);
|
||||
double required_gb = (double)size_bytes / (1024.0 * 1024.0 * 1024.0);
|
||||
|
||||
if (available_bytes < size_bytes) {
|
||||
printf("\n⚠ WARNING: Insufficient disk space!\n");
|
||||
printf(" Required: %.2f GB\n", required_gb);
|
||||
printf(" Available: %.2f GB\n", available_gb);
|
||||
printf(" Shortfall: %.2f GB\n", required_gb - available_gb);
|
||||
printf("\nContinue anyway? (y/N): ");
|
||||
|
||||
char response[10];
|
||||
if (!fgets(response, sizeof(response), stdin) ||
|
||||
(toupper(response[0]) != 'Y')) {
|
||||
printf("Pad generation cancelled.\n");
|
||||
return 1;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
char temp_filename[1024];
|
||||
char pad_path[MAX_HASH_LENGTH + 20];
|
||||
char state_path[MAX_HASH_LENGTH + 20];
|
||||
char chksum_hex[MAX_HASH_LENGTH];
|
||||
|
||||
// Create temporary filename in the pads directory to avoid cross-filesystem issues
|
||||
const char* pads_dir = get_current_pads_dir();
|
||||
snprintf(temp_filename, sizeof(temp_filename), "%s/temp_%ld.pad", pads_dir, time(NULL));
|
||||
|
||||
FILE* urandom = fopen("/dev/urandom", "rb");
|
||||
@@ -1175,9 +1199,56 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
|
||||
|
||||
// Smart method selection based on file size vs pad size
|
||||
if (file_size >= pad_size) {
|
||||
printf("✓ Using Direct XOR method (file ≥ pad size)\n");
|
||||
printf(" Method: Direct XOR - entropy file will be distributed across entire pad\n");
|
||||
target_bytes = file_size; // Use entire file
|
||||
printf("✓ Using Streaming Direct XOR method (file ≥ pad size)\n");
|
||||
printf(" Method: Streaming XOR - entropy file will be distributed across entire pad\n");
|
||||
printf(" Processing: File will be streamed in chunks (no memory limit)\n");
|
||||
|
||||
// Store original permissions and make pad temporarily writable
|
||||
mode_t original_mode;
|
||||
if (make_pad_temporarily_writable(pad_path, &original_mode) != 0) {
|
||||
printf("Error: Cannot make pad file writable for entropy addition\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Use streaming method for large files
|
||||
int result = add_file_entropy_streaming(pad_chksum, file_path, file_size, 1);
|
||||
|
||||
if (result != 0) {
|
||||
printf("Error: Failed to add file entropy to pad\n");
|
||||
restore_pad_permissions(pad_path, original_mode);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Update checksum after entropy addition
|
||||
printf("\n🔄 Updating pad checksum...\n");
|
||||
char new_chksum[65];
|
||||
int checksum_result = update_pad_checksum_after_entropy(pad_chksum, new_chksum);
|
||||
|
||||
if (checksum_result == 0) {
|
||||
printf("✓ Pad checksum updated successfully\n");
|
||||
printf(" Old checksum: %.16s...\n", pad_chksum);
|
||||
printf(" New checksum: %.16s...\n", new_chksum);
|
||||
printf("✓ Pad files renamed to new checksum\n");
|
||||
|
||||
// Restore permissions on the new pad file
|
||||
char new_pad_path[1024];
|
||||
const char* pads_dir = get_current_pads_dir();
|
||||
snprintf(new_pad_path, sizeof(new_pad_path), "%s/%s.pad", pads_dir, new_chksum);
|
||||
restore_pad_permissions(new_pad_path, original_mode);
|
||||
} else if (checksum_result == 2) {
|
||||
printf("ℹ Checksum unchanged (unusual but not an error)\n");
|
||||
restore_pad_permissions(pad_path, original_mode);
|
||||
} else {
|
||||
printf("⚠ Warning: Checksum update failed (entropy was added successfully)\n");
|
||||
printf(" You may need to manually handle the checksum update\n");
|
||||
restore_pad_permissions(pad_path, original_mode);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("\n🎉 SUCCESS! Your pad now has enhanced randomness from the entropy file!\n");
|
||||
print_centered_header("Entropy Enhancement Complete", 1);
|
||||
|
||||
return 0; // Success - exit early, don't continue to buffer-based method
|
||||
} else {
|
||||
printf("✓ Using ChaCha20 method (file < pad size)\n");
|
||||
printf(" Method: ChaCha20 - entropy will be expanded to fill entire pad\n");
|
||||
|
||||
Reference in New Issue
Block a user