3 * $Id: has160.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
5 * The HAS160 message digest function
7 * (c) 2004 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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/bits.h>
35 #include "ghash-def.h"
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @has160_compress@ --- *
43 * Arguments: @has160_ctx *ctx@ = pointer to context block
44 * @const void *sbuf@ = pointer to buffer of appropriate size
48 * Use: HAS160 compression function.
51 void has160_compress(has160_ctx
*ctx
, const void *sbuf
)
56 /* --- Fetch the chaining variables --- */
64 /* --- Fetch the buffer contents --- */
70 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
74 /* --- Definitions for round functions --- */
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)))
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; \
85 /* --- The actual hashing --- *
87 * Hmm, this is more regular than most. The macros are quite grim,
91 #define ROUND(f, ss, k, \
92 i0, i1, i2, i3, i4, i5, i6, i7, \
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); \
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); \
99 FF(f, a, b, c, d, e, buf[i12]^buf[i13]^buf[i14]^buf[i15], 13, ss, k); \
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); \
104 FF(f, a, b, c, d, e, buf[ i0]^buf[ i1]^buf[ i2]^buf[ i3], 9, ss, k); \
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); \
109 FF(f, a, b, c, d, e, buf[ i4]^buf[ i5]^buf[ i6]^buf[ i7], 12, ss, k); \
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); \
116 ROUND(F
, 10, 0x00000000,
117 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
118 ROUND(G
, 17, 0x5a827999,
119 3, 6, 9, 12, 15, 2, 5, 8, 11, 14, 1, 4, 7, 10, 13, 0);
120 ROUND(H
, 25, 0x6ed9eba1,
121 12, 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3);
122 ROUND(G
, 30, 0x8f1bbcdc,
123 7, 2, 13, 8, 3, 14, 9, 4, 15, 10, 5, 0, 11, 6, 1, 12);
125 /* --- Update the chaining variables --- */
134 /* --- @has160_init@ --- *
136 * Arguments: @has160_ctx *ctx@ = pointer to context block to initialize
140 * Use: Initializes a context block ready for hashing.
143 void has160_init(has160_ctx
*ctx
)
151 ctx
->nl
= ctx
->nh
= 0;
154 /* --- @has160_set@ --- *
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
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.
167 void has160_set(has160_ctx
*ctx
, const void *buf
, unsigned long count
)
169 const octet
*p
= buf
;
170 ctx
->a
= LOAD32_L(p
+ 0);
171 ctx
->b
= LOAD32_L(p
+ 4);
172 ctx
->c
= LOAD32_L(p
+ 8);
173 ctx
->d
= LOAD32_L(p
+ 12);
174 ctx
->e
= LOAD32_L(p
+ 16);
176 ctx
->nl
= U32(count
);
177 ctx
->nh
= U32(((count
& ~MASK32
) >> 16) >> 16);
180 /* --- @has160_hash@ --- *
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
188 * Use: Hashes a buffer of data. The buffer may be of any size and
192 void has160_hash(has160_ctx
*ctx
, const void *buf
, size_t sz
)
194 HASH_BUFFER(HAS160
, has160
, ctx
, buf
, sz
);
197 /* --- @has160_done@ --- *
199 * Arguments: @has160_ctx *ctx@ = pointer to context block
200 * @void *hash@ = pointer to output buffer
204 * Use: Returns the hash of the data read so far.
207 void has160_done(has160_ctx
*ctx
, void *hash
)
210 HASH_MD5STRENGTH(HAS160
, has160
, ctx
);
211 STORE32_L(p
+ 0, ctx
->a
);
212 STORE32_L(p
+ 4, ctx
->b
);
213 STORE32_L(p
+ 8, ctx
->c
);
214 STORE32_L(p
+ 12, ctx
->d
);
215 STORE32_L(p
+ 16, ctx
->e
);
218 /* --- @has160_state@ --- *
220 * Arguments: @has160_ctx *ctx@ = pointer to context
221 * @void *state@ = pointer to buffer for current state
223 * Returns: Number of bytes written to the hash function so far.
225 * Use: Returns the current state of the hash function such that
226 * it can be passed to @has160_set@.
229 unsigned long has160_state(has160_ctx
*ctx
, void *state
)
232 STORE32_L(p
+ 0, ctx
->a
);
233 STORE32_L(p
+ 4, ctx
->b
);
234 STORE32_L(p
+ 8, ctx
->c
);
235 STORE32_L(p
+ 12, ctx
->d
);
236 STORE32_L(p
+ 16, ctx
->e
);
237 return (ctx
->nl
| ((ctx
->nh
<< 16) << 16));
240 /* --- Generic interface --- */
242 GHASH_DEF(HAS160
, has160
)
244 /* --- Test code --- */
246 HASH_TEST(HAS160
, has160
)
248 /*----- That's all, folks -------------------------------------------------*/