Kannel: Open Source WAP and SMS gateway  svn-r5335
md5.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * md5.c - MD5 digest algorithm
59  *
60  * The remaining code is the reference MD5 code (md5c.c) from rfc1321
61  */
62 
63 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
64  */
65 
66 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
67  rights reserved.
68 
69  License to copy and use this software is granted provided that it
70  is identified as the "RSA Data Security, Inc. MD5 Message-Digest
71  Algorithm" in all material mentioning or referencing this software
72  or this function.
73 
74  License is also granted to make and use derivative works provided
75  that such works are identified as "derived from the RSA Data
76  Security, Inc. MD5 Message-Digest Algorithm" in all material
77  mentioning or referencing the derived work.
78 
79  RSA Data Security, Inc. makes no representations concerning either
80  the merchantability of this software or the suitability of this
81  software for any particular purpose. It is provided "as is"
82  without express or implied warranty of any kind.
83 
84  These notices must be retained in any copies of any part of this
85  documentation and/or software.
86  */
87 
88 #include <stdio.h>
89 
90 #include "gwlib.h"
91 #include "md5.h"
92 
93 /* Declaration of static functions */
94 
95 static void md5_digest(char *md5str, unsigned char *digest);
96 static void md5_init(md5_ctx *);
97 static void md5_update(md5_ctx *, const unsigned char *, unsigned int);
98 static void md5_final(unsigned char[16], md5_ctx *);
99 
100 
101 
102 static void md5_digest(char *md5str, unsigned char *digest)
103 {
104  int i;
105 
106  for (i = 0; i < 16; i++) {
107  sprintf(md5str, "%02x", digest[i]);
108  md5str += 2;
109  }
110 
111  *md5str = '\0';
112 }
113 
114 /*
115  * Constants for MD5Transform routine.
116  */
117 #define S11 7
118 #define S12 12
119 #define S13 17
120 #define S14 22
121 #define S21 5
122 #define S22 9
123 #define S23 14
124 #define S24 20
125 #define S31 4
126 #define S32 11
127 #define S33 16
128 #define S34 23
129 #define S41 6
130 #define S42 10
131 #define S43 15
132 #define S44 21
133 
134 static void md5_transform(unsigned int[4], const unsigned char[64]);
135 static void md5_encode(unsigned char *, unsigned int *, unsigned int);
136 static void md5_decode(unsigned int *, const unsigned char *, unsigned int);
137 
138 static unsigned char PADDING[64] =
139 {
140  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
143 };
144 
145 /* F, G, H and I are basic MD5 functions.
146  */
147 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
148 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
149 #define H(x, y, z) ((x) ^ (y) ^ (z))
150 #define I(x, y, z) ((y) ^ ((x) | (~z)))
151 
152 /* ROTATE_LEFT rotates x left n bits.
153  */
154 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
155 
156 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
157  Rotation is separate from addition to prevent recomputation.
158  */
159 #define FF(a, b, c, d, x, s, ac) { \
160  (a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); \
161  (a) = ROTATE_LEFT ((a), (s)); \
162  (a) += (b); \
163  }
164 #define GG(a, b, c, d, x, s, ac) { \
165  (a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); \
166  (a) = ROTATE_LEFT ((a), (s)); \
167  (a) += (b); \
168  }
169 #define HH(a, b, c, d, x, s, ac) { \
170  (a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); \
171  (a) = ROTATE_LEFT ((a), (s)); \
172  (a) += (b); \
173  }
174 #define II(a, b, c, d, x, s, ac) { \
175  (a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); \
176  (a) = ROTATE_LEFT ((a), (s)); \
177  (a) += (b); \
178  }
179 
180 static void md5_init(md5_ctx *context)
181 {
182  context->count[0] = context->count[1] = 0;
183  /* Load magic initialization constants. */
184  context->state[0] = 0x67452301;
185  context->state[1] = 0xefcdab89;
186  context->state[2] = 0x98badcfe;
187  context->state[3] = 0x10325476;
188 }
189 
190 /*
191  * MD5 block update operation. Continues an MD5 message-digest
192  * operation, processing another message block, and updating the
193  * context.
194  */
195 static void md5_update(md5_ctx *context, const unsigned char *input,
196  unsigned int inputLen)
197 {
198  unsigned int i, index, partLen;
199 
200  /* Compute number of bytes mod 64 */
201  index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
202 
203  /* Update number of bits */
204  if ((context->count[0] += ((unsigned int) inputLen << 3))
205  < ((unsigned int) inputLen << 3))
206  context->count[1]++;
207  context->count[1] += ((unsigned int) inputLen >> 29);
208 
209  partLen = 64 - index;
210 
211  /* Transform as many times as possible. */
212  if (inputLen >= partLen) {
213  memcpy((unsigned char*) & context->buffer[index],
214  (unsigned char*) input, partLen);
215  md5_transform(context->state, context->buffer);
216 
217  for (i = partLen; i + 63 < inputLen; i += 64)
218  md5_transform(context->state, &input[i]);
219 
220  index = 0;
221  } else
222  i = 0;
223 
224  /* Buffer remaining input */
225  memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
226  inputLen - i);
227 }
228 
229 /*
230  MD5 finalization. Ends an MD5 message-digest operation, writing the
231  the message digest and zeroizing the context.
232  */
233 static void md5_final(unsigned char digest[16], md5_ctx *context)
234 {
235  unsigned char bits[8];
236  unsigned int index, padLen;
237 
238  /* Save number of bits */
239  md5_encode(bits, context->count, 8);
240 
241  /* Pad out to 56 mod 64.
242  */
243  index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
244  padLen = (index < 56) ? (56 - index) : (120 - index);
245  md5_update(context, PADDING, padLen);
246 
247  /* Append length (before padding) */
248  md5_update(context, bits, 8);
249 
250  /* Store state in digest */
251  md5_encode(digest, context->state, 16);
252 
253  /* Zeroize sensitive information.
254  */
255  memset((unsigned char*) context, 0, sizeof(*context));
256 }
257 
258 
259 /*
260  * MD5 basic transformation. Transforms state based on block.
261  */
262 static void md5_transform(state, block)
263 unsigned int state[4];
264 const unsigned char block[64];
265 {
266  unsigned int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
267 
268  md5_decode(x, block, 64);
269 
270  /* Round 1 */
271  FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
272  FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
273  FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
274  FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
275  FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
276  FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
277  FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
278  FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
279  FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
280  FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
281  FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
282  FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
283  FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
284  FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
285  FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
286  FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
287 
288  /* Round 2 */
289  GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
290  GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
291  GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
292  GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
293  GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
294  GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
295  GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
296  GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
297  GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
298  GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
299  GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
300  GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
301  GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
302  GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
303  GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
304  GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
305 
306  /* Round 3 */
307  HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
308  HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
309  HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
310  HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
311  HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
312  HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
313  HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
314  HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
315  HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
316  HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
317  HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
318  HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
319  HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
320  HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
321  HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
322  HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
323 
324  /* Round 4 */
325  II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
326  II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
327  II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
328  II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
329  II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
330  II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
331  II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
332  II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
333  II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
334  II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
335  II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
336  II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
337  II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
338  II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
339  II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
340  II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
341 
342  state[0] += a;
343  state[1] += b;
344  state[2] += c;
345  state[3] += d;
346 
347  /* Zeroize sensitive information. */
348  memset((unsigned char*) x, 0, sizeof(x));
349 }
350 
351 /*
352  Encodes input (unsigned int) into output (unsigned char). Assumes len is
353  a multiple of 4.
354  */
355 static void md5_encode(output, input, len)
356 unsigned char *output;
357 unsigned int *input;
358 unsigned int len;
359 {
360  unsigned int i, j;
361 
362  for (i = 0, j = 0; j < len; i++, j += 4) {
363  output[j] = (unsigned char) (input[i] & 0xff);
364  output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
365  output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
366  output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
367  }
368 }
369 
370 /*
371  Decodes input (unsigned char) into output (unsigned int). Assumes len is
372  a multiple of 4.
373  */
374 static void md5_decode(output, input, len)
375 unsigned int *output;
376 const unsigned char *input;
377 unsigned int len;
378 {
379  unsigned int i, j;
380 
381  for (i = 0, j = 0; j < len; i++, j += 4)
382  output[i] = ((unsigned int) input[j]) | (((unsigned int) input[j + 1]) << 8) |
383  (((unsigned int) input[j + 2]) << 16) | (((unsigned int) input[j + 3]) << 24);
384 }
385 
386 
388 {
390  unsigned char digest[16];
391  Octstr *enc;
392 
393  if (data == NULL)
394  return NULL;
395 
396  md5_init(&context);
398  md5_final(digest, &context);
399 
400  enc = octstr_create_from_data(digest, 16);
401 
402  return enc;
403 }
404 
405 
407 {
408  char md5str[33];
409  Octstr *digest;
410 
411  if (data == NULL)
412  return NULL;
413 
414  md5str[0] = '\0';
415  digest = md5(data);
416  md5_digest(md5str, octstr_get_cstr(digest));
417  octstr_destroy(digest);
418 
419  digest = octstr_create(md5str);
420 
421  return digest;
422 }
423 
424 
425 
Octstr * md5(Octstr *data)
Definition: md5.c:387
#define S44
Definition: md5.c:132
#define S43
Definition: md5.c:131
#define S32
Definition: md5.c:126
Definition: parse.c:65
static void md5_digest(char *md5str, unsigned char *digest)
Definition: md5.c:102
static void md5_final(unsigned char[16], md5_ctx *)
Definition: md5.c:233
#define GG(a, b, c, d, x, s, ac)
Definition: md5.c:164
static void md5_transform(unsigned int[4], const unsigned char[64])
#define S31
Definition: md5.c:125
static void md5_update(md5_ctx *, const unsigned char *, unsigned int)
Definition: md5.c:195
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define II(a, b, c, d, x, s, ac)
Definition: md5.c:174
#define S21
Definition: md5.c:121
#define S12
Definition: md5.c:118
static void md5_decode(unsigned int *, const unsigned char *, unsigned int)
Definition: md5.c:374
#define S22
Definition: md5.c:122
#define HH(a, b, c, d, x, s, ac)
Definition: md5.c:169
#define S14
Definition: md5.c:120
#define S23
Definition: md5.c:123
static void md5_encode(unsigned char *, unsigned int *, unsigned int)
Definition: md5.c:355
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
#define S24
Definition: md5.c:124
#define S42
Definition: md5.c:130
#define FF(a, b, c, d, x, s, ac)
Definition: md5.c:159
static void md5_init(md5_ctx *)
Definition: md5.c:180
#define S41
Definition: md5.c:129
#define S33
Definition: md5.c:127
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
#define S13
Definition: md5.c:119
Definition: octstr.c:118
#define S11
Definition: md5.c:117
#define S34
Definition: md5.c:128
static unsigned char PADDING[64]
Definition: md5.c:138
#define octstr_create_from_data(data, len)
Definition: octstr.h:134
Definition: md5.h:87
Octstr * md5digest(Octstr *data)
Definition: md5.c:406
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.