blob: 0998af7c951351befd7066369cd3267085ea07b2 [file] [log] [blame]
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00001//======- SHA1.h - Private copy of the SHA1 implementation ---*- C++ -* ======//
Mehdi Amini4cd57022016-04-01 04:30:16 +00002//
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//===----------------------------------------------------------------------===//
Rui Ueyamafe336612016-11-20 01:03:22 +00009//
Mehdi Amini4cd57022016-04-01 04:30:16 +000010// This code is taken from public domain
Rui Ueyamafe336612016-11-20 01:03:22 +000011// (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)
Mehdi Amini4cd57022016-04-01 04:30:16 +000013// and modified by wrapping it in a C++ interface for LLVM,
14// and removing unnecessary code.
15//
16//===----------------------------------------------------------------------===//
17
Mehdi Amini180441f2016-04-01 05:12:24 +000018#include "llvm/Support/Host.h"
Mehdi Amini4cd57022016-04-01 04:30:16 +000019#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000020#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko1760dc22016-04-05 20:19:49 +000021using namespace llvm;
Mehdi Amini4cd57022016-04-01 04:30:16 +000022
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023#include <stdint.h>
24#include <string.h>
25
Mehdi Amini180441f2016-04-01 05:12:24 +000026#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
Mehdi Amini4cd57022016-04-01 04:30:16 +000027#define SHA_BIG_ENDIAN
28#endif
29
Rui Ueyamafe336612016-11-20 01:03:22 +000030static uint32_t rol(uint32_t number, int bits) {
31 return (number << bits) | (number >> (32 - bits));
Rui Ueyama218072a92016-11-20 01:13:22 +000032}
Rui Ueyamafe336612016-11-20 01:03:22 +000033
Rui Ueyama218072a92016-11-20 01:13:22 +000034#ifdef SHA_BIG_ENDIAN
Rui Ueyamafe336612016-11-20 01:03:22 +000035static uint32_t blk0(uint32_t *Buf, int I) {
36 Buf[I] = (rol(Buf[I], 24) & 0xFF00FF00) | (rol(Buf[I], 8) & 0x00FF00FF);
37 return Buf[I];
38}
39#else
40static uint32_t blk0(uint32_t *Buf, int I) { return Buf[I]; }
41#endif
42
43static uint32_t blk(uint32_t *Buf, int I) {
44 Buf[I & 15] = rol(Buf[(I + 13) & 15] ^ Buf[(I + 8) & 15] ^ Buf[(I + 2) & 15] ^
45 Buf[I & 15],
46 1);
47 return Buf[I & 15];
48}
49
50static void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
51 int I, uint32_t *Buf) {
52 E += ((B & (C ^ D)) ^ D) + blk0(Buf, I) + 0x5A827999 + rol(A, 5);
53 B = rol(B, 30);
54}
55
56static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
57 int I, uint32_t *Buf) {
58 E += ((B & (C ^ D)) ^ D) + blk(Buf, I) + 0x5A827999 + rol(A, 5);
59 B = rol(B, 30);
60}
61
62static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
63 int I, uint32_t *Buf) {
64 E += (B ^ C ^ D) + blk(Buf, I) + 0x6ED9EBA1 + rol(A, 5);
65 B = rol(B, 30);
66}
67
68static void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
69 int I, uint32_t *Buf) {
70 E += (((B | C) & D) | (B & C)) + blk(Buf, I) + 0x8F1BBCDC + rol(A, 5);
71 B = rol(B, 30);
72}
73
74static void r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,
75 int I, uint32_t *Buf) {
76 E += (B ^ C ^ D) + blk(Buf, I) + 0xCA62C1D6 + rol(A, 5);
77 B = rol(B, 30);
78}
79
Mehdi Amini4cd57022016-04-01 04:30:16 +000080/* code */
81#define SHA1_K0 0x5a827999
82#define SHA1_K20 0x6ed9eba1
83#define SHA1_K40 0x8f1bbcdc
84#define SHA1_K60 0xca62c1d6
85
86#define SEED_0 0x67452301
87#define SEED_1 0xefcdab89
88#define SEED_2 0x98badcfe
89#define SEED_3 0x10325476
90#define SEED_4 0xc3d2e1f0
91
92void SHA1::init() {
93 InternalState.State[0] = SEED_0;
94 InternalState.State[1] = SEED_1;
95 InternalState.State[2] = SEED_2;
96 InternalState.State[3] = SEED_3;
97 InternalState.State[4] = SEED_4;
98 InternalState.ByteCount = 0;
99 InternalState.BufferOffset = 0;
100}
101
Mehdi Amini4cd57022016-04-01 04:30:16 +0000102void SHA1::hashBlock() {
Rui Ueyamafe336612016-11-20 01:03:22 +0000103 uint32_t A = InternalState.State[0];
104 uint32_t B = InternalState.State[1];
105 uint32_t C = InternalState.State[2];
106 uint32_t D = InternalState.State[3];
107 uint32_t E = InternalState.State[4];
Mehdi Amini4cd57022016-04-01 04:30:16 +0000108
Rui Ueyamafe336612016-11-20 01:03:22 +0000109 // 4 rounds of 20 operations each. Loop unrolled.
110 r0(A, B, C, D, E, 0, InternalState.Buffer.L);
111 r0(E, A, B, C, D, 1, InternalState.Buffer.L);
112 r0(D, E, A, B, C, 2, InternalState.Buffer.L);
113 r0(C, D, E, A, B, 3, InternalState.Buffer.L);
114 r0(B, C, D, E, A, 4, InternalState.Buffer.L);
115 r0(A, B, C, D, E, 5, InternalState.Buffer.L);
116 r0(E, A, B, C, D, 6, InternalState.Buffer.L);
117 r0(D, E, A, B, C, 7, InternalState.Buffer.L);
118 r0(C, D, E, A, B, 8, InternalState.Buffer.L);
119 r0(B, C, D, E, A, 9, InternalState.Buffer.L);
120 r0(A, B, C, D, E, 10, InternalState.Buffer.L);
121 r0(E, A, B, C, D, 11, InternalState.Buffer.L);
122 r0(D, E, A, B, C, 12, InternalState.Buffer.L);
123 r0(C, D, E, A, B, 13, InternalState.Buffer.L);
124 r0(B, C, D, E, A, 14, InternalState.Buffer.L);
125 r0(A, B, C, D, E, 15, InternalState.Buffer.L);
126 r1(E, A, B, C, D, 16, InternalState.Buffer.L);
127 r1(D, E, A, B, C, 17, InternalState.Buffer.L);
128 r1(C, D, E, A, B, 18, InternalState.Buffer.L);
129 r1(B, C, D, E, A, 19, InternalState.Buffer.L);
130
131 r2(A, B, C, D, E, 20, InternalState.Buffer.L);
132 r2(E, A, B, C, D, 21, InternalState.Buffer.L);
133 r2(D, E, A, B, C, 22, InternalState.Buffer.L);
134 r2(C, D, E, A, B, 23, InternalState.Buffer.L);
135 r2(B, C, D, E, A, 24, InternalState.Buffer.L);
136 r2(A, B, C, D, E, 25, InternalState.Buffer.L);
137 r2(E, A, B, C, D, 26, InternalState.Buffer.L);
138 r2(D, E, A, B, C, 27, InternalState.Buffer.L);
139 r2(C, D, E, A, B, 28, InternalState.Buffer.L);
140 r2(B, C, D, E, A, 29, InternalState.Buffer.L);
141 r2(A, B, C, D, E, 30, InternalState.Buffer.L);
142 r2(E, A, B, C, D, 31, InternalState.Buffer.L);
143 r2(D, E, A, B, C, 32, InternalState.Buffer.L);
144 r2(C, D, E, A, B, 33, InternalState.Buffer.L);
145 r2(B, C, D, E, A, 34, InternalState.Buffer.L);
146 r2(A, B, C, D, E, 35, InternalState.Buffer.L);
147 r2(E, A, B, C, D, 36, InternalState.Buffer.L);
148 r2(D, E, A, B, C, 37, InternalState.Buffer.L);
149 r2(C, D, E, A, B, 38, InternalState.Buffer.L);
150 r2(B, C, D, E, A, 39, InternalState.Buffer.L);
151
152 r3(A, B, C, D, E, 40, InternalState.Buffer.L);
153 r3(E, A, B, C, D, 41, InternalState.Buffer.L);
154 r3(D, E, A, B, C, 42, InternalState.Buffer.L);
155 r3(C, D, E, A, B, 43, InternalState.Buffer.L);
156 r3(B, C, D, E, A, 44, InternalState.Buffer.L);
157 r3(A, B, C, D, E, 45, InternalState.Buffer.L);
158 r3(E, A, B, C, D, 46, InternalState.Buffer.L);
159 r3(D, E, A, B, C, 47, InternalState.Buffer.L);
160 r3(C, D, E, A, B, 48, InternalState.Buffer.L);
161 r3(B, C, D, E, A, 49, InternalState.Buffer.L);
162 r3(A, B, C, D, E, 50, InternalState.Buffer.L);
163 r3(E, A, B, C, D, 51, InternalState.Buffer.L);
164 r3(D, E, A, B, C, 52, InternalState.Buffer.L);
165 r3(C, D, E, A, B, 53, InternalState.Buffer.L);
166 r3(B, C, D, E, A, 54, InternalState.Buffer.L);
167 r3(A, B, C, D, E, 55, InternalState.Buffer.L);
168 r3(E, A, B, C, D, 56, InternalState.Buffer.L);
169 r3(D, E, A, B, C, 57, InternalState.Buffer.L);
170 r3(C, D, E, A, B, 58, InternalState.Buffer.L);
171 r3(B, C, D, E, A, 59, InternalState.Buffer.L);
172
173 r4(A, B, C, D, E, 60, InternalState.Buffer.L);
174 r4(E, A, B, C, D, 61, InternalState.Buffer.L);
175 r4(D, E, A, B, C, 62, InternalState.Buffer.L);
176 r4(C, D, E, A, B, 63, InternalState.Buffer.L);
177 r4(B, C, D, E, A, 64, InternalState.Buffer.L);
178 r4(A, B, C, D, E, 65, InternalState.Buffer.L);
179 r4(E, A, B, C, D, 66, InternalState.Buffer.L);
180 r4(D, E, A, B, C, 67, InternalState.Buffer.L);
181 r4(C, D, E, A, B, 68, InternalState.Buffer.L);
182 r4(B, C, D, E, A, 69, InternalState.Buffer.L);
183 r4(A, B, C, D, E, 70, InternalState.Buffer.L);
184 r4(E, A, B, C, D, 71, InternalState.Buffer.L);
185 r4(D, E, A, B, C, 72, InternalState.Buffer.L);
186 r4(C, D, E, A, B, 73, InternalState.Buffer.L);
187 r4(B, C, D, E, A, 74, InternalState.Buffer.L);
188 r4(A, B, C, D, E, 75, InternalState.Buffer.L);
189 r4(E, A, B, C, D, 76, InternalState.Buffer.L);
190 r4(D, E, A, B, C, 77, InternalState.Buffer.L);
191 r4(C, D, E, A, B, 78, InternalState.Buffer.L);
192 r4(B, C, D, E, A, 79, InternalState.Buffer.L);
193
194 InternalState.State[0] += A;
195 InternalState.State[1] += B;
196 InternalState.State[2] += C;
197 InternalState.State[3] += D;
198 InternalState.State[4] += E;
Mehdi Amini4cd57022016-04-01 04:30:16 +0000199}
200
201void SHA1::addUncounted(uint8_t data) {
Mehdi Amini4cd57022016-04-01 04:30:16 +0000202#ifdef SHA_BIG_ENDIAN
Rui Ueyamafe336612016-11-20 01:03:22 +0000203 InternalState.Buffer.C[InternalState.BufferOffset] = data;
Mehdi Amini4cd57022016-04-01 04:30:16 +0000204#else
Rui Ueyamafe336612016-11-20 01:03:22 +0000205 InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = data;
Mehdi Amini4cd57022016-04-01 04:30:16 +0000206#endif
Rui Ueyamafe336612016-11-20 01:03:22 +0000207
Mehdi Amini4cd57022016-04-01 04:30:16 +0000208 InternalState.BufferOffset++;
209 if (InternalState.BufferOffset == BLOCK_LENGTH) {
210 hashBlock();
211 InternalState.BufferOffset = 0;
212 }
213}
214
215void SHA1::writebyte(uint8_t data) {
216 ++InternalState.ByteCount;
217 addUncounted(data);
218}
219
220void SHA1::update(ArrayRef<uint8_t> Data) {
221 for (auto &C : Data)
222 writebyte(C);
223}
224
225void SHA1::pad() {
Mehdi Amini180441f2016-04-01 05:12:24 +0000226 // Implement SHA-1 padding (fips180-2 5.1.1)
Mehdi Amini4cd57022016-04-01 04:30:16 +0000227
228 // Pad with 0x80 followed by 0x00 until the end of the block
229 addUncounted(0x80);
230 while (InternalState.BufferOffset != 56)
231 addUncounted(0x00);
232
233 // Append length in the last 8 bytes
234 addUncounted(0); // We're only using 32 bit lengths
235 addUncounted(0); // But SHA-1 supports 64 bit lengths
236 addUncounted(0); // So zero pad the top bits
237 addUncounted(InternalState.ByteCount >> 29); // Shifting to multiply by 8
238 addUncounted(InternalState.ByteCount >>
239 21); // as SHA-1 supports bitstreams as well as
240 addUncounted(InternalState.ByteCount >> 13); // byte.
241 addUncounted(InternalState.ByteCount >> 5);
242 addUncounted(InternalState.ByteCount << 3);
243}
244
245StringRef SHA1::final() {
246 // Pad to complete the last block
247 pad();
248
249#ifdef SHA_BIG_ENDIAN
250 // Just copy the current state
251 for (int i = 0; i < 5; i++) {
252 HashResult[i] = InternalState.State[i];
253 }
254#else
255 // Swap byte order back
256 for (int i = 0; i < 5; i++) {
257 HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
258 (((InternalState.State[i]) << 8) & 0x00ff0000) |
259 (((InternalState.State[i]) >> 8) & 0x0000ff00) |
260 (((InternalState.State[i]) >> 24) & 0x000000ff);
261 }
262#endif
263
264 // Return pointer to hash (20 characters)
265 return StringRef((char *)HashResult, HASH_LENGTH);
266}
267
268StringRef SHA1::result() {
269 auto StateToRestore = InternalState;
270
271 auto Hash = final();
272
273 // Restore the state
274 InternalState = StateToRestore;
275
276 // Return pointer to hash (20 characters)
277 return Hash;
278}