Distribute md2-tab.h
[u/mdw/catacomb] / hmac-def.h
index 0972b68..19df631 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: hmac-def.h,v 1.2 2000/06/17 11:23:44 mdw Exp $
+ * $Id: hmac-def.h,v 1.5 2000/10/15 19:09:20 mdw Exp $
  *
  * Definitions for HMAC and NMAC
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: hmac-def.h,v $
+ * Revision 1.5  2000/10/15 19:09:20  mdw
+ * Support HMAC mode for hash functions which need to store more state than
+ * the hash output size.
+ *
+ * Revision 1.4  2000/07/15 10:00:58  mdw
+ * New generic hash operation for copying hash contexts.
+ *
+ * Revision 1.3  2000/07/02 18:27:42  mdw
+ * (ghash->ops->done): Interface change.  Passing in a null buffer pointer
+ * uses a buffer internal to the ghash object.  The operation returns the
+ * address of the buffer it used.  Clients of generic hashes no longer need
+ * to use dynamically allocated memory for hash results.
+ *
  * Revision 1.2  2000/06/17 11:23:44  mdw
  * Use secure arena for memory allocation.  Minor changes in the generic
  * hash interface.
@@ -80,7 +93,7 @@
                                                                        \
 /* --- Useful constants --- */                                         \
                                                                        \
-const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ };              \
+const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_STATESZ };             \
                                                                        \
 /* --- @pre_nmacinit@ --- *                                            \
  *                                                                     \
@@ -95,8 +108,8 @@ const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ };            \
                                                                        \
 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
 {                                                                      \
-  memcpy(key->ochain, ok, PRE##_HASHSZ);                               \
-  memcpy(key->ichain, ik, PRE##_HASHSZ);                               \
+  memcpy(key->ochain, ok, PRE##_STATESZ);                              \
+  memcpy(key->ichain, ik, PRE##_STATESZ);                              \
   key->ocount = key->icount = 0;                                       \
 }                                                                      \
                                                                        \
@@ -160,7 +173,7 @@ void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz)    \
                                                                        \
 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key)         \
 {                                                                      \
-  memcpy(ctx->chain, key->ochain, PRE##_HASHSZ);                       \
+  memcpy(ctx->chain, key->ochain, PRE##_STATESZ);                      \
   ctx->count = key->ocount;                                            \
   pre##_set(&ctx->ctx, key->ichain, key->icount);                      \
 }                                                                      \
@@ -212,6 +225,7 @@ typedef struct gkctx {                                                      \
 typedef struct gctx {                                                  \
   ghash h;                                                             \
   pre##_macctx c;                                                      \
+  octet buf[PRE##_HASHSZ];                                             \
 } gctx;                                                                        \
                                                                        \
 static ghash *gkinit(gmac *m)                                          \
@@ -237,10 +251,21 @@ static void ghhash(ghash *h, const void *p, size_t sz)                    \
   pre##_machash(&g->c, p, sz);                                         \
 }                                                                      \
                                                                        \
-static void ghdone(ghash *h, void *buf)                                        \
+static octet *ghdone(ghash *h, void *buf)                              \
 {                                                                      \
   gctx *g = (gctx *)h;                                                 \
+  if (!buf)                                                            \
+    buf = g->buf;                                                      \
   pre##_macdone(&g->c, buf);                                           \
+  return (buf);                                                                \
+}                                                                      \
+                                                                       \
+static ghash *ghcopy(ghash *h)                                         \
+{                                                                      \
+  gctx *g = (gctx *)h;                                                 \
+  gctx *gg = S_CREATE(gctx);                                           \
+  memcpy(gg, g, sizeof(gctx));                                         \
+  return (&gg->h);                                                     \
 }                                                                      \
                                                                        \
 static void ghdestroy(ghash *h)                                                \
@@ -268,7 +293,7 @@ const gcmac pre##_hmac =                                            \
 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy };      \
 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit };      \
 static const ghash_ops gops =                                          \
-  { &gch, ghhash, ghdone, ghdestroy };                                 \
+  { &gch, ghhash, ghdone, ghdestroy, ghcopy };                         \
                                                                        \
 HMAC_TEST(PRE, pre)