Version v0.3.21 - Fixing errors with adding entropy to file

This commit is contained in:
2025-12-18 08:34:52 -04:00
parent 33b34bf5a5
commit 05f5d6ca4f
13 changed files with 201 additions and 4 deletions

View File

@@ -560,6 +560,131 @@ int collect_file_entropy(unsigned char* entropy_buffer, size_t target_bytes,
return 0; // Success
}
// Add file entropy directly to pad using streaming (for large files)
int add_file_entropy_streaming(const char* pad_chksum, const char* file_path, size_t file_size, int display_progress) {
// Get pad file path
char pad_path[1024];
char state_path[1024];
get_pad_path(pad_chksum, pad_path, state_path);
// Check if pad exists and get size
struct stat pad_stat;
if (stat(pad_path, &pad_stat) != 0) {
printf("Error: Pad file not found: %s\n", pad_path);
return 1;
}
uint64_t pad_size = pad_stat.st_size;
// Open entropy file for reading
FILE* entropy_file = fopen(file_path, "rb");
if (!entropy_file) {
printf("Error: Cannot open entropy file '%s' for reading\n", file_path);
return 1;
}
// Open pad file for read/write
FILE* pad_file = fopen(pad_path, "r+b");
if (!pad_file) {
printf("Error: Cannot open pad file for modification: %s\n", pad_path);
fclose(entropy_file);
return 1;
}
if (display_progress) {
printf("Adding entropy to pad using streaming direct XOR...\n");
printf("Pad size: %.2f GB (%lu bytes)\n", (double)pad_size / (1024.0*1024.0*1024.0), pad_size);
printf("Entropy file: %.2f GB (%zu bytes)\n", (double)file_size / (1024.0*1024.0*1024.0), file_size);
}
// Process in chunks
unsigned char pad_buffer[64 * 1024];
unsigned char entropy_buffer[64 * 1024];
uint64_t offset = 0;
size_t entropy_offset = 0;
time_t start_time = time(NULL);
while (offset < pad_size) {
size_t chunk_size = sizeof(pad_buffer);
if (pad_size - offset < chunk_size) {
chunk_size = pad_size - offset;
}
// Read current pad data
if (fread(pad_buffer, 1, chunk_size, pad_file) != chunk_size) {
printf("Error: Cannot read pad data at offset %lu\n", offset);
fclose(entropy_file);
fclose(pad_file);
return 1;
}
// Read entropy data (wrap around if file smaller than pad)
size_t entropy_read = 0;
while (entropy_read < chunk_size) {
size_t to_read = chunk_size - entropy_read;
if (to_read > sizeof(entropy_buffer)) {
to_read = sizeof(entropy_buffer);
}
size_t read_bytes = fread(entropy_buffer, 1, to_read, entropy_file);
if (read_bytes == 0) {
// Reached end of entropy file, wrap around
fseek(entropy_file, 0, SEEK_SET);
entropy_offset = 0;
read_bytes = fread(entropy_buffer, 1, to_read, entropy_file);
if (read_bytes == 0) {
printf("Error: Cannot read from entropy file\n");
fclose(entropy_file);
fclose(pad_file);
return 1;
}
}
// XOR this chunk
for (size_t i = 0; i < read_bytes; i++) {
pad_buffer[entropy_read + i] ^= entropy_buffer[i];
}
entropy_read += read_bytes;
entropy_offset += read_bytes;
}
// Seek back and write modified data
if (fseek(pad_file, offset, SEEK_SET) != 0) {
printf("Error: Cannot seek to offset %lu\n", offset);
fclose(entropy_file);
fclose(pad_file);
return 1;
}
if (fwrite(pad_buffer, 1, chunk_size, pad_file) != chunk_size) {
printf("Error: Cannot write modified pad data\n");
fclose(entropy_file);
fclose(pad_file);
return 1;
}
offset += chunk_size;
// Show progress for large pads
if (display_progress && offset % (64 * 1024 * 1024) == 0) {
show_progress(offset, pad_size, start_time);
}
}
fclose(entropy_file);
fclose(pad_file);
if (display_progress) {
show_progress(pad_size, pad_size, start_time);
printf("\n✓ Entropy successfully added to pad using streaming direct XOR\n");
printf("✓ Pad integrity maintained\n");
printf("✓ %zu bytes of entropy distributed across entire pad\n", file_size);
}
return 0;
}
// Collect entropy by source type with unified interface
int collect_entropy_by_source(entropy_source_t source, unsigned char* entropy_buffer,
size_t target_bytes, size_t* collected_bytes, int display_progress) {