Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. 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 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : // Based on md5.cpp from Crypto++ 5.6.0:
21 : // "md5.cpp - modified by Wei Dai from Colin Plumb's public domain md5.c"
22 : // "any modifications are placed in the public domain"
23 :
24 : #include "MD5.h"
25 :
26 9383 : MD5::MD5()
27 : {
28 9383 : InitState();
29 9383 : }
30 :
31 18385 : void MD5::InitState()
32 : {
33 18385 : m_Digest[0] = 0x67452301L;
34 18385 : m_Digest[1] = 0xefcdab89L;
35 18385 : m_Digest[2] = 0x98badcfeL;
36 18385 : m_Digest[3] = 0x10325476L;
37 18385 : m_BufLen = 0;
38 18385 : m_InputLen = 0;
39 18385 : memset(m_Buf, 0xcc, sizeof(m_Buf));
40 0 : }
41 :
42 35095 : void MD5::UpdateRest(const std::uint8_t* data, size_t len)
43 : {
44 35095 : const size_t CHUNK_SIZE = sizeof(m_Buf);
45 :
46 : // Add as much data as possible to the buffer
47 35095 : size_t n = CHUNK_SIZE - m_BufLen;
48 : // ENSURE(len >= n);
49 35095 : memcpy(m_Buf + m_BufLen, data, n);
50 35095 : data += n;
51 35095 : len -= n;
52 :
53 : // Flush the (now full) buffer
54 35095 : Transform(reinterpret_cast<const std::uint32_t*>(m_Buf)); // assumes little-endian
55 :
56 : // Process whole chunks of the input
57 84722 : while (len >= CHUNK_SIZE)
58 : {
59 : #if defined(__ARM_ARCH_7A__)
60 : // Avoid SIGBUS on alignment-sensitive architecture
61 : memcpy(m_Buf, data, CHUNK_SIZE);
62 : Transform(reinterpret_cast<const std::uint32_t*>(m_Buf));
63 : #else
64 14532 : Transform(reinterpret_cast<const std::uint32_t*>(data)); // assumes little-endian; ignores alignment
65 : #endif
66 14532 : data += CHUNK_SIZE;
67 14532 : len -= CHUNK_SIZE;
68 : }
69 :
70 : // Add the remainder to the buffer
71 35095 : memcpy(m_Buf, data, len);
72 35095 : m_BufLen = len;
73 35095 : }
74 :
75 9002 : void MD5::Final(std::uint8_t* digest)
76 : {
77 : // Compute the message length in bits (before padding)
78 9002 : std::uint64_t len = m_InputLen * 8;
79 :
80 : // Pad with 1-bit
81 9002 : const std::uint8_t pad = 0x80;
82 9002 : Update(&pad, 1);
83 :
84 : // Fill with zeros until length % 64 = 56 (bytes)
85 323666 : while (m_BufLen % 64 != 56)
86 : {
87 314664 : const std::uint8_t zero = 0;
88 314664 : Update(&zero, 1);
89 : }
90 :
91 : // Append the length (assumes little-endian)
92 9002 : Update(reinterpret_cast<const std::uint8_t*>(&len), 8);
93 :
94 : // Return the digest (assumes little-endian)
95 9002 : memcpy(digest, m_Digest, DIGESTSIZE);
96 :
97 : // Reset
98 9002 : InitState();
99 9002 : }
100 :
101 : // Use macro rather than inline function for significantly better debug-mode performance
102 : #define rotlFixed(x, y) (((x) << (y)) | ((x) >> (32 - (y))))
103 : // TODO: Crypto++ has an overload using _lrotl on MSVC - is that worthwhile?
104 :
105 49627 : void MD5::Transform(const std::uint32_t* in)
106 : {
107 : #define F1(x, y, z) (z ^ (x & (y ^ z)))
108 : #define F2(x, y, z) F1(z, x, y)
109 : #define F3(x, y, z) (x ^ y ^ z)
110 : #define F4(x, y, z) (y ^ (x | ~z))
111 :
112 : #define MD5STEP(f, w, x, y, z, data, s) \
113 : t = w + f(x, y, z) + data; w = rotlFixed(t, s) + x
114 :
115 49627 : std::uint32_t* digest = m_Digest;
116 :
117 49627 : std::uint32_t a, b, c, d;
118 49627 : std::uint32_t t;
119 :
120 49627 : a = digest[0];
121 49627 : b = digest[1];
122 49627 : c = digest[2];
123 49627 : d = digest[3];
124 :
125 49627 : MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
126 49627 : MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
127 49627 : MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
128 49627 : MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
129 49627 : MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
130 49627 : MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
131 49627 : MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
132 49627 : MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
133 49627 : MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
134 49627 : MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
135 49627 : MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
136 49627 : MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
137 49627 : MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
138 49627 : MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
139 49627 : MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
140 49627 : MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
141 :
142 49627 : MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
143 49627 : MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
144 49627 : MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
145 49627 : MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
146 49627 : MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
147 49627 : MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
148 49627 : MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
149 49627 : MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
150 49627 : MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
151 49627 : MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
152 49627 : MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
153 49627 : MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
154 49627 : MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
155 49627 : MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
156 49627 : MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
157 49627 : MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
158 :
159 49627 : MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
160 49627 : MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
161 49627 : MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
162 49627 : MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
163 49627 : MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
164 49627 : MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
165 49627 : MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
166 49627 : MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
167 49627 : MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
168 49627 : MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
169 49627 : MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
170 49627 : MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
171 49627 : MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
172 49627 : MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
173 49627 : MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
174 49627 : MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
175 :
176 49627 : MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
177 49627 : MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
178 49627 : MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
179 49627 : MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
180 49627 : MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
181 49627 : MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
182 49627 : MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
183 49627 : MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
184 49627 : MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
185 49627 : MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
186 49627 : MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
187 49627 : MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
188 49627 : MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
189 49627 : MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
190 49627 : MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
191 49627 : MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
192 :
193 49627 : digest[0] += a;
194 49627 : digest[1] += b;
195 49627 : digest[2] += c;
196 49627 : digest[3] += d;
197 49627 : }
|