[Cryptech-Commits] [core/platform/novena] 01/01: Add scaling factor to trng_extractor for easier bulk data extraction.

git at cryptech.is git at cryptech.is
Fri Apr 3 22:11:21 UTC 2015


This is an automated email from the git hooks/post-receive script.

paul at psgd.org pushed a commit to branch master
in repository core/platform/novena.

commit 891a24d969181f02762c031b9cfe0fd96c116634
Author: Paul Selkirk <paul at psgd.org>
Date:   Fri Apr 3 18:10:36 2015 -0400

    Add scaling factor to trng_extractor for easier bulk data extraction.
---
 eim/sw/trng_extractor_eim.c | 40 ++++++++++++++++++++++++++--------------
 i2c/sw/trng_extractor_i2c.c | 40 ++++++++++++++++++++++++++--------------
 2 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/eim/sw/trng_extractor_eim.c b/eim/sw/trng_extractor_eim.c
index e023c79..18fc1d6 100644
--- a/eim/sw/trng_extractor_eim.c
+++ b/eim/sw/trng_extractor_eim.c
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdint.h>
+#include <ctype.h>
 
 #include "tc_eim.h"
 #include "cryptech_memory_map.h"
@@ -47,8 +48,8 @@ char *usage =
 \n\
 -a      avalanche entropy\n\
 -r      rosc entropy\n\
--c      csprng\n\
--n      number of 4-byte samples (defaults to 10)\n\
+-c      csprng (default data source)\n\
+-n      number of 4-byte samples (scale with K, M, or G suffix)\n\
 -o      output file (defaults to stdout)\n\
 ";
 
@@ -73,10 +74,13 @@ static int extract(off_t status_addr, off_t data_addr, uint32_t *data)
 /* main */
 int main(int argc, char *argv[])
 {
-    int i, opt, num_words = 10;
-    uint32_t data;
-    off_t status_addr = 0, data_addr = 0;
+    int i, opt;
+    unsigned long num_words = 1;
+    char *endptr;
+    off_t status_addr = CSPRNG_ADDR_STATUS;
+    off_t data_addr = CSPRNG_ADDR_RANDOM;
     FILE *output = stdout;
+    uint32_t data;
 
     /* parse command line */
     while ((opt = getopt(argc, argv, "h?arcn:o:")) != -1) {
@@ -98,11 +102,23 @@ int main(int argc, char *argv[])
             data_addr = CSPRNG_ADDR_RANDOM;
             break;
         case 'n':
-            num_words = atoi(optarg);
-            if (num_words <= 0) {
-                fprintf(stderr, "-n requires a positive integer argument\n");
-                return EXIT_FAILURE;
-            }
+	    num_words = strtoul(optarg, &endptr, 10);
+	    switch (toupper(*endptr)) {
+	    case '\0':
+		break;
+	    case 'K':
+		num_words *= 1000;
+		break;
+	    case 'M':
+		num_words *= 1000000;
+		break;
+	    case 'G':
+		num_words *= 1000000000;
+		break;
+	    default:
+		fprintf(stderr, "unsupported -n suffix %s\n", endptr);
+		return EXIT_FAILURE;
+	    }
             break;
         case 'o':
             output = fopen(optarg, "wb+");
@@ -128,10 +144,6 @@ int main(int argc, char *argv[])
         fprintf(stderr, "\n");
         goto errout;
     }
-    if (status_addr == 0) {
-        fprintf(stderr, "%s: a data source must be specified\n", argv[0]);
-        goto errout;
-    }
 
     /* set up EIM */
     if (eim_setup() != 0) {
diff --git a/i2c/sw/trng_extractor_i2c.c b/i2c/sw/trng_extractor_i2c.c
index 29f054c..7b5b3b2 100644
--- a/i2c/sw/trng_extractor_i2c.c
+++ b/i2c/sw/trng_extractor_i2c.c
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdint.h>
+#include <ctype.h>
 
 #include "tc_i2c.h"
 #include "cryptech_memory_map.h"
@@ -47,8 +48,8 @@ char *usage =
 \n\
 -a      avalanche entropy\n\
 -r      rosc entropy\n\
--c      csprng\n\
--n      number of 4-byte samples (defaults to 10)\n\
+-c      csprng (default data source)\n\
+-n      number of 4-byte samples (scale with K, M, or G suffix)\n\
 -o      output file (defaults to stdout)\n\
 ";
 
@@ -73,10 +74,13 @@ static int extract(off_t status_addr, off_t data_addr, uint32_t *data)
 /* main */
 int main(int argc, char *argv[])
 {
-    int i, opt, num_words = 10;
-    uint32_t data;
-    off_t status_addr = 0, data_addr = 0;
+    int i, opt;
+    unsigned long num_words = 1;
+    char *endptr;
+    off_t status_addr = CSPRNG_ADDR_STATUS;
+    off_t data_addr = CSPRNG_ADDR_RANDOM;
     FILE *output = stdout;
+    uint32_t data;
     char *dev = I2C_dev;
     int addr = I2C_addr;
 
@@ -100,11 +104,23 @@ int main(int argc, char *argv[])
             data_addr = CSPRNG_ADDR_RANDOM;
             break;
         case 'n':
-            num_words = atoi(optarg);
-            if (num_words <= 0) {
-                fprintf(stderr, "-n requires a positive integer argument\n");
-                return EXIT_FAILURE;
-            }
+	    num_words = strtoul(optarg, &endptr, 10);
+	    switch (toupper(*endptr)) {
+	    case '\0':
+		break;
+	    case 'K':
+		num_words *= 1000;
+		break;
+	    case 'M':
+		num_words *= 1000000;
+		break;
+	    case 'G':
+		num_words *= 1000000000;
+		break;
+	    default:
+		fprintf(stderr, "unsupported -n suffix %s\n", endptr);
+		return EXIT_FAILURE;
+	    }
             break;
         case 'o':
             output = fopen(optarg, "wb+");
@@ -140,10 +156,6 @@ int main(int argc, char *argv[])
         fprintf(stderr, "\n");
         goto errout;
     }
-    if (status_addr == 0) {
-        fprintf(stderr, "%s: a data source must be specified\n", argv[0]);
-        goto errout;
-    }
 
     /* set up I2C */
     if (i2c_open(dev, addr) != 0) {



More information about the Commits mailing list