3 * Implementation of the SHA-1 hash function
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
33 #include "ghash-def.h"
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @sha_compress@ --- *
41 * Arguments: @sha_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
46 * Use: SHA-1 compression function.
49 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
56 a
= ctx
->a
; b
= ctx
->b
; c
= ctx
->c
; d
= ctx
->d
; e
= ctx
->e
;
57 for (p
= sbuf
, i
= 0; i
< 16; i
++, p
+= 4) m
[i
] = LOAD32(p
);
59 /* --- Definitions for round functions --- */
61 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
62 #define G(x, y, z) ((x) ^ (y) ^ (z))
63 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
65 #define T(v, w, x, y, z, i, f, k) do { \
66 z = ROL32(v, 5) + f(w, x, y) + z + m[i] + k; \
70 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
71 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
72 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
73 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
75 /* --- Message scheduling --- */
77 #define M(i, i3, i8, i14) do { \
78 uint32 t = m[i] ^ m[i3] ^ m[i8] ^ m[i14]; \
82 /* --- The main compression function --- */
84 FF(a
, b
, c
, d
, e
, 0); M( 0, 13, 8, 2);
85 FF(e
, a
, b
, c
, d
, 1); M( 1, 14, 9, 3);
86 FF(d
, e
, a
, b
, c
, 2); M( 2, 15, 10, 4);
87 FF(c
, d
, e
, a
, b
, 3); M( 3, 0, 11, 5);
88 FF(b
, c
, d
, e
, a
, 4); M( 4, 1, 12, 6);
89 FF(a
, b
, c
, d
, e
, 5); M( 5, 2, 13, 7);
90 FF(e
, a
, b
, c
, d
, 6); M( 6, 3, 14, 8);
91 FF(d
, e
, a
, b
, c
, 7); M( 7, 4, 15, 9);
92 FF(c
, d
, e
, a
, b
, 8); M( 8, 5, 0, 10);
93 FF(b
, c
, d
, e
, a
, 9); M( 9, 6, 1, 11);
94 FF(a
, b
, c
, d
, e
, 10); M(10, 7, 2, 12);
95 FF(e
, a
, b
, c
, d
, 11); M(11, 8, 3, 13);
96 FF(d
, e
, a
, b
, c
, 12); M(12, 9, 4, 14);
97 FF(c
, d
, e
, a
, b
, 13); M(13, 10, 5, 15);
98 FF(b
, c
, d
, e
, a
, 14); M(14, 11, 6, 0);
99 FF(a
, b
, c
, d
, e
, 15); M(15, 12, 7, 1);
100 FF(e
, a
, b
, c
, d
, 0); M( 0, 13, 8, 2);
101 FF(d
, e
, a
, b
, c
, 1); M( 1, 14, 9, 3);
102 FF(c
, d
, e
, a
, b
, 2); M( 2, 15, 10, 4);
103 FF(b
, c
, d
, e
, a
, 3); M( 3, 0, 11, 5);
104 GG(a
, b
, c
, d
, e
, 4); M( 4, 1, 12, 6);
105 GG(e
, a
, b
, c
, d
, 5); M( 5, 2, 13, 7);
106 GG(d
, e
, a
, b
, c
, 6); M( 6, 3, 14, 8);
107 GG(c
, d
, e
, a
, b
, 7); M( 7, 4, 15, 9);
108 GG(b
, c
, d
, e
, a
, 8); M( 8, 5, 0, 10);
109 GG(a
, b
, c
, d
, e
, 9); M( 9, 6, 1, 11);
110 GG(e
, a
, b
, c
, d
, 10); M(10, 7, 2, 12);
111 GG(d
, e
, a
, b
, c
, 11); M(11, 8, 3, 13);
112 GG(c
, d
, e
, a
, b
, 12); M(12, 9, 4, 14);
113 GG(b
, c
, d
, e
, a
, 13); M(13, 10, 5, 15);
114 GG(a
, b
, c
, d
, e
, 14); M(14, 11, 6, 0);
115 GG(e
, a
, b
, c
, d
, 15); M(15, 12, 7, 1);
116 GG(d
, e
, a
, b
, c
, 0); M( 0, 13, 8, 2);
117 GG(c
, d
, e
, a
, b
, 1); M( 1, 14, 9, 3);
118 GG(b
, c
, d
, e
, a
, 2); M( 2, 15, 10, 4);
119 GG(a
, b
, c
, d
, e
, 3); M( 3, 0, 11, 5);
120 GG(e
, a
, b
, c
, d
, 4); M( 4, 1, 12, 6);
121 GG(d
, e
, a
, b
, c
, 5); M( 5, 2, 13, 7);
122 GG(c
, d
, e
, a
, b
, 6); M( 6, 3, 14, 8);
123 GG(b
, c
, d
, e
, a
, 7); M( 7, 4, 15, 9);
124 HH(a
, b
, c
, d
, e
, 8); M( 8, 5, 0, 10);
125 HH(e
, a
, b
, c
, d
, 9); M( 9, 6, 1, 11);
126 HH(d
, e
, a
, b
, c
, 10); M(10, 7, 2, 12);
127 HH(c
, d
, e
, a
, b
, 11); M(11, 8, 3, 13);
128 HH(b
, c
, d
, e
, a
, 12); M(12, 9, 4, 14);
129 HH(a
, b
, c
, d
, e
, 13); M(13, 10, 5, 15);
130 HH(e
, a
, b
, c
, d
, 14); M(14, 11, 6, 0);
131 HH(d
, e
, a
, b
, c
, 15); M(15, 12, 7, 1);
132 HH(c
, d
, e
, a
, b
, 0); M( 0, 13, 8, 2);
133 HH(b
, c
, d
, e
, a
, 1); M( 1, 14, 9, 3);
134 HH(a
, b
, c
, d
, e
, 2); M( 2, 15, 10, 4);
135 HH(e
, a
, b
, c
, d
, 3); M( 3, 0, 11, 5);
136 HH(d
, e
, a
, b
, c
, 4); M( 4, 1, 12, 6);
137 HH(c
, d
, e
, a
, b
, 5); M( 5, 2, 13, 7);
138 HH(b
, c
, d
, e
, a
, 6); M( 6, 3, 14, 8);
139 HH(a
, b
, c
, d
, e
, 7); M( 7, 4, 15, 9);
140 HH(e
, a
, b
, c
, d
, 8); M( 8, 5, 0, 10);
141 HH(d
, e
, a
, b
, c
, 9); M( 9, 6, 1, 11);
142 HH(c
, d
, e
, a
, b
, 10); M(10, 7, 2, 12);
143 HH(b
, c
, d
, e
, a
, 11); M(11, 8, 3, 13);
144 II(a
, b
, c
, d
, e
, 12); M(12, 9, 4, 14);
145 II(e
, a
, b
, c
, d
, 13); M(13, 10, 5, 15);
146 II(d
, e
, a
, b
, c
, 14); M(14, 11, 6, 0);
147 II(c
, d
, e
, a
, b
, 15); M(15, 12, 7, 1);
148 II(b
, c
, d
, e
, a
, 0);
149 II(a
, b
, c
, d
, e
, 1);
150 II(e
, a
, b
, c
, d
, 2);
151 II(d
, e
, a
, b
, c
, 3);
152 II(c
, d
, e
, a
, b
, 4);
153 II(b
, c
, d
, e
, a
, 5);
154 II(a
, b
, c
, d
, e
, 6);
155 II(e
, a
, b
, c
, d
, 7);
156 II(d
, e
, a
, b
, c
, 8);
157 II(c
, d
, e
, a
, b
, 9);
158 II(b
, c
, d
, e
, a
, 10);
159 II(a
, b
, c
, d
, e
, 11);
160 II(e
, a
, b
, c
, d
, 12);
161 II(d
, e
, a
, b
, c
, 13);
162 II(c
, d
, e
, a
, b
, 14);
163 II(b
, c
, d
, e
, a
, 15);
165 /* --- Update the chaining variables --- */
167 ctx
->a
+= a
; ctx
->b
+= b
; ctx
->c
+= c
; ctx
->d
+= d
; ctx
->e
+= e
;
170 /* --- @sha_init@ --- *
172 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
176 * Use: Initializes a context block ready for hashing.
179 void sha_init(sha_ctx
*ctx
)
187 ctx
->nl
= ctx
->nh
= 0;
190 /* --- @sha_set@ --- *
192 * Arguments: @sha_ctx *ctx@ = pointer to context block
193 * @const void *buf@ = pointer to state buffer
194 * @unsigned long count@ = current count of bytes processed
198 * Use: Initializes a context block from a given state. This is
199 * useful in cases where the initial hash state is meant to be
200 * secret, e.g., for NMAC and HMAC support.
203 void sha_set(sha_ctx
*ctx
, const void *buf
, unsigned long count
)
205 const octet
*p
= buf
;
206 ctx
->a
= LOAD32(p
+ 0);
207 ctx
->b
= LOAD32(p
+ 4);
208 ctx
->c
= LOAD32(p
+ 8);
209 ctx
->d
= LOAD32(p
+ 12);
210 ctx
->e
= LOAD32(p
+ 16);
212 ctx
->nl
= U32(count
);
213 ctx
->nh
= U32(((count
& ~(unsigned long)MASK32
) >> 16) >> 16);
216 /* --- @sha_hash@ --- *
218 * Arguments: @sha_ctx *ctx@ = pointer to context block
219 * @const void *buf@ = buffer of data to hash
220 * @size_t sz@ = size of buffer to hash
224 * Use: Hashes a buffer of data. The buffer may be of any size and
228 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
230 HASH_BUFFER(SHA
, sha
, ctx
, buf
, sz
);
233 /* --- @sha_done@ --- *
235 * Arguments: @sha_ctx *ctx@ = pointer to context block
236 * @void *hash@ = pointer to output buffer
240 * Use: Returns the hash of the data read so far.
243 void sha_done(sha_ctx
*ctx
, void *hash
)
246 HASH_PAD(SHA
, sha
, ctx
, 0x80, 0, 8);
247 STORE32(ctx
->buf
+ SHA_BUFSZ
- 8, (ctx
->nl
>> 29) | (ctx
->nh
<< 3));
248 STORE32(ctx
->buf
+ SHA_BUFSZ
- 4, ctx
->nl
<< 3);
249 sha_compress(ctx
, ctx
->buf
);
250 STORE32(p
+ 0, ctx
->a
);
251 STORE32(p
+ 4, ctx
->b
);
252 STORE32(p
+ 8, ctx
->c
);
253 STORE32(p
+ 12, ctx
->d
);
254 STORE32(p
+ 16, ctx
->e
);
257 /* --- @sha_state@ --- *
259 * Arguments: @sha_ctx *ctx@ = pointer to context
260 * @void *state@ = pointer to buffer for current state
262 * Returns: Number of bytes written to the hash function so far.
264 * Use: Returns the current state of the hash function such that
265 * it can be passed to @sha_set@.
268 unsigned long sha_state(sha_ctx
*ctx
, void *state
)
271 STORE32(p
+ 0, ctx
->a
);
272 STORE32(p
+ 4, ctx
->b
);
273 STORE32(p
+ 8, ctx
->c
);
274 STORE32(p
+ 12, ctx
->d
);
275 STORE32(p
+ 16, ctx
->e
);
276 return (ctx
->nl
| ((ctx
->nh
<< 16) << 16));
279 /* --- Generic interface --- */
283 /* --- Test code --- */
287 /*----- That's all, folks -------------------------------------------------*/