catcrypt: Implement symmetric key-encapsulation and signature schemes.
[u/mdw/catacomb] / sha512.h
CommitLineData
eee16120 1/* -*-c-*-
2 *
5d5a3322 3 * $Id$
eee16120 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
5d5a3322 30/*----- Notes on the SHA-512 hash function --------------------------------*
eee16120 31 *
32 * SHA-1 (Secure Hash Algorithm) was designed by the NSA, for use with the
33 * Digital Signature Algorithm. This is an evolution with a larger output
34 * size, intended to provide security commensurate with 256-bit AES. At the
35 * time of writing, SHA-512 is very new, and can't be trusted too far. There
36 * is also a truncated version, SHA-384, which provides security commensurate
37 * with 192-bit AES.
38 */
39
40#ifndef CATACOMB_SHA512_H
41#define CATACOMB_SHA512_H
42#define CATACOMB_SHA384_H
43
44#ifdef __cplusplus
45 extern "C" {
46#endif
47
48/*----- Header files ------------------------------------------------------*/
49
50#include <mLib/bits.h>
51
52#ifndef CATACOMB_GHASH_H
53# include "ghash.h"
54#endif
55
56/*----- Magic numbers -----------------------------------------------------*/
57
58#define SHA512_BUFSZ 128
59#define SHA512_HASHSZ 64
c850c0da 60#define SHA512_STATESZ 64
eee16120 61
62#define SHA384_BUFSZ 128
63#define SHA384_HASHSZ 48
c850c0da 64#define SHA384_STATESZ 64
eee16120 65
66/*----- Data structures ---------------------------------------------------*/
67
68typedef struct sha512_ctx {
69 kludge64 a, b, c, d, e, f, g, h; /* Chaining variables */
70 uint32 nh, nl; /* Byte count so far */
71 unsigned off; /* Offset into buffer */
72 octet buf[SHA512_BUFSZ]; /* Accumulation buffer */
73} sha512_ctx, sha384_ctx;
74
75/*----- Functions provided ------------------------------------------------*/
76
77/* --- @sha512_compress@, @sha384_compress@ --- *
78 *
79 * Arguments: @sha512_ctx *ctx@ = pointer to context block
80 * @const void *sbuf@ = pointer to buffer of appropriate size
81 *
82 * Returns: ---
83 *
84 * Use: SHA-512 compression function.
85 */
86
87extern void sha512_compress(sha512_ctx */*ctx*/, const void */*sbuf*/);
88#define sha384_compress sha512_compress
89
90/* --- @sha512_init@, @sha384_init@ --- *
91 *
92 * Arguments: @sha512_ctx *ctx@ = pointer to context block to initialize
93 *
94 * Returns: ---
95 *
96 * Use: Initializes a context block ready for hashing.
97 */
98
99extern void sha512_init(sha512_ctx */*ctx*/);
100extern void sha384_init(sha512_ctx */*ctx*/);
101
102/* --- @sha512_set@, @sha384_set@ --- *
103 *
104 * Arguments: @sha512_ctx *ctx@ = pointer to context block
105 * @const void *buf@ = pointer to state buffer
106 * @unsigned long count@ = current count of bytes processed
107 *
108 * Returns: ---
109 *
110 * Use: Initializes a context block from a given state. This is
111 * useful in cases where the initial hash state is meant to be
112 * secret, e.g., for NMAC and HMAC support.
113 */
114
115extern void sha512_set(sha512_ctx */*ctx*/, const void */*buf*/,
116 unsigned long /*count*/);
117#define sha384_set sha512_set
118
119/* --- @sha512_hash@, @sha384_hash@ --- *
120 *
121 * Arguments: @sha512_ctx *ctx@ = pointer to context block
122 * @const void *buf@ = buffer of data to hash
123 * @size_t sz@ = size of buffer to hash
124 *
125 * Returns: ---
126 *
127 * Use: Hashes a buffer of data. The buffer may be of any size and
128 * alignment.
129 */
130
131extern void sha512_hash(sha512_ctx */*ctx*/,
132 const void */*buf*/, size_t /*sz*/);
133#define sha384_hash sha512_hash
134
135/* --- @sha512_done@, @sha384_done@ --- *
136 *
137 * Arguments: @sha512_ctx *ctx@ = pointer to context block
138 * @void *hash@ = pointer to output buffer
139 *
140 * Returns: ---
141 *
142 * Use: Returns the hash of the data read so far.
143 */
144
145extern void sha512_done(sha512_ctx */*ctx*/, void */*hash*/);
146extern void sha384_done(sha512_ctx */*ctx*/, void */*hash*/);
147
148/* --- @sha512_state@, @sha384_state@ --- *
149 *
150 * Arguments: @sha512_ctx *ctx@ = pointer to context
151 * @void *state@ = pointer to buffer for current state
152 *
153 * Returns: Number of bytes written to the hash function so far.
154 *
155 * Use: Returns the current state of the hash function such that
156 * it can be passed to @sha512_set@.
157 */
158
159extern unsigned long sha512_state(sha512_ctx */*ctx*/, void */*state*/);
160#define sha384_state sha512_state
161
162/*----- Generic hash interface --------------------------------------------*/
163
164extern const gchash sha512;
165extern const gchash sha384;
166
167/*----- That's all, folks -------------------------------------------------*/
168
169#ifdef __cplusplus
170 }
171#endif
172
173#endif