progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / cfb-def.h
index a76332d..303bc9f 100644 (file)
 #  include "paranoia.h"
 #endif
 
+#ifndef CATACOMB_RSVR_H
+#  include "rsvr.h"
+#endif
+
 /*----- Macros ------------------------------------------------------------*/
 
 /* --- @CFB_DEF@ --- *
@@ -90,8 +94,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 +110,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 = 0; }                   \
                                                                        \
 /* --- @pre_cfbbdry@ --- *                                             \
  *                                                                     \
@@ -122,12 +124,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);                                       \
-  ctx->off = PRE##_BLKSZ;                                              \
-  BURN(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 = 0;                                                                \
+  BURN(t);                                                             \
 }                                                                      \
                                                                        \
 /* --- @pre_cfbsetkey@ --- *                                           \
@@ -141,10 +144,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 = 0; }                                     \
                                                                        \
 /* --- @pre_cfbinit@ --- *                                             \
  *                                                                     \
@@ -163,10 +163,11 @@ void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k)               \
  */                                                                    \
                                                                        \
 void pre##_cfbinit(pre##_cfbctx *ctx,                                  \
-                    const void *key, size_t sz,                        \
-                    const void *iv)                                    \
+                  const void *key, size_t sz,                          \
+                  const void *iv)                                      \
 {                                                                      \
   static const octet zero[PRE##_BLKSZ] = { 0 };                                \
+                                                                       \
   pre##_init(&ctx->ctx, key, sz);                                      \
   pre##_cfbsetiv(ctx, iv ? iv : zero);                                 \
 }                                                                      \
@@ -185,75 +186,75 @@ void pre##_cfbinit(pre##_cfbctx *ctx,                                     \
  *             sensitive to block boundaries.                          \
  */                                                                    \
                                                                        \
+static const rsvr_policy pre##_cfbpolicy = { 0, PRE##_BLKSZ, PRE##_BLKSZ }; \
+                                                                       \
 void pre##_cfbencrypt(pre##_cfbctx *ctx,                               \
-                       const void *src, void *dest,                    \
-                       size_t sz)                                      \
+                     const void *src, void *dest,                      \
+                     size_t sz)                                        \
 {                                                                      \
+  rsvr_plan plan;                                                      \
   const octet *s = src;                                                        \
-  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 --- */                    \
-                                                                       \
-  while (off < PRE##_BLKSZ) {                                          \
-    register octet x = *s++;                                           \
-    ctx->iv[off] ^= x;                                                 \
-    if (d) *d++ = ctx->iv[off];                                                \
-    off++;                                                             \
-    sz--;                                                              \
+  octet *d = dest, *p;                                                 \
+  uint32 t[PRE##_BLKSZ/4];                                             \
+  octet y;                                                             \
+                                                                       \
+  /* Construct a plan and prepare to follow through. */                        \
+  rsvr_mkplan(&plan, &pre##_cfbpolicy, ctx->off, sz);                  \
+  BLKC_LOAD(PRE, t, ctx->b);                                           \
+                                                                       \
+  /* 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, t, t);                                     \
+      BLKC_STORE(PRE, ctx->b, t);                                      \
+    }                                                                  \
+    p = ctx->b + ctx->off; ctx->off += plan.head;                      \
+    if (s) while (plan.head--) { y = *s++ ^ *p; *p++ = y; if (d) *d++ = y; } \
+    else if (d) { memcpy(d, p, plan.head); d += plan.head; }           \
+    BLKC_LOAD(PRE, t, ctx->b);                                         \
   }                                                                    \
                                                                        \
-  /* --- 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;                                               \
+  /* If the buffer is all used, then reset it ready for next time. */  \
+  ctx->off -= plan.from_rsvr;                                          \
+                                                                       \
+  /* Handle multiple whole blocks. */                                  \
+  if (!d) {                                                            \
+    if (!s) while (plan.from_input) {                                  \
+      pre##_eblk(&ctx->ctx, t, t);                                     \
+      plan.from_input -= PRE##_BLKSZ;                                  \
+    } else while (plan.from_input) {                                   \
+      pre##_eblk(&ctx->ctx, t, t);                                     \
+      BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ;                         \
+      plan.from_input -= PRE##_BLKSZ;                                  \
+    }                                                                  \
+  } else {                                                             \
+    if (!s) while (plan.from_input) {                                  \
+      pre##_eblk(&ctx->ctx, t, t);                                     \
+      BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ;                         \
+      plan.from_input -= PRE##_BLKSZ;                                  \
+    } else while (plan.from_input) {                                   \
+      pre##_eblk(&ctx->ctx, t, t);                                     \
+      BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ;                         \
+      BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ;                         \
+      plan.from_input -= PRE##_BLKSZ;                                  \
     }                                                                  \
-    off = 0;                                                           \
-    BLKC_STORE(PRE, ctx->iv, iv);                                      \
   }                                                                    \
                                                                        \
-  /* --- 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--;                                                            \
-    } 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, t, t);                                       \
+    BLKC_STORE(PRE, ctx->b, t);                                                \
+    p = ctx->b; ctx->off += plan.tail;                                 \
+    if (s) while (plan.tail--) { y = *s++ ^ *p; *p++ = y; if (d) *d++ = y; } \
+    else if (d) { memcpy(d, p, plan.tail); d += plan.tail; }           \
   }                                                                    \
-                                                                       \
-  /* --- Done --- */                                                   \
-                                                                       \
-  ctx->off = off;                                                      \
-  return;                                                              \
 }                                                                      \
                                                                        \
 /* --- @pre_cfbdecrypt@ --- *                                          \
@@ -271,70 +272,58 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                          \
  */                                                                    \
                                                                        \
 void pre##_cfbdecrypt(pre##_cfbctx *ctx,                               \
-                       const void *src, void *dest,                    \
-                       size_t sz)                                      \
+                     const void *src, void *dest,                      \
+                     size_t sz)                                        \
 {                                                                      \
+  rsvr_plan plan;                                                      \
   const octet *s = src;                                                        \
-  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 --- */                    \
-                                                                       \
-  while (off < PRE##_BLKSZ) {                                          \
-    register octet x = *s++;                                           \
-    *d++ = ctx->iv[off] ^ x;                                           \
-    ctx->iv[off++] = x;                                                        \
-    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;                                               \
+  octet *d = dest, *p;                                                 \
+  uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4];                           \
+  octet y;                                                             \
+                                                                       \
+  /* Construct a plan and prepare to follow through. */                        \
+  rsvr_mkplan(&plan, &pre##_cfbpolicy, ctx->off, sz);                  \
+  BLKC_LOAD(PRE, t, ctx->b);                                           \
+                                                                       \
+  /* 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, t, t);                                     \
+      BLKC_STORE(PRE, ctx->b, t);                                      \
     }                                                                  \
-    off = 0;                                                           \
-    BLKC_STORE(PRE, ctx->iv, iv);                                      \
+    p = ctx->b + ctx->off;                                             \
+    ctx->off += plan.head;                                             \
+    while (plan.head--) { y = *s++; *d++ = y ^ *p; *p++ = y; }         \
+    BLKC_LOAD(PRE, t, ctx->b);                                         \
   }                                                                    \
                                                                        \
-  /* --- Tidying up the tail end --- */                                        \
+  /* If the buffer is all used, then reset it ready for next time. */  \
+  ctx->off -= plan.from_rsvr;                                          \
                                                                        \
-  if (sz) {                                                            \
-  small:                                                               \
-    do {                                                               \
-      register octet x = *s++;                                         \
-      *d++ = ctx->iv[off] ^ x;                                         \
-      ctx->iv[off++] = x;                                              \
-      sz--;                                                            \
-    } while (sz);                                                      \
+  /* Handle multiple whole blocks. */                                  \
+  while (plan.from_input) {                                            \
+    pre##_eblk(&ctx->ctx, t, t);                                       \
+    BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ;                            \
+    BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ;                       \
+    BLKC_MOVE(PRE, t, u);                                              \
+    plan.from_input -= PRE##_BLKSZ;                                    \
   }                                                                    \
                                                                        \
-  /* --- Done --- */                                                   \
-                                                                       \
-  ctx->off = off;                                                      \
-  return;                                                              \
+  /* 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, t, t);                                       \
+    BLKC_STORE(PRE, ctx->b, t);                                                \
+    p = ctx->b;                                                                \
+    ctx->off += plan.tail;                                             \
+    while (plan.tail--) { y = *s++; *d++ = y ^ *p; *p++ = y; }         \
+  }                                                                    \
 }                                                                      \
                                                                        \
 /* --- Generic cipher interface --- */                                 \
@@ -355,35 +344,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,                                                          \