chiark / gitweb /
symm/sha512.[ch], etc.: Support SHA512/224 and SHA512/256.
[catacomb] / symm / sha512.h
1 /* -*-c-*-
2  *
3  * Implementation of the SHA-512 hash function
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Notes on the SHA-512 hash function --------------------------------*
29  *
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
35  * with 192-bit AES.
36  */
37
38 #ifndef CATACOMB_SHA512_H
39 #define CATACOMB_SHA512_H
40 #define CATACOMB_SHA384_H
41
42 #ifdef __cplusplus
43   extern "C" {
44 #endif
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #include <mLib/bits.h>
49
50 #ifndef CATACOMB_GHASH_H
51 #  include "ghash.h"
52 #endif
53
54 /*----- Magic numbers -----------------------------------------------------*/
55
56 #define SHA512_BUFSZ 128
57 #define SHA512_HASHSZ 64
58 #define SHA512_STATESZ 64
59
60 #define SHA384_BUFSZ 128
61 #define SHA384_HASHSZ 48
62 #define SHA384_STATESZ 64
63
64 #define SHA512_256_BUFSZ 128
65 #define SHA512_256_HASHSZ 32
66 #define SHA512_256_STATESZ 64
67
68 #define SHA512_224_BUFSZ 128
69 #define SHA512_224_HASHSZ 28
70 #define SHA512_224_STATESZ 64
71
72 /*----- Data structures ---------------------------------------------------*/
73
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;
80
81 /*----- Functions provided ------------------------------------------------*/
82
83 /* --- @sha512_compress@, etc. --- *
84  *
85  * Arguments:   @sha512_ctx *ctx@ = pointer to context block
86  *              @const void *sbuf@ = pointer to buffer of appropriate size
87  *
88  * Returns:     ---
89  *
90  * Use:         SHA-512 compression function.
91  */
92
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
97
98 /* --- @sha512_init@, etc. --- *
99  *
100  * Arguments:   @sha512_ctx *ctx@ = pointer to context block to initialize
101  *
102  * Returns:     ---
103  *
104  * Use:         Initializes a context block ready for hashing.
105  */
106
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*/);
111
112 /* --- @sha512_set@, etc. --- *
113  *
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
117  *
118  * Returns:     ---
119  *
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.
123  */
124
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
130
131 /* --- @sha512_hash@, etc. --- *
132  *
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
136  *
137  * Returns:     ---
138  *
139  * Use:         Hashes a buffer of data.  The buffer may be of any size and
140  *              alignment.
141  */
142
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
148
149 /* --- @sha512_done@, etc. --- *
150  *
151  * Arguments:   @sha512_ctx *ctx@ = pointer to context block
152  *              @void *hash@ = pointer to output buffer
153  *
154  * Returns:     ---
155  *
156  * Use:         Returns the hash of the data read so far.
157  */
158
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*/);
163
164 /* --- @sha512_state@, @sha384_state@ --- *
165  *
166  * Arguments:   @sha512_ctx *ctx@ = pointer to context
167  *              @void *state@ = pointer to buffer for current state
168  *
169  * Returns:     Number of bytes written to the hash function so far.
170  *
171  * Use:         Returns the current state of the hash function such that
172  *              it can be passed to @sha512_set@.
173  */
174
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
179
180 /*----- Generic hash interface --------------------------------------------*/
181
182 extern const gchash sha512;
183 extern const gchash sha384;
184 extern const gchash sha512_256;
185 extern const gchash sha512_224;
186
187 /*----- That's all, folks -------------------------------------------------*/
188
189 #ifdef __cplusplus
190   }
191 #endif
192
193 #endif