3 * Implementation of the SHA-512 hash function
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Notes on the SHA-512 hash function --------------------------------*
30 * SHA-1 (Secure Hash Algorithm) was designed by the NSA, for use with the
31 * Digital Signature Algorithm. This is an evolution with a larger output
32 * size, intended to provide security commensurate with 256-bit AES. At the
33 * time of writing, SHA-512 is very new, and can't be trusted too far. There
34 * is also a truncated version, SHA-384, which provides security commensurate
38 #ifndef CATACOMB_SHA512_H
39 #define CATACOMB_SHA512_H
40 #define CATACOMB_SHA384_H
46 /*----- Header files ------------------------------------------------------*/
48 #include <mLib/bits.h>
50 #ifndef CATACOMB_GHASH_H
54 /*----- Magic numbers -----------------------------------------------------*/
56 #define SHA512_BUFSZ 128
57 #define SHA512_HASHSZ 64
58 #define SHA512_STATESZ 64
60 #define SHA384_BUFSZ 128
61 #define SHA384_HASHSZ 48
62 #define SHA384_STATESZ 64
64 #define SHA512_256_BUFSZ 128
65 #define SHA512_256_HASHSZ 32
66 #define SHA512_256_STATESZ 64
68 #define SHA512_224_BUFSZ 128
69 #define SHA512_224_HASHSZ 28
70 #define SHA512_224_STATESZ 64
72 /*----- Data structures ---------------------------------------------------*/
74 typedef struct sha512_ctx
{
75 kludge64 a
, b
, c
, d
, e
, f
, g
, h
; /* Chaining variables */
76 uint32 nh
, nl
; /* Byte count so far */
77 unsigned off
; /* Offset into buffer */
78 octet buf
[SHA512_BUFSZ
]; /* Accumulation buffer */
79 } sha512_ctx
, sha384_ctx
, sha512_256_ctx
, sha512_224_ctx
;
81 /*----- Functions provided ------------------------------------------------*/
83 /* --- @sha512_compress@, etc. --- *
85 * Arguments: @sha512_ctx *ctx@ = pointer to context block
86 * @const void *sbuf@ = pointer to buffer of appropriate size
90 * Use: SHA-512 compression function.
93 extern void sha512_compress(sha512_ctx */
*ctx*/
, const void */
*sbuf*/
);
94 #define sha384_compress sha512_compress
95 #define sha512_256_compress sha512_compress
96 #define sha512_224_compress sha512_compress
98 /* --- @sha512_init@, etc. --- *
100 * Arguments: @sha512_ctx *ctx@ = pointer to context block to initialize
104 * Use: Initializes a context block ready for hashing.
107 extern void sha512_init(sha512_ctx */
*ctx*/
);
108 extern void sha384_init(sha512_ctx */
*ctx*/
);
109 extern void sha512_256_init(sha512_ctx */
*ctx*/
);
110 extern void sha512_224_init(sha512_ctx */
*ctx*/
);
112 /* --- @sha512_set@, etc. --- *
114 * Arguments: @sha512_ctx *ctx@ = pointer to context block
115 * @const void *buf@ = pointer to state buffer
116 * @unsigned long count@ = current count of bytes processed
120 * Use: Initializes a context block from a given state. This is
121 * useful in cases where the initial hash state is meant to be
122 * secret, e.g., for NMAC and HMAC support.
125 extern void sha512_set(sha512_ctx */
*ctx*/
, const void */
*buf*/
,
126 unsigned long /*count*/);
127 #define sha384_set sha512_set
128 #define sha512_256_set sha512_set
129 #define sha512_224_set sha512_set
131 /* --- @sha512_hash@, etc. --- *
133 * Arguments: @sha512_ctx *ctx@ = pointer to context block
134 * @const void *buf@ = buffer of data to hash
135 * @size_t sz@ = size of buffer to hash
139 * Use: Hashes a buffer of data. The buffer may be of any size and
143 extern void sha512_hash(sha512_ctx */
*ctx*/
,
144 const void */
*buf*/
, size_t /*sz*/);
145 #define sha384_hash sha512_hash
146 #define sha512_256_hash sha512_hash
147 #define sha512_224_hash sha512_hash
149 /* --- @sha512_done@, etc. --- *
151 * Arguments: @sha512_ctx *ctx@ = pointer to context block
152 * @void *hash@ = pointer to output buffer
156 * Use: Returns the hash of the data read so far.
159 extern void sha512_done(sha512_ctx */
*ctx*/
, void */
*hash*/
);
160 extern void sha384_done(sha512_ctx */
*ctx*/
, void */
*hash*/
);
161 extern void sha512_256_done(sha512_ctx */
*ctx*/
, void */
*hash*/
);
162 extern void sha512_224_done(sha512_ctx */
*ctx*/
, void */
*hash*/
);
164 /* --- @sha512_state@, @sha384_state@ --- *
166 * Arguments: @sha512_ctx *ctx@ = pointer to context
167 * @void *state@ = pointer to buffer for current state
169 * Returns: Number of bytes written to the hash function so far.
171 * Use: Returns the current state of the hash function such that
172 * it can be passed to @sha512_set@.
175 extern unsigned long sha512_state(sha512_ctx */
*ctx*/
, void */
*state*/
);
176 #define sha384_state sha512_state
177 #define sha512_256_state sha512_state
178 #define sha512_224_state sha512_state
180 /*----- Generic hash interface --------------------------------------------*/
182 extern const gchash sha512
;
183 extern const gchash sha384
;
184 extern const gchash sha512_256
;
185 extern const gchash sha512_224
;
187 /*----- That's all, folks -------------------------------------------------*/