Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b058911fb8 | |||
| a0ce6f3253 |
BIN
otp-x86_64
Executable file
BIN
otp-x86_64
Executable file
Binary file not shown.
400
otp.c
400
otp.c
@@ -44,7 +44,7 @@ static const int base64_decode_table[256] = {
|
|||||||
#define PROGRESS_UPDATE_INTERVAL (64 * 1024 * 1024) // 64MB intervals
|
#define PROGRESS_UPDATE_INTERVAL (64 * 1024 * 1024) // 64MB intervals
|
||||||
#define DEFAULT_PADS_DIR "pads"
|
#define DEFAULT_PADS_DIR "pads"
|
||||||
#define FILES_DIR "files"
|
#define FILES_DIR "files"
|
||||||
#define MAX_ENTROPY_BUFFER 32768 // 32KB entropy buffer
|
#define MAX_ENTROPY_BUFFER (4 * 1024 * 1024) // 4MB entropy buffer for large operations
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -331,7 +331,7 @@ int interactive_mode(void) {
|
|||||||
void show_main_menu(void) {
|
void show_main_menu(void) {
|
||||||
|
|
||||||
|
|
||||||
printf("\n=========================== Main Menu - OTP v0.3.7 ===========================\n\n");
|
printf("\n=========================== Main Menu - OTP v0.3.9 ===========================\n\n");
|
||||||
|
|
||||||
printf(" \033[4mT\033[0mext encrypt\n"); //TEXT ENCRYPT
|
printf(" \033[4mT\033[0mext encrypt\n"); //TEXT ENCRYPT
|
||||||
printf(" \033[4mF\033[0mile encrypt\n"); //FILE ENCRYPT
|
printf(" \033[4mF\033[0mile encrypt\n"); //FILE ENCRYPT
|
||||||
@@ -2551,6 +2551,256 @@ int collect_truerng_entropy(unsigned char* entropy_buffer, size_t target_bytes,
|
|||||||
return 0; // Success
|
return 0; // Success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Streaming TrueRNG entropy collection for full pad enhancement
|
||||||
|
// This function applies entropy directly to the pad in chunks to avoid memory issues
|
||||||
|
int collect_truerng_entropy_streaming(const char* pad_chksum, size_t total_bytes, int display_progress) {
|
||||||
|
char port_path[512];
|
||||||
|
truerng_device_type_t device_type;
|
||||||
|
int serial_fd = -1;
|
||||||
|
|
||||||
|
// Find TrueRNG device
|
||||||
|
if (!find_truerng_port(port_path, sizeof(port_path), &device_type)) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("No TrueRNG device found.\n");
|
||||||
|
printf("\nSupported devices:\n");
|
||||||
|
printf(" - TrueRNG (PID: %s, VID: %s)\n", TRUERNG_VID, TRUERNG_PID);
|
||||||
|
printf(" - TrueRNGpro (PID: %s, VID: %s)\n", TRUERNGPRO_VID, TRUERNGPRO_PID);
|
||||||
|
printf(" - TrueRNGproV2 (PID: %s, VID: %s)\n", TRUERNGPROV2_VID, TRUERNGPROV2_PID);
|
||||||
|
printf("\nPlease connect a TrueRNG device and try again.\n");
|
||||||
|
}
|
||||||
|
return 1; // Device not found
|
||||||
|
}
|
||||||
|
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Found %s at %s\n", get_truerng_device_name(device_type), port_path);
|
||||||
|
printf("Streaming %zu bytes of entropy directly to pad...\n", total_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup serial port
|
||||||
|
serial_fd = setup_truerng_serial_port(port_path);
|
||||||
|
if (serial_fd < 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot open TrueRNG device at %s\n", port_path);
|
||||||
|
printf("Check device permissions or run as root.\n");
|
||||||
|
}
|
||||||
|
return 2; // Serial port setup failed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get pad paths
|
||||||
|
char pad_path[1024];
|
||||||
|
char state_path[1024];
|
||||||
|
get_pad_path(pad_chksum, pad_path, state_path);
|
||||||
|
|
||||||
|
// Open pad file for read/write with temporary permissions
|
||||||
|
if (chmod(pad_path, S_IRUSR | S_IWUSR) != 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot change pad file permissions\n");
|
||||||
|
}
|
||||||
|
close(serial_fd);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE* pad_file = fopen(pad_path, "r+b");
|
||||||
|
if (!pad_file) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot open pad file for modification: %s\n", pad_path);
|
||||||
|
}
|
||||||
|
chmod(pad_path, S_IRUSR); // Restore read-only
|
||||||
|
close(serial_fd);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process pad in chunks
|
||||||
|
const size_t chunk_size = 1024 * 1024; // 1MB chunks
|
||||||
|
unsigned char* entropy_chunk = malloc(chunk_size);
|
||||||
|
unsigned char* pad_chunk = malloc(chunk_size);
|
||||||
|
unsigned char* mixed_chunk = malloc(chunk_size);
|
||||||
|
|
||||||
|
if (!entropy_chunk || !pad_chunk || !mixed_chunk) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot allocate chunk buffers\n");
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t bytes_processed = 0;
|
||||||
|
time_t start_time = time(NULL);
|
||||||
|
|
||||||
|
while (bytes_processed < total_bytes) {
|
||||||
|
size_t current_chunk_size = chunk_size;
|
||||||
|
if (total_bytes - bytes_processed < chunk_size) {
|
||||||
|
current_chunk_size = total_bytes - bytes_processed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect entropy from TrueRNG for this chunk
|
||||||
|
size_t entropy_bytes_in_chunk = 0;
|
||||||
|
while (entropy_bytes_in_chunk < current_chunk_size) {
|
||||||
|
size_t bytes_needed = current_chunk_size - entropy_bytes_in_chunk;
|
||||||
|
size_t read_size = (bytes_needed > 1024) ? 1024 : bytes_needed;
|
||||||
|
|
||||||
|
ssize_t result = read(serial_fd, entropy_chunk + entropy_bytes_in_chunk, read_size);
|
||||||
|
if (result < 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Failed to read from TrueRNG device\n");
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 6;
|
||||||
|
} else if (result == 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: No data received from TrueRNG device\n");
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
entropy_bytes_in_chunk += result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read current pad data for this chunk
|
||||||
|
if (fseek(pad_file, bytes_processed, SEEK_SET) != 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot seek to offset %zu in pad file\n", bytes_processed);
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fread(pad_chunk, 1, current_chunk_size, pad_file) != current_chunk_size) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot read pad data at offset %zu\n", bytes_processed);
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derive Chacha20 key and nonce from entropy chunk
|
||||||
|
unsigned char key[32], nonce[12];
|
||||||
|
if (derive_chacha20_params(entropy_chunk, current_chunk_size, key, nonce) != 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Failed to derive Chacha20 parameters from entropy\n");
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate keystream and XOR with pad data
|
||||||
|
uint32_t counter = bytes_processed / 64; // Chacha20 block counter
|
||||||
|
if (chacha20_encrypt(key, counter, nonce, pad_chunk, mixed_chunk, current_chunk_size) != 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Chacha20 keystream generation failed\n");
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 11;
|
||||||
|
}
|
||||||
|
|
||||||
|
// XOR existing pad with keystream (adds entropy)
|
||||||
|
for (size_t i = 0; i < current_chunk_size; i++) {
|
||||||
|
mixed_chunk[i] = pad_chunk[i] ^ mixed_chunk[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write modified data back to pad
|
||||||
|
if (fseek(pad_file, bytes_processed, SEEK_SET) != 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot seek to write offset %zu\n", bytes_processed);
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fwrite(mixed_chunk, 1, current_chunk_size, pad_file) != current_chunk_size) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Error: Cannot write modified pad data\n");
|
||||||
|
}
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
chmod(pad_path, S_IRUSR);
|
||||||
|
close(serial_fd);
|
||||||
|
return 13;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes_processed += current_chunk_size;
|
||||||
|
|
||||||
|
// Show progress for large pads
|
||||||
|
if (display_progress && bytes_processed % (64 * 1024 * 1024) == 0) { // Every 64MB
|
||||||
|
double percentage = (double)bytes_processed / total_bytes * 100.0;
|
||||||
|
printf("Progress: %.1f%% (%zu/%zu MB)\r", percentage,
|
||||||
|
bytes_processed / (1024*1024), total_bytes / (1024*1024));
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
free(entropy_chunk);
|
||||||
|
free(pad_chunk);
|
||||||
|
free(mixed_chunk);
|
||||||
|
fclose(pad_file);
|
||||||
|
close(serial_fd);
|
||||||
|
|
||||||
|
// Restore read-only permissions
|
||||||
|
if (chmod(pad_path, S_IRUSR) != 0) {
|
||||||
|
if (display_progress) {
|
||||||
|
printf("Warning: Cannot restore pad file to read-only\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (display_progress) {
|
||||||
|
double collection_time = difftime(time(NULL), start_time);
|
||||||
|
printf("\n✓ TrueRNG streaming entropy collection complete!\n");
|
||||||
|
printf(" Enhanced: %zu bytes in %.1f seconds\n", bytes_processed, collection_time);
|
||||||
|
printf(" Device: %s\n", get_truerng_device_name(device_type));
|
||||||
|
if (collection_time > 0) {
|
||||||
|
printf(" Rate: %.1f MB/s\n", (double)bytes_processed / collection_time / (1024.0*1024.0));
|
||||||
|
}
|
||||||
|
printf("✓ Pad integrity maintained\n");
|
||||||
|
printf("✓ Entropy distributed across entire pad\n");
|
||||||
|
printf("✓ Pad restored to read-only mode\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // Success
|
||||||
|
}
|
||||||
|
|
||||||
// Collect dice entropy with manual input validation
|
// Collect dice entropy with manual input validation
|
||||||
int collect_dice_entropy(unsigned char* entropy_buffer, size_t target_bytes,
|
int collect_dice_entropy(unsigned char* entropy_buffer, size_t target_bytes,
|
||||||
size_t* collected_bytes, int display_progress) {
|
size_t* collected_bytes, int display_progress) {
|
||||||
@@ -3243,7 +3493,7 @@ int generate_ascii_armor(const char* chksum, uint64_t offset, const unsigned cha
|
|||||||
strcpy(*ascii_output, "-----BEGIN OTP MESSAGE-----\n");
|
strcpy(*ascii_output, "-----BEGIN OTP MESSAGE-----\n");
|
||||||
|
|
||||||
char temp_line[256];
|
char temp_line[256];
|
||||||
snprintf(temp_line, sizeof(temp_line), "Version: v0.3.7\n");
|
snprintf(temp_line, sizeof(temp_line), "Version: v0.3.9\n");
|
||||||
strcat(*ascii_output, temp_line);
|
strcat(*ascii_output, temp_line);
|
||||||
|
|
||||||
snprintf(temp_line, sizeof(temp_line), "Pad-ChkSum: %s\n", chksum);
|
snprintf(temp_line, sizeof(temp_line), "Pad-ChkSum: %s\n", chksum);
|
||||||
@@ -4729,53 +4979,111 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get entropy amount
|
size_t target_bytes;
|
||||||
printf("\nEntropy collection options:\n");
|
|
||||||
printf(" 1. Recommended (2048 bytes) - Optimal security\n");
|
|
||||||
printf(" 2. Minimum (1024 bytes) - Good security\n");
|
|
||||||
printf(" 3. Maximum (4096 bytes) - Maximum security\n");
|
|
||||||
printf(" 4. Custom amount\n");
|
|
||||||
printf("Enter choice (1-4): ");
|
|
||||||
|
|
||||||
char amount_input[10];
|
// For TrueRNG, automatically use the full pad size
|
||||||
if (!fgets(amount_input, sizeof(amount_input), stdin)) {
|
if (entropy_source == ENTROPY_SOURCE_TRUERNG) {
|
||||||
printf("Error: Failed to read input\n");
|
// Get the pad file size
|
||||||
return 1;
|
char pad_path[1024];
|
||||||
|
char state_path[1024];
|
||||||
|
get_pad_path(pad_chksum, pad_path, state_path);
|
||||||
|
|
||||||
|
struct stat pad_stat;
|
||||||
|
if (stat(pad_path, &pad_stat) != 0) {
|
||||||
|
printf("Error: Cannot get pad file size\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
target_bytes = (size_t)pad_stat.st_size;
|
||||||
|
printf("\nTrueRNG selected - will enhance entire pad with hardware entropy\n");
|
||||||
|
printf("Pad size: %.2f GB (%zu bytes)\n",
|
||||||
|
(double)target_bytes / (1024.0 * 1024.0 * 1024.0), target_bytes);
|
||||||
|
} else {
|
||||||
|
// For other entropy sources, show the selection menu
|
||||||
|
printf("\nEntropy collection options:\n");
|
||||||
|
printf(" 1. Recommended (2048 bytes) - Optimal security\n");
|
||||||
|
printf(" 2. Minimum (1024 bytes) - Good security\n");
|
||||||
|
printf(" 3. Maximum (4096 bytes) - Maximum security\n");
|
||||||
|
printf(" 4. Custom amount\n");
|
||||||
|
printf("Enter choice (1-4): ");
|
||||||
|
|
||||||
|
char amount_input[10];
|
||||||
|
if (!fgets(amount_input, sizeof(amount_input), stdin)) {
|
||||||
|
printf("Error: Failed to read input\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
target_bytes = 2048; // Default
|
||||||
|
int amount_choice = atoi(amount_input);
|
||||||
|
|
||||||
|
switch (amount_choice) {
|
||||||
|
case 1:
|
||||||
|
target_bytes = 2048;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
target_bytes = 1024;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
target_bytes = 4096;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
printf("Enter custom amount (512-8192 bytes): ");
|
||||||
|
char custom_input[32];
|
||||||
|
if (!fgets(custom_input, sizeof(custom_input), stdin)) {
|
||||||
|
printf("Error: Failed to read input\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t custom_amount = (size_t)atoi(custom_input);
|
||||||
|
if (custom_amount < 512 || custom_amount > 8192) {
|
||||||
|
printf("Error: Invalid amount. Must be between 512 and 8192 bytes.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
target_bytes = custom_amount;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
target_bytes = 2048; // Default to recommended
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t target_bytes = 2048; // Default
|
// For TrueRNG with large pads, use streaming approach
|
||||||
int amount_choice = atoi(amount_input);
|
if (entropy_source == ENTROPY_SOURCE_TRUERNG && target_bytes > MAX_ENTROPY_BUFFER) {
|
||||||
|
printf("\nUsing streaming approach for large pad enhancement...\n");
|
||||||
switch (amount_choice) {
|
|
||||||
case 1:
|
int result = collect_truerng_entropy_streaming(pad_chksum, target_bytes, 1);
|
||||||
target_bytes = 2048;
|
|
||||||
break;
|
if (result != 0) {
|
||||||
case 2:
|
printf("Error: TrueRNG streaming entropy collection failed\n");
|
||||||
target_bytes = 1024;
|
return 1;
|
||||||
break;
|
}
|
||||||
case 3:
|
|
||||||
target_bytes = 4096;
|
// Update checksum after entropy addition
|
||||||
break;
|
printf("\n🔄 Updating pad checksum...\n");
|
||||||
case 4:
|
char new_chksum[65];
|
||||||
printf("Enter custom amount (512-8192 bytes): ");
|
int checksum_result = update_pad_checksum_after_entropy(pad_chksum, new_chksum);
|
||||||
char custom_input[32];
|
|
||||||
if (!fgets(custom_input, sizeof(custom_input), stdin)) {
|
if (checksum_result == 0) {
|
||||||
printf("Error: Failed to read input\n");
|
printf("✓ Pad checksum updated successfully\n");
|
||||||
return 1;
|
printf(" Old checksum: %.16s...\n", pad_chksum);
|
||||||
}
|
printf(" New checksum: %.16s...\n", new_chksum);
|
||||||
|
printf("✓ Pad files renamed to new checksum\n");
|
||||||
size_t custom_amount = (size_t)atoi(custom_input);
|
} else if (checksum_result == 2) {
|
||||||
if (custom_amount < 512 || custom_amount > 8192) {
|
printf("ℹ Checksum unchanged (unusual but not an error)\n");
|
||||||
printf("Error: Invalid amount. Must be between 512 and 8192 bytes.\n");
|
} else {
|
||||||
return 1;
|
printf("⚠ Warning: Checksum update failed (entropy was added successfully)\n");
|
||||||
}
|
printf(" You may need to manually handle the checksum update\n");
|
||||||
target_bytes = custom_amount;
|
return 1;
|
||||||
break;
|
}
|
||||||
default:
|
|
||||||
target_bytes = 2048; // Default to recommended
|
printf("\n🎉 SUCCESS! Your entire pad now has enhanced randomness!\n");
|
||||||
break;
|
printf("Press Enter to continue...");
|
||||||
|
getchar();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For other entropy sources or smaller amounts, use traditional approach
|
||||||
printf("\nCollecting %zu bytes of entropy from selected source...\n", target_bytes);
|
printf("\nCollecting %zu bytes of entropy from selected source...\n", target_bytes);
|
||||||
|
|
||||||
// Allocate entropy buffer
|
// Allocate entropy buffer
|
||||||
@@ -4824,7 +5132,7 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
|
|||||||
|
|
||||||
|
|
||||||
void print_usage(const char* program_name) {
|
void print_usage(const char* program_name) {
|
||||||
printf("OTP Cipher - One Time Pad Implementation v0.3.7\n");
|
printf("OTP Cipher - One Time Pad Implementation v0.3.9\n");
|
||||||
printf("Built for testing entropy system\n");
|
printf("Built for testing entropy system\n");
|
||||||
printf("Usage:\n");
|
printf("Usage:\n");
|
||||||
printf(" %s - Interactive mode\n", program_name);
|
printf(" %s - Interactive mode\n", program_name);
|
||||||
|
|||||||
Reference in New Issue
Block a user