Release 2.1.3.
[u/mdw/catacomb] / cfb-def.h
index 85d95db..357bb95 100644 (file)
--- a/cfb-def.h
+++ b/cfb-def.h
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: cfb-def.h,v 1.1 1999/12/10 23:16:39 mdw Exp $
+ * $Id: cfb-def.h,v 1.6 2004/04/17 09:58:37 mdw Exp $
  *
  * Definitions for ciphertext feedback mode
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: cfb-def.h,v $
- * Revision 1.1  1999/12/10 23:16:39  mdw
- * Split mode macros into interface and implementation.
- *
- */
-
 #ifndef CATACOMB_CFB_DEF_H
 #define CATACOMB_CFB_DEF_H
 
 #include <mLib/bits.h>
 #include <mLib/sub.h>
 
+#ifndef CATACOMB_ARENA_H
+#  include "arena.h"
+#endif
+
 #ifndef CATACOMB_BLKC_H
 #  include "blkc.h"
 #endif
 #  include "paranoia.h"
 #endif
 
+#ifndef CATACOMB_PARANOIA_H
+#  include "paranoia.h"
+#endif
+
 /*----- Macros ------------------------------------------------------------*/
 
 /* --- @CFB_DEF@ --- *
@@ -75,7 +75,7 @@
 /* --- @pre_cfbgetiv@ --- *                                            \
  *                                                                     \
  * Arguments:  @const pre_cfbctx *ctx@ = pointer to CFB context block  \
- *             @void *iv#@ = pointer to output data block              \
+ *             @void *iv@ = pointer to output data block               \
  *                                                                     \
  * Returns:    ---                                                     \
  *                                                                     \
@@ -88,8 +88,8 @@
 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv)                 \
 {                                                                      \
   octet *p = iv;                                                       \
-  int off = ctx->off;                                                  \
-  int rest = PRE##_BLKSZ - off;                                                \
+  unsigned off = ctx->off;                                             \
+  unsigned rest = PRE##_BLKSZ - off;                                   \
   memcpy(p, ctx->iv + off, rest);                                      \
   memcpy(p + rest, ctx->iv, off);                                      \
 }                                                                      \
@@ -106,11 +106,8 @@ void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv)                     \
                                                                        \
 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv)                 \
 {                                                                      \
-  uint32 niv[PRE##_BLKSZ / 4];                                         \
-  BLKC_LOAD(PRE, niv, iv);                                             \
-  pre##_eblk(&ctx->ctx, niv, niv);                                     \
-  BLKC_STORE(PRE, ctx->iv, niv);                                       \
-  ctx->off = 0;                                                                \
+  memcpy(ctx->iv, iv, PRE##_BLKSZ);                                    \
+  ctx->off = PRE##_BLKSZ;                                              \
 }                                                                      \
                                                                        \
 /* --- @pre_cfbbdry@ --- *                                             \
@@ -125,10 +122,12 @@ void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv)                    \
                                                                        \
 void pre##_cfbbdry(pre##_cfbctx *ctx)                                  \
 {                                                                      \
-  octet iv[PRE##_BLKSZ];                                               \
-  pre##_cfbgetiv(ctx, iv);                                             \
-  pre##_cfbsetiv(ctx, iv);                                             \
-  BURN(iv);                                                            \
+  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);                                                           \
 }                                                                      \
                                                                        \
 /* --- @pre_cfbsetkey@ --- *                                           \
@@ -144,6 +143,7 @@ void pre##_cfbbdry(pre##_cfbctx *ctx)                                       \
 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k)            \
 {                                                                      \
   ctx->ctx = *k;                                                       \
+  ctx->off = PRE##_BLKSZ;                                              \
 }                                                                      \
                                                                        \
 /* --- @pre_cfbinit@ --- *                                             \
@@ -166,7 +166,7 @@ void pre##_cfbinit(pre##_cfbctx *ctx,                                       \
                     const void *key, size_t sz,                        \
                     const void *iv)                                    \
 {                                                                      \
-  static octet zero[PRE##_BLKSZ] = { 0 };                              \
+  static const octet zero[PRE##_BLKSZ] = { 0 };                                \
   pre##_init(&ctx->ctx, key, sz);                                      \
   pre##_cfbsetiv(ctx, iv ? iv : zero);                                 \
 }                                                                      \
@@ -191,7 +191,7 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                            \
 {                                                                      \
   const octet *s = src;                                                        \
   octet *d = dest;                                                     \
-  int off = ctx->off;                                                  \
+  unsigned off = ctx->off;                                             \
                                                                        \
   /* --- Empty blocks are trivial --- */                               \
                                                                        \
