Debian Codegraph @

md5/md5.cpp

raw symbols SCIP
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"cxx . . $ `<file>/md5/md5.h`/
19 #include <byteswap.h>cxx . . $ `<file>/lib/byteswap.h`/
20
21 #include <algorithm>
22
23 namespacecxx . . $ `$anonymous_namespace_md5/md5.cpp`/ {
24
25 const uint32_tcxx . . $ uint32_t# md5_round_shiftscxx . . $ `$anonymous_namespace_md5/md5.cpp`/md5_round_shifts.[] = {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21};
26
27 const stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# md5_round_constantscxx . . $ `$anonymous_namespace_md5/md5.cpp`/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 stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# rotl_uint32cxx . . $ `$anonymous_namespace_md5/md5.cpp`/rotl_uint32(deb1d1a49ee81c2e).(stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# a, stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# b) noexcept { return b ? (a << b) | (a >> (32 - b)) : a; };
38
39 }; // namespace
40
41 void MD5cxx . . $ MD5#::roundcxx . . $ MD5#round(5fa761607a5a5a5a).(stdcxx . . $ std/::arraycxx . . $ std/array#<stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#, 4> &sums, const uint8_tcxx . . $ uint8_t# *block) const noexcept {
42 // break 64 bytes into 16 dwords
43 stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# m[16];
44 for (stdcxx . . $ std/::size_tcxx . . $ std/size_t# i = 0; i < 16; ++i) {
45 m[i] = (static_cast<stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#>(block[(4 * i)])) | (static_cast<stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#>(block[(4 * i) + 1]) << 8) |
46 (static_cast<stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#>(block[(4 * i) + 2]) << 16) |
47 (static_cast<stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#>(block[(4 * i) + 3]) << 24);
48 }
49
50 stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# a = sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).0], b = sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1], c = sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2], d = sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3];
51
52 // 64 rounds...
53 for (uint32_tcxx . . $ uint32_t# r = 0; r < 64; ++r) {
54 stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t# f, g;
55 uint32_tcxx . . $ uint32_t# s = md5_round_shiftscxx . . $ `$anonymous_namespace_md5/md5.cpp`/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_uint32cxx . . $ `$anonymous_namespace_md5/md5.cpp`/rotl_uint32(deb1d1a49ee81c2e).(f + a + m[g] + md5_round_constantscxx . . $ `$anonymous_namespace_md5/md5.cpp`/md5_round_constants.[r], s) + b;
72 a = d;
73 d = c;
74 c = b;
75 b = f;
76 }
77
78 sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).0] += a, sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1] += b, sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2] += c, sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3] += d;
79 }
80
81 void MD5cxx . . $ MD5#::roundcxx . . $ MD5#round(edd025765e4e5935).(const uint8_tcxx . . $ uint8_t# *block) noexcept { roundcxx . . $ MD5#round(5fa761607a5a5a5a).(sums_cxx . . $ MD5#sums_., block); }
82
83 void MD5cxx . . $ MD5#::updatecxx . . $ MD5#update(d0cd15426258c4d3).(const uint8_tcxx . . $ uint8_t# *data, stdcxx . . $ std/::size_tcxx . . $ std/size_t# n) noexcept {
84 message_len_cxx . . $ MD5#message_len_. += n * 8;
85
86 // if there is already some data in tmpbuf, check if we would fill it
87 if (tmpbuf_n_cxx . . $ MD5#tmpbuf_n_.) {
88 if ((n >= tmpbuf_cxx . . $ MD5#tmpbuf_..sizecxx . . $ std/array#size(c9b1e749b64d5edc).() || tmpbuf_n_cxx . . $ MD5#tmpbuf_n_. + n >= tmpbuf_cxx . . $ MD5#tmpbuf_..sizecxx . . $ std/array#size(c9b1e749b64d5edc).())) {
89 // we would fill tmpbuf at least once; handle partial block first.
90 stdcxx . . $ std/::size_tcxx . . $ std/size_t# remaining = tmpbuf_cxx . . $ MD5#tmpbuf_..sizecxx . . $ std/array#size(c9b1e749b64d5edc).() - tmpbuf_n_cxx . . $ MD5#tmpbuf_n_.;
91 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(data, data + remaining, tmpbuf_cxx . . $ MD5#tmpbuf_..begincxx . . $ std/array#begin(258ee1c9e0a18a5c).() + tmpbuf_n_cxx . . $ MD5#tmpbuf_n_.);
92 data += remaining;
93 n -= remaining;
94 roundcxx . . $ MD5#round(edd025765e4e5935).(tmpbuf_cxx . . $ MD5#tmpbuf_..datacxx . . $ std/array#data(258ee1c9e0a18a5c).());
95 tmpbuf_n_cxx . . $ MD5#tmpbuf_n_. = 0;
96 } else {
97 // we will not fill the buffer - just copy and return
98 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(data, data + n, tmpbuf_cxx . . $ MD5#tmpbuf_..begincxx . . $ std/array#begin(258ee1c9e0a18a5c).() + tmpbuf_n_cxx . . $ MD5#tmpbuf_n_.);
99 tmpbuf_n_cxx . . $ MD5#tmpbuf_n_. += n;
100 return;
101 }
102 }
103
104 // do rounds on full blocks for as long as we can
105 while (n >= tmpbuf_cxx . . $ MD5#tmpbuf_..sizecxx . . $ std/array#size(c9b1e749b64d5edc).()) {
106 roundcxx . . $ MD5#round(edd025765e4e5935).(data);
107 data += tmpbuf_cxx . . $ MD5#tmpbuf_..sizecxx . . $ std/array#size(c9b1e749b64d5edc).();
108 n -= tmpbuf_cxx . . $ MD5#tmpbuf_..sizecxx . . $ std/array#size(c9b1e749b64d5edc).();
109 }
110
111 if (n) {
112 // copy partial block
113 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(data, data + n, tmpbuf_cxx . . $ MD5#tmpbuf_..begincxx . . $ std/array#begin(258ee1c9e0a18a5c).());
114 tmpbuf_n_cxx . . $ MD5#tmpbuf_n_. = n;
115 }
116 }
117
118 void MD5cxx . . $ MD5#::place_lengthcxx . . $ MD5#place_length(2c93ef52c863c53f).(uint8_tcxx . . $ uint8_t# *destination) const noexcept {
119 for (stdcxx . . $ std/::size_tcxx . . $ std/size_t# i = 0; i < 8; ++i)
120 destination[i] = static_cast<uint8_tcxx . . $ uint8_t#>(message_len_cxx . . $ MD5#message_len_. >> (8 * i));
121 }
122
123 stdcxx . . $ std/::arraycxx . . $ std/array#<uint8_tcxx . . $ uint8_t#, 16> MD5cxx . . $ MD5#::digestcxx . . $ MD5#digest(8552f21bb2c3f060).() const noexcept {
124 // copies of sums and buffers
125 auto sums = sums_cxx . . $ MD5#sums_.;
126 auto buf = tmpbuf_cxx . . $ MD5#tmpbuf_.;
127 stdcxx . . $ std/::size_tcxx . . $ std/size_t# n = tmpbuf_n_cxx . . $ MD5#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[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).n++] = 0x80U;
133 while (n < buf.sizecxx . . $ std/array#size(c9b1e749b64d5edc).())
134 buf[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).n++] = 0;
135 roundcxx . . $ MD5#round(5fa761607a5a5a5a).(sums, buf.datacxx . . $ std/array#data(258ee1c9e0a18a5c).());
136
137 // all zeroes, except for length
138 stdcxx . . $ std/::fillcxx . . $ std/fill(4a9bfaed88eccda7).(buf.begincxx . . $ std/array#begin(258ee1c9e0a18a5c).(), buf.begincxx . . $ std/array#begin(258ee1c9e0a18a5c).() + 56, 0);
139
140 } else { // n < 56
141 // append padding to tmpbuf and then the length
142 buf[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).n++] = 0x80U;
143 while (n < 56)
144 buf[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).n++] = 0;
145 }
146
147 place_lengthcxx . . $ MD5#place_length(2c93ef52c863c53f).(&buf[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).56]);
148 roundcxx . . $ MD5#round(5fa761607a5a5a5a).(sums, buf.datacxx . . $ std/array#data(258ee1c9e0a18a5c).());
149
150 return {static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).0]), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).0] >> 8),
151 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).0] >> 16), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).0] >> 24),
152 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1]), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1] >> 8),
153 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1] >> 16), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).1] >> 24),
154 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2]), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2] >> 8),
155 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2] >> 16), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).2] >> 24),
156 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3]), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3] >> 8),
157 static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3] >> 16), static_cast<uint8_tcxx . . $ uint8_t#>(sums[cxx . . $ std/array#`operator[]`(b18cc97d22f46aed).3] >> 24)};
158 }
159
160 void MD5cxx . . $ MD5#::updatecxx . . $ MD5#update(75096ca55fa6cf9c).(float valin) noexcept {
161 float val = INTEL_FLOATcxx . . $ `lib/byteswap.h:109:9`!(valin);
162 uint8_tcxx . . $ uint8_t# *p = (uint8_tcxx . . $ uint8_t# *)&val;
163 updatecxx . . $ MD5#update(d0cd15426258c4d3).(p, sizeof(float));
164 }
165
166 void MD5cxx . . $ MD5#::updatecxx . . $ MD5#update(f349c77f591a7878).(int valin) noexcept {
167 int val = INTEL_INTcxx . . $ `lib/byteswap.h:107:9`!(valin);
168 uint8_tcxx . . $ uint8_t# *p = (uint8_tcxx . . $ uint8_t# *)&val;
169 updatecxx . . $ MD5#update(d0cd15426258c4d3).(p, sizeof(int));
170 }
171
172 void MD5cxx . . $ MD5#::updatecxx . . $ MD5#update(e258ceb3f488048a).(int16_tcxx . . $ int16_t# valin) noexcept {
173 int16_tcxx . . $ int16_t# val = INTEL_SHORTcxx . . $ `lib/byteswap.h:108:9`!(valin);
174 uint8_tcxx . . $ uint8_t# *p = (uint8_tcxx . . $ uint8_t# *)&val;
175 updatecxx . . $ MD5#update(d0cd15426258c4d3).(p, sizeof(int16_tcxx . . $ int16_t#));
176 }
177
178 void MD5cxx . . $ MD5#::updatecxx . . $ MD5#update(6bab9b75950d6f3).(uint32_tcxx . . $ uint32_t# valin) noexcept {
179 uint32_tcxx . . $ uint32_t# val = INTEL_INTcxx . . $ `lib/byteswap.h:107:9`!(valin);
180 uint8_tcxx . . $ uint8_t# *p = (uint8_tcxx . . $ uint8_t# *)&val;
181 updatecxx . . $ MD5#update(d0cd15426258c4d3).(p, sizeof(uint32_tcxx . . $ uint32_t#));
182 }
183
184 void MD5cxx . . $ MD5#::updatecxx . . $ MD5#update(2008a3f7800ba735).(uint8_tcxx . . $ uint8_t# val) noexcept {
185 uint8_tcxx . . $ uint8_t# *p = (uint8_tcxx . . $ uint8_t# *)&val;
186 updatecxx . . $ MD5#update(d0cd15426258c4d3).(p, sizeof(uint8_tcxx . . $ uint8_t#));
187 }
188
189 void MD5cxx . . $ MD5#::digestcxx . . $ MD5#digest(2c93ef52c863c53f).(uint8_tcxx . . $ uint8_t# *destination) const noexcept {
190 auto digest_data = digestcxx . . $ MD5#digest(8552f21bb2c3f060).();
191 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(digest_data.begincxx . . $ std/array#begin(258ee1c9e0a18a5c).(), digest_data.endcxx . . $ std/array#end(258ee1c9e0a18a5c).(), destination);
192 }