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