Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / tlsprf.c
index d45eb43..04c4c25 100644 (file)
--- a/tlsprf.c
+++ b/tlsprf.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: tlsprf.c,v 1.2 2001/04/06 22:05:53 mdw Exp $
+ * $Id: tlsprf.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
  *
  * The TLS pseudo-random function
  *
  * (c) 2001 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: tlsprf.c,v $
- * Revision 1.2  2001/04/06 22:05:53  mdw
- * Change dummy names in grand objects so that they say what sort of thing
- * they are.
- *
- * Revision 1.1  2001/04/04 20:10:52  mdw
- * Add support for the TLS pseudo-random function.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <mLib/alloc.h>
 void tlsdx_init(tlsdx_ctx *c, gmac *m, const void *sd, size_t sdsz)
 {
   c->k = m;
-  c->hashsz = c->k->ops->c->hashsz;
+  c->hashsz = GM_CLASS(c->k)->hashsz;
   c->sd = sd; c->sdsz = sdsz;
 
-  c->i = c->k->ops->init(c->k);
-  c->i->ops->hash(c->i, sd, sdsz);
-  c->ai = c->i->ops->done(c->i, 0);
-  c->o = c->k->ops->init(c->k);
-  c->o->ops->hash(c->o, c->ai, c->hashsz);
-  c->o->ops->hash(c->o, sd, sdsz);
-  c->p = c->o->ops->done(c->o, 0);
+  c->i = GM_INIT(c->k);
+  GH_HASH(c->i, sd, sdsz);
+  c->ai = GH_DONE(c->i, 0);
+  c->o = GM_INIT(c->k);
+  GH_HASH(c->o, c->ai, c->hashsz);
+  GH_HASH(c->o, sd, sdsz);
+  c->p = GH_DONE(c->o, 0);
   c->sz = c->hashsz;
 }
 
@@ -111,16 +99,16 @@ void tlsdx_encrypt(tlsdx_ctx *c, const void *src, void *dest, size_t sz)
     if (c->sz)
       n = c->sz;
     else {
-      h = c->k->ops->init(c->k);
-      h->ops->hash(h, c->ai, c->hashsz);
-      c->ai = h->ops->done(h, 0);
-      c->i->ops->destroy(c->i);
+      h = GM_INIT(c->k);
+      GH_HASH(h, c->ai, c->hashsz);
+      c->ai = GH_DONE(h, 0);
+      GH_DESTROY(c->i);
       c->i = h;
-      c->o->ops->destroy(c->o);
-      h = c->o = c->k->ops->init(c->k);
-      h->ops->hash(h, c->ai, c->hashsz);
-      h->ops->hash(h, c->sd, c->sdsz);
-      c->p = h->ops->done(h, 0);
+      GH_DESTROY(c->o);
+      h = c->o = GM_INIT(c->k);
+      GH_HASH(h, c->ai, c->hashsz);
+      GH_HASH(h, c->sd, c->sdsz);
+      c->p = GH_DONE(h, 0);
       c->sz = n = c->hashsz;
     }
     if (n > sz)
@@ -151,8 +139,8 @@ void tlsdx_encrypt(tlsdx_ctx *c, const void *src, void *dest, size_t sz)
 
 void tlsdx_free(tlsdx_ctx *c)
 {
-  c->i->ops->destroy(c->i);
-  c->o->ops->destroy(c->o);
+  GH_DESTROY(c->i);
+  GH_DESTROY(c->o);
 }
 
 /* --- Generic random number generator --- */
@@ -230,7 +218,7 @@ static int dx_grmisc(grand *r, unsigned op, ...)
     } break;
     default:
       GRAND_BADOP;
-      break;      
+      break;
   }
 
   va_end(ap);
@@ -284,7 +272,7 @@ grand *tlsdx_rand(const gcmac *mc, const void *k, size_t ksz,
 {
   dx_grctx *g = S_CREATE(dx_grctx);
   dstr d = DSTR_INIT;
-  gmac *m = mc->key(k, ksz);
+  gmac *m = GM_KEY(mc, k, ksz);
   octet *q = xmalloc(sdsz);
   memcpy(q, sd, sdsz);
   dstr_putf(&d, "tlsdx(%s)", mc->name);
@@ -435,7 +423,7 @@ static int prf_grmisc(grand *r, unsigned op, ...)
     } break;
     default:
       GRAND_BADOP;
-      break;      
+      break;
   }
 
   va_end(ap);
@@ -528,9 +516,9 @@ static int v_generate(dstr *v)
   if (memcmp(v[2].buf, d.buf, d.len) != 0) {
     ok = 0;
     printf("\nfail tlsprf:"
-          "\n\tkey        = ");
+          "\n\tkey        = ");
     type_hex.dump(&v[0], stdout);
-    printf("\n\tseed       = "); type_hex.dump(&v[1], stdout);
+    printf("\n\tseed      = "); type_hex.dump(&v[1], stdout);
     printf("\n\texpected   = "); type_hex.dump(&v[2], stdout);
     printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
     putchar('\n');