Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/llvm/lib/Support/MD5.cpp
Line
Count
Source (jump to first uncovered line)
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/StringRef.h"
44
#include "llvm/Support/Endian.h"
45
#include "llvm/Support/Format.h"
46
#include "llvm/Support/raw_ostream.h"
47
#include <array>
48
#include <cstdint>
49
#include <cstring>
50
51
// The basic MD5 functions.
52
53
// F and G are optimized compared to their RFC 1321 definitions for
54
// architectures that lack an AND-NOT instruction, just like in Colin Plumb's
55
// implementation.
56
0
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
57
0
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
58
0
#define H(x, y, z) ((x) ^ (y) ^ (z))
59
0
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
60
61
// The MD5 transformation for all four rounds.
62
#define STEP(f, a, b, c, d, x, t, s)                                           \
63
0
  (a) += f((b), (c), (d)) + (x) + (t);                                         \
64
0
  (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));                   \
65
0
  (a) += (b);
66
67
// SET reads 4 input bytes in little-endian byte order and stores them
68
// in a properly aligned word in host byte order.
69
#define SET(n)                                                                 \
70
  (block[(n)] =                                                                \
71
       (MD5_u32plus) ptr[(n) * 4] | ((MD5_u32plus) ptr[(n) * 4 + 1] << 8) |    \
72
       ((MD5_u32plus) ptr[(n) * 4 + 2] << 16) |                                \
73
       ((MD5_u32plus) ptr[(n) * 4 + 3] << 24))
