factorial: Fix usage message to fit in with conventions.
[u/mdw/catacomb] / sha.c
diff --git a/sha.c b/sha.c
index a5a3bf3..02f51bc 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.4 2004/04/08 01:36:15 mdw Exp $
  *
  * Implementation of the SHA-1 hash function
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: sha.c,v $
- * Revision 1.1  1999/09/03 08:41:12  mdw
- * Initial import.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <mLib/bits.h>
 
+#include "ghash.h"
+#include "ghash-def.h"
 #include "hash.h"
 #include "sha.h"
 
@@ -209,7 +203,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 +228,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 & ~MASK32) >> 16) >> 16);
 }
 
 /* --- @sha_hash@ --- *
@@ -268,8 +263,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 +292,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)