d03ab969 |
1 | /* -*-c-*- |
2 | * |
c850c0da |
3 | * $Id: sha.h,v 1.5 2000/10/15 19:09:20 mdw Exp $ |
d03ab969 |
4 | * |
5 | * Implementation of the SHA-1 hash function |
6 | * |
7 | * (c) 1999 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: sha.h,v $ |
c850c0da |
33 | * Revision 1.5 2000/10/15 19:09:20 mdw |
34 | * Support HMAC mode for hash functions which need to store more state than |
35 | * the hash output size. |
36 | * |
1a44eacd |
37 | * Revision 1.4 2000/06/17 11:32:52 mdw |
38 | * Change buffer offset to be unsigned. |
39 | * |
dc2f0497 |
40 | * Revision 1.3 1999/12/10 23:20:03 mdw |
41 | * New hash interface requirements. |
42 | * |
4dd00d47 |
43 | * Revision 1.2 1999/11/19 13:20:08 mdw |
44 | * Modify notes section slightly. |
45 | * |
d03ab969 |
46 | * Revision 1.1 1999/09/03 08:41:12 mdw |
47 | * Initial import. |
48 | * |
49 | */ |
50 | |
51 | /*----- Notes on the SHA-1 hash function ----------------------------------* |
52 | * |
53 | * SHA (Secure Hash Algorithm) was designed by the NSA, for use with the |
4dd00d47 |
54 | * Digital Signature Algorithm. It is defined by FIPS 180-1. It has gained |
55 | * wide acceptance since its initial publication, and is probably now most |
56 | * people's collision-resistant function of choice. The author prefers |
57 | * RIPEMD-160, for no particularly good reasons. |
d03ab969 |
58 | */ |
59 | |
dc2f0497 |
60 | #ifndef CATACOMB_SHA_H |
61 | #define CATACOMB_SHA_H |
d03ab969 |
62 | |
63 | #ifdef __cplusplus |
64 | extern "C" { |
65 | #endif |
66 | |
67 | /*----- Header files ------------------------------------------------------*/ |
68 | |
69 | #include <mLib/bits.h> |
70 | |
dc2f0497 |
71 | #ifndef CATACOMB_GHASH_H |
72 | # include "ghash.h" |
73 | #endif |
74 | |
d03ab969 |
75 | /*----- Magic numbers -----------------------------------------------------*/ |
76 | |
77 | #define SHA_BUFSZ 64 |
78 | #define SHA_HASHSZ 20 |
c850c0da |
79 | #define SHA_STATESZ 20 |
d03ab969 |
80 | |
81 | /*----- Data structures ---------------------------------------------------*/ |
82 | |
83 | typedef struct sha_ctx { |
84 | uint32 a, b, c, d, e; /* Chaining variables */ |
dc2f0497 |
85 | uint32 nl, nh; /* Byte count so far */ |
1a44eacd |
86 | unsigned off; /* Offset into buffer */ |
d03ab969 |
87 | octet buf[SHA_BUFSZ]; /* Accumulation buffer */ |
88 | } sha_ctx; |
89 | |
90 | /*----- Functions provided ------------------------------------------------*/ |
91 | |
92 | /* --- @sha_compress@ --- * |
93 | * |
94 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
95 | * @const void *sbuf@ = pointer to buffer of appropriate size |
96 | * |
97 | * Returns: --- |
98 | * |
99 | * Use: SHA compression function. |
100 | */ |
101 | |
102 | extern void sha_compress(sha_ctx */*ctx*/, const void */*sbuf*/); |
103 | |
104 | /* --- @sha_init@ --- * |
105 | * |
106 | * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize |
107 | * |
108 | * Returns: --- |
109 | * |
110 | * Use: Initializes a context block ready for hashing. |
111 | */ |
112 | |
113 | extern void sha_init(sha_ctx */*ctx*/); |
114 | |
115 | /* --- @sha_set@ --- * |
116 | * |
117 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
118 | * @const void *buf@ = pointer to state buffer |
119 | * @unsigned long count@ = current count of bytes processed |
120 | * |
121 | * Returns: --- |
122 | * |
123 | * Use: Initializes a context block from a given state. This is |
124 | * useful in cases where the initial hash state is meant to be |
125 | * secret, e.g., for NMAC and HMAC support. |
126 | */ |
127 | |
128 | extern void sha_set(sha_ctx */*ctx*/, const void */*buf*/, |
129 | unsigned long /*count*/); |
130 | |
131 | /* --- @sha_hash@ --- * |
132 | * |
133 | * Arguments: @sha_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 sha_hash(sha_ctx */*ctx*/, const void */*buf*/, size_t /*sz*/); |
144 | |
145 | /* --- @sha_done@ --- * |
146 | * |
147 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
148 | * @void *hash@ = pointer to output buffer |
149 | * |
150 | * Returns: --- |
151 | * |
152 | * Use: Returns the hash of the data read so far. |
153 | */ |
154 | |
155 | extern void sha_done(sha_ctx */*ctx*/, void */*hash*/); |
156 | |
157 | /* --- @sha_state@ --- * |
158 | * |
159 | * Arguments: @sha_ctx *ctx@ = pointer to context |
160 | * @void *state@ = pointer to buffer for current state |
161 | * |
162 | * Returns: Number of bytes written to the hash function so far. |
163 | * |
164 | * Use: Returns the current state of the hash function such that |
165 | * it can be passed to @sha_set@. |
166 | */ |
167 | |
168 | extern unsigned long sha_state(sha_ctx */*ctx*/, void */*state*/); |
169 | |
dc2f0497 |
170 | /*----- Generic hash interface --------------------------------------------*/ |
171 | |
172 | extern const gchash sha; |
173 | |
d03ab969 |
174 | /*----- That's all, folks -------------------------------------------------*/ |
175 | |
176 | #ifdef __cplusplus |
177 | } |
178 | #endif |
179 | |
180 | #endif |