Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | |
|
Tom Sepez
2016/11/21 18:18:26
We have an equivalent already in PDFium somewhere.
Tom Sepez
2016/11/21 18:23:16
https://cs.chromium.org/chromium/src/third_party/p
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // The original file was copied from sqlite, and was in the public domain. | |
| 6 | |
| 7 /* | |
| 8 * This code implements the MD5 message-digest algorithm. | |
| 9 * The algorithm is due to Ron Rivest. This code was | |
| 10 * written by Colin Plumb in 1993, no copyright is claimed. | |
| 11 * This code is in the public domain; do with it what you wish. | |
| 12 * | |
| 13 * Equivalent code is available from RSA Data Security, Inc. | |
| 14 * This code has been tested against that, and is equivalent, | |
| 15 * except that you don't need to include two pages of legalese | |
|
npm
2016/11/21 16:15:18
I thought this was you, but then I saw it was copy
| |
| 16 * with every copy. | |
| 17 * | |
| 18 * To compute the message digest of a chunk of bytes, declare an | |
| 19 * MD5Context structure, pass it to MD5Init, call MD5Update as | |
| 20 * needed on buffers full of bytes, and then call MD5Final, which | |
| 21 * will fill a supplied 16-byte array with the digest. | |
| 22 */ | |
| 23 | |
| 24 #include "testing/utils/md5.h" | |
| 25 | |
| 26 #include <string.h> | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // The output of an MD5 operation. | |
| 31 struct MD5Digest { | |
| 32 uint8_t a[16]; | |
| 33 }; | |
| 34 | |
| 35 // Used for storing intermediate data during an MD5 computation. Callers | |
| 36 // should not access the data. | |
| 37 typedef char MD5Context[88]; | |
| 38 | |
| 39 struct Context { | |
| 40 uint32_t buf[4]; | |
| 41 uint32_t bits[2]; | |
| 42 uint8_t in[64]; | |
| 43 }; | |
| 44 | |
| 45 /* | |
| 46 * Note: this code is harmless on little-endian machines. | |
| 47 */ | |
| 48 void byteReverse(uint8_t* buf, unsigned longs) { | |
| 49 do { | |
| 50 uint32_t temp = | |
| 51 static_cast<uint32_t>(static_cast<unsigned>(buf[3]) << 8 | buf[2]) | |
| 52 << 16 | | |
| 53 (static_cast<unsigned>(buf[1]) << 8 | buf[0]); | |
| 54 *reinterpret_cast<uint32_t*>(buf) = temp; | |
| 55 buf += 4; | |
| 56 } while (--longs); | |
| 57 } | |
| 58 | |
| 59 /* The four core functions - F1 is optimized somewhat */ | |
| 60 | |
| 61 /* #define F1(x, y, z) (x & y | ~x & z) */ | |
| 62 #define F1(x, y, z) (z ^ (x & (y ^ z))) | |
| 63 #define F2(x, y, z) F1(z, x, y) | |
| 64 #define F3(x, y, z) (x ^ y ^ z) | |
| 65 #define F4(x, y, z) (y ^ (x | ~z)) | |
| 66 | |
| 67 /* This is the central step in the MD5 algorithm. */ | |
| 68 #define MD5STEP(f, w, x, y, z, data, s) \ | |
| 69 (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x) | |
| 70 | |
| 71 /* | |
| 72 * The core of the MD5 algorithm, this alters an existing MD5 hash to | |
| 73 * reflect the addition of 16 longwords of new data. MD5Update blocks | |
| 74 * the data and converts bytes into longwords for this routine. | |
| 75 */ | |
| 76 void MD5Transform(uint32_t buf[4], const uint32_t in[16]) { | |
| 77 uint32_t a, b, c, d; | |
| 78 | |
| 79 a = buf[0]; | |
| 80 b = buf[1]; | |
| 81 c = buf[2]; | |
| 82 d = buf[3]; | |
| 83 | |
| 84 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); | |
| 85 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); | |
| 86 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); | |
| 87 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); | |
| 88 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); | |
| 89 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); | |
| 90 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); | |
| 91 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); | |
| 92 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); | |
| 93 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); | |
| 94 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); | |
| 95 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); | |
| 96 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); | |
| 97 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); | |
| 98 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); | |
| 99 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); | |
| 100 | |
| 101 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); | |
| 102 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); | |
| 103 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); | |
| 104 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); | |
| 105 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); | |
| 106 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); | |
| 107 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); | |
| 108 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); | |
| 109 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); | |
| 110 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); | |
| 111 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); | |
| 112 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); | |
| 113 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); | |
| 114 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); | |
| 115 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); | |
| 116 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); | |
| 117 | |
| 118 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); | |
| 119 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); | |
| 120 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); | |
| 121 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); | |
| 122 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); | |
| 123 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); | |
| 124 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); | |
| 125 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); | |
| 126 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); | |
| 127 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); | |
| 128 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); | |
| 129 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); | |
| 130 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); | |
| 131 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); | |
| 132 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); | |
| 133 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); | |
| 134 | |
| 135 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); | |
| 136 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); | |
| 137 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); | |
| 138 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); | |
| 139 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); | |
| 140 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); | |
| 141 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); | |
| 142 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); | |
| 143 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); | |
| 144 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); | |
| 145 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); | |
| 146 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); | |
| 147 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); | |
| 148 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); | |
| 149 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); | |
| 150 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); | |
| 151 | |
| 152 buf[0] += a; | |
| 153 buf[1] += b; | |
| 154 buf[2] += c; | |
| 155 buf[3] += d; | |
| 156 } | |
| 157 | |
| 158 /* | |
| 159 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | |
| 160 * initialization constants. | |
| 161 */ | |
| 162 void MD5Init(MD5Context* context) { | |
| 163 struct Context* ctx = reinterpret_cast<struct Context*>(context); | |
| 164 ctx->buf[0] = 0x67452301; | |
| 165 ctx->buf[1] = 0xefcdab89; | |
| 166 ctx->buf[2] = 0x98badcfe; | |
| 167 ctx->buf[3] = 0x10325476; | |
| 168 ctx->bits[0] = 0; | |
| 169 ctx->bits[1] = 0; | |
| 170 } | |
| 171 | |
| 172 /* | |
| 173 * Update context to reflect the concatenation of another buffer full | |
| 174 * of bytes. | |
| 175 */ | |
| 176 void MD5Update(MD5Context* context, const void* data, size_t size) { | |
| 177 struct Context* ctx = reinterpret_cast<struct Context*>(context); | |
| 178 const uint8_t* buf = reinterpret_cast<const uint8_t*>(data); | |
| 179 size_t len = size; | |
| 180 | |
| 181 /* Update bitcount */ | |
| 182 | |
| 183 uint32_t t = ctx->bits[0]; | |
| 184 if ((ctx->bits[0] = t + (static_cast<uint32_t>(len) << 3)) < t) | |
| 185 ctx->bits[1]++; /* Carry from low to high */ | |
| 186 ctx->bits[1] += static_cast<uint32_t>(len >> 29); | |
| 187 | |
| 188 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | |
| 189 | |
| 190 /* Handle any leading odd-sized chunks */ | |
| 191 | |
| 192 if (t) { | |
| 193 uint8_t* p = static_cast<uint8_t*>(ctx->in + t); | |
| 194 | |
| 195 t = 64 - t; | |
| 196 if (len < t) { | |
| 197 memcpy(p, buf, len); | |
| 198 return; | |
| 199 } | |
| 200 memcpy(p, buf, t); | |
| 201 byteReverse(ctx->in, 16); | |
| 202 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); | |
| 203 buf += t; | |
| 204 len -= t; | |
| 205 } | |
| 206 | |
| 207 /* Process data in 64-byte chunks */ | |
| 208 | |
| 209 while (len >= 64) { | |
| 210 memcpy(ctx->in, buf, 64); | |
| 211 byteReverse(ctx->in, 16); | |
| 212 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); | |
| 213 buf += 64; | |
| 214 len -= 64; | |
| 215 } | |
| 216 | |
| 217 /* Handle any remaining bytes of data. */ | |
| 218 | |
| 219 memcpy(ctx->in, buf, len); | |
| 220 } | |
| 221 | |
| 222 /* | |
| 223 * Final wrapup - pad to 64-byte boundary with the bit pattern | |
| 224 * 1 0* (64-bit count of bits processed, MSB-first) | |
| 225 */ | |
| 226 void MD5Final(MD5Digest* digest, MD5Context* context) { | |
| 227 struct Context* ctx = reinterpret_cast<struct Context*>(context); | |
| 228 unsigned count; | |
| 229 uint8_t* p; | |
| 230 | |
| 231 /* Compute number of bytes mod 64 */ | |
| 232 count = (ctx->bits[0] >> 3) & 0x3F; | |
| 233 | |
| 234 /* Set the first char of padding to 0x80. This is safe since there is | |
| 235 always at least one byte free */ | |
| 236 p = ctx->in + count; | |
| 237 *p++ = 0x80; | |
| 238 | |
| 239 /* Bytes of padding needed to make 64 bytes */ | |
| 240 count = 64 - 1 - count; | |
| 241 | |
| 242 /* Pad out to 56 mod 64 */ | |
| 243 if (count < 8) { | |
| 244 /* Two lots of padding: Pad the first block to 64 bytes */ | |
| 245 memset(p, 0, count); | |
| 246 byteReverse(ctx->in, 16); | |
| 247 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); | |
| 248 | |
| 249 /* Now fill the next block with 56 bytes */ | |
| 250 memset(ctx->in, 0, 56); | |
| 251 } else { | |
| 252 /* Pad block to 56 bytes */ | |
| 253 memset(p, 0, count - 8); | |
| 254 } | |
| 255 byteReverse(ctx->in, 14); | |
| 256 | |
| 257 /* Append length in bits and transform */ | |
| 258 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], | |
| 259 sizeof(ctx->bits[0])); | |
| 260 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], | |
| 261 sizeof(ctx->bits[1])); | |
| 262 | |
| 263 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); | |
| 264 byteReverse(reinterpret_cast<uint8_t*>(ctx->buf), 4); | |
| 265 memcpy(digest->a, ctx->buf, 16); | |
| 266 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ | |
| 267 } | |
| 268 | |
| 269 std::string MD5DigestToBase16(const MD5Digest& digest) { | |
| 270 static char const zEncode[] = "0123456789abcdef"; | |
| 271 | |
| 272 std::string ret; | |
| 273 ret.resize(32); | |
| 274 | |
| 275 for (int i = 0, j = 0; i < 16; i++, j += 2) { | |
| 276 uint8_t a = digest.a[i]; | |
| 277 ret[j] = zEncode[(a >> 4) & 0xf]; | |
| 278 ret[j + 1] = zEncode[a & 0xf]; | |
| 279 } | |
| 280 return ret; | |
| 281 } | |
| 282 | |
| 283 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { | |
| 284 MD5Context ctx; | |
| 285 MD5Init(&ctx); | |
| 286 MD5Update(&ctx, data, length); | |
| 287 MD5Final(digest, &ctx); | |
| 288 } | |
| 289 | |
| 290 } // namespace | |
| 291 | |
| 292 namespace pdfium { | |
| 293 | |
| 294 std::string MD5String(const void* data, size_t size) { | |
| 295 MD5Digest digest; | |
| 296 MD5Sum(data, size, &digest); | |
| 297 return MD5DigestToBase16(digest); | |
| 298 } | |
| 299 | |
| 300 } // namespace pdfium | |
| OLD | NEW |