Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

md5.c File Reference

#include <stdio.h>
#include "gwlib.h"
#include "md5.h"

Include dependency graph for md5.c:

Include dependency graph

Go to the source code of this file.

Defines

#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

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

Variables

unsigned char PADDING [64]


Define 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); \
  }

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); \
  }

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); \
  }

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); \
  }

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 data, md5_final(), md5_init(), md5_update(), octstr_create_from_data, octstr_get_cstr, and octstr_len().

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

00388 {
00389     md5_ctx context;
00390     unsigned char digest[16];
00391     Octstr *enc;
00392 
00393     if (data == NULL)
00394         return NULL;
00395 
00396     md5_init(&context);
00397     md5_update(&context, octstr_get_cstr(data), octstr_len(data));
00398     md5_final(digest, &context);
00399 
00400     enc = octstr_create_from_data(digest, 16); 
00401 
00402     return enc; 
00403 }

Here is the call graph for this function:

void md5_decode unsigned int *  ,
const unsigned char *  ,
unsigned  int
[static]
 

Definition at line 374 of file md5.c.

Referenced by md5_transform().

00378 {
00379     unsigned int i, j;
00380 
00381     for (i = 0, j = 0; j < len; i++, j += 4)
00382         output[i] = ((unsigned int) input[j]) | (((unsigned int) input[j + 1]) << 8) |
00383             (((unsigned int) input[j + 2]) << 16) | (((unsigned int) input[j + 3]) << 24);
00384 }

void md5_digest char *  md5str,
unsigned char *  digest
[static]
 

Definition at line 102 of file md5.c.

Referenced by md5digest().

00103 {
00104     int i;
00105 
00106     for (i = 0; i < 16; i++) {
00107         sprintf(md5str, "%02x", digest[i]);
00108         md5str += 2;
00109     }
00110 
00111     *md5str = '\0';
00112 }

void md5_encode unsigned char *  ,
unsigned int *  ,
unsigned  int
[static]
 

Definition at line 355 of file md5.c.

Referenced by md5_final().

00359 {
00360     unsigned int i, j;
00361 
00362     for (i = 0, j = 0; j < len; i++, j += 4) {
00363         output[j] = (unsigned char) (input[i] & 0xff);
00364         output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
00365         output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
00366         output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
00367     }
00368 }

void md5_final unsigned  char[16],
md5_ctx
[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().

00234 {
00235     unsigned char bits[8];
00236     unsigned int index, padLen;
00237 
00238     /* Save number of bits */
00239     md5_encode(bits, context->count, 8);
00240 
00241     /* Pad out to 56 mod 64.
00242      */
00243     index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
00244     padLen = (index < 56) ? (56 - index) : (120 - index);
00245     md5_update(context, PADDING, padLen);
00246 
00247     /* Append length (before padding) */
00248     md5_update(context, bits, 8);
00249 
00250     /* Store state in digest */
00251     md5_encode(digest, context->state, 16);
00252 
00253     /* Zeroize sensitive information.
00254      */
00255     memset((unsigned char*) context, 0, sizeof(*context));
00256 }

Here is the call graph for this function:

void md5_init md5_ctx  )  [static]
 

Definition at line 180 of file md5.c.

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

Referenced by md5().

00181 {
00182     context->count[0] = context->count[1] = 0;
00183     /* Load magic initialization constants. */
00184     context->state[0] = 0x67452301;
00185     context->state[1] = 0xefcdab89;
00186     context->state[2] = 0x98badcfe;
00187     context->state[3] = 0x10325476;
00188 }

void md5_transform state  ,
block 
[static]
 

Definition at line 262 of file md5.c.

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

Referenced by md5_update().

