All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
md5.c
Go to the documentation of this file.
1 /**
2  * $Id: 0445be8754726bedf279b923f01871e54824d0f4 $
3  *
4  * @note license is LGPL, but largely derived from a public domain source.
5  *
6  * @file md5.c
7  * @brief md5 digest functions.
8  */
9 
10 RCSID("$Id: 0445be8754726bedf279b923f01871e54824d0f4 $")
11 
12 #include <freeradius-devel/libradius.h>
13 
14 
15 
16 /*
17  * FORCE MD5 TO USE OUR MD5 HEADER FILE!
18  * If we don't do this, it might pick up the systems broken MD5.
19  */
20 #include <freeradius-devel/md5.h>
21 
22 /** Calculate the MD5 hash of the contents of a buffer
23  *
24  * @param[out] out Where to write the MD5 digest. Must be a minimum of MD5_DIGEST_LENGTH.
25  * @param[in] in Data to hash.
26  * @param[in] inlen Length of the data.
27  */
28 void fr_md5_calc(uint8_t *out, uint8_t const *in, size_t inlen)
29 {
30  FR_MD5_CTX ctx;
31 
32  fr_md5_init(&ctx);
33  fr_md5_update(&ctx, in, inlen);
34  fr_md5_final(out, &ctx);
35 }
36 
37 #ifndef HAVE_OPENSSL_EVP_H
38 /*
39  * This code implements the MD5 message-digest algorithm.
40  * The algorithm is due to Ron Rivest. This code was
41  * written by Colin Plumb in 1993, no copyright is claimed.
42  * This code is in the public domain; do with it what you wish.
43  *
44  * Equivalent code is available from RSA Data Security, Inc.
45  * This code has been tested against that, and is equivalent,
46  * except that you don't need to include two pages of legalese
47  * with every copy.
48  *
49  * To compute the message digest of a chunk of bytes, declare an
50  * MD5Context structure, pass it to fr_md5_init, call fr_md5_update as
51  * needed on buffers full of bytes, and then call fr_md5_final, which
52  * will fill a supplied 16-byte array with the digest.
53  */
54 #define PUT_64BIT_LE(cp, value) do {\
55  (cp)[7] = (value)[1] >> 24;\
56  (cp)[6] = (value)[1] >> 16;\
57  (cp)[5] = (value)[1] >> 8;\
58  (cp)[4] = (value)[1];\
59  (cp)[3] = (value)[0] >> 24;\
60  (cp)[2] = (value)[0] >> 16;\
61  (cp)[1] = (value)[0] >> 8;\
62  (cp)[0] = (value)[0];\
63 } while (0)
64 
65 #define PUT_32BIT_LE(cp, value) do {\
66  (cp)[3] = (value) >> 24;\
67  (cp)[2] = (value) >> 16;\
68  (cp)[1] = (value) >> 8;\
69  (cp)[0] = (value);\
70 } while (0)
71 
72 static const uint8_t PADDING[MD5_BLOCK_LENGTH] = {
73  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
76 };
77 
78 /** Initialise a new MD5 context
79  *
80  * Set bit count to 0 and buffer to mysterious initialization constants.
81  *
82  * @param[out] ctx to initialise.
83  */
85 {
86  ctx->count[0] = 0;
87  ctx->count[1] = 0;
88  ctx->state[0] = 0x67452301;
89  ctx->state[1] = 0xefcdab89;
90  ctx->state[2] = 0x98badcfe;
91  ctx->state[3] = 0x10325476;
92 }
93 
94 /** Feed additional data into the MD5 hashing function
95  *
96  * @param[in,out] ctx to update.
97  * @param[in] in Data to hash.
98  * @param[in] inlen Length of the data.
99  */
100 void fr_md5_update(FR_MD5_CTX *ctx, uint8_t const *in, size_t inlen)
101 {
102  size_t have, need;
103 
104  /* Check how many bytes we already have and how many more we need. */
105  have = (size_t)((ctx->count[0] >> 3) & (MD5_BLOCK_LENGTH - 1));
106  need = MD5_BLOCK_LENGTH - have;
107 
108  /* Update bitcount */
109 /* ctx->count += (uint64_t)inlen << 3;*/
110  if ((ctx->count[0] += ((uint32_t)inlen << 3)) < (uint32_t)inlen) {
111  /* Overflowed ctx->count[0] */
112  ctx->count[1]++;
113  }
114  ctx->count[1] += ((uint32_t)inlen >> 29);
115 
116  if (inlen >= need) {
117  if (have != 0) {
118  memcpy(ctx->buffer + have, in, need);
119  fr_md5_transform(ctx->state, ctx->buffer);
120  in += need;
121  inlen -= need;
122  have = 0;
123  }
124 
125  /* Process data in MD5_BLOCK_LENGTH-byte chunks. */
126  while (inlen >= MD5_BLOCK_LENGTH) {
127  fr_md5_transform(ctx->state, in);
128  in += MD5_BLOCK_LENGTH;
129  inlen -= MD5_BLOCK_LENGTH;
130  }
131  }
132 
133  /* Handle any remaining bytes of data. */
134  if (inlen != 0) memcpy(ctx->buffer + have, in, inlen);
135 }
136 
137 /** Finalise the MD5 context and write out the hash
138  *
139  * Final wrapup - pad to 64-byte boundary with the bit pattern 1 0*
140  * (64-bit count of bits processed, MSB-first).
141  *
142  * @param[out] out Where to write the MD5 digest. Minimum length of MD5_DIGEST_LENGTH.
143  * @param[in,out] ctx to finalise.
144  */
145 void fr_md5_final(uint8_t out[MD5_DIGEST_LENGTH], FR_MD5_CTX *ctx)
146 {
147  uint8_t count[8];
148  size_t padlen;
149  int i;
150 
151  /* Convert count to 8 bytes in little endian order. */
152  PUT_64BIT_LE(count, ctx->count);
153 
154  /* Pad out to 56 mod 64. */
155  padlen = MD5_BLOCK_LENGTH -
156  ((ctx->count[0] >> 3) & (MD5_BLOCK_LENGTH - 1));
157  if (padlen < 1 + 8)
158  padlen += MD5_BLOCK_LENGTH;
159  fr_md5_update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
160  fr_md5_update(ctx, count, 8);
161 
162  if (out != NULL) {
163  for (i = 0; i < 4; i++)
164  PUT_32BIT_LE(out + i * 4, ctx->state[i]);
165  }
166  memset(ctx, 0, sizeof(*ctx)); /* in case it's sensitive */
167 }
168 
169 /* The four core functions - F1 is optimized somewhat */
170 #define F1(x, y, z) (z ^ (x & (y ^ z)))
171 #define F2(x, y, z) F1(z, x, y)
172 #define F3(x, y, z) (x ^ y ^ z)
173 #define F4(x, y, z) (y ^ (x | ~z))
174 
175 /* This is the central step in the MD5 algorithm. */
176 #define MD5STEP(f, w, x, y, z, data, s) (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
177 
178 /** The core of the MD5 algorithm
179  *
180  * This alters an existing MD5 hash to reflect the addition of 16
181  * longwords of new data. fr_md5_update blocks the data and converts bytes
182  * into longwords for this routine.
183  *
184  * @param[in] state 16 bytes of data to feed into the hashing function.
185  * @param[in,out] block MD5 digest block to update.
186  */
187 void fr_md5_transform(uint32_t state[4], uint8_t const block[MD5_BLOCK_LENGTH])
188 {
189  uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
190 
191  for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
192  in[a] = (uint32_t)(
193  (uint32_t)(block[a * 4 + 0]) |
194  (uint32_t)(block[a * 4 + 1]) << 8 |
195  (uint32_t)(block[a * 4 + 2]) << 16 |
196  (uint32_t)(block[a * 4 + 3]) << 24);
197  }
198 
199  a = state[0];
200  b = state[1];
201  c = state[2];
202  d = state[3];
203 
204  MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478, 7);
205  MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
206  MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
207  MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
208  MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf, 7);
209  MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
210  MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
211  MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
212  MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8, 7);
213  MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
214  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
215  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
216  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
217  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
218  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
219  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
220 
221  MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562, 5);
222  MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340, 9);
223  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
224  MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
225  MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d, 5);
226  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
227  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
228  MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
229  MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6, 5);
230  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
231  MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
232  MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
233  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
234  MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8, 9);
235  MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
236  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
237 
238  MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942, 4);
239  MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
240  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
241  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
242  MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44, 4);
243  MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
244  MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
245  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
246  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
247  MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
248  MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
249  MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
250  MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039, 4);
251  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
252  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
253  MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
254 
255  MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244, 6);
256  MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
257  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
258  MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
259  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
260  MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
261  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
262  MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
263  MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f, 6);
264  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
265  MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
266  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
267  MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82, 6);
268  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
269  MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
270  MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
271 
272  state[0] += a;
273  state[1] += b;
274  state[2] += c;
275  state[3] += d;
276 }
277 #endif
#define F2(x, y, z)
Definition: md5.c:171
uint32_t count[2]
Number of bits, mod 2^64.
Definition: md5.h:57
#define F1(x, y, z)
Definition: md5.c:170
void fr_md5_final(uint8_t out[MD5_DIGEST_LENGTH], FR_MD5_CTX *ctx)
Finalise the MD5 context and write out the hash.
Definition: md5.c:145
void fr_md5_init(FR_MD5_CTX *ctx)
Initialise a new MD5 context.
Definition: md5.c:84
void fr_md5_transform(uint32_t state[4], uint8_t const block[MD5_BLOCK_LENGTH])
The core of the MD5 algorithm.
Definition: md5.c:187
static const uint8_t PADDING[MD5_BLOCK_LENGTH]
Definition: md5.c:72
uint8_t buffer[MD5_BLOCK_LENGTH]
Input buffer.
Definition: md5.h:58
void fr_md5_calc(uint8_t *out, uint8_t const *in, size_t inlen)
Calculate the MD5 hash of the contents of a buffer.
Definition: md5.c:28
#define MD5STEP(f, w, x, y, z, data, s)
Definition: md5.c:176
#define F3(x, y, z)
Definition: md5.c:172
uint32_t state[4]
State.
Definition: md5.h:56
#define F4(x, y, z)
Definition: md5.c:173
unsigned int state
Definition: proto_bfd.c:200
#define PUT_64BIT_LE(cp, value)
Definition: md5.c:54
void MD5_BLOCK_LENGTH
Definition: md5.h:68
#define PUT_32BIT_LE(cp, value)
Definition: md5.c:65
void fr_md5_update(FR_MD5_CTX *ctx, uint8_t const *in, size_t inlen)
Feed additional data into the MD5 hashing function.
Definition: md5.c:100
void void MD5_DIGEST_LENGTH
Definition: md5.h:65
#define RCSID(id)
Definition: build.h:135