Pollard's rho algorithm for computing discrete logs.
[u/mdw/catacomb] / hmac-def.h
index 3f96649..38dd64a 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: hmac-def.h,v 1.1 1999/12/10 23:16:40 mdw Exp $
+ * $Id: hmac-def.h,v 1.3 2000/07/02 18:27:42 mdw Exp $
  *
  * Definitions for HMAC and NMAC
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: hmac-def.h,v $
+ * 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.
+ *
  * Revision 1.1  1999/12/10 23:16:40  mdw
  * Split mode macros into interface and implementation.
  *
 
 /*----- Header files ------------------------------------------------------*/
 
+#include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <mLib/bits.h>
 #include <mLib/sub.h>
 
+#ifndef CATACOMB_ARENA_H
+#  include "arena.h"
+#endif
+
 #ifndef CATACOMB_GMAC_H
 #  include "gmac.h"
 #endif
 
 #define HMAC_DEF(PRE, pre)                                             \
                                                                        \
+/* --- Useful constants --- */                                         \
+                                                                       \
+const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ };              \
+                                                                       \
 /* --- @pre_nmacinit@ --- *                                            \
  *                                                                     \
  * Arguments:  @pre_macctx *key@ = pointer to a MAC key object         \
@@ -199,12 +218,13 @@ typedef struct gkctx {                                                    \
 typedef struct gctx {                                                  \
   ghash h;                                                             \
   pre##_macctx c;                                                      \
+  octet buf[PRE##_HASHSZ];                                             \
 } gctx;                                                                        \
                                                                        \
 static ghash *gkinit(gmac *m)                                          \
 {                                                                      \
   gkctx *gk = (gkctx *)m;                                              \
-  gctx *g = CREATE(gctx);                                              \
+  gctx *g = S_CREATE(gctx);                                            \
   g->h.ops = &gops;                                                    \
   pre##_macinit(&g->c, &gk->k);                                                \
   return (&g->h);                                                      \
@@ -212,7 +232,7 @@ static ghash *gkinit(gmac *m)                                               \
                                                                        \
 static gmac *gkey(const void *k, size_t sz)                            \
 {                                                                      \
-  gkctx *gk = CREATE(gkctx);                                           \
+  gkctx *gk = S_CREATE(gkctx);                                         \
   gk->m.ops = &gkops;                                                  \
   pre##_hmacinit(&gk->k, k, sz);                                       \
   return (&gk->m);                                                     \
@@ -224,28 +244,41 @@ 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 void ghdestroy(ghash *h)                                                \
 {                                                                      \
   gctx *g = (gctx *)h;                                                 \
-  DESTROY(g);                                                          \
+  BURN(*g);                                                            \
+  S_DESTROY(g);                                                                \
 }                                                                      \
                                                                        \
 static void gkdestroy(gmac *m)                                         \
 {                                                                      \
   gkctx *gk = (gkctx *)m;                                              \
-  DESTROY(gk);                                                         \
+  BURN(*gk);                                                           \
+  S_DESTROY(gk);                                                       \
+}                                                                      \
+                                                                       \
+static ghash *ghinit(void)                                             \
+{                                                                      \
+  assert(((void)"Attempt to instantiate an unkeyed MAC", 0));          \
+  return (0);                                                          \
 }                                                                      \
                                                                        \
-const gcmac pre##_hmac = { { #pre "-hmac", PRE##_HASHSZ }, gkey };     \
-static const gmac_ops gkops = { &pre##_hmac.b, gkinit, gkdestroy };    \
+const gcmac pre##_hmac =                                               \
+  { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey };                        \
+static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy };      \
+static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit };      \
 static const ghash_ops gops =                                          \
-  { &pre##_hmac.b, ghhash, ghdone, ghdestroy };                                \
+  { &gch, ghhash, ghdone, ghdestroy };                                 \
                                                                        \
 HMAC_TEST(PRE, pre)