Mercurial > pmdwin
comparison lfg.c @ 0:c55ea9478c80
Hello Gensokyo!
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 21 May 2013 10:29:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c55ea9478c80 |
---|---|
1 /*! \file | |
2 * This code implements the MD5 message-digest algorithm. | |
3 * The algorithm is due to Ron Rivest. This code was | |
4 * written by Colin Plumb in 1993, no copyright is claimed. | |
5 * This code is in the public domain; do with it what you wish. | |
6 * | |
7 * Equivalent code is available from RSA Data Security, Inc. | |
8 * This code has been tested against that, and is equivalent, | |
9 * except that you don't need to include two pages of legalese | |
10 * with every copy. | |
11 * | |
12 * To compute the message digest of a chunk of bytes, declare an | |
13 * MD5Context structure, pass it to MD5Init, call MD5Update as | |
14 * needed on buffers full of bytes, and then call MD5Final, which | |
15 * will fill a supplied 16-byte array with the digest. | |
16 */ | |
17 #include <stdint.h> | |
18 #include <string.h> | |
19 | |
20 typedef struct { | |
21 uint32_t buf[4]; | |
22 uint32_t bytes[2]; | |
23 uint32_t in[16]; | |
24 } MD5_CTX; | |
25 #define ROTATE(a,n) ({ register unsigned int ret; \ | |
26 __asm__ volatile("roll %%cl,%0" \ | |
27 : "=r"(ret) \ | |
28 : "c"(n), "0"((unsigned int)(a)) \ | |
29 : "cc"); ret; }) | |
30 | |
31 /*! | |
32 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
33 * initialization constants. | |
34 */ | |
35 void | |
36 MD5Init(MD5_CTX *ctx) { | |
37 ctx->buf[0] = 0x67452301; | |
38 ctx->buf[1] = 0xefcdab89; | |
39 ctx->buf[2] = 0x98badcfe; | |
40 ctx->buf[3] = 0x10325476; | |
41 | |
42 ctx->bytes[0] = 0; | |
43 ctx->bytes[1] = 0; | |
44 } | |
45 | |
46 /*@{*/ | |
47 /*! The four core functions - F1 is optimized somewhat */ | |
48 | |
49 /* #define F1(x, y, z) (x & y | ~x & z) */ | |
50 #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
51 #define F2(x, y, z) F1(z, x, y) | |
52 #define F3(x, y, z) (x ^ y ^ z) | |
53 #define F4(x, y, z) (y ^ (x | ~z)) | |
54 /*@}*/ | |
55 | |
56 /*! This is the central step in the MD5 algorithm. */ | |
57 #define MD5STEP(f,w,x,y,z,in,s) \ | |
58 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) | |
59 | |
60 /*! | |
61 * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
62 * reflect the addition of 16 longwords of new data. MD5Update blocks | |
63 * the data and converts bytes into longwords for this routine. | |
64 */ | |
65 static void | |
66 transform(uint32_t buf[4], uint32_t const in[16]) { | |
67 register uint32_t a, b, c, d; | |
68 | |
69 a = buf[0]; | |
70 b = buf[1]; | |
71 c = buf[2]; | |
72 d = buf[3]; | |
73 | |
74 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
75 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
76 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
77 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
78 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
79 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
80 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
81 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
82 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
83 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
84 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
85 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
86 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
87 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
88 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
89 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
90 | |
91 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
92 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
93 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
94 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
95 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
96 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
97 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
98 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
99 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
100 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
101 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
102 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
103 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
104 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
105 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
106 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
107 | |
108 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
109 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
110 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
111 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
112 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
113 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
114 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
115 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
116 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
117 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
118 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
119 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
120 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
121 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
122 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
123 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
124 | |
125 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
126 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
127 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
128 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
129 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
130 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
131 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
132 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
133 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
134 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
135 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
136 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
137 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
138 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
139 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
140 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
141 | |
142 buf[0] += a; | |
143 buf[1] += b; | |
144 buf[2] += c; | |
145 buf[3] += d; | |
146 } | |
147 | |
148 /*! | |
149 * Update context to reflect the concatenation of another buffer full | |
150 * of bytes. | |
151 */ | |
152 void | |
153 MD5Update(MD5_CTX *ctx, const unsigned char *buf, unsigned int len) { | |
154 uint32_t t; | |
155 | |
156 /* Update byte count */ | |
157 | |
158 t = ctx->bytes[0]; | |
159 if ((ctx->bytes[0] = t + len) < t) | |
160 ctx->bytes[1]++; /* Carry from low to high */ | |
161 | |
162 t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ | |
163 if (t > len) { | |
164 memcpy((unsigned char *)ctx->in + 64 - t, buf, len); | |
165 return; | |
166 } | |
167 /* First chunk is an odd size */ | |
168 memcpy((unsigned char *)ctx->in + 64 - t, buf, t); | |
169 transform(ctx->buf, ctx->in); | |
170 buf += t; | |
171 len -= t; | |
172 | |
173 /* Process data in 64-byte chunks */ | |
174 while (len >= 64) { | |
175 memcpy(ctx->in, buf, 64); | |
176 transform(ctx->buf, ctx->in); | |
177 buf += 64; | |
178 len -= 64; | |
179 } | |
180 | |
181 /* Handle any remaining bytes of data. */ | |
182 memcpy(ctx->in, buf, len); | |
183 } | |
184 | |
185 static inline void small_memset(void *addr, int c, size_t size) { | |
186 __asm__ volatile("xor %%al, %%al \t\n" | |
187 "rep; stosb \t\n" | |
188 :"+D"(addr) :"c"(size) :"%al"); | |
189 } | |
190 | |
191 /*! | |
192 * Final wrapup - pad to 64-byte boundary with the bit pattern | |
193 * 1 0* (64-bit count of bits processed, MSB-first) | |
194 */ | |
195 void | |
196 MD5Final(MD5_CTX *ctx, unsigned char *digest) { | |
197 int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ | |
198 unsigned char *p = (unsigned char *)ctx->in + count; | |
199 | |
200 /* Set the first char of padding to 0x80. There is always room. */ | |
201 *p++ = 0x80; | |
202 | |
203 /* Bytes of padding needed to make 56 bytes (-8..55) */ | |
204 count = 56 - 1 - count; | |
205 | |
206 if (count < 0) { /* Padding forces an extra block */ | |
207 small_memset(p, 0, count + 8); | |
208 transform(ctx->buf, ctx->in); | |
209 p = (unsigned char *)ctx->in; | |
210 count = 56; | |
211 } | |
212 small_memset(p, 0, count); | |
213 | |
214 /* Append length in bits and transform */ | |
215 ctx->in[14] = ctx->bytes[0] << 3; | |
216 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; | |
217 transform(ctx->buf, ctx->in); | |
218 memcpy(digest, ctx->buf, 16); | |
219 } | |
220 | |
221 typedef struct { | |
222 unsigned int state[64]; | |
223 unsigned int index; | |
224 } AVLFG; | |
225 static AVLFG c; | |
226 | |
227 void lfg_srand(unsigned int seed){ | |
228 uint32_t i, tmp[4]={0}; | |
229 MD5_CTX ctx; | |
230 | |
231 for(i=0; i<64; i+=4){ | |
232 tmp[0]=seed; tmp[3]=i; | |
233 MD5Init(&ctx); | |
234 MD5Update(&ctx, (uint8_t*)tmp, 16); | |
235 MD5Final(&ctx, (uint8_t*)tmp); | |
236 c.state[i ]= tmp[0]; | |
237 c.state[i+1]= tmp[1]; | |
238 c.state[i+2]= tmp[2]; | |
239 c.state[i+3]= tmp[3]; | |
240 } | |
241 c.index=0; | |
242 } | |
243 | |
244 /** | |
245 * Get the next random unsigned 32-bit number using an ALFG. | |
246 */ | |
247 unsigned int lfg_rand(void){ | |
248 c.state[c.index & 63] = c.state[(c.index-24) & 63] + c.state[(c.index-55) & 63]; | |
249 return c.state[c.index++ & 63]; | |
250 } | |
251 | |
252 #ifdef TEST | |
253 #include <stdio.h> | |
254 void av_md5_sum(uint8_t *dst, const uint8_t *src, const int len) | |
255 { | |
256 MD5_CTX ctx; | |
257 | |
258 MD5Init(&ctx); | |
259 MD5Update(&ctx, src, len); | |
260 MD5Final(&ctx, dst); | |
261 } | |
262 | |
263 static void print_md5(uint8_t *md5) | |
264 { | |
265 int i; | |
266 for (i = 0; i < 16; i++) | |
267 printf("%02x", md5[i]); | |
268 printf("\n"); | |
269 } | |
270 | |
271 int main(void){ | |
272 uint8_t md5val[16]; | |
273 int i; | |
274 uint8_t in[1000]; | |
275 | |
276 for (i = 0; i < 1000; i++) | |
277 in[i] = i * i; | |
278 av_md5_sum(md5val, in, 1000); print_md5(md5val); | |
279 av_md5_sum(md5val, in, 63); print_md5(md5val); | |
280 av_md5_sum(md5val, in, 64); print_md5(md5val); | |
281 av_md5_sum(md5val, in, 65); print_md5(md5val); | |
282 for (i = 0; i < 1000; i++) | |
283 in[i] = i % 127; | |
284 av_md5_sum(md5val, in, 999); print_md5(md5val); | |
285 | |
286 return 0; | |
287 } | |
288 #endif | |
289 |