symm/...: Reformat encryption mode loops and related code.
[catacomb] / symm / counter-def.h
index 2744430..8ec798d 100644 (file)
@@ -84,9 +84,7 @@
  */                                                                    \
                                                                        \
 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv)         \
-{                                                                      \
-  BLKC_STORE(PRE, iv, ctx->n);                                         \
-}                                                                      \
+  { BLKC_STORE(PRE, iv, ctx->c); }                                     \
                                                                        \
 /* --- @pre_countersetiv@ --- *                                                \
  *                                                                     \
@@ -99,10 +97,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@ --- *                                         \
  *                                                                     \
@@ -115,10 +110,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@ --- *                                       \
  *                                                                     \
@@ -131,9 +123,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@ --- *                                         \
  *                                                                     \
@@ -156,6 +146,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);                             \
 }                                                                      \
@@ -183,68 +174,48 @@ void pre##_counterencrypt(pre##_counterctx *ctx,                  \
   const octet *s = src;                                                        \
   octet *d = dest;                                                     \
   unsigned off = ctx->off;                                             \
+  uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4];                           \
+  octet y;                                                             \
                                                                        \
   /* --- Empty blocks are trivial --- */                               \
                                                                        \
-  if (!sz)                                                             \
-    return;                                                            \
+  if (!sz) return;                                                     \
                                                                        \
   /* --- If I can deal with the block from my buffer, do that --- */   \
                                                                        \
-  if (sz < PRE##_BLKSZ - off)                                          \
-    goto small;                                                                \
+  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--;                                                            \
-    }                                                                  \
-  }                                                                    \
+  if (!d) sz -= PRE##_BLKSZ - off;                                     \
+  else while (off < PRE##_BLKSZ)                                       \
+    { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; }              \
                                                                        \
   /* --- 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;                                               \
+  for (;;) {                                                           \
+    pre##_eblk(&ctx->ctx, ctx->c, t);                                  \
+    BLKC_STEP(PRE, ctx->c);                                            \
+    if (sz < PRE##_BLKSZ) break;                                       \
+    if (!d) /* do nothing */;                                          \
+    else if (!s) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; }          \
+    else {                                                             \
+      BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ;                          \
+      BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ;                     \
     }                                                                  \
-                                                                       \
-    BLKC_STORE(PRE, ctx->buf, n);                                      \
-    off = 0;                                                           \
+    sz -= PRE##_BLKSZ;                                                 \
   }                                                                    \
                                                                        \
+  BLKC_STORE(PRE, ctx->b, t);                                          \
+  off = 0;                                                             \
+                                                                       \
   /* --- 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);                                                      \
+    if (!d) off += sz;                                                 \
+    else do { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; }      \
+    while (sz);                                                                \
   }                                                                    \
                                                                        \
   /* --- Done --- */                                                   \
@@ -271,29 +242,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,                                                      \
@@ -343,11 +301,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: {                                            \
@@ -391,10 +349,7 @@ 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 = {                                       \
   name "-counter",                                                     \