New hash interface requirements.
[u/mdw/catacomb] / sha.c
diff --git a/sha.c b/sha.c
index a5a3bf3..2fe1846 100644 (file)
--- a/sha.c
+++ b/sha.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sha.c,v 1.1 1999/09/03 08:41:12 mdw Exp $
+ * $Id: sha.c,v 1.2 1999/12/10 23:20:03 mdw Exp $
  *
  * Implementation of the SHA-1 hash function
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: sha.c,v $
+ * Revision 1.2  1999/12/10 23:20:03  mdw
+ * New hash interface requirements.
+ *
  * Revision 1.1  1999/09/03 08:41:12  mdw
  * Initial import.
  *
@@ -39,6 +42,8 @@
 
 #include <mLib/bits.h>
 
+#include "ghash.h"
+#include "ghash-def.h"
 #include "hash.h"
 #include "sha.h"
 
@@ -209,7 +214,7 @@ void sha_init(sha_ctx *ctx)
   ctx->d = 0x10325476;
   ctx->e = 0xc3d2e1f0;
   ctx->off = 0;
-  ctx->count = 0;
+  ctx->nl = ctx->nh = 0;
 }
 
 /* --- @sha_set@ --- *
@@ -234,7 +239,8 @@ void sha_set(sha_ctx *ctx, const void *buf, unsigned long count)
   ctx->d = LOAD32(p + 12);
   ctx->e = LOAD32(p + 16);
   ctx->off = 0;
-  ctx->count = count;
+  ctx->nl = U32(count);
+  ctx->nh = U32((count >> 16) >> 16);
 }
 
 /* --- @sha_hash@ --- *
@@ -268,8 +274,8 @@ void sha_done(sha_ctx *ctx, void *hash)
 {
   octet *p = hash;
   HASH_PAD(SHA, sha, ctx, 0x80, 0, 8);
-  STORE32(ctx->buf + SHA_BUFSZ - 8, ctx->count >> 29);
-  STORE32(ctx->buf + SHA_BUFSZ - 4, ctx->count << 3);
+  STORE32(ctx->buf + SHA_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3));
+  STORE32(ctx->buf + SHA_BUFSZ - 4, ctx->nl << 3);
   sha_compress(ctx, ctx->buf);
   STORE32(p +  0, ctx->a);
   STORE32(p +  4, ctx->b);
@@ -297,9 +303,13 @@ unsigned long sha_state(sha_ctx *ctx, void *state)
   STORE32(p +  8, ctx->c);
   STORE32(p + 12, ctx->d);
   STORE32(p + 16, ctx->e);
-  return (ctx->count);
+  return (ctx->nl | ((ctx->nh << 16) << 16));
 }
 
+/* --- Generic interface --- */
+
+GHASH_DEF(SHA, sha)
+
 /* --- Test code --- */
 
 HASH_TEST(SHA, sha)