symm/...: Reformat encryption mode loops and related code.
[catacomb] / symm / cfb-def.h
index 5432467..8040d94 100644 (file)
@@ -90,8 +90,9 @@ void pre##_cfbgetiv(const pre##_cfbctx *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_cfbsetiv@ --- *                                            \
@@ -105,10 +106,7 @@ void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv)                     \
  */                                                                    \
                                                                        \
 void pre##_cfbsetiv(pre##_cfbctx *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_cfbbdry@ --- *                                             \
  *                                                                     \
@@ -122,12 +120,13 @@ void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv)                    \
                                                                        \
 void pre##_cfbbdry(pre##_cfbctx *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_cfbsetkey@ --- *                                           \
@@ -141,10 +140,7 @@ void pre##_cfbbdry(pre##_cfbctx *ctx)                                      \
  */                                                                    \
                                                                        \
 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k)            \
-{                                                                      \
-  ctx->ctx = *k;                                                       \
-  ctx->off = PRE##_BLKSZ;                                              \
-}                                                                      \
+  { ctx->ctx = *k; ctx->off = PRE##_BLKSZ; }                           \
                                                                        \
 /* --- @pre_cfbinit@ --- *                                             \
  *                                                                     \
@@ -167,6 +163,7 @@ void pre##_cfbinit(pre##_cfbctx *ctx,                                       \
                   const void *iv)                                      \
 {                                                                      \
   static const octet zero[PRE##_BLKSZ] = { 0 };                                \
+                                                                       \
   pre##_init(&ctx->ctx, key, sz);                                      \
   pre##_cfbsetiv(ctx, iv ? iv : zero);                                 \
 }                                                                      \
@@ -192,61 +189,50 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                          \
   const octet *s = src;                                                        \
   octet *d = dest;                                                     \
   unsigned off = ctx->off;                                             \
+  uint32 t[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 --- */                    \
                                                                        \
   while (off < PRE##_BLKSZ) {                                          \
-    register octet x = *s++;                                           \
-    ctx->iv[off] ^= x;                                                 \
-    if (d) *d++ = ctx->iv[off];                                                \
-    off++;                                                             \
-    sz--;                                                              \
+    y = s ? *s++ : 0;                                                  \
+    ctx->b[off] ^= y;                                                  \
+    if (d) *d++ = ctx->b[off];                                         \
+    off++; 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 (s) {                                                         \
-       BLKC_XLOAD(PRE, iv, s);                                         \
-       s += PRE##_BLKSZ;                                               \
-      }                                                                        \
-      if (d) {                                                         \
-       BLKC_STORE(PRE, d, iv);                                         \
-       d += PRE##_BLKSZ;                                               \
-      }                                                                        \
-      sz -= PRE##_BLKSZ;                                               \
-    }                                                                  \
-    off = 0;                                                           \
-    BLKC_STORE(PRE, ctx->iv, iv);                                      \
+  BLKC_LOAD(PRE, t, ctx->b);                                           \
+                                                                       \
+  for (;;) {                                                           \
+    pre##_eblk(&ctx->ctx, t, t);                                       \
+    if (sz < PRE##_BLKSZ) break;                                       \
+    if (s) { BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ; }                        \
+    if (d) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; }                        \
+    sz -= PRE##_BLKSZ;                                                 \
   }                                                                    \
                                                                        \
+  BLKC_STORE(PRE, ctx->b, t);                                          \
+  off = 0;                                                             \
+                                                                       \
   /* --- Tidying up the tail end --- */                                        \
                                                                        \
   if (sz) {                                                            \
   small:                                                               \
     do {                                                               \
-      register octet x = *s++;                                         \
-      ctx->iv[off] ^= x;                                               \
-      if (d) *d++ = ctx->iv[off];                                      \
-      off++;                                                           \
-      sz--;                                                            \
+      y = s ? *s++ : 0;                                                        \
+      ctx->b[off] ^= y;                                                        \
+      if (d) *d++ = ctx->b[off];                                       \
+      off++; sz--;                                                     \
     } while (sz);                                                      \
   }                                                                    \
                                                                        \
@@ -277,58 +263,44 @@ void pre##_cfbdecrypt(pre##_cfbctx *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 --- */                    \
                                                                        \
-  while (off < PRE##_BLKSZ) {                                          \
-    register octet x = *s++;                                           \
-    *d++ = ctx->iv[off] ^ x;                                           \
-    ctx->iv[off++] = x;                                                        \
-    sz--;                                                              \
-  }                                                                    \
+  while (off < PRE##_BLKSZ)                                            \
+    { y = *s++; *d++ = ctx->b[off] ^ y; ctx->b[off++] = y; sz--; }     \
                                                                        \
   /* --- Main encryption loop --- */                                   \
                                                                        \
-  {                                                                    \
-    uint32 iv[PRE##_BLKSZ / 4];                                                \
-    BLKC_LOAD(PRE, iv, ctx->iv);                                       \
-                                                                       \
-    for (;;) {                                                         \
-      uint32 x[PRE##_BLKSZ / 4];                                       \
-      pre##_eblk(&ctx->ctx, iv, iv);                                   \
-      if (sz < PRE##_BLKSZ)                                            \
-       break;                                                          \
-      BLKC_LOAD(PRE, x, s);                                            \
-      BLKC_XSTORE(PRE, d, iv, x);                                      \
-      BLKC_MOVE(PRE, iv, x);                                           \
-      s += PRE##_BLKSZ;                                                        \
-      d += PRE##_BLKSZ;                                                        \
-      sz -= PRE##_BLKSZ;                                               \
-    }                                                                  \
-    off = 0;                                                           \
-    BLKC_STORE(PRE, ctx->iv, iv);                                      \
+  BLKC_LOAD(PRE, t, ctx->b);                                           \
+                                                                       \
+  for (;;) {                                                           \
+    pre##_eblk(&ctx->ctx, t, t);                                       \
+    if (sz < PRE##_BLKSZ) break;                                       \
+    BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ;                            \
+    BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ;                       \
+    BLKC_MOVE(PRE, t, u);                                              \
+    sz -= PRE##_BLKSZ;                                                 \
   }                                                                    \
                                                                        \
+  BLKC_STORE(PRE, ctx->b, t);                                          \
+  off = 0;                                                             \
+                                                                       \
   /* --- Tidying up the tail end --- */                                        \
                                                                        \
   if (sz) {                                                            \
   small:                                                               \
-    do {                                                               \
-      register octet x = *s++;                                         \
-      *d++ = ctx->iv[off] ^ x;                                         \
-      ctx->iv[off++] = x;                                              \
-      sz--;                                                            \
-    } while (sz);                                                      \
+    do { y = *s++; *d++ = ctx->b[off] ^ y; ctx->b[off++] = y; sz--; }  \
+    while (sz);                                                                \
   }                                                                    \
                                                                        \
   /* --- Done --- */                                                   \
@@ -355,35 +327,19 @@ 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##_cfbencrypt(&g->k, s, t, sz);                                   \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_cfbencrypt(&g->k, s, t, sz); }          \
                                                                        \
 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz)    \
-{                                                                      \
-  gctx *g = (gctx *)c;                                                 \
-  pre##_cfbdecrypt(&g->k, s, t, sz);                                   \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_cfbdecrypt(&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##_cfbsetiv(&g->k, iv);                                           \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_cfbsetiv(&g->k, iv); }                  \
                                                                        \
 static void gbdry(gcipher *c)                                          \
-{                                                                      \
-  gctx *g = (gctx *)c;                                                 \
-  pre##_cfbbdry(&g->k);                                                        \
-}                                                                      \
+  { gctx *g = (gctx *)c; pre##_cfbbdry(&g->k); }                       \
                                                                        \
 static const gcipher_ops gops = {                                      \
   &pre##_cfb,                                                          \