UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
md5.c
Go to the documentation of this file.
1/*
2 * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
3 *
4 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
5 * reserved.
6 *
7 * License to copy and use this software is granted provided that it is
8 * identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm"
9 * in all material mentioning or referencing this software or this function.
10 *
11 * License is also granted to make and use derivative works provided that such
12 * works are identified as "derived from the RSA Data Security, Inc. MD5
13 * Message-Digest Algorithm" in all material mentioning or referencing the
14 * derived work.
15 *
16 * RSA Data Security, Inc. makes no representations concerning either the
17 * merchantability of this software or the suitability of this software for
18 * any particular purpose. It is provided "as is" without express or implied
19 * warranty of any kind.
20 *
21 * These notices must be retained in any copies of any part of this
22 * documentation and/or software.
23 *
24 * $Revision: 1.1 $
25 * $Date: 2007/11/08 09:48:59 $
26 *
27 */
28
29#include "config.h"
30#include "config_unix.h"
31#include "config_win32.h"
32#include "md5.h"
33
34/*
35 * Constants for MD5Transform routine.
36 */
37#define S11 7
38#define S12 12
39#define S13 17
40#define S14 22
41#define S21 5
42#define S22 9
43#define S23 14
44#define S24 20
45#define S31 4
46#define S32 11
47#define S33 16
48#define S34 23
49#define S41 6
50#define S42 10
51#define S43 15
52#define S44 21
53
54static unsigned char PADDING[64] = {
55 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
58};
59
60/*
61 * F, G, H and I are basic MD5 functions.
62 */
63#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
64#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
65#define H(x, y, z) ((x) ^ (y) ^ (z))
66#define I(x, y, z) ((y) ^ ((x) | (~z)))
67
68/*
69 * MD5_ROTATE_LEFT rotates x left n bits.
70 */
71#define MD5_ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
72
73/*
74 * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is
75 * separate from addition to prevent recomputation.
76 */
77#define FF(a, b, c, d, x, s, ac) { \
78 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
79 (a) = MD5_ROTATE_LEFT ((a), (s)); \
80 (a) += (b); \
81 }
82#define GG(a, b, c, d, x, s, ac) { \
83 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
84 (a) = MD5_ROTATE_LEFT ((a), (s)); \
85 (a) += (b); \
86 }
87#define HH(a, b, c, d, x, s, ac) { \
88 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
89 (a) = MD5_ROTATE_LEFT ((a), (s)); \
90 (a) += (b); \
91 }
92#define II(a, b, c, d, x, s, ac) { \
93 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
94 (a) = MD5_ROTATE_LEFT ((a), (s)); \
95 (a) += (b); \
96 }
97
98/*
99 * Encodes input (uint32_t) into output (unsigned char). Assumes len is a
100 * multiple of 4.
101 */
102static void Encode(unsigned char *output, uint32_t * input, unsigned int len)
103{
104 unsigned int i, j;
105
106 assert((len % 4) == 0);
107
108 for (i = 0, j = 0; j < len; i++, j += 4) {
109 output[j] = (unsigned char)(input[i] & 0xff);
110 output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
111 output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
112 output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
113 }
114}
115
116/*
117 * Decodes input (unsigned char) into output (uint32_t). Assumes len is a
118 * multiple of 4.
119 */
120static void Decode(uint32_t * output, const unsigned char *input, unsigned int len)
121{
122 unsigned int i, j;
123
124 for (i = 0, j = 0; j < len; i++, j += 4)
125 output[i] =
126 ((uint32_t) input[j]) | (((uint32_t) input[j + 1]) << 8) |
127 (((uint32_t) input[j + 2]) << 16) |
128 (((uint32_t) input[j + 3]) << 24);
129}
130
131/*
132 * MD5 basic transformation. Transforms state based on block.
133 */
134static void MD5Transform(uint32_t state[4], const unsigned char block[64])
135{
136 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
137
138 Decode(x, block, 64);
139
140 /* Round 1 */
141 FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
142 FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
143 FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
144 FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
145 FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
146 FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
147 FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
148 FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
149 FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
150 FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
151 FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
152 FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
153 FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
154 FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
155 FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
156 FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
157
158 /* Round 2 */
159 GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
160 GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
161 GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
162 GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
163 GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
164 GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
165 GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
166 GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
167 GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
168 GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
169 GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
170 GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
171 GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
172 GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
173 GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
174 GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
175
176 /* Round 3 */
177 HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
178 HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
179 HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
180 HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
181 HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
182 HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
183 HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
184 HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
185 HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
186 HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
187 HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
188 HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
189 HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
190 HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
191 HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
192 HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
193
194 /* Round 4 */
195 II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
196 II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
197 II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
198 II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
199 II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
200 II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
201 II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
202 II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
203 II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
204 II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
205 II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
206 II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
207 II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
208 II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
209 II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
210 II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
211
212 state[0] += a;
213 state[1] += b;
214 state[2] += c;
215 state[3] += d;
216
217 /*
218 * Zeroize sensitive information.
219 */
220 memset((unsigned char *)x, 0, sizeof(x));
221}
222
229void MD5Init(MD5CTX * context)
230{
231 context->count[0] = context->count[1] = 0;
232 /* Load magic initialization constants. */
233 context->state[0] = 0x67452301;
234 context->state[1] = 0xefcdab89;
235 context->state[2] = 0x98badcfe;
236 context->state[3] = 0x10325476;
237}
238
249void MD5Update(MD5CTX * context, const unsigned char *input, unsigned int inputLen)
250{
251 unsigned int i, index, partLen;
252
253 /* Compute number of bytes mod 64 */
254 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
255
256 /* Update number of bits */
257 if ((context->count[0] +=
258 ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
259 context->count[1]++;
260 }
261 context->count[1] += ((uint32_t) inputLen >> 29);
262
263 partLen = 64 - index;
264
265 /* Transform as many times as possible. */
266 if (inputLen >= partLen) {
267 memcpy((unsigned char *)&context->buffer[index],
268 (const unsigned char *)input, partLen);
269 MD5Transform(context->state, context->buffer);
270
271 for (i = partLen; i + 63 < inputLen; i += 64) {
272 MD5Transform(context->state, &input[i]);
273 }
274 index = 0;
275 } else {
276 i = 0;
277 }
278 /* Buffer remaining input */
279 if ((inputLen - i) != 0) {
280 memcpy((unsigned char *)&context->buffer[index],
281 (const unsigned char *)&input[i], inputLen - i);
282 }
283}
284
295void MD5Final(unsigned char digest[16], MD5CTX * context)
296{
297 unsigned char bits[8];
298 unsigned int index, padLen;
299
300 /* Save number of bits */
301 Encode(bits, context->count, 8);
302
303 /*
304 * Pad out to 56 mod 64.
305 */
306 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
307 padLen = (index < 56) ? (56 - index) : (120 - index);
308 MD5Update(context, PADDING, padLen);
309
310 /* Append length (before padding) */
311 MD5Update(context, bits, 8);
312 /* Store state in digest */
313 Encode(digest, context->state, 16);
314
315 /*
316 * Zeroize sensitive information.
317 */
318 memset((unsigned char *)context, 0, sizeof(*context));
319}
void ** state
int void size_t * len
#define FF(a, b, c, d, x, s, ac)
Definition md5.c:77
#define S24
Definition md5.c:44
#define S33
Definition md5.c:47
#define S32
Definition md5.c:46
#define S12
Definition md5.c:38
#define S42
Definition md5.c:50
static void MD5Transform(uint32_t state[4], const unsigned char block[64])
Definition md5.c:134
void MD5Update(MD5CTX *context, const unsigned char *input, unsigned int inputLen)
Definition md5.c:249
#define S11
Definition md5.c:37
#define S43
Definition md5.c:51
#define S23
Definition md5.c:43
#define GG(a, b, c, d, x, s, ac)
Definition md5.c:82
#define S44
Definition md5.c:52
static void Decode(uint32_t *output, const unsigned char *input, unsigned int len)
Definition md5.c:120
#define S14
Definition md5.c:40
#define HH(a, b, c, d, x, s, ac)
Definition md5.c:87
#define S13
Definition md5.c:39
void MD5Final(unsigned char digest[16], MD5CTX *context)
Definition md5.c:295
#define S41
Definition md5.c:49
#define S21
Definition md5.c:41
#define II(a, b, c, d, x, s, ac)
Definition md5.c:92
#define S22
Definition md5.c:42
static unsigned char PADDING[64]
Definition md5.c:54
#define S31
Definition md5.c:45
void MD5Init(MD5CTX *context)
Definition md5.c:229
#define S34
Definition md5.c:48
static void Encode(unsigned char *output, uint32_t *input, unsigned int len)
Definition md5.c:102
Definition md5.h:34
uint32_t state[4]
Definition md5.h:35
unsigned char buffer[64]
Definition md5.h:37
uint32_t count[2]
Definition md5.h:36