Portability fix for broken compilers.
[u/mdw/catacomb] / md5.c
CommitLineData
d03ab969 1/* -*-c-*-
2 *
5d59cd77 3 * $Id: md5.c,v 1.3 2000/06/17 11:31:43 mdw Exp $
d03ab969 4 *
5 * The MD5 message digest function
6 *
7 * (c) 1998 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: md5.c,v $
5d59cd77 33 * Revision 1.3 2000/06/17 11:31:43 mdw
34 * Portability fix for broken compilers.
35 *
dc2f0497 36 * Revision 1.2 1999/12/10 23:20:03 mdw
37 * New hash interface requirements.
38 *
d03ab969 39 * Revision 1.1 1999/09/03 08:41:12 mdw
40 * Initial import.
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46#include <mLib/bits.h>
47
dc2f0497 48#include "ghash.h"
49#include "ghash-def.h"
d03ab969 50#include "hash.h"
51#include "md5.h"
52
53/*----- Main code ---------------------------------------------------------*/
54
55/* --- @md5_compress@ --- *
56 *
57 * Arguments: @md5_ctx *ctx@ = pointer to context block
58 * @const void *sbuf@ = pointer to buffer of appropriate size
59 *
60 * Returns: ---
61 *
62 * Use: MD5 compression function.
63 */
64
65void md5_compress(md5_ctx *ctx, const void *sbuf)
66{
67 uint32 a, b, c, d;
68 uint32 buf[16];
69
70 /* --- Fetch the chaining variables --- */
71
72 a = ctx->a;
73 b = ctx->b;
74 c = ctx->c;
75 d = ctx->d;
76
77 /* --- Fetch the buffer contents --- */
78
79 {
80 int i;
81 const octet *p;
82
83 for (i = 0, p = sbuf; i < 16; i++, p += 4)
84 buf[i] = LOAD32_L(p);
85 }
86
87 /* --- Definitions for round functions --- */
88
89#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
90#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
91#define H(x, y, z) ((x) ^ (y) ^ (z))
92#define I(x, y, z) ((y) ^ ((x) | ~(z)))
93
94#define T(w, x, y, z, i, r, k, f) do { \
95 uint32 _t = w + f(x, y, z) + buf[i] + k; \
96 w = ROL32(_t, r) + x; \
97} while (0)
98
99#define FF(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, F)
100#define GG(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, G)
101#define HH(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, H)
102#define II(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, I)
103
104 /* --- The main compression function --- */
105
106 FF(a, b, c, d, 0, 7, 0xd76aa478);
107 FF(d, a, b, c, 1, 12, 0xe8c7b756);
108 FF(c, d, a, b, 2, 17, 0x242070db);
109 FF(b, c, d, a, 3, 22, 0xc1bdceee);
110 FF(a, b, c, d, 4, 7, 0xf57c0faf);
111 FF(d, a, b, c, 5, 12, 0x4787c62a);
112 FF(c, d, a, b, 6, 17, 0xa8304613);
113 FF(b, c, d, a, 7, 22, 0xfd469501);
114 FF(a, b, c, d, 8, 7, 0x698098d8);
115 FF(d, a, b, c, 9, 12, 0x8b44f7af);
116 FF(c, d, a, b, 10, 17, 0xffff5bb1);
117 FF(b, c, d, a, 11, 22, 0x895cd7be);
118 FF(a, b, c, d, 12, 7, 0x6b901122);
119 FF(d, a, b, c, 13, 12, 0xfd987193);
120 FF(c, d, a, b, 14, 17, 0xa679438e);
121 FF(b, c, d, a, 15, 22, 0x49b40821);
122
123 GG(a, b, c, d, 1, 5, 0xf61e2562);
124 GG(d, a, b, c, 6, 9, 0xc040b340);
125 GG(c, d, a, b, 11, 14, 0x265e5a51);
126 GG(b, c, d, a, 0, 20, 0xe9b6c7aa);
127 GG(a, b, c, d, 5, 5, 0xd62f105d);
128 GG(d, a, b, c, 10, 9, 0x02441453);
129 GG(c, d, a, b, 15, 14, 0xd8a1e681);
130 GG(b, c, d, a, 4, 20, 0xe7d3fbc8);
131 GG(a, b, c, d, 9, 5, 0x21e1cde6);
132 GG(d, a, b, c, 14, 9, 0xc33707d6);
133 GG(c, d, a, b, 3, 14, 0xf4d50d87);
134 GG(b, c, d, a, 8, 20, 0x455a14ed);
135 GG(a, b, c, d, 13, 5, 0xa9e3e905);
136 GG(d, a, b, c, 2, 9, 0xfcefa3f8);
137 GG(c, d, a, b, 7, 14, 0x676f02d9);
138 GG(b, c, d, a, 12, 20, 0x8d2a4c8a);
139
140 HH(a, b, c, d, 5, 4, 0xfffa3942);
141 HH(d, a, b, c, 8, 11, 0x8771f681);
142 HH(c, d, a, b, 11, 16, 0x6d9d6122);
143 HH(b, c, d, a, 14, 23, 0xfde5380c);
144 HH(a, b, c, d, 1, 4, 0xa4beea44);
145 HH(d, a, b, c, 4, 11, 0x4bdecfa9);
146 HH(c, d, a, b, 7, 16, 0xf6bb4b60);
147 HH(b, c, d, a, 10, 23, 0xbebfbc70);
148 HH(a, b, c, d, 13, 4, 0x289b7ec6);
149 HH(d, a, b, c, 0, 11, 0xeaa127fa);
150 HH(c, d, a, b, 3, 16, 0xd4ef3085);
151 HH(b, c, d, a, 6, 23, 0x04881d05);
152 HH(a, b, c, d, 9, 4, 0xd9d4d039);
153 HH(d, a, b, c, 12, 11, 0xe6db99e5);
154 HH(c, d, a, b, 15, 16, 0x1fa27cf8);
155 HH(b, c, d, a, 2, 23, 0xc4ac5665);
156
157 II(a, b, c, d, 0, 6, 0xf4292244);
158 II(d, a, b, c, 7, 10, 0x432aff97);
159 II(c, d, a, b, 14, 15, 0xab9423a7);
160 II(b, c, d, a, 5, 21, 0xfc93a039);
161 II(a, b, c, d, 12, 6, 0x655b59c3);
162 II(d, a, b, c, 3, 10, 0x8f0ccc92);
163 II(c, d, a, b, 10, 15, 0xffeff47d);
164 II(b, c, d, a, 1, 21, 0x85845dd1);
165 II(a, b, c, d, 8, 6, 0x6fa87e4f);
166 II(d, a, b, c, 15, 10, 0xfe2ce6e0);
167 II(c, d, a, b, 6, 15, 0xa3014314);
168 II(b, c, d, a, 13, 21, 0x4e0811a1);
169 II(a, b, c, d, 4, 6, 0xf7537e82);
170 II(d, a, b, c, 11, 10, 0xbd3af235);
171 II(c, d, a, b, 2, 15, 0x2ad7d2bb);
172 II(b, c, d, a, 9, 21, 0xeb86d391);
173
174 /* --- Update the chaining variables --- */
175
176 ctx->a += a;
177 ctx->b += b;
178 ctx->c += c;
179 ctx->d += d;
180}
181
182/* --- @md5_init@ --- *
183 *
184 * Arguments: @md5_ctx *ctx@ = pointer to context block to initialize
185 *
186 * Returns: ---
187 *
188 * Use: Initializes a context block ready for hashing.
189 */
190
191void md5_init(md5_ctx *ctx)
192{
193 ctx->a = 0x67452301;
194 ctx->b = 0xefcdab89;
195 ctx->c = 0x98badcfe;
196 ctx->d = 0x10325476;
197 ctx->off = 0;
dc2f0497 198 ctx->nl = ctx->nh = 0;
d03ab969 199}
200
201/* --- @md5_set@ --- *
202 *
203 * Arguments: @md5_ctx *ctx@ = pointer to context block
204 * @const void *buf@ = pointer to state buffer
205 * @unsigned long count@ = current count of bytes processed
206 *
207 * Returns: ---
208 *
209 * Use: Initializes a context block from a given state. This is
210 * useful in cases where the initial hash state is meant to be
211 * secret, e.g., for NMAC and HMAC support.
212 */
213
214void md5_set(md5_ctx *ctx, const void *buf, unsigned long count)
215{
216 const octet *p = buf;
217 ctx->a = LOAD32_L(p + 0);
218 ctx->b = LOAD32_L(p + 4);
219 ctx->c = LOAD32_L(p + 8);
220 ctx->d = LOAD32_L(p + 12);
221 ctx->off = 0;
dc2f0497 222 ctx->nl = U32(count);
5d59cd77 223 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
d03ab969 224}
225
226/* --- @md5_hash@ --- *
227 *
228 * Arguments: @md5_ctx *ctx@ = pointer to context block
229 * @const void *buf@ = buffer of data to hash
230 * @size_t sz@ = size of buffer to hash
231 *
232 * Returns: ---
233 *
234 * Use: Hashes a buffer of data. The buffer may be of any size and
235 * alignment.
236 */
237
238void md5_hash(md5_ctx *ctx, const void *buf, size_t sz)
239{
240 HASH_BUFFER(MD5, md5, ctx, buf, sz);
241}
242
243/* --- @md5_done@ --- *
244 *
245 * Arguments: @md5_ctx *ctx@ = pointer to context block
246 * @void *hash@ = pointer to output buffer
247 *
248 * Returns: ---
249 *
250 * Use: Returns the hash of the data read so far.
251 */
252
253void md5_done(md5_ctx *ctx, void *hash)
254{
255 octet *p = hash;
256 HASH_MD5STRENGTH(MD5, md5, ctx);
257 STORE32_L(p + 0, ctx->a);
258 STORE32_L(p + 4, ctx->b);
259 STORE32_L(p + 8, ctx->c);
260 STORE32_L(p + 12, ctx->d);
261}
262
263/* --- @md5_state@ --- *
264 *
265 * Arguments: @md5_ctx *ctx@ = pointer to context
266 * @void *state@ = pointer to buffer for current state
267 *
268 * Returns: Number of bytes written to the hash function so far.
269 *
270 * Use: Returns the current state of the hash function such that
271 * it can be passed to @md5_set@.
272 */
273
274unsigned long md5_state(md5_ctx *ctx, void *state)
275{
276 octet *p = state;
277 STORE32_L(p + 0, ctx->a);
278 STORE32_L(p + 4, ctx->b);
279 STORE32_L(p + 8, ctx->c);
280 STORE32_L(p + 12, ctx->d);
dc2f0497 281 return (ctx->nl | ((ctx->nh << 16) << 16));
d03ab969 282}
283
dc2f0497 284/* --- Generic interface --- */
285
286GHASH_DEF(MD5, md5)
287
d03ab969 288/* --- Test code --- */
289
290HASH_TEST(MD5, md5)
291
292/*----- That's all, folks -------------------------------------------------*/