symm/*-def.h: Fix layout bogosities.
[catacomb] / symm / ofb-def.h
index 358ee54..2aafacb 100644 (file)
@@ -65,7 +65,9 @@
  * Use:                Creates definitions for output feedback mode.
  */
 
-#define OFB_DEF(PRE, pre)                                              \
+#define OFB_DEF(PRE, pre) OFB_DEFX(PRE, pre, #pre, #pre)
+
+#define OFB_DEFX(PRE, pre, name, fname)                                        \
                                                                        \
 /* --- @pre_ofbgetiv@ --- *                                            \
  *                                                                     \
@@ -182,8 +184,8 @@ void pre##_ofbinit(pre##_ofbctx *ctx,                                       \
  */                                                                    \
                                                                        \
 void pre##_ofbencrypt(pre##_ofbctx *ctx,                               \
-                       const void *src, void *dest,                    \
-                       size_t sz)                                      \
+                     const void *src, void *dest,                      \
+                     size_t sz)                                        \
 {                                                                      \
   const octet *s = src;                                                        \
   octet *d = dest;                                                     \
@@ -306,7 +308,7 @@ static const gcipher_ops gops = {                                   \
 };                                                                     \
                                                                        \
 const gccipher pre##_ofb = {                                           \
-  #pre "-ofb", pre##_keysz, PRE##_BLKSZ,                               \
+  name "-ofb", pre##_keysz, PRE##_BLKSZ,                               \
   ginit                                                                        \
 };                                                                     \
                                                                        \
@@ -407,7 +409,7 @@ static void grfill(grand *r, void *p, size_t sz)                    \
 }                                                                      \
                                                                        \
 static const grand_ops grops = {                                       \
-  #pre "-ofb",                                                         \
+  name "-ofb",                                                         \
   GRAND_CRYPTO, 0,                                                     \
   grmisc, grdestroy,                                                   \
   grword, grbyte, grword, grand_defaultrange, grfill                   \
@@ -432,15 +434,15 @@ grand *pre##_ofbrand(const void *k, size_t sz)                            \
   return (&g->r);                                                      \
 }                                                                      \
                                                                        \
-OFB_TEST(PRE, pre)
+OFB_TESTX(PRE, pre, name, name)
 
 /*----- Test rig ----------------------------------------------------------*/
 
-#ifdef TEST_RIG
+#define OFB_TEST(PRE, pre) OFB_TESTX(PRE, pre, #pre, #pre)
 
-#include <stdio.h>
+#ifdef TEST_RIG
 
-#include "daftstory.h"
+#include "modes-test.h"
 
 /* --- @OFB_TEST@ --- *
  *
@@ -449,91 +451,30 @@ OFB_TEST(PRE, pre)
  * Use:                Standard test rig for OFB functions.
  */
 
-#define OFB_TEST(PRE, pre)                                             \
-                                                                       \
-/* --- Initial plaintext for the test --- */                           \
-                                                                       \
-static const octet text[] = TEXT;                                      \
-                                                                       \
-/* --- Key and IV to use --- */                                                \
+#define OFB_TESTX(PRE, pre, name, fname)                               \
                                                                        \
-static const octet key[] = KEY;                                                \
-static const octet iv[] = IV;                                          \
+static pre##_ctx key;                                                  \
+static pre##_ofbctx ctx;                                               \
                                                                        \
