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) { \
18 a += (F((b), (c), (d)) + X[(k)]); \
19 a = ROTATELEFT32(a, s); \
21 #define T(a, b, c, d, k, s) { \
22 a += (G((b), (c), (d)) + X[(k)] + 0x5A827999); \
23 a = ROTATELEFT32(a, s); \
25 #define U(a, b, c, d, k, s) { \
26 a += (H((b), (c), (d)) + X[(k)] + 0x6ED9EBA1); \
27 a = ROTATELEFT32(a, s); \
30 static uint32_t
X[16];
31 static uint32_t
A,
AA;
32 static uint32_t
B,
BB;
33 static uint32_t
C,
CC;
34 static uint32_t
D,
DD;
43 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);
44 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);
45 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);
46 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);
48 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);
49 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);
50 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);
51 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);
53 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);
54 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);
55 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);
56 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);
65 static void PerformMD4(
const unsigned char *buf,
int length,
unsigned char *digest)
67 static void PerformMD4(
const unsigned char *buf,
int length, uint32_t *digest)
70 int len = length / 64;
71 int rem = length % 64;
74 const unsigned char *ptr = buf;
82 for (
i = 0;
i < len;
i++) {
83 for (
j = 0;
j < 16;
j++) {
84 X[
j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|
85 (ptr[2]<<16)|(ptr[3]<<24));
94 for (
j = 0;
j <
i;
j++) {
95 X[
j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|
96 (ptr[2]<<16)|(ptr[3]<<24));
106 X[
j] = ((ptr[0]<< 0)|((0x80U)<< 8));
109 X[
j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|((0x80
U)<<16));
112 X[
j] = ((ptr[0]<< 0)|(ptr[1]<< 8)|(ptr[2]<<16)|((0x80U)<<24));
127 X[14] = (length & 0x1FFFFFFF) << 3;
128 X[15] = (length & ~0x1FFFFFFF) >> 29;
133 digest[ 0] = (
A & 0x000000FF) >> 0;
134 digest[ 1] = (
A & 0x0000FF00) >> 8;
135 digest[ 2] = (
A & 0x00FF0000) >> 16;
136 digest[ 3] = (
A & 0xFF000000) >> 24;
137 digest[ 4] = (
B & 0x000000FF) >> 0;
138 digest[ 5] = (
B & 0x0000FF00) >> 8;
139 digest[ 6] = (
B & 0x00FF0000) >> 16;
140 digest[ 7] = (
B & 0xFF000000) >> 24;
141 digest[ 8] = (
C & 0x000000FF) >> 0;
142 digest[ 9] = (
C & 0x0000FF00) >> 8;
143 digest[10] = (
C & 0x00FF0000) >> 16;
144 digest[11] = (
C & 0xFF000000) >> 24;
145 digest[12] = (
D & 0x000000FF) >> 0;
146 digest[13] = (
D & 0x0000FF00) >> 8;
147 digest[14] = (
D & 0x00FF0000) >> 16;
148 digest[15] = (
D & 0xFF000000) >> 24;
150 digest[0] = SWAPLSB(
A);
151 digest[1] = SWAPLSB(
B);
152 digest[2] = SWAPLSB(
C);
153 digest[3] = SWAPLSB(
D);
160 for (
j = 0;
j < 16;
j++)
172 val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];