7c7b7b99fc68f601f1b4ea04f8dbb5a8f776b2bd
3 * $Id: has160.c,v 1.1 2004/04/04 19:42:59 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2004/04/04 19:42:59 mdw
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
43 #include "ghash-def.h"
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @has160_compress@ --- *
51 * Arguments: @has160_ctx *ctx@ = pointer to context block
52 * @const void *sbuf@ = pointer to buffer of appropriate size
56 * Use: HAS160 compression function.
59 void has160_compress(has160_ctx
*ctx
, const void *sbuf
)
64 /* --- Fetch the chaining variables --- */
72 /* --- Fetch the buffer contents --- */
78 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
82 /* --- Definitions for round functions --- */
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)))
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; \
93 /* --- The actual hashing --- *
95 * Hmm, this is more regular than most. The macros are quite grim,
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); \
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);
133 /* --- Update the chaining variables --- */
142 /* --- @has160_init@ --- *
144 * Arguments: @has160_ctx *ctx@ = pointer to context block to initialize
148 * Use: Initializes a context block ready for hashing.
151 void has160_init(has160_ctx
*ctx
)
159 ctx
->nl
= ctx
->nh
= 0;
162 /* --- @has160_set@ --- *
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
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.
175 void has160_set(has160_ctx
*ctx
, const void *buf
, unsigned long count
)
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);
184 ctx
->nl
= U32(count
);
185 ctx
->nh
= U32(((count
& ~MASK32
) >> 16) >> 16);
188 /* --- @has160_hash@ --- *
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
196 * Use: Hashes a buffer of data. The buffer may be of any size and
200 void has160_hash(has160_ctx
*ctx
, const void *buf
, size_t sz
)
202 HASH_BUFFER(HAS160
, has160
, ctx
, buf
, sz
);
205 /* --- @has160_done@ --- *
207 * Arguments: @has160_ctx *ctx@ = pointer to context block
208 * @void *hash@ = pointer to output buffer
212 * Use: Returns the hash of the data read so far.
215 void has160_done(has160_ctx
*ctx
, void *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
);
226 /* --- @has160_state@ --- *
228 * Arguments: @has160_ctx *ctx@ = pointer to context
229 * @void *state@ = pointer to buffer for current state
231 * Returns: Number of bytes written to the hash function so far.
233 * Use: Returns the current state of the hash function such that
234 * it can be passed to @has160_set@.
237 unsigned long has160_state(has160_ctx
*ctx
, void *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));
248 /* --- Generic interface --- */
250 GHASH_DEF(HAS160
, has160
)
252 /* --- Test code --- */
254 HASH_TEST(HAS160
, has160
)
256 /*----- That's all, folks -------------------------------------------------*/