-/* --- Buffers for encryption and decryption output --- */             \
+static void pre##_ofb_test_setup(const octet *k, size_t ksz)           \
+  { pre##_init(&key, k, ksz); pre##_ofbsetkey(&ctx, &key); }           \
                                                                        \
-static octet ct[sizeof(text)];                                         \
-static octet pt[sizeof(text)];                                         \
+static void pre##_ofb_test_reset(const octet *iv)                      \
+  { pre##_ofbsetiv(&ctx, iv); }                                                \
                                                                        \
-static void hexdump(const octet *p, size_t sz, size_t off)             \
-{                                                                      \
-  const octet *q = p + sz;                                             \
-  for (sz = 0; p < q; p++, sz++) {                                     \
-    printf("%02x", *p);                                                        \
-    if ((off + sz + 1) % PRE##_BLKSZ == 0)                             \
-      putchar(':');                                                    \
-  }                                                                    \
-}                                                                      \
+static void pre##_ofb_test_enc(const octet *s, octet *d, size_t sz)    \
+  { pre##_ofbencrypt(&ctx, s, d, sz); }                                        \
                                                                        \
-int main(void)                                                         \
+int main(int argc, char *argv[])                                       \
 {                                                                      \
-  size_t sz = 0, rest;                                                 \
-  pre##_ofbctx ctx;                                                    \
-  int status = 0;                                                      \
-  int done = 0;                                                                \
-  pre##_ctx k;                                                         \
-                                                                       \
-  size_t keysz = PRE##_KEYSZ ?                                         \
-    PRE##_KEYSZ : strlen((const char *)key);                           \
-                                                                       \
-  fputs(#pre "-ofb: ", stdout);                                                \
-                                                                       \
-  pre##_init(&k, key, keysz);                                          \
-  pre##_ofbsetkey(&ctx, &k);                                           \
-                                                                       \
-  while (sz <= sizeof(text)) {                                         \
-    rest = sizeof(text) - sz;                                          \
-    memcpy(ct, text, sizeof(text));                                    \
-    pre##_ofbsetiv(&ctx, iv);                                          \
-    pre##_ofbencrypt(&ctx, ct, ct, sz);                                        \
-    pre##_ofbencrypt(&ctx, ct + sz, ct + sz, rest);                    \
-    memcpy(pt, ct, sizeof(text));                                      \
-    pre##_ofbsetiv(&ctx, iv);                                          \
-    pre##_ofbencrypt(&ctx, pt, pt, rest);                              \
-    pre##_ofbencrypt(&ctx, pt + rest, pt + rest, sz);                  \
-    if (memcmp(pt, text, sizeof(text)) == 0) {                         \
-      done++;                                                          \
-      if (sizeof(text) < 40 || done % 8 == 0)                          \
-       fputc('.', stdout);                                             \
-      if (done % 480 == 0)                                             \
-       fputs("\n\t", stdout);                                          \
-      fflush(stdout);                                                  \
-    } else {                                                           \
-      printf("\nError (sz = %lu)\n", (unsigned long)sz);               \
-      status = 1;                                                      \
-      printf("\tplaintext      = "); hexdump(text, sz, 0);             \
-       printf(", "); hexdump(text + sz, rest, sz);                     \
-       fputc('\n', stdout);                                            \
-      printf("\tciphertext     = "); hexdump(ct, sz, 0);               \
-       printf(", "); hexdump(ct + sz, rest, sz);                       \
-       fputc('\n', stdout);                                            \
-      printf("\trecovered text = "); hexdump(pt, sz, 0);               \
-       printf(", "); hexdump(pt + sz, rest, sz);                       \
-       fputc('\n', stdout);                                            \
-      fputc('\n', stdout);                                             \
-    }                                                                  \
-    if (sz < 63)                                                       \
-      sz++;                                                            \
-    else                                                               \
-      sz += 9;                                                         \
-  }                                                                    \
-                                                                       \
-  fputs(status ? " failed\n" : " ok\n", stdout);                       \
-  return (status);                                                     \
+  return test_encmode(fname "-ofb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0,    \
+                     pre##_ofb_test_setup, pre##_ofb_test_reset,       \
+                     pre##_ofb_test_enc, pre##_ofb_test_enc,           \
+                     argc, argv);                                      \
 }
 
 #else
-#  define OFB_TEST(PRE, pre)
+#  define OFB_TESTX(PRE, pre, name, fname)
 #endif
 
 /*----- That's all, folks -------------------------------------------------*/