chiark / gitweb /
Support --seed-file (and --seed-length)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 21 Feb 2013 16:23:12 +0000 (16:23 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 21 Feb 2013 17:57:53 +0000 (17:57 +0000)
This allows us to read the seed from a file (which might be
/dev/*random or simply a file with key material).

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
vbig.1
vbig.cc

diff --git a/vbig.1 b/vbig.1
index be514251908488409289c2f831330e7b6ff516b8..9076d0f8cb0c5507059b94e7e9cf03b2dbffaf4b 100644 (file)
--- a/vbig.1
+++ b/vbig.1
@@ -35,12 +35,23 @@ kilobytes, megabytes or gigabytes.
 The size is mandatory when creating a file but optional when verifying
 it, unless \-\-entire is specified.  If \fBSIZE\fR not specified when
 writing-then-verifying, it is as if \fB\-\-entire\fR was specified.
+.PP
+A fixed default seed is used if no seed or seed file is specified.
 .SH OPTIONS
 .TP
 .B --seed\fR, \fB-s \fISEED
 Specifies the random seed to use.
 Optional.
 .TP
+.B --seed-file\fR, \fB-S \fISEED-FILE
+Specifies wehre the random seed should be read from.  The
+first \fISEED-LENGTH\fR bytes will be used, or the whole file
+if it is shorter.
+.TP
+.B --seed-length\fR, \fB-L \fISEED-LENGTH
+Specifies the (maximum) seed length in bytes.
+Optional; default is 2048.
+.TP
 .B --both\fR, \fB-b
 Selects both mode.
 \fIPATH\fR will be filled with \fISIZE\fR psuedo-random bytes and
diff --git a/vbig.cc b/vbig.cc
index f4f6217e5a28aee60d3bd8516c96f3ce84ad8a6f..5dcb42136e25000db7ba623d15b97a6ec2f76011 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
 #include <sys/stat.h>
 #include "Arcfour.h"
 
+#define DEFAULT_SEED_LENGTH 2048;
+
 // Command line options
 const struct option opts[] = {
   { "seed", required_argument, 0, 's' },
+  { "seed-file", required_argument, 0, 'S' },
+  { "seed-length", required_argument, 0, 'L' },
   { "both", no_argument, 0, 'b' },
   { "verify", no_argument, 0, 'v' },
   { "create", no_argument, 0, 'c' },
@@ -50,7 +54,9 @@ static void help(void) {
          "  vbig [--seed SEED] --verify|--create PATH [SIZE]\n"
          "\n"
          "Options:\n"
-         "  --seed, -s     Specify random seed\n"
+         "  --seed, -s     Specify random seed as string\n"
+         "  --seed-file, -S Read random seed from (start of) this file\n"
+         "  --seed-length, -L Set (maximum) seed length to read from file\n"
          "  --verify, -v   Verify that PATH contains the expected contents\n"
          "  --create, -c   Create PATH with psuedo-random contents\n"
          "  --flush, -f    Flush cache\n"
@@ -111,8 +117,9 @@ static void flushCache(FILE *fp) {
 static long long execute(mode_type mode, bool entire, const char *show);
 
 static const char default_seed[] = "hexapodia as the key insight";
-static const void *seed;
+static void *seed;
 static size_t seedlen;
+static const char *seedpath;
 static const char *path;
 static bool entireopt = false;
 static bool flush = false;
@@ -121,9 +128,15 @@ static long long size;
 int main(int argc, char **argv) {
   mode_type mode = BOTH;
   int n;
-  while((n = getopt_long(argc, argv, "+s:vcefhV", opts, 0)) >= 0) {
+  char *ep;
+  while((n = getopt_long(argc, argv, "+s:S:L:vcefhV", opts, 0)) >= 0) {
     switch(n) {
     case 's': seed = optarg; seedlen = strlen(optarg); break;
+    case 'S': seedpath = optarg; break;
+    case 'L':
+      seedlen = strtoul(optarg,&ep,0);
+      if(ep==optarg || *ep) fatal(0, "bad number for -S");
+      break;
     case 'b': mode = BOTH; break;
     case 'v': mode = VERIFY; break;
     case 'c': mode = CREATE; break;
@@ -148,8 +161,24 @@ int main(int argc, char **argv) {
     if(argc < (mode == VERIFY ? 1 : 2))
       fatal(0, "insufficient arguments");
   }
+  if(seed && seedpath)
+    fatal(0, "both --seed and --seed-file specified");
+  if(seedpath) {
+    if(!seedlen)
+      seedlen = DEFAULT_SEED_LENGTH;
+    FILE *seedfile = fopen(seedpath, "rb");
+    if(!seedfile)
+      fatal(errno, "%s", seedpath);
+    seed = malloc(seedlen);
+    if(!seed)
+      fatal(errno, "allocate seed");
+    seedlen = fread(seed, 1, seedlen, seedfile);
+    if(ferror(seedfile))
+      fatal(errno, "read %s", seedpath);
+    fclose(seedfile);
+  }
   if (!seed) {
-    seed = default_seed;
+    seed = (void*)default_seed;
     seedlen = sizeof(default_seed)-1;
   }
   path = argv[0];