CryptoDit/entropy.c
2026-01-31 16:45:53 +01:00

89 lines
2.1 KiB
C

#include "entropy.h"
#include "common.h"
int handle_event(void *ctx, void *data, size_t data_sz){
struct random *s_random = (struct random*)data;
pid_t pid = s_random->pid;
char procname[PROCNAME_SIZE];
char pathname[64];
FILE *fd;
memset(procname, 0, PROCNAME_SIZE);
memset(pathname, 0, 64);
// Get the proc name
snprintf(pathname, 64, "/proc/%d/stat", pid);
if ((fd = fopen(pathname, "r")) == NULL){
printf("Failed to open the file\n");
return -1;
}
fscanf(fd, "%*d %s", procname);
printf("proc: %s; pid: %d\n", procname, s_random->pid);
fclose(fd);
return 0;
}
int entropy(char **argv, int argc){
const char *fileObj = "entropy_ebpf.o";
struct bpf_object *obj;
struct bpf_program *program;
struct ring_buffer *rb;
int err;
int fd_map_data;
int running = 1;
// Load eBPF
obj = bpf_object__open_file(fileObj, NULL);
if (!obj){
printf("Failed to open %s\n", fileObj);
return -1;
}
err = bpf_object__load(obj);
if (err){
printf("Failed to load object\n");
return -1;
}
//program = bpf_object__find_program_by_name(obj, "get_random");
program = bpf_object__find_program_by_name(obj, "get_random_user");
if (!program){
printf("Failed to find the program\n");
return -1;
}
fd_map_data = bpf_object__find_map_fd_by_name(obj, "data");
if (!fd_map_data){
printf("Failed to find the FD of the map\n");
return -1;
}
bpf_program__attach(program);
/* Start the ringbuffer */
rb = ring_buffer__new(fd_map_data, handle_event, NULL, NULL);
if (!rb){
printf("Failed to create the ringbuf\n");
bpf_object__close(obj);
return -1;
}
while(running){
err = ring_buffer__poll(rb, 100 /* timeout, ms */);
if (err == -EINTR){
printf("Failed to get the ringbuf\n");
running = 0;
break;
}
}
ring_buffer__free(rb);
bpf_object__close(obj);
// Generate RSA keys and check the entropy available
}