chiark / gitweb /
Use /dev/urandom (or corresponding file) by default in --both mode
[vbig.git] / vbig.cc
diff --git a/vbig.cc b/vbig.cc
index 4bad93029254361c95c03855124b30f8d9eabea4..d2cc1a2c8ce4b8b28ade90a2fb98321a8a0403e7 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' },
   { "flush", no_argument, 0, 'f' },
@@ -49,20 +54,22 @@ static void help(void) {
          "  vbig [--seed SEED] --verify|--create PATH [SIZE]\n"
          "\n"
          "Options:\n"
-         "  --seed, -s     Specify random seed\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"
-         "  --entire, -e   Write until full; read until EOF\n"
-         "  --help, -h     Display usage message\n"
-         "  --version, -V  Display version string\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"
+         "  --entire, -e      Write until full; read until EOF\n"
+         "  --help, -h        Display usage message\n"
+         "  --version, -V     Display version string\n");
 }
 
 // Possible modes of operation
 enum mode_type {
-  NONE,
   VERIFY,
-  CREATE
+  CREATE,
+  BOTH
 };
 
 // Report an error and exit
@@ -107,20 +114,30 @@ static void flushCache(FILE *fp) {
 #endif
 }
 
-static void execute(mode_type mode, bool entire, const char *show);
+static long long execute(mode_type mode, bool entire, const char *show);
 
-static const char *seed = "hexapodia as the key insight";
+static const char default_seed[] = "hexapodia as the key insight";
+static void *seed;
+static size_t seedlen;
+static const char *seedpath;
 static const char *path;
 static bool entireopt = false;
 static bool flush = false;
 static long long size;
 
 int main(int argc, char **argv) {
-  mode_type mode = NONE;
+  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; break;
+    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;
     case 'e': entireopt = true; break;
@@ -133,10 +150,10 @@ int main(int argc, char **argv) {
   }
   argc -= optind;
   argv += optind;
-  if(mode == NONE)
-    fatal(0, "must specify one of --verify or --create");
   if(argc > 2)
     fatal(0, "excess arguments");
+  if(argc == 1 && mode == BOTH)
+    entireopt = true;
   if(entireopt) {
     if(argc != 1)
       fatal(0, "with --entire, size should not be specified");
@@ -144,6 +161,34 @@ 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(mode == BOTH && !seed && !seedpath) {
+#ifdef HAVE_RANDOM_DEVICE
+    seedpath = RANDOM_DEVICE;
+#else
+    fatal(0, "no --seed or --seed-file specified in --both mode"
+         " and random device not supported on this system");
+#endif
+  }
+  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 = (void*)default_seed;
+    seedlen = sizeof(default_seed)-1;
+  }
   path = argv[0];
   if(argc > 1) {
     errno = 0;
@@ -170,12 +215,17 @@ int main(int argc, char **argv) {
     size = sb.st_size;
   }
   const char *show = entireopt ? (mode == CREATE ? "written" : "verified") : 0;
-  execute(mode, entireopt, show);
+  if(mode == BOTH) {
+    size = execute(CREATE, entireopt, 0);
+    execute(VERIFY, false, show);
+  } else {
+    execute(mode, entireopt, show);
+  }
   return 0;
 }
 
-static void execute(mode_type mode, bool entire, const char *show) {
-  Arcfour rng(seed, strlen(seed));
+static long long execute(mode_type mode, bool entire, const char *show) {
+  Arcfour rng((const char*)seed, seedlen);
   FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
   if(!fp)
     fatal(errno, "%s", path);
@@ -185,6 +235,9 @@ static void execute(mode_type mode, bool entire, const char *show) {
     setvbuf(fp, 0, _IONBF, 0);
   char generated[4096], input[4096];
   long long remain = size;
+  static const size_t rc4drop = 3072; // en.wikipedia.org/wiki/RC4#Security
+  assert(rc4drop <= sizeof(generated));
+  rng.stream(generated, rc4drop);
   while(remain > 0) {
     size_t bytesGenerated = (remain > (ssize_t)sizeof generated
                              ? sizeof generated
@@ -240,4 +293,5 @@ static void execute(mode_type mode, bool entire, const char *show) {
     if(ferror(stdout) || fflush(stdout))
       fatal(errno, "flush stdout");
   }
+  return done;
 }