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