progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / counter-def.h
index 2f35323..3c949c7 100644 (file)
 #  include "paranoia.h"
 #endif
 
+#ifndef CATACOMB_RSVR_H
+#  include "rsvr.h"
+#endif
+
 /*----- Macros ------------------------------------------------------------*/
 
 /* --- @COUNTER_DEF@ --- *
@@ -65,7 +69,9 @@
  * Use:                Creates definitions for counter mode.
  */
 
-#define COUNTER_DEF(PRE, pre)                                          \
+#define COUNTER_DEF(PRE, pre) COUNTER_DEFX(PRE, pre, #pre, #pre)
+
+#define COUNTER_DEFX(PRE, pre, name, fname)                            \
                                                                        \
 /* --- @pre_countergetiv@ --- *                                                \
  *                                                                     \
@@ -82,9 +88,7 @@
  */                                                                    \
                                                                        \
 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv)         \
-{                                                                      \
-  BLKC_STORE(PRE, iv, ctx->n);                                         \
-}                                                                      \
+  { BLKC_STORE(PRE, iv, ctx->c); }                                     \
                                                                        \
 /* --- @pre_countersetiv@ --- *                                                \
  *                                                                     \
@@ -97,10 +101,7 @@ void pre##_countergetiv(const pre##_counterctx *ctx, void *iv)              \
  */                                                                    \
                                                                        \
 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv)         \