74
#define GET(n) (block[(n)])
75
76
using namespace llvm;
77
78
/// This processes one or more 64-byte data blocks, but does NOT update
79
///the bit counters.  There are no alignment requirements.
80
0
const uint8_t *MD5::body(ArrayRef<uint8_t> Data) {
81
0
  const uint8_t *ptr;
82
0
  MD5_u32plus a, b, c, d;
83
0
  MD5_u32plus saved_a, saved_b, saved_c, saved_d;
84
0
  unsigned long Size = Data.size();
85
0
86
0
  ptr = Data.data();
87
0
88
0
  a = this->a;
89
0
  b = this->b;
90
0
  c = this->c;
91
0
  d = this->d;
92
0
93
0
  do {
94
0
    saved_a = a;
95
0
    saved_b = b;
96
0
    saved_c = c;
97
0
    saved_d = d;
98
0
99
0
    // Round 1
100
0
    STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
101
0
    STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
102
0
    STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
103
0
    STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
104
0
    STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
105
0
    STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
106
0
    STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
107
0
    STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
108
0
    STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
109
0
    STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
110
0
    STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
111
0
    STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
112
0
    STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
113
0
    STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
114
0
    STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
115
0
    STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
116
0
117
0
    // Round 2
118
0
    STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
119
0
    STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
120
0
    STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
121
0
    STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
122
0
    STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
123
0
    STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
124
0
    STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
125
0
    STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
126
0
    STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
127
0
    STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
128
0
    STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
129
0
    STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
130
0
    STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
131
0
    STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
132
0
    STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
133
0
    STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
134
0
135
0
    // Round 3
136
0
    STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
137
0
    STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
138
0
    STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
139
0
    STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
140
0
    STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
141
0
    STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
142
0
    STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
143
0
    STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
144
0
    STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
145
0
    STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
146
0
    STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
147
0
    STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
148
0
    STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
149
0
    STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
150
0
    STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
151
0
    STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
152
0
153
0
    // Round 4
154
0
    STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
155
0
    STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
156
0
    STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
157
0
    STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
158
0
    STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
159
0
    STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
160
0
    STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
161
0
    STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
162
0
    STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
163
0
    STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
164
0
    STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
165
0
    STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
166
0
    STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
167
0
    STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
168
0
    STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
169
0
    STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
170
0
171
0
    a += saved_a;
172
0
    b += saved_b;
173
0
    c += saved_c;
174
0
    d += saved_d;
175
0
176
0
    ptr += 64;
177
0
  } while (Size -= 64);
178
0
179
0
  this->a = a;
180
0
  this->b = b;
181
0
  this->c = c;
182
0
  this->d = d;
183
0
184
0
  return ptr;
185
0
}
186
187
0
MD5::MD5() = default;
188
189
/// Incrementally add the bytes in \p Data to the hash.
190
0
void MD5::update(ArrayRef<uint8_t> Data) {
191
0
  MD5_u32plus saved_lo;
192
0
  unsigned long used, free;
193
0
  const uint8_t *Ptr = Data.data();
194
0
  unsigned long Size = Data.size();
195
0
196
0
  saved_lo = lo;
197
0
  if ((lo = (saved_lo + Size) & 0x1fffffff) < saved_lo)
198
0
    hi++;
199
0
  hi += Size >> 29;
200
0
201
0
  used = saved_lo & 0x3f;
202
0
203
0
  if (used) {
204
0
    free = 64 - used;
205
0
206
0
    if (Size < free) {
207
0
      memcpy(&buffer[used], Ptr, Size);
208
0
      return;
209
0
    }
210
0
211
0
    memcpy(&buffer[used], Ptr, free);
212
0
    Ptr = Ptr + free;
213
0
    Size -= free;
214
0
    body(makeArrayRef(buffer, 64));
215
0
  }
216
0
217
0
  if (Size >= 64) {
218
0
    Ptr = body(makeArrayRef(Ptr, Size & ~(unsigned long) 0x3f));
219
0
    Size &= 0x3f;
220
0
  }
221
0
222
0
  memcpy(buffer, Ptr, Size);
223
0
}
224
225
/// Add the bytes in the StringRef \p Str to the hash.
226
// Note that this isn't a string and so this won't include any trailing NULL
227
// bytes.
228
0
void MD5::update(StringRef Str) {
229
0
  ArrayRef<uint8_t> SVal((const uint8_t *)Str.data(), Str.size());
230
0
  update(SVal);
231
0
}
232
233
/// Finish the hash and place the resulting hash into \p result.
234
/// \param Result is assumed to be a minimum of 16-bytes in size.
235
0
void MD5::final(MD5Result &Result) {
236
0
  unsigned long used, free;
237
0
238
0
  used = lo & 0x3f;
239
0
240
0
  buffer[used++] = 0x80;
241
0
242
0
  free = 64 - used;
243
0
244
0
  if (free < 8) {
245
0
    memset(&buffer[used], 0, free);
246
0
    body(makeArrayRef(buffer, 64));
247
0
    used = 0;
248
0
    free = 64;
249
0
  }
250
0
251
0
  memset(&buffer[used], 0, free - 8);
252
0
253
0
  lo <<= 3;
254
0
  support::endian::write32le(&buffer[56], lo);
255
0
  support::endian::write32le(&buffer[60], hi);
256
0
257
0
  body(makeArrayRef(buffer, 64));
258
0
259
0
  support::endian::write32le(&Result[0], a);
260
0
  support::endian::write32le(&Result[4], b);
261
0
  support::endian::write32le(&Result[8], c);
262
0
  support::endian::write32le(&Result[12], d);
263
0
}
264
265
0
SmallString<32> MD5::MD5Result::digest() const {
266
0
  SmallString<32> Str;
267
0
  raw_svector_ostream Res(Str);
268
0
  for (int i = 0; i < 16; ++i)
269
0
    Res << format("%.2x", Bytes[i]);
270
0
  return Str;
271
0
}
272
273
0
void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) {
274
0
  Str = Result.digest();
275
0
}
276
277
0
std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) {
278
0
  MD5 Hash;
279
0
  Hash.update(Data);
280
0
  MD5::MD5Result Res;
281
0
  Hash.final(Res);
282
0
283
0
  return Res;
284
0
}