@@ -207,7 +207,9 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                            \
                                                                        \
   while (off < PRE##_BLKSZ) {                                          \
     register octet x = *s++;                                           \
-    *d++ = ctx->iv[off++] ^= x;                                                \
+    ctx->iv[off] ^= x;                                                 \
+    if (d) *d++ = ctx->iv[off];                                                \
+    off++;                                                             \
     sz--;                                                              \
   }                                                                    \
                                                                        \
@@ -221,10 +223,14 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                          \
       pre##_eblk(&ctx->ctx, iv, iv);                                   \
       if (sz < PRE##_BLKSZ)                                            \
        break;                                                          \
-      BLKC_XLOAD(PRE, iv, s);                                          \
-      BLKC_STORE(PRE, d, iv);                                          \
-      s += PRE##_BLKSZ;                                                        \
-      d += PRE##_BLKSZ;                                                        \
+      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;                                                           \
@@ -237,7 +243,9 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                            \
   small:                                                               \
     do {                                                               \
       register octet x = *s++;                                         \
-      *d++ = ctx->iv[off++] ^= x;                                      \
+      ctx->iv[off] ^= x;                                               \
+      if (d) *d++ = ctx->iv[off];                                      \
+      off++;                                                           \
       sz--;                                                            \
     } while (sz);                                                      \
   }                                                                    \
@@ -248,7 +256,7 @@ void pre##_cfbencrypt(pre##_cfbctx *ctx,                            \
   return;                                                              \
 }                                                                      \
                                                                        \
-/* --- @pre_cfbencrypt@ --- *                                          \
+/* --- @pre_cfbdecrypt@ --- *                                          \
  *                                                                     \
  * Arguments:  @pre_cfbctx *ctx@ = pointer to CFB context block        \
  *             @const void *src@ = pointer to source data              \
@@ -268,7 +276,7 @@ void pre##_cfbdecrypt(pre##_cfbctx *ctx,                            \
 {                                                                      \
   const octet *s = src;                                                        \
   octet *d = dest;                                                     \
-  int off = ctx->off;                                                  \
+  unsigned off = ctx->off;                                             \
                                                                        \
   /* --- Empty blocks are trivial --- */                               \
                                                                        \
@@ -340,7 +348,7 @@ typedef struct gctx {                                                       \
                                                                        \
 static gcipher *ginit(const void *k, size_t sz)                                \
 {                                                                      \
-  gctx *g = CREATE(gctx);                                              \
+  gctx *g = S_CREATE(gctx);                                            \
   g->c.ops = &gops;                                                    \
   pre##_cfbinit(&g->k, k, sz, 0);                                      \
   return (&g->c);                                                      \
@@ -361,7 +369,8 @@ static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
 static void gdestroy(gcipher *c)                                       \
 {                                                                      \
   gctx *g = (gctx *)c;                                                 \
-  DESTROY(g);                                                          \
+  BURN(*g);                                                            \
+  S_DESTROY(g);                                                                \
 }                                                                      \
                                                                        \
 static void gsetiv(gcipher *c, const void *iv)                         \
@@ -377,12 +386,12 @@ static void gbdry(gcipher *c)                                             \
 }                                                                      \
                                                                        \
 static const gcipher_ops gops = {                                      \
-  &pre##_cfb.b,                                                                \
-   gencrypt, gdecrypt, gdestroy, gsetiv, gbdry                         \
+  &pre##_cfb,                                                          \
+  gencrypt, gdecrypt, gdestroy, gsetiv, gbdry                          \
 };                                                                     \
                                                                        \
 const gccipher pre##_cfb = {                                           \
-  { #pre "-cfb", PRE##_KEYSZ, PRE##_BLKSZ },                           \
+  #pre "-cfb", pre##_keysz, PRE##_BLKSZ,                               \
   ginit                                                                        \
 };                                                                     \
                                                                        \
@@ -458,22 +467,22 @@ int main(void)                                                            \
     if (memcmp(pt, text, sizeof(text)) == 0) {                         \
       done++;                                                          \
       if (sizeof(text) < 40 || done % 8 == 0)                          \
-        fputc('.', stdout);                                            \
+       fputc('.', stdout);                                             \
       if (done % 480 == 0)                                             \
-        fputs("\n\t", stdout);                                         \
+       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(", "); hexdump(text + sz, rest);                         \
+       fputc('\n', stdout);                                            \
       printf("\tciphertext     = "); hexdump(ct, sz);                  \
-        printf(", "); hexdump(ct + sz, rest);                          \
-        fputc('\n', stdout);                                           \
+       printf(", "); hexdump(ct + sz, rest);                           \
+       fputc('\n', stdout);                                            \
       printf("\trecovered text = "); hexdump(pt, sz);                  \
-        printf(", "); hexdump(pt + sz, rest);                          \
-        fputc('\n', stdout);                                           \
+       printf(", "); hexdump(pt + sz, rest);                           \
+       fputc('\n', stdout);                                            \
       fputc('\n', stdout);                                             \
     }                                                                  \
     if (sz < 63)                                                       \