Back to index

lightning-sunbird  0.9+nobinonly
Defines | Typedefs | Functions
md4.c File Reference
#include <string.h>
#include "md4.h"

Go to the source code of this file.

Defines

#define F(x, y, z)   (((x) & (y)) | (~(x) & (z)))
#define G(x, y, z)   (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define H(x, y, z)   ((x) ^ (y) ^ (z))
#define ROTL(x, n)   (((x) << (n)) | ((x) >> (0x20 - n)))
#define RD1(a, b, c, d, k, s)   a += F(b,c,d) + X[k]; a = ROTL(a,s)
#define RD2(a, b, c, d, k, s)   a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s)
#define RD3(a, b, c, d, k, s)   a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s)

Typedefs

typedef PRUint32 Uint32
typedef PRUint8 Uint8

Functions

static void w2b (Uint8 *out, const Uint32 *in, Uint32 len)
static void b2w (Uint32 *out, const Uint8 *in, Uint32 len)
static void md4step (Uint32 state[4], const Uint8 *data)
void md4sum (const Uint8 *input, Uint32 inputLen, Uint8 *result)
 md4sum - computes the MD4 sum over the input buffer per RFC 1320

Define Documentation

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

Definition at line 49 of file md4.c.

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

Definition at line 52 of file md4.c.

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

Definition at line 55 of file md4.c.

#define RD1 (   a,
  b,
  c,
  d,
  k,
  s 
)    a += F(b,c,d) + X[k]; a = ROTL(a,s)

Definition at line 61 of file md4.c.

#define RD2 (   a,
  b,
  c,
  d,
  k,
  s 
)    a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s)

Definition at line 64 of file md4.c.

#define RD3 (   a,
  b,
  c,
  d,
  k,
  s 
)    a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s)

Definition at line 67 of file md4.c.

#define ROTL (   x,
  n 
)    (((x) << (n)) | ((x) >> (0x20 - n)))

Definition at line 58 of file md4.c.


Typedef Documentation

typedef PRUint32 Uint32

Definition at line 45 of file md4.c.

typedef PRUint8 Uint8

Definition at line 46 of file md4.c.


Function Documentation

static void b2w ( Uint32 out,
const Uint8 in,
Uint32  len 
) [static]

Definition at line 88 of file md4.c.

{
  Uint32 *wp; const Uint8 *bp, *bpend;

  wp = out;
  bp = in;
  bpend = in + len;

  for (; bp != bpend; bp += 4, ++wp)
  {
    *wp = (Uint32) (bp[0]      ) |
          (Uint32) (bp[1] <<  8) |
          (Uint32) (bp[2] << 16) |
          (Uint32) (bp[3] << 24);
  }
}

Here is the caller graph for this function:

static void md4step ( Uint32  state[4],
const Uint8 data 
) [static]

Definition at line 106 of file md4.c.

{
  Uint32 A, B, C, D, X[16];

  b2w(X, data, 64);

  A = state[0];
  B = state[1];
  C = state[2];
  D = state[3];

  RD1(A,B,C,D, 0,3); RD1(D,A,B,C, 1,7); RD1(C,D,A,B, 2,11); RD1(B,C,D,A, 3,19);
  RD1(A,B,C,D, 4,3); RD1(D,A,B,C, 5,7); RD1(C,D,A,B, 6,11); RD1(B,C,D,A, 7,19);
  RD1(A,B,C,D, 8,3); RD1(D,A,B,C, 9,7); RD1(C,D,A,B,10,11); RD1(B,C,D,A,11,19);
  RD1(A,B,C,D,12,3); RD1(D,A,B,C,13,7); RD1(C,D,A,B,14,11); RD1(B,C,D,A,15,19);

  RD2(A,B,C,D, 0,3); RD2(D,A,B,C, 4,5); RD2(C,D,A,B, 8, 9); RD2(B,C,D,A,12,13); 
  RD2(A,B,C,D, 1,3); RD2(D,A,B,C, 5,5); RD2(C,D,A,B, 9, 9); RD2(B,C,D,A,13,13); 
  RD2(A,B,C,D, 2,3); RD2(D,A,B,C, 6,5); RD2(C,D,A,B,10, 9); RD2(B,C,D,A,14,13); 
  RD2(A,B,C,D, 3,3); RD2(D,A,B,C, 7,5); RD2(C,D,A,B,11, 9); RD2(B,C,D,A,15,13); 

  RD3(A,B,C,D, 0,3); RD3(D,A,B,C, 8,9); RD3(C,D,A,B, 4,11); RD3(B,C,D,A,12,15);
  RD3(A,B,C,D, 2,3); RD3(D,A,B,C,10,9); RD3(C,D,A,B, 6,11); RD3(B,C,D,A,14,15);
  RD3(A,B,C,D, 1,3); RD3(D,A,B,C, 9,9); RD3(C,D,A,B, 5,11); RD3(B,C,D,A,13,15);
  RD3(A,B,C,D, 3,3); RD3(D,A,B,C,11,9); RD3(C,D,A,B, 7,11); RD3(B,C,D,A,15,15);

  state[0] += A;
  state[1] += B;
  state[2] += C;
  state[3] += D;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void md4sum ( const PRUint8 input,
PRUint32  inputLen,
PRUint8 result 
)

md4sum - computes the MD4 sum over the input buffer per RFC 1320

Parameters:
inputbuffer containing input data
inputLenlength of input buffer (number of bytes)
result16-byte buffer that will contain the MD4 sum upon return

NOTE: MD4 is superceded by MD5. do not use MD4 unless required by the protocol you are implementing (e.g., NTLM requires MD4).

NOTE: this interface is designed for relatively small buffers. A streaming interface would make more sense if that were a requirement. Currently, this is good enough for the applications we care about.

Definition at line 138 of file md4.c.

{
  Uint8 final[128];
  Uint32 i, n, m, state[4];

  /* magic initial states */
  state[0] = 0x67452301;
  state[1] = 0xEFCDAB89;
  state[2] = 0x98BADCFE;
  state[3] = 0x10325476;

  /* compute number of complete 64-byte segments contained in input */
  m = inputLen >> 6;

  /* digest first m segments */
  for (i=0; i<m; ++i)
    md4step(state, (input + (i << 6)));

  /* build final buffer */
  n = inputLen % 64;
  memcpy(final, input + (m << 6), n);
  final[n] = 0x80;
  memset(final + n + 1, 0, 120 - (n + 1));

  inputLen = inputLen << 3;
  w2b(final + (n >= 56 ? 120 : 56), &inputLen, 4);

  md4step(state, final);
  if (n >= 56)
    md4step(state, final + 64);

  /* copy state to result */
  w2b(result, state, 16);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void w2b ( Uint8 out,
const Uint32 in,
Uint32  len 
) [static]

Definition at line 70 of file md4.c.

{
  Uint8 *bp; const Uint32 *wp, *wpend;

  bp = out;
  wp = in;
  wpend = wp + (len >> 2);

  for (; wp != wpend; ++wp, bp += 4)
  {
    bp[0] = (Uint8) ((*wp      ) & 0xFF);
    bp[1] = (Uint8) ((*wp >>  8) & 0xFF);
    bp[2] = (Uint8) ((*wp >> 16) & 0xFF);
    bp[3] = (Uint8) ((*wp >> 24) & 0xFF);
  }
}

Here is the caller graph for this function: