Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions examples/htool_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../libhoth_usb.h"
#include "ec_util.h"
Expand Down Expand Up @@ -324,8 +325,21 @@ struct libhoth_device* htool_libhoth_usb_device(void) {
if (!ctx || !usb_dev) {
return NULL;
}
struct libhoth_usb_device_init_options opts = {.usb_device = usb_dev,
.usb_ctx = ctx};

struct timespec monotonic_time;
// `clock_gettime` function is guaranteed by POSIX standard on compliant
// systems. But it may have implementation defined resolution. So xor with PID
// as well
if (clock_gettime(CLOCK_MONOTONIC, &monotonic_time) != 0) {
fprintf(stderr, "Could not get clock time to generate PRNG seed\n");
return NULL;
}
uint32_t prng_seed =
monotonic_time.tv_sec ^ monotonic_time.tv_nsec ^ getpid();

struct libhoth_usb_device_init_options opts = {
.usb_device = usb_dev, .usb_ctx = ctx, .prng_seed = prng_seed};

int rv = libhoth_usb_open(&opts, &result);
if (rv) {
// TODO: Convert error-code to a string
Expand Down
3 changes: 2 additions & 1 deletion libhoth_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ int libhoth_usb_open(const struct libhoth_usb_device_init_options* options,
status = libhoth_usb_mailbox_open(usb_dev, config_descriptor);
break;
case LIBHOTH_USB_INTERFACE_TYPE_FIFO:
status = libhoth_usb_fifo_open(usb_dev, config_descriptor);
status =
libhoth_usb_fifo_open(usb_dev, config_descriptor, options->prng_seed);
break;
default:
status = LIBHOTH_ERR_INTERFACE_NOT_FOUND;
Expand Down
4 changes: 4 additions & 0 deletions libhoth_usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <libusb.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -30,6 +31,9 @@ struct libhoth_usb_device_init_options {
// The libusb context to use for operations. Can be NULL for the default
// context.
libusb_context* usb_ctx;
// Seed value to use for Pseudo-random number generator for communicating with
// RoT over USB FIFO interface
uint32_t prng_seed;
};

// Note that the options struct only needs to to live for the duration of
Expand Down
4 changes: 3 additions & 1 deletion libhoth_usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct libhoth_usb_fifo {
int all_transfers_completed;
bool in_transfer_completed;
bool out_transfer_completed;
uint32_t prng_state;
};

struct libhoth_usb_interface_info {
Expand All @@ -78,7 +79,8 @@ struct libhoth_usb_device {
};

int libhoth_usb_fifo_open(struct libhoth_usb_device *dev,
const struct libusb_config_descriptor *descriptor);
const struct libusb_config_descriptor *descriptor,
uint32_t prng_seed);
int libhoth_usb_fifo_send_request(struct libhoth_usb_device *dev,
const void *request, size_t request_size);
int libhoth_usb_fifo_receive_response(struct libhoth_usb_device *dev,
Expand Down
18 changes: 16 additions & 2 deletions libhoth_usb_fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,20 @@ static void fifo_transfer_callback(struct libusb_transfer *transfer) {
}
}

// 32-bits XOR shift algorithm from "Xorshift RNGs" by George Marsaglia
static uint32_t libhoth_generate_pseudorandom_u32(uint32_t *seed) {
*seed ^= (*seed << 13);
// The paper seems to have a typo in the algorithm presented on Pg 4, missing
// the ^ operation for second assignment. Pg 3 shows 8 shift operations that
// can serve as basis for xorshift. All of them have ^= operation.
*seed ^= (*seed >> 17);
*seed ^= (*seed << 5);
return *seed;
}

int libhoth_usb_fifo_open(struct libhoth_usb_device *dev,
const struct libusb_config_descriptor *descriptor) {
const struct libusb_config_descriptor *descriptor,
uint32_t prng_seed) {
int status = LIBHOTH_OK;
if (dev == NULL || descriptor == NULL ||
dev->info.type != LIBHOTH_USB_INTERFACE_TYPE_FIFO) {
Expand Down Expand Up @@ -143,6 +155,7 @@ int libhoth_usb_fifo_open(struct libhoth_usb_device *dev,
status = LIBHOTH_ERR_MALLOC_FAILED;
goto err_out;
}
drvdata->prng_state = prng_seed;
return LIBHOTH_OK;
err_out:
if (drvdata->in_buffer != NULL) free(drvdata->in_buffer);
Expand All @@ -167,7 +180,8 @@ int libhoth_usb_fifo_send_request(struct libhoth_usb_device *dev,
// Prepare the buffer with a request ID
struct libhoth_usb_fifo *drvdata = &dev->driver_data.fifo;
for (int i = 0; i < LIBHOTH_USB_FIFO_REQUEST_ID_SIZE; i++) {
drvdata->out_buffer[i] = (uint8_t)rand();
drvdata->out_buffer[i] =
(uint8_t)libhoth_generate_pseudorandom_u32(&drvdata->prng_state);
}

memcpy(drvdata->out_buffer + LIBHOTH_USB_FIFO_REQUEST_ID_SIZE, request,
Expand Down