symm/...: Reformat encryption mode loops and related code.
[catacomb] / symm / ofb-def.h
index 2aafacb..10f799c 100644 (file)
@@ -87,8 +87,9 @@ void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv)                        \
   octet *p = iv;                                                       \
   unsigned off = ctx->off;                                             \
   unsigned rest = PRE##_BLKSZ - off;                                   \
-  memcpy(p, ctx->iv + off, rest);                                      \
-  memcpy(p + rest, ctx->iv, off);                                      \
+                                                                       \
+  memcpy(p, ctx->b + off, rest);                                       \
+  memcpy(p + rest, ctx->b, off);                                       \
 }                                                                      \
                                                                        \
 /* --- @pre_ofbsetiv@ --- *                                            \
@@ -102,10 +103,7 @@ void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv)                     \
  */                                                                    \
                                                                        \
 void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv)                 \
-{                                                                      \
-  memcpy(ctx->iv, iv, PRE##_BLKSZ);                                    \
-  ctx->off = PRE##_BLKSZ;                                              \
-}                                                                      \
+  { memcpy(ctx->b, iv, PRE##_BLKSZ); ctx->off = PRE##_BLKSZ; }         \
                                                                        \
 /* --- @pre_ofbbdry@ --- *                                             \
  *                                                                     \
@@ -119,12 +117,13 @@ void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv)                    \
                                                                        \
 void pre##_ofbbdry(pre##_ofbctx *ctx)                                  \
 {                                                                      \
-  uint32 niv[PRE##_BLKSZ / 4];                                         \
-  BLKC_LOAD(PRE, niv, ctx->iv);                                                \
-  pre##_eblk(&ctx->ctx, niv, niv);                                     \
-  BLKC_STORE(PRE, ctx->iv, niv);                                       \
+  uint32 t[PRE##_BLKSZ/4];                                             \
+                                                                       \
+  BLKC_LOAD(PRE, t, ctx->b);                                           \
+  pre##_eblk(&ctx->ctx, t, t);                                         \
+  BLKC_STORE(PRE, ctx->b, t);                                          \
   ctx->off = PRE##_BLKSZ;                                              \
-  BURN(niv);                                                           \
+  BURN(t);                                                             \
 }                                                                      \
                                                                        \
 /* --- @pre_ofbsetkey@ --- *                                           \
@@ -138,9 +137,7 @@ void pre##_ofbbdry(pre##_ofbctx *ctx)                                       \
  */                                                                    \
                                                                        \
 void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k)            \
-{                                                                      \
-  ctx->ctx = *k;                                                       \
-}                                                                      \
+  { ctx->ctx = *k; }                                                   \
                                                                        \
 /* --- @pre_ofbinit@ --- *                                             \
  *                                                                     \
@@ -163,6 +160,7 @@ void pre##_ofbinit(pre##_ofbctx *ctx,                                       \
                     const void *iv)                                    \
 {                                                                      \
   static const octet zero[PRE##_BLKSZ] = { 0 };                                \
+                                                                       \
   pre##_init(&ctx->ctx, key, sz);                                      \
   pre##_ofbsetiv(ctx, iv ? iv : zero);                                 \
 }                                                                      \
@@ -190,68 +188,49 @@ void pre##_ofbencrypt(pre##_ofbctx *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->iv[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 iv[PRE##_BLKSZ / 4];                                                \
-    BLKC_LOAD(PRE, iv, ctx->iv);                                       \
-                                                                       \
-    for (;;) {                                                         \
-      pre##_eblk(&ctx->ctx, iv, iv);                                   \
-      if (sz < PRE##_BLKSZ)                                            \
-       break;                                                          \
-      if (d) {                                                         \
-       if (!s)                                                         \
-         BLKC_STORE(PRE, d, iv);                                       \
-       else {                                                          \
-         uint32 x[PRE##_BLKSZ / 4];                                    \
-         BLKC_LOAD(PRE, x, s);                                         \
-         BLKC_XSTORE(PRE, d, iv, x);                                   \
-         s += PRE##_BLKSZ;                                             \
-       }                                                               \
-       d += PRE##_BLKSZ;                                               \
-      }                                                                        \
-      sz -= PRE##_BLKSZ;                                               \
-    }                                                                  \
+  BLKC_LOAD(PRE, t, ctx->b);                                           \
                                                                        \
-    BLKC_STORE(PRE, ctx->iv, iv);                                      \
-    off = 0;                                                           \
+  for (;;) {                                                           \
+    pre##_eblk(&ctx->ctx, t, t);                                       \
+    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;                     \
+    }                                                                  \
+    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->iv[off++] ^ x;                                       \
-      sz--;                                                            \
-    } while (sz);                                                      \
+    if (!d) off += sz;                                                 \
+    else do { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; }      \
+    while (sz);                                                                \
   }                                                                    \
                                                                        \
   /* --- Done --- */                                                   \
@@ -278,23 +257,13 @@ 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##_ofbencrypt(&g->k, s, t, sz);                                   \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_ofbencrypt(&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##_ofbsetiv(&g->k, iv);                                           \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_ofbsetiv(&g->k, iv); }                  \
                                                                        \
 static void gbdry(gcipher *c)                                          \
 {                                                                      \
@@ -320,11 +289,7 @@ typedef struct grctx {                                                     \
 } grctx;                                                               \
                                                                        \
 static void grdestroy(grand *r)                                                \
-{                                                                      \
-  grctx *g = (grctx *)r;                                               \
-  BURN(*g);                                                            \
-  S_DESTROY(g);                                                                \
-}                                                                      \
+  { grctx *g = (grctx *)r; BURN(*g); S_DESTROY(g); }                   \
                                                                        \
 static int grmisc(grand *r, unsigned op, ...)                          \
 {                                                                      \
@@ -403,10 +368,7 @@ static uint32 grword(grand *r)                                             \
 }                                                                      \
                                                                        \
 static void grfill(grand *r, void *p, size_t sz)                       \
-{                                                                      \
-  grctx *g = (grctx *)r;                                               \
-  pre##_ofbencrypt(&g->k, 0, p, sz);                                   \
-}                                                                      \
+  { grctx *g = (grctx *)r; pre##_ofbencrypt(&g->k, 0, p, sz); }                \
                                                                        \
 static const grand_ops grops = {                                       \
   name "-ofb",                                                         \