Преглед изворни кода

Reduced size from 2087 bytes to 1056 bytes

tags/v0.9.3
Michael Brown пре 18 година
родитељ
комит
2eed0bb8e5
1 измењених фајлова са 100 додато и 4 уклоњено
  1. 100
    4
      src/crypto/md5.c

+ 100
- 4
src/crypto/md5.c Прегледај датотеку

@@ -26,13 +26,90 @@
26 26
 #define MD5_BLOCK_WORDS		16
27 27
 #define MD5_HASH_WORDS		4
28 28
 
29
+#define __md5step __attribute__ (( regparm ( 3 ) ))
30
+
31
+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;
35
+};
36
+
37
+static uint32_t __md5step f1 ( uint32_t b, uint32_t c, uint32_t d ) {
38
+	return ( d ^ ( b & ( c ^ d ) ) );
39
+}
40
+
41
+static uint32_t __md5step f2 ( uint32_t b, uint32_t c, uint32_t d ) {
42
+	return ( c ^ ( d & ( b ^ c ) ) );
43
+}
44
+
45
+static uint32_t __md5step f3 ( uint32_t b, uint32_t c, uint32_t d ) {
46
+	return ( b ^ c ^ d );
47
+}
48
+
49
+static uint32_t __md5step f4 ( uint32_t b, uint32_t c, uint32_t d ) {
50
+	return ( c ^ ( b | ~d ) );
51
+}
52
+
53
+struct md5_step md5_steps[4] = {
54
+	{
55
+		.f = f1,
56
+		.coefficient = 1,
57
+		.constant = 0,
58
+	},
59
+	{
60
+		.f = f2,
61
+		.coefficient = 5,
62
+		.constant = 1,
63
+	},
64
+	{
65
+		.f = f3,
66
+		.coefficient = 3,
67
+		.constant = 5,
68
+	},
69
+	{
70
+		.f = f4,
71
+		.coefficient = 7,
72
+		.constant = 0,
73
+	}
74
+};
75
+
76
+static const uint8_t r[64] = {
77
+	7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
78
+	5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
79
+	4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
80
+	6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
81
+};
82
+
83
+static const uint32_t k[] = {
84
+	0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL,
85
+	0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
86
+	0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL,
87
+	0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL,
88
+	0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL,
89
+	0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL,
90
+	0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL,
91
+	0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL,
92
+	0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL,
93
+	0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL,
94
+	0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL,
95
+	0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL,
96
+	0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL,
97
+	0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,
98
+	0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL,
99
+	0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL,
100
+	0xe1f27f3aUL, 0xf5710fb0UL, 0xada0e5c4UL, 0x98e4c919UL
101
+};
102
+
103
+
29 104
 #define F1(x, y, z)	(z ^ (x & (y ^ z)))
30 105
 #define F2(x, y, z)	F1(z, x, y)
31 106
 #define F3(x, y, z)	(x ^ y ^ z)
32 107
 #define F4(x, y, z)	(y ^ (x | ~z))
33 108
 
34
-#define MD5STEP(f, w, x, y, z, in, s) \
35
-	(w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
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 )
36 113
 
37 114
 struct md5_ctx {
38 115
 	u32 hash[MD5_HASH_WORDS];
@@ -40,15 +117,33 @@ struct md5_ctx {
40 117
 	u64 byte_count;
41 118
 };
42 119
 
43
-static void md5_transform(u32 *hash, u32 const *in)
120
+static void md5_transform(u32 *hash, const u32 *in)
44 121
 {
45
-	u32 a, b, c, d;
122
+	u32 a, b, c, d, f, g, t, q;
123
+	int i;
124
+	struct md5_step *step;
46 125
 
47 126
 	a = hash[0];
48 127
 	b = hash[1];
49 128
 	c = hash[2];
50 129
 	d = hash[3];
51 130
 
131
+#if 1
132
+
133
+	for ( i = 0 ; i < 64 ; i++ ) {
134
+		step = &md5_steps[i >> 4];
135
+		f = step->f ( b, c, d );
136
+		g = ( ( i * step->coefficient + step->constant ) & 0xf );
137
+		t = d;
138
+		d = c;
139
+		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 );
144
+	}
145
+
146
+#else
52 147
 	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
53 148
 	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
54 149
 	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
@@ -116,6 +211,7 @@ static void md5_transform(u32 *hash, u32 const *in)
116 211
 	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
117 212
 	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
118 213
 	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
214
+#endif
119 215
 
120 216
 	hash[0] += a;
121 217
 	hash[1] += b;

Loading…
Откажи
Сачувај