11 #define ROTATELEFT32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
13 #define F(X, Y, Z) (((X)&(Y)) | ((~X) & (Z)))
14 #define G(X, Y, Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
15 #define H(X, Y, Z) ((X) ^ (Y) ^ (Z))
17 #define S(a, b, c, d, k, s) \
19 a += (F((b), (c), (d)) + X[(k)]); \
20 a = ROTATELEFT32(a, s); \
22 #define T(a, b, c, d, k, s) \
24 a += (G((b), (c), (d)) + X[(k)] + 0x5A827999); \
25 a = ROTATELEFT32(a, s); \
27 #define U(a, b, c, d, k, s) \
29 a += (H((b), (c), (d)) + X[(k)] + 0x6ED9EBA1); \
30 a = ROTATELEFT32(a, s); \
33 static uint32_t
X[16];
34 static uint32_t
A,
AA;
35 static uint32_t
B,
BB;
36 static uint32_t
C,
CC;
37 static uint32_t
D,
DD;
57 S(
C,
D,
A,
B, 10, 11);
58 S(
B,
C,
D,
A, 11, 19);
61 S(
C,
D,
A,
B, 14, 11);
62 S(
B,
C,
D,
A, 15, 19);
67 T(
B,
C,
D,
A, 12, 13);
71 T(
B,
C,
D,
A, 13, 13);
75 T(
B,
C,
D,
A, 14, 13);
79 T(
B,
C,
D,
A, 15, 13);
84 U(
B,
C,
D,
A, 12, 15);
88 U(
B,
C,
D,
A, 14, 15);
92 U(
B,
C,
D,
A, 13, 15);
96 U(
B,
C,
D,
A, 15, 15);
105 PerformMD4(
const unsigned char *buf,
int length,
unsigned char *digest)
107 int len = length / 64;
108 int rem = length % 64;
111 const unsigned char *ptr = buf;
119 for (
i = 0;
i < len;
i++)
121 for (
j = 0;
j < 16;
j++)
123 X[
j] = ((ptr[0] << 0) | (ptr[1] << 8) |
124 (ptr[2] << 16) | (ptr[3] << 24));
134 for (
j = 0;
j <
i;
j++)
136 X[
j] = ((ptr[0] << 0) | (ptr[1] << 8) |
137 (ptr[2] << 16) | (ptr[3] << 24));
148 X[
j] = ((ptr[0] << 0) | ((0x80U) << 8));
151 X[
j] = ((ptr[0] << 0) | (ptr[1] << 8) | ((0x80
U) << 16));
156 0) | (ptr[1] << 8) | (ptr[2] << 16) | ((0x80U) << 24));
179 X[14] = (length & 0x1FFFFFFF) << 3;
180 X[15] = (length & ~0x1FFFFFFF) >> 29;
184 digest[0] = (
A & 0x000000FF) >> 0;
185 digest[1] = (
A & 0x0000FF00) >> 8;
186 digest[2] = (
A & 0x00FF0000) >> 16;
187 digest[3] = (
A & 0xFF000000) >> 24;
188 digest[4] = (
B & 0x000000FF) >> 0;
189 digest[5] = (
B & 0x0000FF00) >> 8;
190 digest[6] = (
B & 0x00FF0000) >> 16;
191 digest[7] = (
B & 0xFF000000) >> 24;
192 digest[8] = (
C & 0x000000FF) >> 0;
193 digest[9] = (
C & 0x0000FF00) >> 8;
194 digest[10] = (
C & 0x00FF0000) >> 16;
195 digest[11] = (
C & 0xFF000000) >> 24;
196 digest[12] = (
D & 0x000000FF) >> 0;
197 digest[13] = (
D & 0x0000FF00) >> 8;
198 digest[14] = (
D & 0x00FF0000) >> 16;
199 digest[15] = (
D & 0xFF000000) >> 24;
206 for (
j = 0;
j < 16;
j++)
220 val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];