-{                                                                      \
-  BLKC_LOAD(PRE, ctx->n, iv);                                          \
-  ctx->off = PRE##_BLKSZ;                                              \
-}                                                                      \
+  { BLKC_LOAD(PRE, ctx->c, iv); ctx->off = PRE##_BLKSZ; }              \
                                                                        \
 /* --- @pre_counterbdry@ --- *                                         \
  *                                                                     \
@@ -113,10 +114,7 @@ void pre##_countersetiv(pre##_counterctx *ctx, const void *iv)             \
  */                                                                    \
                                                                        \
 void pre##_counterbdry(pre##_counterctx *ctx)                          \
-{                                                                      \
-  BLKC_STEP(PRE, ctx->n);                                              \
-  ctx->off = PRE##_BLKSZ;                                              \
-}                                                                      \
+  { BLKC_STEP(PRE, ctx->c); ctx->off = PRE##_BLKSZ; }                  \
                                                                        \
 /* --- @pre_countersetkey@ --- *                                       \
  *                                                                     \
@@ -129,9 +127,7 @@ void pre##_counterbdry(pre##_counterctx *ctx)                               \
  */                                                                    \
                                                                        \
 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k)    \
-{                                                                      \
-  ctx->ctx = *k;                                                       \
-}                                                                      \
+  { ctx->ctx = *k; }                                                   \
                                                                        \
 /* --- @pre_counterinit@ --- *                                         \
  *                                                                     \
@@ -154,6 +150,7 @@ void pre##_counterinit(pre##_counterctx *ctx,                               \
                       const void *iv)                                  \
 {                                                                      \
   static const octet zero[PRE##_BLKSZ] = { 0 };                                \
+                                                                       \
   pre##_init(&ctx->ctx, key, sz);                                      \
   pre##_countersetiv(ctx, iv ? iv : zero);                             \
 }                                                                      \
@@ -174,81 +171,66 @@ void pre##_counterinit(pre##_counterctx *ctx,                             \
  *             use the cipher as a random data generator.              \
  */                                                                    \
                                                                        \
+static const rsvr_policy pre##_counterpolicy =                         \
+  { 0, PRE##_BLKSZ, PRE##_BLKSZ };                                     \
+                                                                       \
 void pre##_counterencrypt(pre##_counterctx *ctx,                       \
                          const void *src, void *dest,                  \
                          size_t sz)                                    \
 {                                                                      \
-  const octet *s = src;                                                        \
+  rsvr_plan plan;                                                      \
+  const octet *s = src, *p;                                            \
   octet *d = dest;                                                     \
-  unsigned off = ctx->off;                                             \
-                                                                       \
-  /* --- Empty blocks are trivial --- */                               \
-                                                                       \
-  if (!sz)                                                             \
-    return;                                                            \
-                                                                       \
-  /* --- If I can deal with the block from my buffer, do that --- */   \
-                                                                       \
-  if (sz < PRE##_BLKSZ - off)                                          \
-    goto small;                                                                \
-                                                                       \
-  /* --- Finish off what's left in my buffer --- */                    \
-                                                                       \
-  if (!d)                                                              \
-    sz -= PRE##_BLKSZ - off;                                           \
-  else {                                                               \
-    while (off < PRE##_BLKSZ) {                                                \
-      register octet x = s ? *s++ : 0;                                 \
-      *d++ = ctx->buf[off++] ^ x;                                      \
-      sz--;                                                            \
+  uint32 t[PRE##_BLKSZ/4];                                             \
+                                                                       \
+  /* Construct a plan and prepare to follow through. */                        \
+  rsvr_mkplan(&plan, &pre##_counterpolicy, ctx->off, sz);              \
+                                                                       \
+  /* Initial portion, fulfilled from the buffer.  If the chunk is small        \
+   * enough, then this will be the only portion.  If the buffer is     \
+   * currently empty, then we must prepare it.                         \
+   */                                                                  \
+  if (plan.head) {                                                     \
+    if (!ctx->off) {                                                   \
+      pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);                \
+      BLKC_STORE(PRE, ctx->b, t);                                      \
     }                                                                  \
+    p = ctx->b + ctx->off; ctx->off += plan.head;                      \
+    if (!d) /* nothing to do */;                                       \
+    else if (!s) { memcpy(d, p, plan.head); d += plan.head; }          \
+    else while (plan.head--) *d++ = *s++ ^ *p++;                       \
   }                                                                    \
                                                                        \
-  /* --- Main encryption loop --- */                                   \
-                                                                       \
-  {                                                                    \
-    uint32 n[PRE##_BLKSZ / 4];                                         \
-                                                                       \
-    for (;;) {                                                         \
-      pre##_eblk(&ctx->ctx, ctx->n, n);                                        \
-      BLKC_STEP(PRE, ctx->n);                                          \
-      if (sz < PRE##_BLKSZ)                                            \
-       break;                                                          \
-      if (d) {                                                         \
-       if (!s)                                                         \
-         BLKC_STORE(PRE, d, n);                                        \
-       else {                                                          \
-         uint32 x[PRE##_BLKSZ / 4];                                    \
-         BLKC_LOAD(PRE, x, s);                                         \
-         BLKC_XSTORE(PRE, d, n, x);                                    \
-         s += PRE##_BLKSZ;                                             \
-       }                                                               \
-       d += PRE##_BLKSZ;                                               \
-      }                                                                        \
-      sz -= PRE##_BLKSZ;                                               \
-    }                                                                  \
+  /* If the buffer is all used, then reset it ready for next time. */  \
+  ctx->off -= plan.from_rsvr;                                          \
                                                                        \
-    BLKC_STORE(PRE, ctx->buf, n);                                      \
-    off = 0;                                                           \
+  /* Handle multiple whole blocks. */                                  \
+  if (!d)                                                              \
+    BLKC_ADD(PRE, ctx->c, plan.from_input/PRE##_BLKSZ);                        \
+  else if (!s) while (plan.from_input) {                               \
+    pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);          \
+    BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ;                           \
+    plan.from_input -= PRE##_BLKSZ;                                    \
+  } else while (plan.from_input) {                                     \
+    pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);          \
+    BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ;                           \
+    BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ;                           \
+    plan.from_input -= PRE##_BLKSZ;                                    \
   }                                                                    \
                                                                        \
-  /* --- Tidying up the tail end --- */                                        \
-                                                                       \
-  if (sz) {                                                            \
-  small:                                                               \
-    if (!d)                                                            \
-      off += sz;                                                       \
-    else do {                                                          \
-      register octet x = s ? *s++ : 0;                                 \
-      *d++ = ctx->buf[off++] ^ x;                                      \
-      sz--;                                                            \
-    } while (sz);                                                      \
+  /* Final portion.  Note that the buffer must be empty if there is a  \
+   * tail, since otherwise the input data would have been part of the  \
+   * head portion instad. */                                           \
+  if (!plan.tail)                                                      \
+    BLKC_STORE(PRE, ctx->b, t);                                                \
+  else {                                                               \
+    pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);          \
+    BLKC_STORE(PRE, ctx->b, t);                                                \
+    p = ctx->b; ctx->off += plan.tail;                                 \
+    if (!d) /* nothing to do */;                                       \
+    else if (!s) { memcpy(d, p, plan.tail); d += plan.tail; }          \
+    else while (plan.tail--) *d++ = *s++ ^ *p++;                       \
   }                                                                    \
-                                                                       \
-  /* --- Done --- */                                                   \
-                                                                       \
-  ctx->off = off;                                                      \
-  return;                                                              \
 }                                                                      \
                                                                        \
 /* --- Generic cipher interface --- */                                 \
@@ -269,29 +251,16 @@ static gcipher *ginit(const void *k, size_t sz)                           \
 }                                                                      \
                                                                        \
 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz)    \
-{                                                                      \
-  gctx *g = (gctx *)c;                                                 \
-  pre##_counterencrypt(&g->k, s, t, sz);                               \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_counterencrypt(&g->k, s, t, sz); }      \
                                                                        \
 static void gdestroy(gcipher *c)                                       \
-{                                                                      \
-  gctx *g = (gctx *)c;                                                 \
-  BURN(*g);                                                            \
-  S_DESTROY(g);                                                                \
-}                                                                      \
+  { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); }                     \
                                                                        \
 static void gsetiv(gcipher *c, const void *iv)                         \
-{                                                                      \
-  gctx *g = (gctx *)c;                                                 \
-  pre##_countersetiv(&g->k, iv);                                       \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_countersetiv(&g->k, iv); }              \
                                                                        \
 static void gbdry(gcipher *c)                                          \
-{                                                                      \
-  gctx *g = (gctx *)c;                                                 \
-  pre##_counterbdry(&g->k);                                            \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_counterbdry(&g->k); }                   \
                                                                        \
 static const gcipher_ops gops = {                                      \
   &pre##_counter,                                                      \
@@ -299,7 +268,7 @@ static const gcipher_ops gops = {                                   \
 };                                                                     \
                                                                        \
 const gccipher pre##_counter = {                                       \
-  #pre "-counter", pre##_keysz, PRE##_BLKSZ,                           \
+  name "-counter", pre##_keysz, PRE##_BLKSZ,                           \
   ginit                                                                        \
 };                                                                     \
                                                                        \
@@ -341,11 +310,11 @@ static int grmisc(grand *r, unsigned op, ...)                             \
       }                                                                        \
       break;                                                           \
     case GRAND_SEEDINT:                                                        \
-      BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned));                     \
+      BLKC_SET(PRE, g->k.c, va_arg(ap, unsigned));                     \
       g->k.off = PRE##_BLKSZ;                                          \
       break;                                                           \
     case GRAND_SEEDUINT32:                                             \
-      BLKC_SET(PRE, g->k.n, va_arg(ap, uint32));                       \
+      BLKC_SET(PRE, g->k.c, va_arg(ap, uint32));                       \
       g->k.off = PRE##_BLKSZ;                                          \
       break;                                                           \
     case GRAND_SEEDBLOCK: {                                            \
@@ -389,16 +358,13 @@ static uint32 grword(grand *r)                                            \
 }                                                                      \
                                                                        \
 static void grfill(grand *r, void *p, size_t sz)                       \
-{                                                                      \
-  grctx *g = (grctx *)r;                                               \
-  pre##_counterencrypt(&g->k, 0, p, sz);                               \
-}                                                                      \
+  { grctx *g = (grctx *)r; pre##_counterencrypt(&g->k, 0, p, sz); }    \
                                                                        \
 static const grand_ops grops = {                                       \
-  #pre "-counter",                                                     \
+  name "-counter",                                                     \
   GRAND_CRYPTO, 0,                                                     \
   grmisc, grdestroy,                                                   \
-  grword, grbyte, grword, grand_range, grfill                          \
+  grword, grbyte, grword, grand_defaultrange, grfill                   \
 };                                                                     \
                                                                        \
 /* --- @pre_counterrand@ --- *                                         \
@@ -420,15 +386,15 @@ grand *pre##_counterrand(const void *k, size_t sz)                        \
   return (&g->r);                                                      \
 }                                                                      \
                                                                        \
-COUNTER_TEST(PRE, pre)
+COUNTER_TESTX(PRE, pre, name, fname)
 
 /*----- Test rig ----------------------------------------------------------*/
 
-#ifdef TEST_RIG
+#define COUNTER_TEST(PRE, pre) COUNTER_TESTX(PRE, pre, #pre, #pre)
 
-#include <stdio.h>
+#ifdef TEST_RIG
 
-#include "daftstory.h"
+#include "modes-test.h"
 
 /* --- @COUNTER_TEST@ --- *
  *
@@ -437,91 +403,30 @@ COUNTER_TEST(PRE, pre)
  * Use:                Standard test rig for counter functions.
  */
 
-#define COUNTER_TEST(PRE, pre)                                         \
-                                                                       \
-/* --- Initial plaintext for the test --- */                           \
-                                                                       \
-static const octet text[] = TEXT;                                      \
-                                                                       \
-/* --- Key and IV to use --- */                                                \
+#define COUNTER_TESTX(PRE, pre, name, fname)                           \
                                                                        \
-static const octet key[] = KEY;                                                \
-static const octet iv[] = IV;                                          \
+static pre##_ctx key;                                                  \
+static pre##_counterctx ctx;                                           \
                                                                        \
-/* --- Buffers for encryption and decryption output --- */             \
+static void pre##_counter_test_setup(const octet *k, size_t ksz)       \
+  { pre##_init(&key, k, ksz); pre##_countersetkey(&ctx, &key); }       \
                                                                        \
-static octet ct[sizeof(text)];                                         \
-static octet pt[sizeof(text)];                                         \
+static void pre##_counter_test_reset(const octet *iv)                  \
+  { pre##_countersetiv(&ctx, iv); }                                    \
                                                                        \
-static void hexdump(const octet *p, size_t sz)                         \
-{                                                                      \
-  const octet *q = p + sz;                                             \
-  for (sz = 0; p < q; p++, sz++) {                                     \
-    printf("%02x", *p);                                                        \
-    if ((sz + 1) % PRE##_BLKSZ == 0)                                   \
-      putchar(':');                                                    \
-  }                                                                    \
-}                                                                      \
+static void pre##_counter_test_enc(const octet *s, octet *d, size_t sz)        \
+  { pre##_counterencrypt(&ctx, s, d, sz); }                            \
                                                                        \
-int main(void)                                                         \
+int main(int argc, char *argv[])                                       \
 {                                                                      \
-  size_t sz = 0, rest;                                                 \
-  pre##_counterctx ctx;                                                        \
-  int status = 0;                                                      \
-  int done = 0;                                                                \
-  pre##_ctx k;                                                         \
-                                                                       \
-  size_t keysz = PRE##_KEYSZ ?                                         \
-    PRE##_KEYSZ : strlen((const char *)key);                           \
-                                                                       \
-  fputs(#pre "-counter: ", stdout);                                    \
-                                                                       \
-  pre##_init(&k, key, keysz);                                          \
-  pre##_countersetkey(&ctx, &k);                                       \
-                                                                       \
-  while (sz <= sizeof(text)) {                                         \
-    rest = sizeof(text) - sz;                                          \
-    memcpy(ct, text, sizeof(text));                                    \
-    pre##_countersetiv(&ctx, iv);                                      \
-    pre##_counterencrypt(&ctx, ct, ct, sz);                            \
-    pre##_counterencrypt(&ctx, ct + sz, ct + sz, rest);                        \
-    memcpy(pt, ct, sizeof(text));                                      \
-    pre##_countersetiv(&ctx, iv);                                      \
-    pre##_counterencrypt(&ctx, pt, pt, rest);                          \
-    pre##_counterencrypt(&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);                        \
-       printf(", "); hexdump(text + sz, rest);                         \
-       fputc('\n', stdout);                                            \
-      printf("\tciphertext     = "); hexdump(ct, sz);                  \
-       printf(", "); hexdump(ct + sz, rest);                           \
-       fputc('\n', stdout);                                            \
-      printf("\trecovered text = "); hexdump(pt, sz);                  \
-       printf(", "); hexdump(pt + sz, rest);                           \
-       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 "-counter", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0,        \
+                     pre##_counter_test_setup, pre##_counter_test_reset, \
+                     pre##_counter_test_enc, pre##_counter_test_enc,   \
+                     argc, argv);                                      \
 }
 
 #else
-#  define COUNTER_TEST(PRE, pre)
+#  define COUNTER_TESTX(PRE, pre, name, fname)
 #endif
 
 /*----- That's all, folks -------------------------------------------------*/