Added DEBUG_FIPS define to usbtrng-v2.0
authorTom Marble <tmarble@info9.net>
Sat, 21 Mar 2015 23:21:30 +0000 (18:21 -0500)
committerTom Marble <tmarble@info9.net>
Sat, 21 Mar 2015 23:21:30 +0000 (18:21 -0500)
- undef => firmware will emulate chaoskey
- defined => firmware will show command menu (to allow debugging)

Added -X option in ao-usbtrng to transition to bootloader

ao-tools/ao-usbtrng/ao-usbtrng.c
src/drivers/ao_trng.c
src/drivers/ao_trng.h
src/usbtrng-v2.0/ao_pins.h
src/usbtrng-v2.0/ao_usbtrng.c

index 3eb2b4d5faf0bdebaeb20fa780ec3bb20ddda802..b74bf33c34ccb2514d459d3ed06094fc28565d2d 100644 (file)
@@ -36,12 +36,14 @@ static const struct option options[] = {
        { .name = "tty", .has_arg = 1, .val = 'T' },
        { .name = "device", .has_arg = 1, .val = 'D' },
        { .name = "verbose", .has_arg = 0, .val = 'v' },
+       { .name = "reset", .has_arg = 0, .val = 'R' },
+       { .name = "bootloader", .has_arg = 0, .val = 'X' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--verbose] [--device=<AltOS-device>] [-tty=<tty>] [<kbytes>]\n", program);
+       fprintf(stderr, "usage: %s [--verbose] [--reset] [--bootloader] [--device=<AltOS-device>] [-tty=<tty>] [<kbytes>]\n", program);
        exit(1);
 }
 
@@ -63,12 +65,13 @@ main (int argc, char **argv)
        char                    *tty = NULL;
        int                     verbose = 0;
        int                     reset = 0;
+       int                     bootloader = 0;
        int                     ret = 0;
        int                     kbytes = 0; /* 0 == continuous */
        int                     written;
        uint8_t                 bits[1024];
 
-       while ((c = getopt_long(argc, argv, "vRT:D:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "vRXT:D:", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -79,6 +82,9 @@ main (int argc, char **argv)
                case 'R':
                        reset++;
                        break;
+               case 'X':
+                       bootloader++;
+                       break;
                case 'v':
                        verbose++;
                        break;
@@ -108,7 +114,9 @@ main (int argc, char **argv)
        if (!cc)
                exit(1);
 
-        if (reset) {
+        if (bootloader) {
+               cc_usb_printf(cc, "X\n");
+        } else if (reset) {
                cc_usb_printf(cc, "R\n");
         } else {
                if (kbytes) {
index 18ee38ecc895ad2040639f286cf1a6e781b8c367..4d90d24f278a347a3b9e5fe6490d10cc16eda59f 100644 (file)
@@ -18,6 +18,7 @@
 #include <ao.h>
 #include <ao_adc_fast.h>
 #include <ao_crc.h>
+#include <ao_boot.h>
 #include <ao_trng.h>
 
 static struct ao_task ao_blink_green_task;
@@ -36,15 +37,6 @@ ao_blink_green(void)
        }
 }
 
-static void
-ao_blink_green_toggle(void)
-{
-       ao_blinking_green = !ao_blinking_green;
-       if (!ao_blinking_green)
-               ao_led_off(AO_LED_GREEN);
-       ao_wakeup(&ao_blinking_green);
-}
-
 static struct ao_task ao_blink_red_task;
 static uint8_t ao_failed = 0; /* 0 NOMINAL, 1 FAILED */
 static uint8_t ao_post = 0; /* 0 POST needed, 1 powered up */
@@ -62,6 +54,8 @@ ao_trng_failure()
        ao_wakeup(&ao_failed);
 }
 
+#ifdef DEBUG_FIPS
+
 static void
 ao_trng_fetch(void)
 {
@@ -124,26 +118,116 @@ ao_trng_fetch_cmd(void)
                ao_trng_fetch();
 }
 
+static void
+ao_trng_status(void)
+{
+       if (ao_failed)
+               printf("FAILED\n");
+       else
+               printf("NOMINAL\n");
+}
+
+void ao_trng_reset(void); /* forward declaration */
+
+static void
+ao_blink_green_toggle(void)
+{
+       ao_blinking_green = !ao_blinking_green;
+       if (!ao_blinking_green)
+               ao_led_off(AO_LED_GREEN);
+       ao_wakeup(&ao_blinking_green);
+}
+
+static const struct ao_cmds ao_trng_cmds[] = {
+       { ao_trng_fetch_cmd, "f <kbytes>\0Fetch a block of numbers" },
+       { ao_trng_reset, "R\0Reset" },
+       { ao_blink_green_toggle, "G\0Toggle green LED blinking" },
+       { ao_trng_status, "s\0Show status" },
+       { ao_trng_failure, "z\0Simulate failure" },
+       { 0, NULL },
+};
+
+#else
+
+static void
+ao_trng_send(void)
+{
+       static uint16_t *buffer[2];
+       int             usb_buf_id;
+       uint16_t        i;
+       uint16_t        *buf;
+       uint16_t        t;
+       uint32_t        *rnd = (uint32_t *) ao_adc_ring;
+
+       if (!buffer[0]) {
+               buffer[0] = ao_usb_alloc();
+               buffer[1] = ao_usb_alloc();
+               if (!buffer[0])
+                       return;
+       }
+
+       usb_buf_id = 0;
+
+       ao_crc_reset();
+
+       for (;;) {
+               ao_led_on(AO_LED_TRNG_ACTIVE);
+               t = ao_adc_get(AO_USB_IN_SIZE) >> 1;    /* one 16-bit value per output byte */
+               buf = buffer[usb_buf_id];
+               for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
+                       *buf++ = ao_crc_in_32_out_16(rnd[t]);
+                       t = (t + 1) & ((AO_ADC_RING_SIZE>>1) - 1);
+               }
+               ao_adc_ack(AO_USB_IN_SIZE);
+               ao_led_off(AO_LED_TRNG_ACTIVE);
+               ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
+               usb_buf_id = 1-usb_buf_id;
+       }
+}
+
+static struct ao_task ao_trng_send_task;
+
+static void
+ao_bootloader_cmd(void)
+{
+       for (;;) {
+               getchar(); /* any char will do */
+                /* give feedback we are going into bootloader mode */
+               ao_led_on(AO_LED_GREEN);
+               ao_delay(AO_MS_TO_TICKS(500));
+               ao_led_off(AO_LED_GREEN);
+               ao_delay(AO_MS_TO_TICKS(500));
+               ao_led_on(AO_LED_GREEN);
+               ao_delay(AO_MS_TO_TICKS(500));
+               ao_led_off(AO_LED_GREEN);
+               ao_delay(AO_MS_TO_TICKS(500));
+               ao_led_on(AO_LED_GREEN);
+               ao_delay(AO_MS_TO_TICKS(500));
+               ao_led_off(AO_LED_GREEN);
+                ao_boot_loader();
+       }
+}
+
+static struct ao_task ao_bootloader_cmd_task;
+
+#endif
+
+
 /* NOTE: the reset function also functions as the Power On Self Test */
 void
 ao_trng_reset(void)
 {
-       printf("Resetting...\n");
+       /* printf("Resetting...\n"); */
        ao_failed = 0;
        ao_led_off(AO_LED_RED);
        ao_wakeup(&ao_failed);
        /* get the first 1k bits and ensure there are no duplicates */
        /* FIXME ao_trng_fetch(); */
-       putchar('\n');
-       if (ao_failed) { /* show failure */
-               printf("FAILED self test\n");
-       } else { /* show success */
-               printf("PASS - operation NOMINAL\n");
-               /* this blocks! */
+        if (!ao_failed) {
                ao_led_on(AO_LED_GREEN);
                ao_delay(AO_MS_TO_TICKS(1000));
                ao_led_off(AO_LED_GREEN);
-       }
+        }
 }
 
 static void
@@ -163,28 +247,15 @@ ao_blink_red(void)
        }
 }
 
