3 * $Id: sha.c,v 1.1 1999/09/03 08:41:12 mdw Exp $
5 * Implementation of the SHA-1 hash function
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/09/03 08:41:12 mdw
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @sha_compress@ --- *
49 * Arguments: @sha_ctx *ctx@ = pointer to context block
50 * @const void *sbuf@ = pointer to buffer of appropriate size
54 * Use: SHA-1 compression function.
57 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
62 /* --- Fetch the chaining variables --- */
70 /* --- Fetch and expand the buffer contents --- */
76 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
78 for (i
= 16; i
< 80; i
++) {
79 uint32 x
= buf
[i
- 3] ^ buf
[i
- 8] ^ buf
[i
- 14] ^ buf
[i
- 16];
84 /* --- Definitions for round functions --- */
86 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
87 #define G(x, y, z) ((x) ^ (y) ^ (z))
88 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
90 #define T(v, w, x, y, z, i, f, k) do { \
91 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
95 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
96 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
97 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
98 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
100 /* --- The main compression function --- */
102 FF(a
, b
, c
, d
, e
, 0);
103 FF(e
, a
, b
, c
, d
, 1);
104 FF(d
, e
, a
, b
, c
, 2);
105 FF(c
, d
, e
, a
, b
, 3);
106 FF(b
, c
, d
, e
, a
, 4);
107 FF(a
, b
, c
, d
, e
, 5);
108 FF(e
, a
, b
, c
, d
, 6);
109 FF(d
, e
, a
, b
, c
, 7);
110 FF(c
, d
, e
, a
, b
, 8);
111 FF(b
, c
, d
, e
, a
, 9);
112 FF(a
, b
, c
, d
, e
, 10);
113 FF(e
, a
, b
, c
, d
, 11);
114 FF(d
, e
, a
, b
, c
, 12);
115 FF(c
, d
, e
, a
, b
, 13);
116 FF(b
, c
, d
, e
, a
, 14);
117 FF(a
, b
, c
, d
, e
, 15);
118 FF(e
, a
, b
, c
, d
, 16);
119 FF(d
, e
, a
, b
, c
, 17);
120 FF(c
, d
, e
, a
, b
, 18);
121 FF(b
, c
, d
, e
, a
, 19);
123 GG(a
, b
, c
, d
, e
, 20);
124 GG(e
, a
, b
, c
, d
, 21);
125 GG(d
, e
, a
, b
, c
, 22);
126 GG(c
, d
, e
, a
, b
, 23);
127 GG(b
, c
, d
, e
, a
, 24);
128 GG(a
, b
, c
, d
, e
, 25);
129 GG(e
, a
, b
, c
, d
, 26);
130 GG(d
, e
, a
, b
, c
, 27);
131 GG(c
, d
, e
, a
, b
, 28);
132 GG(b
, c
, d
, e
, a
, 29);
133 GG(a
, b
, c
, d
, e
, 30);
134 GG(e
, a
, b
, c
, d
, 31);
135 GG(d
, e
, a
, b
, c
, 32);
136 GG(c
, d
, e
, a
, b
, 33);
137 GG(b
, c
, d
, e
, a
, 34);
138 GG(a
, b
, c
, d
, e
, 35);
139 GG(e
, a
, b
, c
, d
, 36);
140 GG(d
, e
, a
, b
, c
, 37);
141 GG(c
, d
, e
, a
, b
, 38);
142 GG(b
, c
, d
, e
, a
, 39);
144 HH(a
, b
, c
, d
, e
, 40);
145 HH(e
, a
, b
, c
, d
, 41);
146 HH(d
, e
, a
, b
, c
, 42);
147 HH(c
, d
, e
, a
, b
, 43);
148 HH(b
, c
, d
, e
, a
, 44);
149 HH(a
, b
, c
, d
, e
, 45);
150 HH(e
, a
, b
, c
, d
, 46);
151 HH(d
, e
, a
, b
, c
, 47);
152 HH(c
, d
, e
, a
, b
, 48);
153 HH(b
, c
, d
, e
, a
, 49);
154 HH(a
, b
, c
, d
, e
, 50);
155 HH(e
, a
, b
, c
, d
, 51);
156 HH(d
, e
, a
, b
, c
, 52);
157 HH(c
, d
, e
, a
, b
, 53);
158 HH(b
, c
, d
, e
, a
, 54);
159 HH(a
, b
, c
, d
, e
, 55);
160 HH(e
, a
, b
, c
, d
, 56);
161 HH(d
, e
, a
, b
, c
, 57);
162 HH(c
, d
, e
, a
, b
, 58);
163 HH(b
, c
, d
, e
, a
, 59);
165 II(a
, b
, c
, d
, e
, 60);
166 II(e
, a
, b
, c
, d
, 61);
167 II(d
, e
, a
, b
, c
, 62);
168 II(c
, d
, e
, a
, b
, 63);
169 II(b
, c
, d
, e
, a
, 64);
170 II(a
, b
, c
, d
, e
, 65);
171 II(e
, a
, b
, c
, d
, 66);
172 II(d
, e
, a
, b
, c
, 67);
173 II(c
, d
, e
, a
, b
, 68);
174 II(b
, c
, d
, e
, a
, 69);
175 II(a
, b
, c
, d
, e
, 70);
176 II(e
, a
, b
, c
, d
, 71);
177 II(d
, e
, a
, b
, c
, 72);
178 II(c
, d
, e
, a
, b
, 73);
179 II(b
, c
, d
, e
, a
, 74);
180 II(a
, b
, c
, d
, e
, 75);
181 II(e
, a
, b
, c
, d
, 76);
182 II(d
, e
, a
, b
, c
, 77);
183 II(c
, d
, e
, a
, b
, 78);
184 II(b
, c
, d
, e
, a
, 79);
186 /* --- Update the chaining variables --- */
195 /* --- @sha_init@ --- *
197 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
201 * Use: Initializes a context block ready for hashing.
204 void sha_init(sha_ctx
*ctx
)
215 /* --- @sha_set@ --- *
217 * Arguments: @sha_ctx *ctx@ = pointer to context block
218 * @const void *buf@ = pointer to state buffer
219 * @unsigned long count@ = current count of bytes processed
223 * Use: Initializes a context block from a given state. This is
224 * useful in cases where the initial hash state is meant to be
225 * secret, e.g., for NMAC and HMAC support.
228 void sha_set(sha_ctx
*ctx
, const void *buf
, unsigned long count
)
230 const octet
*p
= buf
;
231 ctx
->a
= LOAD32(p
+ 0);
232 ctx
->b
= LOAD32(p
+ 4);
233 ctx
->c
= LOAD32(p
+ 8);
234 ctx
->d
= LOAD32(p
+ 12);
235 ctx
->e
= LOAD32(p
+ 16);
240 /* --- @sha_hash@ --- *
242 * Arguments: @sha_ctx *ctx@ = pointer to context block
243 * @const void *buf@ = buffer of data to hash
244 * @size_t sz@ = size of buffer to hash
248 * Use: Hashes a buffer of data. The buffer may be of any size and
252 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
254 HASH_BUFFER(SHA
, sha
, ctx
, buf
, sz
);
257 /* --- @sha_done@ --- *
259 * Arguments: @sha_ctx *ctx@ = pointer to context block
260 * @void *hash@ = pointer to output buffer
264 * Use: Returns the hash of the data read so far.
267 void sha_done(sha_ctx
*ctx
, void *hash
)
270 HASH_PAD(SHA
, sha
, ctx
, 0x80, 0, 8);
271 STORE32(ctx
->buf
+ SHA_BUFSZ
- 8, ctx
->count
>> 29);
272 STORE32(ctx
->buf
+ SHA_BUFSZ
- 4, ctx
->count
<< 3);
273 sha_compress(ctx
, ctx
->buf
);
274 STORE32(p
+ 0, ctx
->a
);
275 STORE32(p
+ 4, ctx
->b
);
276 STORE32(p
+ 8, ctx
->c
);
277 STORE32(p
+ 12, ctx
->d
);
278 STORE32(p
+ 16, ctx
->e
);
281 /* --- @sha_state@ --- *
283 * Arguments: @sha_ctx *ctx@ = pointer to context
284 * @void *state@ = pointer to buffer for current state
286 * Returns: Number of bytes written to the hash function so far.
288 * Use: Returns the current state of the hash function such that
289 * it can be passed to @sha_set@.
292 unsigned long sha_state(sha_ctx
*ctx
, void *state
)
295 STORE32(p
+ 0, ctx
->a
);
296 STORE32(p
+ 4, ctx
->b
);
297 STORE32(p
+ 8, ctx
->c
);
298 STORE32(p
+ 12, ctx
->d
);
299 STORE32(p
+ 16, ctx
->e
);
303 /* --- Test code --- */
307 /*----- That's all, folks -------------------------------------------------*/