Michael Brown 18 лет назад
Родитель
Сommit
013f8c119a
1 измененных файлов: 25 добавлений и 105 удалений
  1. 25
    105
      src/crypto/md5.c

+ 25
- 105
src/crypto/md5.c Просмотреть файл

@@ -22,31 +22,40 @@
22 22
 #include <gpxe/crypto.h>
23 23
 
24 24
 #define MD5_DIGEST_SIZE		16
25
-#define MD5_HMAC_BLOCK_SIZE	64
26 25
 #define MD5_BLOCK_WORDS		16
27 26
 #define MD5_HASH_WORDS		4
28 27
 
28
+struct md5_ctx {
29
+	u32 hash[MD5_HASH_WORDS];
30
+	u32 block[MD5_BLOCK_WORDS];
31
+	u64 byte_count;
32
+};
33
+
29 34
 #define __md5step __attribute__ (( regparm ( 3 ) ))
30 35
 
31 36
 struct md5_step {
32
-	uint32_t __md5step ( * f ) ( uint32_t b, uint32_t c, uint32_t d );
33
-	uint8_t coefficient;
34
-	uint8_t constant;
37
+	u32 __md5step ( * f ) ( u32 b, u32 c, u32 d );
38
+	u8 coefficient;
39
+	u8 constant;
35 40
 };
36 41
 
37
-static uint32_t __md5step f1 ( uint32_t b, uint32_t c, uint32_t d ) {
42
+static u32 __md5step f1(u32 b, u32 c, u32 d)
43
+{
38 44
 	return ( d ^ ( b & ( c ^ d ) ) );
39 45
 }
40 46
 
41
-static uint32_t __md5step f2 ( uint32_t b, uint32_t c, uint32_t d ) {
47
+static u32 __md5step f2(u32 b, u32 c, u32 d)
48
+{
42 49
 	return ( c ^ ( d & ( b ^ c ) ) );
43 50
 }
44 51
 
45
-static uint32_t __md5step f3 ( uint32_t b, uint32_t c, uint32_t d ) {
52
+static u32 __md5step f3(u32 b, u32 c, u32 d)
53
+{
46 54
 	return ( b ^ c ^ d );
47 55
 }
48 56
 
49
-static uint32_t __md5step f4 ( uint32_t b, uint32_t c, uint32_t d ) {
57
+static u32 __md5step f4(u32 b, u32 c, u32 d)
58
+{
50 59
 	return ( c ^ ( b | ~d ) );
51 60
 }
52 61
 
@@ -73,14 +82,14 @@ struct md5_step md5_steps[4] = {
73 82
 	}
74 83
 };
75 84
 
76
-static const uint8_t r[64] = {
85
+static const u8 r[64] = {
77 86
 	7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
78 87
 	5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
79 88
 	4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
80 89
 	6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
81 90
 };
82 91
 
83
-static const uint32_t k[] = {
92
+static const u32 k[] = {
84 93
 	0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL,
85 94
 	0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
86 95
 	0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL,
@@ -100,26 +109,9 @@ static const uint32_t k[] = {
100 109
 	0xe1f27f3aUL, 0xf5710fb0UL, 0xada0e5c4UL, 0x98e4c919UL
101 110
 };
102 111
 
103
-
104
-#define F1(x, y, z)	(z ^ (x & (y ^ z)))
105
-#define F2(x, y, z)	F1(z, x, y)
106
-#define F3(x, y, z)	(x ^ y ^ z)
107
-#define F4(x, y, z)	(y ^ (x | ~z))
108
-
109
-#define MD5STEP(f, w, x, y, z, in, s) do { 			\
110
-		(w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) ; \
111
-		printf ( "a=%#08x, b=%#08x, c=%#08x, d=%#08x\n", a, b, c, d );\
112
-} while ( 0 )
113
-
114
-struct md5_ctx {
115
-	u32 hash[MD5_HASH_WORDS];
116
-	u32 block[MD5_BLOCK_WORDS];
117
-	u64 byte_count;
118
-};
119
-
120 112
 static void md5_transform(u32 *hash, const u32 *in)
121 113
 {
122
-	u32 a, b, c, d, f, g, t, q;
114
+	u32 a, b, c, d, f, g, temp;
123 115
 	int i;
124 116
 	struct md5_step *step;
125 117
 
@@ -128,91 +120,19 @@ static void md5_transform(u32 *hash, const u32 *in)
128 120
 	c = hash[2];
129 121
 	d = hash[3];
130 122
 
131
-#if 1
132
-
133 123
 	for ( i = 0 ; i < 64 ; i++ ) {
134 124
 		step = &md5_steps[i >> 4];
135 125
 		f = step->f ( b, c, d );
136 126
 		g = ( ( i * step->coefficient + step->constant ) & 0xf );
137
-		t = d;
127
+		temp = d;
138 128
 		d = c;
139 129
 		c = b;
140
-		q = ( a + f + k[i] + in[g] );
141
-		b += ( ( q << r[i] ) | ( q >> ( 32-r[i] ) ) );
142
-		a = t;
143
-		printf ( "a=%#08x, b=%#08x, c=%#08x, d=%#08x\n", a, b, c, d );
130
+		a += ( f + k[i] + in[g] );
131
+		a = ( ( a << r[i] ) | ( a >> ( 32-r[i] ) ) );
132
+		b += a;
133
+		a = temp;
144 134
 	}
145 135
 
146
-#else
147
-	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
148
-	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
149
-	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
150
-	MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
151
-	MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
152
-	MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
153
-	MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
154
-	MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
155
-	MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
156
-	MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
157
-	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
158
-	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
159
-	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
160
-	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
161
-	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
162
-	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
163
-
164
-	MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
165
-	MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
166
-	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
167
-	MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
168
-	MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
169
-	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
170
-	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
171
-	MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
172
-	MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
173
-	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
174
-	MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
175
-	MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
176
-	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
177
-	MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
178
-	MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
179
-	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
180
-
181
-	MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
182
-	MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
183
-	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
184
-	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
185
-	MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
186
-	MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
187
-	MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
188
-	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
189
-	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
190
-	MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
191
-	MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
192
-	MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
193
-	MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
194
-	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
195
-	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
196
-	MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
197
-
198
-	MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
199
-	MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
200
-	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
201
-	MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
202
-	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
203
-	MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
204
-	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
205
-	MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
206
-	MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
207
-	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
208
-	MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
209
-	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
210
-	MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
211
-	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
212
-	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
213
-	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
214
-#endif
215
-
216 136
 	hash[0] += a;
217 137
 	hash[1] += b;
218 138
 	hash[2] += c;

Загрузка…
Отмена
Сохранить