1/*
2 * This code is derived from (original license follows):
3 *
4 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
5 * MD5 Message-Digest Algorithm (RFC 1321).
6 *
7 * Homepage:
8 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
9 *
10 * Author:
11 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
12 *
13 * This software was written by Alexander Peslyak in 2001. No copyright is
14 * claimed, and the software is hereby placed in the public domain.
15 * In case this attempt to disclaim copyright and place the software in the
16 * public domain is deemed null and void, then the software is
17 * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
18 * general public under the following terms:
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted.
22 *
23 * There's ABSOLUTELY NO WARRANTY, express or implied.
24 *
25 * (This is a heavily cut-down "BSD license".)
26 *
27 * This differs from Colin Plumb's older public domain implementation in that
28 * no exactly 32-bit integer data type is required (any 32-bit or wider
29 * unsigned integer data type will do), there's no compile-time endianness
30 * configuration, and the function prototypes match OpenSSL's. No code from
31 * Colin Plumb's implementation has been reused; this comment merely compares
32 * the properties of the two independent implementations.
33 *
34 * The primary goals of this implementation are portability and ease of use.
35 * It is meant to be fast, but not as fast as possible. Some known
36 * optimizations are not included to reduce source code size and avoid
37 * compile-time configuration.
38 */
39
40#include "llvm/Support/MD5.h"
41#include "llvm/ADT/ArrayRef.h"
42#include "llvm/ADT/SmallString.h"
43#include "llvm/ADT/StringExtras.h"
44#include "llvm/ADT/StringRef.h"
45#include "llvm/Support/Endian.h"
46#include <cstdint>
47#include <cstring>
48
49// The basic MD5 functions.
50
51// F and G are optimized compared to their RFC 1321 definitions for
52// architectures that lack an AND-NOT instruction, just like in Colin Plumb's
53// implementation.
54#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
55#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
56#define H(x, y, z) ((x) ^ (y) ^ (z))
57#define I(x, y, z) ((y) ^ ((x) | ~(z)))
58
59// The MD5 transformation for all four rounds.
60#define STEP(f, a, b, c, d, x, t, s) \
61 (a) += f((b), (c), (d)) + (x) + (t); \
62 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
63 (a) += (b);
64
65// SET reads 4 input bytes in little-endian byte order and stores them
66// in a properly aligned word in host byte order.
67#define SET(n) \
68 (InternalState.block[(n)] = (MD5_u32plus)ptr[(n)*4] | \
69 ((MD5_u32plus)ptr[(n)*4 + 1] << 8) | \
70 ((MD5_u32plus)ptr[(n)*4 + 2] << 16) | \
71 ((MD5_u32plus)ptr[(n)*4 + 3] << 24))
72#define GET(n) (InternalState.block[(n)])
73
74using namespace llvm;
75
76/// This processes one or more 64-byte data blocks, but does NOT update
77///the bit counters. There are no alignment requirements.
78const uint8_t *MD5::body(ArrayRef<uint8_t> Data) {
79 const uint8_t *ptr;
80 MD5_u32plus a, b, c, d;
81 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
82 unsigned long Size = Data.size();
83
84 ptr = Data.data();
85
86 a = InternalState.a;
87 b = InternalState.b;
88 c = InternalState.c;
89 d = InternalState.d;
90
91 do {
92 saved_a = a;
93 saved_b = b;
94 saved_c = c;
95 saved_d = d;
96
97 // Round 1
98 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
99 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
100 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
101 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
102 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
103 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
104 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
105 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
106 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
107 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
108 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
109 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
110 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
111 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
112 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
113 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
114
115 // Round 2
116 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
117 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
118 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
119 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
120 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
121 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
122 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
123 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
124 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
125 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
126 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
127 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
128 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
129 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
130 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
131 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
132
133 // Round 3
134 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
135 STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
136 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
137 STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
138 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
139 STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
140 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
141 STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
142 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
143 STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
144 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
145 STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
146 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
147 STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
148 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
149 STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
150
151 // Round 4
152 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
153 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
154 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
155 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
156 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
157 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
158 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
159 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
160 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
161 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
162 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
163 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
164 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
165 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
166 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
167 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
168
169 a += saved_a;
170 b += saved_b;
171 c += saved_c;
172 d += saved_d;
173
174 ptr += 64;
175 } while (Size -= 64);
176
177 InternalState.a = a;
178 InternalState.b = b;
179 InternalState.c = c;
180 InternalState.d = d;
181
182 return ptr;
183}
184
185MD5::MD5() = default;
186
187/// Incrementally add the bytes in \p Data to the hash.
188void MD5::update(ArrayRef<uint8_t> Data) {
189 // An empty ArrayRef may have a null data(), which must not reach memcpy.
190 if (Data.empty())
191 return;
192
193 MD5_u32plus saved_lo;
194 unsigned long used, free;
195 const uint8_t *Ptr = Data.data();
196 unsigned long Size = Data.size();
197
198 saved_lo = InternalState.lo;
199 if ((InternalState.lo = (saved_lo + Size) & 0x1fffffff) < saved_lo)
200 InternalState.hi++;
201 InternalState.hi += Size >> 29;
202
203 used = saved_lo & 0x3f;
204
205 if (used) {
206 free = 64 - used;
207
208 if (Size < free) {
209 memcpy(dest: &InternalState.buffer[used], src: Ptr, n: Size);
210 return;
211 }
212
213 memcpy(dest: &InternalState.buffer[used], src: Ptr, n: free);
214 Ptr = Ptr + free;
215 Size -= free;
216 body(Data: ArrayRef(InternalState.buffer, 64));
217 }
218
219 if (Size >= 64) {
220 Ptr = body(Data: ArrayRef(Ptr, Size & ~(unsigned long)0x3f));
221 Size &= 0x3f;
222 }
223
224 memcpy(dest: InternalState.buffer, src: Ptr, n: Size);
225}
226
227/// Add the bytes in the StringRef \p Str to the hash.
228// Note that this isn't a string and so this won't include any trailing NULL
229// bytes.
230void MD5::update(StringRef Str) {
231 ArrayRef<uint8_t> SVal((const uint8_t *)Str.data(), Str.size());
232 update(Data: SVal);
233}
234
235/// Finish the hash and place the resulting hash into \p result.
236/// \param Result is assumed to be a minimum of 16-bytes in size.
237void MD5::final(MD5Result &Result) {
238 unsigned long used, free;
239
240 used = InternalState.lo & 0x3f;
241
242 InternalState.buffer[used++] = 0x80;
243
244 free = 64 - used;
245
246 if (free < 8) {
247 memset(s: &InternalState.buffer[used], c: 0, n: free);
248 body(Data: ArrayRef(InternalState.buffer, 64));
249 used = 0;
250 free = 64;
251 }
252
253 memset(s: &InternalState.buffer[used], c: 0, n: free - 8);
254
255 InternalState.lo <<= 3;
256 support::endian::write32le(P: &InternalState.buffer[56], V: InternalState.lo);
257 support::endian::write32le(P: &InternalState.buffer[60], V: InternalState.hi);
258
259 body(Data: ArrayRef(InternalState.buffer, 64));
260
261 support::endian::write32le(P: &Result[0], V: InternalState.a);
262 support::endian::write32le(P: &Result[4], V: InternalState.b);
263 support::endian::write32le(P: &Result[8], V: InternalState.c);
264 support::endian::write32le(P: &Result[12], V: InternalState.d);
265}
266
267MD5::MD5Result MD5::final() {
268 MD5Result Result;
269 final(Result);
270 return Result;
271}
272
273MD5::MD5Result MD5::result() {
274 auto StateToRestore = InternalState;
275
276 auto Hash = final();
277
278 // Restore the state
279 InternalState = StateToRestore;
280
281 return Hash;
282}
283
284SmallString<32> MD5::MD5Result::digest() const {
285 SmallString<32> Str;
286 toHex(Input: *this, /*LowerCase*/ true, Output&: Str);
287 return Str;
288}
289
290void MD5::stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str) {
291 toHex(Input: Result, /*LowerCase*/ true, Output&: Str);
292}
293
294MD5::MD5Result MD5::hash(ArrayRef<uint8_t> Data) {
295 MD5 Hash;
296 Hash.update(Data);
297 MD5::MD5Result Res;
298 Hash.final(Result&: Res);
299
300 return Res;
301}
302
303#undef F
304#undef G
305#undef H
306#undef I
307#undef STEP
308#undef SET
309#undef GET
310