3 * $Id: md5.c,v 1.2 1999/12/10 23:20:03 mdw Exp $
5 * The MD5 message digest function
7 * (c) 1998 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.2 1999/12/10 23:20:03 mdw
34 * New hash interface requirements.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
46 #include "ghash-def.h"
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @md5_compress@ --- *
54 * Arguments: @md5_ctx *ctx@ = pointer to context block
55 * @const void *sbuf@ = pointer to buffer of appropriate size
59 * Use: MD5 compression function.
62 void md5_compress(md5_ctx
*ctx
, const void *sbuf
)
67 /* --- Fetch the chaining variables --- */
74 /* --- Fetch the buffer contents --- */
80 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
84 /* --- Definitions for round functions --- */
86 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
87 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
88 #define H(x, y, z) ((x) ^ (y) ^ (z))
89 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
91 #define T(w, x, y, z, i, r, k, f) do { \
92 uint32 _t = w + f(x, y, z) + buf[i] + k; \
93 w = ROL32(_t, r) + x; \
96 #define FF(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, F)
97 #define GG(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, G)
98 #define HH(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, H)
99 #define II(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, I)
101 /* --- The main compression function --- */
103 FF(a
, b
, c
, d
, 0, 7, 0xd76aa478);
104 FF(d
, a
, b
, c
, 1, 12, 0xe8c7b756);
105 FF(c
, d
, a
, b
, 2, 17, 0x242070db);
106 FF(b
, c
, d
, a
, 3, 22, 0xc1bdceee);
107 FF(a
, b
, c
, d
, 4, 7, 0xf57c0faf);
108 FF(d
, a
, b
, c
, 5, 12, 0x4787c62a);
109 FF(c
, d
, a
, b
, 6, 17, 0xa8304613);
110 FF(b
, c
, d
, a
, 7, 22, 0xfd469501);
111 FF(a
, b
, c
, d
, 8, 7, 0x698098d8);
112 FF(d
, a
, b
, c
, 9, 12, 0x8b44f7af);
113 FF(c
, d
, a
, b
, 10, 17, 0xffff5bb1);
114 FF(b
, c
, d
, a
, 11, 22, 0x895cd7be);
115 FF(a
, b
, c
, d
, 12, 7, 0x6b901122);
116 FF(d
, a
, b
, c
, 13, 12, 0xfd987193);
117 FF(c
, d
, a
, b
, 14, 17, 0xa679438e);
118 FF(b
, c
, d
, a
, 15, 22, 0x49b40821);
120 GG(a
, b
, c
, d
, 1, 5, 0xf61e2562);
121 GG(d
, a
, b
, c
, 6, 9, 0xc040b340);
122 GG(c
, d
, a
, b
, 11, 14, 0x265e5a51);
123 GG(b
, c
, d
, a
, 0, 20, 0xe9b6c7aa);
124 GG(a
, b
, c
, d
, 5, 5, 0xd62f105d);
125 GG(d
, a
, b
, c
, 10, 9, 0x02441453);
126 GG(c
, d
, a
, b
, 15, 14, 0xd8a1e681);
127 GG(b
, c
, d
, a
, 4, 20, 0xe7d3fbc8);
128 GG(a
, b
, c
, d
, 9, 5, 0x21e1cde6);
129 GG(d
, a
, b
, c
, 14, 9, 0xc33707d6);
130 GG(c
, d
, a
, b
, 3, 14, 0xf4d50d87);
131 GG(b
, c
, d
, a
, 8, 20, 0x455a14ed);
132 GG(a
, b
, c
, d
, 13, 5, 0xa9e3e905);
133 GG(d
, a
, b
, c
, 2, 9, 0xfcefa3f8);
134 GG(c
, d
, a
, b
, 7, 14, 0x676f02d9);
135 GG(b
, c
, d
, a
, 12, 20, 0x8d2a4c8a);
137 HH(a
, b
, c
, d
, 5, 4, 0xfffa3942);
138 HH(d
, a
, b
, c
, 8, 11, 0x8771f681);
139 HH(c
, d
, a
, b
, 11, 16, 0x6d9d6122);
140 HH(b
, c
, d
, a
, 14, 23, 0xfde5380c);
141 HH(a
, b
, c
, d
, 1, 4, 0xa4beea44);
142 HH(d
, a
, b
, c
, 4, 11, 0x4bdecfa9);
143 HH(c
, d
, a
, b
, 7, 16, 0xf6bb4b60);
144 HH(b
, c
, d
, a
, 10, 23, 0xbebfbc70);
145 HH(a
, b
, c
, d
, 13, 4, 0x289b7ec6);
146 HH(d
, a
, b
, c
, 0, 11, 0xeaa127fa);
147 HH(c
, d
, a
, b
, 3, 16, 0xd4ef3085);
148 HH(b
, c
, d
, a
, 6, 23, 0x04881d05);
149 HH(a
, b
, c
, d
, 9, 4, 0xd9d4d039);
150 HH(d
, a
, b
, c
, 12, 11, 0xe6db99e5);
151 HH(c
, d
, a
, b
, 15, 16, 0x1fa27cf8);
152 HH(b
, c
, d
, a
, 2, 23, 0xc4ac5665);
154 II(a
, b
, c
, d
, 0, 6, 0xf4292244);
155 II(d
, a
, b
, c
, 7, 10, 0x432aff97);
156 II(c
, d
, a
, b
, 14, 15, 0xab9423a7);
157 II(b
, c
, d
, a
, 5, 21, 0xfc93a039);
158 II(a
, b
, c
, d
, 12, 6, 0x655b59c3);
159 II(d
, a
, b
, c
, 3, 10, 0x8f0ccc92);
160 II(c
, d
, a
, b
, 10, 15, 0xffeff47d);
161 II(b
, c
, d
, a
, 1, 21, 0x85845dd1);
162 II(a
, b
, c
, d
, 8, 6, 0x6fa87e4f);
163 II(d
, a
, b
, c
, 15, 10, 0xfe2ce6e0);
164 II(c
, d
, a
, b
, 6, 15, 0xa3014314);
165 II(b
, c
, d
, a
, 13, 21, 0x4e0811a1);
166 II(a
, b
, c
, d
, 4, 6, 0xf7537e82);
167 II(d
, a
, b
, c
, 11, 10, 0xbd3af235);
168 II(c
, d
, a
, b
, 2, 15, 0x2ad7d2bb);
169 II(b
, c
, d
, a
, 9, 21, 0xeb86d391);
171 /* --- Update the chaining variables --- */
179 /* --- @md5_init@ --- *
181 * Arguments: @md5_ctx *ctx@ = pointer to context block to initialize
185 * Use: Initializes a context block ready for hashing.
188 void md5_init(md5_ctx
*ctx
)
195 ctx
->nl
= ctx
->nh
= 0;
198 /* --- @md5_set@ --- *
200 * Arguments: @md5_ctx *ctx@ = pointer to context block
201 * @const void *buf@ = pointer to state buffer
202 * @unsigned long count@ = current count of bytes processed
206 * Use: Initializes a context block from a given state. This is
207 * useful in cases where the initial hash state is meant to be
208 * secret, e.g., for NMAC and HMAC support.
211 void md5_set(md5_ctx
*ctx
, const void *buf
, unsigned long count
)
213 const octet
*p
= buf
;
214 ctx
->a
= LOAD32_L(p
+ 0);
215 ctx
->b
= LOAD32_L(p
+ 4);
216 ctx
->c
= LOAD32_L(p
+ 8);
217 ctx
->d
= LOAD32_L(p
+ 12);
219 ctx
->nl
= U32(count
);
220 ctx
->nh
= U32((count
>> 16) >> 16);
223 /* --- @md5_hash@ --- *
225 * Arguments: @md5_ctx *ctx@ = pointer to context block
226 * @const void *buf@ = buffer of data to hash
227 * @size_t sz@ = size of buffer to hash
231 * Use: Hashes a buffer of data. The buffer may be of any size and
235 void md5_hash(md5_ctx
*ctx
, const void *buf
, size_t sz
)
237 HASH_BUFFER(MD5
, md5
, ctx
, buf
, sz
);
240 /* --- @md5_done@ --- *
242 * Arguments: @md5_ctx *ctx@ = pointer to context block
243 * @void *hash@ = pointer to output buffer
247 * Use: Returns the hash of the data read so far.
250 void md5_done(md5_ctx
*ctx
, void *hash
)
253 HASH_MD5STRENGTH(MD5
, md5
, ctx
);
254 STORE32_L(p
+ 0, ctx
->a
);
255 STORE32_L(p
+ 4, ctx
->b
);
256 STORE32_L(p
+ 8, ctx
->c
);
257 STORE32_L(p
+ 12, ctx
->d
);
260 /* --- @md5_state@ --- *
262 * Arguments: @md5_ctx *ctx@ = pointer to context
263 * @void *state@ = pointer to buffer for current state
265 * Returns: Number of bytes written to the hash function so far.
267 * Use: Returns the current state of the hash function such that
268 * it can be passed to @md5_set@.
271 unsigned long md5_state(md5_ctx
*ctx
, void *state
)
274 STORE32_L(p
+ 0, ctx
->a
);
275 STORE32_L(p
+ 4, ctx
->b
);
276 STORE32_L(p
+ 8, ctx
->c
);
277 STORE32_L(p
+ 12, ctx
->d
);
278 return (ctx
->nl
| ((ctx
->nh
<< 16) << 16));
281 /* --- Generic interface --- */
285 /* --- Test code --- */
289 /*----- That's all, folks -------------------------------------------------*/