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