My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Only use for md4 hashes < ~50 bytes
Reasonably fast
*/

#define DEBUG 0


#include <inttypes.h>
#include <string.h>

#if DEBUG == 1
#include <stdio.h>
#endif


#define md4_A 0x67452301
#define md4_B 0xefcdab89
#define md4_C 0x98badcfe
#define md4_D 0x10325476

struct md4_context
{
uint32_t A;
uint32_t B;
uint32_t C;
uint32_t D;
uint32_t M[16];
};


inline uint32_t md4_rotl (uint32_t x, uint32_t s)
{
return (x << s) | (x >> (32 - s));
}


inline md4_F (uint32_t x, uint32_t y, uint32_t z)
{
return (x & y) | ((~x) & z);
}


inline md4_G (uint32_t x, uint32_t y, uint32_t z)
{
return (x & y) | (x & z) | (y & z);
}


inline md4_H (uint32_t x, uint32_t y, uint32_t z)
{
return x ^ y ^ z;
}


inline uint32_t l2bendian (uint32_t little)
{
return (little << 24)
| (little >> 24)
| ((little & 0xFF00) << 8)
| ((little & 0xFF0000) >> 8);
}


void md4_begin (struct md4_context * context, unsigned char * data, int data_len)
{

int i;

memset (context->M, 0, 64);
memcpy (context->M, data, data_len);

((unsigned char *) context->M)[data_len] = 0x80;

context->M[14] = data_len * 8;

#if DEBUG == 1
printf("M %x %x %x %x\n", context->M[0], context->M[1], context->M[2], context->M[3]);
printf("M %x %x %x %x\n", context->M[4], context->M[5], context->M[6], context->M[7]);
printf("M %x %x %x %x\n", context->M[8], context->M[9], context->M[10], context->M[11]);
printf("M %x %x %x %x\n", context->M[12], context->M[13], context->M[14], context->M[15]);
#endif
}



void md4_transform (struct md4_context * context)
{

uint32_t a = md4_A;
uint32_t b = md4_B;
uint32_t c = md4_C;
uint32_t d = md4_D;


// round 1
a = md4_rotl(a + md4_F(b, c, d) + context->M[0], 3);
d = md4_rotl(d + md4_F(a, b, c) + context->M[1], 7);
c = md4_rotl(c + md4_F(d, a, b) + context->M[2], 11);
b = md4_rotl(b + md4_F(c, d, a) + context->M[3], 19);

a = md4_rotl(a + md4_F(b, c, d) + context->M[4], 3);
d = md4_rotl(d + md4_F(a, b, c) + context->M[5], 7);
c = md4_rotl(c + md4_F(d, a, b) + context->M[6], 11);
b = md4_rotl(b + md4_F(c, d, a) + context->M[7], 19);

a = md4_rotl(a + md4_F(b, c, d) + context->M[8], 3);
d = md4_rotl(d + md4_F(a, b, c) + context->M[9], 7);
c = md4_rotl(c + md4_F(d, a, b) + context->M[10], 11);
b = md4_rotl(b + md4_F(c, d, a) + context->M[11], 19);

a = md4_rotl(a + md4_F(b, c, d) + context->M[12], 3);
d = md4_rotl(d + md4_F(a, b, c) + context->M[13], 7);
c = md4_rotl(c + md4_F(d, a, b) + context->M[14], 11);
b = md4_rotl(b + md4_F(c, d, a) + context->M[15], 19);

// round 2
a = md4_rotl(a + md4_G(b, c, d) + context->M[0] + 0x5a827999, 3);
d = md4_rotl(d + md4_G(a, b, c) + context->M[4] + 0x5a827999, 5);
c = md4_rotl(c + md4_G(d, a, b) + context->M[8] + 0x5a827999, 9);
b = md4_rotl(b + md4_G(c, d, a) + context->M[12] + 0x5a827999, 13);

a = md4_rotl(a + md4_G(b, c, d) + context->M[1] + 0x5a827999, 3);
d = md4_rotl(d + md4_G(a, b, c) + context->M[5] + 0x5a827999, 5);
c = md4_rotl(c + md4_G(d, a, b) + context->M[9] + 0x5a827999, 9);
b = md4_rotl(b + md4_G(c, d, a) + context->M[13] + 0x5a827999, 13);

a = md4_rotl(a + md4_G(b, c, d) + context->M[2] + 0x5a827999, 3);
d = md4_rotl(d + md4_G(a, b, c) + context->M[6] + 0x5a827999, 5);
c = md4_rotl(c + md4_G(d, a, b) + context->M[10] + 0x5a827999, 9);
b = md4_rotl(b + md4_G(c, d, a) + context->M[14] + 0x5a827999, 13);

a = md4_rotl(a + md4_G(b, c, d) + context->M[3] + 0x5a827999, 3);
d = md4_rotl(d + md4_G(a, b, c) + context->M[7] + 0x5a827999, 5);
c = md4_rotl(c + md4_G(d, a, b) + context->M[11] + 0x5a827999, 9);
b = md4_rotl(b + md4_G(c, d, a) + context->M[15] + 0x5a827999, 13);

// round 3
a = md4_rotl(a + md4_H(b, c, d) + context->M[0] + 0x6ed9eba1, 3);
d = md4_rotl(d + md4_H(a, b, c) + context->M[8] + 0x6ed9eba1, 9);
c = md4_rotl(c + md4_H(d, a, b) + context->M[4] + 0x6ed9eba1, 11);
b = md4_rotl(b + md4_H(c, d, a) + context->M[12] + 0x6ed9eba1, 15);

a = md4_rotl(a + md4_H(b, c, d) + context->M[2] + 0x6ed9eba1, 3);
d = md4_rotl(d + md4_H(a, b, c) + context->M[10] + 0x6ed9eba1, 9);
c = md4_rotl(c + md4_H(d, a, b) + context->M[6] + 0x6ed9eba1, 11);
b = md4_rotl(b + md4_H(c, d, a) + context->M[14] + 0x6ed9eba1, 15);

a = md4_rotl(a + md4_H(b, c, d) + context->M[1] + 0x6ed9eba1, 3);
d = md4_rotl(d + md4_H(a, b, c) + context->M[9] + 0x6ed9eba1, 9);
c = md4_rotl(c + md4_H(d, a, b) + context->M[5] + 0x6ed9eba1, 11);
b = md4_rotl(b + md4_H(c, d, a) + context->M[13] + 0x6ed9eba1, 15);

a = md4_rotl(a + md4_H(b, c, d) + context->M[3] + 0x6ed9eba1, 3);
d = md4_rotl(d + md4_H(a, b, c) + context->M[11] + 0x6ed9eba1, 9);
c = md4_rotl(c + md4_H(d, a, b) + context->M[7] + 0x6ed9eba1, 11);
b = md4_rotl(b + md4_H(c, d, a) + context->M[15] + 0x6ed9eba1, 15);

context->A = md4_A + a;
context->B = md4_B + b;
context->C = md4_C + c;
context->D = md4_D + d;

}

Change log

r36 by endeavormac on Mar 17, 2010   Diff
reasonably md4 implementation for small
plaintexts (NT Hashes)
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4895 bytes, 164 lines
Powered by Google Project Hosting