progs/rspit.c: Better handling of block cipher IVs.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 22 Dec 2014 20:32:58 +0000 (20:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 22 Mar 2015 01:02:45 +0000 (01:02 +0000)
  * Check the IV length during option parsing, rather than at the end.

  * Don't accumulate IV material because we don't do that with keys.

progs/rspit.c

index 7ca1cce..2793fb4 100644 (file)
@@ -831,9 +831,14 @@ static grand *gen_ofb(unsigned i)
        break;
       case 'i': {
        char *p;
+       DRESET(&iv);
        unhex(optarg, &p, &iv);
        if (*p)
          die(EXIT_FAILURE, "bad hex IV `%s'", optarg);
+       if (iv.len != ciphertab[i].blksz) {
+         die(EXIT_FAILURE, "bad IV length %lu (must be %lu)",
+             (unsigned long)iv.len, (unsigned long)ciphertab[i].blksz);
+       }
       } break;
       default:
        return (0);
@@ -843,13 +848,8 @@ static grand *gen_ofb(unsigned i)
   if (!d.len)
     randkey(&d, ciphertab[i].keysz);
   r = ciphertab[i].ofb(d.buf, d.len);
-  if (iv.len) {
-    if (iv.len != ciphertab[i].blksz) {
-      die(EXIT_FAILURE, "bad IV length %lu (must be %lu)",
-         (unsigned long)iv.len, (unsigned long)ciphertab[i].blksz);
-    }
+  if (iv.len)
     r->ops->misc(r, GRAND_SEEDBLOCK, iv.buf);
-  }
 
   dstr_destroy(&d);
   dstr_destroy(&iv);
@@ -888,9 +888,14 @@ static grand *gen_counter(unsigned i)
        break;
       case 'i': {
        char *p;
+       DRESET(&iv);
        unhex(optarg, &p, &iv);
        if (*p)
          die(EXIT_FAILURE, "bad hex IV `%s'", optarg);
+       if (iv.len != ciphertab[i].blksz) {
+         die(EXIT_FAILURE, "bad IV length %lu (must be %lu)",
+             (unsigned long)iv.len, (unsigned long)ciphertab[i].blksz);
+       }
       } break;
       default:
        return (0);
@@ -900,13 +905,8 @@ static grand *gen_counter(unsigned i)
   if (!d.len)
     randkey(&d, ciphertab[i].keysz);
   r = ciphertab[i].counter(d.buf, d.len);
-  if (iv.len) {
-    if (iv.len != ciphertab[i].blksz) {
-      die(EXIT_FAILURE, "bad IV length %lu (must be %lu)",
-         (unsigned long)iv.len, (unsigned long)ciphertab[i].blksz);
-    }
+  if (iv.len)
     r->ops->misc(r, GRAND_SEEDBLOCK, iv.buf);
-  }
 
   dstr_destroy(&d);
   dstr_destroy(&iv);