Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0ce6f3253 | |||
| 5e1de92454 | |||
| 55cf7b1937 | |||
| 3d990091eb |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,5 +4,6 @@ files/
|
||||
Gemini.md
|
||||
TropicOfCancer-HenryMiller.txt
|
||||
.gitea_token
|
||||
true_rng/
|
||||
|
||||
# Auto-generated files (none currently)
|
||||
|
||||
BIN
otp-x86_64
BIN
otp-x86_64
Binary file not shown.
719
otp.c
719
otp.c
@@ -7,6 +7,7 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <dirent.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
@@ -330,7 +331,7 @@ int interactive_mode(void) {
|
||||
void show_main_menu(void) {
|
||||
|
||||
|
||||
printf("\n=========================== Main Menu - OTP v0.3.6 ===========================\n\n");
|
||||
printf("\n=========================== Main Menu - OTP v0.3.8 ===========================\n\n");
|
||||
|
||||
printf(" \033[4mT\033[0mext encrypt\n"); //TEXT ENCRYPT
|
||||
printf(" \033[4mF\033[0mile encrypt\n"); //FILE ENCRYPT
|
||||
@@ -2185,8 +2186,36 @@ int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) {
|
||||
|
||||
|
||||
|
||||
// To be removed
|
||||
// Format file size for display
|
||||
// void format_size_string(uint64_t bytes, char* result, size_t result_size) {
|
||||
// if (bytes == 0) {
|
||||
// strncpy(result, "Unknown", result_size - 1);
|
||||
// result[result_size - 1] = '\0';
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (bytes < 1024) {
|
||||
// snprintf(result, result_size, "%luB", bytes);
|
||||
// } else if (bytes < 1024 * 1024) {
|
||||
// snprintf(result, result_size, "%.1fKB", (double)bytes / 1024.0);
|
||||
// } else if (bytes < 1024 * 1024 * 1024) {
|
||||
// snprintf(result, result_size, "%.1fMB", (double)bytes / (1024.0 * 1024.0));
|
||||
// } else if (bytes < 1024ULL * 1024 * 1024 * 1024) {
|
||||
// snprintf(result, result_size, "%.2fGB", (double)bytes / (1024.0 * 1024.0 * 1024.0));
|
||||
// } else {
|
||||
// snprintf(result, result_size, "%.2fTB", (double)bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0));
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 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);
|
||||
@@ -2258,6 +2287,372 @@ unsigned char* custom_base64_decode(const char* input, int* output_length) {
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TRUERNG DEVICE DETECTION AND COMMUNICATION
|
||||
// Ported from true_rng/main.c for entropy collection
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Read USB device info from sysfs (ported from TrueRNG reference)
|
||||
int read_usb_device_info(const char* port_name, char* vid, char* pid) {
|
||||
char path[512];
|
||||
FILE *fp;
|
||||
|
||||
// Try to read idVendor first (works for both ttyUSB and ttyACM devices)
|
||||
snprintf(path, sizeof(path), "/sys/class/tty/%s/device/../idVendor", port_name);
|
||||
fp = fopen(path, "r");
|
||||
if (fp) {
|
||||
if (fgets(vid, 8, fp) != NULL) {
|
||||
// Remove newline if present
|
||||
int len = strlen(vid);
|
||||
if (len > 0 && vid[len-1] == '\n') {
|
||||
vid[len-1] = '\0';
|
||||
}
|
||||
} else {
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
fclose(fp);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Try to read idProduct
|
||||
snprintf(path, sizeof(path), "/sys/class/tty/%s/device/../idProduct", port_name);
|
||||
fp = fopen(path, "r");
|
||||
if (fp) {
|
||||
if (fgets(pid, 8, fp) != NULL) {
|
||||
// Remove newline if present
|
||||
int len = strlen(pid);
|
||||
if (len > 0 && pid[len-1] == '\n') {
|
||||
pid[len-1] = '\0';
|
||||
}
|
||||
} else {
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
fclose(fp);
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Find TrueRNG device port (ported and adapted from TrueRNG reference)
|
||||
// Returns: 0=not found, 1=TrueRNGproV2, 2=TrueRNGpro, 3=TrueRNG
|
||||
int find_truerng_port(char* port_path, size_t port_path_size, truerng_device_type_t* device_type) {
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
char vid[8], pid[8];
|
||||
int device_found = 0;
|
||||
|
||||
dir = opendir("/dev");
|
||||
if (dir == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
// Look for ttyUSB* or ttyACM* devices
|
||||
if (strncmp(entry->d_name, "ttyUSB", 6) == 0 ||
|
||||
strncmp(entry->d_name, "ttyACM", 6) == 0) {
|
||||
|
||||
if (read_usb_device_info(entry->d_name, vid, pid)) {
|
||||
// Convert to uppercase for comparison
|
||||
for (int i = 0; vid[i]; i++) vid[i] = toupper(vid[i]);
|
||||
for (int i = 0; pid[i]; i++) pid[i] = toupper(pid[i]);
|
||||
|
||||
// Check for TrueRNGproV2
|
||||
if (strcmp(vid, TRUERNGPROV2_VID) == 0 && strcmp(pid, TRUERNGPROV2_PID) == 0) {
|
||||
snprintf(port_path, port_path_size, "/dev/%s", entry->d_name);
|
||||
*device_type = TRUERNG_PRO_V2;
|
||||
device_found = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
// Check for TrueRNGpro
|
||||
if (strcmp(vid, TRUERNGPRO_VID) == 0 && strcmp(pid, TRUERNGPRO_PID) == 0) {
|
||||
snprintf(port_path, port_path_size, "/dev/%s", entry->d_name);
|
||||
*device_type = TRUERNG_PRO;
|
||||
device_found = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
// Check for TrueRNG
|
||||
if (strcmp(vid, TRUERNG_VID) == 0 && strcmp(pid, TRUERNG_PID) == 0) {
|
||||
snprintf(port_path, port_path_size, "/dev/%s", entry->d_name);
|
||||
*device_type = TRUERNG_ORIGINAL;
|
||||
device_found = 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
return device_found;
|
||||
}
|
||||
|
||||
// Setup serial port for TrueRNG communication (ported from TrueRNG reference)
|
||||
int setup_truerng_serial_port(const char* port_path) {
|
||||
int fd;
|
||||
struct termios tty;
|
||||
|
||||
fd = open(port_path, O_RDWR | O_NOCTTY);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get current port settings
|
||||
if (tcgetattr(fd, &tty) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set baud rate (TrueRNG devices use 9600)
|
||||
cfsetospeed(&tty, B9600);
|
||||
cfsetispeed(&tty, B9600);
|
||||
|
||||
// 8N1 mode
|
||||
tty.c_cflag &= ~PARENB; // No parity
|
||||
tty.c_cflag &= ~CSTOPB; // 1 stop bit
|
||||
tty.c_cflag &= ~CSIZE; // Clear size bits
|
||||
tty.c_cflag |= CS8; // 8 data bits
|
||||
tty.c_cflag &= ~CRTSCTS; // No hardware flow control
|
||||
tty.c_cflag |= CREAD | CLOCAL; // Enable reading and ignore modem controls
|
||||
|
||||
// Raw input mode
|
||||
tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
|
||||
tty.c_oflag &= ~OPOST;
|
||||
|
||||
// Set for blocking reads - wait for data indefinitely
|
||||
tty.c_cc[VMIN] = 1; // Block until at least 1 character is received
|
||||
tty.c_cc[VTIME] = 0; // No timeout
|
||||
|
||||
// Apply settings
|
||||
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Flush input buffer
|
||||
tcflush(fd, TCIFLUSH);
|
||||
|
||||
// Set DTR
|
||||
int status;
|
||||
ioctl(fd, TIOCMGET, &status);
|
||||
status |= TIOCM_DTR;
|
||||
ioctl(fd, TIOCMSET, &status);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Get friendly name for TrueRNG device type
|
||||
const char* get_truerng_device_name(truerng_device_type_t device_type) {
|
||||
switch (device_type) {
|
||||
case TRUERNG_PRO_V2: return "TrueRNGproV2";
|
||||
case TRUERNG_PRO: return "TrueRNGpro";
|
||||
case TRUERNG_ORIGINAL: return "TrueRNG";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
// Collect entropy from TrueRNG device with equivalent quality to keyboard entropy
|
||||
int collect_truerng_entropy(unsigned char* entropy_buffer, size_t target_bytes,
|
||||
size_t* collected_bytes, int display_progress) {
|
||||
char port_path[512];
|
||||
truerng_device_type_t device_type;
|
||||
int serial_fd = -1;
|
||||
|
||||
// Find TrueRNG device
|
||||
if (!find_truerng_port(port_path, sizeof(port_path), &device_type)) {
|
||||
if (display_progress) {
|
||||
printf("No TrueRNG device found.\n");
|
||||
printf("\nSupported devices:\n");
|
||||
printf(" - TrueRNG (PID: %s, VID: %s)\n", TRUERNG_VID, TRUERNG_PID);
|
||||
printf(" - TrueRNGpro (PID: %s, VID: %s)\n", TRUERNGPRO_VID, TRUERNGPRO_PID);
|
||||
printf(" - TrueRNGproV2 (PID: %s, VID: %s)\n", TRUERNGPROV2_VID, TRUERNGPROV2_PID);
|
||||
printf("\nPlease connect a TrueRNG device and try again.\n");
|
||||
}
|
||||
return 1; // Device not found
|
||||
}
|
||||
|
||||
if (display_progress) {
|
||||
printf("Found %s at %s\n", get_truerng_device_name(device_type), port_path);
|
||||
printf("Collecting %zu bytes of entropy...\n", target_bytes);
|
||||
}
|
||||
|
||||
// Setup serial port
|
||||
serial_fd = setup_truerng_serial_port(port_path);
|
||||
if (serial_fd < 0) {
|
||||
if (display_progress) {
|
||||
printf("Error: Cannot open TrueRNG device at %s\n", port_path);
|
||||
printf("Check device permissions or run as root.\n");
|
||||
}
|
||||
return 2; // Serial port setup failed
|
||||
}
|
||||
|
||||
// Collect entropy data
|
||||
size_t bytes_read = 0;
|
||||
unsigned char buffer[1024]; // Read in 1KB chunks
|
||||
time_t start_time = time(NULL);
|
||||
|
||||
while (bytes_read < target_bytes) {
|
||||
size_t chunk_size = sizeof(buffer);
|
||||
if (target_bytes - bytes_read < chunk_size) {
|
||||
chunk_size = target_bytes - bytes_read;
|
||||
}
|
||||
|
||||
size_t bytes_in_chunk = 0;
|
||||
while (bytes_in_chunk < chunk_size) {
|
||||
ssize_t result = read(serial_fd, buffer + bytes_in_chunk, chunk_size - bytes_in_chunk);
|
||||
if (result < 0) {
|
||||
if (display_progress) {
|
||||
printf("Error: Failed to read from TrueRNG device\n");
|
||||
}
|
||||
close(serial_fd);
|
||||
return 3; // Read failed
|
||||
} else if (result == 0) {
|
||||
if (display_progress) {
|
||||
printf("Error: No data received from TrueRNG device\n");
|
||||
}
|
||||
close(serial_fd);
|
||||
return 4; // No data
|
||||
}
|
||||
bytes_in_chunk += result;
|
||||
}
|
||||
|
||||
// Copy to entropy buffer
|
||||
memcpy(entropy_buffer + bytes_read, buffer, chunk_size);
|
||||
bytes_read += chunk_size;
|
||||
|
||||
// Show progress for large collections
|
||||
if (display_progress && bytes_read % (4 * 1024) == 0) { // Every 4KB
|
||||
double percentage = (double)bytes_read / target_bytes * 100.0;
|
||||
printf("Progress: %.1f%% (%zu/%zu bytes)\r", percentage, bytes_read, target_bytes);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
close(serial_fd);
|
||||
|
||||
if (display_progress) {
|
||||
double collection_time = difftime(time(NULL), start_time);
|
||||
printf("\n✓ TrueRNG entropy collection complete!\n");
|
||||
printf(" Collected: %zu bytes in %.1f seconds\n", bytes_read, collection_time);
|
||||
printf(" Device: %s\n", get_truerng_device_name(device_type));
|
||||
if (collection_time > 0) {
|
||||
printf(" Rate: %.1f KB/s\n", (double)bytes_read / collection_time / 1024.0);
|
||||
}
|
||||
}
|
||||
|
||||
*collected_bytes = bytes_read;
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Collect dice entropy with manual input validation
|
||||
int collect_dice_entropy(unsigned char* entropy_buffer, size_t target_bytes,
|
||||
size_t* collected_bytes, int display_progress) {
|
||||
if (display_progress) {
|
||||
printf("=== Dice Entropy Collection ===\n");
|
||||
printf("Enter dice rolls as sequences of digits 1-6.\n");
|
||||
printf("Target: %zu bytes (%zu dice rolls needed)\n", target_bytes, target_bytes * 4);
|
||||
printf("Press Enter after each sequence, or 'done' when finished.\n\n");
|
||||
}
|
||||
|
||||
size_t entropy_bits = 0;
|
||||
size_t target_bits = target_bytes * 8;
|
||||
unsigned char current_byte = 0;
|
||||
int bits_in_byte = 0;
|
||||
size_t bytes_written = 0;
|
||||
|
||||
char input[256];
|
||||
|
||||
while (entropy_bits < target_bits && bytes_written < target_bytes) {
|
||||
if (display_progress) {
|
||||
double percentage = (double)entropy_bits / target_bits * 100.0;
|
||||
printf("Progress: %.1f%% (%zu/%zu bits) - Enter dice rolls: ",
|
||||
percentage, entropy_bits, target_bits);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
if (!fgets(input, sizeof(input), stdin)) {
|
||||
if (display_progress) {
|
||||
printf("Error: Failed to read input\n");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Remove newline
|
||||
input[strcspn(input, "\n")] = 0;
|
||||
|
||||
// Check for done command
|
||||
if (strcmp(input, "done") == 0 && entropy_bits >= target_bits / 2) {
|
||||
break; // Allow early exit if we have at least half the target
|
||||
}
|
||||
|
||||
// Process dice rolls
|
||||
for (size_t i = 0; input[i] && entropy_bits < target_bits && bytes_written < target_bytes; i++) {
|
||||
char c = input[i];
|
||||
if (c >= '1' && c <= '6') {
|
||||
// Convert dice roll (1-6) to 3 bits of entropy
|
||||
unsigned char roll_value = c - '1'; // 0-5
|
||||
|
||||
// Pack 3 bits into current byte
|
||||
for (int bit = 2; bit >= 0 && entropy_bits < target_bits; bit--) {
|
||||
current_byte = (current_byte << 1) | ((roll_value >> bit) & 1);
|
||||
bits_in_byte++;
|
||||
entropy_bits++;
|
||||
|
||||
if (bits_in_byte == 8) {
|
||||
entropy_buffer[bytes_written++] = current_byte;
|
||||
current_byte = 0;
|
||||
bits_in_byte = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle partial byte
|
||||
if (bits_in_byte > 0 && bytes_written < target_bytes) {
|
||||
// Pad remaining bits with zeros
|
||||
current_byte <<= (8 - bits_in_byte);
|
||||
entropy_buffer[bytes_written++] = current_byte;
|
||||
}
|
||||
|
||||
if (display_progress) {
|
||||
printf("\n✓ Dice entropy collection complete!\n");
|
||||
printf(" Collected: %zu bytes from dice rolls\n", bytes_written);
|
||||
printf(" Entropy bits: %zu\n", entropy_bits);
|
||||
}
|
||||
|
||||
*collected_bytes = bytes_written;
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Collect entropy by source type with unified interface
|
||||
int collect_entropy_by_source(entropy_source_t source, unsigned char* entropy_buffer,
|
||||
size_t target_bytes, size_t* collected_bytes, int display_progress) {
|
||||
switch (source) {
|
||||
case ENTROPY_SOURCE_KEYBOARD:
|
||||
return collect_entropy_with_feedback(entropy_buffer, target_bytes, collected_bytes, 1);
|
||||
|
||||
case ENTROPY_SOURCE_TRUERNG:
|
||||
return collect_truerng_entropy(entropy_buffer, target_bytes, collected_bytes, display_progress);
|
||||
|
||||
case ENTROPY_SOURCE_DICE:
|
||||
return collect_dice_entropy(entropy_buffer, target_bytes, collected_bytes, display_progress);
|
||||
|
||||
default:
|
||||
if (display_progress) {
|
||||
printf("Error: Unknown entropy source\n");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
double get_precise_time(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
@@ -2848,7 +3243,7 @@ int generate_ascii_armor(const char* chksum, uint64_t offset, const unsigned cha
|
||||
strcpy(*ascii_output, "-----BEGIN OTP MESSAGE-----\n");
|
||||
|
||||
char temp_line[256];
|
||||
snprintf(temp_line, sizeof(temp_line), "Version: v0.3.6\n");
|
||||
snprintf(temp_line, sizeof(temp_line), "Version: v0.3.8\n");
|
||||
strcat(*ascii_output, temp_line);
|
||||
|
||||
snprintf(temp_line, sizeof(temp_line), "Pad-ChkSum: %s\n", chksum);
|
||||
@@ -3728,7 +4123,7 @@ char* select_pad_interactive(const char* title, const char* prompt, pad_filter_t
|
||||
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));
|
||||
// get_directory_display(full_path, pads[pad_count].location, sizeof(pads[pad_count].location));
|
||||
|
||||
pad_count++;
|
||||
}
|
||||
@@ -3961,7 +4356,7 @@ int handle_pads_menu(void) {
|
||||
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));
|
||||
// get_directory_display(full_path, pads[pad_count].location, sizeof(pads[pad_count].location));
|
||||
|
||||
pad_count++;
|
||||
}
|
||||
@@ -4166,158 +4561,212 @@ int handle_pads_menu(void) {
|
||||
}
|
||||
}
|
||||
|
||||
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, '/');
|
||||
// To be removed
|
||||
// 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, ".");
|
||||
}
|
||||
// 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");
|
||||
// // 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/");
|
||||
}
|
||||
// // 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
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
// // 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';
|
||||
}
|
||||
// // 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
// // 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));
|
||||
// // 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;
|
||||
}
|
||||
}
|
||||
// // 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;
|
||||
}
|
||||
// // 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';
|
||||
}
|
||||
}
|
||||
// // 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';
|
||||
// }
|
||||
// }
|
||||
|
||||
int handle_add_entropy_to_pad(const char* pad_chksum) {
|
||||
printf("\n=== Add Entropy to Pad: %.16s... ===\n", pad_chksum);
|
||||
printf("This will enhance the randomness of your pad using keyboard entropy.\n");
|
||||
printf("The entropy will be processed with Chacha20 and distributed throughout the entire pad.\n\n");
|
||||
|
||||
printf("Entropy collection options:\n");
|
||||
// Present entropy source selection menu with consistent formatting
|
||||
printf("Select entropy source:\n");
|
||||
printf(" \033[4mK\033[0meyboard entropy - Random typing for entropy collection\n");
|
||||
printf(" \033[4mD\033[0mice rolls - Manual dice roll input for high-quality entropy\n");
|
||||
printf(" \033[4mT\033[0mrueRNG devices - Hardware random number generators\n");
|
||||
printf(" E\033[4mx\033[0mit\n");
|
||||
printf("\nSelect option: ");
|
||||
|
||||
char source_input[10];
|
||||
if (!fgets(source_input, sizeof(source_input), stdin)) {
|
||||
printf("Error: Failed to read input\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char choice = toupper(source_input[0]);
|
||||
entropy_source_t entropy_source;
|
||||
|
||||
switch (choice) {
|
||||
case 'K':
|
||||
entropy_source = ENTROPY_SOURCE_KEYBOARD;
|
||||
break;
|
||||
case 'D':
|
||||
entropy_source = ENTROPY_SOURCE_DICE;
|
||||
break;
|
||||
case 'T':
|
||||
entropy_source = ENTROPY_SOURCE_TRUERNG;
|
||||
break;
|
||||
case 'X':
|
||||
return 0; // Exit
|
||||
default:
|
||||
printf("Invalid choice. Please select K, D, T, or X.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t target_bytes;
|
||||
|
||||
// For TrueRNG, automatically use the full pad size
|
||||
if (entropy_source == ENTROPY_SOURCE_TRUERNG) {
|
||||
// Get the pad file size
|
||||
char pad_path[1024];
|
||||
char state_path[1024];
|
||||
get_pad_path(pad_chksum, pad_path, state_path);
|
||||
|
||||
struct stat pad_stat;
|
||||
if (stat(pad_path, &pad_stat) != 0) {
|
||||
printf("Error: Cannot get pad file size\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
target_bytes = (size_t)pad_stat.st_size;
|
||||
printf("\nTrueRNG selected - will enhance entire pad with hardware entropy\n");
|
||||
printf("Pad size: %.2f GB (%zu bytes)\n",
|
||||
(double)target_bytes / (1024.0 * 1024.0 * 1024.0), target_bytes);
|
||||
} else {
|
||||
// For other entropy sources, show the selection menu
|
||||
printf("\nEntropy collection options:\n");
|
||||
printf(" 1. Recommended (2048 bytes) - Optimal security\n");
|
||||
printf(" 2. Minimum (1024 bytes) - Good security\n");
|
||||
printf(" 3. Maximum (4096 bytes) - Maximum security\n");
|
||||
printf(" 4. Custom amount\n");
|
||||
printf("Enter choice (1-4): ");
|
||||
|
||||
char choice_input[10];
|
||||
if (!fgets(choice_input, sizeof(choice_input), stdin)) {
|
||||
char amount_input[10];
|
||||
if (!fgets(amount_input, sizeof(amount_input), stdin)) {
|
||||
printf("Error: Failed to read input\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t target_bytes = 2048; // Default
|
||||
int choice = atoi(choice_input);
|
||||
target_bytes = 2048; // Default
|
||||
int amount_choice = atoi(amount_input);
|
||||
|
||||
switch (choice) {
|
||||
switch (amount_choice) {
|
||||
case 1:
|
||||
target_bytes = 2048;
|
||||
break;
|
||||
@@ -4346,8 +4795,9 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
|
||||
target_bytes = 2048; // Default to recommended
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\nCollecting %zu bytes of entropy...\n", target_bytes);
|
||||
printf("\nCollecting %zu bytes of entropy from selected source...\n", target_bytes);
|
||||
|
||||
// Allocate entropy buffer
|
||||
unsigned char* entropy_buffer = malloc(MAX_ENTROPY_BUFFER);
|
||||
@@ -4356,9 +4806,9 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Collect entropy with visual feedback
|
||||
// Collect entropy using unified interface
|
||||
size_t collected_bytes = 0;
|
||||
int result = collect_entropy_with_feedback(entropy_buffer, target_bytes, &collected_bytes, 1);
|
||||
int result = collect_entropy_by_source(entropy_source, entropy_buffer, target_bytes, &collected_bytes, 1);
|
||||
|
||||
if (result != 0) {
|
||||
printf("Error: Entropy collection failed\n");
|
||||
@@ -4393,8 +4843,9 @@ int handle_add_entropy_to_pad(const char* pad_chksum) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void print_usage(const char* program_name) {
|
||||
printf("OTP Cipher - One Time Pad Implementation v0.3.6\n");
|
||||
printf("OTP Cipher - One Time Pad Implementation v0.3.8\n");
|
||||
printf("Built for testing entropy system\n");
|
||||
printf("Usage:\n");
|
||||
printf(" %s - Interactive mode\n", program_name);
|
||||
|
||||
46
otp.h
46
otp.h
@@ -14,7 +14,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// Constants
|
||||
#define MAX_INPUT_SIZE 4096
|
||||
@@ -92,6 +98,11 @@ 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);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// USB DRIVE MANAGEMENT FUNCTIONS
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// EXTERNAL TOOL INTEGRATION FUNCTIONS
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -119,6 +130,13 @@ int decrypt_ascii_file(const char* input_file, const char* output_file);
|
||||
// ENHANCED ENTROPY SYSTEM FUNCTIONS
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Entropy source types
|
||||
typedef enum {
|
||||
ENTROPY_SOURCE_KEYBOARD = 1,
|
||||
ENTROPY_SOURCE_DICE = 2,
|
||||
ENTROPY_SOURCE_TRUERNG = 3
|
||||
} entropy_source_t;
|
||||
|
||||
// Terminal control for entropy collection
|
||||
int setup_raw_terminal(struct termios* original_termios);
|
||||
void restore_terminal(struct termios* original_termios);
|
||||
@@ -130,6 +148,34 @@ void display_entropy_progress(const entropy_collection_state_t* state);
|
||||
void draw_progress_bar(double percentage, int width);
|
||||
void draw_quality_bar(double quality, int width, const char* label);
|
||||
|
||||
// TrueRNG Device Constants (updated to match otp.c implementation)
|
||||
#define TRUERNG_VID "04D8"
|
||||
#define TRUERNG_PID "F5FE"
|
||||
#define TRUERNGPRO_VID "16D0"
|
||||
#define TRUERNGPRO_PID "0AA0"
|
||||
#define TRUERNGPROV2_VID "04D8"
|
||||
#define TRUERNGPROV2_PID "EBB5"
|
||||
|
||||
// TrueRNG Device Type enumeration
|
||||
typedef enum {
|
||||
TRUERNG_ORIGINAL = 1,
|
||||
TRUERNG_PRO = 2,
|
||||
TRUERNG_PRO_V2 = 3
|
||||
} truerng_device_type_t;
|
||||
|
||||
// TrueRNG entropy collection functions (updated to match implementation)
|
||||
int find_truerng_port(char* port_path, size_t port_path_size, truerng_device_type_t* device_type);
|
||||
int setup_truerng_serial_port(const char* port_path);
|
||||
int collect_truerng_entropy(unsigned char* entropy_buffer, size_t target_bytes, size_t* collected_bytes, int display_progress);
|
||||
const char* get_truerng_device_name(truerng_device_type_t device_type);
|
||||
int read_usb_device_info(const char* port_name, char* vid, char* pid);
|
||||
|
||||
// Dice entropy collection functions (updated to match implementation)
|
||||
int collect_dice_entropy(unsigned char* entropy_buffer, size_t target_bytes, size_t* collected_bytes, int display_progress);
|
||||
|
||||
// Unified entropy collection interface (updated to match implementation)
|
||||
int collect_entropy_by_source(entropy_source_t source, unsigned char* entropy_buffer, size_t target_bytes, size_t* collected_bytes, int display_progress);
|
||||
|
||||
// Entropy quality calculation
|
||||
double calculate_timing_quality(const entropy_collection_state_t* state);
|
||||
double calculate_variety_quality(const entropy_collection_state_t* state);
|
||||
|
||||
1
true_rng
Submodule
1
true_rng
Submodule
Submodule true_rng added at 52ed7af980
Reference in New Issue
Block a user