librsync  2.3.2
Macros | Functions | Variables
mdfour.c File Reference

MD4 message digest algorithm. More...

Go to the source code of this file.

Macros

#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 lshift(x, s)   (((x)<<(s)) | ((x)>>(32-(s))))
 
#define ROUND1(a, b, c, d, k, s)   a = lshift(a + F(b,c,d) + X[k], s)
 
#define ROUND2(a, b, c, d, k, s)   a = lshift(a + G(b,c,d) + X[k] + 0x5A827999U, s)
 
#define ROUND3(a, b, c, d, k, s)   a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1U, s)
 

Functions

static void rs_mdfour_block (rs_mdfour_t *md, void const *p)
 
static void rs_mdfour64 (rs_mdfour_t *m, const void *p)
 Update an MD4 accumulator from a 64-byte chunk.
 
static void copy4 (unsigned char *out, uint32_t const x)
 These next routines are necessary because MD4 is specified in terms of little-endian int32s, but we have a byte buffer.
 
void rs_mdfour_begin (rs_mdfour_t *md)
 
static void rs_mdfour_tail (rs_mdfour_t *m)
 Handle special behaviour for processing the last block of a file when calculating its MD4 checksum.
 
void rs_mdfour_update (rs_mdfour_t *md, void const *in_void, size_t n)
 Feed some data into the MD4 accumulator.
 
void rs_mdfour_result (rs_mdfour_t *md, unsigned char *out)
 
void rs_mdfour (unsigned char *out, void const *in, size_t n)
 

Variables

static unsigned char PADDING [64]
 padding data used for finalising
 

Detailed Description

MD4 message digest algorithm.

Todo:
Perhaps use the MD4 routine from OpenSSL if it's installed. It's probably not worth the trouble.

This was originally written by Andrew Tridgell for use in Samba. It was then modified by;

2002-06-xx: Robert Weber rober.nosp@m.t.we.nosp@m.ber@C.nosp@m.olor.nosp@m.ado.e.nosp@m.du optimisations and fixed >512M support.

2002-06-27: Donovan Baarda abo@m.nosp@m.inki.nosp@m.rri.a.nosp@m.pana.nosp@m..org..nosp@m.au further optimisations and cleanups.

2004-09-09: Simon Law sflla.nosp@m.w@de.nosp@m.bian..nosp@m.org handle little-endian machines that can't do unaligned access (e.g. ia64, pa-risc).

Definition in file mdfour.c.

Macro Definition Documentation

◆ F

#define F (   X,
  Y,
 
)    (((X)&(Y)) | ((~(X))&(Z)))

Definition at line 49 of file mdfour.c.

◆ G

#define G (   X,
  Y,
 
)    (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))

Definition at line 50 of file mdfour.c.

◆ H

#define H (   X,
  Y,
 
)    ((X)^(Y)^(Z))

Definition at line 51 of file mdfour.c.

◆ lshift

#define lshift (   x,
 
)    (((x)<<(s)) | ((x)>>(32-(s))))

Definition at line 52 of file mdfour.c.

◆ ROUND1

#define ROUND1 (   a,
  b,
  c,
  d,
  k,
 
)    a = lshift(a + F(b,c,d) + X[k], s)

Definition at line 54 of file mdfour.c.

◆ ROUND2

#define ROUND2 (   a,
  b,
  c,
  d,
  k,
 
)    a = lshift(a + G(b,c,d) + X[k] + 0x5A827999U, s)

Definition at line 55 of file mdfour.c.

◆ ROUND3

#define ROUND3 (   a,
  b,
  c,
  d,
  k,
 
)    a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1U, s)

Definition at line 56 of file mdfour.c.

Function Documentation

◆ rs_mdfour_block()

static void rs_mdfour_block ( rs_mdfour_t md,
void const *  p 
)
inlinestatic

Definition at line 228 of file mdfour.c.

◆ rs_mdfour64()

static void rs_mdfour64 ( rs_mdfour_t m,
const void *  p 
)
static

Update an MD4 accumulator from a 64-byte chunk.

This cannot be used for the last chunk of the file, which must be padded and contain the file length. rs_mdfour_tail() is used for that.

Todo:
Recode to be fast, and to use system integer types. Perhaps if we can find an mdfour implementation already on the system (e.g. in OpenSSL) then we should use it instead of our own?
Parameters
*mAn rs_mdfour_t instance to accumulate with.
*pAn array of uint32 integers, as read little-endian from the file.

Definition at line 79 of file mdfour.c.

◆ copy4()

static void copy4 ( unsigned char *  out,
uint32_t const  x 
)
inlinestatic

These next routines are necessary because MD4 is specified in terms of little-endian int32s, but we have a byte buffer.

On little-endian platforms, I think we can just use the buffer pointer directly.

There are some nice endianness routines in glib, including assembler variants. If we ever depended on glib, then it could be good to use them instead.

Definition at line 163 of file mdfour.c.

◆ rs_mdfour_begin()

void rs_mdfour_begin ( rs_mdfour_t md)

Definition at line 243 of file mdfour.c.

◆ rs_mdfour_tail()

static void rs_mdfour_tail ( rs_mdfour_t m)
static

Handle special behaviour for processing the last block of a file when calculating its MD4 checksum.

This must be called exactly once per file.

Modified by Robert Weber to use uint64 in order that we can sum files > 2^29 = 512 MB. –Rober.nosp@m.t.We.nosp@m.ber@c.nosp@m.olor.nosp@m.ado.e.nosp@m.du

Definition at line 264 of file mdfour.c.

◆ rs_mdfour_update()

void rs_mdfour_update ( rs_mdfour_t md,
void const *  in_void,
size_t  n 
)

Feed some data into the MD4 accumulator.

Parameters
mdMD4 accumulator.
in_voidData to add.
nNumber of bytes fed in.

Definition at line 292 of file mdfour.c.

◆ rs_mdfour_result()

void rs_mdfour_result ( rs_mdfour_t md,
unsigned char *  out 
)

Definition at line 329 of file mdfour.c.

◆ rs_mdfour()

void rs_mdfour ( unsigned char *  out,
void const *  in,
size_t  n 
)

Definition at line 339 of file mdfour.c.

Variable Documentation

◆ PADDING

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
}

padding data used for finalising

Definition at line 59 of file mdfour.c.