-static void
-ao_trng_status(void)
-{
-       if (ao_failed)
-               printf("FAILED\n");
-       else
-               printf("NOMINAL\n");
-}
-
-static const struct ao_cmds ao_trng_cmds[] = {
-       { ao_trng_fetch_cmd, "f <kbytes>\0Fetch a block of numbers" },
-       { ao_trng_reset, "R\0Reset" },
-       { ao_blink_green_toggle, "G\0Toggle green LED blinking" },
-       { ao_trng_status, "s\0Show status" },
-       { ao_trng_failure, "z\0Simulate failure" },
-       { 0, NULL },
-};
-
 void
 ao_trng_init(void)
 {
        ao_add_task(&ao_blink_red_task, ao_blink_red, "blink_red");
        ao_add_task(&ao_blink_green_task, ao_blink_green, "blink_green");
+#ifdef DEBUG_FIPS
        ao_cmd_register(ao_trng_cmds);
+#else
+       ao_add_task(&ao_bootloader_cmd_task, ao_bootloader_cmd, "bootloader_cmd");
+       ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");
+#endif
 }
index 78577428f5a6edcb5550b36f6d913ca1cdc99b34..0fc0e55aa08f3ca9c58981475c0a2bd074a77618 100644 (file)
 #ifndef _AO_TRNG_H_
 #define _AO_TRNG_H_
 
+/* if this is defined the command processor will enable
+   debugging FIPS 140-2 errors
+#define DEBUG_FIPS 1
+*/
+
 void
 ao_trng_init(void);
 
index 1997d2052a49d37ccde48087416c7c67a4fc2c75..46c4b5cf3fffa2509daa5b555622276f8a4166ce 100644 (file)
@@ -61,7 +61,6 @@
 #define AO_CRC_INIT    0xffffffff
 
 /* TRNG */
-#define AO_LED_TRNG_READ       AO_LED_RED
-#define AO_LED_TRNG_WRITE      AO_LED_GREEN
+#define AO_LED_TRNG_ACTIVE     AO_LED_GREEN
 
 #endif /* _AO_PINS_H_ */
index 42713b6ec842fb2023e9e4441527cdbdf6e5ed08..bcb317394ebd6482f2efb81ad4b917a22289e2ee 100644 (file)
@@ -30,9 +30,9 @@ void main(void)
        ao_dma_init();
        ao_adc_init();
        ao_crc_init();
-
+#ifdef DEBUG_FIPS
        ao_cmd_init();
-
+#endif
        ao_usb_init();
 
        ao_trng_init();