00265 {
00266     unsigned int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
00267 
00268     md5_decode(x, block, 64);
00269 
00270     /* Round 1 */
00271     FF(a, b, c, d, x[0], S11, 0xd76aa478);  /* 1 */
00272     FF(d, a, b, c, x[1], S12, 0xe8c7b756);  /* 2 */
00273     FF(c, d, a, b, x[2], S13, 0x242070db);  /* 3 */
00274     FF(b, c, d, a, x[3], S14, 0xc1bdceee);  /* 4 */
00275     FF(a, b, c, d, x[4], S11, 0xf57c0faf);  /* 5 */
00276     FF(d, a, b, c, x[5], S12, 0x4787c62a);  /* 6 */
00277     FF(c, d, a, b, x[6], S13, 0xa8304613);  /* 7 */
00278     FF(b, c, d, a, x[7], S14, 0xfd469501);  /* 8 */
00279     FF(a, b, c, d, x[8], S11, 0x698098d8);  /* 9 */
00280     FF(d, a, b, c, x[9], S12, 0x8b44f7af);  /* 10 */
00281     FF(c, d, a, b, x[10], S13, 0xffff5bb1);     /* 11 */
00282     FF(b, c, d, a, x[11], S14, 0x895cd7be);     /* 12 */
00283     FF(a, b, c, d, x[12], S11, 0x6b901122);     /* 13 */
00284     FF(d, a, b, c, x[13], S12, 0xfd987193);     /* 14 */
00285     FF(c, d, a, b, x[14], S13, 0xa679438e);     /* 15 */
00286     FF(b, c, d, a, x[15], S14, 0x49b40821);     /* 16 */
00287 
00288     /* Round 2 */
00289     GG(a, b, c, d, x[1], S21, 0xf61e2562);  /* 17 */
00290     GG(d, a, b, c, x[6], S22, 0xc040b340);  /* 18 */
00291     GG(c, d, a, b, x[11], S23, 0x265e5a51);     /* 19 */
00292     GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);  /* 20 */
00293     GG(a, b, c, d, x[5], S21, 0xd62f105d);  /* 21 */
00294     GG(d, a, b, c, x[10], S22, 0x2441453);  /* 22 */
00295     GG(c, d, a, b, x[15], S23, 0xd8a1e681);     /* 23 */
00296     GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);  /* 24 */
00297     GG(a, b, c, d, x[9], S21, 0x21e1cde6);  /* 25 */
00298     GG(d, a, b, c, x[14], S22, 0xc33707d6);     /* 26 */
00299     GG(c, d, a, b, x[3], S23, 0xf4d50d87);  /* 27 */
00300     GG(b, c, d, a, x[8], S24, 0x455a14ed);  /* 28 */
00301     GG(a, b, c, d, x[13], S21, 0xa9e3e905);     /* 29 */
00302     GG(d, a, b, c, x[2], S22, 0xfcefa3f8);  /* 30 */
00303     GG(c, d, a, b, x[7], S23, 0x676f02d9);  /* 31 */
00304     GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);     /* 32 */
00305 
00306     /* Round 3 */
00307     HH(a, b, c, d, x[5], S31, 0xfffa3942);  /* 33 */
00308     HH(d, a, b, c, x[8], S32, 0x8771f681);  /* 34 */
00309     HH(c, d, a, b, x[11], S33, 0x6d9d6122);     /* 35 */
00310     HH(b, c, d, a, x[14], S34, 0xfde5380c);     /* 36 */
00311     HH(a, b, c, d, x[1], S31, 0xa4beea44);  /* 37 */
00312     HH(d, a, b, c, x[4], S32, 0x4bdecfa9);  /* 38 */
00313     HH(c, d, a, b, x[7], S33, 0xf6bb4b60);  /* 39 */
00314     HH(b, c, d, a, x[10], S34, 0xbebfbc70);     /* 40 */
00315     HH(a, b, c, d, x[13], S31, 0x289b7ec6);     /* 41 */
00316     HH(d, a, b, c, x[0], S32, 0xeaa127fa);  /* 42 */
00317     HH(c, d, a, b, x[3], S33, 0xd4ef3085);  /* 43 */
00318     HH(b, c, d, a, x[6], S34, 0x4881d05);   /* 44 */
00319     HH(a, b, c, d, x[9], S31, 0xd9d4d039);  /* 45 */
00320     HH(d, a, b, c, x[12], S32, 0xe6db99e5);     /* 46 */
00321     HH(c, d, a, b, x[15], S33, 0x1fa27cf8);     /* 47 */
00322     HH(b, c, d, a, x[2], S34, 0xc4ac5665);  /* 48 */
00323 
00324     /* Round 4 */
00325     II(a, b, c, d, x[0], S41, 0xf4292244);  /* 49 */
00326     II(d, a, b, c, x[7], S42, 0x432aff97);  /* 50 */
00327     II(c, d, a, b, x[14], S43, 0xab9423a7);     /* 51 */
00328     II(b, c, d, a, x[5], S44, 0xfc93a039);  /* 52 */
00329     II(a, b, c, d, x[12], S41, 0x655b59c3);     /* 53 */
00330     II(d, a, b, c, x[3], S42, 0x8f0ccc92);  /* 54 */
00331     II(c, d, a, b, x[10], S43, 0xffeff47d);     /* 55 */
00332     II(b, c, d, a, x[1], S44, 0x85845dd1);  /* 56 */
00333     II(a, b, c, d, x[8], S41, 0x6fa87e4f);  /* 57 */
00334     II(d, a, b, c, x[15], S42, 0xfe2ce6e0);     /* 58 */
00335     II(c, d, a, b, x[6], S43, 0xa3014314);  /* 59 */
00336     II(b, c, d, a, x[13], S44, 0x4e0811a1);     /* 60 */
00337     II(a, b, c, d, x[4], S41, 0xf7537e82);  /* 61 */
00338     II(d, a, b, c, x[11], S42, 0xbd3af235);     /* 62 */
00339     II(c, d, a, b, x[2], S43, 0x2ad7d2bb);  /* 63 */
00340     II(b, c, d, a, x[9], S44, 0xeb86d391);  /* 64 */
00341 
00342     state[0] += a;
00343     state[1] += b;
00344     state[2] += c;
00345     state[3] += d;
00346 
00347     /* Zeroize sensitive information. */
00348     memset((unsigned char*) x, 0, sizeof(x));
00349 }

