Version 0.0.1

This commit is contained in:
Your Name 2025-09-28 11:05:26 -04:00
parent 7f8bc98b48
commit e018f30fdd
5 changed files with 304 additions and 29 deletions

View File

@ -51,6 +51,9 @@ gcc -Wall -Wextra -std=c99 -O2 -o truerng main.c
- `-f, --format <FORMAT>` - Output format: binary, hex, base64, decimal
- Default: binary when piped, hex in interactive mode
- `-o, --output <FILE>` - Output filename (works with all formats)
- `-d, --device <DEVICE>` - Select specific device (index, port, or type)
- Examples: `1`, `/dev/ttyACM0`, `pro`, `prov2`, `truerng`
- `-l, --list` - List all available TrueRNG devices
- `-q, --quiet` - Suppress statistics/progress output
- `-v, --verbose` - Show detailed device information
- `-h, --help` - Show help message
@ -65,23 +68,51 @@ The `-n` option supports convenient size suffixes (case-insensitive):
Decimal values are supported: `1.5MB`, `2.5GB`, etc.
### Device Selection
When multiple TrueRNG devices are connected, you can select a specific device using the `-d` option:
- **By index**: Use the device number from `--list` (e.g., `-d 1`, `-d 2`)
- **By port path**: Use the full device path (e.g., `-d /dev/ttyACM0`)
- **By device type**: Use device type keywords:
- `truerng` - Select original TrueRNG device
- `pro` - Select TrueRNGpro device
- `prov2` - Select TrueRNGproV2 device
```bash
# List all available devices
./truerng --list
# Use first device from list
./truerng -d 1 -n 1MB
# Use specific port
./truerng -d /dev/ttyACM1 -n 512K
# Use TrueRNGpro device
./truerng -d pro -n 2MB -f hex
```
### Examples
```bash
# Basic usage - interactive mode with hex output
./truerng
# Generate 1KB of data in hex format
# List available devices
./truerng --list
# Generate 1KB of data in hex format using first device
./truerng -n 1K -f hex
# Generate 2.5MB and pipe to another program
./truerng -n 2.5MB | xxd
# Generate 2.5MB using specific device and pipe to another program
./truerng -d 2 -n 2.5MB | xxd
# Save 1GB of binary data to file quietly
./truerng -n 1GB -o random.dat -q
# Save 1GB of binary data to file quietly using TrueRNGpro
./truerng -d pro -n 1GB -o random.dat -q
# Generate 512KB as hex and save to file
./truerng -n 512K -f hex -o output.hex
# Generate 512KB as hex and save to file using specific port
./truerng -d /dev/ttyACM0 -n 512K -f hex -o output.hex
# Generate base64 output with verbose device info
./truerng -n 1MB -f base64 -v
@ -103,6 +134,14 @@ The program automatically detects the execution context:
- **Interactive mode**: Shows progress, statistics, and defaults to hex output
- **Piped mode**: Optimized for piping, defaults to binary output
- **File output**: Can be combined with any format using `-o` option
- **Multiple devices**: Automatically detects and warns when multiple devices are present
### Multiple Device Handling
When multiple TrueRNG devices are connected:
- Without `-d` option: Uses the first available device with a warning
- With `-d` option: Uses the specified device
- Use `--list` to see all available devices and their details
## Configuration
@ -135,6 +174,14 @@ The C implementation:
## Sample Output
### Device Listing
```bash
./truerng --list
Available TrueRNG devices:
1. TrueRNGproV2 at /dev/ttyACM0 (VID:04D8 PID:EBB5)
2. TrueRNG at /dev/ttyACM2 (VID:04D8 PID:F5FE)
```
### Interactive Mode
```
TrueRNG - True Random Number Generator
@ -155,6 +202,17 @@ Extra zeros: 48
=======
```
### Multiple Device Warning
```
Multiple TrueRNG devices found - using first available
(Use -d option to select specific device or --list to see all)
TrueRNG - True Random Number Generator
==================================================
TrueRNGproV2 Found
Using port: /dev/ttyACM0
...
```
### Piped Mode with Verbose
```
TrueRNG - True Random Number Generator
@ -193,13 +251,24 @@ Options:
Supports suffixes: K, MB, GB, TB (e.g., 1K, 2.5MB, 1GB)
-f, --format <FORMAT> Output format: binary, hex, base64, decimal (default: binary when piped, interactive otherwise)
-o, --output <FILE> Output filename (ignored in piped mode)
-d, --device <DEVICE> Select specific device (index, port, or type)
Examples: 1, /dev/ttyACM0, pro, prov2
-l, --list List all available TrueRNG devices
-q, --quiet Suppress statistics/progress
-v, --verbose Show detailed device information
-h, --help Show this help message
Device Selection:
When multiple devices are present, use -d to select:
-d 1 Select first device from list
-d /dev/ttyACM1 Select by port path
-d pro Select TrueRNGpro device
-d prov2 Select TrueRNGproV2 device
-d truerng Select original TrueRNG device
Examples:
truerng -n 1024 -f hex # Interactive mode with hex output
truerng -n 1K -f hex # Same as above using K suffix
truerng -n 2.5MB | xxd # Piped mode with MB suffix
truerng -n 1GB -o random.dat -q # Save 1GB to file quietly
truerng -n 512K -f hex -o output.hex # Save 512KB as hex to file
truerng --list # List available devices
truerng -n 1K -f hex # Use first available device
truerng -d 2 -n 1MB # Use second device from list
truerng -d /dev/ttyACM1 -n 512K # Use specific port
truerng -d pro -n 1GB -o random.dat # Use TrueRNGpro device

