finish infnoise_context & start work on listUSBDevices (to return a device list struct)
This commit is contained in:
@@ -225,18 +225,22 @@ int main(int argc, char **argv) {
|
||||
char *message = "no data?";
|
||||
bool errorFlag = false;
|
||||
if (opts.listDevices) {
|
||||
if (!listUSBDevices(&message)) {
|
||||
fputs(message, stderr);
|
||||
return 1;
|
||||
devlist_node *devlist = listUSBDevices(&message);
|
||||
devlist_node *curdev;
|
||||
uint8_t i=0;
|
||||
for (curdev = devlist; curdev != NULL;i++) {
|
||||
printf("Manufacturer: %s, Description: %s, Serial: %s\n", curdev->manufacturer, curdev.description,
|
||||
curdev.serial);
|
||||
curdev = curdev->next;
|
||||
}
|
||||
//fputs(message, stdout); // todo: put list of devices to &message and print here, not in libinfnoise
|
||||
//fputs(message, stdout); // TODO: iterate through infnoise_devlist and print stuff
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (opts.devRandom) {
|
||||
#ifdef LINUX
|
||||
inmWriteEntropyStart(BUFLEN / 8u, opts.debug); // todo: create method in libinfnoise.h for this?
|
||||
// also todo: check superUser in this mode (it will fail silently if not :-/)
|
||||
inmWriteEntropyStart(BUFLEN / 8u, opts.debug); // TODO: create method in libinfnoise.h for this?
|
||||
// also TODO: check superUser in this mode (it will fail silently if not :-/)
|
||||
#endif
|
||||
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
|
||||
int devRandomFD = open("/dev/random", O_WRONLY);
|
||||
@@ -288,9 +292,8 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!opts.noOutput) { // TODO: pass entropy, so we know how much to write to /dev/random. For testing, use 64 instead of 0.
|
||||
//(next step: use entropyBytes from context struct)
|
||||
outputBytes(result, totalBytesWritten - prevTotalBytesWritten, 0, opts.devRandom, &message);
|
||||
if (!opts.noOutput) {
|
||||
outputBytes(result, totalBytesWritten - prevTotalBytesWritten, context.entropyThisTime, opts.devRandom, &message);
|
||||
}
|
||||
|
||||
if (opts.debug && (1u << 20u)*(totalBytesWritten / (1u << 20u)) > (1u << 20u)*(prevTotalBytesWritten / (1u << 20u))) {
|
||||
|
||||
@@ -200,9 +200,8 @@ bool isSuperUser(void) {
|
||||
|
||||
// Return a list of all infinite noise multipliers found.
|
||||
|
||||
bool listUSBDevices(char** message) {
|
||||
devlist_node* listUSBDevices(char **message) {
|
||||
struct ftdi_context ftdic;
|
||||
|
||||
ftdi_init(&ftdic);
|
||||
|
||||
struct ftdi_device_list *devlist;
|
||||
@@ -210,7 +209,7 @@ bool listUSBDevices(char** message) {
|
||||
char manufacturer[128], description[128], serial[128];
|
||||
int i = 0;
|
||||
|
||||
// search devices
|
||||
// search devices
|
||||
int rc = ftdi_usb_find_all(&ftdic, &devlist, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID);
|
||||
|
||||
if (rc < 0) {
|
||||
@@ -221,24 +220,42 @@ bool listUSBDevices(char** message) {
|
||||
}
|
||||
}
|
||||
|
||||
devlist_node *return_list =NULL;
|
||||
devlist_node *current_entry =NULL;
|
||||
|
||||
for (curdev = devlist; curdev != NULL; i++) {
|
||||
//printf("Device: %d, ", i);
|
||||
if (return_list == NULL) {
|
||||
*return_list = (devlist_node) malloc(sizeof(struct infnoise_devlist_node));
|
||||
(*return_list)->id = i;
|
||||
(*return_list)->serial = serial;
|
||||
(*return_list)->manufacturer = manufacturer;
|
||||
(*return_list)->description = description;
|
||||
current_entry = return_list;
|
||||
} else {
|
||||
(*current_entry)->next = (devlist_node) malloc(sizeof(struct infnoise_devlist_node));
|
||||
current_entry = (*current_entry)->next;
|
||||
(*current_entry)->id = i;
|
||||
(*current_entry)->serial = serial;
|
||||
(*current_entry)->manufacturer = manufacturer;
|
||||
(*current_entry)->description = description;
|
||||
}
|
||||
|
||||
rc = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, serial, 128);
|
||||
if (rc < 0) {
|
||||
if (!isSuperUser()) {
|
||||
*message = "Can't find Infinite Noise Multiplier. Try running as super user?";
|
||||
return false;
|
||||
*message = "Can't find Infinite Noise Multiplier. Try running as super user?";
|
||||
return return_list;
|
||||
}
|
||||
//*message = "ftdi_usb_get_strings failed: %d (%s)\n", rc, ftdi_get_error_string(ftdic));
|
||||
return false;
|
||||
return return_list;
|
||||
}
|
||||
|
||||
// print to stdout
|
||||
printf("Manufacturer: %s, Description: %s, Serial: %s\n", manufacturer, description, serial);
|
||||
//printf("Manufacturer: %s, Description: %s, Serial: %s\n", manufacturer, description, serial);
|
||||
curdev = curdev->next;
|
||||
//current_node = current_node->next; // ???
|
||||
}
|
||||
|
||||
return true;
|
||||
return return_list;
|
||||
}
|
||||
|
||||
// Initialize the Infinite Noise Multiplier USB interface.
|
||||
@@ -345,12 +362,11 @@ uint32_t readRawData(struct infnoise_context *context, uint8_t *result) {
|
||||
uint32_t us = diffTime(&start, &end);
|
||||
if (us <= MAX_MICROSEC_FOR_SAMPLES) {
|
||||
uint8_t bytes[BUFLEN / 8u];
|
||||
uint32_t entropy = extractBytes(bytes, inBuf, &context->message, &context->errorFlag);
|
||||
context->entropyThisTime = extractBytes(bytes, inBuf, &context->message, &context->errorFlag);
|
||||
|
||||
// call health check and process bytes if OK
|
||||
if (inmHealthCheckOkToUseData() && inmEntropyOnTarget(entropy, BUFLEN)) {
|
||||
uint32_t byteswritten = processBytes(bytes, result, entropy, true, 0, &context->message, &context->errorFlag);
|
||||
return byteswritten;
|
||||
if (inmHealthCheckOkToUseData() && inmEntropyOnTarget(context->entropyThisTime, BUFLEN)) {
|
||||
return processBytes(bytes, result, context->entropyThisTime, true, 0, &context->message, &context->errorFlag);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -378,12 +394,11 @@ uint32_t readData(struct infnoise_context *context, uint8_t *result, uint32_t ou
|
||||
uint32_t us = diffTime(&start, &end);
|
||||
if (us <= MAX_MICROSEC_FOR_SAMPLES) {
|
||||
uint8_t bytes[BUFLEN / 8u];
|
||||
uint32_t entropy = extractBytes(bytes, inBuf, &context->message, &context->errorFlag);
|
||||
context->entropyThisTime = extractBytes(bytes, inBuf, &context->message, &context->errorFlag);
|
||||
|
||||
// call health check and process bytes if OK
|
||||
if (inmHealthCheckOkToUseData() && inmEntropyOnTarget(entropy, BUFLEN)) {
|
||||
uint32_t byteswritten = processBytes(bytes, result, entropy, false, outputMultiplier, &context->message, &context->errorFlag);
|
||||
return byteswritten;
|
||||
if (inmHealthCheckOkToUseData() && inmEntropyOnTarget(context->entropyThisTime, BUFLEN)) {
|
||||
return processBytes(bytes, result, context->entropyThisTime, false, outputMultiplier, &context->message, &context->errorFlag);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -392,6 +407,7 @@ uint32_t readData(struct infnoise_context *context, uint8_t *result, uint32_t ou
|
||||
|
||||
#ifdef LIB_EXAMPLE_PROGRAM
|
||||
// example use of libinfnoise - with keccak
|
||||
// TODO: rewrite!
|
||||
|
||||
int main() {
|
||||
char *serial = NULL; // use any device, can be set to a specific serial
|
||||
|
||||
@@ -15,16 +15,26 @@
|
||||
|
||||
struct infnoise_context {
|
||||
struct ftdi_context ftdic;
|
||||
uint32_t entropyBytes;
|
||||
uint32_t entropyThisTime;
|
||||
char *message;
|
||||
bool errorFlag;
|
||||
//uint8_t keccakState[KeccakPermutationSizeInBytes];
|
||||
} ;
|
||||
};
|
||||
|
||||
bool listUSBDevices(char** message);
|
||||
struct infnoise_devlist_node {
|
||||
uint8_t id;
|
||||
char *manufacturer;
|
||||
char *description;
|
||||
char *serial;
|
||||
struct infnoise_devlist_node *next;
|
||||
};
|
||||
|
||||
typedef struct infnoise_devlist_node* devlist_node;
|
||||
|
||||
devlist_node* listUSBDevices(char** message);
|
||||
|
||||
bool initInfnoise(struct infnoise_context *context, char *serial, bool keccak, bool debug);
|
||||
|
||||
uint32_t readRawData(struct infnoise_context *context, uint8_t *result);
|
||||
|
||||
uint32_t readData(struct infnoise_context *context, uint8_t *result, uint32_t outputMultiplier);
|
||||
uint32_t readData(struct infnoise_context *context, uint8_t *result, uint32_t outputMultiplier);
|
||||
|
||||
Reference in New Issue
Block a user