Here is the call graph for this function:

void md5_transform unsigned  int[4],
const unsigned  char[64]
[static]
 

void md5_update md5_ctx ,
const unsigned char *  ,
unsigned  int
[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().

00197 {
00198     unsigned int i, index, partLen;
00199 
00200     /* Compute number of bytes mod 64 */
00201     index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
00202 
00203     /* Update number of bits */
00204     if ((context->count[0] += ((unsigned int) inputLen << 3))
00205         < ((unsigned int) inputLen << 3))
00206         context->count[1]++;
00207     context->count[1] += ((unsigned int) inputLen >> 29);
00208 
00209     partLen = 64 - index;
00210 
00211     /* Transform as many times as possible. */
00212     if (inputLen >= partLen) {
00213         memcpy((unsigned char*) & context->buffer[index], 
00214                (unsigned char*) input, partLen);
00215         md5_transform(context->state, context->buffer);
00216 
00217         for (i = partLen; i + 63 < inputLen; i += 64)
00218             md5_transform(context->state, &input[i]);
00219 
00220         index = 0;
00221     } else
00222         i = 0;
00223 
00224     /* Buffer remaining input */
00225     memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
00226            inputLen - i);
00227 }

Here is the call graph for this function:

Octstr* md5digest Octstr data  ) 
 

Definition at line 406 of file md5.c.

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

00407 {
00408     char md5str[33];
00409     Octstr *digest;
00410 
00411     if (data == NULL)
00412         return NULL;
00413 
00414     md5str[0] = '\0';
00415     digest = md5(data);
00416     md5_digest(md5str, octstr_get_cstr(digest));
00417     octstr_destroy(digest);
00418 
00419     digest = octstr_create(md5str);
00420 
00421     return digest;
00422 }

Here is the call graph for this function:


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.