Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
md5.c File Reference
#include <stdio.h>
#include "gwlib.h"
#include "md5.h"

Go to the source code of this file.

Macros

#define S11   7
 
#define S12   12
 
#define S13   17
 
#define S14   22
 
#define S21   5
 
#define S22   9
 
#define S23   14
 
#define S24   20
 
#define S31   4
 
#define S32   11
 
#define S33   16
 
#define S34   23
 
#define S41   6
 
#define S42   10
 
#define S43   15
 
#define S44   21
 
#define F(x, y, z)   (((x) & (y)) | ((~x) & (z)))
 
#define G(x, y, z)   (((x) & (z)) | ((y) & (~z)))
 
#define H(x, y, z)   ((x) ^ (y) ^ (z))
 
#define I(x, y, z)   ((y) ^ ((x) | (~z)))
 
#define ROTATE_LEFT(x, n)   (((x) << (n)) | ((x) >> (32-(n))))
 
#define FF(a, b, c, d, x, s, ac)
 
#define GG(a, b, c, d, x, s, ac)
 
#define HH(a, b, c, d, x, s, ac)
 
#define II(a, b, c, d, x, s, ac)
 

Functions

static void md5_digest (char *md5str, unsigned char *digest)
 
static void md5_init (md5_ctx *)
 
static void md5_update (md5_ctx *, const unsigned char *, unsigned int)
 
static void md5_final (unsigned char[16], md5_ctx *)
 
static void md5_transform (unsigned int[4], const unsigned char[64])
 
static void md5_encode (unsigned char *, unsigned int *, unsigned int)
 
static void md5_decode (unsigned int *, const unsigned char *, unsigned int)
 
static void md5_transform (state, block)
 
Octstrmd5 (Octstr *data)
 
Octstrmd5digest (Octstr *data)
 

Variables

static unsigned char PADDING [64]
 

Macro Definition Documentation

#define F (   x,
  y,
 
)    (((x) & (y)) | ((~x) & (z)))

Definition at line 147 of file md5.c.

#define FF (   a,
  b,
  c,
  d,
  x,
  s,
  ac 
)
Value:
{ \
(a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define F(x, y, z)
Definition: md5.c:147
#define ROTATE_LEFT(x, n)
Definition: md5.c:154

Definition at line 159 of file md5.c.

Referenced by md5_transform().

#define G (   x,
  y,
 
)    (((x) & (z)) | ((y) & (~z)))

Definition at line 148 of file md5.c.

#define GG (   a,
  b,
  c,
  d,
  x,
  s,
  ac 
)
Value:
{ \
(a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define ROTATE_LEFT(x, n)
Definition: md5.c:154
#define G(x, y, z)
Definition: md5.c:148

Definition at line 164 of file md5.c.

Referenced by md5_transform().

#define H (   x,
  y,
 
)    ((x) ^ (y) ^ (z))

Definition at line 149 of file md5.c.

#define HH (   a,
  b,
  c,
  d,
  x,
  s,
  ac 
)
Value:
{ \
(a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define ROTATE_LEFT(x, n)
Definition: md5.c:154
#define H(x, y, z)
Definition: md5.c:149

Definition at line 169 of file md5.c.

Referenced by md5_transform().

#define I (   x,
  y,
 
)    ((y) ^ ((x) | (~z)))

Definition at line 150 of file md5.c.

#define II (   a,
  b,
  c,
  d,
  x,
  s,
  ac 
)
Value:
{ \
(a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define ROTATE_LEFT(x, n)
Definition: md5.c:154
#define I(x, y, z)
Definition: md5.c:150

Definition at line 174 of file md5.c.

Referenced by md5_transform().

#define ROTATE_LEFT (   x,
 
)    (((x) << (n)) | ((x) >> (32-(n))))

Definition at line 154 of file md5.c.

#define S11   7

Definition at line 117 of file md5.c.

Referenced by md5_transform().

#define S12   12

Definition at line 118 of file md5.c.

Referenced by md5_transform().

#define S13   17

Definition at line 119 of file md5.c.

Referenced by md5_transform().

#define S14   22

Definition at line 120 of file md5.c.

Referenced by md5_transform().

#define S21   5

Definition at line 121 of file md5.c.

Referenced by md5_transform().

#define S22   9

Definition at line 122 of file md5.c.

Referenced by md5_transform().

#define S23   14

Definition at line 123 of file md5.c.

Referenced by md5_transform().

#define S24   20

Definition at line 124 of file md5.c.

Referenced by md5_transform().

#define S31   4

Definition at line 125 of file md5.c.

Referenced by md5_transform().

#define S32   11

Definition at line 126 of file md5.c.

Referenced by md5_transform().

#define S33   16

Definition at line 127 of file md5.c.

Referenced by md5_transform().

#define S34   23

Definition at line 128 of file md5.c.

Referenced by md5_transform().

#define S41   6

Definition at line 129 of file md5.c.

Referenced by md5_transform().

#define S42   10

Definition at line 130 of file md5.c.

Referenced by md5_transform().

#define S43   15

Definition at line 131 of file md5.c.

Referenced by md5_transform().

#define S44   21

Definition at line 132 of file md5.c.

Referenced by md5_transform().

Function Documentation

Octstr* md5 ( Octstr data)

Definition at line 387 of file md5.c.

References md5_final(), md5_init(), md5_update(), octstr_create_from_data, octstr_get_cstr, and octstr_len().

Referenced by cfg_get_group_checksum(), main(), md5digest(), and radius_authenticate_pdu().

388 {
390  unsigned char digest[16];
391  Octstr *enc;
392 
393  if (data == NULL)
394  return NULL;
395 
396  md5_init(&context);
397  md5_update(&context, octstr_get_cstr(data), octstr_len(data));
398  md5_final(digest, &context);
399 
400  enc = octstr_create_from_data(digest, 16);
401 
402  return enc;
403 }
Definition: parse.c:65
static void md5_final(unsigned char[16], md5_ctx *)
Definition: md5.c:233
static void md5_update(md5_ctx *, const unsigned char *, unsigned int)
Definition: md5.c:195
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
static void md5_init(md5_ctx *)
Definition: md5.c:180
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
#define octstr_create_from_data(data, len)
Definition: octstr.h:134
Definition: md5.h:87
static void md5_decode ( unsigned int *  output,
const unsigned char *  input,
unsigned int  len 
)
static

Definition at line 374 of file md5.c.

Referenced by md5_transform().

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 }
static void md5_digest ( char *  md5str,
unsigned char *  digest 
)
static

