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