Compare commits

...

5 Commits

5 changed files with 139 additions and 57 deletions

1
.gitignore vendored
View File

@@ -5,5 +5,6 @@ Gemini.md
TropicOfCancer-HenryMiller.txt
.gitea_token
true_rng/
swiftrng/
# Auto-generated files (none currently)

BIN
otp-arm64

Binary file not shown.

Binary file not shown.

192
otp.c
View File

@@ -78,10 +78,28 @@ void init_terminal_dimensions(void) {
// If ioctl fails, keep the default values (80x24)
}
// Print centered header with = padding
void print_centered_header(const char* text) {
// Print centered header with = padding, screen clearing, and optional pause
void print_centered_header(const char* text, int pause_before_clear) {
if (!text) return;
// Phase 1: Pause if requested
if (pause_before_clear) {
printf("\nPress Enter to continue...");
fflush(stdout);
// Wait for Enter key
int c;
while ((c = getchar()) != '\n' && c != EOF) {
// Consume any extra characters until newline
}
}
// Phase 2: Clear screen using terminal height
for (int i = 0; i < terminal_height; i++) {
printf("\n");
}
// Phase 3: Display centered header (existing logic)
int text_len = strlen(text);
int available_width = terminal_width;
@@ -387,7 +405,7 @@ int interactive_mode(void) {
void show_main_menu(void) {
printf("\n");
print_centered_header("Main Menu - OTP v0.3.10");
print_centered_header("Main Menu - OTP v0.3.15", 0);
printf("\n");
printf(" \033[4mT\033[0mext encrypt\n"); //TEXT ENCRYPT
@@ -400,7 +418,7 @@ void show_main_menu(void) {
int handle_generate_menu(void) {
printf("\n");
print_centered_header("Generate New Pad");
print_centered_header("Generate New Pad", 0);
printf("Enter pad size (examples: 1GB, 5TB, 512MB, 2048): ");
char size_input[64];
@@ -426,7 +444,7 @@ int handle_generate_menu(void) {
int handle_encrypt_menu(void) {
printf("\n");
print_centered_header("Encrypt Data");
print_centered_header("Encrypt Data", 0);
printf("Available pads:\n");
char* selected = select_pad_interactive("Available pads:", "Select pad (or press Enter to continue)", PAD_FILTER_ALL, 0);
@@ -568,7 +586,7 @@ int handle_encrypt_menu(void) {
int handle_decrypt_menu(void) {
printf("\n");
print_centered_header("Smart Decrypt");
print_centered_header("Smart Decrypt", 0);
printf("Enter encrypted data (paste ASCII armor), file path, or press Enter to browse files:\n");
char input_line[MAX_LINE_LENGTH];
@@ -802,7 +820,7 @@ int show_pad_info(const char* chksum) {
uint64_t used_bytes;
read_state_offset(chksum, &used_bytes);
print_centered_header("Pad Information");
print_centered_header("Pad Information", 0);
printf("ChkSum: %s\n", chksum);
printf("File: %s\n", pad_filename);
@@ -842,13 +860,13 @@ int generate_pad(uint64_t size_bytes, int display_progress) {
return 1;
}
char temp_filename[64];
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
snprintf(temp_filename, sizeof(temp_filename), "temp_%ld.pad", time(NULL));
// Create temporary filename in the pads directory to avoid cross-filesystem issues
snprintf(temp_filename, sizeof(temp_filename), "%s/temp_%ld.pad", current_pads_dir, time(NULL));
FILE* urandom = fopen("/dev/urandom", "rb");
if (!urandom) {
@@ -909,7 +927,10 @@ int generate_pad(uint64_t size_bytes, int display_progress) {
fclose(pad_file);
// Calculate XOR checksum of the pad file
if (calculate_checksum(temp_filename, chksum_hex) != 0) {
if (display_progress) {
printf("Calculating pad checksum...\n");
}
if (calculate_checksum_with_progress(temp_filename, chksum_hex, display_progress, size_bytes) != 0) {
printf("Error: Cannot calculate pad checksum\n");
unlink(temp_filename);
return 1;
@@ -918,39 +939,11 @@ int generate_pad(uint64_t size_bytes, int display_progress) {
// Get final paths in pads directory
get_pad_path(chksum_hex, pad_path, state_path);
// Try rename first (works for same filesystem)
// Rename temporary file to final name (atomic operation within same directory)
if (rename(temp_filename, pad_path) != 0) {
// If rename fails, try copy and delete (works across filesystems)
FILE* temp_file = fopen(temp_filename, "rb");
FILE* dest_file = fopen(pad_path, "wb");
if (!temp_file || !dest_file) {
printf("Error: Cannot copy pad file to pads directory\n");
if (temp_file) fclose(temp_file);
if (dest_file) fclose(dest_file);
unlink(temp_filename);
return 1;
}
// Copy file in chunks
unsigned char copy_buffer[64 * 1024];
size_t bytes_read;
while ((bytes_read = fread(copy_buffer, 1, sizeof(copy_buffer), temp_file)) > 0) {
if (fwrite(copy_buffer, 1, bytes_read, dest_file) != bytes_read) {
printf("Error: Failed to copy pad file to pads directory\n");
fclose(temp_file);
fclose(dest_file);
unlink(temp_filename);
unlink(pad_path);
return 1;
}
}
fclose(temp_file);
fclose(dest_file);
// Remove temporary file after successful copy
printf("Error: Cannot rename temporary pad file to final name\n");
unlink(temp_filename);
return 1;
}
// Set pad file to read-only
@@ -977,6 +970,9 @@ int generate_pad(uint64_t size_bytes, int display_progress) {
printf("Pad file set to read-only\n");
printf("Use 'Add entropy' in Pads menu to enhance randomness.\n");
// Pause before returning to menu to let user see the success message
print_centered_header("Pad Generation Complete", 1);
return 0;
}
@@ -1117,6 +1113,9 @@ int add_entropy_to_pad(const char* pad_chksum, const unsigned char* entropy_data
printf(" Old checksum: %.16s...\n", pad_chksum);
printf(" New checksum: %.16s...\n", new_chksum);
printf("✓ Pad files renamed to new checksum\n");
// Pause before returning to menu to let user see the success message
print_centered_header("Entropy Addition Complete", 1);
} else if (checksum_result == 2) {
printf(" Checksum unchanged (unusual but not an error)\n");
} else {
@@ -1600,6 +1599,9 @@ int encrypt_file(const char* pad_identifier, const char* input_file, const char*
printf("Format: Binary (.otp)\n");
}
// Pause before returning to menu to let user see the success message
print_centered_header("File Encryption Complete", 1);
// Cleanup
free(encrypted_data);
free(pad_chksum);
@@ -1768,6 +1770,9 @@ int decrypt_binary_file(FILE* input_fp, const char* output_file) {
printf("File decrypted successfully: %s\n", output_file);
printf("Restored permissions and metadata\n");
// Pause before returning to menu to let user see the success message
print_centered_header("File Decryption Complete", 1);
// Cleanup
free(encrypted_data);
free(pad_data);
@@ -3095,6 +3100,9 @@ int collect_truerng_entropy_streaming(const char* pad_chksum, size_t total_bytes
printf("✓ Pad integrity maintained\n");
printf("✓ Entropy distributed across entire pad\n");
printf("✓ Pad restored to read-only mode\n");
// Pause before returning to menu to let user see the success message
print_centered_header("TrueRNG Enhancement Complete", 1);
}
return 0; // Success
@@ -3104,7 +3112,7 @@ int collect_truerng_entropy_streaming(const char* pad_chksum, size_t total_bytes
int collect_dice_entropy(unsigned char* entropy_buffer, size_t target_bytes,
size_t* collected_bytes, int display_progress) {
if (display_progress) {
print_centered_header("Dice Entropy Collection");
print_centered_header("Dice Entropy Collection", 0);
printf("Enter dice rolls as sequences of digits 1-6.\n");
printf("Target: %zu bytes (%zu dice rolls needed)\n", target_bytes, target_bytes * 4);
printf("Press Enter after each sequence, or 'done' when finished.\n\n");
@@ -3792,7 +3800,7 @@ int generate_ascii_armor(const char* chksum, uint64_t offset, const unsigned cha
strcpy(*ascii_output, "-----BEGIN OTP MESSAGE-----\n");
char temp_line[256];
snprintf(temp_line, sizeof(temp_line), "Version: v0.3.10\n");
snprintf(temp_line, sizeof(temp_line), "Version: v0.3.15\n");
strcat(*ascii_output, temp_line);
snprintf(temp_line, sizeof(temp_line), "Pad-ChkSum: %s\n", chksum);
@@ -4457,7 +4465,7 @@ int launch_file_manager(const char* start_directory, char* selected_file, size_t
int handle_text_encrypt(void) {
printf("\n");
print_centered_header("Text Encrypt");
print_centered_header("Text Encrypt", 0);
// Launch text editor directly
char text_buffer[MAX_INPUT_SIZE];
@@ -4487,7 +4495,7 @@ int handle_text_encrypt(void) {
int handle_file_encrypt(void) {
printf("\n");
print_centered_header("File Encrypt");
print_centered_header("File Encrypt", 0);
// Launch file manager directly
char input_file[512];
@@ -4574,7 +4582,7 @@ int calculate_checksum(const char* filename, char* checksum_hex) {
// Process this chunk with XOR checksum
for (size_t i = 0; i < bytes_read; i++) {
unsigned char bucket = (total_bytes + i) % 32;
checksum[bucket] ^= buffer[i] ^ (((total_bytes + i) >> 8) & 0xFF) ^
checksum[bucket] ^= buffer[i] ^ (((total_bytes + i) >> 8) & 0xFF) ^
(((total_bytes + i) >> 16) & 0xFF) ^ (((total_bytes + i) >> 24) & 0xFF);
}
total_bytes += bytes_read;
@@ -4606,6 +4614,70 @@ int calculate_checksum(const char* filename, char* checksum_hex) {
return 0;
}
// Calculate checksum with progress display for large files
int calculate_checksum_with_progress(const char* filename, char* checksum_hex, int display_progress, uint64_t file_size) {
FILE* file = fopen(filename, "rb");
if (!file) {
return 1;
}
unsigned char checksum[32];
unsigned char buffer[64 * 1024]; // 64KB buffer for large files
size_t bytes_read;
// Initialize checksum
memset(checksum, 0, 32);
size_t total_bytes = 0;
time_t start_time = time(NULL);
// Calculate XOR checksum of entire file with progress
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// Process this chunk with XOR checksum
for (size_t i = 0; i < bytes_read; i++) {
unsigned char bucket = (total_bytes + i) % 32;
checksum[bucket] ^= buffer[i] ^ (((total_bytes + i) >> 8) & 0xFF) ^
(((total_bytes + i) >> 16) & 0xFF) ^ (((total_bytes + i) >> 24) & 0xFF);
}
total_bytes += bytes_read;
// Show progress for large files (every 64MB or if display_progress is enabled)
if (display_progress && file_size > 10 * 1024 * 1024 && total_bytes % (64 * 1024 * 1024) == 0) {
show_progress(total_bytes, file_size, start_time);
}
}
// Final progress update
if (display_progress && file_size > 10 * 1024 * 1024) {
show_progress(file_size, file_size, start_time);
printf("\n");
}
fclose(file);
// Now encrypt the checksum with the first 32 bytes of the pad
fseek(file = fopen(filename, "rb"), 0, SEEK_SET);
unsigned char pad_key[32];
if (fread(pad_key, 1, 32, file) != 32) {
fclose(file);
return 1;
}
fclose(file);
// XOR encrypt the checksum with pad data to create unique identifier
unsigned char encrypted_checksum[32];
for (int i = 0; i < 32; i++) {
encrypted_checksum[i] = checksum[i] ^ pad_key[i];
}
// Convert to hex string (64 characters)
for (int i = 0; i < 32; i++) {
sprintf(checksum_hex + (i * 2), "%02x", encrypted_checksum[i]);
}
checksum_hex[64] = '\0';
return 0;
}
// Unified pad selection function - extracts the best UI from handle_pads_menu()
char* select_pad_interactive(const char* title, const char* prompt, pad_filter_type_t filter_type, int allow_cancel) {
// Get list of pads from current directory
@@ -4845,7 +4917,7 @@ char* select_pad_interactive(const char* title, const char* prompt, pad_filter_t
int handle_pads_menu(void) {
printf("\n");
print_centered_header("Pad Management");
print_centered_header("Pad Management", 0);
// Get list of pads from current directory
DIR* dir = opendir(current_pads_dir);
@@ -5147,7 +5219,7 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
char header_text[128];
snprintf(header_text, sizeof(header_text), "Add Entropy to Pad: %.16s...", pad_chksum);
printf("\n");
print_centered_header(header_text);
print_centered_header(header_text, 0);
// Present entropy source selection menu with consistent formatting
printf("Select entropy source:\n");
@@ -5281,8 +5353,9 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
}
printf("\n🎉 SUCCESS! Your entire pad now has enhanced randomness!\n");
printf("Press Enter to continue...");
getchar();
// Use enhanced pause mechanism instead of simple getchar
print_centered_header("Pad Enhancement Complete", 1);
return 0;
}
@@ -5328,8 +5401,9 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
}
printf("\n🎉 SUCCESS! Your pad now has enhanced randomness!\n");
printf("Press Enter to continue...");
getchar();
// Use enhanced pause mechanism instead of simple getchar
print_centered_header("Entropy Enhancement Complete", 1);
return 0;
}
@@ -5338,7 +5412,7 @@ int handle_verify_pad(const char* pad_chksum) {
char header_text[128];
snprintf(header_text, sizeof(header_text), "Verify Pad Integrity: %.16s...", pad_chksum);
printf("\n");
print_centered_header(header_text);
print_centered_header(header_text, 0);
// Construct pad file path
char pad_path[1024];
@@ -5383,6 +5457,9 @@ int handle_verify_pad(const char* pad_chksum) {
}
printf("\n✅ This pad is safe to use for encryption.\n");
// Pause before returning to menu to let user see the verification results
print_centered_header("Pad Verification Complete", 1);
return 0;
} else {
printf("❌ FAILURE: Pad integrity check failed!\n");
@@ -5400,7 +5477,7 @@ int handle_delete_pad(const char* pad_chksum) {
char header_text[128];
snprintf(header_text, sizeof(header_text), "Delete Pad: %.16s...", pad_chksum);
printf("\n");
print_centered_header(header_text);
print_centered_header(header_text, 0);
// Construct pad and state file paths
char pad_path[1024];
@@ -5521,12 +5598,15 @@ int handle_delete_pad(const char* pad_chksum) {
printf(" Checksum: %.16s...\n", pad_chksum);
printf(" Both pad and state files have been removed\n");
// Pause before returning to menu to let user see the success message
print_centered_header("Pad Deletion Complete", 1);
return 0;
}
void print_usage(const char* program_name) {
printf("OTP Cipher - One Time Pad Implementation v0.3.10\n");
printf("OTP Cipher - One Time Pad Implementation v0.3.15\n");
printf("Built for testing entropy system\n");
printf("Usage:\n");
printf(" %s - Interactive mode\n", program_name);

3
otp.h
View File

@@ -223,6 +223,7 @@ void show_progress(uint64_t current, uint64_t total, time_t start_time);
int read_state_offset(const char* pad_chksum, uint64_t* offset);
int write_state_offset(const char* pad_chksum, uint64_t offset);
int calculate_checksum(const char* filename, char* checksum_hex);
int calculate_checksum_with_progress(const char* filename, char* checksum_hex, int display_progress, uint64_t file_size);
////////////////////////////////////////////////////////////////////////////////
// UNIVERSAL CORE FUNCTIONS FOR CODE CONSOLIDATION
@@ -254,7 +255,7 @@ unsigned char* custom_base64_decode(const char* input, int* output_length);
// Terminal dimension and UI functions
void init_terminal_dimensions(void);
void print_centered_header(const char* text);
void print_centered_header(const char* text, int pause_before_clear);
////////////////////////////////////////////////////////////////////////////////
// MENU SYSTEM FUNCTIONS