Rearrange the file tree.
[u/mdw/catacomb] / symm / sha512.h
diff --git a/symm/sha512.h b/symm/sha512.h
new file mode 100644 (file)
index 0000000..2f3edc2
--- /dev/null
@@ -0,0 +1,171 @@
+/* -*-c-*-
+ *
+ * Implementation of the SHA-512 hash function
+ *
+ * (c) 2000 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * 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.
+ */
+
+/*----- Notes on the SHA-512 hash function --------------------------------*
+ *
+ * SHA-1 (Secure Hash Algorithm) was designed by the NSA, for use with the
+ * Digital Signature Algorithm.  This is an evolution with a larger output
+ * size, intended to provide security commensurate with 256-bit AES.  At the
+ * time of writing, SHA-512 is very new, and can't be trusted too far.  There
+ * is also a truncated version, SHA-384, which provides security commensurate
+ * with 192-bit AES.
+ */
+
+#ifndef CATACOMB_SHA512_H
+#define CATACOMB_SHA512_H
+#define CATACOMB_SHA384_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+
+#ifndef CATACOMB_GHASH_H
+#  include "ghash.h"
+#endif
+
+/*----- Magic numbers -----------------------------------------------------*/
+
+#define SHA512_BUFSZ 128
+#define SHA512_HASHSZ 64
+#define SHA512_STATESZ 64
+
+#define SHA384_BUFSZ 128
+#define SHA384_HASHSZ 48
+#define SHA384_STATESZ 64
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct sha512_ctx {
+  kludge64 a, b, c, d, e, f, g, h;     /* Chaining variables */
+  uint32 nh, nl;                       /* Byte count so far */
+  unsigned off;                                /* Offset into buffer */
+  octet buf[SHA512_BUFSZ];             /* Accumulation buffer */
+} sha512_ctx, sha384_ctx;
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @sha512_compress@, @sha384_compress@ --- *
+ *
+ * Arguments:  @sha512_ctx *ctx@ = pointer to context block
+ *             @const void *sbuf@ = pointer to buffer of appropriate size
+ *
+ * Returns:    ---
+ *
+ * Use:                SHA-512 compression function.
+ */
+
+extern void sha512_compress(sha512_ctx */*ctx*/, const void */*sbuf*/);
+#define sha384_compress sha512_compress
+
+/* --- @sha512_init@, @sha384_init@ --- *
+ *
+ * Arguments:  @sha512_ctx *ctx@ = pointer to context block to initialize
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a context block ready for hashing.
+ */
+
+extern void sha512_init(sha512_ctx */*ctx*/);
+extern void sha384_init(sha512_ctx */*ctx*/);
+
+/* --- @sha512_set@, @sha384_set@ --- *
+ *
+ * Arguments:  @sha512_ctx *ctx@ = pointer to context block
+ *             @const void *buf@ = pointer to state buffer
+ *             @unsigned long count@ = current count of bytes processed
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a context block from a given state.  This is
+ *             useful in cases where the initial hash state is meant to be
+ *             secret, e.g., for NMAC and HMAC support.
+ */
+
+extern void sha512_set(sha512_ctx */*ctx*/, const void */*buf*/,
+                      unsigned long /*count*/);
+#define sha384_set sha512_set
+
+/* --- @sha512_hash@, @sha384_hash@ --- *
+ *
+ * Arguments:  @sha512_ctx *ctx@ = pointer to context block
+ *             @const void *buf@ = buffer of data to hash
+ *             @size_t sz@ = size of buffer to hash
+ *
+ * Returns:    ---
+ *
+ * Use:                Hashes a buffer of data.  The buffer may be of any size and
+ *             alignment.
+ */
+
+extern void sha512_hash(sha512_ctx */*ctx*/,
+                       const void */*buf*/, size_t /*sz*/);
+#define sha384_hash sha512_hash
+
+/* --- @sha512_done@, @sha384_done@ --- *
+ *
+ * Arguments:  @sha512_ctx *ctx@ = pointer to context block
+ *             @void *hash@ = pointer to output buffer
+ *
+ * Returns:    ---
+ *
+ * Use:                Returns the hash of the data read so far.
+ */
+
+extern void sha512_done(sha512_ctx */*ctx*/, void */*hash*/);
+extern void sha384_done(sha512_ctx */*ctx*/, void */*hash*/);
+
+/* --- @sha512_state@, @sha384_state@ --- *
+ *
+ * Arguments:  @sha512_ctx *ctx@ = pointer to context
+ *             @void *state@ = pointer to buffer for current state
+ *
+ * Returns:    Number of bytes written to the hash function so far.
+ *
+ * Use:                Returns the current state of the hash function such that
+ *             it can be passed to @sha512_set@.
+ */
+
+extern unsigned long sha512_state(sha512_ctx */*ctx*/, void */*state*/);
+#define sha384_state sha512_state
+
+/*----- Generic hash interface --------------------------------------------*/
+
+extern const gchash sha512;
+extern const gchash sha384;
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif