Eric Christopher | e1dc3c4 | 2013-05-21 01:28:35 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * This code is derived from (original license follows): |
| 3 | * |
| 4 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. |
| 5 | * MD5 Message-Digest Algorithm (RFC 1321). |
| 6 | * |
| 7 | * Homepage: |
| 8 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 |
| 9 | * |
| 10 | * Author: |
| 11 | * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> |
| 12 | * |
| 13 | * This software was written by Alexander Peslyak in 2001. No copyright is |
| 14 | * claimed, and the software is hereby placed in the public domain. |
| 15 | * In case this attempt to disclaim copyright and place the software in the |
| 16 | * public domain is deemed null and void, then the software is |
| 17 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the |
| 18 | * general public under the following terms: |
| 19 | * |
| 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted. |
| 22 | * |
| 23 | * There's ABSOLUTELY NO WARRANTY, express or implied. |
| 24 | * |
| 25 | * (This is a heavily cut-down "BSD license".) |
| 26 | * |
| 27 | * This differs from Colin Plumb's older public domain implementation in that |
| 28 | * no exactly 32-bit integer data type is required (any 32-bit or wider |
| 29 | * unsigned integer data type will do), there's no compile-time endianness |
| 30 | * configuration, and the function prototypes match OpenSSL's. No code from |
| 31 | * Colin Plumb's implementation has been reused; this comment merely compares |
| 32 | * the properties of the two independent implementations. |
| 33 | * |
| 34 | * The primary goals of this implementation are portability and ease of use. |
| 35 | * It is meant to be fast, but not as fast as possible. Some known |
| 36 | * optimizations are not included to reduce source code size and avoid |
| 37 | * compile-time configuration. |
| 38 | */ |
| 39 | |
| 40 | #include "llvm/Support/MD5.h" |
| 41 | #include <cstring> |
| 42 | |
| 43 | // The basic MD5 functions. |
| 44 | |
| 45 | // F and G are optimized compared to their RFC 1321 definitions for |
| 46 | // architectures that lack an AND-NOT instruction, just like in Colin Plumb's |
| 47 | // implementation. |
| 48 | #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) |
| 49 | #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) |
| 50 | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
| 51 | #define I(x, y, z) ((y) ^ ((x) | ~(z))) |
| 52 | |
| 53 | // The MD5 transformation for all four rounds. |
| 54 | #define STEP(f, a, b, c, d, x, t, s) \ |
| 55 | (a) += f((b), (c), (d)) + (x) + (t); \ |
| 56 | (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ |
| 57 | (a) += (b); |
| 58 | |
| 59 | // SET reads 4 input bytes in little-endian byte order and stores them |
| 60 | // in a properly aligned word in host byte order. |
| 61 | #define SET(n) \ |
| 62 | (block[(n)] = \ |
| 63 | (MD5_u32plus) ptr[(n) * 4] | ((MD5_u32plus) ptr[(n) * 4 + 1] << 8) | \ |
| 64 | ((MD5_u32plus) ptr[(n) * 4 + 2] << 16) | \ |
| 65 | ((MD5_u32plus) ptr[(n) * 4 + 3] << 24)) |
| 66 | #define GET(n) (block[(n)]) |
| 67 | |
| 68 | namespace llvm { |
| 69 | |
| 70 | /// \brief This processes one or more 64-byte data blocks, but does NOT update |
| 71 | ///the bit counters. There are no alignment requirements. |
| 72 | void *MD5::body(void *data, unsigned long size) { |
| 73 | unsigned char *ptr; |
| 74 | MD5_u32plus a, b, c, d; |
| 75 | MD5_u32plus saved_a, saved_b, saved_c, saved_d; |
| 76 | |
| 77 | ptr = (unsigned char *)data; |
| 78 | |
| 79 | a = this->a; |
| 80 | b = this->b; |
| 81 | c = this->c; |
| 82 | d = this->d; |
| 83 | |
| 84 | do { |
| 85 | saved_a = a; |
| 86 | saved_b = b; |
| 87 | saved_c = c; |
| 88 | saved_d = d; |
| 89 | |
| 90 | // Round 1 |
| 91 | STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) |
| 92 | STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) |
| 93 | STEP(F, c, d, a, b, SET(2), 0x242070db, 17) |
| 94 | STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) |
| 95 | STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) |
| 96 | STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) |
| 97 | STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) |
| 98 | STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) |
| 99 | STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) |
| 100 | STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) |
| 101 | STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) |
| 102 | STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) |
| 103 | STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) |
| 104 | STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) |
| 105 | STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) |
| 106 | STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) |
| 107 | |
| 108 | // Round 2 |
| 109 | STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) |
| 110 | STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) |
| 111 | STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) |
| 112 | STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) |
| 113 | STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) |
| 114 | STEP(G, d, a, b, c, GET(10), 0x02441453, 9) |
| 115 | STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) |
| 116 | STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) |
| 117 | STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) |
| 118 | STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) |
| 119 | STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) |
| 120 | STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) |
| 121 | STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) |
| 122 | STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) |
| 123 | STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) |
| 124 | STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) |
| 125 | |
| 126 | // Round 3 |
| 127 | STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) |
| 128 | STEP(H, d, a, b, c, GET(8), 0x8771f681, 11) |
| 129 | STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) |
| 130 | STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23) |
| 131 | STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) |
| 132 | STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11) |
| 133 | STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) |
| 134 | STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23) |
| 135 | STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) |
| 136 | STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11) |
| 137 | STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) |
| 138 | STEP(H, b, c, d, a, GET(6), 0x04881d05, 23) |
| 139 | STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) |
| 140 | STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11) |
| 141 | STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) |
| 142 | STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23) |
| 143 | |
| 144 | // Round 4 |
| 145 | STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) |
| 146 | STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) |
| 147 | STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) |
| 148 | STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) |
| 149 | STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) |
| 150 | STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) |
| 151 | STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) |
| 152 | STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) |
| 153 | STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) |
| 154 | STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) |
| 155 | STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) |
| 156 | STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) |
| 157 | STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) |
| 158 | STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) |
| 159 | STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) |
| 160 | STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) |
| 161 | |
| 162 | a += saved_a; |
| 163 | b += saved_b; |
| 164 | c += saved_c; |
| 165 | d += saved_d; |
| 166 | |
| 167 | ptr += 64; |
| 168 | } while (size -= 64); |
| 169 | |
| 170 | this->a = a; |
| 171 | this->b = b; |
| 172 | this->c = c; |
| 173 | this->d = d; |
| 174 | |
| 175 | return ptr; |
| 176 | } |
| 177 | |
| 178 | MD5::MD5() |
| 179 | : a(0x67452301), b(0xefcdab89), c(0x98badcfe), d(0x10325476), hi(0), lo(0) { |
| 180 | } |
| 181 | |
| 182 | /// Incrementally add \p size of \p data to the hash. |
| 183 | void MD5::Update(void *data, unsigned long size) { |
| 184 | MD5_u32plus saved_lo; |
| 185 | unsigned long used, free; |
| 186 | |
| 187 | saved_lo = lo; |
| 188 | if ((lo = (saved_lo + size) & 0x1fffffff) < saved_lo) |
| 189 | hi++; |
| 190 | hi += size >> 29; |
| 191 | |
| 192 | used = saved_lo & 0x3f; |
| 193 | |
| 194 | if (used) { |
| 195 | free = 64 - used; |
| 196 | |
| 197 | if (size < free) { |
| 198 | memcpy(&buffer[used], data, size); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | memcpy(&buffer[used], data, free); |
| 203 | data = (unsigned char *)data + free; |
| 204 | size -= free; |
| 205 | body(buffer, 64); |
| 206 | } |
| 207 | |
| 208 | if (size >= 64) { |
| 209 | data = body(data, size & ~(unsigned long) 0x3f); |
| 210 | size &= 0x3f; |
| 211 | } |
| 212 | |
| 213 | memcpy(buffer, data, size); |
| 214 | } |
| 215 | |
| 216 | /// \brief Finish the hash and place the resulting hash into \p result. |
| 217 | /// \param result is assumed to be a minimum of 16-bytes in size. |
| 218 | void MD5::Final(unsigned char *result) { |
| 219 | unsigned long used, free; |
| 220 | |
| 221 | used = lo & 0x3f; |
| 222 | |
| 223 | buffer[used++] = 0x80; |
| 224 | |
| 225 | free = 64 - used; |
| 226 | |
| 227 | if (free < 8) { |
| 228 | memset(&buffer[used], 0, free); |
| 229 | body(buffer, 64); |
| 230 | used = 0; |
| 231 | free = 64; |
| 232 | } |
| 233 | |
| 234 | memset(&buffer[used], 0, free - 8); |
| 235 | |
| 236 | lo <<= 3; |
| 237 | buffer[56] = lo; |
| 238 | buffer[57] = lo >> 8; |
| 239 | buffer[58] = lo >> 16; |
| 240 | buffer[59] = lo >> 24; |
| 241 | buffer[60] = hi; |
| 242 | buffer[61] = hi >> 8; |
| 243 | buffer[62] = hi >> 16; |
| 244 | buffer[63] = hi >> 24; |
| 245 | |
| 246 | body(buffer, 64); |
| 247 | |
| 248 | result[0] = a; |
| 249 | result[1] = a >> 8; |
| 250 | result[2] = a >> 16; |
| 251 | result[3] = a >> 24; |
| 252 | result[4] = b; |
| 253 | result[5] = b >> 8; |
| 254 | result[6] = b >> 16; |
| 255 | result[7] = b >> 24; |
| 256 | result[8] = c; |
| 257 | result[9] = c >> 8; |
| 258 | result[10] = c >> 16; |
| 259 | result[11] = c >> 24; |
| 260 | result[12] = d; |
| 261 | result[13] = d >> 8; |
| 262 | result[14] = d >> 16; |
| 263 | result[15] = d >> 24; |
| 264 | } |
| 265 | |
| 266 | } |