progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / sha.c
1 /* -*-c-*-
2 *
3 * Implementation of the SHA-1 hash function
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/bits.h>
31
32 #include "ghash.h"
33 #include "ghash-def.h"
34 #include "hash.h"
35 #include "sha.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @sha_compress@ --- *
40 *
41 * Arguments: @sha_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
43 *
44 * Returns: ---
45 *
46 * Use: SHA-1 compression function.
47 */
48
49 void sha_compress(sha_ctx *ctx, const void *sbuf)
50 {
51 uint32 a, b, c, d, e;
52 uint32 m[16];
53 const octet *p;
54 int i;
55
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);
58
59 /* --- Definitions for round functions --- */
60
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)))
64
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; \
67 w = ROR32(w, 2); \
68 } while (0)
69
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)
74
75 /* --- Message scheduling --- */
76
77 #define M(i, i3, i8, i14) do { \
78 uint32 t = m[i] ^ m[i3] ^ m[i8] ^ m[i14]; \
79 m[i] = ROL32(t, 1); \
80 } while (0)
81
82 /* --- The main compression function --- */
83
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);
164
165 /* --- Update the chaining variables --- */
166
167 ctx->a += a; ctx->b += b; ctx->c += c; ctx->d += d; ctx->e += e;
168 }
169
170 /* --- @sha_init@ --- *
171 *
172 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
173 *
174 * Returns: ---
175 *
176 * Use: Initializes a context block ready for hashing.
177 */
178
179 void sha_init(sha_ctx *ctx)
180 {
181 ctx->a = 0x67452301;
182 ctx->b = 0xefcdab89;
183 ctx->c = 0x98badcfe;
184 ctx->d = 0x10325476;
185 ctx->e = 0xc3d2e1f0;
186 ctx->off = 0;
187 ctx->nl = ctx->nh = 0;
188 }
189
190 /* --- @sha_set@ --- *
191 *
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
195 *
196 * Returns: ---
197 *
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.
201 */
202
203 void sha_set(sha_ctx *ctx, const void *buf, unsigned long count)
204 {
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);
211 ctx->off = 0;
212 ctx->nl = U32(count);
213 ctx->nh = U32(((count & ~(unsigned long)MASK32) >> 16) >> 16);
214 }
215
216 /* --- @sha_hash@ --- *
217 *
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
221 *
222 * Returns: ---
223 *
224 * Use: Hashes a buffer of data. The buffer may be of any size and
225 * alignment.
226 */
227
228 void sha_hash(sha_ctx *ctx, const void *buf, size_t sz)
229 {
230 HASH_BUFFER(SHA, sha, ctx, buf, sz);
231 }
232
233 /* --- @sha_done@ --- *
234 *
235 * Arguments: @sha_ctx *ctx@ = pointer to context block
236 * @void *hash@ = pointer to output buffer
237 *
238 * Returns: ---
239 *
240 * Use: Returns the hash of the data read so far.
241 */
242
243 void sha_done(sha_ctx *ctx, void *hash)
244 {
245 octet *p = 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);
255 }
256
257 /* --- @sha_state@ --- *
258 *
259 * Arguments: @sha_ctx *ctx@ = pointer to context
260 * @void *state@ = pointer to buffer for current state
261 *
262 * Returns: Number of bytes written to the hash function so far.
263 *
264 * Use: Returns the current state of the hash function such that
265 * it can be passed to @sha_set@.
266 */
267
268 unsigned long sha_state(sha_ctx *ctx, void *state)
269 {
270 octet *p = 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));
277 }
278
279 /* --- Generic interface --- */
280
281 GHASH_DEF(SHA, sha)
282
283 /* --- Test code --- */
284
285 HASH_TEST(SHA, sha)
286
287 /*----- That's all, folks -------------------------------------------------*/