/src/vvdec/source/Lib/libmd5/MD5.h
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | |
43 | | #pragma once |
44 | | |
45 | | #include <stdio.h> |
46 | | #include <string> |
47 | | #include <stdint.h> |
48 | | #include <cstring> |
49 | | |
50 | | namespace vvdec |
51 | | { |
52 | | |
53 | | static const uint32_t MD5_DIGEST_STRING_LENGTH=16; |
54 | | |
55 | | #ifndef __BIG_ENDIAN__ |
56 | | # define byteReverse(buf, len) /* Nothing */ |
57 | | #else |
58 | | static void byteReverse(uint32_t *buf, unsigned len); |
59 | | /* |
60 | | * Note: this code is harmless on little-endian machines. |
61 | | */ |
62 | | static void byteReverse(uint32_t *buf, unsigned len) |
63 | | { |
64 | | uint32_t t; |
65 | | do { |
66 | | char* bytes = (char *) buf; |
67 | | t = ((unsigned) bytes[3] << 8 | bytes[2]) << 16 | |
68 | | ((unsigned) bytes[1] << 8 | bytes[0]); |
69 | | *buf = t; |
70 | | buf++; |
71 | | } while (--len); |
72 | | } |
73 | | #endif |
74 | | |
75 | | namespace libmd5 { |
76 | | |
77 | | |
78 | | class MD5 |
79 | | { |
80 | | |
81 | | public: |
82 | | typedef struct _context_md5_t { |
83 | | uint32_t buf[4]; |
84 | | uint32_t bits[2]; |
85 | | union { |
86 | | unsigned char b8[64]; |
87 | | uint32_t b32[16]; |
88 | | } in; |
89 | | } context_md5_t; |
90 | | |
91 | | |
92 | | public: |
93 | | /** |
94 | | * initialize digest state |
95 | | */ |
96 | | MD5() |
97 | 0 | { |
98 | 0 | ctx.buf[0] = 0x67452301; |
99 | 0 | ctx.buf[1] = 0xefcdab89; |
100 | 0 | ctx.buf[2] = 0x98badcfe; |
101 | 0 | ctx.buf[3] = 0x10325476; |
102 | |
|
103 | 0 | ctx.bits[0] = 0; |
104 | 0 | ctx.bits[1] = 0; |
105 | 0 | } |
106 | | |
107 | | /** |
108 | | * compute digest over buf of length len. |
109 | | * multiple calls may extend the digest over more data. |
110 | | */ |
111 | | void update(unsigned char *buf, unsigned len) |
112 | 0 | { |
113 | 0 | uint32_t t; |
114 | | |
115 | | /* Update bitcount */ |
116 | |
|
117 | 0 | t = ctx.bits[0]; |
118 | 0 | if ((ctx.bits[0] = t + ((uint32_t) len << 3)) < t) |
119 | 0 | ctx.bits[1]++; /* Carry from low to high */ |
120 | 0 | ctx.bits[1] += len >> 29; |
121 | |
|
122 | 0 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
123 | | |
124 | | /* Handle any leading odd-sized chunks */ |
125 | |
|
126 | 0 | if (t) { |
127 | 0 | unsigned char *p = ctx.in.b8 + t; |
128 | |
|
129 | 0 | t = 64 - t; |
130 | 0 | if (len < t) { |
131 | 0 | ::memcpy(p, buf, len); |
132 | 0 | return; |
133 | 0 | } |
134 | 0 | memcpy(p, buf, t); |
135 | 0 | byteReverse(ctx.in.b32, 16); |
136 | 0 | MD5Transform(ctx.buf, ctx.in.b32); |
137 | 0 | buf += t; |
138 | 0 | len -= t; |
139 | 0 | } |
140 | | /* Process data in 64-byte chunks */ |
141 | | |
142 | 0 | while (len >= 64) { |
143 | 0 | memcpy(ctx.in.b8, buf, 64); |
144 | 0 | byteReverse(ctx.in.b32, 16); |
145 | 0 | MD5Transform(ctx.buf, ctx.in.b32); |
146 | 0 | buf += 64; |
147 | 0 | len -= 64; |
148 | 0 | } |
149 | | |
150 | | /* Handle any remaining bytes of data. */ |
151 | |
|
152 | 0 | memcpy(ctx.in.b8, buf, len); |
153 | 0 | } |
154 | | |
155 | | /** |
156 | | * flush any outstanding MD5 data, write the digest into digest. |
157 | | */ |
158 | | void finalize(unsigned char digest[MD5_DIGEST_STRING_LENGTH]) |
159 | 0 | { |
160 | 0 | unsigned count; |
161 | 0 | unsigned char *p; |
162 | | |
163 | | /* Compute number of bytes mod 64 */ |
164 | 0 | count = (ctx.bits[0] >> 3) & 0x3F; |
165 | | |
166 | | /* Set the first char of padding to 0x80. This is safe since there is |
167 | | always at least one byte free */ |
168 | 0 | p = ctx.in.b8 + count; |
169 | 0 | *p++ = 0x80; |
170 | | |
171 | | /* Bytes of padding needed to make 64 bytes */ |
172 | 0 | count = 64 - 1 - count; |
173 | | |
174 | | /* Pad out to 56 mod 64 */ |
175 | 0 | if (count < 8) { |
176 | | /* Two lots of padding: Pad the first block to 64 bytes */ |
177 | 0 | memset(p, 0, count); |
178 | 0 | byteReverse(ctx.in.b32, 16); |
179 | 0 | MD5Transform(ctx.buf, ctx.in.b32); |
180 | | |
181 | | /* Now fill the next block with 56 bytes */ |
182 | 0 | memset(ctx.in.b8, 0, 56); |
183 | 0 | } else { |
184 | | /* Pad block to 56 bytes */ |
185 | 0 | memset(p, 0, count - 8); |
186 | 0 | } |
187 | 0 | byteReverse(ctx.in.b32, 14); |
188 | | |
189 | | /* Append length in bits and transform */ |
190 | 0 | ctx.in.b32[14] = ctx.bits[0]; |
191 | 0 | ctx.in.b32[15] = ctx.bits[1]; |
192 | |
|
193 | 0 | MD5Transform(ctx.buf, ctx.in.b32); |
194 | 0 | byteReverse((uint32_t *) ctx.buf, 4); |
195 | 0 | memcpy(digest, ctx.buf, 16); |
196 | |
|
197 | 0 | memset(&ctx, 0, sizeof( ctx)); /* In case it's sensitive */ |
198 | | /* The original version of this code omitted the asterisk. In |
199 | | effect, only the first part of ctx was wiped with zeros, not |
200 | | the whole thing. Bug found by Derek Jones. Original line: */ |
201 | | // memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ |
202 | 0 | } |
203 | | |
204 | | |
205 | | private: |
206 | | |
207 | | |
208 | | /* The four core functions - F1 is optimized somewhat */ |
209 | | |
210 | | /* #define F1(x, y, z) (x & y | ~x & z) */ |
211 | 0 | #define F1(x, y, z) (z ^ (x & (y ^ z))) |
212 | 0 | #define F2(x, y, z) F1(z, x, y) |
213 | 0 | #define F3(x, y, z) (x ^ y ^ z) |
214 | 0 | #define F4(x, y, z) (y ^ (x | ~z)) |
215 | | |
216 | | /* This is the central step in the MD5 algorithm. */ |
217 | | #define MD5STEP(f, w, x, y, z, data, s) \ |
218 | 0 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) |
219 | | |
220 | | |
221 | | /* |
222 | | * The core of the MD5 algorithm, this alters an existing MD5 hash to |
223 | | * reflect the addition of 16 longwords of new data. MD5Update blocks |
224 | | * the data and converts bytes into longwords for this routine. |
225 | | */ |
226 | | static void MD5Transform(uint32_t buf[4], uint32_t const in[16]) |
227 | 0 | { |
228 | 0 | uint32_t a, b, c, d; |
229 | |
|
230 | 0 | a = buf[0]; |
231 | 0 | b = buf[1]; |
232 | 0 | c = buf[2]; |
233 | 0 | d = buf[3]; |
234 | |
|
235 | 0 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); |
236 | 0 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); |
237 | 0 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); |
238 | 0 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); |
239 | 0 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); |
240 | 0 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); |
241 | 0 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); |
242 | 0 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); |
243 | 0 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); |
244 | 0 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); |
245 | 0 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); |
246 | 0 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); |
247 | 0 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); |
248 | 0 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); |
249 | 0 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); |
250 | 0 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); |
251 | |
|
252 | 0 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); |
253 | 0 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); |
254 | 0 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); |
255 | 0 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); |
256 | 0 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); |
257 | 0 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); |
258 | 0 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); |
259 | 0 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); |
260 | 0 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); |
261 | 0 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); |
262 | 0 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); |
263 | 0 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); |
264 | 0 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); |
265 | 0 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); |
266 | 0 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); |
267 | 0 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); |
268 | |
|
269 | 0 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); |
270 | 0 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); |
271 | 0 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); |
272 | 0 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); |
273 | 0 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); |
274 | 0 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); |
275 | 0 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); |
276 | 0 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); |
277 | 0 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); |
278 | 0 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); |
279 | 0 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); |
280 | 0 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); |
281 | 0 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); |
282 | 0 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); |
283 | 0 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); |
284 | 0 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); |
285 | |
|
286 | 0 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); |
287 | 0 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); |
288 | 0 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); |
289 | 0 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); |
290 | 0 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); |
291 | 0 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); |
292 | 0 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); |
293 | 0 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); |
294 | 0 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); |
295 | 0 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); |
296 | 0 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); |
297 | 0 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); |
298 | 0 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); |
299 | 0 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); |
300 | 0 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); |
301 | 0 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); |
302 | |
|
303 | 0 | buf[0] += a; |
304 | 0 | buf[1] += b; |
305 | 0 | buf[2] += c; |
306 | 0 | buf[3] += d; |
307 | 0 | } |
308 | | |
309 | | private: |
310 | | context_md5_t ctx; |
311 | | }; |
312 | | |
313 | | } |
314 | | |
315 | | } // namespace |