| OLD | NEW | 
| (Empty) |  | 
 |    1 /* | 
 |    2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
 |    3  * Copyright (C) 2006-present eyeo GmbH | 
 |    4  * | 
 |    5  * Adblock Plus is free software: you can redistribute it and/or modify | 
 |    6  * it under the terms of the GNU General Public License version 3 as | 
 |    7  * published by the Free Software Foundation. | 
 |    8  * | 
 |    9  * Adblock Plus is distributed in the hope that it will be useful, | 
 |   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 |   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 |   12  * GNU General Public License for more details. | 
 |   13  * | 
 |   14  * You should have received a copy of the GNU General Public License | 
 |   15  * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 
 |   16  */ | 
 |   17  | 
 |   18 /* Original notice */ | 
 |   19 /* | 
 |   20  * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. | 
 |   21  * MD5 Message-Digest Algorithm (RFC 1321). | 
 |   22  * | 
 |   23  * Homepage: | 
 |   24  * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 | 
 |   25  * | 
 |   26  * Author: | 
 |   27  * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> | 
 |   28  * | 
 |   29  * This software was written by Alexander Peslyak in 2001.  No copyright is | 
 |   30  * claimed, and the software is hereby placed in the public domain. | 
 |   31  * In case this attempt to disclaim copyright and place the software in the | 
 |   32  * public domain is deemed null and void, then the software is | 
 |   33  * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the | 
 |   34  * general public under the following terms: | 
 |   35  * | 
 |   36  * Redistribution and use in source and binary forms, with or without | 
 |   37  * modification, are permitted. | 
 |   38  * | 
 |   39  * There's ABSOLUTELY NO WARRANTY, express or implied. | 
 |   40  * | 
 |   41  * (This is a heavily cut-down "BSD license".) | 
 |   42  * | 
 |   43  * This differs from Colin Plumb's older public domain implementation in that | 
 |   44  * no exactly 32-bit integer data type is required (any 32-bit or wider | 
 |   45  * unsigned integer data type will do), there's no compile-time endianness | 
 |   46  * configuration, and the function prototypes match OpenSSL's.  No code from | 
 |   47  * Colin Plumb's implementation has been reused; this comment merely compares | 
 |   48  * the properties of the two independent implementations. | 
 |   49  * | 
 |   50  * The primary goals of this implementation are portability and ease of use. | 
 |   51  * It is meant to be fast, but not as fast as possible.  Some known | 
 |   52  * optimizations are not included to reduce source code size and avoid | 
 |   53  * compile-time configuration. | 
 |   54  */ | 
 |   55  | 
 |   56 #include <string.h> | 
 |   57  | 
 |   58 #include "String.h" | 
 |   59  | 
 |   60 #include "Md5.h" | 
 |   61  | 
 |   62 /* | 
 |   63  * The basic MD5 functions. | 
 |   64  * | 
 |   65  * F and G are optimized compared to their RFC 1321 definitions for | 
 |   66  * architectures that lack an AND-NOT instruction, just like in Colin Plumb's | 
 |   67  * implementation. | 
 |   68  */ | 
 |   69 #define F(x, y, z)                      ((z) ^ ((x) & ((y) ^ (z)))) | 
 |   70 #define G(x, y, z)                      ((y) ^ ((z) & ((x) ^ (y)))) | 
 |   71 #define H(x, y, z)                      (((x) ^ (y)) ^ (z)) | 
 |   72 #define H2(x, y, z)                     ((x) ^ ((y) ^ (z))) | 
 |   73 #define I(x, y, z)                      ((y) ^ ((x) | ~(z))) | 
 |   74  | 
 |   75 /* | 
 |   76  * The MD5 transformation for all four rounds. | 
 |   77  */ | 
 |   78 #define STEP(f, a, b, c, d, x, t, s)                            \ | 
 |   79   (a) += f((b), (c), (d)) + (x) + (t);                          \ | 
 |   80   (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));    \ | 
 |   81   (a) += (b) | 
 |   82  | 
 |   83 /* | 
 |   84  * SET reads 4 input bytes in little-endian byte order and stores them in a | 
 |   85  * properly aligned word in host byte order. | 
 |   86  * | 
 |   87  * The check for little-endian architectures that tolerate unaligned memory | 
 |   88  * accesses is just an optimization.  Nothing will break if it fails to detect | 
 |   89  * a suitable architecture. | 
 |   90  * | 
 |   91  * Unfortunately, this optimization may be a C strict aliasing rules violation | 
 |   92  * if the caller's data buffer has effective type that cannot be aliased by | 
 |   93  * uint32_t.  In practice, this problem may occur if these MD5 routines are | 
 |   94  * inlined into a calling function, or with future and dangerously advanced | 
 |   95  * link-time optimizations.  For the time being, keeping these MD5 routines in | 
 |   96  * their own translation unit avoids the problem. | 
 |   97  */ | 
 |   98 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__) | 
 |   99 #define SET(n)                                  \ | 
 |  100   (*(uint32_t *)&ptr[(n) * 4]) | 
 |  101 #define GET(n)                                  \ | 
 |  102   SET(n) | 
 |  103 #else | 
 |  104 #define SET(n)                                  \ | 
 |  105   (this->block[(n)] =                            \ | 
 |  106      (uint32_t)ptr[(n) * 4] |                \ | 
 |  107    ((uint32_t)ptr[(n) * 4 + 1] << 8) |       \ | 
 |  108    ((uint32_t)ptr[(n) * 4 + 2] << 16) |      \ | 
 |  109    ((uint32_t)ptr[(n) * 4 + 3] << 24)) | 
 |  110 #define GET(n)                                  \ | 
 |  111   (this->block[(n)]) | 
 |  112 #endif | 
 |  113  | 
 |  114 /* | 
 |  115  * This processes one or more 64-byte data blocks, but does NOT update the bit | 
 |  116  * counters.  There are no alignment requirements. | 
 |  117  */ | 
 |  118 const void *MD5::Body(const void *data, unsigned long size) | 
 |  119 { | 
 |  120   const unsigned char *ptr; | 
 |  121   uint32_t a, b, c, d; | 
 |  122   uint32_t saved_a, saved_b, saved_c, saved_d; | 
 |  123  | 
 |  124   ptr = (const unsigned char *)data; | 
 |  125  | 
 |  126   a = this->a; | 
 |  127   b = this->b; | 
 |  128   c = this->c; | 
 |  129   d = this->d; | 
 |  130  | 
 |  131   do { | 
 |  132     saved_a = a; | 
 |  133     saved_b = b; | 
 |  134     saved_c = c; | 
 |  135     saved_d = d; | 
 |  136  | 
 |  137 /* Round 1 */ | 
 |  138     STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7); | 
 |  139     STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12); | 
 |  140     STEP(F, c, d, a, b, SET(2), 0x242070db, 17); | 
 |  141     STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22); | 
 |  142     STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7); | 
 |  143     STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12); | 
 |  144     STEP(F, c, d, a, b, SET(6), 0xa8304613, 17); | 
 |  145     STEP(F, b, c, d, a, SET(7), 0xfd469501, 22); | 
 |  146     STEP(F, a, b, c, d, SET(8), 0x698098d8, 7); | 
 |  147     STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12); | 
 |  148     STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17); | 
 |  149     STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22); | 
 |  150     STEP(F, a, b, c, d, SET(12), 0x6b901122, 7); | 
 |  151     STEP(F, d, a, b, c, SET(13), 0xfd987193, 12); | 
 |  152     STEP(F, c, d, a, b, SET(14), 0xa679438e, 17); | 
 |  153     STEP(F, b, c, d, a, SET(15), 0x49b40821, 22); | 
 |  154  | 
 |  155 /* Round 2 */ | 
 |  156     STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5); | 
 |  157     STEP(G, d, a, b, c, GET(6), 0xc040b340, 9); | 
 |  158     STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14); | 
 |  159     STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20); | 
 |  160     STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5); | 
 |  161     STEP(G, d, a, b, c, GET(10), 0x02441453, 9); | 
 |  162     STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14); | 
 |  163     STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20); | 
 |  164     STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5); | 
 |  165     STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9); | 
 |  166     STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14); | 
 |  167     STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20); | 
 |  168     STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5); | 
 |  169     STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9); | 
 |  170     STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14); | 
 |  171     STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20); | 
 |  172  | 
 |  173 /* Round 3 */ | 
 |  174     STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4); | 
 |  175     STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11); | 
 |  176     STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16); | 
 |  177     STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23); | 
 |  178     STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4); | 
 |  179     STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11); | 
 |  180     STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16); | 
 |  181     STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23); | 
 |  182     STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4); | 
 |  183     STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11); | 
 |  184     STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16); | 
 |  185     STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23); | 
 |  186     STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4); | 
 |  187     STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11); | 
 |  188     STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16); | 
 |  189     STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23); | 
 |  190  | 
 |  191 /* Round 4 */ | 
 |  192     STEP(I, a, b, c, d, GET(0), 0xf4292244, 6); | 
 |  193     STEP(I, d, a, b, c, GET(7), 0x432aff97, 10); | 
 |  194     STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15); | 
 |  195     STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21); | 
 |  196     STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6); | 
 |  197     STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10); | 
 |  198     STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15); | 
 |  199     STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21); | 
 |  200     STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6); | 
 |  201     STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10); | 
 |  202     STEP(I, c, d, a, b, GET(6), 0xa3014314, 15); | 
 |  203     STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21); | 
 |  204     STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6); | 
 |  205     STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10); | 
 |  206     STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15); | 
 |  207     STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21); | 
 |  208  | 
 |  209     a += saved_a; | 
 |  210     b += saved_b; | 
 |  211     c += saved_c; | 
 |  212     d += saved_d; | 
 |  213  | 
 |  214     ptr += 64; | 
 |  215   } while (size -= 64); | 
 |  216  | 
 |  217   this->a = a; | 
 |  218   this->b = b; | 
 |  219   this->c = c; | 
 |  220   this->d = d; | 
 |  221  | 
 |  222   return ptr; | 
 |  223 } | 
 |  224  | 
 |  225 void MD5::InitState() | 
 |  226 { | 
 |  227   a = 0x67452301; | 
 |  228   b = 0xefcdab89; | 
 |  229   c = 0x98badcfe; | 
 |  230   d = 0x10325476; | 
 |  231 } | 
 |  232  | 
 |  233 MD5::MD5() | 
 |  234   : lo(0), hi(0) | 
 |  235 { | 
 |  236   InitState(); | 
 |  237 } | 
 |  238  | 
 |  239 void MD5::Update(const void *data, size_t size) | 
 |  240 { | 
 |  241   uint32_t saved_lo; | 
 |  242   unsigned long used, available; | 
 |  243  | 
 |  244   saved_lo = this->lo; | 
 |  245   if ((this->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) | 
 |  246     this->hi++; | 
 |  247   this->hi += size >> 29; | 
 |  248  | 
 |  249   used = saved_lo & 0x3f; | 
 |  250  | 
 |  251   if (used) { | 
 |  252     available = 64 - used; | 
 |  253  | 
 |  254     if (size < available) { | 
 |  255       memcpy(&this->buffer[used], data, size); | 
 |  256       return; | 
 |  257     } | 
 |  258  | 
 |  259     memcpy(&this->buffer[used], data, available); | 
 |  260     data = (const unsigned char *)data + available; | 
 |  261     size -= available; | 
 |  262     Body(this->buffer, 64); | 
 |  263   } | 
 |  264  | 
 |  265   if (size >= 64) { | 
 |  266     data = Body(data, size & ~(unsigned long)0x3f); | 
 |  267     size &= 0x3f; | 
 |  268   } | 
 |  269  | 
 |  270   memcpy(this->buffer, data, size); | 
 |  271 } | 
 |  272  | 
 |  273 #define OUT(dst, src)                           \ | 
 |  274   (dst)[0] = (unsigned char)(src);              \ | 
 |  275   (dst)[1] = (unsigned char)((src) >> 8);       \ | 
 |  276   (dst)[2] = (unsigned char)((src) >> 16);      \ | 
 |  277   (dst)[3] = (unsigned char)((src) >> 24) | 
 |  278  | 
 |  279 void MD5::Final(uint8_t *result) | 
 |  280 { | 
 |  281   unsigned long used, available; | 
 |  282  | 
 |  283   used = this->lo & 0x3f; | 
 |  284  | 
 |  285   this->buffer[used++] = 0x80; | 
 |  286  | 
 |  287   available = 64 - used; | 
 |  288  | 
 |  289   if (available < 8) { | 
 |  290     memset(&this->buffer[used], 0, available); | 
 |  291     Body(this->buffer, 64); | 
 |  292     used = 0; | 
 |  293     available = 64; | 
 |  294   } | 
 |  295  | 
 |  296   memset(&this->buffer[used], 0, available - 8); | 
 |  297  | 
 |  298   this->lo <<= 3; | 
 |  299   OUT(&this->buffer[56], this->lo); | 
 |  300   OUT(&this->buffer[60], this->hi); | 
 |  301  | 
 |  302   Body(this->buffer, 64); | 
 |  303  | 
 |  304   OUT(&result[0], this->a); | 
 |  305   OUT(&result[4], this->b); | 
 |  306   OUT(&result[8], this->c); | 
 |  307   OUT(&result[12], this->d); | 
 |  308  | 
 |  309   // Clear the data. | 
 |  310   lo = hi = 0; | 
 |  311   InitState(); | 
 |  312   memset(&buffer, 0, sizeof(*buffer)); | 
 |  313   memset(&block, 0, sizeof(*block)); | 
 |  314 } | 
 |  315  | 
 |  316 void MD5::Update(const String& input) | 
 |  317 { | 
 |  318   uint8_t utf8[] = { 0, 0, 0 }; | 
 |  319   size_t len = 0; | 
 |  320  | 
 |  321   for (String::size_type i = 0; i < input.length(); i++) | 
 |  322   { | 
 |  323     // convert the string it utf-8 | 
 |  324     auto ch = input[i]; | 
 |  325     if (ch <= 0x7f) | 
 |  326     { | 
 |  327       utf8[0] = ch; | 
 |  328       len = 1; | 
 |  329     } | 
 |  330     else if (ch <= 0x7ff) | 
 |  331     { | 
 |  332       utf8[0] = (ch & 0x3f) | 0x80; | 
 |  333       utf8[1] = ((ch >> 6) & 0x1f) | 0xc0; | 
 |  334       len = 2; | 
 |  335     } | 
 |  336     else | 
 |  337     { | 
 |  338       utf8[0] = (ch & 0x3f) | 0x80; | 
 |  339       utf8[1] = ((ch >> 6) & 0x3f) | 0x80; | 
 |  340       utf8[2] = ((ch >> 12) & 0x0f) | 0xe0; | 
 |  341       len = 3; | 
 |  342     } | 
 |  343     // String::value_type is only 16-bits for now. | 
 |  344     // Update this if we change it. | 
 |  345     Update(utf8, len); | 
 |  346   } | 
 |  347 } | 
| OLD | NEW |