237
main.c
View File

@ -48,11 +48,25 @@
// Output formats
enum Format { BINARY, HEX, BASE64, DECIMAL };
// Device types
enum DeviceType { DEVICE_TRUERNGPROV2 = 1, DEVICE_TRUERNGPRO = 2, DEVICE_TRUERNG = 3 };
// Device information structure
struct DeviceInfo {
char port_path[MAX_PATH];
char device_name[32];
enum DeviceType type;
char vid[8];
char pid[8];
};
// Command-line options structure
struct Options {
long long bytes;
enum Format format;
char *output_file;
char *device_selector;
int list_devices;
int quiet;
int verbose;
int help;
@ -183,23 +197,35 @@ void print_usage(void) {
printf(" Supports suffixes: K, MB, GB, TB (e.g., 1K, 2.5MB, 1GB)\n");
printf(" -f, --format <FORMAT> Output format: binary, hex, base64, decimal (default: binary when piped, interactive otherwise)\n");
printf(" -o, --output <FILE> Output filename (ignored in piped mode)\n");
printf(" -d, --device <DEVICE> Select specific device (index, port, or type)\n");
printf(" Examples: 1, /dev/ttyACM0, pro, prov2\n");
printf(" -l, --list List all available TrueRNG devices\n");
printf(" -q, --quiet Suppress statistics/progress\n");
printf(" -v, --verbose Show detailed device information\n");
printf(" -h, --help Show this help message\n");
printf("\nDevice Selection:\n");
printf(" When multiple devices are present, use -d to select:\n");
printf(" -d 1 Select first device from list\n");
printf(" -d /dev/ttyACM1 Select by port path\n");
printf(" -d pro Select TrueRNGpro device\n");
printf(" -d prov2 Select TrueRNGproV2 device\n");
printf(" -d truerng Select original TrueRNG device\n");
printf("\nExamples:\n");
printf(" truerng -n 1024 -f hex # Interactive mode with hex output\n");
printf(" truerng -n 1K -f hex # Same as above using K suffix\n");
printf(" truerng -n 2.5MB | xxd # Piped mode with MB suffix\n");
printf(" truerng -n 1GB -o random.dat -q # Save 1GB to file quietly\n");
printf(" truerng -n 512K -f hex -o output.hex # Save 512KB as hex to file\n");
printf(" truerng --list # List available devices\n");
printf(" truerng -n 1K -f hex # Use first available device\n");
printf(" truerng -d 2 -n 1MB # Use second device from list\n");
printf(" truerng -d /dev/ttyACM1 -n 512K # Use specific port\n");
printf(" truerng -d pro -n 1GB -o random.dat # Use TrueRNGpro device\n");
}
int parse_arguments(int argc, char *argv[]) {
static const char *opt_string = "n:f:o:qvh";
static const char *opt_string = "n:f:o:d:lqvh";
static struct option long_options[] = {
{"bytes", required_argument, 0, 'n'},
{"format", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{"device", required_argument, 0, 'd'},
{"list", no_argument, 0, 'l'},
{"quiet", no_argument, 0, 'q'},
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
@ -209,6 +235,8 @@ int parse_arguments(int argc, char *argv[]) {
options.bytes = 1048576; // Default 1MB
options.format = BINARY;
options.output_file = NULL;
options.device_selector = NULL;
options.list_devices = 0;
options.quiet = 0;
options.verbose = 0;
options.help = 0;
@ -239,6 +267,12 @@ int parse_arguments(int argc, char *argv[]) {
case 'o':
options.output_file = optarg;
break;
case 'd':
options.device_selector = optarg;
break;
case 'l':
options.list_devices = 1;
break;
case 'q':
options.quiet = 1;
break;
@ -262,7 +296,9 @@ int is_piped(void) {
// Function prototypes
void init_ones_lookup_table(void);
int find_truerng_port(char* port_path, int piped, int verbose);
int find_all_truerng_devices(struct DeviceInfo devices[], int max_devices);
int select_device_from_list(struct DeviceInfo devices[], int device_count, const char* selector, char* selected_port);
void list_devices(struct DeviceInfo devices[], int device_count);
int setup_serial_port(const char* port_path);
int read_usb_device_info(const char* port_name, char* vid, char* pid);
double get_time_diff(struct timeval start, struct timeval end);
@ -289,6 +325,132 @@ void init_ones_lookup_table(void) {
}
}
// Find all TrueRNG devices and populate device list
// Returns: number of devices found
int find_all_truerng_devices(struct DeviceInfo devices[], int max_devices) {
DIR *dir;
struct dirent *entry;
char vid[8], pid[8];
int device_count = 0;
dir = opendir("/dev");
if (dir == NULL) {
perror("Cannot open /dev directory");
return 0;
}
while ((entry = readdir(dir)) != NULL && device_count < max_devices) {
// 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_PID) == 0 && strcmp(pid, TRUERNGPROV2_HID) == 0) {
snprintf(devices[device_count].port_path, MAX_PATH, "/dev/%s", entry->d_name);
strcpy(devices[device_count].device_name, "TrueRNGproV2");
devices[device_count].type = DEVICE_TRUERNGPROV2;
strcpy(devices[device_count].vid, vid);
strcpy(devices[device_count].pid, pid);
device_count++;
continue;
}
// Check for TrueRNGpro
if (strcmp(vid, TRUERNGPRO_PID) == 0 && strcmp(pid, TRUERNGPRO_HID) == 0) {
snprintf(devices[device_count].port_path, MAX_PATH, "/dev/%s", entry->d_name);
strcpy(devices[device_count].device_name, "TrueRNGpro");
devices[device_count].type = DEVICE_TRUERNGPRO;
strcpy(devices[device_count].vid, vid);
strcpy(devices[device_count].pid, pid);
device_count++;
continue;
}
// Check for TrueRNG
if (strcmp(vid, TRUERNG_PID) == 0 && strcmp(pid, TRUERNG_HID) == 0) {
snprintf(devices[device_count].port_path, MAX_PATH, "/dev/%s", entry->d_name);
strcpy(devices[device_count].device_name, "TrueRNG");
devices[device_count].type = DEVICE_TRUERNG;
strcpy(devices[device_count].vid, vid);
strcpy(devices[device_count].pid, pid);
device_count++;
continue;
}
}
}
}
closedir(dir);
return device_count;
}
// List all available devices
void list_devices(struct DeviceInfo devices[], int device_count) {
if (device_count == 0) {
printf("No TrueRNG devices found.\n");
return;
}
printf("Available TrueRNG devices:\n");
for (int i = 0; i < device_count; i++) {
printf(" %d. %s at %s (VID:%s PID:%s)\n",
i + 1,
devices[i].device_name,
devices[i].port_path,
devices[i].vid,
devices[i].pid);
}
}
// Select device from list based on selector string
// Returns: 0 on success, -1 on error
int select_device_from_list(struct DeviceInfo devices[], int device_count, const char* selector, char* selected_port) {
if (device_count == 0) {
return -1;
}
// If no selector, use first device
if (!selector) {
strcpy(selected_port, devices[0].port_path);
return 0;
}
// Try to parse as device index (1-based)
char* endptr;
long index = strtol(selector, &endptr, 10);
if (*endptr == '\0' && index >= 1 && index <= device_count) {
strcpy(selected_port, devices[index - 1].port_path);
return 0;
}
// Try to match by port path
if (strncmp(selector, "/dev/", 5) == 0) {
for (int i = 0; i < device_count; i++) {
if (strcmp(devices[i].port_path, selector) == 0) {
strcpy(selected_port, devices[i].port_path);
return 0;
}
}
}
// Try to match by device type
for (int i = 0; i < device_count; i++) {
if ((strcasecmp(selector, "prov2") == 0 && devices[i].type == DEVICE_TRUERNGPROV2) ||
(strcasecmp(selector, "pro") == 0 && devices[i].type == DEVICE_TRUERNGPRO) ||
(strcasecmp(selector, "truerng") == 0 && devices[i].type == DEVICE_TRUERNG)) {
strcpy(selected_port, devices[i].port_path);
return 0;
}
}
return -1; // No match found
}
// Read USB device info from sysfs
int read_usb_device_info(const char* port_name, char* vid, char* pid) {
char path[MAX_PATH];
@ -471,6 +633,33 @@ int main(int argc, char *argv[]) {
return 0;
}
// Initialize lookup table
init_ones_lookup_table();
// Find all available devices
struct DeviceInfo devices[MAX_PORTS];
int device_count = find_all_truerng_devices(devices, MAX_PORTS);
// Handle device listing
if (options.list_devices) {
list_devices(devices, device_count);
return 0;
}
// Check if any devices were found
if (device_count == 0) {
fprintf(stderr, "No TrueRNG devices found\n");
return 1;
}
// Select device based on options
char port_path[MAX_PATH];
if (select_device_from_list(devices, device_count, options.device_selector, port_path) != 0) {
fprintf(stderr, "Error: Could not select device '%s'\n", options.device_selector);
fprintf(stderr, "Use --list to see available devices\n");
return 1;
}
// Determine mode: interactive (no args) vs piped (has args or actual piping)
int interactive_mode = (argc == 1);
int piped_mode = !interactive_mode || is_piped();
@ -481,6 +670,16 @@ int main(int argc, char *argv[]) {
options.format = HEX;
}
// Show multiple device warning if verbose or interactive
if (device_count > 1 && !options.quiet) {
if (interactive_mode || (piped_mode && options.verbose)) {
if (!options.device_selector) {
printf("Multiple TrueRNG devices found - using first available\n");
printf("(Use -d option to select specific device or --list to see all)\n");
}
}
}
// Print headers based on mode
if (interactive_mode && !options.quiet) {
// Interactive mode: always show headers unless quiet
@ -493,7 +692,20 @@ int main(int argc, char *argv[]) {
printf("==================================================\n");
}
char port_path[MAX_PATH];
// Find the selected device info for display
const char* device_name = "Unknown";
for (int i = 0; i < device_count; i++) {
if (strcmp(devices[i].port_path, port_path) == 0) {
device_name = devices[i].device_name;
break;
}
}
// Show device info
if ((interactive_mode && !options.quiet) || (piped_mode && options.verbose && !options.quiet)) {
printf("%s Found\n", device_name);
}
int serial_fd;
FILE *fp;
unsigned char buffer[BLOCKSIZE];
@ -503,15 +715,6 @@ int main(int argc, char *argv[]) {
struct timeval starttime, currenttime;
double elapsed_time, rate;
// Initialize lookup table
init_ones_lookup_table();
// Find TrueRNG device
if (!find_truerng_port(port_path, piped_mode, options.verbose)) {
fprintf(stderr, "TrueRNG Not Found\n");
return 1;
}
// Setup output
if (piped_mode && !options.output_file) {
fp = stdout;

1
test_combined.hex Normal file
View File

@ -0,0 +1 @@
b840d95473b9c6b70441dcfb8f99752e3d0873c70955cdaec370775d4ebdec4f447e2a5024b184dae5a691b3674d3d79a79c85032d4eb04f42e1e7c55b724cb43c813ced3fd6c44068a8d33dc39a66fc0c142e6720338e34b22334b7fa562efd3421b12f

2
test_size_suffix.hex Normal file
View File

@ -0,0 +1,2 @@
b5ad1a2e70f717484d7ea8a9ed773b616e9c01f426a142b8a9558365b673d6f7be913381320ccdd2944e857234dd12220a5c4672928c13fb77972b0ee02892d4f7984d94a23196d5aab92a9d7d6208387b828a47a045496476e34b54cbc0171374435d99489118a02c6435b1df6dee752014043cdfb9b36ced09404b1534ff1a4eb1e81c1b8dc36c96b311c206639dba2fc87596431494058d8a54f0b9280980cd8f60db61e9d4ccfed2348d92b91f7fa6e18b0bc3a6794bbd852c6d22eb51f1df7716450eac298eb6209db8ae39dc8ac57906ef42726fe2bcf2edbab9fc5c3e5ada2b53ec5b6ebaeaf7c113d851273fe5b3e5c4d9c492ec39af77f2876a258081a6d482bd70c5075916719c08cb76cace9a4d70be94de54e2ae9a49332acfb4eafe6257be2b583673e195041f9bdf6e3b1042e7c25882fc30fd4466e5b1f93c4d828bb3f74e4ece86948e65facfe233eaa2d52caae02d8824f0f18655e5723973b8b8db37bcde508d18ce91bdf9325418a53aa079e7a0b46752364b10f234281db5931464ad53d0f4f38d44c4108742a87ce570b6bc2703d7bd79b950290ebcb638f49bea08996e0bd9ca2c183c84fb29be0b2ee9d6b5ad61fe3c3e0765c01ea8c8ef93e20f55c2e845a9b96b40b1c5004a077c18d29d92287bdd07e9de08b5beb701dc07dc7b8b05838012aef8f7d34f6618d74e382cde984016784bb1ca97e7c825023d0ea222a7375e22fd0b3f415c6e3cf16559a980b051b2bd716a0cefd11e4ecefa6a8c71c50a08f262f2c875d4ba989703a3c8f4068f8ede04768e932514b1efbaeca5d32c291f2d90142a28e0b73c6f9bcbb8dc4cd45574e04116d21ee49a6f3b42ffe22ef34d14d99f85fe69d97023d8bb75e7ad443ac430988ccd26d69e511906cba9b935c66df1e1643b4c53295edb1d07c12c50c28b9580d84f837898ea2bb9d815cc7f0ad41471a7e0a01893cc1220c2a77e291073256ab318aa07428b907a9179eb6ec6bbe2912064b9502116b840a6e3413447b1ed2d4301e8422da451b9e39bc3ae0eddd821b523e0b42374754998ff9cd6f4d50535c22586374f332ffde03e6f926a6b2c6d10751fba86cd43f2bb4a9aea5af15b7e7bf51da92f2162ee0adee6f55b19f6d0230c7f15674f435b133fb52423dca9101e5c3994d9f547542bbbe35c49a6d658cac36ac5e731bdd27af3daf09884fd8006c961ff3e56fcbbc2d5d2f44b86eab7bd67d8dcdc10e397fc7032cd499282b6b0699277e92d553aecbca2bd39159af4a36d9e64a8934acbfaf532e1f1e4d338ab46296d0501cb1966fddbe845143dbc5a242adc1c14b258259e349bdce70b939f4dd6bc6d5923a0a9a305765d13a0b65181af280ee847ad6e5e2f4c08e63e775bfc45a09e0c08e9f97c0cb05403b3bea181f78989cc0f8b815ce86d651d68aace01
aa0501e41fead7cb6474caf23187fa5050d0ba5452d6056d20c462eed32dff6002816490d6a2b3983e224ae6942ad00ac01c1f2a0905eee0cd748f11e17c7ec53d6c486b2f0ae1dab65a1d006e4ff3baaf422e330a05273dbf4c933141ab85dc726e5e60b8fd452a4310f0972e7b9a8fd027112db21f35f220edd18a6e469454bab230dbb0543f8220c2b96cbe1ab2e14865a9ad7347d42e203870e30166a1377350888f2f146374bb5386cbcacb8753aff7c66f39ed93d221ac6a7037ec91db588e860450d07fc0c09ee0e37ffdc8312ff262b214833ce7183e2dabb8667c597fd38652931d69fc99ff6149e854f44ace4f06bea49335d9c2254b7b2a11d8f486d542330bb378a113a3a9856b115549ac7bcfc8513dfd6c0ab2a6d6931e56c8df982a5a10f80481ca02639d2bddcb084cc1ee62f8ecbf1c00f660d56a8fc4564d8992f33d6b14f1bd4bb85b35cee96da6e12ecf9d979a632f34e8effc70ae0b211f00a6f5d4c534275567a24256d68c3f56f4a5b06b2d9357e6b86ecfc30bc3e9f62ee97bb9540376bf87eaf63a92539c217dc9ed700fbf0c258ed28b40a4f6ec184c34114947a9cfdd360a209f56d22adb9ef413fc09317f77c51703ca945e8e1ffb1833aeb2516ad4e5ad0990fe148da244dfd4cb1b5c4ea940cdd8e80c1e58d43ad5999aa1f067564428e083350ae3ed170b674bfec379cb59f8bc5e7ace524ab8c9809d6e746c6e2062dc854010d7d0e01aa6cba4e229bf9a4fce37b3ddddd38e12722e24e29b806fdde1b701edbbbe6270623aadb1c22ec67fc69a84225fc4f5b8885e8895b1717c8a232fe773333561513d429584770ea86728b3eec3ee780f08a8cc9357cb6da4a007bbba13f556f0efae6e408731bec18852c4b91568358bccaa5ed061b607e4d71d484185b0dc5ca114ff5ce4dd4b42c78436bba590614e82445dd10ed8ada9689e90afbeea6a84aac8a7c7673b8369b6d4536d6f0b715f32aa686ff6f6c4b02d4e4096954a10b686cdfc0233f68f080fb75dc4fe1679143dae10e466fc57af144944419c8b629a01510bff0cb6bd048849f78a43e024cc7596895ef59d36991adb5869a42f4fc4b9557984b8dc373b7e27b1175a16686dfef79d29e3e62e324bfdf797f8a0f8965cde8d18d8e1d62c945c0b98de92aa53e3b6e0eea212498840b2e3659c3d8652f4543f6e814df2cba04c32d524444b5cbc50be7849b324d45ce2fe9db8622f7c2f9117acd27fd7a63828657d313d93d9f0f21e57810957c1f34994eb32ee7a532b4f20f221b26247043bd4175b825f8014728c0a9f9ea799f101ab30a45d50ed977e8ce571cad37c5998f47e64964a41274d0fb202b8945eea54863bfcd1baed363921375687dc0ac83c5c157a0a2788f7fe204409292f6ee4e2ebeeb8b2ff62b332c12f018cc81fe4262198d2

BIN
truerng

Binary file not shown.