130 #define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d) |
125 #define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d) |
131 #define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca) |
126 #define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca) |
132 #define T63 0x2ad7d2bb |
127 #define T63 0x2ad7d2bb |
133 #define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e) |
128 #define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e) |
134 |
129 |
135 |
130 static inline void Md5Set1(const uint32 *X, uint32 *a, const uint32 *b, const uint32 *c, const uint32 *d, const uint8 k, const uint8 s, const uint32 Ti) |
136 static void |
131 { |
137 md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) |
132 uint32 t = (*b & *c) | (~*b & *d); |
138 { |
133 t += *a + X[k] + Ti; |
139 md5_word_t |
134 *a = ROL(t, s) + *b; |
140 a = pms->abcd[0], b = pms->abcd[1], |
135 } |
141 c = pms->abcd[2], d = pms->abcd[3]; |
136 |
142 md5_word_t t; |
137 static inline void Md5Set2(const uint32 *X, uint32 *a, const uint32 *b, const uint32 *c, const uint32 *d, const uint8 k, const uint8 s, const uint32 Ti) |
143 #if BYTE_ORDER > 0 |
138 { |
144 /* Define storage only for big-endian CPUs. */ |
139 uint32 t = (*b & *d) | (*c & ~*d); |
145 md5_word_t X[16]; |
140 t += *a + X[k] + Ti; |
146 #else |
141 *a = ROL(t, s) + *b; |
147 /* Define storage for little-endian or both types of CPUs. */ |
142 } |
148 md5_word_t xbuf[16]; |
143 |
149 const md5_word_t *X; |
144 |
150 #endif |
145 static inline void Md5Set3(const uint32 *X, uint32 *a, const uint32 *b, const uint32 *c, const uint32 *d, const uint8 k, const uint8 s, const uint32 Ti) |
151 |
146 { |
152 { |
147 uint32 t = *b ^ *c ^ *d; |
153 #if BYTE_ORDER == 0 |
148 t += *a + X[k] + Ti; |
154 /* |
149 *a = ROL(t, s) + *b; |
155 * Determine dynamically whether this is a big-endian or |
150 } |
156 * little-endian machine, since we can use a more efficient |
151 |
157 * algorithm on the latter. |
152 static inline void Md5Set4(const uint32 *X, uint32 *a, const uint32 *b, const uint32 *c, const uint32 *d, const uint8 k, const uint8 s, const uint32 Ti) |
158 */ |
153 { |
159 static const int w = 1; |
154 uint32 t = *c ^ (*b | ~*d); |
160 |
155 t += *a + X[k] + Ti; |
161 if (*((const md5_byte_t *)&w)) /* dynamic little-endian */ |
156 *a = ROL(t, s) + *b; |
162 #endif |
157 } |
163 #if BYTE_ORDER <= 0 /* little-endian */ |
158 |
164 { |
159 Md5::Md5() |
165 /* |
160 { |
166 * On little-endian machines, we can process properly aligned |
161 count[0] = 0; |
167 * data without copying it. |
162 count[1] = 0; |
168 */ |
163 abcd[0] = 0x67452301; |
169 if (!((data - (const md5_byte_t *)0) & 3)) { |
164 abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476; |
170 /* data are properly aligned */ |
165 abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301; |
171 X = (const md5_word_t *)data; |
166 abcd[3] = 0x10325476; |
172 } else { |
167 } |
173 /* not aligned */ |
168 |
174 memcpy(xbuf, data, 64); |
169 void Md5::Process(const uint8 *data /*[64]*/) |
175 X = xbuf; |
170 { |
176 } |
171 uint32 a = this->abcd[0]; |
|
172 uint32 b = this->abcd[1]; |
|
173 uint32 c = this->abcd[2]; |
|
174 uint32 d = this->abcd[3]; |
|
175 |
|
176 uint32 X[16]; |
|
177 |
|
178 /* Convert the uint8 data to uint32 LE */ |
|
179 uint32 *px = (uint32 *)data; |
|
180 for (uint i = 0; i < 16; i++) { |
|
181 X[i] = TO_LE32(*px); |
|
182 px++; |
177 } |
183 } |
178 #endif |
184 |
179 #if BYTE_ORDER == 0 |
185 /* Round 1. */ |
180 else /* dynamic big-endian */ |
186 Md5Set1(X, &a, &b, &c, &d, 0, 7, T1); |
181 #endif |
187 Md5Set1(X, &d, &a, &b, &c, 1, 12, T2); |
182 #if BYTE_ORDER >= 0 /* big-endian */ |
188 Md5Set1(X, &c, &d, &a, &b, 2, 17, T3); |
183 { |
189 Md5Set1(X, &b, &c, &d, &a, 3, 22, T4); |
184 /* |
190 Md5Set1(X, &a, &b, &c, &d, 4, 7, T5); |
185 * On big-endian machines, we must arrange the bytes in the |
191 Md5Set1(X, &d, &a, &b, &c, 5, 12, T6); |
186 * right order. |
192 Md5Set1(X, &c, &d, &a, &b, 6, 17, T7); |
187 */ |
193 Md5Set1(X, &b, &c, &d, &a, 7, 22, T8); |
188 const md5_byte_t *xp = data; |
194 Md5Set1(X, &a, &b, &c, &d, 8, 7, T9); |
189 int i; |
195 Md5Set1(X, &d, &a, &b, &c, 9, 12, T10); |
190 |
196 Md5Set1(X, &c, &d, &a, &b, 10, 17, T11); |
191 # if BYTE_ORDER == 0 |
197 Md5Set1(X, &b, &c, &d, &a, 11, 22, T12); |
192 X = xbuf; /* (dynamic only) */ |
198 Md5Set1(X, &a, &b, &c, &d, 12, 7, T13); |
193 # else |
199 Md5Set1(X, &d, &a, &b, &c, 13, 12, T14); |
194 # define xbuf X /* (static only) */ |
200 Md5Set1(X, &c, &d, &a, &b, 14, 17, T15); |
195 # endif |
201 Md5Set1(X, &b, &c, &d, &a, 15, 22, T16); |
196 for (i = 0; i < 16; ++i, xp += 4) |
202 |
197 xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); |
203 /* Round 2. */ |
|
204 Md5Set2(X, &a, &b, &c, &d, 1, 5, T17); |
|
205 Md5Set2(X, &d, &a, &b, &c, 6, 9, T18); |
|
206 Md5Set2(X, &c, &d, &a, &b, 11, 14, T19); |
|
207 Md5Set2(X, &b, &c, &d, &a, 0, 20, T20); |
|
208 Md5Set2(X, &a, &b, &c, &d, 5, 5, T21); |
|
209 Md5Set2(X, &d, &a, &b, &c, 10, 9, T22); |
|
210 Md5Set2(X, &c, &d, &a, &b, 15, 14, T23); |
|
211 Md5Set2(X, &b, &c, &d, &a, 4, 20, T24); |
|
212 Md5Set2(X, &a, &b, &c, &d, 9, 5, T25); |
|
213 Md5Set2(X, &d, &a, &b, &c, 14, 9, T26); |
|
214 Md5Set2(X, &c, &d, &a, &b, 3, 14, T27); |
|
215 Md5Set2(X, &b, &c, &d, &a, 8, 20, T28); |
|
216 Md5Set2(X, &a, &b, &c, &d, 13, 5, T29); |
|
217 Md5Set2(X, &d, &a, &b, &c, 2, 9, T30); |
|
218 Md5Set2(X, &c, &d, &a, &b, 7, 14, T31); |
|
219 Md5Set2(X, &b, &c, &d, &a, 12, 20, T32); |
|
220 |
|
221 /* Round 3. */ |
|
222 Md5Set3(X, &a, &b, &c, &d, 5, 4, T33); |
|
223 Md5Set3(X, &d, &a, &b, &c, 8, 11, T34); |
|
224 Md5Set3(X, &c, &d, &a, &b, 11, 16, T35); |
|
225 Md5Set3(X, &b, &c, &d, &a, 14, 23, T36); |
|
226 Md5Set3(X, &a, &b, &c, &d, 1, 4, T37); |
|
227 Md5Set3(X, &d, &a, &b, &c, 4, 11, T38); |
|
228 Md5Set3(X, &c, &d, &a, &b, 7, 16, T39); |
|
229 Md5Set3(X, &b, &c, &d, &a, 10, 23, T40); |
|
230 Md5Set3(X, &a, &b, &c, &d, 13, 4, T41); |
|
231 Md5Set3(X, &d, &a, &b, &c, 0, 11, T42); |
|
232 Md5Set3(X, &c, &d, &a, &b, 3, 16, T43); |
|
233 Md5Set3(X, &b, &c, &d, &a, 6, 23, T44); |
|
234 Md5Set3(X, &a, &b, &c, &d, 9, 4, T45); |
|
235 Md5Set3(X, &d, &a, &b, &c, 12, 11, T46); |
|
236 Md5Set3(X, &c, &d, &a, &b, 15, 16, T47); |
|
237 Md5Set3(X, &b, &c, &d, &a, 2, 23, T48); |
|
238 |
|
239 /* Round 4. */ |
|
240 Md5Set4(X, &a, &b, &c, &d, 0, 6, T49); |
|
241 Md5Set4(X, &d, &a, &b, &c, 7, 10, T50); |
|
242 Md5Set4(X, &c, &d, &a, &b, 14, 15, T51); |
|
243 Md5Set4(X, &b, &c, &d, &a, 5, 21, T52); |
|
244 Md5Set4(X, &a, &b, &c, &d, 12, 6, T53); |
|
245 Md5Set4(X, &d, &a, &b, &c, 3, 10, T54); |
|
246 Md5Set4(X, &c, &d, &a, &b, 10, 15, T55); |
|
247 Md5Set4(X, &b, &c, &d, &a, 1, 21, T56); |
|
248 Md5Set4(X, &a, &b, &c, &d, 8, 6, T57); |
|
249 Md5Set4(X, &d, &a, &b, &c, 15, 10, T58); |
|
250 Md5Set4(X, &c, &d, &a, &b, 6, 15, T59); |
|
251 Md5Set4(X, &b, &c, &d, &a, 13, 21, T60); |
|
252 Md5Set4(X, &a, &b, &c, &d, 4, 6, T61); |
|
253 Md5Set4(X, &d, &a, &b, &c, 11, 10, T62); |
|
254 Md5Set4(X, &c, &d, &a, &b, 2, 15, T63); |
|
255 Md5Set4(X, &b, &c, &d, &a, 9, 21, T64); |
|
256 |
|
257 /* Then perform the following additions. (That is increment each |
|
258 * of the four registers by the value it had before this block |
|
259 * was started.) */ |
|
260 this->abcd[0] += a; |
|
261 this->abcd[1] += b; |
|
262 this->abcd[2] += c; |
|
263 this->abcd[3] += d; |
|
264 } |
|
265 |
|
266 void Md5::Append(const void *data, const size_t nbytes) |
|
267 { |
|
268 const uint8 *p = (const uint8 *)data; |
|
269 size_t left = nbytes; |
|
270 const size_t offset = (this->count[0] >> 3) & 63; |
|
271 const uint32 nbits = (uint32)(nbytes << 3); |
|
272 |
|
273 if (nbytes <= 0) return; |
|
274 |
|
275 /* Update the message length. */ |
|
276 this->count[1] += (uint32)(nbytes >> 29); |
|
277 this->count[0] += nbits; |
|
278 |
|
279 if (this->count[0] < nbits) this->count[1]++; |
|
280 |
|
281 /* Process an initial partial block. */ |
|
282 if (offset) { |
|
283 size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes); |
|
284 |
|
285 memcpy(this->buf + offset, p, copy); |
|
286 |
|
287 if (offset + copy < 64) return; |
|
288 |
|
289 p += copy; |
|
290 left -= copy; |
|
291 this->Process(this->buf); |
198 } |
292 } |
199 #endif |
293 |
200 } |
294 /* Process full blocks. */ |
201 |
295 for (; left >= 64; p += 64, left -= 64) this->Process(p); |
202 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) |
296 |
203 |
297 /* Process a final partial block. */ |
204 /* Round 1. */ |
298 if (left) memcpy(this->buf, p, left); |
205 /* Let [abcd k s i] denote the operation |
299 } |
206 a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ |
300 |
207 #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) |
301 void Md5::Finish(uint8 digest[16]) |
208 #define SET(a, b, c, d, k, s, Ti)\ |
302 { |
209 t = a + F(b,c,d) + X[k] + Ti;\ |
303 static const uint8 pad[64] = { |
210 a = ROTATE_LEFT(t, s) + b |
304 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
211 /* Do the following 16 operations. */ |
305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
212 SET(a, b, c, d, 0, 7, T1); |
306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
213 SET(d, a, b, c, 1, 12, T2); |
307 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
214 SET(c, d, a, b, 2, 17, T3); |
308 }; |
215 SET(b, c, d, a, 3, 22, T4); |
309 uint8 data[8]; |
216 SET(a, b, c, d, 4, 7, T5); |
310 uint i; |
217 SET(d, a, b, c, 5, 12, T6); |
311 |
218 SET(c, d, a, b, 6, 17, T7); |
312 /* Save the length before padding. */ |
219 SET(b, c, d, a, 7, 22, T8); |
313 for (i = 0; i < 8; ++i) |
220 SET(a, b, c, d, 8, 7, T9); |
314 data[i] = (uint8)(this->count[i >> 2] >> ((i & 3) << 3)); |
221 SET(d, a, b, c, 9, 12, T10); |
315 |
222 SET(c, d, a, b, 10, 17, T11); |
316 /* Pad to 56 bytes mod 64. */ |
223 SET(b, c, d, a, 11, 22, T12); |
317 this->Append(pad, ((55 - (this->count[0] >> 3)) & 63) + 1); |
224 SET(a, b, c, d, 12, 7, T13); |
318 /* Append the length. */ |
225 SET(d, a, b, c, 13, 12, T14); |
319 this->Append(data, 8); |
226 SET(c, d, a, b, 14, 17, T15); |
320 |
227 SET(b, c, d, a, 15, 22, T16); |
321 for (i = 0; i < 16; ++i) |
228 #undef SET |
322 digest[i] = (uint8)(this->abcd[i >> 2] >> ((i & 3) << 3)); |
229 |
323 } |
230 /* Round 2. */ |
|
231 /* Let [abcd k s i] denote the operation |
|
232 a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */ |
|
233 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) |
|
234 #define SET(a, b, c, d, k, s, Ti)\ |
|
235 t = a + G(b,c,d) + X[k] + Ti;\ |
|
236 a = ROTATE_LEFT(t, s) + b |
|
237 /* Do the following 16 operations. */ |
|
238 SET(a, b, c, d, 1, 5, T17); |
|
239 SET(d, a, b, c, 6, 9, T18); |
|
240 SET(c, d, a, b, 11, 14, T19); |
|
241 SET(b, c, d, a, 0, 20, T20); |
|
242 SET(a, b, c, d, 5, 5, T21); |
|
243 SET(d, a, b, c, 10, 9, T22); |
|
244 SET(c, d, a, b, 15, 14, T23); |
|
245 SET(b, c, d, a, 4, 20, T24); |
|
246 SET(a, b, c, d, 9, 5, T25); |
|
247 SET(d, a, b, c, 14, 9, T26); |
|
248 SET(c, d, a, b, 3, 14, T27); |
|
249 SET(b, c, d, a, 8, 20, T28); |
|
250 SET(a, b, c, d, 13, 5, T29); |
|
251 SET(d, a, b, c, 2, 9, T30); |
|
252 SET(c, d, a, b, 7, 14, T31); |
|
253 SET(b, c, d, a, 12, 20, T32); |
|
254 #undef SET |
|
255 |
|
256 /* Round 3. */ |
|
257 /* Let [abcd k s t] denote the operation |
|
258 a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */ |
|
259 #define H(x, y, z) ((x) ^ (y) ^ (z)) |
|
260 #define SET(a, b, c, d, k, s, Ti)\ |
|
261 t = a + H(b,c,d) + X[k] + Ti;\ |
|
262 a = ROTATE_LEFT(t, s) + b |
|
263 /* Do the following 16 operations. */ |
|
264 SET(a, b, c, d, 5, 4, T33); |
|
265 SET(d, a, b, c, 8, 11, T34); |
|
266 SET(c, d, a, b, 11, 16, T35); |
|
267 SET(b, c, d, a, 14, 23, T36); |
|
268 SET(a, b, c, d, 1, 4, T37); |
|
269 SET(d, a, b, c, 4, 11, T38); |
|
270 SET(c, d, a, b, 7, 16, T39); |
|
271 SET(b, c, d, a, 10, 23, T40); |
|
272 SET(a, b, c, d, 13, 4, T41); |
|
273 SET(d, a, b, c, 0, 11, T42); |
|
274 SET(c, d, a, b, 3, 16, T43); |
|
275 SET(b, c, d, a, 6, 23, T44); |
|
276 SET(a, b, c, d, 9, 4, T45); |
|
277 SET(d, a, b, c, 12, 11, T46); |
|
278 SET(c, d, a, b, 15, 16, T47); |
|
279 SET(b, c, d, a, 2, 23, T48); |
|
280 #undef SET |
|
281 |
|
282 /* Round 4. */ |
|
283 /* Let [abcd k s t] denote the operation |
|
284 a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */ |
|
285 #define I(x, y, z) ((y) ^ ((x) | ~(z))) |
|
286 #define SET(a, b, c, d, k, s, Ti)\ |
|
287 t = a + I(b,c,d) + X[k] + Ti;\ |
|
288 a = ROTATE_LEFT(t, s) + b |
|
289 /* Do the following 16 operations. */ |
|
290 SET(a, b, c, d, 0, 6, T49); |
|
291 SET(d, a, b, c, 7, 10, T50); |
|
292 SET(c, d, a, b, 14, 15, T51); |
|
293 SET(b, c, d, a, 5, 21, T52); |
|
294 SET(a, b, c, d, 12, 6, T53); |
|
295 SET(d, a, b, c, 3, 10, T54); |
|
296 SET(c, d, a, b, 10, 15, T55); |
|
297 SET(b, c, d, a, 1, 21, T56); |
|
298 SET(a, b, c, d, 8, 6, T57); |
|
299 SET(d, a, b, c, 15, 10, T58); |
|
300 SET(c, d, a, b, 6, 15, T59); |
|
301 SET(b, c, d, a, 13, 21, T60); |
|
302 SET(a, b, c, d, 4, 6, T61); |
|
303 SET(d, a, b, c, 11, 10, T62); |
|
304 SET(c, d, a, b, 2, 15, T63); |
|
305 SET(b, c, d, a, 9, 21, T64); |
|
306 #undef SET |
|
307 |
|
308 /* Then perform the following additions. (That is increment each |
|
309 of the four registers by the value it had before this block |
|
310 was started.) */ |
|
311 pms->abcd[0] += a; |
|
312 pms->abcd[1] += b; |
|
313 pms->abcd[2] += c; |
|
314 pms->abcd[3] += d; |
|
315 } |
|
316 |
|
317 void |
|
318 md5_init(md5_state_t *pms) |
|
319 { |
|
320 pms->count[0] = pms->count[1] = 0; |
|
321 pms->abcd[0] = 0x67452301; |
|
322 pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476; |
|
323 pms->abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301; |
|
324 pms->abcd[3] = 0x10325476; |
|
325 } |
|
326 |
|
327 void |
|
328 md5_append(md5_state_t *pms, const void *data, size_t nbytes) |
|
329 { |
|
330 const md5_byte_t *p = (const md5_byte_t *)data; |
|
331 size_t left = nbytes; |
|
332 size_t offset = (pms->count[0] >> 3) & 63; |
|
333 md5_word_t nbits = (md5_word_t)(nbytes << 3); |
|
334 |
|
335 if (nbytes <= 0) |
|
336 return; |
|
337 |
|
338 /* Update the message length. */ |
|
339 pms->count[1] += (md5_word_t)(nbytes >> 29); |
|
340 pms->count[0] += nbits; |
|
341 if (pms->count[0] < nbits) |
|
342 pms->count[1]++; |
|
343 |
|
344 /* Process an initial partial block. */ |
|
345 if (offset) { |
|
346 size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes); |
|
347 |
|
348 memcpy(pms->buf + offset, p, copy); |
|
349 if (offset + copy < 64) |
|
350 return; |
|
351 p += copy; |
|
352 left -= copy; |
|
353 md5_process(pms, pms->buf); |
|
354 } |
|
355 |
|
356 /* Process full blocks. */ |
|
357 for (; left >= 64; p += 64, left -= 64) |
|
358 md5_process(pms, p); |
|
359 |
|
360 /* Process a final partial block. */ |
|
361 if (left) |
|
362 memcpy(pms->buf, p, left); |
|
363 } |
|
364 |
|
365 void |
|
366 md5_finish(md5_state_t *pms, md5_byte_t digest[16]) |
|
367 { |
|
368 static const md5_byte_t pad[64] = { |
|
369 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
371 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
372 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
|
373 }; |
|
374 md5_byte_t data[8]; |
|
375 int i; |
|
376 |
|
377 /* Save the length before padding. */ |
|
378 for (i = 0; i < 8; ++i) |
|
379 data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3)); |
|
380 /* Pad to 56 bytes mod 64. */ |
|
381 md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1); |
|
382 /* Append the length. */ |
|
383 md5_append(pms, data, 8); |
|
384 for (i = 0; i < 16; ++i) |
|
385 digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); |
|
386 } |
|