Add set -e.
[u/mdw/catacomb] / has160.c
1 /* -*-c-*-
2 *
3 * $Id: has160.c,v 1.1 2004/04/04 19:42:59 mdw Exp $
4 *
5 * The HAS160 message digest function
6 *
7 * (c) 2004 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: has160.c,v $
33 * Revision 1.1 2004/04/04 19:42:59 mdw
34 * Add set -e.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <mLib/bits.h>
41
42 #include "ghash.h"
43 #include "ghash-def.h"
44 #include "hash.h"
45 #include "has160.h"
46
47 /*----- Main code ---------------------------------------------------------*/
48
49 /* --- @has160_compress@ --- *
50 *
51 * Arguments: @has160_ctx *ctx@ = pointer to context block
52 * @const void *sbuf@ = pointer to buffer of appropriate size
53 *
54 * Returns: ---
55 *
56 * Use: HAS160 compression function.
57 */
58
59 void has160_compress(has160_ctx *ctx, const void *sbuf)
60 {
61 uint32 a, b, c, d, e;
62 uint32 buf[16];
63
64 /* --- Fetch the chaining variables --- */
65
66 a = ctx->a;
67 b = ctx->b;
68 c = ctx->c;
69 d = ctx->d;
70 e = ctx->e;
71
72 /* --- Fetch the buffer contents --- */
73
74 {
75 int i;
76 const octet *p;
77
78 for (i = 0, p = sbuf; i < 16; i++, p += 4)
79 buf[i] = LOAD32_L(p);
80 }
81
82 /* --- Definitions for round functions --- */
83
84 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
85 #define G(x, y, z) ((x) ^ (y) ^ (z))
86 #define H(x, y, z) ((y) ^ ((x) | ~(z)))
87
88 #define FF(f, a, b, c, d, e, x, s, ss, k) do { \
89 e += ROL32(a, s) + f(b, c, d) + (x) + k; \
90 b = ROL32(b, ss); \
91 } while (0)
92
93 /* --- The actual hashing --- *
94 *
95 * Hmm, this is more regular than most. The macros are quite grim,
96 * though.
97 */
98
99 #define ROUND(f, ss, k, \
100 i0, i1, i2, i3, i4, i5, i6, i7, \
101 i8, i9, i10, i11, i12, i13, i14, i15) do { \
102 FF(f, a, b, c, d, e, buf[ i8]^buf[ i9]^buf[i10]^buf[i11], 5, ss, k); \
103 FF(f, e, a, b, c, d, buf[ i0], 11, ss, k); \
104 FF(f, d, e, a, b, c, buf[ i1], 7, ss, k); \
105 FF(f, c, d, e, a, b, buf[ i2], 15, ss, k); \
106 FF(f, b, c, d, e, a, buf[ i3], 6, ss, k); \
107 FF(f, a, b, c, d, e, buf[i12]^buf[i13]^buf[i14]^buf[i15], 13, ss, k); \
108 FF(f, e, a, b, c, d, buf[ i4], 8, ss, k); \
109 FF(f, d, e, a, b, c, buf[ i5], 14, ss, k); \
110 FF(f, c, d, e, a, b, buf[ i6], 7, ss, k); \
111 FF(f, b, c, d, e, a, buf[ i7], 12, ss, k); \
112 FF(f, a, b, c, d, e, buf[ i0]^buf[ i1]^buf[ i2]^buf[ i3], 9, ss, k); \
113 FF(f, e, a, b, c, d, buf[ i8], 11, ss, k); \
114 FF(f, d, e, a, b, c, buf[ i9], 8, ss, k); \
115 FF(f, c, d, e, a, b, buf[i10], 15, ss, k); \
116 FF(f, b, c, d, e, a, buf[i11], 6, ss, k); \
117 FF(f, a, b, c, d, e, buf[ i4]^buf[ i5]^buf[ i6]^buf[ i7], 12, ss, k); \
118 FF(f, e, a, b, c, d, buf[i12], 9, ss, k); \
119 FF(f, d, e, a, b, c, buf[i13], 14, ss, k); \
120 FF(f, c, d, e, a, b, buf[i14], 5, ss, k); \
121 FF(f, b, c, d, e, a, buf[i15], 13, ss, k); \
122 } while (0)
123
124 ROUND(F, 10, 0x00000000,
125 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
126 ROUND(G, 17, 0x5a827999,
127 3, 6, 9, 12, 15, 2, 5, 8, 11, 14, 1, 4, 7, 10, 13, 0);
128 ROUND(H, 25, 0x6ed9eba1,
129 12, 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3);
130 ROUND(G, 30, 0x8f1bbcdc,
131 7, 2, 13, 8, 3, 14, 9, 4, 15, 10, 5, 0, 11, 6, 1, 12);
132
133 /* --- Update the chaining variables --- */
134
135 ctx->a += a;
136 ctx->b += b;
137 ctx->c += c;
138 ctx->d += d;
139 ctx->e += e;
140 }
141
142 /* --- @has160_init@ --- *
143 *
144 * Arguments: @has160_ctx *ctx@ = pointer to context block to initialize
145 *
146 * Returns: ---
147 *
148 * Use: Initializes a context block ready for hashing.
149 */
150
151 void has160_init(has160_ctx *ctx)
152 {
153 ctx->a = 0x67452301;
154 ctx->b = 0xefcdab89;
155 ctx->c = 0x98badcfe;
156 ctx->d = 0x10325476;
157 ctx->e = 0xc3d2e1f0;
158 ctx->off = 0;
159 ctx->nl = ctx->nh = 0;
160 }
161
162 /* --- @has160_set@ --- *
163 *
164 * Arguments: @has160_ctx *ctx@ = pointer to context block
165 * @const void *buf@ = pointer to state buffer
166 * @unsigned long count@ = current count of bytes processed
167 *
168 * Returns: ---
169 *
170 * Use: Initializes a context block from a given state. This is
171 * useful in cases where the initial hash state is meant to be
172 * secret, e.g., for NMAC and HMAC support.
173 */
174
175 void has160_set(has160_ctx *ctx, const void *buf, unsigned long count)
176 {
177 const octet *p = buf;
178 ctx->a = LOAD32_L(p + 0);
179 ctx->b = LOAD32_L(p + 4);
180 ctx->c = LOAD32_L(p + 8);
181 ctx->d = LOAD32_L(p + 12);
182 ctx->e = LOAD32_L(p + 16);
183 ctx->off = 0;
184 ctx->nl = U32(count);
185 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
186 }
187
188 /* --- @has160_hash@ --- *
189 *
190 * Arguments: @has160_ctx *ctx@ = pointer to context block
191 * @const void *buf@ = buffer of data to hash
192 * @size_t sz@ = size of buffer to hash
193 *
194 * Returns: ---
195 *
196 * Use: Hashes a buffer of data. The buffer may be of any size and
197 * alignment.
198 */
199
200 void has160_hash(has160_ctx *ctx, const void *buf, size_t sz)
201 {
202 HASH_BUFFER(HAS160, has160, ctx, buf, sz);
203 }
204
205 /* --- @has160_done@ --- *
206 *
207 * Arguments: @has160_ctx *ctx@ = pointer to context block
208 * @void *hash@ = pointer to output buffer
209 *
210 * Returns: ---
211 *
212 * Use: Returns the hash of the data read so far.
213 */
214
215 void has160_done(has160_ctx *ctx, void *hash)
216 {
217 octet *p = hash;
218 HASH_MD5STRENGTH(HAS160, has160, ctx);
219 STORE32_L(p + 0, ctx->a);
220 STORE32_L(p + 4, ctx->b);
221 STORE32_L(p + 8, ctx->c);
222 STORE32_L(p + 12, ctx->d);
223 STORE32_L(p + 16, ctx->e);
224 }
225
226 /* --- @has160_state@ --- *
227 *
228 * Arguments: @has160_ctx *ctx@ = pointer to context
229 * @void *state@ = pointer to buffer for current state
230 *
231 * Returns: Number of bytes written to the hash function so far.
232 *
233 * Use: Returns the current state of the hash function such that
234 * it can be passed to @has160_set@.
235 */
236
237 unsigned long has160_state(has160_ctx *ctx, void *state)
238 {
239 octet *p = state;
240 STORE32_L(p + 0, ctx->a);
241 STORE32_L(p + 4, ctx->b);
242 STORE32_L(p + 8, ctx->c);
243 STORE32_L(p + 12, ctx->d);
244 STORE32_L(p + 16, ctx->e);
245 return (ctx->nl | ((ctx->nh << 16) << 16));
246 }
247
248 /* --- Generic interface --- */
249
250 GHASH_DEF(HAS160, has160)
251
252 /* --- Test code --- */
253
254 HASH_TEST(HAS160, has160)
255
256 /*----- That's all, folks -------------------------------------------------*/