3752 lines
128 KiB
C
3752 lines
128 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#define _DEFAULT_SOURCE
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <dirent.h>
|
|
#include <time.h>
|
|
#include <ctype.h>
|
|
#include <termios.h>
|
|
#include <fcntl.h>
|
|
#include "src/version.h"
|
|
|
|
// Custom base64 character set
|
|
static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
static const int base64_decode_table[256] = {
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
|
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,
|
|
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
|
|
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
|
|
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
|
|
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
|
|
};
|
|
|
|
#define MAX_INPUT_SIZE 4096
|
|
#define MAX_LINE_LENGTH 1024
|
|
#define MAX_HASH_LENGTH 65
|
|
#define PROGRESS_UPDATE_INTERVAL (64 * 1024 * 1024) // 64MB intervals
|
|
#define DEFAULT_PADS_DIR "pads"
|
|
#define FILES_DIR "files"
|
|
#define MAX_ENTROPY_BUFFER 32768 // 32KB entropy buffer
|
|
|
|
// Global variable for current pads directory (can be local or OTP thumb drive)
|
|
static char current_pads_dir[512] = DEFAULT_PADS_DIR;
|
|
|
|
// Global variable for default pad path from preferences
|
|
static char default_pad_path[1024] = "";
|
|
|
|
// Function prototypes
|
|
int main(int argc, char* argv[]);
|
|
int interactive_mode(void);
|
|
int command_line_mode(int argc, char* argv[]);
|
|
int pipe_mode(int argc, char* argv[], const char* piped_text);
|
|
|
|
// Stdin detection functions
|
|
int has_stdin_data(void);
|
|
char* read_stdin_text(void);
|
|
|
|
// Preferences management functions
|
|
int load_preferences(void);
|
|
int save_preferences(void);
|
|
char* get_preference(const char* key);
|
|
int set_preference(const char* key, const char* value);
|
|
char* get_default_pad_path(void);
|
|
int set_default_pad_path(const char* pad_path);
|
|
|
|
// OTP thumb drive detection function
|
|
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size);
|
|
|
|
// Editor and file manager functions
|
|
char* get_preferred_editor(void);
|
|
char* get_preferred_file_manager(void);
|
|
int launch_text_editor(const char* initial_content, char* result_buffer, size_t buffer_size);
|
|
int launch_file_manager(const char* start_directory, char* selected_file, size_t buffer_size);
|
|
|
|
// Core functions
|
|
int generate_pad(uint64_t size_bytes, int show_progress);
|
|
int generate_pad_with_entropy(uint64_t size_bytes, int show_progress, int use_keyboard_entropy);
|
|
int encrypt_text(const char* pad_identifier, const char* input_text);
|
|
int decrypt_text(const char* pad_identifier, const char* encrypted_message);
|
|
int decrypt_text_silent(const char* pad_identifier, const char* encrypted_message);
|
|
int encrypt_file(const char* pad_identifier, const char* input_file, const char* output_file, int ascii_armor);
|
|
int decrypt_file(const char* input_file, const char* output_file);
|
|
int decrypt_binary_file(FILE* input_fp, const char* output_file);
|
|
int decrypt_ascii_file(const char* input_file, const char* output_file);
|
|
|
|
// Keyboard entropy functions
|
|
int setup_raw_terminal(struct termios* original_termios);
|
|
void restore_terminal(struct termios* original_termios);
|
|
int collect_keyboard_entropy(unsigned char* entropy_buffer, size_t max_size, size_t* collected);
|
|
void simple_entropy_mix(unsigned char* urandom_buffer, size_t buffer_size,
|
|
const unsigned char* entropy_data, size_t entropy_size);
|
|
|
|
// Directory management
|
|
int ensure_pads_directory(void);
|
|
void get_pad_path(const char* chksum, char* pad_path, char* state_path);
|
|
const char* get_files_directory(void);
|
|
void get_default_file_path(const char* filename, char* result_path, size_t result_size);
|
|
|
|
// Utility functions
|
|
uint64_t parse_size_string(const char* size_str);
|
|
char* find_pad_by_prefix(const char* prefix);
|
|
int list_available_pads(void);
|
|
int show_pad_info(const char* chksum);
|
|
void show_progress(uint64_t current, uint64_t total, time_t start_time);
|
|
|
|
// File operations
|
|
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);
|
|
char* custom_base64_encode(const unsigned char* input, int length);
|
|
unsigned char* custom_base64_decode(const char* input, int* output_length);
|
|
|
|
// Menu functions
|
|
void show_main_menu(void);
|
|
int handle_generate_menu(void);
|
|
int handle_encrypt_menu(void);
|
|
int handle_decrypt_menu(void);
|
|
int handle_pads_menu(void);
|
|
int handle_text_encrypt(void);
|
|
int handle_file_encrypt(void);
|
|
|
|
// Enhanced input functions
|
|
int get_filename_with_default(const char* prompt, const char* default_path, char* result, size_t result_size);
|
|
|
|
// Directory display functions
|
|
void get_directory_display(const char* file_path, char* result, size_t result_size);
|
|
|
|
void print_usage(const char* program_name);
|
|
|
|
int main(int argc, char* argv[]) {
|
|
// Load preferences first
|
|
load_preferences();
|
|
|
|
// Check for piped input first (before any output)
|
|
int is_pipe_mode = (argc == 1 && has_stdin_data());
|
|
|
|
// Check for decrypt command with piped input
|
|
int is_decrypt_pipe = (argc == 2 &&
|
|
(strcmp(argv[1], "decrypt") == 0 || strcmp(argv[1], "-d") == 0) &&
|
|
has_stdin_data());
|
|
|
|
// Check for OTP thumb drive on startup
|
|
char otp_drive_path[512];
|
|
if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) {
|
|
// Only show messages in interactive/command mode, not pipe mode
|
|
if (!is_pipe_mode && !is_decrypt_pipe) {
|
|
printf("Detected OTP thumb drive: %s\n", otp_drive_path);
|
|
printf("Using as default pads directory for this session.\n\n");
|
|
}
|
|
strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1);
|
|
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
|
|
}
|
|
|
|
if (is_pipe_mode) {
|
|
// No arguments but has piped data - enter pipe mode
|
|
char* piped_text = read_stdin_text();
|
|
if (piped_text) {
|
|
int result = pipe_mode(argc, argv, piped_text);
|
|
free(piped_text);
|
|
return result;
|
|
}
|
|
// If reading stdin failed, fall back to interactive mode
|
|
return interactive_mode();
|
|
} else if (argc == 1) {
|
|
return interactive_mode();
|
|
} else {
|
|
return command_line_mode(argc, argv);
|
|
}
|
|
}
|
|
|
|
int interactive_mode(void) {
|
|
char input[10];
|
|
|
|
while (1) {
|
|
show_main_menu();
|
|
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
|
printf("Goodbye!\n");
|
|
break;
|
|
}
|
|
|
|
char choice = toupper(input[0]);
|
|
|
|
switch (choice) {
|
|
case 'T':
|
|
handle_text_encrypt();
|
|
break;
|
|
case 'F':
|
|
handle_file_encrypt();
|
|
break;
|
|
case 'D':
|
|
handle_decrypt_menu();
|
|
break;
|
|
case 'P':
|
|
handle_pads_menu();
|
|
break;
|
|
case 'X':
|
|
case 'Q':
|
|
printf("Goodbye!\n");
|
|
return 0;
|
|
default:
|
|
printf("Invalid choice. Please try again.\n");
|
|
break;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int command_line_mode(int argc, char* argv[]) {
|
|
// Check for help flags first
|
|
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0 ||
|
|
strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0 ||
|
|
strcmp(argv[1], "help") == 0) {
|
|
print_usage(argv[0]);
|
|
return 0;
|
|
}
|
|
|
|
if (strcmp(argv[1], "generate") == 0 || strcmp(argv[1], "-g") == 0) {
|
|
if (argc != 3) {
|
|
printf("Usage: %s generate|-g <size>\n", argv[0]);
|
|
printf("Size examples: 1024, 1GB, 5TB, 512MB\n");
|
|
return 1;
|
|
}
|
|
uint64_t size = parse_size_string(argv[2]);
|
|
if (size == 0) {
|
|
printf("Error: Invalid size format\n");
|
|
return 1;
|
|
}
|
|
return generate_pad_with_entropy(size, 1, 0); // No keyboard entropy for command line
|
|
}
|
|
else if (strcmp(argv[1], "encrypt") == 0 || strcmp(argv[1], "-e") == 0) {
|
|
if (argc < 3 || argc > 4) {
|
|
printf("Usage: %s encrypt|-e <pad_chksum_or_prefix> [text_to_encrypt]\n", argv[0]);
|
|
return 1;
|
|
}
|
|
// Pass text if provided, otherwise NULL for interactive mode
|
|
const char* text = (argc == 4) ? argv[3] : NULL;
|
|
return encrypt_text(argv[2], text);
|
|
}
|
|
else if (strcmp(argv[1], "decrypt") == 0 || strcmp(argv[1], "-d") == 0) {
|
|
if (argc == 2) {
|
|
// Check for piped input first
|
|
if (has_stdin_data()) {
|
|
// Piped decrypt mode - read stdin and decrypt silently
|
|
char* piped_message = read_stdin_text();
|
|
if (piped_message) {
|
|
int result = decrypt_text_silent(NULL, piped_message);
|
|
free(piped_message);
|
|
return result;
|
|
}
|
|
}
|
|
// Interactive mode - no arguments needed
|
|
return decrypt_text(NULL, NULL);
|
|
}
|
|
else if (argc == 3) {
|
|
// Check if the argument looks like an encrypted message (starts with -----)
|
|
if (strncmp(argv[2], "-----BEGIN OTP MESSAGE-----", 27) == 0) {
|
|
// Inline decrypt with message only
|
|
return decrypt_text(NULL, argv[2]);
|
|
} else {
|
|
// Check if it's a file (contains . or ends with known extensions)
|
|
if (strstr(argv[2], ".") != NULL) {
|
|
// Treat as file
|
|
return decrypt_file(argv[2], NULL);
|
|
} else {
|
|
// Interactive decrypt with pad hint (legacy support)
|
|
return decrypt_text(argv[2], NULL);
|
|
}
|
|
}
|
|
}
|
|
else if (argc == 4) {
|
|
// Check for -o flag for output file
|
|
if (strcmp(argv[2], "-o") == 0) {
|
|
printf("Usage: %s decrypt|-d <input_file> [-o <output_file>]\n", argv[0]);
|
|
return 1;
|
|
} else {
|
|
// Legacy format: pad_chksum and message, or file with output
|
|
return decrypt_text(argv[2], argv[3]);
|
|
}
|
|
}
|
|
else if (argc == 5 && strcmp(argv[3], "-o") == 0) {
|
|
// File decryption with output: -d <input_file> -o <output_file>
|
|
return decrypt_file(argv[2], argv[4]);
|
|
}
|
|
else {
|
|
printf("Usage: %s decrypt|-d [encrypted_message|file] [-o output_file]\n", argv[0]);
|
|
printf(" %s decrypt|-d [encrypted_message] (pad info from message)\n", argv[0]);
|
|
return 1;
|
|
}
|
|
}
|
|
else if (strcmp(argv[1], "-f") == 0) {
|
|
// File encryption mode: -f <input_file> <pad_prefix> [-a] [-o <output_file>]
|
|
if (argc < 4) {
|
|
printf("Usage: %s -f <input_file> <pad_prefix> [-a] [-o <output_file>]\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
const char* input_file = argv[2];
|
|
const char* pad_prefix = argv[3];
|
|
int ascii_armor = 0;
|
|
const char* output_file = NULL;
|
|
|
|
// Parse optional flags
|
|
for (int i = 4; i < argc; i++) {
|
|
if (strcmp(argv[i], "-a") == 0) {
|
|
ascii_armor = 1;
|
|
} else if (strcmp(argv[i], "-o") == 0 && i + 1 < argc) {
|
|
output_file = argv[++i];
|
|
}
|
|
}
|
|
|
|
return encrypt_file(pad_prefix, input_file, output_file, ascii_armor);
|
|
}
|
|
else if (strcmp(argv[1], "list") == 0 || strcmp(argv[1], "-l") == 0) {
|
|
return list_available_pads();
|
|
}
|
|
else {
|
|
print_usage(argv[0]);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
void show_main_menu(void) {
|
|
|
|
printf("\n\n\n\n=========================== Main Menu - OTP %s ===========================\n\n", get_version() );
|
|
|
|
printf(" \033[4mT\033[0mext encrypt\n");
|
|
printf(" \033[4mF\033[0mile encrypt\n");
|
|
printf(" \033[4mD\033[0mecrypt\n");
|
|
printf(" \033[4mP\033[0mads\n");
|
|
printf(" E\033[4mx\033[0mit\n");
|
|
printf("\nSelect option: ");
|
|
}
|
|
|
|
int handle_generate_menu(void) {
|
|
printf("\n=== Generate New Pad ===\n");
|
|
printf("Enter pad size (examples: 1GB, 5TB, 512MB, 2048): ");
|
|
|
|
char size_input[64];
|
|
if (!fgets(size_input, sizeof(size_input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
size_input[strcspn(size_input, "\n")] = 0;
|
|
uint64_t size = parse_size_string(size_input);
|
|
|
|
if (size == 0) {
|
|
printf("Error: Invalid size format\n");
|
|
return 1;
|
|
}
|
|
|
|
// Ask about keyboard entropy
|
|
printf("\nAdd keyboard entropy for enhanced security? (y/N): ");
|
|
char entropy_choice[10];
|
|
int use_keyboard_entropy = 0;
|
|
|
|
if (fgets(entropy_choice, sizeof(entropy_choice), stdin)) {
|
|
if (entropy_choice[0] == 'y' || entropy_choice[0] == 'Y') {
|
|
use_keyboard_entropy = 1;
|
|
}
|
|
}
|
|
|
|
double size_gb = (double)size / (1024.0 * 1024.0 * 1024.0);
|
|
if (use_keyboard_entropy) {
|
|
printf("Generating %.2f GB pad with keyboard entropy...\n", size_gb);
|
|
} else {
|
|
printf("Generating %.2f GB pad...\n", size_gb);
|
|
}
|
|
|
|
return generate_pad_with_entropy(size, 1, use_keyboard_entropy);
|
|
}
|
|
|
|
int handle_encrypt_menu(void) {
|
|
printf("\n=== Encrypt Data ===\n");
|
|
|
|
int pad_count = list_available_pads();
|
|
if (pad_count == 0) {
|
|
printf("No pads available. Generate a pad first.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Ask user to choose between text and file encryption
|
|
printf("\nSelect encryption type:\n");
|
|
printf(" 1. Text message\n");
|
|
printf(" 2. File\n");
|
|
printf("Enter choice (1-2): ");
|
|
|
|
char choice_input[10];
|
|
if (!fgets(choice_input, sizeof(choice_input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
int choice = atoi(choice_input);
|
|
|
|
if (choice == 1) {
|
|
// Text encryption
|
|
printf("\nPad selection options:\n");
|
|
printf(" 1. Select from numbered list\n");
|
|
printf(" 2. Enter checksum/prefix manually\n");
|
|
printf(" 3. Browse pad files\n");
|
|
printf("Enter choice (1-3): ");
|
|
|
|
char pad_choice[10];
|
|
if (!fgets(pad_choice, sizeof(pad_choice), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
char input[MAX_HASH_LENGTH];
|
|
if (atoi(pad_choice) == 3) {
|
|
// Use file manager to browse pads directory
|
|
char selected_pad[512];
|
|
if (launch_file_manager("pads", selected_pad, sizeof(selected_pad)) == 0) {
|
|
// Extract checksum from selected .pad file
|
|
char* filename = strrchr(selected_pad, '/');
|
|
if (!filename) filename = selected_pad;
|
|
else filename++; // Skip the '/'
|
|
|
|
// Remove .pad extension to get checksum
|
|
if (strlen(filename) == 68 && strstr(filename, ".pad")) {
|
|
strncpy(input, filename, 64);
|
|
input[64] = '\0';
|
|
} else {
|
|
printf("Invalid pad file selected.\n");
|
|
return 1;
|
|
}
|
|
} else {
|
|
printf("Falling back to manual pad selection.\n");
|
|
printf("Enter pad selection (number, chksum, or prefix): ");
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
input[strcspn(input, "\n")] = 0;
|
|
}
|
|
} else {
|
|
// Manual pad selection
|
|
printf("Enter pad selection (number, chksum, or prefix): ");
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
input[strcspn(input, "\n")] = 0;
|
|
}
|
|
|
|
return encrypt_text(input, NULL); // NULL for interactive mode
|
|
}
|
|
else if (choice == 2) {
|
|
// File encryption
|
|
printf("\nFile selection options:\n");
|
|
printf(" 1. Type file path directly\n");
|
|
printf(" 2. Use file manager\n");
|
|
printf("Enter choice (1-2): ");
|
|
|
|
char file_choice[10];
|
|
char input_file[512];
|
|
|
|
if (!fgets(file_choice, sizeof(file_choice), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
if (atoi(file_choice) == 2) {
|
|
// Use file manager
|
|
if (launch_file_manager(".", input_file, sizeof(input_file)) != 0) {
|
|
printf("Falling back to manual file path entry.\n");
|
|
printf("Enter input file path: ");
|
|
if (!fgets(input_file, sizeof(input_file), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
input_file[strcspn(input_file, "\n")] = 0;
|
|
}
|
|
} else {
|
|
// Direct file path input
|
|
printf("Enter input file path: ");
|
|
if (!fgets(input_file, sizeof(input_file), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
input_file[strcspn(input_file, "\n")] = 0;
|
|
}
|
|
|
|
// Check if file exists
|
|
if (access(input_file, R_OK) != 0) {
|
|
printf("Error: File '%s' not found or cannot be read\n", input_file);
|
|
return 1;
|
|
}
|
|
|
|
printf("\nEnter pad selection (number, chksum, or prefix): ");
|
|
char pad_input[MAX_HASH_LENGTH];
|
|
if (!fgets(pad_input, sizeof(pad_input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
pad_input[strcspn(pad_input, "\n")] = 0;
|
|
|
|
// Ask for output format
|
|
printf("\nSelect output format:\n");
|
|
printf(" 1. Binary (.otp) - preserves file permissions\n");
|
|
printf(" 2. ASCII (.otp.asc) - text-safe format\n");
|
|
printf("Enter choice (1-2): ");
|
|
|
|
char format_input[10];
|
|
if (!fgets(format_input, sizeof(format_input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
int ascii_armor = (atoi(format_input) == 2) ? 1 : 0;
|
|
|
|
// Generate default output filename with files directory and use enhanced input function
|
|
char default_output[1024]; // Increased size to prevent truncation warnings
|
|
char temp_default[1024];
|
|
|
|
// Generate base filename with appropriate extension
|
|
if (ascii_armor) {
|
|
snprintf(temp_default, sizeof(temp_default), "%s.otp.asc", input_file);
|
|
} else {
|
|
snprintf(temp_default, sizeof(temp_default), "%s.otp", input_file);
|
|
}
|
|
|
|
// Apply files directory default path
|
|
get_default_file_path(temp_default, default_output, sizeof(default_output));
|
|
|
|
char output_file[512];
|
|
if (get_filename_with_default("Output filename:", default_output, output_file, sizeof(output_file)) != 0) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
const char* output_filename = output_file;
|
|
|
|
return encrypt_file(pad_input, input_file, output_filename, ascii_armor);
|
|
}
|
|
else {
|
|
printf("Invalid choice. Please enter 1 or 2.\n");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int handle_decrypt_menu(void) {
|
|
printf("\n=== Smart Decrypt ===\n");
|
|
printf("Enter encrypted data (paste ASCII armor), file path, or press Enter to browse files:\n");
|
|
|
|
char input_line[MAX_LINE_LENGTH];
|
|
if (!fgets(input_line, sizeof(input_line), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
// Remove newline
|
|
input_line[strcspn(input_line, "\n")] = 0;
|
|
|
|
if (strlen(input_line) == 0) {
|
|
// Empty input - launch file manager to browse for files
|
|
char selected_file[512];
|
|
if (launch_file_manager(get_files_directory(), selected_file, sizeof(selected_file)) != 0) {
|
|
printf("Error: Could not launch file manager\n");
|
|
return 1;
|
|
}
|
|
|
|
// Generate smart default output filename with files directory and use enhanced input function
|
|
char temp_default[512];
|
|
char default_output[512];
|
|
strncpy(temp_default, selected_file, sizeof(temp_default) - 1);
|
|
temp_default[sizeof(temp_default) - 1] = '\0';
|
|
|
|
// Remove common encrypted extensions to get a better default
|
|
if (strstr(temp_default, ".otp.asc")) {
|
|
// Replace .otp.asc with original extension or no extension
|
|
char* ext_pos = strstr(temp_default, ".otp.asc");
|
|
*ext_pos = '\0';
|
|
} else if (strstr(temp_default, ".otp")) {
|
|
// Replace .otp with original extension or no extension
|
|
char* ext_pos = strstr(temp_default, ".otp");
|
|
*ext_pos = '\0';
|
|
} else {
|
|
// No recognized encrypted extension, add .decrypted suffix
|
|
strncat(temp_default, ".decrypted", sizeof(temp_default) - strlen(temp_default) - 1);
|
|
}
|
|
|
|
// Apply files directory default path
|
|
get_default_file_path(temp_default, default_output, sizeof(default_output));
|
|
|
|
char output_file[512];
|
|
if (get_filename_with_default("Output filename:", default_output, output_file, sizeof(output_file)) != 0) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
return decrypt_file(selected_file, output_file);
|
|
}
|
|
else if (strncmp(input_line, "-----BEGIN OTP MESSAGE-----", 27) == 0) {
|
|
// Looks like ASCII armor - collect the full message
|
|
char full_message[MAX_INPUT_SIZE * 4] = {0};
|
|
strcat(full_message, input_line);
|
|
strcat(full_message, "\n");
|
|
|
|
printf("Continue pasting the message (end with -----END OTP MESSAGE-----):\n");
|
|
|
|
char line[MAX_LINE_LENGTH];
|
|
while (fgets(line, sizeof(line), stdin)) {
|
|
strncat(full_message, line, sizeof(full_message) - strlen(full_message) - 1);
|
|
if (strstr(line, "-----END OTP MESSAGE-----")) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return decrypt_text(NULL, full_message);
|
|
}
|
|
else {
|
|
// Check if it looks like a file path
|
|
if (access(input_line, R_OK) == 0) {
|
|
// It's a valid file - decrypt it with enhanced input for output filename
|
|
char temp_default[512];
|
|
char default_output[512];
|
|
strncpy(temp_default, input_line, sizeof(temp_default) - 1);
|
|
temp_default[sizeof(temp_default) - 1] = '\0';
|
|
|
|
// Remove common encrypted extensions to get a better default
|
|
if (strstr(temp_default, ".otp.asc")) {
|
|
// Replace .otp.asc with original extension or no extension
|
|
char* ext_pos = strstr(temp_default, ".otp.asc");
|
|
*ext_pos = '\0';
|
|
} else if (strstr(temp_default, ".otp")) {
|
|
// Replace .otp with original extension or no extension
|
|
char* ext_pos = strstr(temp_default, ".otp");
|
|
*ext_pos = '\0';
|
|
} else {
|
|
// No recognized encrypted extension, add .decrypted suffix
|
|
strncat(temp_default, ".decrypted", sizeof(temp_default) - strlen(temp_default) - 1);
|
|
}
|
|
|
|
// Apply files directory default path
|
|
get_default_file_path(temp_default, default_output, sizeof(default_output));
|
|
|
|
char output_file[512];
|
|
if (get_filename_with_default("Output filename:", default_output, output_file, sizeof(output_file)) != 0) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
return decrypt_file(input_line, output_file);
|
|
} else {
|
|
printf("Input not recognized as ASCII armor or valid file path.\n");
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
uint64_t parse_size_string(const char* size_str) {
|
|
if (!size_str) return 0;
|
|
|
|
char* endptr;
|
|
double value = strtod(size_str, &endptr);
|
|
|
|
if (value <= 0) return 0;
|
|
|
|
// Skip whitespace
|
|
while (*endptr && isspace(*endptr)) endptr++;
|
|
|
|
uint64_t multiplier = 1;
|
|
|
|
if (*endptr) {
|
|
char unit[4];
|
|
strncpy(unit, endptr, 3);
|
|
unit[3] = '\0';
|
|
|
|
// Convert to uppercase
|
|
for (int i = 0; unit[i]; i++) {
|
|
unit[i] = toupper(unit[i]);
|
|
}
|
|
|
|
if (strcmp(unit, "K") == 0 || strcmp(unit, "KB") == 0) {
|
|
multiplier = 1024ULL;
|
|
} else if (strcmp(unit, "M") == 0 || strcmp(unit, "MB") == 0) {
|
|
multiplier = 1024ULL * 1024ULL;
|
|
} else if (strcmp(unit, "G") == 0 || strcmp(unit, "GB") == 0) {
|
|
multiplier = 1024ULL * 1024ULL * 1024ULL;
|
|
} else if (strcmp(unit, "T") == 0 || strcmp(unit, "TB") == 0) {
|
|
multiplier = 1024ULL * 1024ULL * 1024ULL * 1024ULL;
|
|
} else {
|
|
return 0; // Invalid unit
|
|
}
|
|
} else {
|
|
// No unit specified, treat as bytes
|
|
multiplier = 1;
|
|
}
|
|
|
|
return (uint64_t)(value * multiplier);
|
|
}
|
|
|
|
char* find_pad_by_prefix(const char* prefix) {
|
|
DIR* dir = opendir(current_pads_dir);
|
|
if (!dir) {
|
|
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
|
|
return NULL;
|
|
}
|
|
|
|
struct dirent* entry;
|
|
char* matches[100]; // Store up to 100 matches
|
|
int match_count = 0;
|
|
|
|
// Always try hex prefix matching first
|
|
size_t prefix_len = strlen(prefix);
|
|
while ((entry = readdir(dir)) != NULL && match_count < 100) {
|
|
// Skip . and .. entries, and only process .pad files
|
|
if (entry->d_name[0] == '.') continue;
|
|
if (!strstr(entry->d_name, ".pad")) continue;
|
|
if (strlen(entry->d_name) != 68) continue; // 64 char chksum + ".pad"
|
|
|
|
// Compare prefix with the filename (checksum part)
|
|
if (strncmp(entry->d_name, prefix, prefix_len) == 0) {
|
|
matches[match_count] = malloc(65);
|
|
strncpy(matches[match_count], entry->d_name, 64);
|
|
matches[match_count][64] = '\0';
|
|
match_count++;
|
|
}
|
|
}
|
|
|
|
// If no hex prefix matches and it looks like a small number, try number selection
|
|
if (match_count == 0) {
|
|
char* endptr;
|
|
int selection = strtol(prefix, &endptr, 10);
|
|
if (*endptr == '\0' && selection > 0 && selection <= 100) {
|
|
// It's a number, find the nth pad
|
|
int current = 0;
|
|
rewinddir(dir);
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (entry->d_name[0] == '.') continue;
|
|
if (!strstr(entry->d_name, ".pad")) continue;
|
|
if (strlen(entry->d_name) != 68) continue;
|
|
|
|
current++;
|
|
if (current == selection) {
|
|
matches[match_count] = malloc(65);
|
|
strncpy(matches[match_count], entry->d_name, 64);
|
|
matches[match_count][64] = '\0';
|
|
match_count = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
if (match_count == 0) {
|
|
printf("No pads found matching '%s'\n", prefix);
|
|
printf("Available pads:\n");
|
|
list_available_pads();
|
|
return NULL;
|
|
} else if (match_count == 1) {
|
|
char* result = matches[0];
|
|
return result;
|
|
} else {
|
|
printf("Multiple matches found for '%s':\n", prefix);
|
|
for (int i = 0; i < match_count; i++) {
|
|
printf("%d. %.16s...\n", i + 1, matches[i]);
|
|
}
|
|
printf("Please be more specific or enter a number (1-%d): ", match_count);
|
|
fflush(stdout);
|
|
|
|
char choice_input[64];
|
|
if (fgets(choice_input, sizeof(choice_input), stdin)) {
|
|
choice_input[strcspn(choice_input, "\n")] = 0;
|
|
|
|
// Check if it's a number
|
|
char* endptr;
|
|
int choice = strtol(choice_input, &endptr, 10);
|
|
if (*endptr == '\0' && choice >= 1 && choice <= match_count) {
|
|
// Valid choice, return selected pad
|
|
char* result = matches[choice - 1];
|
|
// Free the others
|
|
for (int i = 0; i < match_count; i++) {
|
|
if (i != choice - 1) {
|
|
free(matches[i]);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
// Invalid choice or no input, free all and return NULL
|
|
for (int i = 0; i < match_count; i++) {
|
|
free(matches[i]);
|
|
}
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
int list_available_pads(void) {
|
|
DIR* dir = opendir(current_pads_dir);
|
|
if (!dir) {
|
|
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
|
|
return 0;
|
|
}
|
|
|
|
struct dirent* entry;
|
|
int count = 0;
|
|
|
|
printf("\nAvailable pads:\n");
|
|
printf("%-4s %-20s %-12s %-12s %-8s\n", "No.", "ChkSum", "Size", "Used", "% Used");
|
|
printf("%-4s %-20s %-12s %-12s %-8s\n", "---", "-------------------", "----------", "----------", "------");
|
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (strstr(entry->d_name, ".pad") && strlen(entry->d_name) == 68) {
|
|
count++;
|
|
char chksum[65];
|
|
strncpy(chksum, entry->d_name, 64);
|
|
chksum[64] = '\0';
|
|
|
|
// Get pad file size
|
|
char full_path[1024]; // Increased buffer size to accommodate longer paths
|
|
snprintf(full_path, sizeof(full_path), "%s/%s", current_pads_dir, entry->d_name);
|
|
struct stat st;
|
|
if (stat(full_path, &st) == 0) {
|
|
// Get used bytes from state
|
|
uint64_t used_bytes;
|
|
read_state_offset(chksum, &used_bytes);
|
|
|
|
// Format sizes
|
|
char size_str[32], used_str[32];
|
|
|
|
// Format total size
|
|
if (st.st_size < 1024) {
|
|
snprintf(size_str, sizeof(size_str), "%luB", st.st_size);
|
|
} else if (st.st_size < 1024 * 1024) {
|
|
snprintf(size_str, sizeof(size_str), "%.1fKB", (double)st.st_size / 1024.0);
|
|
} else if (st.st_size < 1024 * 1024 * 1024) {
|
|
snprintf(size_str, sizeof(size_str), "%.1fMB", (double)st.st_size / (1024.0 * 1024.0));
|
|
} else {
|
|
snprintf(size_str, sizeof(size_str), "%.2fGB", (double)st.st_size / (1024.0 * 1024.0 * 1024.0));
|
|
}
|
|
|
|
// Format used size
|
|
if (used_bytes < 1024) {
|
|
snprintf(used_str, sizeof(used_str), "%luB", used_bytes);
|
|
} else if (used_bytes < 1024 * 1024) {
|
|
snprintf(used_str, sizeof(used_str), "%.1fKB", (double)used_bytes / 1024.0);
|
|
} else if (used_bytes < 1024 * 1024 * 1024) {
|
|
snprintf(used_str, sizeof(used_str), "%.1fMB", (double)used_bytes / (1024.0 * 1024.0));
|
|
} else {
|
|
snprintf(used_str, sizeof(used_str), "%.2fGB", (double)used_bytes / (1024.0 * 1024.0 * 1024.0));
|
|
}
|
|
|
|
// Calculate percentage
|
|
double percentage = (double)used_bytes / st.st_size * 100.0;
|
|
|
|
printf("%-4d %-20.16s %-12s %-12s %.1f%%\n", count, chksum, size_str, used_str, percentage);
|
|
}
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
if (count == 0) {
|
|
printf("No pads found.\n");
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
int show_pad_info(const char* chksum) {
|
|
char pad_filename[MAX_HASH_LENGTH + 10];
|
|
char state_filename[MAX_HASH_LENGTH + 10];
|
|
|
|
snprintf(pad_filename, sizeof(pad_filename), "%s.pad", chksum);
|
|
snprintf(state_filename, sizeof(state_filename), "%s.state", chksum);
|
|
|
|
struct stat st;
|
|
if (stat(pad_filename, &st) != 0) {
|
|
printf("Pad not found: %s\n", chksum);
|
|
return 1;
|
|
}
|
|
|
|
uint64_t used_bytes;
|
|
read_state_offset(chksum, &used_bytes);
|
|
|
|
printf("=== Pad Information ===\n");
|
|
printf("ChkSum: %s\n", chksum);
|
|
printf("File: %s\n", pad_filename);
|
|
|
|
double size_gb = (double)st.st_size / (1024.0 * 1024.0 * 1024.0);
|
|
double used_gb = (double)used_bytes / (1024.0 * 1024.0 * 1024.0);
|
|
double remaining_gb = (double)(st.st_size - used_bytes) / (1024.0 * 1024.0 * 1024.0);
|
|
|
|
printf("Total size: %.2f GB (%lu bytes)\n", size_gb, st.st_size);
|
|
printf("Used: %.2f GB (%lu bytes)\n", used_gb, used_bytes);
|
|
printf("Remaining: %.2f GB (%lu bytes)\n", remaining_gb, st.st_size - used_bytes);
|
|
printf("Usage: %.1f%%\n", (double)used_bytes / st.st_size * 100.0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
void show_progress(uint64_t current, uint64_t total, time_t start_time) {
|
|
time_t now = time(NULL);
|
|
double elapsed = difftime(now, start_time);
|
|
|
|
if (elapsed < 1.0) elapsed = 1.0; // Avoid division by zero
|
|
|
|
double percentage = (double)current / total * 100.0;
|
|
double speed = (double)current / elapsed / (1024.0 * 1024.0); // MB/s
|
|
|
|
uint64_t remaining_bytes = total - current;
|
|
double eta = remaining_bytes / (current / elapsed);
|
|
|
|
printf("\rProgress: %.1f%% (%.1f MB/s, ETA: %.0fs) ", percentage, speed, eta);
|
|
fflush(stdout);
|
|
}
|
|
|
|
int generate_pad(uint64_t size_bytes, int display_progress) {
|
|
char temp_filename[32];
|
|
char pad_filename[MAX_HASH_LENGTH + 10];
|
|
char state_filename[MAX_HASH_LENGTH + 10];
|
|
char chksum_hex[MAX_HASH_LENGTH];
|
|
|
|
// Create temporary filename
|
|
snprintf(temp_filename, sizeof(temp_filename), "temp_%ld.pad", time(NULL));
|
|
|
|
FILE* urandom = fopen("/dev/urandom", "rb");
|
|
if (!urandom) {
|
|
printf("Error: Cannot open /dev/urandom\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE* pad_file = fopen(temp_filename, "wb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot create temporary pad file %s\n", temp_filename);
|
|
fclose(urandom);
|
|
return 1;
|
|
}
|
|
|
|
unsigned char buffer[64 * 1024]; // 64KB buffer
|
|
uint64_t bytes_written = 0;
|
|
time_t start_time = time(NULL);
|
|
|
|
if (display_progress) {
|
|
printf("Generating pad...\n");
|
|
}
|
|
|
|
while (bytes_written < size_bytes) {
|
|
uint64_t chunk_size = sizeof(buffer);
|
|
if (size_bytes - bytes_written < chunk_size) {
|
|
chunk_size = size_bytes - bytes_written;
|
|
}
|
|
|
|
if (fread(buffer, 1, (size_t)chunk_size, urandom) != (size_t)chunk_size) {
|
|
printf("Error: Failed to read from /dev/urandom\n");
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
if (fwrite(buffer, 1, (size_t)chunk_size, pad_file) != (size_t)chunk_size) {
|
|
printf("Error: Failed to write to pad file\n");
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
bytes_written += chunk_size;
|
|
|
|
if (display_progress && bytes_written % PROGRESS_UPDATE_INTERVAL == 0) {
|
|
show_progress(bytes_written, size_bytes, start_time);
|
|
}
|
|
}
|
|
|
|
if (display_progress) {
|
|
show_progress(size_bytes, size_bytes, start_time);
|
|
printf("\n");
|
|
}
|
|
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
|
|
// Calculate XOR checksum of the pad file
|
|
if (calculate_checksum(temp_filename, chksum_hex) != 0) {
|
|
printf("Error: Cannot calculate pad checksum\n");
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
// Rename file to its chksum
|
|
snprintf(pad_filename, sizeof(pad_filename), "%s.pad", chksum_hex);
|
|
snprintf(state_filename, sizeof(state_filename), "%s.state", chksum_hex);
|
|
|
|
if (rename(temp_filename, pad_filename) != 0) {
|
|
printf("Error: Cannot rename pad file to chksum-based name\n");
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
// Set pad file to read-only
|
|
if (chmod(pad_filename, S_IRUSR) != 0) {
|
|
printf("Warning: Cannot set pad file to read-only\n");
|
|
}
|
|
|
|
// Initialize state file with offset 0
|
|
if (write_state_offset(chksum_hex, 0) != 0) {
|
|
printf("Error: Failed to create state file\n");
|
|
unlink(pad_filename);
|
|
return 1;
|
|
}
|
|
|
|
double size_gb = (double)size_bytes / (1024.0 * 1024.0 * 1024.0);
|
|
printf("Generated pad: %s (%.2f GB)\n", pad_filename, size_gb);
|
|
printf("Pad chksum: %s\n", chksum_hex);
|
|
printf("State file: %s\n", state_filename);
|
|
printf("Pad file set to read-only\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int generate_pad_with_entropy(uint64_t size_bytes, int display_progress, int use_keyboard_entropy) {
|
|
if (ensure_pads_directory() != 0) {
|
|
printf("Error: Cannot create pads directory\n");
|
|
return 1;
|
|
}
|
|
|
|
char temp_filename[64];
|
|
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));
|
|
|
|
FILE* urandom = fopen("/dev/urandom", "rb");
|
|
if (!urandom) {
|
|
printf("Error: Cannot open /dev/urandom\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE* pad_file = fopen(temp_filename, "wb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot create temporary pad file %s\n", temp_filename);
|
|
fclose(urandom);
|
|
return 1;
|
|
}
|
|
|
|
// Setup keyboard entropy collection if requested
|
|
struct termios original_termios;
|
|
unsigned char* entropy_buffer = NULL;
|
|
size_t entropy_collected = 0;
|
|
int terminal_setup = 0;
|
|
|
|
if (use_keyboard_entropy) {
|
|
entropy_buffer = malloc(MAX_ENTROPY_BUFFER);
|
|
if (!entropy_buffer) {
|
|
printf("Error: Cannot allocate entropy buffer\n");
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
if (setup_raw_terminal(&original_termios) == 0) {
|
|
terminal_setup = 1;
|
|
printf("Type random keys to add entropy (optional):\n");
|
|
} else {
|
|
printf("Warning: Cannot setup terminal for keyboard entropy collection\n");
|
|
use_keyboard_entropy = 0;
|
|
free(entropy_buffer);
|
|
entropy_buffer = NULL;
|
|
}
|
|
}
|
|
|
|
unsigned char urandom_buffer[64 * 1024]; // 64KB buffer
|
|
unsigned char output_buffer[64 * 1024];
|
|
uint64_t bytes_written = 0;
|
|
|
|
if (display_progress) {
|
|
printf("Generating pad...\n");
|
|
if (use_keyboard_entropy) {
|
|
printf("(Keyboard entropy: collecting...)\n");
|
|
}
|
|
}
|
|
|
|
while (bytes_written < size_bytes) {
|
|
uint64_t chunk_size = sizeof(urandom_buffer);
|
|
if (size_bytes - bytes_written < chunk_size) {
|
|
chunk_size = size_bytes - bytes_written;
|
|
}
|
|
|
|
// Read from /dev/urandom
|
|
if (fread(urandom_buffer, 1, (size_t)chunk_size, urandom) != (size_t)chunk_size) {
|
|
printf("Error: Failed to read from /dev/urandom\n");
|
|
if (terminal_setup) restore_terminal(&original_termios);
|
|
if (entropy_buffer) free(entropy_buffer);
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
if (use_keyboard_entropy && terminal_setup) {
|
|
// Collect available keyboard entropy
|
|
size_t chunk_entropy = 0;
|
|
collect_keyboard_entropy(entropy_buffer + entropy_collected,
|
|
MAX_ENTROPY_BUFFER - entropy_collected, &chunk_entropy);
|
|
entropy_collected += chunk_entropy;
|
|
|
|
if (entropy_collected > 512) { // Have enough entropy to mix
|
|
// Copy urandom data to output buffer
|
|
memcpy(output_buffer, urandom_buffer, chunk_size);
|
|
|
|
// Simple XOR mixing with keyboard entropy
|
|
simple_entropy_mix(output_buffer, chunk_size, entropy_buffer, entropy_collected);
|
|
|
|
// Reset entropy buffer for next chunk
|
|
entropy_collected = 0;
|
|
} else {
|
|
// Not enough entropy yet, use urandom only
|
|
memcpy(output_buffer, urandom_buffer, chunk_size);
|
|
}
|
|
} else {
|
|
// No keyboard entropy, use urandom directly
|
|
memcpy(output_buffer, urandom_buffer, chunk_size);
|
|
}
|
|
|
|
if (fwrite(output_buffer, 1, (size_t)chunk_size, pad_file) != (size_t)chunk_size) {
|
|
printf("Error: Failed to write to pad file\n");
|
|
if (terminal_setup) restore_terminal(&original_termios);
|
|
if (entropy_buffer) free(entropy_buffer);
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
bytes_written += chunk_size;
|
|
|
|
if (display_progress && bytes_written % PROGRESS_UPDATE_INTERVAL == 0) {
|
|
printf("\rProgress: %.1f%% ", (double)bytes_written / size_bytes * 100.0);
|
|
if (use_keyboard_entropy && terminal_setup) {
|
|
printf("(keyboard entropy: %.1fKB) ", (double)entropy_collected / 1024.0);
|
|
}
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
|
|
if (terminal_setup) {
|
|
restore_terminal(&original_termios);
|
|
}
|
|
if (entropy_buffer) {
|
|
free(entropy_buffer);
|
|
}
|
|
|
|
if (display_progress) {
|
|
printf("\rProgress: 100.0%%");
|
|
if (use_keyboard_entropy) {
|
|
printf(" (keyboard entropy: MIXED)");
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
fclose(urandom);
|
|
fclose(pad_file);
|
|
|
|
// Calculate XOR checksum of the pad file
|
|
if (calculate_checksum(temp_filename, chksum_hex) != 0) {
|
|
printf("Error: Cannot calculate pad checksum\n");
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
// Get final paths in pads directory
|
|
get_pad_path(chksum_hex, pad_path, state_path);
|
|
|
|
// Try rename first (works for same filesystem)
|
|
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
|
|
unlink(temp_filename);
|
|
}
|
|
|
|
// Set pad file to read-only
|
|
if (chmod(pad_path, S_IRUSR) != 0) {
|
|
printf("Warning: Cannot set pad file to read-only\n");
|
|
}
|
|
|
|
// Initialize state file with offset 32 (first 32 bytes used for checksum encryption)
|
|
FILE* state_file = fopen(state_path, "wb");
|
|
if (state_file) {
|
|
uint64_t reserved_bytes = 32;
|
|
fwrite(&reserved_bytes, sizeof(uint64_t), 1, state_file);
|
|
fclose(state_file);
|
|
} else {
|
|
printf("Error: Failed to create state file\n");
|
|
unlink(pad_path);
|
|
return 1;
|
|
}
|
|
|
|
double size_gb = (double)size_bytes / (1024.0 * 1024.0 * 1024.0);
|
|
printf("Generated pad: %s (%.2f GB)\n", pad_path, size_gb);
|
|
printf("Pad checksum: %s\n", chksum_hex);
|
|
printf("State file: %s\n", state_path);
|
|
if (use_keyboard_entropy) {
|
|
printf("Enhanced with keyboard entropy!\n");
|
|
}
|
|
printf("Pad file set to read-only\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int encrypt_text(const char* pad_identifier, const char* input_text) {
|
|
char* pad_chksum = find_pad_by_prefix(pad_identifier);
|
|
if (!pad_chksum) {
|
|
return 1;
|
|
}
|
|
|
|
char text_buffer[MAX_INPUT_SIZE];
|
|
char chksum_hex[MAX_HASH_LENGTH];
|
|
uint64_t current_offset;
|
|
|
|
char pad_path[MAX_HASH_LENGTH + 20];
|
|
char state_path[MAX_HASH_LENGTH + 20];
|
|
get_pad_path(pad_chksum, pad_path, state_path);
|
|
|
|
// Check if pad file exists
|
|
if (access(pad_path, R_OK) != 0) {
|
|
printf("Error: Pad file %s not found\n", pad_path);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Read current offset
|
|
if (read_state_offset(pad_chksum, ¤t_offset) != 0) {
|
|
printf("Error: Cannot read state file\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Ensure we never encrypt before offset 32 (reserved for checksum encryption)
|
|
if (current_offset < 32) {
|
|
printf("Warning: State offset below reserved area, adjusting to 32\n");
|
|
current_offset = 32;
|
|
if (write_state_offset(pad_chksum, current_offset) != 0) {
|
|
printf("Warning: Failed to update state file\n");
|
|
}
|
|
}
|
|
|
|
// Calculate XOR checksum of pad file
|
|
if (calculate_checksum(pad_path, chksum_hex) != 0) {
|
|
printf("Error: Cannot calculate pad checksum\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Get input text - either from parameter or user input
|
|
if (input_text != NULL) {
|
|
// Use provided text
|
|
strncpy(text_buffer, input_text, sizeof(text_buffer) - 1);
|
|
text_buffer[sizeof(text_buffer) - 1] = '\0';
|
|
} else {
|
|
// Get input text from user (interactive mode)
|
|
printf("\nText input options:\n");
|
|
printf(" 1. Type text directly\n");
|
|
printf(" 2. Use text editor\n");
|
|
printf("Enter choice (1-2): ");
|
|
|
|
char input_choice[10];
|
|
if (!fgets(input_choice, sizeof(input_choice), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
if (atoi(input_choice) == 2) {
|
|
// Use text editor
|
|
if (launch_text_editor(NULL, text_buffer, sizeof(text_buffer)) != 0) {
|
|
printf("Falling back to direct text input.\n");
|
|
printf("Enter text to encrypt: ");
|
|
fflush(stdout);
|
|
|
|
if (fgets(text_buffer, sizeof(text_buffer), stdin) == NULL) {
|
|
printf("Error: Failed to read input\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Remove newline if present
|
|
size_t len = strlen(text_buffer);
|
|
if (len > 0 && text_buffer[len - 1] == '\n') {
|
|
text_buffer[len - 1] = '\0';
|
|
}
|
|
}
|
|
} else {
|
|
// Direct text input
|
|
printf("Enter text to encrypt: ");
|
|
fflush(stdout);
|
|
|
|
if (fgets(text_buffer, sizeof(text_buffer), stdin) == NULL) {
|
|
printf("Error: Failed to read input\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Remove newline if present
|
|
size_t len = strlen(text_buffer);
|
|
if (len > 0 && text_buffer[len - 1] == '\n') {
|
|
text_buffer[len - 1] = '\0';
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t input_len = strlen(text_buffer);
|
|
if (input_len == 0) {
|
|
printf("Error: No input provided\n");
|
|
free(pad_chksum);
|
|
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");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
if (current_offset + input_len > (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);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Read pad data at current offset
|
|
FILE* pad_file = fopen(pad_path, "rb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot open pad file\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(pad_file, current_offset, SEEK_SET) != 0) {
|
|
printf("Error: Cannot seek to offset in pad file\n");
|
|
fclose(pad_file);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
unsigned char* pad_data = malloc(input_len);
|
|
if (fread(pad_data, 1, input_len, pad_file) != input_len) {
|
|
printf("Error: Cannot read pad data\n");
|
|
free(pad_data);
|
|
fclose(pad_file);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
fclose(pad_file);
|
|
|
|
// XOR encrypt the input
|
|
unsigned char* ciphertext = malloc(input_len);
|
|
for (size_t i = 0; i < input_len; i++) {
|
|
ciphertext[i] = text_buffer[i] ^ pad_data[i];
|
|
}
|
|
|
|
// Encode as base64
|
|
char* base64_cipher = custom_base64_encode(ciphertext, input_len);
|
|
|
|
// Update state offset
|
|
if (write_state_offset(pad_chksum, current_offset + input_len) != 0) {
|
|
printf("Warning: Failed to update state file\n");
|
|
}
|
|
|
|
// Output in ASCII armor format - clean format for piping, spaced format for interactive
|
|
int is_interactive = (input_text == NULL); // Interactive if no input_text provided
|
|
|
|
if (is_interactive) {
|
|
printf("\n\n-----BEGIN OTP MESSAGE-----\n");
|
|
} else {
|
|
printf("-----BEGIN OTP MESSAGE-----\n");
|
|
}
|
|
|
|
printf("Version: %s\n", get_version());
|
|
printf("Pad-ChkSum: %s\n", chksum_hex);
|
|
printf("Pad-Offset: %lu\n", current_offset);
|
|
printf("\n");
|
|
|
|
// Print base64 data in 64-character lines
|
|
int b64_len = strlen(base64_cipher);
|
|
for (int i = 0; i < b64_len; i += 64) {
|
|
printf("%.64s\n", base64_cipher + i);
|
|
}
|
|
|
|
if (is_interactive) {
|
|
printf("-----END OTP MESSAGE-----\n\n\n");
|
|
} else {
|
|
printf("-----END OTP MESSAGE-----\n");
|
|
}
|
|
|
|
// Cleanup
|
|
free(pad_data);
|
|
free(ciphertext);
|
|
free(base64_cipher);
|
|
free(pad_chksum);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int decrypt_text(const char* pad_identifier, const char* encrypted_message) {
|
|
// For command line mode, pad_identifier is ignored - we'll get the chksum from the message
|
|
(void)pad_identifier; // Suppress unused parameter warning
|
|
|
|
char line[MAX_LINE_LENGTH];
|
|
char stored_chksum[MAX_HASH_LENGTH];
|
|
char current_chksum[MAX_HASH_LENGTH];
|
|
uint64_t pad_offset;
|
|
char base64_data[MAX_INPUT_SIZE * 2] = {0};
|
|
int in_data_section = 0;
|
|
|
|
if (encrypted_message != NULL) {
|
|
// Parse provided encrypted message
|
|
char *message_copy = strdup(encrypted_message);
|
|
char *line_ptr = strtok(message_copy, "\n");
|
|
|
|
int found_begin = 0;
|
|
while (line_ptr != NULL) {
|
|
if (strcmp(line_ptr, "-----BEGIN OTP MESSAGE-----") == 0) {
|
|
found_begin = 1;
|
|
}
|
|
else if (strcmp(line_ptr, "-----END OTP MESSAGE-----") == 0) {
|
|
break;
|
|
}
|
|
else if (found_begin) {
|
|
if (strncmp(line_ptr, "Pad-ChkSum: ", 12) == 0) {
|
|
strncpy(stored_chksum, line_ptr + 12, 64);
|
|
stored_chksum[64] = '\0';
|
|
}
|
|
else if (strncmp(line_ptr, "Pad-Offset: ", 12) == 0) {
|
|
pad_offset = strtoull(line_ptr + 12, NULL, 10);
|
|
}
|
|
else if (strlen(line_ptr) == 0) {
|
|
in_data_section = 1;
|
|
}
|
|
else if (in_data_section) {
|
|
strncat(base64_data, line_ptr, sizeof(base64_data) - strlen(base64_data) - 1);
|
|
}
|
|
}
|
|
line_ptr = strtok(NULL, "\n");
|
|
}
|
|
free(message_copy);
|
|
|
|
if (!found_begin) {
|
|
printf("Error: Invalid message format - missing BEGIN header\n");
|
|
return 1;
|
|
}
|
|
} else {
|
|
// Interactive mode - read from stdin
|
|
printf("Enter encrypted message (paste the full ASCII armor block):\n");
|
|
|
|
// Read the ASCII armor format
|
|
int found_begin = 0;
|
|
while (fgets(line, sizeof(line), stdin)) {
|
|
line[strcspn(line, "\n")] = 0;
|
|
|
|
if (strcmp(line, "-----BEGIN OTP MESSAGE-----") == 0) {
|
|
found_begin = 1;
|
|
continue;
|
|
}
|
|
|
|
if (strcmp(line, "-----END OTP MESSAGE-----") == 0) {
|
|
break;
|
|
}
|
|
|
|
if (!found_begin) continue;
|
|
|
|
if (strncmp(line, "Pad-ChkSum: ", 12) == 0) {
|
|
strncpy(stored_chksum, line + 12, 64);
|
|
stored_chksum[64] = '\0';
|
|
}
|
|
else if (strncmp(line, "Pad-Offset: ", 12) == 0) {
|
|
pad_offset = strtoull(line + 12, NULL, 10);
|
|
}
|
|
else if (strlen(line) == 0) {
|
|
in_data_section = 1;
|
|
}
|
|
else if (in_data_section) {
|
|
strncat(base64_data, line, sizeof(base64_data) - strlen(base64_data) - 1);
|
|
}
|
|
}
|
|
|
|
if (!found_begin) {
|
|
printf("Error: Invalid message format - missing BEGIN header\n");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Now we have the pad chksum from the message, construct filename
|
|
char pad_path[MAX_HASH_LENGTH + 20];
|
|
char state_path[MAX_HASH_LENGTH + 20];
|
|
get_pad_path(stored_chksum, pad_path, state_path);
|
|
|
|
// Check if we have this pad
|
|
if (access(pad_path, R_OK) != 0) {
|
|
printf("Error: Required pad not found: %s\n", stored_chksum);
|
|
printf("Available pads:\n");
|
|
list_available_pads();
|
|
return 1;
|
|
}
|
|
|
|
// Verify pad integrity
|
|
if (calculate_checksum(pad_path, current_chksum) != 0) {
|
|
printf("Error: Cannot calculate current pad checksum\n");
|
|
return 1;
|
|
}
|
|
|
|
if (strcmp(stored_chksum, current_chksum) != 0) {
|
|
printf("Warning: Pad integrity check failed!\n");
|
|
printf("Expected: %s\n", stored_chksum);
|
|
printf("Current: %s\n", current_chksum);
|
|
printf("Continue anyway? (y/N): ");
|
|
fflush(stdout);
|
|
|
|
char response[10];
|
|
if (fgets(response, sizeof(response), stdin) == NULL ||
|
|
(response[0] != 'y' && response[0] != 'Y')) {
|
|
printf("Decryption aborted.\n");
|
|
return 1;
|
|
}
|
|
} else {
|
|
printf("Pad integrity: VERIFIED\n");
|
|
}
|
|
|
|
// Decode base64
|
|
int ciphertext_len;
|
|
unsigned char* ciphertext = custom_base64_decode(base64_data, &ciphertext_len);
|
|
if (!ciphertext) {
|
|
printf("Error: Invalid base64 data\n");
|
|
return 1;
|
|
}
|
|
|
|
// Read pad data at specified offset
|
|
FILE* pad_file = fopen(pad_path, "rb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot open pad file %s\n", pad_path);
|
|
free(ciphertext);
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(pad_file, pad_offset, SEEK_SET) != 0) {
|
|
printf("Error: Cannot seek to offset %lu in pad file\n", pad_offset);
|
|
free(ciphertext);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
|
|
unsigned char* pad_data = malloc(ciphertext_len);
|
|
if (fread(pad_data, 1, ciphertext_len, pad_file) != (size_t)ciphertext_len) {
|
|
printf("Error: Cannot read pad data\n");
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
fclose(pad_file);
|
|
|
|
// XOR decrypt
|
|
char* plaintext = malloc(ciphertext_len + 1);
|
|
for (int i = 0; i < ciphertext_len; i++) {
|
|
plaintext[i] = ciphertext[i] ^ pad_data[i];
|
|
}
|
|
plaintext[ciphertext_len] = '\0';
|
|
|
|
printf("Decrypted: %s\n", plaintext);
|
|
|
|
// Cleanup
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
free(plaintext);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int decrypt_text_silent(const char* pad_identifier, const char* encrypted_message) {
|
|
// For piped decrypt mode - silent operation with minimal output
|
|
(void)pad_identifier; // Suppress unused parameter warning
|
|
|
|
char line[MAX_LINE_LENGTH];
|
|
char stored_chksum[MAX_HASH_LENGTH];
|
|
char current_chksum[MAX_HASH_LENGTH];
|
|
uint64_t pad_offset;
|
|
char base64_data[MAX_INPUT_SIZE * 2] = {0};
|
|
int in_data_section = 0;
|
|
|
|
fprintf(stderr, "DEBUG: decrypt_text_silent called\n");
|
|
fprintf(stderr, "DEBUG: encrypted_message is %s\n", encrypted_message ? "not NULL" : "NULL");
|
|
|
|
if (encrypted_message != NULL) {
|
|
fprintf(stderr, "DEBUG: Message length: %lu\n", strlen(encrypted_message));
|
|
fprintf(stderr, "DEBUG: First 50 chars: %.50s\n", encrypted_message);
|
|
|
|
// Parse provided encrypted message
|
|
char *message_copy = strdup(encrypted_message);
|
|
char *line_ptr = strtok(message_copy, "\n");
|
|
|
|
int found_begin = 0;
|
|
while (line_ptr != NULL) {
|
|
fprintf(stderr, "DEBUG: Processing line: '%s'\n", line_ptr);
|
|
|
|
if (strcmp(line_ptr, "-----BEGIN OTP MESSAGE-----") == 0) {
|
|
found_begin = 1;
|
|
fprintf(stderr, "DEBUG: Found BEGIN header\n");
|
|
}
|
|
else if (strcmp(line_ptr, "-----END OTP MESSAGE-----") == 0) {
|
|
fprintf(stderr, "DEBUG: Found END header\n");
|
|
break;
|
|
}
|
|
else if (found_begin) {
|
|
if (strncmp(line_ptr, "Pad-ChkSum: ", 12) == 0) {
|
|
strncpy(stored_chksum, line_ptr + 12, 64);
|
|
stored_chksum[64] = '\0';
|
|
fprintf(stderr, "DEBUG: Found checksum: %.16s...\n", stored_chksum);
|
|
}
|
|
else if (strncmp(line_ptr, "Pad-Offset: ", 12) == 0) {
|
|
pad_offset = strtoull(line_ptr + 12, NULL, 10);
|
|
fprintf(stderr, "DEBUG: Found offset: %lu\n", pad_offset);
|
|
}
|
|
else if (strlen(line_ptr) == 0) {
|
|
in_data_section = 1;
|
|
fprintf(stderr, "DEBUG: Entering data section\n");
|
|
}
|
|
else if (in_data_section) {
|
|
strncat(base64_data, line_ptr, sizeof(base64_data) - strlen(base64_data) - 1);
|
|
fprintf(stderr, "DEBUG: Added data line: %s\n", line_ptr);
|
|
}
|
|
else if (strncmp(line_ptr, "Version:", 8) != 0 && strncmp(line_ptr, "Pad-", 4) != 0) {
|
|
// This might be base64 data without a blank line separator
|
|
strncat(base64_data, line_ptr, sizeof(base64_data) - strlen(base64_data) - 1);
|
|
fprintf(stderr, "DEBUG: Added potential data line: %s\n", line_ptr);
|
|
}
|
|
}
|
|
line_ptr = strtok(NULL, "\n");
|
|
}
|
|
free(message_copy);
|
|
|
|
fprintf(stderr, "DEBUG: Parsing complete. found_begin=%d, base64_data='%s'\n", found_begin, base64_data);
|
|
|
|
if (!found_begin) {
|
|
fprintf(stderr, "Error: Invalid message format - missing BEGIN header\n");
|
|
return 1;
|
|
}
|
|
} else {
|
|
fprintf(stderr, "Error: No encrypted message provided\n");
|
|
return 1;
|
|
}
|
|
|
|
// Now we have the pad chksum from the message, construct filename
|
|
char pad_path[MAX_HASH_LENGTH + 20];
|
|
char state_path[MAX_HASH_LENGTH + 20];
|
|
get_pad_path(stored_chksum, pad_path, state_path);
|
|
|
|
// Check if we have this pad
|
|
if (access(pad_path, R_OK) != 0) {
|
|
fprintf(stderr, "Error: Required pad not found: %s\n", stored_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Verify pad integrity (silent check)
|
|
if (calculate_checksum(pad_path, current_chksum) != 0) {
|
|
fprintf(stderr, "Error: Cannot calculate current pad checksum\n");
|
|
return 1;
|
|
}
|
|
|
|
if (strcmp(stored_chksum, current_chksum) != 0) {
|
|
fprintf(stderr, "Error: Pad integrity check failed!\n");
|
|
return 1;
|
|
}
|
|
|
|
// Decode base64
|
|
int ciphertext_len;
|
|
unsigned char* ciphertext = custom_base64_decode(base64_data, &ciphertext_len);
|
|
if (!ciphertext) {
|
|
fprintf(stderr, "Error: Invalid base64 data\n");
|
|
return 1;
|
|
}
|
|
|
|
// Read pad data at specified offset
|
|
FILE* pad_file = fopen(pad_path, "rb");
|
|
if (!pad_file) {
|
|
fprintf(stderr, "Error: Cannot open pad file %s\n", pad_path);
|
|
free(ciphertext);
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(pad_file, pad_offset, SEEK_SET) != 0) {
|
|
fprintf(stderr, "Error: Cannot seek to offset %lu in pad file\n", pad_offset);
|
|
free(ciphertext);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
|
|
unsigned char* pad_data = malloc(ciphertext_len);
|
|
if (fread(pad_data, 1, ciphertext_len, pad_file) != (size_t)ciphertext_len) {
|
|
fprintf(stderr, "Error: Cannot read pad data\n");
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
fclose(pad_file);
|
|
|
|
// XOR decrypt
|
|
char* plaintext = malloc(ciphertext_len + 1);
|
|
for (int i = 0; i < ciphertext_len; i++) {
|
|
plaintext[i] = ciphertext[i] ^ pad_data[i];
|
|
}
|
|
plaintext[ciphertext_len] = '\0';
|
|
|
|
// Output only the decrypted text - no extra messages
|
|
printf("%s", plaintext);
|
|
|
|
// Cleanup
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
free(plaintext);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int encrypt_file(const char* pad_identifier, const char* input_file, const char* output_file, int ascii_armor) {
|
|
char* pad_chksum = find_pad_by_prefix(pad_identifier);
|
|
if (!pad_chksum) {
|
|
return 1;
|
|
}
|
|
|
|
char chksum_hex[MAX_HASH_LENGTH];
|
|
uint64_t current_offset;
|
|
|
|
char pad_path[MAX_HASH_LENGTH + 20];
|
|
char state_path[MAX_HASH_LENGTH + 20];
|
|
get_pad_path(pad_chksum, pad_path, state_path);
|
|
|
|
// Check if input file exists and get its size
|
|
struct stat input_stat;
|
|
if (stat(input_file, &input_stat) != 0) {
|
|
printf("Error: Input file %s not found\n", input_file);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
uint64_t file_size = input_stat.st_size;
|
|
if (file_size == 0) {
|
|
printf("Error: Input file is empty\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Check if pad file exists
|
|
if (access(pad_path, R_OK) != 0) {
|
|
printf("Error: Pad file %s not found\n", pad_path);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Read current offset
|
|
if (read_state_offset(pad_chksum, ¤t_offset) != 0) {
|
|
printf("Error: Cannot read state file\n");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Ensure we never encrypt before offset 32
|
|
if (current_offset < 32) {
|
|
printf("Warning: State offset below reserved area, adjusting to 32\n");
|
|
current_offset = 32;
|
|
if (write_state_offset(pad_chksum, current_offset) != 0) {
|
|
printf("Warning: Failed to update state file\n");
|
|
}
|
|
}
|
|
|
|
// Calculate XOR checksum of pad file
|
|
if (calculate_checksum(pad_path, chksum_hex) != 0) {
|
|
printf("Error: Cannot calculate pad checksum\n");
|
|
free(pad_chksum);
|
|
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");
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
if (current_offset + file_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);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Generate output filename if not specified, using files directory
|
|
char default_output[512];
|
|
if (output_file == NULL) {
|
|
char temp_output[512];
|
|
if (ascii_armor) {
|
|
snprintf(temp_output, sizeof(temp_output), "%s.otp.asc", input_file);
|
|
} else {
|
|
snprintf(temp_output, sizeof(temp_output), "%s.otp", input_file);
|
|
}
|
|
|
|
// Apply files directory default path
|
|
get_default_file_path(temp_output, default_output, sizeof(default_output));
|
|
output_file = default_output;
|
|
}
|
|
|
|
// Open input file
|
|
FILE* input_fp = fopen(input_file, "rb");
|
|
if (!input_fp) {
|
|
printf("Error: Cannot open input file %s\n", input_file);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Open pad file
|
|
FILE* pad_file = fopen(pad_path, "rb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot open pad file\n");
|
|
fclose(input_fp);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(pad_file, current_offset, SEEK_SET) != 0) {
|
|
printf("Error: Cannot seek to offset in pad file\n");
|
|
fclose(input_fp);
|
|
fclose(pad_file);
|
|
free(pad_chksum);
|
|
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;
|
|
time_t start_time = time(NULL);
|
|
|
|
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;
|
|
}
|
|
|
|
// XOR encrypt
|
|
for (uint64_t i = 0; i < chunk_size; i++) {
|
|
encrypted_data[bytes_processed + i] = buffer[i] ^ pad_buffer[i];
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
if (file_size > 10 * 1024 * 1024) {
|
|
show_progress(file_size, file_size, start_time);
|
|
printf("\n");
|
|
}
|
|
|
|
fclose(input_fp);
|
|
fclose(pad_file);
|
|
|
|
// Write output file
|
|
if (ascii_armor) {
|
|
// ASCII armored format - same as message format
|
|
FILE* output_fp = fopen(output_file, "w");
|
|
if (!output_fp) {
|
|
printf("Error: Cannot create output file %s\n", output_file);
|
|
free(encrypted_data);
|
|
free(pad_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Encode as base64
|
|
char* base64_data = custom_base64_encode(encrypted_data, file_size);
|
|
|
|
// Write ASCII armor
|
|
fprintf(output_fp, "-----BEGIN OTP MESSAGE-----\n");
|
|
fprintf(output_fp, "Version: %s\n", get_version());
|
|
fprintf(output_fp, "Pad-ChkSum: %s\n", chksum_hex);
|
|
fprintf(output_fp, "Pad-Offset: %lu\n", current_offset);
|
|
fprintf(output_fp, "\n");
|
|
|
|
// Write base64 data in 64-character lines
|
|
int b64_len = strlen(base64_data);
|
|
for (int i = 0; i < b64_len; i += 64) {
|
|
fprintf(output_fp, "%.64s\n", base64_data + i);
|
|
}
|
|
|
|
fprintf(output_fp, "-----END OTP MESSAGE-----\n");
|
|
|
|
fclose(output_fp);
|
|
free(base64_data);
|
|
} else {
|
|
// Binary format
|
|
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_chksum);
|
|
return 1;
|
|
}
|
|
|
|
// Write binary header
|
|
// Magic: "OTP\0"
|
|
fwrite("OTP\0", 1, 4, output_fp);
|
|
|
|
// Version: 2 bytes
|
|
uint16_t version = 1;
|
|
fwrite(&version, sizeof(uint16_t), 1, output_fp);
|
|
|
|
// Pad checksum: 32 bytes (binary)
|
|
unsigned char pad_chksum_bin[32];
|
|
for (int i = 0; i < 32; i++) {
|
|
sscanf(chksum_hex + i*2, "%2hhx", &pad_chksum_bin[i]);
|
|
}
|
|
fwrite(pad_chksum_bin, 1, 32, output_fp);
|
|
|
|
// Pad offset: 8 bytes
|
|
fwrite(¤t_offset, sizeof(uint64_t), 1, output_fp);
|
|
|
|
|
|
// File mode: 4 bytes
|
|
uint32_t file_mode = input_stat.st_mode;
|
|
fwrite(&file_mode, sizeof(uint32_t), 1, output_fp);
|
|
|
|
// File size: 8 bytes
|
|
fwrite(&file_size, sizeof(uint64_t), 1, output_fp);
|
|
|
|
// Encrypted data
|
|
fwrite(encrypted_data, 1, file_size, output_fp);
|
|
|
|
fclose(output_fp);
|
|
}
|
|
|
|
// Update state offset
|
|
if (write_state_offset(pad_chksum, current_offset + file_size) != 0) {
|
|
printf("Warning: Failed to update state file\n");
|
|
}
|
|
|
|
printf("File encrypted successfully: %s\n", output_file);
|
|
if (ascii_armor) {
|
|
printf("Format: ASCII armored (.otp.asc)\n");
|
|
} else {
|
|
printf("Format: Binary (.otp)\n");
|
|
}
|
|
|
|
// Cleanup
|
|
free(encrypted_data);
|
|
free(pad_chksum);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int decrypt_file(const char* input_file, const char* output_file) {
|
|
// Check if input file exists
|
|
if (access(input_file, R_OK) != 0) {
|
|
printf("Error: Input file %s not found\n", input_file);
|
|
return 1;
|
|
}
|
|
|
|
FILE* input_fp = fopen(input_file, "rb");
|
|
if (!input_fp) {
|
|
printf("Error: Cannot open input file %s\n", input_file);
|
|
return 1;
|
|
}
|
|
|
|
// Read first few bytes to determine format
|
|
char magic[4];
|
|
if (fread(magic, 1, 4, input_fp) != 4) {
|
|
printf("Error: Cannot read file header\n");
|
|
fclose(input_fp);
|
|
return 1;
|
|
}
|
|
|
|
fseek(input_fp, 0, SEEK_SET); // Reset to beginning
|
|
|
|
if (memcmp(magic, "OTP\0", 4) == 0) {
|
|
// Binary format
|
|
return decrypt_binary_file(input_fp, output_file);
|
|
} else {
|
|
// Assume ASCII armored format, read entire file as text
|
|
fclose(input_fp);
|
|
return decrypt_ascii_file(input_file, output_file);
|
|
}
|
|
}
|
|
|
|
int decrypt_binary_file(FILE* input_fp, const char* output_file) {
|
|
// Read binary header
|
|
char magic[4];
|
|
uint16_t version;
|
|
unsigned char pad_chksum_bin[32];
|
|
uint64_t pad_offset;
|
|
uint32_t file_mode;
|
|
uint64_t 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) {
|
|
printf("Error: Cannot read binary header\n");
|
|
fclose(input_fp);
|
|
return 1;
|
|
}
|
|
|
|
if (memcmp(magic, "OTP\0", 4) != 0) {
|
|
printf("Error: Invalid binary format\n");
|
|
fclose(input_fp);
|
|
return 1;
|
|
}
|
|
|
|
// Convert binary checksum to hex
|
|
char pad_chksum_hex[65];
|
|
for (int i = 0; i < 32; i++) {
|
|
sprintf(pad_chksum_hex + i*2, "%02x", pad_chksum_bin[i]);
|
|
}
|
|
pad_chksum_hex[64] = '\0';
|
|
|
|
printf("Decrypting binary file...\n");
|
|
printf("File size: %lu bytes\n", file_size);
|
|
|
|
// Check if we have the required pad
|
|
char pad_path[MAX_HASH_LENGTH + 20];
|
|
char state_path[MAX_HASH_LENGTH + 20];
|
|
get_pad_path(pad_chksum_hex, pad_path, state_path);
|
|
|
|
if (access(pad_path, R_OK) != 0) {
|
|
printf("Error: Required pad not found: %s\n", pad_chksum_hex);
|
|
printf("Available pads:\n");
|
|
list_available_pads();
|
|
fclose(input_fp);
|
|
return 1;
|
|
}
|
|
|
|
// Determine output filename
|
|
char default_output[512];
|
|
if (output_file == NULL) {
|
|
snprintf(default_output, sizeof(default_output), "decrypted.bin");
|
|
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) {
|
|
printf("Error: Cannot read encrypted data\n");
|
|
free(encrypted_data);
|
|
fclose(input_fp);
|
|
return 1;
|
|
}
|
|
fclose(input_fp);
|
|
|
|
// Open pad file and decrypt
|
|
FILE* pad_file = fopen(pad_path, "rb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot open pad file\n");
|
|
free(encrypted_data);
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(pad_file, pad_offset, SEEK_SET) != 0) {
|
|
printf("Error: Cannot seek to offset in pad file\n");
|
|
free(encrypted_data);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
|
|
unsigned char* pad_data = malloc(file_size);
|
|
if (fread(pad_data, 1, file_size, pad_file) != file_size) {
|
|
printf("Error: Cannot read pad data\n");
|
|
free(encrypted_data);
|
|
free(pad_data);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
fclose(pad_file);
|
|
|
|
// XOR decrypt
|
|
for (uint64_t i = 0; i < file_size; i++) {
|
|
encrypted_data[i] ^= pad_data[i];
|
|
}
|
|
|
|
// Write decrypted file
|
|
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) {
|
|
printf("Error: Cannot write decrypted data\n");
|
|
free(encrypted_data);
|
|
free(pad_data);
|
|
fclose(output_fp);
|
|
return 1;
|
|
}
|
|
fclose(output_fp);
|
|
|
|
// Restore file permissions
|
|
if (chmod(output_file, file_mode) != 0) {
|
|
printf("Warning: Cannot restore file permissions\n");
|
|
}
|
|
|
|
printf("File decrypted successfully: %s\n", output_file);
|
|
printf("Restored permissions and metadata\n");
|
|
|
|
// Cleanup
|
|
free(encrypted_data);
|
|
free(pad_data);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int decrypt_ascii_file(const char* input_file, const char* output_file) {
|
|
FILE* input_fp = fopen(input_file, "r");
|
|
if (!input_fp) {
|
|
printf("Error: Cannot open input file %s\n", input_file);
|
|
return 1;
|
|
}
|
|
|
|
// Read the entire ASCII armored content
|
|
char line[MAX_LINE_LENGTH];
|
|
char stored_chksum[MAX_HASH_LENGTH];
|
|
uint64_t pad_offset;
|
|
char base64_data[MAX_INPUT_SIZE * 8] = {0}; // Larger buffer for files
|
|
int in_data_section = 0;
|
|
int found_begin = 0;
|
|
|
|
while (fgets(line, sizeof(line), input_fp)) {
|
|
line[strcspn(line, "\n\r")] = 0; // Remove newlines
|
|
|
|
if (strcmp(line, "-----BEGIN OTP MESSAGE-----") == 0) {
|
|
found_begin = 1;
|
|
continue;
|
|
}
|
|
|
|
if (strcmp(line, "-----END OTP MESSAGE-----") == 0) {
|
|
break;
|
|
}
|
|
|
|
if (!found_begin) continue;
|
|
|
|
if (strncmp(line, "Pad-ChkSum: ", 12) == 0) {
|
|
strncpy(stored_chksum, line + 12, 64);
|
|
stored_chksum[64] = '\0';
|
|
}
|
|
else if (strncmp(line, "Pad-Offset: ", 12) == 0) {
|
|
pad_offset = strtoull(line + 12, NULL, 10);
|
|
}
|
|
else if (strlen(line) == 0) {
|
|
in_data_section = 1;
|
|
}
|
|
else if (in_data_section) {
|
|
strncat(base64_data, line, sizeof(base64_data) - strlen(base64_data) - 1);
|
|
}
|
|
}
|
|
fclose(input_fp);
|
|
|
|
if (!found_begin) {
|
|
printf("Error: Invalid ASCII armored format\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("Decrypting ASCII armored file...\n");
|
|
|
|
// Check if we have the required pad
|
|
char pad_path[MAX_HASH_LENGTH + 20];
|
|
char state_path[MAX_HASH_LENGTH + 20];
|
|
get_pad_path(stored_chksum, pad_path, state_path);
|
|
|
|
if (access(pad_path, R_OK) != 0) {
|
|
printf("Error: Required pad not found: %s\n", stored_chksum);
|
|
printf("Available pads:\n");
|
|
list_available_pads();
|
|
return 1;
|
|
}
|
|
|
|
// Decode base64
|
|
int ciphertext_len;
|
|
unsigned char* ciphertext = custom_base64_decode(base64_data, &ciphertext_len);
|
|
if (!ciphertext) {
|
|
printf("Error: Invalid base64 data\n");
|
|
return 1;
|
|
}
|
|
|
|
// Determine output filename
|
|
char default_output[512];
|
|
if (output_file == NULL) {
|
|
// Remove .otp.asc extension if present
|
|
strncpy(default_output, input_file, sizeof(default_output) - 1);
|
|
default_output[sizeof(default_output) - 1] = '\0';
|
|
|
|
char* ext = strstr(default_output, ".otp.asc");
|
|
if (ext) {
|
|
*ext = '\0';
|
|
} else {
|
|
// Just add .decrypted suffix
|
|
strncat(default_output, ".decrypted", sizeof(default_output) - strlen(default_output) - 1);
|
|
}
|
|
output_file = default_output;
|
|
}
|
|
|
|
// Open pad file and decrypt
|
|
FILE* pad_file = fopen(pad_path, "rb");
|
|
if (!pad_file) {
|
|
printf("Error: Cannot open pad file\n");
|
|
free(ciphertext);
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(pad_file, pad_offset, SEEK_SET) != 0) {
|
|
printf("Error: Cannot seek to offset in pad file\n");
|
|
free(ciphertext);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
|
|
unsigned char* pad_data = malloc(ciphertext_len);
|
|
if (fread(pad_data, 1, ciphertext_len, pad_file) != (size_t)ciphertext_len) {
|
|
printf("Error: Cannot read pad data\n");
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
fclose(pad_file);
|
|
return 1;
|
|
}
|
|
fclose(pad_file);
|
|
|
|
// XOR decrypt
|
|
for (int i = 0; i < ciphertext_len; i++) {
|
|
ciphertext[i] ^= pad_data[i];
|
|
}
|
|
|
|
// Write decrypted file
|
|
FILE* output_fp = fopen(output_file, "wb");
|
|
if (!output_fp) {
|
|
printf("Error: Cannot create output file %s\n", output_file);
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
return 1;
|
|
}
|
|
|
|
if (fwrite(ciphertext, 1, ciphertext_len, output_fp) != (size_t)ciphertext_len) {
|
|
printf("Error: Cannot write decrypted data\n");
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
fclose(output_fp);
|
|
return 1;
|
|
}
|
|
fclose(output_fp);
|
|
|
|
printf("File decrypted successfully: %s\n", output_file);
|
|
printf("Note: ASCII format does not preserve original filename/permissions\n");
|
|
|
|
// Cleanup
|
|
free(ciphertext);
|
|
free(pad_data);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int read_state_offset(const char* pad_chksum, uint64_t* offset) {
|
|
char state_filename[1024];
|
|
snprintf(state_filename, sizeof(state_filename), "%s/%s.state", current_pads_dir, pad_chksum);
|
|
|
|
FILE* state_file = fopen(state_filename, "rb");
|
|
if (!state_file) {
|
|
*offset = 0;
|
|
return 0;
|
|
}
|
|
|
|
if (fread(offset, sizeof(uint64_t), 1, state_file) != 1) {
|
|
fclose(state_file);
|
|
*offset = 0;
|
|
return 0;
|
|
}
|
|
|
|
fclose(state_file);
|
|
return 0;
|
|
}
|
|
|
|
int write_state_offset(const char* pad_chksum, uint64_t offset) {
|
|
char state_filename[1024];
|
|
snprintf(state_filename, sizeof(state_filename), "%s/%s.state", current_pads_dir, pad_chksum);
|
|
|
|
FILE* state_file = fopen(state_filename, "wb");
|
|
if (!state_file) {
|
|
return 1;
|
|
}
|
|
|
|
if (fwrite(&offset, sizeof(uint64_t), 1, state_file) != 1) {
|
|
fclose(state_file);
|
|
return 1;
|
|
}
|
|
|
|
fclose(state_file);
|
|
return 0;
|
|
}
|
|
|
|
int calculate_checksum(const char* filename, char* checksum_hex) {
|
|
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;
|
|
|
|
// Calculate XOR checksum of entire file
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// Keyboard entropy functions
|
|
int setup_raw_terminal(struct termios* original_termios) {
|
|
struct termios new_termios;
|
|
|
|
if (tcgetattr(STDIN_FILENO, original_termios) != 0) {
|
|
return 1;
|
|
}
|
|
|
|
new_termios = *original_termios;
|
|
new_termios.c_lflag &= ~(ICANON | ECHO);
|
|
new_termios.c_cc[VMIN] = 0;
|
|
new_termios.c_cc[VTIME] = 0;
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &new_termios) != 0) {
|
|
return 1;
|
|
}
|
|
|
|
// Set stdin to non-blocking
|
|
int flags = fcntl(STDIN_FILENO, F_GETFL);
|
|
if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) == -1) {
|
|
tcsetattr(STDIN_FILENO, TCSANOW, original_termios);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void restore_terminal(struct termios* original_termios) {
|
|
tcsetattr(STDIN_FILENO, TCSANOW, original_termios);
|
|
|
|
// Reset stdin to blocking
|
|
int flags = fcntl(STDIN_FILENO, F_GETFL);
|
|
fcntl(STDIN_FILENO, F_SETFL, flags & ~O_NONBLOCK);
|
|
}
|
|
|
|
int collect_keyboard_entropy(unsigned char* entropy_buffer, size_t max_size, size_t* collected) {
|
|
struct timespec timestamp;
|
|
unsigned char entropy_block[16];
|
|
uint32_t sequence_counter = 0;
|
|
char key;
|
|
*collected = 0;
|
|
|
|
while (*collected < max_size - 16) {
|
|
if (read(STDIN_FILENO, &key, 1) == 1) {
|
|
clock_gettime(CLOCK_MONOTONIC, ×tamp);
|
|
|
|
// Create entropy block: [key][timestamp][sequence_counter]
|
|
entropy_block[0] = key;
|
|
memcpy(&entropy_block[1], ×tamp.tv_sec, 8);
|
|
memcpy(&entropy_block[9], ×tamp.tv_nsec, 4);
|
|
memcpy(&entropy_block[13], &sequence_counter, 3);
|
|
|
|
// Add to entropy buffer
|
|
memcpy(entropy_buffer + *collected, entropy_block, 16);
|
|
*collected += 16;
|
|
sequence_counter++;
|
|
} else {
|
|
// No key available, add some timing entropy
|
|
clock_gettime(CLOCK_MONOTONIC, ×tamp);
|
|
if (*collected + 12 < max_size) {
|
|
memcpy(entropy_buffer + *collected, ×tamp, 12);
|
|
*collected += 12;
|
|
}
|
|
usleep(1000); // 1ms delay
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
// Directory management functions
|
|
int ensure_pads_directory(void) {
|
|
struct stat st = {0};
|
|
if (stat(current_pads_dir, &st) == -1) {
|
|
if (mkdir(current_pads_dir, 0755) != 0) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
const char* get_files_directory(void) {
|
|
struct stat st = {0};
|
|
if (stat(FILES_DIR, &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
return FILES_DIR;
|
|
}
|
|
return "."; // Fall back to current directory
|
|
}
|
|
|
|
void get_default_file_path(const char* filename, char* result_path, size_t result_size) {
|
|
const char* files_dir = get_files_directory();
|
|
|
|
// If filename already has a path (contains '/'), use it as-is
|
|
if (strchr(filename, '/') != NULL) {
|
|
strncpy(result_path, filename, result_size - 1);
|
|
result_path[result_size - 1] = '\0';
|
|
return;
|
|
}
|
|
|
|
// Otherwise, prepend the files directory
|
|
snprintf(result_path, result_size, "%s/%s", files_dir, filename);
|
|
}
|
|
|
|
void get_pad_path(const char* chksum, char* pad_path, char* state_path) {
|
|
snprintf(pad_path, 1024, "%s/%s.pad", current_pads_dir, chksum);
|
|
snprintf(state_path, 1024, "%s/%s.state", current_pads_dir, chksum);
|
|
}
|
|
|
|
// Stdin detection functions implementation
|
|
int has_stdin_data(void) {
|
|
// Check if stdin is a pipe/redirect (not a terminal)
|
|
if (!isatty(STDIN_FILENO)) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
char* read_stdin_text(void) {
|
|
size_t capacity = 4096;
|
|
size_t length = 0;
|
|
char* buffer = malloc(capacity);
|
|
|
|
if (!buffer) {
|
|
return NULL;
|
|
}
|
|
|
|
char chunk[1024];
|
|
while (fgets(chunk, sizeof(chunk), stdin)) {
|
|
size_t chunk_len = strlen(chunk);
|
|
|
|
// Ensure we have enough capacity
|
|
while (length + chunk_len >= capacity) {
|
|
capacity *= 2;
|
|
char* new_buffer = realloc(buffer, capacity);
|
|
if (!new_buffer) {
|
|
free(buffer);
|
|
return NULL;
|
|
}
|
|
buffer = new_buffer;
|
|
}
|
|
|
|
strcpy(buffer + length, chunk);
|
|
length += chunk_len;
|
|
}
|
|
|
|
// Remove trailing newline if present
|
|
if (length > 0 && buffer[length - 1] == '\n') {
|
|
buffer[length - 1] = '\0';
|
|
length--;
|
|
}
|
|
|
|
// If empty, free and return NULL
|
|
if (length == 0) {
|
|
free(buffer);
|
|
return NULL;
|
|
}
|
|
|
|
return buffer;
|
|
}
|
|
|
|
int pipe_mode(int argc, char* argv[], const char* piped_text) {
|
|
(void)argc; // Suppress unused parameter warning
|
|
(void)argv; // Suppress unused parameter warning
|
|
|
|
// Check if we have a default pad configured
|
|
char* default_pad = get_default_pad_path();
|
|
if (default_pad) {
|
|
// Verify the default pad exists and extract checksum
|
|
if (access(default_pad, R_OK) == 0) {
|
|
// Extract checksum from pad filename
|
|
char* filename = strrchr(default_pad, '/');
|
|
if (!filename) filename = default_pad;
|
|
else filename++; // Skip the '/'
|
|
|
|
// Extract checksum (remove .pad extension)
|
|
if (strlen(filename) >= 68 && strstr(filename, ".pad")) {
|
|
char pad_checksum[65];
|
|
strncpy(pad_checksum, filename, 64);
|
|
pad_checksum[64] = '\0';
|
|
|
|
free(default_pad);
|
|
|
|
// Encrypt using the default pad (silent mode)
|
|
return encrypt_text(pad_checksum, piped_text);
|
|
}
|
|
}
|
|
|
|
fprintf(stderr, "Error: Default pad not found or invalid: %s\n", default_pad);
|
|
free(default_pad);
|
|
return 1;
|
|
}
|
|
|
|
fprintf(stderr, "Error: No default pad configured for pipe mode\n");
|
|
fprintf(stderr, "Configure a default pad in ~/.otp/otp.conf\n");
|
|
return 1;
|
|
}
|
|
|
|
// Preferences management functions implementation
|
|
int load_preferences(void) {
|
|
char* home_dir = getenv("HOME");
|
|
if (!home_dir) {
|
|
return 1; // No home directory
|
|
}
|
|
|
|
char preferences_dir[1024];
|
|
char preferences_file[1024];
|
|
snprintf(preferences_dir, sizeof(preferences_dir), "%s/.otp", home_dir);
|
|
snprintf(preferences_file, sizeof(preferences_file), "%s/otp.conf", preferences_dir);
|
|
|
|
FILE* file = fopen(preferences_file, "r");
|
|
if (!file) {
|
|
return 0; // No preferences file, use defaults
|
|
}
|
|
|
|
char line[1024];
|
|
while (fgets(line, sizeof(line), file)) {
|
|
// Remove newline
|
|
line[strcspn(line, "\n")] = 0;
|
|
|
|
// Skip empty lines and comments
|
|
if (strlen(line) == 0 || line[0] == '#') {
|
|
continue;
|
|
}
|
|
|
|
// Parse key=value pairs
|
|
char* equals = strchr(line, '=');
|
|
if (equals) {
|
|
*equals = '\0';
|
|
char* key = line;
|
|
char* value = equals + 1;
|
|
|
|
// Trim whitespace
|
|
while (*key == ' ' || *key == '\t') key++;
|
|
while (*value == ' ' || *value == '\t') value++;
|
|
|
|
if (strcmp(key, "default_pad") == 0) {
|
|
strncpy(default_pad_path, value, sizeof(default_pad_path) - 1);
|
|
default_pad_path[sizeof(default_pad_path) - 1] = '\0';
|
|
}
|
|
}
|
|
}
|
|
|
|
fclose(file);
|
|
return 0;
|
|
}
|
|
|
|
int save_preferences(void) {
|
|
char* home_dir = getenv("HOME");
|
|
if (!home_dir) {
|
|
return 1;
|
|
}
|
|
|
|
char preferences_dir[1024];
|
|
char preferences_file[1024];
|
|
snprintf(preferences_dir, sizeof(preferences_dir), "%s/.otp", home_dir);
|
|
snprintf(preferences_file, sizeof(preferences_file), "%s/otp.conf", preferences_dir);
|
|
|
|
// Create .otp directory if it doesn't exist
|
|
struct stat st = {0};
|
|
if (stat(preferences_dir, &st) == -1) {
|
|
if (mkdir(preferences_dir, 0755) != 0) {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
FILE* file = fopen(preferences_file, "w");
|
|
if (!file) {
|
|
return 1;
|
|
}
|
|
|
|
fprintf(file, "# OTP Preferences File\n");
|
|
fprintf(file, "# This file is automatically generated and updated by the OTP program\n\n");
|
|
|
|
if (strlen(default_pad_path) > 0) {
|
|
fprintf(file, "default_pad=%s\n", default_pad_path);
|
|
}
|
|
|
|
fclose(file);
|
|
return 0;
|
|
}
|
|
|
|
char* get_preference(const char* key) {
|
|
if (strcmp(key, "default_pad") == 0) {
|
|
if (strlen(default_pad_path) > 0) {
|
|
return strdup(default_pad_path);
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int set_preference(const char* key, const char* value) {
|
|
if (strcmp(key, "default_pad") == 0) {
|
|
if (value) {
|
|
strncpy(default_pad_path, value, sizeof(default_pad_path) - 1);
|
|
default_pad_path[sizeof(default_pad_path) - 1] = '\0';
|
|
} else {
|
|
default_pad_path[0] = '\0';
|
|
}
|
|
return save_preferences();
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
char* get_default_pad_path(void) {
|
|
if (strlen(default_pad_path) > 0) {
|
|
return strdup(default_pad_path);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int set_default_pad_path(const char* pad_path) {
|
|
return set_preference("default_pad", pad_path);
|
|
}
|
|
|
|
// OTP thumb drive detection function implementation
|
|
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) {
|
|
const char* mount_dirs[] = {"/media", "/run/media", "/mnt", NULL};
|
|
|
|
for (int mount_idx = 0; mount_dirs[mount_idx] != NULL; mount_idx++) {
|
|
DIR* mount_dir = opendir(mount_dirs[mount_idx]);
|
|
if (!mount_dir) continue;
|
|
|
|
struct dirent* mount_entry;
|
|
while ((mount_entry = readdir(mount_dir)) != NULL) {
|
|
if (mount_entry->d_name[0] == '.') continue;
|
|
|
|
char mount_path[512];
|
|
snprintf(mount_path, sizeof(mount_path), "%s/%s", mount_dirs[mount_idx], mount_entry->d_name);
|
|
|
|
// For /media, we need to go one level deeper (user directories)
|
|
if (strcmp(mount_dirs[mount_idx], "/media") == 0) {
|
|
// This is /media/[username] - look inside for drives
|
|
DIR* user_dir = opendir(mount_path);
|
|
if (!user_dir) continue;
|
|
|
|
struct dirent* user_entry;
|
|
while ((user_entry = readdir(user_dir)) != NULL) {
|
|
if (user_entry->d_name[0] == '.') continue;
|
|
|
|
// Check if drive name starts with "OTP"
|
|
if (strncmp(user_entry->d_name, "OTP", 3) != 0) continue;
|
|
|
|
char user_mount_path[512];
|
|
snprintf(user_mount_path, sizeof(user_mount_path), "%s/%s", mount_path, user_entry->d_name);
|
|
|
|
// Check if this is a readable directory
|
|
DIR* drive_dir = opendir(user_mount_path);
|
|
if (drive_dir) {
|
|
closedir(drive_dir);
|
|
strncpy(otp_drive_path, user_mount_path, path_size - 1);
|
|
otp_drive_path[path_size - 1] = '\0';
|
|
closedir(user_dir);
|
|
closedir(mount_dir);
|
|
return 1; // Found OTP drive
|
|
}
|
|
}
|
|
closedir(user_dir);
|
|
} else if (strcmp(mount_dirs[mount_idx], "/run/media") == 0) {
|
|
// For /run/media, we need to go one level deeper (skip username)
|
|
DIR* user_dir = opendir(mount_path);
|
|
if (!user_dir) continue;
|
|
|
|
struct dirent* user_entry;
|
|
while ((user_entry = readdir(user_dir)) != NULL) {
|
|
if (user_entry->d_name[0] == '.') continue;
|
|
|
|
// Check if drive name starts with "OTP"
|
|
if (strncmp(user_entry->d_name, "OTP", 3) != 0) continue;
|
|
|
|
char user_mount_path[512];
|
|
snprintf(user_mount_path, sizeof(user_mount_path), "%s/%s", mount_path, user_entry->d_name);
|
|
|
|
// Check if this is a readable directory
|
|
DIR* drive_dir = opendir(user_mount_path);
|
|
if (drive_dir) {
|
|
closedir(drive_dir);
|
|
strncpy(otp_drive_path, user_mount_path, path_size - 1);
|
|
otp_drive_path[path_size - 1] = '\0';
|
|
closedir(user_dir);
|
|
closedir(mount_dir);
|
|
return 1; // Found OTP drive
|
|
}
|
|
}
|
|
closedir(user_dir);
|
|
} else {
|
|
// Direct mount point (like /mnt/OTP_DRIVE)
|
|
// Check if drive name starts with "OTP"
|
|
if (strncmp(mount_entry->d_name, "OTP", 3) == 0) {
|
|
DIR* drive_dir = opendir(mount_path);
|
|
if (drive_dir) {
|
|
closedir(drive_dir);
|
|
strncpy(otp_drive_path, mount_path, path_size - 1);
|
|
otp_drive_path[path_size - 1] = '\0';
|
|
closedir(mount_dir);
|
|
return 1; // Found OTP drive
|
|
}
|
|
}
|
|
}
|
|
}
|
|
closedir(mount_dir);
|
|
}
|
|
|
|
return 0; // No OTP drive found
|
|
}
|
|
|
|
|
|
|
|
|
|
// Custom base64 encode function
|
|
char* custom_base64_encode(const unsigned char* input, int length) {
|
|
int output_length = 4 * ((length + 2) / 3);
|
|
char* encoded = malloc(output_length + 1);
|
|
if (!encoded) return NULL;
|
|
|
|
int i, j;
|
|
for (i = 0, j = 0; i < length;) {
|
|
uint32_t octet_a = i < length ? input[i++] : 0;
|
|
uint32_t octet_b = i < length ? input[i++] : 0;
|
|
uint32_t octet_c = i < length ? input[i++] : 0;
|
|
|
|
uint32_t triple = (octet_a << 16) + (octet_b << 8) + octet_c;
|
|
|
|
encoded[j++] = base64_chars[(triple >> 18) & 63];
|
|
encoded[j++] = base64_chars[(triple >> 12) & 63];
|
|
encoded[j++] = base64_chars[(triple >> 6) & 63];
|
|
encoded[j++] = base64_chars[triple & 63];
|
|
}
|
|
|
|
// Add padding
|
|
for (int pad = 0; pad < (3 - length % 3) % 3; pad++) {
|
|
encoded[output_length - 1 - pad] = '=';
|
|
}
|
|
|
|
encoded[output_length] = '\0';
|
|
return encoded;
|
|
}
|
|
|
|
// Custom base64 decode function
|
|
unsigned char* custom_base64_decode(const char* input, int* output_length) {
|
|
int input_length = strlen(input);
|
|
if (input_length % 4 != 0) return NULL;
|
|
|
|
*output_length = input_length / 4 * 3;
|
|
if (input[input_length - 1] == '=') (*output_length)--;
|
|
if (input[input_length - 2] == '=') (*output_length)--;
|
|
|
|
unsigned char* decoded = malloc(*output_length);
|
|
if (!decoded) return NULL;
|
|
|
|
int i, j;
|
|
for (i = 0, j = 0; i < input_length;) {
|
|
int sextet_a = input[i] == '=' ? 0 & i++ : base64_decode_table[(unsigned char)input[i++]];
|
|
int sextet_b = input[i] == '=' ? 0 & i++ : base64_decode_table[(unsigned char)input[i++]];
|
|
int sextet_c = input[i] == '=' ? 0 & i++ : base64_decode_table[(unsigned char)input[i++]];
|
|
int sextet_d = input[i] == '=' ? 0 & i++ : base64_decode_table[(unsigned char)input[i++]];
|
|
|
|
if (sextet_a == -1 || sextet_b == -1 || sextet_c == -1 || sextet_d == -1) {
|
|
free(decoded);
|
|
return NULL;
|
|
}
|
|
|
|
uint32_t triple = (sextet_a << 18) + (sextet_b << 12) + (sextet_c << 6) + sextet_d;
|
|
|
|
if (j < *output_length) decoded[j++] = (triple >> 16) & 255;
|
|
if (j < *output_length) decoded[j++] = (triple >> 8) & 255;
|
|
if (j < *output_length) decoded[j++] = triple & 255;
|
|
}
|
|
|
|
return decoded;
|
|
}
|
|
|
|
// Simple keyboard entropy mixing function
|
|
void simple_entropy_mix(unsigned char* urandom_buffer, size_t buffer_size,
|
|
const unsigned char* entropy_data, size_t entropy_size) {
|
|
if (!entropy_data || entropy_size == 0) return;
|
|
|
|
for (size_t i = 0; i < buffer_size; i++) {
|
|
// XOR with entropy data in a rotating pattern
|
|
unsigned char entropy_byte = entropy_data[i % entropy_size];
|
|
// Mix position information
|
|
entropy_byte ^= (i & 0xFF) ^ ((i >> 8) & 0xFF);
|
|
urandom_buffer[i] ^= entropy_byte;
|
|
}
|
|
}
|
|
|
|
// Enhanced input function with editable pre-filled text
|
|
int get_filename_with_default(const char* prompt, const char* default_path, char* result, size_t result_size) {
|
|
// Find the last directory separator
|
|
char* last_slash = strrchr(default_path, '/');
|
|
char directory[1024] = "";
|
|
char filename[512] = "";
|
|
|
|
if (last_slash) {
|
|
// Extract directory path
|
|
size_t dir_len = last_slash - default_path + 1; // Include the trailing slash
|
|
if (dir_len < sizeof(directory)) {
|
|
strncpy(directory, default_path, dir_len);
|
|
directory[dir_len] = '\0';
|
|
}
|
|
|
|
// Extract filename
|
|
strncpy(filename, last_slash + 1, sizeof(filename) - 1);
|
|
filename[sizeof(filename) - 1] = '\0';
|
|
} else {
|
|
// No directory separator, treat as filename only
|
|
strncpy(filename, default_path, sizeof(filename) - 1);
|
|
filename[sizeof(filename) - 1] = '\0';
|
|
}
|
|
|
|
// Setup terminal for raw input
|
|
struct termios orig_termios;
|
|
if (tcgetattr(STDIN_FILENO, &orig_termios) != 0) {
|
|
// Fallback to simple input if terminal control fails
|
|
printf("\n%s\n%s: ", prompt, directory);
|
|
fflush(stdout);
|
|
|
|
char input_buffer[512];
|
|
if (!fgets(input_buffer, sizeof(input_buffer), stdin)) {
|
|
return 1;
|
|
}
|
|
input_buffer[strcspn(input_buffer, "\n")] = 0;
|
|
|
|
if (strlen(input_buffer) == 0) {
|
|
strncpy(result, default_path, result_size - 1);
|
|
} else {
|
|
if (strlen(directory) > 0) {
|
|
snprintf(result, result_size, "%s%s", directory, input_buffer);
|
|
} else {
|
|
strncpy(result, input_buffer, result_size - 1);
|
|
}
|
|
}
|
|
result[result_size - 1] = '\0';
|
|
return 0;
|
|
}
|
|
|
|
// Set up raw terminal mode
|
|
struct termios raw_termios = orig_termios;
|
|
raw_termios.c_lflag &= ~(ECHO | ICANON);
|
|
raw_termios.c_cc[VMIN] = 1;
|
|
raw_termios.c_cc[VTIME] = 0;
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &raw_termios) != 0) {
|
|
// Fallback if terminal setup fails
|
|
printf("\n%s\n%s: ", prompt, directory);
|
|
fflush(stdout);
|
|
|
|
char input_buffer[512];
|
|
if (!fgets(input_buffer, sizeof(input_buffer), stdin)) {
|
|
return 1;
|
|
}
|
|
input_buffer[strcspn(input_buffer, "\n")] = 0;
|
|
|
|
if (strlen(input_buffer) == 0) {
|
|
strncpy(result, default_path, result_size - 1);
|
|
} else {
|
|
if (strlen(directory) > 0) {
|
|
snprintf(result, result_size, "%s%s", directory, input_buffer);
|
|
} else {
|
|
strncpy(result, input_buffer, result_size - 1);
|
|
}
|
|
}
|
|
result[result_size - 1] = '\0';
|
|
return 0;
|
|
}
|
|
|
|
// Display prompt and directory
|
|
printf("\n%s\n%s", prompt, directory);
|
|
fflush(stdout);
|
|
|
|
// Initialize editing buffer with default filename
|
|
char edit_buffer[512];
|
|
strncpy(edit_buffer, filename, sizeof(edit_buffer) - 1);
|
|
edit_buffer[sizeof(edit_buffer) - 1] = '\0';
|
|
int cursor_pos = strlen(edit_buffer);
|
|
int buffer_len = cursor_pos;
|
|
|
|
// Display initial filename
|
|
printf("%s", edit_buffer);
|
|
fflush(stdout);
|
|
|
|
// Main editing loop
|
|
int c;
|
|
while ((c = getchar()) != EOF) {
|
|
if (c == '\n' || c == '\r') {
|
|
// Enter key - accept input
|
|
break;
|
|
} else if (c == 127 || c == 8) {
|
|
// Backspace/Delete
|
|
if (cursor_pos > 0) {
|
|
// Move everything after cursor one position left
|
|
memmove(&edit_buffer[cursor_pos - 1], &edit_buffer[cursor_pos], buffer_len - cursor_pos + 1);
|
|
cursor_pos--;
|
|
buffer_len--;
|
|
|
|
// Redraw line: move cursor to start of filename area, clear to end, redraw buffer
|
|
printf("\r%s\033[K%s", directory, edit_buffer);
|
|
|
|
// Position cursor correctly
|
|
if (cursor_pos < buffer_len) {
|
|
printf("\033[%dD", buffer_len - cursor_pos);
|
|
}
|
|
fflush(stdout);
|
|
}
|
|
} else if (c == 27) {
|
|
// Escape sequence (arrow keys, etc.)
|
|
int c1 = getchar();
|
|
int c2 = getchar();
|
|
if (c1 == '[') {
|
|
if (c2 == 'C' && cursor_pos < buffer_len) {
|
|
// Right arrow
|
|
cursor_pos++;
|
|
printf("\033[1C");
|
|
fflush(stdout);
|
|
} else if (c2 == 'D' && cursor_pos > 0) {
|
|
// Left arrow
|
|
cursor_pos--;
|
|
printf("\033[1D");
|
|
fflush(stdout);
|
|
} else if (c2 == 'H') {
|
|
// Home key
|
|
printf("\033[%dD", cursor_pos);
|
|
cursor_pos = 0;
|
|
fflush(stdout);
|
|
} else if (c2 == 'F') {
|
|
// End key
|
|
printf("\033[%dC", buffer_len - cursor_pos);
|
|
cursor_pos = buffer_len;
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
} else if (c >= 32 && c <= 126) {
|
|
// Printable character
|
|
if (buffer_len < (int)sizeof(edit_buffer) - 1) {
|
|
// Move everything after cursor one position right
|
|
memmove(&edit_buffer[cursor_pos + 1], &edit_buffer[cursor_pos], buffer_len - cursor_pos + 1);
|
|
edit_buffer[cursor_pos] = c;
|
|
cursor_pos++;
|
|
buffer_len++;
|
|
|
|
// Redraw from cursor position
|
|
printf("%c", c);
|
|
if (cursor_pos < buffer_len) {
|
|
// Print remaining characters and move cursor back
|
|
printf("%s\033[%dD", &edit_buffer[cursor_pos], buffer_len - cursor_pos);
|
|
}
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
// Ignore other control characters
|
|
}
|
|
|
|
// Restore terminal
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
|
|
printf("\n");
|
|
|
|
// Construct final result
|
|
if (buffer_len == 0) {
|
|
// Empty input, use default
|
|
strncpy(result, default_path, result_size - 1);
|
|
} else {
|
|
// Combine directory with edited filename
|
|
edit_buffer[buffer_len] = '\0';
|
|
if (strlen(directory) > 0) {
|
|
snprintf(result, result_size, "%s%s", directory, edit_buffer);
|
|
} else {
|
|
strncpy(result, edit_buffer, result_size - 1);
|
|
}
|
|
}
|
|
result[result_size - 1] = '\0';
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Editor and file manager implementations
|
|
|
|
char* get_preferred_editor(void) {
|
|
// Check EDITOR environment variable first
|
|
char* editor = getenv("EDITOR");
|
|
if (editor && strlen(editor) > 0) {
|
|
// Verify the editor exists
|
|
char command[512];
|
|
snprintf(command, sizeof(command), "which %s >/dev/null 2>&1", editor);
|
|
if (system(command) == 0) {
|
|
return strdup(editor);
|
|
}
|
|
}
|
|
|
|
// Check VISUAL environment variable
|
|
editor = getenv("VISUAL");
|
|
if (editor && strlen(editor) > 0) {
|
|
char command[512];
|
|
snprintf(command, sizeof(command), "which %s >/dev/null 2>&1", editor);
|
|
if (system(command) == 0) {
|
|
return strdup(editor);
|
|
}
|
|
}
|
|
|
|
// Try common editors in order of preference
|
|
const char* common_editors[] = {"vim", "vi", "nano", "emacs", "gedit", NULL};
|
|
for (int i = 0; common_editors[i] != NULL; i++) {
|
|
char command[512];
|
|
snprintf(command, sizeof(command), "which %s >/dev/null 2>&1", common_editors[i]);
|
|
if (system(command) == 0) {
|
|
return strdup(common_editors[i]);
|
|
}
|
|
}
|
|
|
|
return NULL; // No editor found
|
|
}
|
|
|
|
char* get_preferred_file_manager(void) {
|
|
// Try file managers in order of preference
|
|
const char* file_managers[] = {"ranger", "fzf", "nnn", "lf", NULL};
|
|
|
|
for (int i = 0; file_managers[i] != NULL; i++) {
|
|
char command[512];
|
|
snprintf(command, sizeof(command), "which %s >/dev/null 2>&1", file_managers[i]);
|
|
if (system(command) == 0) {
|
|
return strdup(file_managers[i]);
|
|
}
|
|
}
|
|
|
|
return NULL; // No file manager found
|
|
}
|
|
|
|
int launch_text_editor(const char* initial_content, char* result_buffer, size_t buffer_size) {
|
|
char* editor = get_preferred_editor();
|
|
if (!editor) {
|
|
printf("Error: No text editor found. Set EDITOR environment variable or install vim/nano.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Create temporary file
|
|
char temp_filename[64];
|
|
snprintf(temp_filename, sizeof(temp_filename), "/tmp/otp_edit_%ld.tmp", time(NULL));
|
|
|
|
// Write initial content to temp file if provided
|
|
if (initial_content && strlen(initial_content) > 0) {
|
|
FILE* temp_file = fopen(temp_filename, "w");
|
|
if (temp_file) {
|
|
fputs(initial_content, temp_file);
|
|
fclose(temp_file);
|
|
}
|
|
} else {
|
|
// Create empty temp file
|
|
FILE* temp_file = fopen(temp_filename, "w");
|
|
if (temp_file) {
|
|
fclose(temp_file);
|
|
}
|
|
}
|
|
|
|
// Launch editor
|
|
printf("Opening %s for text editing...\n", editor);
|
|
char command[512];
|
|
snprintf(command, sizeof(command), "%s %s", editor, temp_filename);
|
|
|
|
int result = system(command);
|
|
|
|
if (result == 0) {
|
|
// Read the edited content back
|
|
FILE* temp_file = fopen(temp_filename, "r");
|
|
if (temp_file) {
|
|
size_t bytes_read = fread(result_buffer, 1, buffer_size - 1, temp_file);
|
|
result_buffer[bytes_read] = '\0';
|
|
|
|
// Remove trailing newline if present
|
|
if (bytes_read > 0 && result_buffer[bytes_read - 1] == '\n') {
|
|
result_buffer[bytes_read - 1] = '\0';
|
|
}
|
|
|
|
fclose(temp_file);
|
|
} else {
|
|
printf("Error: Cannot read edited content\n");
|
|
free(editor);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
} else {
|
|
printf("Editor exited with error or was cancelled\n");
|
|
free(editor);
|
|
unlink(temp_filename);
|
|
return 1;
|
|
}
|
|
|
|
// Clean up
|
|
unlink(temp_filename);
|
|
free(editor);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int launch_file_manager(const char* start_directory, char* selected_file, size_t buffer_size) {
|
|
char* fm = get_preferred_file_manager();
|
|
if (!fm) {
|
|
printf("No file manager found. Please install ranger, fzf, nnn, or lf.\n");
|
|
printf("Falling back to manual file path entry.\n");
|
|
return 1; // Fall back to manual entry
|
|
}
|
|
|
|
char temp_filename[64];
|
|
snprintf(temp_filename, sizeof(temp_filename), "/tmp/otp_file_%ld.tmp", time(NULL));
|
|
|
|
char command[512];
|
|
int result = 1;
|
|
|
|
printf("Opening %s for file selection...\n", fm);
|
|
|
|
if (strcmp(fm, "ranger") == 0) {
|
|
snprintf(command, sizeof(command), "cd '%s' && ranger --choosefile=%s",
|
|
start_directory ? start_directory : ".", temp_filename);
|
|
} else if (strcmp(fm, "fzf") == 0) {
|
|
snprintf(command, sizeof(command), "cd '%s' && find . -type f | fzf > %s",
|
|
start_directory ? start_directory : ".", temp_filename);
|
|
} else if (strcmp(fm, "nnn") == 0) {
|
|
snprintf(command, sizeof(command), "cd '%s' && nnn -p %s",
|
|
start_directory ? start_directory : ".", temp_filename);
|
|
} else if (strcmp(fm, "lf") == 0) {
|
|
snprintf(command, sizeof(command), "cd '%s' && lf -selection-path=%s",
|
|
start_directory ? start_directory : ".", temp_filename);
|
|
}
|
|
|
|
result = system(command);
|
|
|
|
if (result == 0 || result == 256) { // Some file managers return 256 on success
|
|
// Read selected file from temp file
|
|
FILE* temp_file = fopen(temp_filename, "r");
|
|
if (temp_file) {
|
|
if (fgets(selected_file, buffer_size, temp_file)) {
|
|
// Remove trailing newline
|
|
selected_file[strcspn(selected_file, "\n\r")] = 0;
|
|
|
|
// For relative paths from fzf, make absolute if needed
|
|
if (selected_file[0] == '.' && selected_file[1] == '/') {
|
|
char current_dir[512];
|
|
if (getcwd(current_dir, sizeof(current_dir))) {
|
|
char abs_path[1024];
|
|
snprintf(abs_path, sizeof(abs_path), "%s/%s", current_dir, selected_file + 2);
|
|
strncpy(selected_file, abs_path, buffer_size - 1);
|
|
selected_file[buffer_size - 1] = '\0';
|
|
}
|
|
}
|
|
|
|
fclose(temp_file);
|
|
unlink(temp_filename);
|
|
free(fm);
|
|
return 0; // Success
|
|
}
|
|
fclose(temp_file);
|
|
}
|
|
}
|
|
|
|
// Clean up and indicate failure
|
|
unlink(temp_filename);
|
|
free(fm);
|
|
return 1; // Fall back to manual entry
|
|
}
|
|
|
|
int handle_text_encrypt(void) {
|
|
printf("\n=== Text Encrypt ===\n");
|
|
|
|
// Launch text editor directly
|
|
char text_buffer[MAX_INPUT_SIZE];
|
|
if (launch_text_editor(NULL, text_buffer, sizeof(text_buffer)) != 0) {
|
|
printf("Error: Could not launch text editor\n");
|
|
return 1;
|
|
}
|
|
|
|
if (strlen(text_buffer) == 0) {
|
|
printf("No text entered - canceling encryption\n");
|
|
return 1;
|
|
}
|
|
|
|
// List available pads and get selection
|
|
int pad_count = list_available_pads();
|
|
if (pad_count == 0) {
|
|
printf("No pads available. Generate a pad first.\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("\nEnter pad selection (number, checksum, or prefix): ");
|
|
char pad_input[MAX_HASH_LENGTH];
|
|
if (!fgets(pad_input, sizeof(pad_input), stdin)) {
|
|
printf("Error: Failed to read pad selection\n");
|
|
return 1;
|
|
}
|
|
pad_input[strcspn(pad_input, "\n")] = 0;
|
|
|
|
return encrypt_text(pad_input, text_buffer);
|
|
}
|
|
|
|
int handle_file_encrypt(void) {
|
|
printf("\n=== File Encrypt ===\n");
|
|
|
|
// Launch file manager directly
|
|
char input_file[512];
|
|
if (launch_file_manager(".", input_file, sizeof(input_file)) != 0) {
|
|
printf("Error: Could not launch file manager\n");
|
|
return 1;
|
|
}
|
|
|
|
// Check if file exists
|
|
if (access(input_file, R_OK) != 0) {
|
|
printf("Error: File '%s' not found or cannot be read\n", input_file);
|
|
return 1;
|
|
}
|
|
|
|
// List available pads
|
|
int pad_count = list_available_pads();
|
|
if (pad_count == 0) {
|
|
printf("No pads available. Generate a pad first.\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("\nEnter pad selection (number, checksum, or prefix): ");
|
|
char pad_input[MAX_HASH_LENGTH];
|
|
if (!fgets(pad_input, sizeof(pad_input), stdin)) {
|
|
printf("Error: Failed to read pad selection\n");
|
|
return 1;
|
|
}
|
|
pad_input[strcspn(pad_input, "\n")] = 0;
|
|
|
|
// Ask for output format
|
|
printf("\nSelect output format:\n");
|
|
printf("1. Binary (.otp) - preserves file permissions\n");
|
|
printf("2. ASCII (.otp.asc) - text-safe format\n");
|
|
printf("Enter choice (1-2): ");
|
|
|
|
char format_input[10];
|
|
if (!fgets(format_input, sizeof(format_input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
int ascii_armor = (atoi(format_input) == 2) ? 1 : 0;
|
|
|
|
// Generate default output filename
|
|
char default_output[1024]; // Increased buffer size to prevent truncation warnings
|
|
if (ascii_armor) {
|
|
snprintf(default_output, sizeof(default_output), "%s.otp.asc", input_file);
|
|
} else {
|
|
snprintf(default_output, sizeof(default_output), "%s.otp", input_file);
|
|
}
|
|
|
|
// Use enhanced input function for output filename
|
|
char output_file[512];
|
|
if (get_filename_with_default("Output filename:", default_output, output_file, sizeof(output_file)) != 0) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
const char* output_filename = output_file;
|
|
|
|
return encrypt_file(pad_input, input_file, output_filename, ascii_armor);
|
|
}
|
|
|
|
|
|
int handle_pads_menu(void) {
|
|
printf("\n=== Pad Management ===\n");
|
|
|
|
// Get list of pads from current directory
|
|
DIR* dir = opendir(current_pads_dir);
|
|
if (!dir) {
|
|
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
|
|
return 1;
|
|
}
|
|
|
|
// Structure to store pad information
|
|
struct PadInfo {
|
|
char chksum[65];
|
|
char size_str[32];
|
|
char used_str[32];
|
|
double percentage;
|
|
char location[256]; // Store location info
|
|
};
|
|
|
|
struct PadInfo pads[100]; // Support up to 100 pads
|
|
int pad_count = 0;
|
|
|
|
// Collect all pad information
|
|
struct dirent* entry;
|
|
while ((entry = readdir(dir)) != NULL && pad_count < 100) {
|
|
if (strstr(entry->d_name, ".pad") && strlen(entry->d_name) == 68) {
|
|
strncpy(pads[pad_count].chksum, entry->d_name, 64);
|
|
pads[pad_count].chksum[64] = '\0';
|
|
|
|
// Get pad file size and usage info
|
|
char full_path[512];
|
|
snprintf(full_path, sizeof(full_path), "%s/%s", current_pads_dir, entry->d_name);
|
|
struct stat st;
|
|
if (stat(full_path, &st) == 0) {
|
|
// Get used bytes from state
|
|
uint64_t used_bytes;
|
|
read_state_offset(pads[pad_count].chksum, &used_bytes);
|
|
|
|
// Format total size
|
|
if (st.st_size < 1024) {
|
|
snprintf(pads[pad_count].size_str, sizeof(pads[pad_count].size_str), "%luB", st.st_size);
|
|
} else if (st.st_size < 1024 * 1024) {
|
|
snprintf(pads[pad_count].size_str, sizeof(pads[pad_count].size_str), "%.1fKB", (double)st.st_size / 1024.0);
|
|
} else if (st.st_size < 1024 * 1024 * 1024) {
|
|
snprintf(pads[pad_count].size_str, sizeof(pads[pad_count].size_str), "%.1fMB", (double)st.st_size / (1024.0 * 1024.0));
|
|
} else {
|
|
snprintf(pads[pad_count].size_str, sizeof(pads[pad_count].size_str), "%.2fGB", (double)st.st_size / (1024.0 * 1024.0 * 1024.0));
|
|
}
|
|
|
|
// Format used size
|
|
if (used_bytes < 1024) {
|
|
snprintf(pads[pad_count].used_str, sizeof(pads[pad_count].used_str), "%luB", used_bytes);
|
|
} else if (used_bytes < 1024 * 1024) {
|
|
snprintf(pads[pad_count].used_str, sizeof(pads[pad_count].used_str), "%.1fKB", (double)used_bytes / 1024.0);
|
|
} else if (used_bytes < 1024 * 1024 * 1024) {
|
|
snprintf(pads[pad_count].used_str, sizeof(pads[pad_count].used_str), "%.1fMB", (double)used_bytes / (1024.0 * 1024.0));
|
|
} else {
|
|
snprintf(pads[pad_count].used_str, sizeof(pads[pad_count].used_str), "%.2fGB", (double)used_bytes / (1024.0 * 1024.0 * 1024.0));
|
|
}
|
|
|
|
// Calculate percentage
|
|
pads[pad_count].percentage = (double)used_bytes / st.st_size * 100.0;
|
|
|
|
// Set location info using directory display
|
|
get_directory_display(full_path, pads[pad_count].location, sizeof(pads[pad_count].location));
|
|
|
|
pad_count++;
|
|
}
|
|
}
|
|
}
|
|
closedir(dir);
|
|
|
|
if (pad_count == 0) {
|
|
printf("No pads found.\n");
|
|
printf("\nOptions:\n");
|
|
printf(" \033[4mG\033[0menerate new pad\n");
|
|
printf(" \033[4mB\033[0mack to main menu\n");
|
|
printf("\nSelect option: ");
|
|
|
|
char input[10];
|
|
if (fgets(input, sizeof(input), stdin)) {
|
|
char choice = toupper(input[0]);
|
|
if (choice == 'G') {
|
|
int result = handle_generate_menu();
|
|
if (result == 0) {
|
|
// After successful pad generation, return to pads menu
|
|
return handle_pads_menu();
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Calculate minimal unique prefixes for each pad
|
|
char prefixes[100][65]; // Store the minimal prefix for each pad
|
|
int prefix_lengths[100]; // Length of minimal prefix for each pad
|
|
|
|
for (int i = 0; i < pad_count; i++) {
|
|
prefix_lengths[i] = 1;
|
|
|
|
// Find minimal unique prefix
|
|
while (prefix_lengths[i] <= 64) {
|
|
int unique = 1;
|
|
|
|
// Check if current prefix is unique among all other pads
|
|
for (int j = 0; j < pad_count; j++) {
|
|
if (i != j && strncmp(pads[i].chksum, pads[j].chksum, prefix_lengths[i]) == 0) {
|
|
unique = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (unique) {
|
|
break;
|
|
}
|
|
prefix_lengths[i]++;
|
|
}
|
|
|
|
// Store the minimal prefix
|
|
strncpy(prefixes[i], pads[i].chksum, prefix_lengths[i]);
|
|
prefixes[i][prefix_lengths[i]] = '\0';
|
|
}
|
|
|
|
// Display pads with minimal prefixes underlined
|
|
printf("\nAvailable pads:\n");
|
|
printf("%-8s %-12s %-12s %-12s %-8s\n", "ChkSum", "Dir", "Size", "Used", "% Used");
|
|
printf("%-8s %-12s %-12s %-12s %-8s\n", "--------", "------------", "----------", "----------", "------");
|
|
|
|
for (int i = 0; i < pad_count; i++) {
|
|
// Display first 8 characters of checksum with prefix underlined
|
|
char checksum_8char[9];
|
|
strncpy(checksum_8char, pads[i].chksum, 8);
|
|
checksum_8char[8] = '\0';
|
|
|
|
printf("\033[4m%.*s\033[0m%s %-12s %-12s %-12s %.1f%%\n",
|
|
prefix_lengths[i], checksum_8char, // Underlined prefix
|
|
checksum_8char + prefix_lengths[i], // Rest of 8-char checksum
|
|
pads[i].location, // Use the stored location info
|
|
pads[i].size_str,
|
|
pads[i].used_str,
|
|
pads[i].percentage);
|
|
}
|
|
|
|
printf("\nActions:\n");
|
|
printf(" \033[4mG\033[0menerate new pad\n");
|
|
printf(" \033[4mB\033[0mack to main menu\n");
|
|
printf("\nSelect pad (by prefix) or action: ");
|
|
|
|
char input[MAX_HASH_LENGTH];
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
// Handle actions first
|
|
if (toupper(input[0]) == 'G') {
|
|
int result = handle_generate_menu();
|
|
if (result == 0) {
|
|
// After successful pad generation, return to pads menu
|
|
return handle_pads_menu();
|
|
}
|
|
return result;
|
|
} else if (toupper(input[0]) == 'B') {
|
|
return 0; // Back to main menu
|
|
}
|
|
|
|
// Find matching pad by prefix
|
|
int selected_pad = -1;
|
|
for (int i = 0; i < pad_count; i++) {
|
|
if (strncmp(input, pads[i].chksum, strlen(input)) == 0) {
|
|
if (selected_pad == -1) {
|
|
selected_pad = i;
|
|
} else {
|
|
// Multiple matches - ambiguous
|
|
printf("Ambiguous prefix. Multiple matches found.\n");
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (selected_pad == -1) {
|
|
printf("No pad found matching prefix '%s'\n", input);
|
|
return 1;
|
|
}
|
|
|
|
// Show selected pad actions
|
|
printf("\n=== Pad: %.16s... ===\n", pads[selected_pad].chksum);
|
|
printf("Size: %s\n", pads[selected_pad].size_str);
|
|
printf("Used: %s (%.1f%%)\n", pads[selected_pad].used_str, pads[selected_pad].percentage);
|
|
|
|
printf("\nPad Actions:\n");
|
|
printf(" \033[4mI\033[0mnfo - Show detailed pad information\n");
|
|
printf(" \033[4mB\033[0mack to pad list\n");
|
|
printf("\nSelect action: ");
|
|
|
|
char action[10];
|
|
if (!fgets(action, sizeof(action), stdin)) {
|
|
printf("Error: Failed to read input\n");
|
|
return 1;
|
|
}
|
|
|
|
char action_choice = toupper(action[0]);
|
|
if (action_choice == 'I') {
|
|
return show_pad_info(pads[selected_pad].chksum);
|
|
}
|
|
|
|
// Default: back to pad list (recursive call)
|
|
return handle_pads_menu();
|
|
}
|
|
|
|
void get_directory_display(const char* file_path, char* result, size_t result_size) {
|
|
// Extract directory path from full file path
|
|
char dir_path[512];
|
|
char* last_slash = strrchr(file_path, '/');
|
|
|
|
if (last_slash) {
|
|
size_t dir_len = last_slash - file_path;
|
|
if (dir_len >= sizeof(dir_path)) {
|
|
dir_len = sizeof(dir_path) - 1;
|
|
}
|
|
strncpy(dir_path, file_path, dir_len);
|
|
dir_path[dir_len] = '\0';
|
|
} else {
|
|
// No directory separator, assume current directory
|
|
strcpy(dir_path, ".");
|
|
}
|
|
|
|
// USB Drive Detection and Smart Shortening
|
|
char* home_dir = getenv("HOME");
|
|
|
|
// Check for USB/removable media mount patterns
|
|
if (strstr(dir_path, "/media/") || strstr(dir_path, "/run/media/") || strstr(dir_path, "/mnt/")) {
|
|
// Extract USB label/name
|
|
char* media_start = NULL;
|
|
if (strstr(dir_path, "/media/")) {
|
|
media_start = strstr(dir_path, "/media/");
|
|
} else if (strstr(dir_path, "/run/media/")) {
|
|
media_start = strstr(dir_path, "/run/media/");
|
|
} else if (strstr(dir_path, "/mnt/")) {
|
|
media_start = strstr(dir_path, "/mnt/");
|
|
}
|
|
|
|
if (media_start) {
|
|
// Find the USB label part
|
|
char* path_after_media = strchr(media_start + 1, '/');
|
|
if (path_after_media) {
|
|
path_after_media++; // Skip the slash
|
|
|
|
// For /media/user/LABEL pattern, skip the username to get to the drive label
|
|
if (strstr(media_start, "/media/")) {
|
|
char* next_slash = strchr(path_after_media, '/');
|
|
if (next_slash) {
|
|
path_after_media = next_slash + 1;
|
|
}
|
|
}
|
|
// For /run/media/user/LABEL pattern, skip the username
|
|
else if (strstr(media_start, "/run/media/")) {
|
|
char* next_slash = strchr(path_after_media, '/');
|
|
if (next_slash) {
|
|
path_after_media = next_slash + 1;
|
|
}
|
|
}
|
|
|
|
// Extract just the USB label (up to next slash or end)
|
|
char* label_end = strchr(path_after_media, '/');
|
|
char usb_label[32];
|
|
if (label_end) {
|
|
size_t label_len = label_end - path_after_media;
|
|
if (label_len > sizeof(usb_label) - 1) label_len = sizeof(usb_label) - 1;
|
|
strncpy(usb_label, path_after_media, label_len);
|
|
usb_label[label_len] = '\0';
|
|
} else {
|
|
// USB label is the last part
|
|
strncpy(usb_label, path_after_media, sizeof(usb_label) - 1);
|
|
usb_label[sizeof(usb_label) - 1] = '\0';
|
|
}
|
|
|
|
// Format with USB: prefix, limiting total length to fit in result
|
|
snprintf(result, result_size, "USB:%s", usb_label);
|
|
// Truncate if too long
|
|
if (strlen(result) > 11) {
|
|
result[11] = '\0';
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Home directory shortening
|
|
if (home_dir && strncmp(dir_path, home_dir, strlen(home_dir)) == 0) {
|
|
if (dir_path[strlen(home_dir)] == '/' || dir_path[strlen(home_dir)] == '\0') {
|
|
// Replace home directory with ~
|
|
char temp[512];
|
|
snprintf(temp, sizeof(temp), "~%s", dir_path + strlen(home_dir));
|
|
|
|
// If result is too long, truncate intelligently
|
|
if (strlen(temp) > 11) {
|
|
// Show ~/...end_part
|
|
char* last_part = strrchr(temp, '/');
|
|
if (last_part && strlen(last_part) < 8) {
|
|
snprintf(result, result_size, "~...%s", last_part);
|
|
} else {
|
|
strncpy(result, temp, 11);
|
|
result[11] = '\0';
|
|
}
|
|
} else {
|
|
strncpy(result, temp, result_size - 1);
|
|
result[result_size - 1] = '\0';
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Current working directory
|
|
if (strcmp(dir_path, ".") == 0 || strcmp(dir_path, current_pads_dir) == 0) {
|
|
strncpy(result, "pads", result_size - 1);
|
|
result[result_size - 1] = '\0';
|
|
return;
|
|
}
|
|
|
|
// System/other paths - smart truncation with ellipsis
|
|
if (strlen(dir_path) > 11) {
|
|
// Try to show the most meaningful part
|
|
char* last_part = strrchr(dir_path, '/');
|
|
if (last_part && strlen(last_part) < 9) {
|
|
// Show .../last_part
|
|
snprintf(result, result_size, "...%s", last_part);
|
|
} else {
|
|
// Show first part with ellipsis
|
|
strncpy(result, dir_path, 8);
|
|
strncpy(result + 8, "...", result_size - 8 - 1);
|
|
result[result_size - 1] = '\0';
|
|
}
|
|
} else {
|
|
// Short enough, use as-is
|
|
strncpy(result, dir_path, result_size - 1);
|
|
result[result_size - 1] = '\0';
|
|
}
|
|
}
|
|
|
|
void print_usage(const char* program_name) {
|
|
printf("OTP Cipher - One Time Pad Implementation %s\n", get_version());
|
|
printf("%s\n", get_build_info());
|
|
printf("Usage:\n");
|
|
printf(" %s - Interactive mode\n", program_name);
|
|
printf(" %s generate|-g <size> - Generate new pad\n", program_name);
|
|
printf(" %s encrypt|-e <pad_checksum_prefix> [text] - Encrypt text\n", program_name);
|
|
printf(" %s decrypt|-d [encrypted_message] - Decrypt message\n", program_name);
|
|
printf(" %s -f <file> <pad_prefix> [-a] [-o <out>] - Encrypt file\n", program_name);
|
|
printf(" %s list|-l - List available pads\n", program_name);
|
|
printf("\nFile Operations:\n");
|
|
printf(" -f <file> <pad> - Encrypt file (binary .otp format)\n");
|
|
printf(" -f <file> <pad> -a - Encrypt file (ASCII .otp.asc format)\n");
|
|
printf(" -o <output> - Specify output filename\n");
|
|
printf("\nShort flags:\n");
|
|
printf(" -g generate -e encrypt -d decrypt -l list -f file\n");
|
|
printf("\nExamples:\n");
|
|
printf(" %s -e 1a2b3c \"Hello world\" - Encrypt inline text\n", program_name);
|
|
printf(" %s -f document.pdf 1a2b - Encrypt file (binary)\n", program_name);
|
|
printf(" %s -f document.pdf 1a2b -a - Encrypt file (ASCII)\n", program_name);
|
|
printf(" %s -f document.pdf 1a2b -o secret.otp - Encrypt with custom output\n", program_name);
|
|
printf(" %s -d \"-----BEGIN OTP MESSAGE-----...\" - Decrypt message/file\n", program_name);
|
|
printf(" %s -d encrypted.otp.asc - Decrypt ASCII file\n", program_name);
|
|
printf(" %s -g 1GB - Generate 1GB pad\n", program_name);
|
|
printf(" %s -l - List pads\n", program_name);
|
|
printf("\nSize examples: 1GB, 5TB, 512MB, 2048 (bytes)\n");
|
|
printf("Pad selection: Full chksum, prefix, or number from list\n");
|
|
}
|