| 1 | /* Descent 3 |
| 2 | * Copyright (C) 2024 Descent Developers |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "md5.h" |
| 19 | #include <byteswap.h> |
| 20 | |
| 21 | #include <algorithm> |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const uint32_t md5_round_shifts[] = {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21}; |
| 26 | |
| 27 | const std::uint32_t md5_round_constants[] = { |
| 28 | 0xD76AA478UL, 0xE8C7B756UL, 0x242070DBUL, 0xC1BDCEEEUL, 0xF57C0FAFUL, 0x4787C62AUL, 0xA8304613UL, 0xFD469501UL, |
| 29 | 0x698098D8UL, 0x8B44F7AFUL, 0xFFFF5BB1UL, 0x895CD7BEUL, 0x6B901122UL, 0xFD987193UL, 0xA679438EUL, 0x49B40821UL, |
| 30 | 0xF61E2562UL, 0xC040B340UL, 0x265E5A51UL, 0xE9B6C7AAUL, 0xD62F105DUL, 0x02441453UL, 0xD8A1E681UL, 0xE7D3FBC8UL, |
| 31 | 0x21E1CDE6UL, 0xC33707D6UL, 0xF4D50D87UL, 0x455A14EDUL, 0xA9E3E905UL, 0xFCEFA3F8UL, 0x676F02D9UL, 0x8D2A4C8AUL, |
| 32 | 0xFFFA3942UL, 0x8771F681UL, 0x6D9D6122UL, 0xFDE5380CUL, 0xA4BEEA44UL, 0x4BDECFA9UL, 0xF6BB4B60UL, 0xBEBFBC70UL, |
| 33 | 0x289B7EC6UL, 0xEAA127FAUL, 0xD4EF3085UL, 0x04881D05UL, 0xD9D4D039UL, 0xE6DB99E5UL, 0x1FA27CF8UL, 0xC4AC5665UL, |
| 34 | 0xF4292244UL, 0x432AFF97UL, 0xAB9423A7UL, 0xFC93A039UL, 0x655B59C3UL, 0x8F0CCC92UL, 0xFFEFF47DUL, 0x85845DD1UL, |
| 35 | 0x6FA87E4FUL, 0xFE2CE6E0UL, 0xA3014314UL, 0x4E0811A1UL, 0xF7537E82UL, 0xBD3AF235UL, 0x2AD7D2BBUL, 0xEB86D391UL}; |
| 36 | |
| 37 | std::uint32_t rotl_uint32(std::uint32_t a, std::uint32_t b) noexcept { return b ? (a << b) | (a >> (32 - b)) : a; }; |
| 38 | |
| 39 | }; // namespace |
| 40 | |
| 41 | void MD5::round(std::array<std::uint32_t, 4> &sums, const uint8_t *block) const noexcept { |
| 42 | // break 64 bytes into 16 dwords |
| 43 | std::uint32_t m[16]; |
| 44 | for (std::size_t i = 0; i < 16; ++i) { |
| 45 | m[i] = (static_cast<std::uint32_t>(block[(4 * i)])) | (static_cast<std::uint32_t>(block[(4 * i) + 1]) << 8) | |
| 46 | (static_cast<std::uint32_t>(block[(4 * i) + 2]) << 16) | |
| 47 | (static_cast<std::uint32_t>(block[(4 * i) + 3]) << 24); |
| 48 | } |
| 49 | |
| 50 | std::uint32_t a = sums[0], b = sums[1], c = sums[2], d = sums[3]; |
| 51 | |
| 52 | // 64 rounds... |
| 53 | for (uint32_t r = 0; r < 64; ++r) { |
| 54 | std::uint32_t f, g; |
| 55 | uint32_t s = md5_round_shifts[((r >> 2) & ~3) | (r & 3)]; |
| 56 | switch (r >> 4) { |
| 57 | case 0: |
| 58 | f = (b & c) | (~b & d), g = r; |
| 59 | break; |
| 60 | case 1: |
| 61 | f = (b & d) | (c & ~d), g = (5 * r + 1) & 15; |
| 62 | break; |
| 63 | case 2: |
| 64 | f = b ^ c ^ d, g = (3 * r + 5) & 15; |
| 65 | break; |
| 66 | case 3: |
| 67 | f = c ^ (b | ~d), g = (7 * r) & 15; |
| 68 | break; |
| 69 | default:; // unreachable |
| 70 | } |
| 71 | f = rotl_uint32(f + a + m[g] + md5_round_constants[r], s) + b; |
| 72 | a = d; |
| 73 | d = c; |
| 74 | c = b; |
| 75 | b = f; |
| 76 | } |
| 77 | |
| 78 | sums[0] += a, sums[1] += b, sums[2] += c, sums[3] += d; |
| 79 | } |
| 80 | |
| 81 | void MD5::round(const uint8_t *block) noexcept { round(sums_, block); } |
| 82 | |
| 83 | void MD5::update(const uint8_t *data, std::size_t n) noexcept { |
| 84 | message_len_ += n * 8; |
| 85 | |
| 86 | // if there is already some data in tmpbuf, check if we would fill it |
| 87 | if (tmpbuf_n_) { |
| 88 | if ((n >= tmpbuf_.size() || tmpbuf_n_ + n >= tmpbuf_.size())) { |
| 89 | // we would fill tmpbuf at least once; handle partial block first. |
| 90 | std::size_t remaining = tmpbuf_.size() - tmpbuf_n_; |
| 91 | std::copy(data, data + remaining, tmpbuf_.begin() + tmpbuf_n_); |
| 92 | data += remaining; |
| 93 | n -= remaining; |
| 94 | round(tmpbuf_.data()); |
| 95 | tmpbuf_n_ = 0; |
| 96 | } else { |
| 97 | // we will not fill the buffer - just copy and return |
| 98 | std::copy(data, data + n, tmpbuf_.begin() + tmpbuf_n_); |
| 99 | tmpbuf_n_ += n; |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // do rounds on full blocks for as long as we can |
| 105 | while (n >= tmpbuf_.size()) { |
| 106 | round(data); |
| 107 | data += tmpbuf_.size(); |
| 108 | n -= tmpbuf_.size(); |
| 109 | } |
| 110 | |
| 111 | if (n) { |
| 112 | // copy partial block |
| 113 | std::copy(data, data + n, tmpbuf_.begin()); |
| 114 | tmpbuf_n_ = n; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void MD5::place_length(uint8_t *destination) const noexcept { |
| 119 | for (std::size_t i = 0; i < 8; ++i) |
| 120 | destination[i] = static_cast<uint8_t>(message_len_ >> (8 * i)); |
| 121 | } |
| 122 | |
| 123 | std::array<uint8_t, 16> MD5::digest() const noexcept { |
| 124 | // copies of sums and buffers |
| 125 | auto sums = sums_; |
| 126 | auto buf = tmpbuf_; |
| 127 | std::size_t n = tmpbuf_n_; |
| 128 | |
| 129 | if (n >= 56) { |
| 130 | // must append current block, required padding bit and length won't fit. |
| 131 | // n is never buf.size() (= 64) yet |
| 132 | buf[n++] = 0x80U; |
| 133 | while (n < buf.size()) |
| 134 | buf[n++] = 0; |
| 135 | round(sums, buf.data()); |
| 136 | |
| 137 | // all zeroes, except for length |
| 138 | std::fill(buf.begin(), buf.begin() + 56, 0); |
| 139 | |
| 140 | } else { // n < 56 |
| 141 | // append padding to tmpbuf and then the length |
| 142 | buf[n++] = 0x80U; |
| 143 | while (n < 56) |
| 144 | buf[n++] = 0; |
| 145 | } |
| 146 | |
| 147 | place_length(&buf[56]); |
| 148 | round(sums, buf.data()); |
| 149 | |
| 150 | return {static_cast<uint8_t>(sums[0]), static_cast<uint8_t>(sums[0] >> 8), |
| 151 | static_cast<uint8_t>(sums[0] >> 16), static_cast<uint8_t>(sums[0] >> 24), |
| 152 | static_cast<uint8_t>(sums[1]), static_cast<uint8_t>(sums[1] >> 8), |
| 153 | static_cast<uint8_t>(sums[1] >> 16), static_cast<uint8_t>(sums[1] >> 24), |
| 154 | static_cast<uint8_t>(sums[2]), static_cast<uint8_t>(sums[2] >> 8), |
| 155 | static_cast<uint8_t>(sums[2] >> 16), static_cast<uint8_t>(sums[2] >> 24), |
| 156 | static_cast<uint8_t>(sums[3]), static_cast<uint8_t>(sums[3] >> 8), |
| 157 | static_cast<uint8_t>(sums[3] >> 16), static_cast<uint8_t>(sums[3] >> 24)}; |
| 158 | } |
| 159 | |
| 160 | void MD5::update(float valin) noexcept { |
| 161 | float val = INTEL_FLOAT(valin); |
| 162 | uint8_t *p = (uint8_t *)&val; |
| 163 | update(p, sizeof(float)); |
| 164 | } |
| 165 | |
| 166 | void MD5::update(int valin) noexcept { |
| 167 | int val = INTEL_INT(valin); |
| 168 | uint8_t *p = (uint8_t *)&val; |
| 169 | update(p, sizeof(int)); |
| 170 | } |
| 171 | |
| 172 | void MD5::update(int16_t valin) noexcept { |
| 173 | int16_t val = INTEL_SHORT(valin); |
| 174 | uint8_t *p = (uint8_t *)&val; |
| 175 | update(p, sizeof(int16_t)); |
| 176 | } |
| 177 | |
| 178 | void MD5::update(uint32_t valin) noexcept { |
| 179 | uint32_t val = INTEL_INT(valin); |
| 180 | uint8_t *p = (uint8_t *)&val; |
| 181 | update(p, sizeof(uint32_t)); |
| 182 | } |
| 183 | |
| 184 | void MD5::update(uint8_t val) noexcept { |
| 185 | uint8_t *p = (uint8_t *)&val; |
| 186 | update(p, sizeof(uint8_t)); |
| 187 | } |
| 188 | |
| 189 | void MD5::digest(uint8_t *destination) const noexcept { |
| 190 | auto digest_data = digest(); |
| 191 | std::copy(digest_data.begin(), digest_data.end(), destination); |
| 192 | } |