symm/modes-test.c: Test discarding output by changing encryption order.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 30 Oct 2018 10:24:29 +0000 (10:24 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 25 Nov 2018 11:38:04 +0000 (11:38 +0000)
Some of the code paths for discarding output are, or might become, quite
complicated, so they're worth exercising.

symm/modes-test.c
symm/modes-test.h

index bbda53b..b06d230 100644 (file)
@@ -303,7 +303,8 @@ static int step(size_t *sz_inout, size_t max)
  *
  *               * @enc@ is called to encrypt a source buffer @s@ and write
  *                 the ciphertext to a destination @d@; @sz@ is the common
- *                 size of these buffers.
+ *                 size of these buffers.  @d@ might be null, to discard
+ *                 output; @s@ might be null, to process all-zero input.
  *
  *               * @dec@ is called to decrypt a source buffer @s@ and write
  *                 the recovered plaintext to a destination @d@; @sz@ is the
@@ -451,15 +452,21 @@ int test_encmode(const char *name,
     sz2 = TEXTSZ - sz1 - sz0;
     ok = 1;
 
-    /* Encrypt the three fragments. */
+    /* Encrypt the last fragment first, to check discarding behaviour. */
+    if (sz2) {
+      reset(iv);
+      enc(text, 0, sz0);
+      enc(text + sz0, 0, sz1);
+      enc(text + sz0 + sz1, ct + sz0 + sz1, sz2);
+    }
+
+    /* Encrypt the first two fragments. */
     reset(iv);
     enc(text, ct, sz0);
     if (sz1) {
       memcpy(ct + sz0, text + sz0, sz1);
       enc(ct + sz0, ct + sz0, sz1);
     }
-    if (sz2)
-      enc(text + sz0 + sz1, ct + sz0 + sz1, sz2);
 
     /* Try to check consistency.  We can't do this if (a) the mode is
      * non-resumable and the fragments sizes are misaligned, or (b) this is
index a53341c..83d35dc 100644 (file)
@@ -105,7 +105,8 @@ typedef void encfn(const octet */*s*/, octet */*d*/, size_t /*sz*/);
  *
  *               * @enc@ is called to encrypt a source buffer @s@ and write
  *                 the ciphertext to a destination @d@; @sz@ is the common
- *                 size of these buffers.
+ *                 size of these buffers.  @d@ might be null, to discard
+ *                 output; @s@ might be null, to process all-zero input.
  *
  *               * @dec@ is called to decrypt a source buffer @s@ and write
  *                 the recovered plaintext to a destination @d@; @sz@ is the