Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/SHA1.cpp
Line
Count
Source
1
//====- SHA1.cpp - Private copy of the SHA1 implementation ---*- C++ -* ======//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This code is taken from public domain
10
// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c and
11
// http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/hash/sha1/sha1.c?rev=1.6)
12
// and modified by wrapping it in a C++ interface for LLVM,
13
// and removing unnecessary code.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "llvm/Support/SHA1.h"
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/Support/Host.h"
20
using namespace llvm;
21
22
#include <stdint.h>
23
#include <string.h>
24
25
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
26
#define SHA_BIG_ENDIAN
27
#endif
28
29
221M
static uint32_t rol(uint32_t Number, int Bits) {
30
221M
  return (Number << Bits) | (Number >> (32 - Bits));
31
221M
}
32
33
15.8M
static uint32_t blk0(uint32_t *Buf, int I) { return Buf[I]; }
34
35
63.2M
static uint32_t blk(uint32_t *Buf, int I) {
36
63.2M
  Buf[I & 15] = rol(Buf[(I + 13) & 15] ^ Buf[(I + 8) & 15] ^ Buf[(I + 2) & 15] ^
37
63.2M
                        Buf[I & 15],
38
63.2M
                    1);
39
63.2M
  return Buf[I & 15];
40
63.2M
}
41
42
static void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
43
15.8M
               int I, uint32_t *Buf) {
44
15.8M
  E += ((B & (C ^ D)) ^ D) + blk0(Buf, I) + 0x5A827999 + rol(A, 5);
45
15.8M
  B = rol(B, 30);
46
15.8M
}
47
48
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
49
3.95M
               int I, uint32_t *Buf) {
50
3.95M
  E += ((B & (C ^ D)) ^ D) + blk(Buf, I) + 0x5A827999 + rol(A, 5);
51
3.95M
  B = rol(B, 30);
52
3.95M
}
53
54
static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
55
19.7M
               int I, uint32_t *Buf) {
56
19.7M
  E += (B ^ C ^ D) + blk(Buf, I) + 0x6ED9EBA1 + rol(A, 5);
57
19.7M
  B = rol(B, 30);
58
19.7M
}
59
60
static void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
61
19.7M
               int I, uint32_t *Buf) {
62
19.7M
  E += (((B | C) & D) | (B & C)) + blk(Buf, I) + 0x8F1BBCDC + rol(A, 5);
63
19.7M
  B = rol(B, 30);
64
19.7M
}
65
66
static void r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
67
19.7M
               int I, uint32_t *Buf) {
68
19.7M
  E += (B ^ C ^ D) + blk(Buf, I) + 0xCA62C1D6 + rol(A, 5);
69
19.7M
  B = rol(B, 30);
70
19.7M
}
71
72
/* code */
73
#define SHA1_K0 0x5a827999
74
#define SHA1_K20 0x6ed9eba1
75
#define SHA1_K40 0x8f1bbcdc
76
#define SHA1_K60 0xca62c1d6
77
78
13.5k
#define SEED_0 0x67452301
79
13.5k
#define SEED_1 0xefcdab89
80
13.5k
#define SEED_2 0x98badcfe
81
13.5k
#define SEED_3 0x10325476
82
13.5k
#define SEED_4 0xc3d2e1f0
83
84
13.5k
void SHA1::init() {
85
13.5k
  InternalState.State[0] = SEED_0;
86
13.5k
  InternalState.State[1] = SEED_1;
87
13.5k
  InternalState.State[2] = SEED_2;
88
13.5k
  InternalState.State[3] = SEED_3;
89
13.5k
  InternalState.State[4] = SEED_4;
90
13.5k
  InternalState.ByteCount = 0;
91
13.5k
  InternalState.BufferOffset = 0;
92
13.5k
}
93
94
989k
void SHA1::hashBlock() {
95
989k
  uint32_t A = InternalState.State[0];
96
989k
  uint32_t B = InternalState.State[1];
97
989k
  uint32_t C = InternalState.State[2];
98
989k
  uint32_t D = InternalState.State[3];
99
989k
  uint32_t E = InternalState.State[4];
100
989k
101
989k
  // 4 rounds of 20 operations each. Loop unrolled.
102
989k
  r0(A, B, C, D, E, 0, InternalState.Buffer.L);
103
989k
  r0(E, A, B, C, D, 1, InternalState.Buffer.L);
104
989k
  r0(D, E, A, B, C, 2, InternalState.Buffer.L);
105
989k
  r0(C, D, E, A, B, 3, InternalState.Buffer.L);
106
989k
  r0(B, C, D, E, A, 4, InternalState.Buffer.L);
107
989k
  r0(A, B, C, D, E, 5, InternalState.Buffer.L);
108
989k
  r0(E, A, B, C, D, 6, InternalState.Buffer.L);
109
989k
  r0(D, E, A, B, C, 7, InternalState.Buffer.L);
110
989k
  r0(C, D, E, A, B, 8, InternalState.Buffer.L);
111
989k
  r0(B, C, D, E, A, 9, InternalState.Buffer.L);
112
989k
  r0(A, B, C, D, E, 10, InternalState.Buffer.L);
113
989k
  r0(E, A, B, C, D, 11, InternalState.Buffer.L);
114
989k
  r0(D, E, A, B, C, 12, InternalState.Buffer.L);
115
989k
  r0(C, D, E, A, B, 13, InternalState.Buffer.L);
116
989k
  r0(B, C, D, E, A, 14, InternalState.Buffer.L);
117
989k
  r0(A, B, C, D, E, 15, InternalState.Buffer.L);
118
989k
  r1(E, A, B, C, D, 16, InternalState.Buffer.L);
119
989k
  r1(D, E, A, B, C, 17, InternalState.Buffer.L);
120
989k
  r1(C, D, E, A, B, 18, InternalState.Buffer.L);
121
989k
  r1(B, C, D, E, A, 19, InternalState.Buffer.L);
122
989k
123
989k
  r2(A, B, C, D, E, 20, InternalState.Buffer.L);
124
989k
  r2(E, A, B, C, D, 21, InternalState.Buffer.L);
125
989k
  r2(D, E, A, B, C, 22, InternalState.Buffer.L);
126
989k
  r2(C, D, E, A, B, 23, InternalState.Buffer.L);
127
989k
  r2(B, C, D, E, A, 24, InternalState.Buffer.L);
128
989k
  r2(A, B, C, D, E, 25, InternalState.Buffer.L);
129
989k
  r2(E, A, B, C, D, 26, InternalState.Buffer.L);
130
989k
  r2(D, E, A, B, C, 27, InternalState.Buffer.L);
131
989k
  r2(C, D, E, A, B, 28, InternalState.Buffer.L);
132
989k
  r2(B, C, D, E, A, 29, InternalState.Buffer.L);
133
989k
  r2(A, B, C, D, E, 30, InternalState.Buffer.L);
134
989k
  r2(E, A, B, C, D, 31, InternalState.Buffer.L);
135
989k
  r2(D, E, A, B, C, 32, InternalState.Buffer.L);
136
989k
  r2(C, D, E, A, B, 33, InternalState.Buffer.L);
137
989k
  r2(B, C, D, E, A, 34, InternalState.Buffer.L);
138
989k
  r2(A, B, C, D, E, 35, InternalState.Buffer.L);
139
989k
  r2(E, A, B, C, D, 36, InternalState.Buffer.L);
140
989k
  r2(D, E, A, B, C, 37, InternalState.Buffer.L);
141
989k
  r2(C, D, E, A, B, 38, InternalState.Buffer.L);
142
989k
  r2(B, C, D, E, A, 39, InternalState.Buffer.L);
143
989k
144
989k
  r3(A, B, C, D, E, 40, InternalState.Buffer.L);
145
989k
  r3(E, A, B, C, D, 41, InternalState.Buffer.L);
146
989k
  r3(D, E, A, B, C, 42, InternalState.Buffer.L);
147
989k
  r3(C, D, E, A, B, 43, InternalState.Buffer.L);
148
989k
  r3(B, C, D, E, A, 44, InternalState.Buffer.L);
149
989k
  r3(A, B, C, D, E, 45, InternalState.Buffer.L);
150
989k
  r3(E, A, B, C, D, 46, InternalState.Buffer.L);
151
989k
  r3(D, E, A, B, C, 47, InternalState.Buffer.L);
152
989k
  r3(C, D, E, A, B, 48, InternalState.Buffer.L);
153
989k
  r3(B, C, D, E, A, 49, InternalState.Buffer.L);
154
989k
  r3(A, B, C, D, E, 50, InternalState.Buffer.L);
155
989k
  r3(E, A, B, C, D, 51, InternalState.Buffer.L);
156
989k
  r3(D, E, A, B, C, 52, InternalState.Buffer.L);
157
989k
  r3(C, D, E, A, B, 53, InternalState.Buffer.L);
158
989k
  r3(B, C, D, E, A, 54, InternalState.Buffer.L);
159
989k
  r3(A, B, C, D, E, 55, InternalState.Buffer.L);
160
989k
  r3(E, A, B, C, D, 56, InternalState.Buffer.L);
161
989k
  r3(D, E, A, B, C, 57, InternalState.Buffer.L);
162
989k
  r3(C, D, E, A, B, 58, InternalState.Buffer.L);
163
989k
  r3(B, C, D, E, A, 59, InternalState.Buffer.L);
164
989k
165
989k
  r4(A, B, C, D, E, 60, InternalState.Buffer.L);
166
989k
  r4(E, A, B, C, D, 61, InternalState.Buffer.L);
167
989k
  r4(D, E, A, B, C, 62, InternalState.Buffer.L);
168
989k
  r4(C, D, E, A, B, 63, InternalState.Buffer.L);
169
989k
  r4(B, C, D, E, A, 64, InternalState.Buffer.L);
170
989k
  r4(A, B, C, D, E, 65, InternalState.Buffer.L);
171
989k
  r4(E, A, B, C, D, 66, InternalState.Buffer.L);
172
989k
  r4(D, E, A, B, C, 67, InternalState.Buffer.L);
173
989k
  r4(C, D, E, A, B, 68, InternalState.Buffer.L);
174
989k
  r4(B, C, D, E, A, 69, InternalState.Buffer.L);
175
989k
  r4(A, B, C, D, E, 70, InternalState.Buffer.L);
176
989k
  r4(E, A, B, C, D, 71, InternalState.Buffer.L);
177
989k
  r4(D, E, A, B, C, 72, InternalState.Buffer.L);
178
989k
  r4(C, D, E, A, B, 73, InternalState.Buffer.L);
179
989k
  r4(B, C, D, E, A, 74, InternalState.Buffer.L);
180
989k
  r4(A, B, C, D, E, 75, InternalState.Buffer.L);
181
989k
  r4(E, A, B, C, D, 76, InternalState.Buffer.L);
182
989k
  r4(D, E, A, B, C, 77, InternalState.Buffer.L);
183
989k
  r4(C, D, E, A, B, 78, InternalState.Buffer.L);
184
989k
  r4(B, C, D, E, A, 79, InternalState.Buffer.L);
185
989k
186
989k
  InternalState.State[0] += A;
187
989k
  InternalState.State[1] += B;
188
989k
  InternalState.State[2] += C;
189
989k
  InternalState.State[3] += D;
190
989k
  InternalState.State[4] += E;
191
989k
}
192
193
63.2M
void SHA1::addUncounted(uint8_t Data) {
194
#ifdef SHA_BIG_ENDIAN
195
  InternalState.Buffer.C[InternalState.BufferOffset] = Data;
196
#else
197
  InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
198
63.2M
#endif
199
63.2M
200
63.2M
  InternalState.BufferOffset++;
201
63.2M
  if (InternalState.BufferOffset == BLOCK_LENGTH) {
202
989k
    hashBlock();
203
989k
    InternalState.BufferOffset = 0;
204
989k
  }
205
63.2M
}
206
207
63.0M
void SHA1::writebyte(uint8_t Data) {
208
63.0M
  ++InternalState.ByteCount;
209
63.0M
  addUncounted(Data);
210
63.0M
}
211
212
24.3k
void SHA1::update(ArrayRef<uint8_t> Data) {
213
24.3k
  for (auto &C : Data)
214
63.0M
    writebyte(C);
215
24.3k
}
216
217
5.12k
void SHA1::pad() {
218
5.12k
  // Implement SHA-1 padding (fips180-2 5.1.1)
219
5.12k
220
5.12k
  // Pad with 0x80 followed by 0x00 until the end of the block
221
5.12k
  addUncounted(0x80);
222
169k
  while (InternalState.BufferOffset != 56)
223
164k
    addUncounted(0x00);
224
5.12k
225
5.12k
  // Append length in the last 8 bytes
226
5.12k
  addUncounted(0); // We're only using 32 bit lengths
227
5.12k
  addUncounted(0); // But SHA-1 supports 64 bit lengths
228
5.12k
  addUncounted(0); // So zero pad the top bits
229
5.12k
  addUncounted(InternalState.ByteCount >> 29); // Shifting to multiply by 8
230
5.12k
  addUncounted(InternalState.ByteCount >>
231
5.12k
               21); // as SHA-1 supports bitstreams as well as
232
5.12k
  addUncounted(InternalState.ByteCount >> 13); // byte.
233
5.12k
  addUncounted(InternalState.ByteCount >> 5);
234
5.12k
  addUncounted(InternalState.ByteCount << 3);
235
5.12k
}
236
237
5.11k
StringRef SHA1::final() {
238
5.11k
  // Pad to complete the last block
239
5.11k
  pad();
240
5.11k
241
#ifdef SHA_BIG_ENDIAN
242
  // Just copy the current state
243
  for (int i = 0; i < 5; i++) {
244
    HashResult[i] = InternalState.State[i];
245
  }
246
#else
247
  // Swap byte order back
248
30.7k
  for (int i = 0; i < 5; 
i++25.5k
) {
249
25.5k
    HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
250
25.5k
                    (((InternalState.State[i]) << 8) & 0x00ff0000) |
251
25.5k
                    (((InternalState.State[i]) >> 8) & 0x0000ff00) |
252
25.5k
                    (((InternalState.State[i]) >> 24) & 0x000000ff);
253
25.5k
  }
254
5.11k
#endif
255
5.11k
256
5.11k
  // Return pointer to hash (20 characters)
257
5.11k
  return StringRef((char *)HashResult, HASH_LENGTH);
258
5.11k
}
259
260
1.49k
StringRef SHA1::result() {
261
1.49k
  auto StateToRestore = InternalState;
262
1.49k
263
1.49k
  auto Hash = final();
264
1.49k
265
1.49k
  // Restore the state
266
1.49k
  InternalState = StateToRestore;
267
1.49k
268
1.49k
  // Return pointer to hash (20 characters)
269
1.49k
  return Hash;
270
1.49k
}
271
272
11
std::array<uint8_t, 20> SHA1::hash(ArrayRef<uint8_t> Data) {
273
11
  SHA1 Hash;
274
11
  Hash.update(Data);
275
11
  StringRef S = Hash.final();
276
11
277
11
  std::array<uint8_t, 20> Arr;
278
11
  memcpy(Arr.data(), S.data(), S.size());
279
11
  return Arr;
280
11
}