md5.c (15609B)
1 #include <stdlib.h> /* for malloc() */ 2 #include <string.h> /* for memcpy() */ 3 4 #include "private/md5.h" 5 #include "share/alloc.h" 6 #include "share/compat.h" 7 #include "share/endswap.h" 8 9 /* 10 * This code implements the MD5 message-digest algorithm. 11 * The algorithm is due to Ron Rivest. This code was 12 * written by Colin Plumb in 1993, no copyright is claimed. 13 * This code is in the public domain; do with it what you wish. 14 * 15 * Equivalent code is available from RSA Data Security, Inc. 16 * This code has been tested against that, and is equivalent, 17 * except that you don't need to include two pages of legalese 18 * with every copy. 19 * 20 * To compute the message digest of a chunk of bytes, declare an 21 * MD5Context structure, pass it to MD5Init, call MD5Update as 22 * needed on buffers full of bytes, and then call MD5Final, which 23 * will fill a supplied 16-byte array with the digest. 24 * 25 * Changed so as no longer to depend on Colin Plumb's `usual.h' header 26 * definitions; now uses stuff from dpkg's config.h. 27 * - Ian Jackson <ijackson@nyx.cs.du.edu>. 28 * Still in the public domain. 29 * 30 * Josh Coalson: made some changes to integrate with libFLAC. 31 * Still in the public domain. 32 */ 33 34 /* The four core functions - F1 is optimized somewhat */ 35 36 /* #define F1(x, y, z) (x & y | ~x & z) */ 37 #define F1(x, y, z) (z ^ (x & (y ^ z))) 38 #define F2(x, y, z) F1(z, x, y) 39 #define F3(x, y, z) (x ^ y ^ z) 40 #define F4(x, y, z) (y ^ (x | ~z)) 41 42 /* This is the central step in the MD5 algorithm. */ 43 #define MD5STEP(f,w,x,y,z,in,s) \ 44 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) 45 46 /* 47 * The core of the MD5 algorithm, this alters an existing MD5 hash to 48 * reflect the addition of 16 longwords of new data. MD5Update blocks 49 * the data and converts bytes into longwords for this routine. 50 */ 51 static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16]) 52 { 53 register FLAC__uint32 a, b, c, d; 54 55 a = buf[0]; 56 b = buf[1]; 57 c = buf[2]; 58 d = buf[3]; 59 60 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 61 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 62 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 63 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 64 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 65 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 66 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 67 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 68 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 69 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 70 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 71 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 72 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 73 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 74 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 75 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 76 77 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 78 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 79 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 80 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 81 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 82 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 83 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 84 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 85 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 86 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 87 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 88 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 89 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 90 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 91 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 92 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 93 94 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 95 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 96 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 97 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 98 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 99 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 100 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 101 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 102 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 103 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 104 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 105 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 106 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 107 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 108 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 109 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 110 111 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 112 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 113 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 114 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 115 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 116 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 117 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 118 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 119 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 120 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 121 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 122 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 123 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 124 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 125 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 126 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 127 128 buf[0] += a; 129 buf[1] += b; 130 buf[2] += c; 131 buf[3] += d; 132 } 133 134 #define byteSwap(buf, words) 135 #define byteSwapX16(buf) 136 137 /* 138 * Update context to reflect the concatenation of another buffer full 139 * of bytes. 140 */ 141 static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, uint32_t len) 142 { 143 FLAC__uint32 t; 144 145 /* Update byte count */ 146 147 t = ctx->bytes[0]; 148 if ((ctx->bytes[0] = t + len) < t) 149 ctx->bytes[1]++; /* Carry from low to high */ 150 151 t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ 152 if (t > len) { 153 memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len); 154 return; 155 } 156 /* First chunk is an odd size */ 157 memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t); 158 byteSwapX16(ctx->in); 159 FLAC__MD5Transform(ctx->buf, ctx->in); 160 buf += t; 161 len -= t; 162 163 /* Process data in 64-byte chunks */ 164 while (len >= 64) { 165 memcpy(ctx->in, buf, 64); 166 byteSwapX16(ctx->in); 167 FLAC__MD5Transform(ctx->buf, ctx->in); 168 buf += 64; 169 len -= 64; 170 } 171 172 /* Handle any remaining bytes of data. */ 173 memcpy(ctx->in, buf, len); 174 } 175 176 /* 177 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 178 * initialization constants. 179 */ 180 void FLAC__MD5Init(FLAC__MD5Context *ctx) 181 { 182 ctx->buf[0] = 0x67452301; 183 ctx->buf[1] = 0xefcdab89; 184 ctx->buf[2] = 0x98badcfe; 185 ctx->buf[3] = 0x10325476; 186 187 ctx->bytes[0] = 0; 188 ctx->bytes[1] = 0; 189 190 ctx->internal_buf.p8 = 0; 191 ctx->capacity = 0; 192 } 193 194 /* 195 * Final wrapup - pad to 64-byte boundary with the bit pattern 196 * 1 0* (64-bit count of bits processed, MSB-first) 197 */ 198 void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx) 199 { 200 int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ 201 FLAC__byte *p = (FLAC__byte *)ctx->in + count; 202 203 /* Set the first char of padding to 0x80. There is always room. */ 204 *p++ = 0x80; 205 206 /* Bytes of padding needed to make 56 bytes (-8..55) */ 207 count = 56 - 1 - count; 208 209 if (count < 0) { /* Padding forces an extra block */ 210 memset(p, 0, count + 8); 211 byteSwapX16(ctx->in); 212 FLAC__MD5Transform(ctx->buf, ctx->in); 213 p = (FLAC__byte *)ctx->in; 214 count = 56; 215 } 216 memset(p, 0, count); 217 byteSwap(ctx->in, 14); 218 219 /* Append length in bits and transform */ 220 ctx->in[14] = ctx->bytes[0] << 3; 221 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; 222 FLAC__MD5Transform(ctx->buf, ctx->in); 223 224 byteSwap(ctx->buf, 4); 225 memcpy(digest, ctx->buf, 16); 226 if (0 != ctx->internal_buf.p8) { 227 free(ctx->internal_buf.p8); 228 ctx->internal_buf.p8 = 0; 229 ctx->capacity = 0; 230 } 231 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ 232 } 233 234 /* 235 * Convert the incoming audio signal to a byte stream 236 */ 237 static void format_input_(FLAC__multibyte *mbuf, const FLAC__int32 * const signal[], uint32_t channels, uint32_t samples, uint32_t bytes_per_sample) 238 { 239 FLAC__byte *buf_ = mbuf->p8; 240 FLAC__int16 *buf16 = mbuf->p16; 241 FLAC__int32 *buf32 = mbuf->p32; 242 FLAC__int32 a_word; 243 uint32_t channel, sample; 244 245 /* Storage in the output buffer, buf, is little endian. */ 246 247 #define BYTES_CHANNEL_SELECTOR(bytes, channels) (bytes * 100 + channels) 248 249 /* First do the most commonly used combinations. */ 250 switch (BYTES_CHANNEL_SELECTOR (bytes_per_sample, channels)) { 251 /* One byte per sample. */ 252 case (BYTES_CHANNEL_SELECTOR (1, 1)): 253 for (sample = 0; sample < samples; sample++) 254 *buf_++ = (FLAC__byte)signal[0][sample]; 255 return; 256 257 case (BYTES_CHANNEL_SELECTOR (1, 2)): 258 for (sample = 0; sample < samples; sample++) { 259 *buf_++ = (FLAC__byte)signal[0][sample]; 260 *buf_++ = (FLAC__byte)signal[1][sample]; 261 } 262 return; 263 264 case (BYTES_CHANNEL_SELECTOR (1, 4)): 265 for (sample = 0; sample < samples; sample++) { 266 *buf_++ = (FLAC__byte)signal[0][sample]; 267 *buf_++ = (FLAC__byte)signal[1][sample]; 268 *buf_++ = (FLAC__byte)signal[2][sample]; 269 *buf_++ = (FLAC__byte)signal[3][sample]; 270 } 271 return; 272 273 case (BYTES_CHANNEL_SELECTOR (1, 6)): 274 for (sample = 0; sample < samples; sample++) { 275 *buf_++ = (FLAC__byte)signal[0][sample]; 276 *buf_++ = (FLAC__byte)signal[1][sample]; 277 *buf_++ = (FLAC__byte)signal[2][sample]; 278 *buf_++ = (FLAC__byte)signal[3][sample]; 279 *buf_++ = (FLAC__byte)signal[4][sample]; 280 *buf_++ = (FLAC__byte)signal[5][sample]; 281 } 282 return; 283 284 case (BYTES_CHANNEL_SELECTOR (1, 8)): 285 for (sample = 0; sample < samples; sample++) { 286 *buf_++ = (FLAC__byte)signal[0][sample]; 287 *buf_++ = (FLAC__byte)signal[1][sample]; 288 *buf_++ = (FLAC__byte)signal[2][sample]; 289 *buf_++ = (FLAC__byte)signal[3][sample]; 290 *buf_++ = (FLAC__byte)signal[4][sample]; 291 *buf_++ = (FLAC__byte)signal[5][sample]; 292 *buf_++ = (FLAC__byte)signal[6][sample]; 293 *buf_++ = (FLAC__byte)signal[7][sample]; 294 } 295 return; 296 297 /* Two bytes per sample. */ 298 case (BYTES_CHANNEL_SELECTOR (2, 1)): 299 for (sample = 0; sample < samples; sample++) 300 *buf16++ = (FLAC__int16)H2LE_16(signal[0][sample]); 301 return; 302 303 case (BYTES_CHANNEL_SELECTOR (2, 2)): 304 for (sample = 0; sample < samples; sample++) { 305 *buf16++ = (FLAC__int16)H2LE_16(signal[0][sample]); 306 *buf16++ = (FLAC__int16)H2LE_16(signal[1][sample]); 307 } 308 return; 309 310 case (BYTES_CHANNEL_SELECTOR (2, 4)): 311 for (sample = 0; sample < samples; sample++) { 312 *buf16++ = (FLAC__int16)H2LE_16(signal[0][sample]); 313 *buf16++ = (FLAC__int16)H2LE_16(signal[1][sample]); 314 *buf16++ = (FLAC__int16)H2LE_16(signal[2][sample]); 315 *buf16++ = (FLAC__int16)H2LE_16(signal[3][sample]); 316 } 317 return; 318 319 case (BYTES_CHANNEL_SELECTOR (2, 6)): 320 for (sample = 0; sample < samples; sample++) { 321 *buf16++ = (FLAC__int16)H2LE_16(signal[0][sample]); 322 *buf16++ = (FLAC__int16)H2LE_16(signal[1][sample]); 323 *buf16++ = (FLAC__int16)H2LE_16(signal[2][sample]); 324 *buf16++ = (FLAC__int16)H2LE_16(signal[3][sample]); 325 *buf16++ = (FLAC__int16)H2LE_16(signal[4][sample]); 326 *buf16++ = (FLAC__int16)H2LE_16(signal[5][sample]); 327 } 328 return; 329 330 case (BYTES_CHANNEL_SELECTOR (2, 8)): 331 for (sample = 0; sample < samples; sample++) { 332 *buf16++ = (FLAC__int16)H2LE_16(signal[0][sample]); 333 *buf16++ = (FLAC__int16)H2LE_16(signal[1][sample]); 334 *buf16++ = (FLAC__int16)H2LE_16(signal[2][sample]); 335 *buf16++ = (FLAC__int16)H2LE_16(signal[3][sample]); 336 *buf16++ = (FLAC__int16)H2LE_16(signal[4][sample]); 337 *buf16++ = (FLAC__int16)H2LE_16(signal[5][sample]); 338 *buf16++ = (FLAC__int16)H2LE_16(signal[6][sample]); 339 *buf16++ = (FLAC__int16)H2LE_16(signal[7][sample]); 340 } 341 return; 342 343 /* Three bytes per sample. */ 344 case (BYTES_CHANNEL_SELECTOR (3, 1)): 345 for (sample = 0; sample < samples; sample++) { 346 a_word = signal[0][sample]; 347 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 348 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 349 *buf_++ = (FLAC__byte)a_word; 350 } 351 return; 352 353 case (BYTES_CHANNEL_SELECTOR (3, 2)): 354 for (sample = 0; sample < samples; sample++) { 355 a_word = signal[0][sample]; 356 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 357 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 358 *buf_++ = (FLAC__byte)a_word; 359 a_word = signal[1][sample]; 360 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 361 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 362 *buf_++ = (FLAC__byte)a_word; 363 } 364 return; 365 366 /* Four bytes per sample. */ 367 case (BYTES_CHANNEL_SELECTOR (4, 1)): 368 for (sample = 0; sample < samples; sample++) 369 *buf32++ = H2LE_32(signal[0][sample]); 370 return; 371 372 case (BYTES_CHANNEL_SELECTOR (4, 2)): 373 for (sample = 0; sample < samples; sample++) { 374 *buf32++ = H2LE_32(signal[0][sample]); 375 *buf32++ = H2LE_32(signal[1][sample]); 376 } 377 return; 378 379 case (BYTES_CHANNEL_SELECTOR (4, 4)): 380 for (sample = 0; sample < samples; sample++) { 381 *buf32++ = H2LE_32(signal[0][sample]); 382 *buf32++ = H2LE_32(signal[1][sample]); 383 *buf32++ = H2LE_32(signal[2][sample]); 384 *buf32++ = H2LE_32(signal[3][sample]); 385 } 386 return; 387 388 case (BYTES_CHANNEL_SELECTOR (4, 6)): 389 for (sample = 0; sample < samples; sample++) { 390 *buf32++ = H2LE_32(signal[0][sample]); 391 *buf32++ = H2LE_32(signal[1][sample]); 392 *buf32++ = H2LE_32(signal[2][sample]); 393 *buf32++ = H2LE_32(signal[3][sample]); 394 *buf32++ = H2LE_32(signal[4][sample]); 395 *buf32++ = H2LE_32(signal[5][sample]); 396 } 397 return; 398 399 case (BYTES_CHANNEL_SELECTOR (4, 8)): 400 for (sample = 0; sample < samples; sample++) { 401 *buf32++ = H2LE_32(signal[0][sample]); 402 *buf32++ = H2LE_32(signal[1][sample]); 403 *buf32++ = H2LE_32(signal[2][sample]); 404 *buf32++ = H2LE_32(signal[3][sample]); 405 *buf32++ = H2LE_32(signal[4][sample]); 406 *buf32++ = H2LE_32(signal[5][sample]); 407 *buf32++ = H2LE_32(signal[6][sample]); 408 *buf32++ = H2LE_32(signal[7][sample]); 409 } 410 return; 411 412 default: 413 break; 414 } 415 416 /* General version. */ 417 switch (bytes_per_sample) { 418 case 1: 419 for (sample = 0; sample < samples; sample++) 420 for (channel = 0; channel < channels; channel++) 421 *buf_++ = (FLAC__byte)signal[channel][sample]; 422 return; 423 424 case 2: 425 for (sample = 0; sample < samples; sample++) 426 for (channel = 0; channel < channels; channel++) 427 *buf16++ = (FLAC__int16)H2LE_16(signal[channel][sample]); 428 return; 429 430 case 3: 431 for (sample = 0; sample < samples; sample++) 432 for (channel = 0; channel < channels; channel++) { 433 a_word = signal[channel][sample]; 434 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 435 *buf_++ = (FLAC__byte)a_word; a_word >>= 8; 436 *buf_++ = (FLAC__byte)a_word; 437 } 438 return; 439 440 case 4: 441 for (sample = 0; sample < samples; sample++) 442 for (channel = 0; channel < channels; channel++) 443 *buf32++ = H2LE_32(signal[channel][sample]); 444 return; 445 446 default: 447 break; 448 } 449 } 450 451 /* 452 * Convert the incoming audio signal to a byte stream and FLAC__MD5Update it. 453 */ 454 FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], uint32_t channels, uint32_t samples, uint32_t bytes_per_sample) 455 { 456 const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample; 457 458 /* overflow check */ 459 if ((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample) 460 return false; 461 if ((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples) 462 return false; 463 464 if (ctx->capacity < bytes_needed) { 465 if (0 == (ctx->internal_buf.p8 = safe_realloc_(ctx->internal_buf.p8, bytes_needed))) { 466 if (0 == (ctx->internal_buf.p8 = safe_malloc_(bytes_needed))) { 467 ctx->capacity = 0; 468 return false; 469 } 470 } 471 ctx->capacity = bytes_needed; 472 } 473 474 format_input_(&ctx->internal_buf, signal, channels, samples, bytes_per_sample); 475 476 FLAC__MD5Update(ctx, ctx->internal_buf.p8, (uint32_t)bytes_needed); 477 478 return true; 479 }