Reformat md4.c

This commit is contained in:
Yamagi Burmeister 2010-08-31 09:29:06 +00:00
parent a497787aab
commit ee120a0bf0

View file

@ -1,12 +1,12 @@
#include <inttypes.h>
/*
Public Domain C source implementation of RFC 1320:
The MD4 Message-Digest Algorithm
http://www.faqs.org/rfcs/rfc1320.html
* Public Domain C source implementation of RFC 1320
* - The MD4 Message-Digest Algorithm -
*
* http://www.faqs.org/rfcs/rfc1320.html
* by Steven Fuller
*/
by Steven Fuller
*/
#include <inttypes.h>
#define ROTATELEFT32(x, s) (((x)<<(s))|((x)>>(32-(s))))
@ -14,18 +14,18 @@ by Steven Fuller
#define G(X, Y, Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
#define H(X, Y, Z) ((X) ^ (Y) ^ (Z))
#define S(a, b, c, d, k, s) { \
a += (F((b), (c), (d)) + X[(k)]); \
a = ROTATELEFT32(a, s); \
}
#define T(a, b, c, d, k, s) { \
a += (G((b), (c), (d)) + X[(k)] + 0x5A827999); \
a = ROTATELEFT32(a, s); \
}
#define U(a, b, c, d, k, s) { \
a += (H((b), (c), (d)) + X[(k)] + 0x6ED9EBA1); \
a = ROTATELEFT32(a, s); \
}
#define S(a, b, c, d, k, s) { \
a += (F((b), (c), (d)) + X[(k)]); \
a = ROTATELEFT32(a, s); \
}
#define T(a, b, c, d, k, s) { \
a += (G((b), (c), (d)) + X[(k)] + 0x5A827999); \
a = ROTATELEFT32(a, s); \
}
#define U(a, b, c, d, k, s) { \
a += (H((b), (c), (d)) + X[(k)] + 0x6ED9EBA1); \
a = ROTATELEFT32(a, s); \
}
static uint32_t X[16];
static uint32_t A, AA;
@ -39,66 +39,103 @@ static void DoMD4()
BB = B;
CC = C;
DD = D;
S(A, B, C, D, 0, 3); S(D, A, B, C, 1, 7); S(C, D, A, B, 2, 11); S(B, C, D, A, 3, 19);
S(A, B, C, D, 4, 3); S(D, A, B, C, 5, 7); S(C, D, A, B, 6, 11); S(B, C, D, A, 7, 19);
S(A, B, C, D, 8, 3); S(D, A, B, C, 9, 7); S(C, D, A, B, 10, 11); S(B, C, D, A, 11, 19);
S(A, B, C, D, 12, 3); S(D, A, B, C, 13, 7); S(C, D, A, B, 14, 11); S(B, C, D, A, 15, 19);
T(A, B, C, D, 0, 3); T(D, A, B, C, 4, 5); T(C, D, A, B, 8, 9); T(B, C, D, A, 12, 13);
T(A, B, C, D, 1, 3); T(D, A, B, C, 5, 5); T(C, D, A, B, 9, 9); T(B, C, D, A, 13, 13);
T(A, B, C, D, 2, 3); T(D, A, B, C, 6, 5); T(C, D, A, B, 10, 9); T(B, C, D, A, 14, 13);
T(A, B, C, D, 3, 3); T(D, A, B, C, 7, 5); T(C, D, A, B, 11, 9); T(B, C, D, A, 15, 13);
U(A, B, C, D, 0, 3); U(D, A, B, C, 8, 9); U(C, D, A, B, 4, 11); U(B, C, D, A, 12, 15);
U(A, B, C, D, 2, 3); U(D, A, B, C, 10, 9); U(C, D, A, B, 6, 11); U(B, C, D, A, 14, 15);
U(A, B, C, D, 1, 3); U(D, A, B, C, 9, 9); U(C, D, A, B, 5, 11); U(B, C, D, A, 13, 15);
U(A, B, C, D, 3, 3); U(D, A, B, C, 11, 9); U(C, D, A, B, 7, 11); U(B, C, D, A, 15, 15);
S(A, B, C, D, 0, 3);
S(D, A, B, C, 1, 7);
S(C, D, A, B, 2, 11);
S(B, C, D, A, 3, 19);
S(A, B, C, D, 4, 3);
S(D, A, B, C, 5, 7);
S(C, D, A, B, 6, 11);
S(B, C, D, A, 7, 19);
S(A, B, C, D, 8, 3);
S(D, A, B, C, 9, 7);
S(C, D, A, B, 10, 11);
S(B, C, D, A, 11, 19);
S(A, B, C, D, 12, 3);
S(D, A, B, C, 13, 7);
S(C, D, A, B, 14, 11);
S(B, C, D, A, 15, 19);
T(A, B, C, D, 0, 3);
T(D, A, B, C, 4, 5);
T(C, D, A, B, 8, 9);
T(B, C, D, A, 12, 13);
T(A, B, C, D, 1, 3);
T(D, A, B, C, 5, 5);
T(C, D, A, B, 9, 9);
T(B, C, D, A, 13, 13);
T(A, B, C, D, 2, 3);
T(D, A, B, C, 6, 5);
T(C, D, A, B, 10, 9);
T(B, C, D, A, 14, 13);
T(A, B, C, D, 3, 3);
T(D, A, B, C, 7, 5);
T(C, D, A, B, 11, 9);
T(B, C, D, A, 15, 13);
U(A, B, C, D, 0, 3);
U(D, A, B, C, 8, 9);
U(C, D, A, B, 4, 11);
U(B, C, D, A, 12, 15);
U(A, B, C, D, 2, 3);
U(D, A, B, C, 10, 9);
U(C, D, A, B, 6, 11);
U(B, C, D, A, 14, 15);
U(A, B, C, D, 1, 3);
U(D, A, B, C, 9, 9);
U(C, D, A, B, 5, 11);
U(B, C, D, A, 13, 15);
U(A, B, C, D, 3, 3);
U(D, A, B, C, 11, 9);
U(C, D, A, B, 7, 11);
U(B, C, D, A, 15, 15);
A += AA;
B += BB;
C += CC;
D += DD;
}
#if 1
static void PerformMD4(const unsigned char *buf, int length, unsigned char *digest)
#else
static void PerformMD4(const unsigned char *buf, int length, uint32_t *digest)
#endif
{
int len = length / 64; /* number of full blocks */
int rem = length % 64; /* number of left over bytes */
int len = length / 64; /* number of full blocks */
int rem = length % 64; /* number of left over bytes */
int i, j;
const unsigned char *ptr = buf;
/* initialize the MD buffer */
A = 0x67452301;
B = 0xEFCDAB89;
C = 0x98BADCFE;
D = 0x10325476;
for (i = 0; i < len; i++) {
for (j = 0; j < 16; j++) {
for (i = 0; i < len; i++)
{
for (j = 0; j < 16; j++)
{
X[j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|
(ptr[2]<<16)|(ptr[3]<<24));
(ptr[2]<<16)|(ptr[3]<<24));
ptr += 4;
}
DoMD4();
}
i = rem / 4;
for (j = 0; j < i; j++) {
for (j = 0; j < i; j++)
{
X[j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|
(ptr[2]<<16)|(ptr[3]<<24));
(ptr[2]<<16)|(ptr[3]<<24));
ptr += 4;
}
switch(rem % 4) {
switch(rem % 4)
{
case 0:
X[j] = 0x80U;
break;
@ -112,21 +149,25 @@ static void PerformMD4(const unsigned char *buf, int length, uint32_t *digest)
X[j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|(ptr[2]<<16)|((0x80U)<<24));
break;
}
j++;
if (j > 14) {
if (j > 14)
{
for (; j < 16; j++)
X[j] = 0;
DoMD4();
j = 0;
}
for (; j < 14; j++)
X[j] = 0;
X[14] = (length & 0x1FFFFFFF) << 3;
X[15] = (length & ~0x1FFFFFFF) >> 29;
DoMD4();
digest[ 0] = (A & 0x000000FF) >> 0;
@ -145,24 +186,25 @@ static void PerformMD4(const unsigned char *buf, int length, uint32_t *digest)
digest[13] = (D & 0x0000FF00) >> 8;
digest[14] = (D & 0x00FF0000) >> 16;
digest[15] = (D & 0xFF000000) >> 24;
A = AA = 0;
B = BB = 0;
C = CC = 0;
D = DD = 0;
for (j = 0; j < 16; j++)
X[j] = 0;
}
/* Quake2-specific function */
unsigned Com_BlockChecksum (void *buffer, int length)
{
uint32_t digest[4];
unsigned val;
PerformMD4((unsigned char *)buffer, length, (unsigned char *)digest);
val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];
return val;
}