duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

md5_digest.cpp (7233B)


      1 #include "md5_digest.h"
      2 
      3 // based heavily on this implementation:
      4 // http://www.fourmilab.ch/md5/
      5 
      6 #ifndef HIGHFIRST
      7 #define byteReverse(buf, len) /* Nothing */
      8 #else
      9 /*
     10  * Note: this code is harmless on little-endian machines.
     11  */
     12 static void byteReverse(unsigned char* buf, unsigned longs)
     13 {
     14   u32 t;
     15   do
     16   {
     17     t = (u32)((unsigned)buf[3] << 8 | buf[2]) << 16 | ((unsigned)buf[1] << 8 | buf[0]);
     18     *(u32*)buf = t;
     19     buf += 4;
     20   } while (--longs);
     21 }
     22 #endif
     23 
     24 /* The four core functions - F1 is optimized somewhat */
     25 
     26 /* #define F1(x, y, z) (x & y | ~x & z) */
     27 #define F1(x, y, z) (z ^ (x & (y ^ z)))
     28 #define F2(x, y, z) F1(z, x, y)
     29 #define F3(x, y, z) (x ^ y ^ z)
     30 #define F4(x, y, z) (y ^ (x | ~z))
     31 
     32 /* This is the central step in the MD5 algorithm. */
     33 #define MD5STEP(f, w, x, y, z, data, s) (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
     34 
     35 /*
     36  * The core of the MD5 algorithm, this alters an existing MD5 hash to
     37  * reflect the addition of 16 longwords of new data.  MD5Update blocks
     38  * the data and converts bytes into longwords for this routine.
     39  */
     40 static void MD5Transform(u32 buf[4], u32 in[16])
     41 {
     42   // register u32 a, b, c, d;
     43   u32 a, b, c, d;
     44 
     45   a = buf[0];
     46   b = buf[1];
     47   c = buf[2];
     48   d = buf[3];
     49 
     50   MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
     51   MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
     52   MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
     53   MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
     54   MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
     55   MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
     56   MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
     57   MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
     58   MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
     59   MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
     60   MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
     61   MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
     62   MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
     63   MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
     64   MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
     65   MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
     66 
     67   MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
     68   MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
     69   MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
     70   MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
     71   MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
     72   MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
     73   MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
     74   MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
     75   MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
     76   MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
     77   MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
     78   MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
     79   MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
     80   MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
     81   MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
     82   MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
     83 
     84   MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
     85   MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
     86   MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
     87   MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
     88   MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
     89   MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
     90   MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
     91   MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
     92   MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
     93   MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
     94   MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
     95   MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
     96   MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
     97   MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
     98   MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
     99   MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
    100 
    101   MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
    102   MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
    103   MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
    104   MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
    105   MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
    106   MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
    107   MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
    108   MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
    109   MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
    110   MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
    111   MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
    112   MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
    113   MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
    114   MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
    115   MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
    116   MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
    117 
    118   buf[0] += a;
    119   buf[1] += b;
    120   buf[2] += c;
    121   buf[3] += d;
    122 }
    123 
    124 MD5Digest::MD5Digest()
    125 {
    126   Reset();
    127 }
    128 
    129 void MD5Digest::Reset()
    130 {
    131   this->buf[0] = 0x67452301;
    132   this->buf[1] = 0xefcdab89;
    133   this->buf[2] = 0x98badcfe;
    134   this->buf[3] = 0x10325476;
    135 
    136   this->bits[0] = 0;
    137   this->bits[1] = 0;
    138 }
    139 
    140 std::array<u8, MD5Digest::DIGEST_SIZE> MD5Digest::HashData(std::span<const u8> data)
    141 {
    142   std::array<u8, DIGEST_SIZE> ret;
    143 
    144   MD5Digest digest;
    145   digest.Update(data);
    146   digest.Final(ret);
    147   return ret;
    148 }
    149 
    150 void MD5Digest::Update(const void* pData, u32 cbData)
    151 {
    152   u32 t;
    153   const u8* pByteData = reinterpret_cast<const u8*>(pData);
    154 
    155   /* Update bitcount */
    156 
    157   t = this->bits[0];
    158   if ((this->bits[0] = t + ((u32)cbData << 3)) < t)
    159     this->bits[1]++; /* Carry from low to high */
    160   this->bits[1] += cbData >> 29;
    161 
    162   t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
    163 
    164   /* Handle any leading odd-sized chunks */
    165 
    166   if (t)
    167   {
    168     u8* p = (u8*)this->in + t;
    169 
    170     t = 64 - t;
    171     if (cbData < t)
    172     {
    173       std::memcpy(p, pByteData, cbData);
    174       return;
    175     }
    176     std::memcpy(p, pByteData, t);
    177     byteReverse(this->in, 16);
    178     MD5Transform(this->buf, (u32*)this->in);
    179     pByteData += t;
    180     cbData -= t;
    181   }
    182   /* Process data in 64-byte chunks */
    183 
    184   while (cbData >= 64)
    185   {
    186     std::memcpy(this->in, pByteData, 64);
    187     byteReverse(this->in, 16);
    188     MD5Transform(this->buf, (u32*)this->in);
    189     pByteData += 64;
    190     cbData -= 64;
    191   }
    192 
    193   /* Handle any remaining bytes of data. */
    194 
    195   std::memcpy(this->in, pByteData, cbData);
    196 }
    197 
    198 void MD5Digest::Update(std::span<const u8> data)
    199 {
    200   if (!data.empty())
    201     Update(data.data(), static_cast<u32>(data.size_bytes()));
    202 }
    203 
    204 void MD5Digest::Final(std::span<u8, DIGEST_SIZE> digest)
    205 {
    206   u32 count;
    207   u8* p;
    208 
    209   /* Compute number of bytes mod 64 */
    210   count = (this->bits[0] >> 3) & 0x3F;
    211 
    212   /* Set the first char of padding to 0x80.  This is safe since there is
    213      always at least one byte free */
    214   p = this->in + count;
    215   *p++ = 0x80;
    216 
    217   /* Bytes of padding needed to make 64 bytes */
    218   count = 64 - 1 - count;
    219 
    220   /* Pad out to 56 mod 64 */
    221   if (count < 8)
    222   {
    223     /* Two lots of padding:  Pad the first block to 64 bytes */
    224     std::memset(p, 0, count);
    225     byteReverse(this->in, 16);
    226     MD5Transform(this->buf, (u32*)this->in);
    227 
    228     /* Now fill the next block with 56 bytes */
    229     std::memset(this->in, 0, 56);
    230   }
    231   else
    232   {
    233     /* Pad block to 56 bytes */
    234     std::memset(p, 0, count - 8);
    235   }
    236   byteReverse(this->in, 14);
    237 
    238   /* Append length in bits and transform */
    239   ((u32*)this->in)[14] = this->bits[0];
    240   ((u32*)this->in)[15] = this->bits[1];
    241 
    242   MD5Transform(this->buf, (u32*)this->in);
    243   byteReverse((unsigned char*)this->buf, 4);
    244   std::memcpy(digest.data(), this->buf, 16);
    245 }