OreBolt-OS/modules/orebolt-noise/noise.c

56 lines
1.8 KiB
C
Executable File

/*
* noise.c -- USB Hardware TRNG Stream module
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* True random number generator. Generates true random 32-byte entropy
* blocks via getrandom(GRND_RANDOM) and pipes them to the host
* over CDC ACM serial (/dev/ttyGS0) at 100Hz.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/random.h>
#include <linux/input.h>
#include "h2_ui.h"
#include "log_manager.h"
int main(void) {
init_h2_graphics_runtime("USB TRNG HARDWARE PIPE");
LOG_INF("noise module started");
lv_obj_t *scr = lv_scr_act();
lv_obj_t *title = lv_label_create(scr);
lv_label_set_text(title, "USB HARDWARE TRNG STREAM");
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
lv_obj_set_style_text_color(title, COLOR_PRIMARY, LV_PART_MAIN);
lv_obj_t *status = lv_label_create(scr);
lv_label_set_text(status, "Piping true entropy to host CDC serial...\nNode: /dev/ttyGS0");
lv_obj_align(status, LV_ALIGN_CENTER, 0, 0);
int serial_fd = open("/dev/ttyGS0", O_WRONLY | O_NOCTTY);
if (serial_fd < 0) LOG_WRN("cannot open /dev/ttyGS0");
int input_fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK);
struct input_event ev;
uint8_t entropy_block[32];
while (1) {
lv_timer_handler();
if (getrandom(entropy_block, sizeof(entropy_block), GRND_RANDOM) == sizeof(entropy_block)) {
if (serial_fd != -1) (void)write(serial_fd, entropy_block, sizeof(entropy_block));
}
if (input_fd >= 0 && read(input_fd, &ev, sizeof(struct input_event)) > 0) {
if (ev.type == EV_KEY && ev.code == H2_KEY_BACK && ev.value == 1) break;
}
usleep(10000);
}
if (serial_fd != -1) close(serial_fd);
if (input_fd >= 0) close(input_fd);
LOG_INF("noise module exited");
return 0;
}