New SHA variants with longer outputs.
[u/mdw/catacomb] / sha512.h
1 /* -*-c-*-
2 *
3 * $Id: sha512.h,v 1.1 2000/10/15 17:48:15 mdw Exp $
4 *
5 * Implementation of the SHA-512 hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: sha512.h,v $
33 * Revision 1.1 2000/10/15 17:48:15 mdw
34 * New SHA variants with longer outputs.
35 *
36 */
37
38 /*----- Notes on the SHA-512 hash function ----------------------------------*
39 *
40 * SHA-1 (Secure Hash Algorithm) was designed by the NSA, for use with the
41 * Digital Signature Algorithm. This is an evolution with a larger output
42 * size, intended to provide security commensurate with 256-bit AES. At the
43 * time of writing, SHA-512 is very new, and can't be trusted too far. There
44 * is also a truncated version, SHA-384, which provides security commensurate
45 * with 192-bit AES.
46 */
47
48 #ifndef CATACOMB_SHA512_H
49 #define CATACOMB_SHA512_H
50 #define CATACOMB_SHA384_H
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 /*----- Header files ------------------------------------------------------*/
57
58 #include <mLib/bits.h>
59
60 #ifndef CATACOMB_GHASH_H
61 # include "ghash.h"
62 #endif
63
64 /*----- Magic numbers -----------------------------------------------------*/
65
66 #define SHA512_BUFSZ 128
67 #define SHA512_HASHSZ 64
68
69 #define SHA384_BUFSZ 128
70 #define SHA384_HASHSZ 48
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;
80
81 /*----- Functions provided ------------------------------------------------*/
82
83 /* --- @sha512_compress@, @sha384_compress@ --- *
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
96 /* --- @sha512_init@, @sha384_init@ --- *
97 *
98 * Arguments: @sha512_ctx *ctx@ = pointer to context block to initialize
99 *
100 * Returns: ---
101 *
102 * Use: Initializes a context block ready for hashing.
103 */
104
105 extern void sha512_init(sha512_ctx */*ctx*/);
106 extern void sha384_init(sha512_ctx */*ctx*/);
107
108 /* --- @sha512_set@, @sha384_set@ --- *
109 *
110 * Arguments: @sha512_ctx *ctx@ = pointer to context block
111 * @const void *buf@ = pointer to state buffer
112 * @unsigned long count@ = current count of bytes processed
113 *
114 * Returns: ---
115 *
116 * Use: Initializes a context block from a given state. This is
117 * useful in cases where the initial hash state is meant to be
118 * secret, e.g., for NMAC and HMAC support.
119 */
120
121 extern void sha512_set(sha512_ctx */*ctx*/, const void */*buf*/,
122 unsigned long /*count*/);
123 #define sha384_set sha512_set
124
125 /* --- @sha512_hash@, @sha384_hash@ --- *
126 *
127 * Arguments: @sha512_ctx *ctx@ = pointer to context block
128 * @const void *buf@ = buffer of data to hash
129 * @size_t sz@ = size of buffer to hash
130 *
131 * Returns: ---
132 *
133 * Use: Hashes a buffer of data. The buffer may be of any size and
134 * alignment.
135 */
136
137 extern void sha512_hash(sha512_ctx */*ctx*/,
138 const void */*buf*/, size_t /*sz*/);
139 #define sha384_hash sha512_hash
140
141 /* --- @sha512_done@, @sha384_done@ --- *
142 *
143 * Arguments: @sha512_ctx *ctx@ = pointer to context block
144 * @void *hash@ = pointer to output buffer
145 *
146 * Returns: ---
147 *
148 * Use: Returns the hash of the data read so far.
149 */
150
151 extern void sha512_done(sha512_ctx */*ctx*/, void */*hash*/);
152 extern void sha384_done(sha512_ctx */*ctx*/, void */*hash*/);
153
154 /* --- @sha512_state@, @sha384_state@ --- *
155 *
156 * Arguments: @sha512_ctx *ctx@ = pointer to context
157 * @void *state@ = pointer to buffer for current state
158 *
159 * Returns: Number of bytes written to the hash function so far.
160 *
161 * Use: Returns the current state of the hash function such that
162 * it can be passed to @sha512_set@.
163 */
164
165 extern unsigned long sha512_state(sha512_ctx */*ctx*/, void */*state*/);
166 #define sha384_state sha512_state
167
168 /*----- Generic hash interface --------------------------------------------*/
169
170 extern const gchash sha512;
171 extern const gchash sha384;
172
173 /*----- That's all, folks -------------------------------------------------*/
174
175 #ifdef __cplusplus
176 }
177 #endif
178
179 #endif