1 #define compile \
2 { gcc -o su_md5 -O2 -g -Wall -DTEST -I. su_md5.c } ; exit 0
3 /* -*- c-style: java -*- */
4 
5 /*
6  * This file is part of the Sofia-SIP package
7  *
8  * Copyright (C) 2005 Nokia Corporation.
9  *
10  * Contact: Pekka Pessi <pekka.pessi@nokia.com>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1 of
15  * the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25  * 02110-1301 USA
26  *
27  */
28 
29 /*
30  * This code implements the MD5 message-digest algorithm. The algorithm is
31  * due to Ron Rivest. This code was initially written by Colin Plumb in
32  * 1993, no copyright is claimed. This code is in the public domain; do with
33  * it what you wish.
34  *
35  * Equivalent code is available from RSA Data Security, Inc.  This code has
36  * been tested against that, and is equivalent, except that you don't need
37  * to include two pages of legalese with every copy.
38  */
39 
40 /** @ingroup su_md5
41  *
42  * @CFILE su_md5.c MD5 Implementation
43  *
44  * To compute the message digest of a chunk of bytes, declare an su_md5_t
45  * context structure, pass it to su_md5_init(), call su_md5_update() as
46  * needed on buffers full of bytes, and then call su_md5_digest(), which
47  * will fill a supplied 16-byte array with the current digest.
48  *
49  * @note
50  * This code was modified in 1997 by Jim Kingdon of Cyclic Software to
51  * not require an integer type which is exactly 32 bits.  This work
52  * draws on the changes for the same purpose by Tatu Ylonen
53  * <ylo@cs.hut.fi> as part of SSH, but since I didn't actually use
54  * that code, there is no copyright issue.  I hereby disclaim
55  * copyright in any changes I have made; this code remains in the
56  * public domain.
57  *
58  * @note Regarding su_* namespace: this avoids potential conflicts
59  * with libraries such as some versions of Kerberos.  No particular
60  * need to worry about whether the system supplies an MD5 library, as
61  * this file is only about 3k of object code.
62  *
63  */
64 
65 #include <string.h>	/* for memcpy() and memset() */
66 
67 #include "sofia-sip/su_md5.h"
68 
69 static void su_md5_transform(uint32_t buf[4], const unsigned char inraw[64]);
70 
71 /* Little-endian byte-swapping routines.  Note that these do not depend on
72    the size of datatypes such as cvs_uint32, nor do they require us to
73    detect the endianness of the machine we are running on.  It is possible
74    they should be macros for speed, but I would be surprised if they were a
75    performance bottleneck for MD5. These are inlined by any sane compiler,
76    anyways. */
77 
getu32(const unsigned char * addr)78 static uint32_t getu32(const unsigned char *addr)
79 {
80   return (((((unsigned long)addr[3] << 8) | addr[2]) << 8)
81 	  | addr[1]) << 8 | addr[0];
82 }
83 
putu32(uint32_t data,unsigned char * addr)84 static void putu32(uint32_t data, unsigned char *addr)
85 {
86   addr[0] = (unsigned char)data;
87   addr[1] = (unsigned char)(data >> 8);
88   addr[2] = (unsigned char)(data >> 16);
89   addr[3] = (unsigned char)(data >> 24);
90 }
91 
92 /** Initialize MD5 context.
93  *
94  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
95  * initialization constants.
96  *
97  * @param ctx Pointer to context structure.
98  */
99 void
su_md5_init(su_md5_t * ctx)100 su_md5_init(su_md5_t *ctx)
101 {
102   ctx->buf[0] = 0x67452301;
103   ctx->buf[1] = 0xefcdab89;
104   ctx->buf[2] = 0x98badcfe;
105   ctx->buf[3] = 0x10325476;
106 
107   ctx->bits[0] = 0;
108   ctx->bits[1] = 0;
109 }
110 
111 /** Clear MD5 context.
112  *
113  * The function su_md5_deinit() clears MD5 context.
114  *
115  * @param context  Pointer to MD5 context structure.
116  */
su_md5_deinit(su_md5_t * context)117 void su_md5_deinit(su_md5_t *context)
118 {
119   memset(context, 0, sizeof *context);
120 }
121 
122 /** Update MD5 context.
123  *
124  * Update context to reflect the concatenation of another buffer full
125  * of bytes.
126  *
127  * @param ctx Pointer to context structure
128  * @param b   Pointer to data
129  * @param len Length of @a b as bytes
130  */
131 void
su_md5_update(su_md5_t * ctx,void const * b,usize_t len)132 su_md5_update(su_md5_t *ctx,
133 	      void const *b,
134 	      usize_t len)
135 {
136   unsigned char const *buf = (unsigned char const *)b;
137   uint32_t t;
138 
139   /* Update bitcount */
140 
141   t = ctx->bits[0];
142   if ((ctx->bits[0] = (t + ((uint32_t)len << 3)) & 0xffffffff) < t)
143     ctx->bits[1]++;	/* Carry from low to high */
144   ctx->bits[1] += (uint32_t)(len >> 29);
145 
146   t = (t >> 3) & 0x3f;	/* Bytes already in shsInfo->data */
147 
148   /* Handle any leading odd-sized chunks */
149 
150   if ( t ) {
151     unsigned char *p = ctx->in + t;
152 
153     t = 64 - t;
154 
155     if (len < t) {
156       memcpy(p, buf, len);
157       return;
158     }
159 
160     memcpy(p, buf, t);
161     su_md5_transform (ctx->buf, ctx->in);
162     buf += t;
163     len -= t;
164   }
165 
166   /* Process data in 64-byte chunks */
167 
168   while (len >= 64) {
169     su_md5_transform(ctx->buf, buf);
170     buf += 64;
171     len -= 64;
172   }
173 
174   /* Handle any remaining bytes of data. */
175   memcpy(ctx->in, buf, len);
176 }
177 
178 /** Copy memory, fix case to lower. */
179 static
mem_i_cpy(unsigned char * d,unsigned char const * s,size_t len)180 void mem_i_cpy(unsigned char *d, unsigned char const *s, size_t len)
181 {
182   size_t i;
183 
184   for (i = 0; i < len; i++)
185     if (s[i] >= 'A' && s[i] <= 'Z')
186       d[i] = s[i] + ('a' - 'A');
187     else
188       d[i] = s[i];
189 }
190 
191 /**Update MD5 context.
192  *
193  * The function su_md5_iupdate() updates context to reflect the
194  * concatenation of another buffer full of case-independent characters.
195  *
196  * @param ctx Pointer to context structure
197  * @param b   Pointer to data
198  * @param len Length of @a b as bytes
199  */
200 void
su_md5_iupdate(su_md5_t * ctx,void const * b,usize_t len)201 su_md5_iupdate(su_md5_t *ctx,
202 	       void const *b,
203 	       usize_t len)
204 {
205   unsigned char const *buf = (unsigned char const *)b;
206   uint32_t t;
207 
208   /* Update bitcount */
209 
210   t = ctx->bits[0];
211   if ((ctx->bits[0] = (t + ((uint32_t)len << 3)) & 0xffffffff) < t)
212     ctx->bits[1]++;	/* Carry from low to high */
213   ctx->bits[1] += (uint32_t)(len >> 29);
214 
215   t = (t >> 3) & 0x3f;	/* Bytes already in shsInfo->data */
216 
217   /* Handle any leading odd-sized chunks */
218 
219   if ( t ) {
220     unsigned char *p = ctx->in + t;
221 
222     t = sizeof(ctx->in) - t;
223 
224     if (len < t) {
225       mem_i_cpy(p, buf, len);
226       return;
227     }
228     mem_i_cpy(p, buf, t);
229     su_md5_transform (ctx->buf, ctx->in);
230     buf += t;
231     len -= t;
232   }
233 
234   /* Process data in 64-byte chunks */
235   while (len >= sizeof(ctx->in)) {
236     mem_i_cpy(ctx->in, buf, sizeof(ctx->in));
237     su_md5_transform(ctx->buf, ctx->in);
238     buf += sizeof(ctx->in);
239     len -= sizeof(ctx->in);
240   }
241 
242   /* Handle any remaining bytes of data. */
243   mem_i_cpy(ctx->in, buf, len);
244 }
245 
246 /** Update MD5 context with contents of string.
247  *
248  * The function su_md5_strupdate() updates context to reflect the
249  * concatenation of NUL-terminated string.
250  *
251  * @param ctx Pointer to context structure
252  * @param s   Pointer to string
253  */
su_md5_strupdate(su_md5_t * ctx,char const * s)254 void su_md5_strupdate(su_md5_t *ctx, char const *s)
255 {
256   if (s)
257     su_md5_update(ctx, s, strlen(s));
258 }
259 
260 /** Update MD5 context with contents of string, including final NUL.
261  *
262  * The function su_md5_str0update() updates context to reflect the
263  * concatenation of NUL-terminated string, including the final NUL.
264  *
265  * @param ctx Pointer to context structure
266  * @param s   Pointer to string
267  */
su_md5_str0update(su_md5_t * ctx,char const * s)268 void su_md5_str0update(su_md5_t *ctx, char const *s)
269 {
270   if (!s)
271     s = "";
272 
273   su_md5_update(ctx, s, strlen(s) + 1);
274 }
275 
276 /** Update MD5 context with contents of case-independent string.
277  *
278  * The function su_md5_striupdate() updates context to reflect the
279  * concatenation of NUL-terminated string.
280  *
281  * @param ctx Pointer to context structure
282  * @param s   Pointer to string
283  */
su_md5_striupdate(su_md5_t * ctx,char const * s)284 void su_md5_striupdate(su_md5_t *ctx, char const *s)
285 {
286   if (s)
287     su_md5_iupdate(ctx, s, strlen(s));
288 }
289 
290 /** Update MD5 context with contents of case-independent string, including
291  * final NUL.
292  *
293  * The function su_md5_stri0update() updates context to reflect the
294  * concatenation of NUL-terminated string, including the final NUL.
295  *
296  * @param ctx Pointer to context structure
297  * @param s   Pointer to string
298  */
su_md5_stri0update(su_md5_t * ctx,char const * s)299 void su_md5_stri0update(su_md5_t *ctx, char const *s)
300 {
301   if (!s)
302     s = "";
303 
304   su_md5_iupdate(ctx, s, strlen(s) + 1);
305 }
306 
307 
308 /** Generate digest.
309  *
310  * Final wrapup. Pad message to 64-byte boundary with the bit pattern 1 0*
311  * (64-bit count of bits processed, MSB-first), then concatenate message
312  * with its length (measured in bits) as 64-byte big-endian integer.
313  *
314  * @param context  Pointer to context structure
315  * @param digest   Digest array to be filled
316  */
317 void
su_md5_digest(su_md5_t const * context,uint8_t digest[16])318 su_md5_digest(su_md5_t const *context, uint8_t digest[16])
319 {
320   unsigned count;
321   unsigned char *p;
322 
323   su_md5_t ctx[1];
324 
325   ctx[0] = context[0];
326 
327   /* Compute number of bytes mod 64 */
328   count = (ctx->bits[0] >> 3) & 0x3F;
329 
330   /* Set the first char of padding to 0x80.  This is safe since there is
331      always at least one byte free */
332   p = ctx->in + count;
333   *p++ = 0x80;
334 
335   /* Bytes of padding needed to make 64 bytes */
336   count = 64 - 1 - count;
337 
338   /* Pad out to 56 mod 64 */
339   if (count < 8) {
340     /* Two lots of padding:  Pad the first block to 64 bytes */
341     memset(p, 0, count);
342     su_md5_transform (ctx->buf, ctx->in);
343 
344     /* Now fill the next block with 56 bytes */
345     memset(ctx->in, 0, 56);
346   } else {
347     /* Pad block to 56 bytes */
348     memset(p, 0, count-8);
349   }
350 
351   /* Append length in bits and transform */
352   putu32(ctx->bits[0], ctx->in + 56);
353   putu32(ctx->bits[1], ctx->in + 60);
354 
355   su_md5_transform(ctx->buf, ctx->in);
356   putu32(ctx->buf[0], digest);
357   putu32(ctx->buf[1], digest + 4);
358   putu32(ctx->buf[2], digest + 8);
359   putu32(ctx->buf[3], digest + 12);
360   memset(ctx, 0, sizeof(ctx));	/* In case it's sensitive */
361 }
362 
su_md5_hexdigest(su_md5_t const * ctx,char digest[2* SU_MD5_DIGEST_SIZE+1])363 void su_md5_hexdigest(su_md5_t const *ctx,
364 		      char digest[2 * SU_MD5_DIGEST_SIZE + 1])
365 {
366   uint8_t b, bin[SU_MD5_DIGEST_SIZE];
367   short i, j;
368 
369   su_md5_digest(ctx, bin);
370 
371   for (i = j = 0; i < 16; i++) {
372     b = (bin[i] >> 4) & 15;
373     digest[j++] = b + (b > 9 ? 'a' - 10 : '0');
374     b = bin[i] & 15;
375     digest[j++] = b + (b > 9 ? 'a' - 10 : '0');
376   }
377 
378   digest[j] = '\0';
379 }
380 
381 #ifndef ASM_MD5
382 
383 /* The four core functions - F1 is optimized somewhat */
384 
385 /* #define F1(x, y, z) (x & y | ~x & z) */
386 #define F1(x, y, z) (z ^ (x & (y ^ z)))
387 #define F2(x, y, z) F1(z, x, y)
388 #define F3(x, y, z) (x ^ y ^ z)
389 #define F4(x, y, z) (y ^ (x | ~z))
390 
391 /* This is the central step in the MD5 algorithm. */
392 #define MD5STEP(f, w, x, y, z, data, s) \
393 	( w += f(x, y, z) + data, w &= 0xffffffff, w = w<<s | w>>(32-s), w += x )
394 
395 /** @internal
396  *
397  * Add 64 bytes of data to hash.
398  *
399  * The core of the MD5 algorithm, this alters an existing MD5 hash to
400  * reflect the addition of 16 longwords of new data.  MD5Update blocks
401  * the data and converts bytes into longwords for this routine.
402  */
403 static void
su_md5_transform(uint32_t buf[4],const unsigned char inraw[64])404 su_md5_transform(uint32_t buf[4], const unsigned char inraw[64])
405 {
406   register uint32_t a, b, c, d;
407   uint32_t in[16];
408   int i;
409 
410   for (i = 0; i < 16; ++i)
411     in[i] = getu32 (inraw + 4 * i);
412 
413   a = buf[0];
414   b = buf[1];
415   c = buf[2];
416   d = buf[3];
417 
418   MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478,  7);
419   MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
420   MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
421   MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
422   MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf,  7);
423   MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
424   MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
425   MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
426   MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8,  7);
427   MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
428   MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
429   MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
430   MD5STEP(F1, a, b, c, d, in[12]+0x6b901122,  7);
431   MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
432   MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
433   MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
434 
435   MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562,  5);
436   MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340,  9);
437   MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
438   MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
439   MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d,  5);
440   MD5STEP(F2, d, a, b, c, in[10]+0x02441453,  9);
441   MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
442   MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
443   MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6,  5);
444   MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6,  9);
445   MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
446   MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
447   MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905,  5);
448   MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8,  9);
449   MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
450   MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
451 
452   MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942,  4);
453   MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
454   MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
455   MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
456   MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44,  4);
457   MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
458   MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
459   MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
460   MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6,  4);
461   MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
462   MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
463   MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
464   MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039,  4);
465   MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
466   MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
467   MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
468 
469   MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244,  6);
470   MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
471   MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
472   MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
473   MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3,  6);
474   MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
475   MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
476   MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
477   MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f,  6);
478   MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
479   MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
480   MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
481   MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82,  6);
482   MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
483   MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
484   MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
485 
486   buf[0] += a;
487   buf[1] += b;
488   buf[2] += c;
489   buf[3] += d;
490 }
491 #endif
492