| 1 | //============================================================================ |
| 2 | // |
| 3 | // SSSS tt lll lll |
| 4 | // SS SS tt ll ll |
| 5 | // SS tttttt eeee ll ll aaaa |
| 6 | // SSSS tt ee ee ll ll aa |
| 7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
| 8 | // SS SS tt ee ll ll aa aa |
| 9 | // SSSS ttt eeeee llll llll aaaaa |
| 10 | // |
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
| 12 | // and the Stella Team |
| 13 | // |
| 14 | // This file is derived from the RSA Data Security, Inc. MD5 Message-Digest |
| 15 | // Algorithm. See the header below for copyright information. |
| 16 | // |
| 17 | // See the file "License.txt" for information on usage and redistribution of |
| 18 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 19 | //============================================================================ |
| 20 | |
| 21 | #include "FSNode.hxx" |
| 22 | #include "MD5.hxx" |
| 23 | |
| 24 | /* |
| 25 | Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All |
| 26 | rights reserved. |
| 27 | |
| 28 | License to copy and use this software is granted provided that it |
| 29 | is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
| 30 | Algorithm" in all material mentioning or referencing this software |
| 31 | or this function. |
| 32 | |
| 33 | License is also granted to make and use derivative works provided |
| 34 | that such works are identified as "derived from the RSA Data |
| 35 | Security, Inc. MD5 Message-Digest Algorithm" in all material |
| 36 | mentioning or referencing the derived work. |
| 37 | |
| 38 | RSA Data Security, Inc. makes no representations concerning either |
| 39 | the merchantability of this software or the suitability of this |
| 40 | software for any particular purpose. It is provided "as is" |
| 41 | without express or implied warranty of any kind. |
| 42 | |
| 43 | These notices must be retained in any copies of any part of this |
| 44 | documentation and/or software. |
| 45 | */ |
| 46 | |
| 47 | namespace MD5 { |
| 48 | |
| 49 | // Setup the types used by the MD5 routines |
| 50 | using POINTER = uInt8*; |
| 51 | |
| 52 | // MD5 context. |
| 53 | struct MD5_CTX |
| 54 | { |
| 55 | uInt32 state[4]; /* state (ABCD) */ |
| 56 | uInt32 count[2]; /* number of bits, modulo 2^64 (lsb first) */ |
| 57 | uInt8 buffer[64]; /* input buffer */ |
| 58 | }; |
| 59 | |
| 60 | // Constants for MD5Transform routine. |
| 61 | #define S11 7 |
| 62 | #define S12 12 |
| 63 | #define S13 17 |
| 64 | #define S14 22 |
| 65 | #define S21 5 |
| 66 | #define S22 9 |
| 67 | #define S23 14 |
| 68 | #define S24 20 |
| 69 | #define S31 4 |
| 70 | #define S32 11 |
| 71 | #define S33 16 |
| 72 | #define S34 23 |
| 73 | #define S41 6 |
| 74 | #define S42 10 |
| 75 | #define S43 15 |
| 76 | #define S44 21 |
| 77 | |
| 78 | static void MD5Init(MD5_CTX*); |
| 79 | static void MD5Update(MD5_CTX*, const uInt8*, uInt32); |
| 80 | static void MD5Final(uInt8[16], MD5_CTX*); |
| 81 | static void MD5Transform(uInt32 [4], const uInt8 [64]); |
| 82 | static void Encode(uInt8*, uInt32*, uInt32); |
| 83 | static void Decode(uInt32*, const uInt8*, uInt32); |
| 84 | |
| 85 | static uInt8 PADDING[64] = { |
| 86 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 87 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 88 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 89 | }; |
| 90 | |
| 91 | // F, G, H and I are basic MD5 functions. |
| 92 | #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
| 93 | #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
| 94 | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
| 95 | #define I(x, y, z) ((y) ^ ((x) | (~z))) |
| 96 | |
| 97 | // ROTATE_LEFT rotates x left n bits. |
| 98 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
| 99 | |
| 100 | // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. |
| 101 | // Rotation is separate from addition to prevent recomputation. |
| 102 | #define FF(a, b, c, d, x, s, ac) { \ |
| 103 | (a) += F ((b), (c), (d)) + (x) + uInt32(ac); \ |
| 104 | (a) = ROTATE_LEFT ((a), (s)); \ |
| 105 | (a) += (b); \ |
| 106 | } |
| 107 | #define GG(a, b, c, d, x, s, ac) { \ |
| 108 | (a) += G ((b), (c), (d)) + (x) + uInt32(ac); \ |
| 109 | (a) = ROTATE_LEFT ((a), (s)); \ |
| 110 | (a) += (b); \ |
| 111 | } |
| 112 | #define HH(a, b, c, d, x, s, ac) { \ |
| 113 | (a) += H ((b), (c), (d)) + (x) + uInt32(ac); \ |
| 114 | (a) = ROTATE_LEFT ((a), (s)); \ |
| 115 | (a) += (b); \ |
| 116 | } |
| 117 | #define II(a, b, c, d, x, s, ac) { \ |
| 118 | (a) += I ((b), (c), (d)) + (x) + uInt32(ac); \ |
| 119 | (a) = ROTATE_LEFT ((a), (s)); \ |
| 120 | (a) += (b); \ |
| 121 | } |
| 122 | |
| 123 | // MD5 initialization. Begins an MD5 operation, writing a new context. |
| 124 | static void MD5Init(MD5_CTX* context) |
| 125 | { |
| 126 | context->count[0] = context->count[1] = 0; |
| 127 | /* Load magic initialization constants. */ |
| 128 | context->state[0] = 0x67452301; |
| 129 | context->state[1] = 0xefcdab89; |
| 130 | context->state[2] = 0x98badcfe; |
| 131 | context->state[3] = 0x10325476; |
| 132 | } |
| 133 | |
| 134 | // MD5 block update operation. Continues an MD5 message-digest |
| 135 | // operation, processing another message block, and updating the |
| 136 | // context. |
| 137 | static void MD5Update(MD5_CTX* context, const uInt8* input, |
| 138 | uInt32 inputLen) |
| 139 | { |
| 140 | uInt32 i, index, partLen; |
| 141 | |
| 142 | /* Compute number of bytes mod 64 */ |
| 143 | index = uInt32((context->count[0] >> 3) & 0x3F); |
| 144 | |
| 145 | /* Update number of bits */ |
| 146 | if ((context->count[0] += (uInt32(inputLen) << 3)) |
| 147 | < (uInt32(inputLen) << 3)) |
| 148 | context->count[1]++; |
| 149 | context->count[1] += (uInt32(inputLen) >> 29); |
| 150 | |
| 151 | partLen = 64 - index; |
| 152 | |
| 153 | /* Transform as many times as possible. */ |
| 154 | if (inputLen >= partLen) { |
| 155 | memcpy (const_cast<POINTER>(&context->buffer[index]), |
| 156 | const_cast<POINTER>(input), partLen); |
| 157 | MD5Transform (context->state, context->buffer); |
| 158 | |
| 159 | for (i = partLen; i + 63 < inputLen; i += 64) |
| 160 | MD5Transform (context->state, &input[i]); |
| 161 | |
| 162 | index = 0; |
| 163 | } |
| 164 | else |
| 165 | i = 0; |
| 166 | |
| 167 | /* Buffer remaining input */ |
| 168 | memcpy(const_cast<POINTER>(&context->buffer[index]), |
| 169 | const_cast<POINTER>(&input[i]), inputLen-i); |
| 170 | } |
| 171 | |
| 172 | // MD5 finalization. Ends an MD5 message-digest operation, writing the |
| 173 | // the message digest and zeroizing the context. |
| 174 | static void MD5Final(uInt8 digest[16], MD5_CTX* context) |
| 175 | { |
| 176 | uInt8 bits[8]; |
| 177 | uInt32 index, padLen; |
| 178 | |
| 179 | /* Save number of bits */ |
| 180 | Encode (bits, context->count, 8); |
| 181 | |
| 182 | /* Pad out to 56 mod 64. */ |
| 183 | index = uInt32((context->count[0] >> 3) & 0x3f); |
| 184 | padLen = (index < 56) ? (56 - index) : (120 - index); |
| 185 | MD5Update (context, PADDING, padLen); |
| 186 | |
| 187 | /* Append length (before padding) */ |
| 188 | MD5Update (context, bits, 8); |
| 189 | /* Store state in digest */ |
| 190 | Encode (digest, context->state, 16); |
| 191 | |
| 192 | /* Zeroize sensitive information. */ |
| 193 | memset (reinterpret_cast<POINTER>(context), 0, sizeof(*context)); |
| 194 | } |
| 195 | |
| 196 | // MD5 basic transformation. Transforms state based on block. |
| 197 | static void MD5Transform(uInt32 state[4], const uInt8 block[64]) |
| 198 | { |
| 199 | uInt32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; |
| 200 | |
| 201 | Decode (x, block, 64); |
| 202 | |
| 203 | /* Round 1 */ |
| 204 | FF (a, b, c, d, x[ 0], S11, 0xd76aa478) /* 1 */ |
| 205 | FF (d, a, b, c, x[ 1], S12, 0xe8c7b756) /* 2 */ |
| 206 | FF (c, d, a, b, x[ 2], S13, 0x242070db) /* 3 */ |
| 207 | FF (b, c, d, a, x[ 3], S14, 0xc1bdceee) /* 4 */ |
| 208 | FF (a, b, c, d, x[ 4], S11, 0xf57c0faf) /* 5 */ |
| 209 | FF (d, a, b, c, x[ 5], S12, 0x4787c62a) /* 6 */ |
| 210 | FF (c, d, a, b, x[ 6], S13, 0xa8304613) /* 7 */ |
| 211 | FF (b, c, d, a, x[ 7], S14, 0xfd469501) /* 8 */ |
| 212 | FF (a, b, c, d, x[ 8], S11, 0x698098d8) /* 9 */ |
| 213 | FF (d, a, b, c, x[ 9], S12, 0x8b44f7af) /* 10 */ |
| 214 | FF (c, d, a, b, x[10], S13, 0xffff5bb1) /* 11 */ |
| 215 | FF (b, c, d, a, x[11], S14, 0x895cd7be) /* 12 */ |
| 216 | FF (a, b, c, d, x[12], S11, 0x6b901122) /* 13 */ |
| 217 | FF (d, a, b, c, x[13], S12, 0xfd987193) /* 14 */ |
| 218 | FF (c, d, a, b, x[14], S13, 0xa679438e) /* 15 */ |
| 219 | FF (b, c, d, a, x[15], S14, 0x49b40821) /* 16 */ |
| 220 | |
| 221 | /* Round 2 */ |
| 222 | GG (a, b, c, d, x[ 1], S21, 0xf61e2562) /* 17 */ |
| 223 | GG (d, a, b, c, x[ 6], S22, 0xc040b340) /* 18 */ |
| 224 | GG (c, d, a, b, x[11], S23, 0x265e5a51) /* 19 */ |
| 225 | GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa) /* 20 */ |
| 226 | GG (a, b, c, d, x[ 5], S21, 0xd62f105d) /* 21 */ |
| 227 | GG (d, a, b, c, x[10], S22, 0x2441453) /* 22 */ |
| 228 | GG (c, d, a, b, x[15], S23, 0xd8a1e681) /* 23 */ |
| 229 | GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8) /* 24 */ |
| 230 | GG (a, b, c, d, x[ 9], S21, 0x21e1cde6) /* 25 */ |
| 231 | GG (d, a, b, c, x[14], S22, 0xc33707d6) /* 26 */ |
| 232 | GG (c, d, a, b, x[ 3], S23, 0xf4d50d87) /* 27 */ |
| 233 | GG (b, c, d, a, x[ 8], S24, 0x455a14ed) /* 28 */ |
| 234 | GG (a, b, c, d, x[13], S21, 0xa9e3e905) /* 29 */ |
| 235 | GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8) /* 30 */ |
| 236 | GG (c, d, a, b, x[ 7], S23, 0x676f02d9) /* 31 */ |
| 237 | GG (b, c, d, a, x[12], S24, 0x8d2a4c8a) /* 32 */ |
| 238 | |
| 239 | /* Round 3 */ |
| 240 | HH (a, b, c, d, x[ 5], S31, 0xfffa3942) /* 33 */ |
| 241 | HH (d, a, b, c, x[ 8], S32, 0x8771f681) /* 34 */ |
| 242 | HH (c, d, a, b, x[11], S33, 0x6d9d6122) /* 35 */ |
| 243 | HH (b, c, d, a, x[14], S34, 0xfde5380c) /* 36 */ |
| 244 | HH (a, b, c, d, x[ 1], S31, 0xa4beea44) /* 37 */ |
| 245 | HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9) /* 38 */ |
| 246 | HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60) /* 39 */ |
| 247 | HH (b, c, d, a, x[10], S34, 0xbebfbc70) /* 40 */ |
| 248 | HH (a, b, c, d, x[13], S31, 0x289b7ec6) /* 41 */ |
| 249 | HH (d, a, b, c, x[ 0], S32, 0xeaa127fa) /* 42 */ |
| 250 | HH (c, d, a, b, x[ 3], S33, 0xd4ef3085) /* 43 */ |
| 251 | HH (b, c, d, a, x[ 6], S34, 0x4881d05) /* 44 */ |
| 252 | HH (a, b, c, d, x[ 9], S31, 0xd9d4d039) /* 45 */ |
| 253 | HH (d, a, b, c, x[12], S32, 0xe6db99e5) /* 46 */ |
| 254 | HH (c, d, a, b, x[15], S33, 0x1fa27cf8) /* 47 */ |
| 255 | HH (b, c, d, a, x[ 2], S34, 0xc4ac5665) /* 48 */ |
| 256 | |
| 257 | /* Round 4 */ |
| 258 | II (a, b, c, d, x[ 0], S41, 0xf4292244) /* 49 */ |
| 259 | II (d, a, b, c, x[ 7], S42, 0x432aff97) /* 50 */ |
| 260 | II (c, d, a, b, x[14], S43, 0xab9423a7) /* 51 */ |
| 261 | II (b, c, d, a, x[ 5], S44, 0xfc93a039) /* 52 */ |
| 262 | II (a, b, c, d, x[12], S41, 0x655b59c3) /* 53 */ |
| 263 | II (d, a, b, c, x[ 3], S42, 0x8f0ccc92) /* 54 */ |
| 264 | II (c, d, a, b, x[10], S43, 0xffeff47d) /* 55 */ |
| 265 | II (b, c, d, a, x[ 1], S44, 0x85845dd1) /* 56 */ |
| 266 | II (a, b, c, d, x[ 8], S41, 0x6fa87e4f) /* 57 */ |
| 267 | II (d, a, b, c, x[15], S42, 0xfe2ce6e0) /* 58 */ |
| 268 | II (c, d, a, b, x[ 6], S43, 0xa3014314) /* 59 */ |
| 269 | II (b, c, d, a, x[13], S44, 0x4e0811a1) /* 60 */ |
| 270 | II (a, b, c, d, x[ 4], S41, 0xf7537e82) /* 61 */ |
| 271 | II (d, a, b, c, x[11], S42, 0xbd3af235) /* 62 */ |
| 272 | II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb) /* 63 */ |
| 273 | II (b, c, d, a, x[ 9], S44, 0xeb86d391) /* 64 */ |
| 274 | |
| 275 | state[0] += a; |
| 276 | state[1] += b; |
| 277 | state[2] += c; |
| 278 | state[3] += d; |
| 279 | |
| 280 | /* Zeroize sensitive information. */ |
| 281 | memset (reinterpret_cast<POINTER>(x), 0, sizeof(x)); |
| 282 | } |
| 283 | |
| 284 | // Encodes input (uInt32) into output (uInt8). Assumes len is |
| 285 | // a multiple of 4. |
| 286 | static void Encode(uInt8* output, uInt32* input, uInt32 len) |
| 287 | { |
| 288 | uInt32 i, j; |
| 289 | |
| 290 | for (i = 0, j = 0; j < len; ++i, j += 4) { |
| 291 | output[j] = uInt8(input[i] & 0xff); |
| 292 | output[j+1] = uInt8((input[i] >> 8) & 0xff); |
| 293 | output[j+2] = uInt8((input[i] >> 16) & 0xff); |
| 294 | output[j+3] = uInt8((input[i] >> 24) & 0xff); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Decodes input (uInt8) into output (uInt32). Assumes len is |
| 299 | // a multiple of 4. |
| 300 | static void Decode(uInt32* output, const uInt8* input, uInt32 len) |
| 301 | { |
| 302 | uInt32 i, j; |
| 303 | |
| 304 | for (i = 0, j = 0; j < len; ++i, j += 4) |
| 305 | output[i] = (uInt32(input[j])) | ((uInt32(input[j+1])) << 8) | |
| 306 | ((uInt32(input[j+2])) << 16) | ((uInt32(input[j+3])) << 24); |
| 307 | } |
| 308 | |
| 309 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 310 | string hash(const ByteBuffer& buffer, uInt32 length) |
| 311 | { |
| 312 | return hash(buffer.get(), length); |
| 313 | } |
| 314 | |
| 315 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 316 | string hash(const uInt8* buffer, uInt32 length) |
| 317 | { |
| 318 | char hex[] = "0123456789abcdef" ; |
| 319 | MD5_CTX context; |
| 320 | uInt8 md5[16]; |
| 321 | |
| 322 | MD5Init(&context); |
| 323 | MD5Update(&context, buffer, length); |
| 324 | MD5Final(md5, &context); |
| 325 | |
| 326 | string result; |
| 327 | for(int t = 0; t < 16; ++t) |
| 328 | { |
| 329 | result += hex[(md5[t] >> 4) & 0x0f]; |
| 330 | result += hex[md5[t] & 0x0f]; |
| 331 | } |
| 332 | |
| 333 | return result; |
| 334 | } |
| 335 | |
| 336 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 337 | string hash(const FilesystemNode& node) |
| 338 | { |
| 339 | ByteBuffer image; |
| 340 | uInt32 size = 0; |
| 341 | try |
| 342 | { |
| 343 | size = node.read(image); |
| 344 | } |
| 345 | catch(...) |
| 346 | { |
| 347 | return EmptyString; |
| 348 | } |
| 349 | |
| 350 | const string& md5 = hash(image, size); |
| 351 | return md5; |
| 352 | } |
| 353 | |
| 354 | } // Namespace MD5 |
| 355 | |