Definition at line 102 of file md5.c.

Referenced by md5digest().

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 }
static void md5_encode ( unsigned char *  output,
unsigned int *  input,
unsigned int  len 
)
static

Definition at line 355 of file md5.c.

Referenced by md5_final().

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 }
static void md5_final ( unsigned char  digest[16],
md5_ctx context 
)
static

Definition at line 233 of file md5.c.

References md5_ctx::count, md5_encode(), md5_update(), PADDING, and md5_ctx::state.

Referenced by md5().

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 }
unsigned int count[2]
Definition: md5.h:89
static void md5_update(md5_ctx *, const unsigned char *, unsigned int)
Definition: md5.c:195
static void md5_encode(unsigned char *, unsigned int *, unsigned int)
Definition: md5.c:355
unsigned int state[4]
Definition: md5.h:88
static unsigned char PADDING[64]
Definition: md5.c:138
static void md5_init ( md5_ctx context)
static

Definition at line 180 of file md5.c.

References md5_ctx::count, and md5_ctx::state.

Referenced by md5().

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 }
unsigned int count[2]
Definition: md5.h:89
unsigned int state[4]
Definition: md5.h:88
static void md5_transform ( unsigned  int[4],
const unsigned  char[64] 
)
static

Referenced by md5_update().

static void md5_transform ( state  ,
block   
)
static

Definition at line 262 of file md5.c.

References FF, GG, HH, II, md5_decode(), S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, and S44.

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 }
#define S44
Definition: md5.c:132
#define S43
Definition: md5.c:131
#define S32
Definition: md5.c:126
#define GG(a, b, c, d, x, s, ac)
Definition: md5.c:164
#define S31
Definition: md5.c:125
#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
#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
#define S41
Definition: md5.c:129
#define S33
Definition: md5.c:127
#define S13
Definition: md5.c:119
#define S11
Definition: md5.c:117
#define S34
Definition: md5.c:128
static void md5_update ( md5_ctx context,
const unsigned char *  input,
unsigned int  inputLen 
)
static

Definition at line 195 of file md5.c.

References md5_ctx::buffer, md5_ctx::count, md5_transform(), and md5_ctx::state.

Referenced by md5(), and md5_final().

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 }
unsigned int count[2]
Definition: md5.h:89
static void md5_transform(unsigned int[4], const unsigned char[64])
unsigned char buffer[64]
Definition: md5.h:90
unsigned int state[4]
Definition: md5.h:88
Octstr* md5digest ( Octstr data)

Definition at line 406 of file md5.c.

References md5(), md5_digest(), octstr_create, octstr_destroy(), and octstr_get_cstr.

Referenced by main(), and our_hash_func().

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 }
Octstr * md5(Octstr *data)
Definition: md5.c:387
static void md5_digest(char *md5str, unsigned char *digest)
Definition: md5.c:102
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118

Variable Documentation

unsigned char PADDING[64]
static
Initial value:
=
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}

Definition at line 138 of file md5.c.

Referenced by md5_final().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.