blob: 5ff2eed34c4dd78a257a9ae1fa473352295107b5 [file] [log] [blame]
Eugene Zelenko1760dc22016-04-05 20:19:49 +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//===----------------------------------------------------------------------===//
9// This code is taken from public domain
10// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
11// and modified by wrapping it in a C++ interface for LLVM,
12// and removing unnecessary code.
13//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenko1760dc22016-04-05 20:19:49 +000016#include "llvm/ADT/StringRef.h"
Mehdi Amini180441f2016-04-01 05:12:24 +000017#include "llvm/Support/Host.h"
Mehdi Amini4cd57022016-04-01 04:30:16 +000018#include "llvm/Support/SHA1.h"
Mehdi Amini4cd57022016-04-01 04:30:16 +000019
Eugene Zelenko1760dc22016-04-05 20:19:49 +000020#include <cstring>
21
22using namespace llvm;
Mehdi Amini4cd57022016-04-01 04:30:16 +000023
Mehdi Amini180441f2016-04-01 05:12:24 +000024#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
Mehdi Amini4cd57022016-04-01 04:30:16 +000025#define SHA_BIG_ENDIAN
26#endif
27
28/* code */
29#define SHA1_K0 0x5a827999
30#define SHA1_K20 0x6ed9eba1
31#define SHA1_K40 0x8f1bbcdc
32#define SHA1_K60 0xca62c1d6
33
34#define SEED_0 0x67452301
35#define SEED_1 0xefcdab89
36#define SEED_2 0x98badcfe
37#define SEED_3 0x10325476
38#define SEED_4 0xc3d2e1f0
39
40void SHA1::init() {
41 InternalState.State[0] = SEED_0;
42 InternalState.State[1] = SEED_1;
43 InternalState.State[2] = SEED_2;
44 InternalState.State[3] = SEED_3;
45 InternalState.State[4] = SEED_4;
46 InternalState.ByteCount = 0;
47 InternalState.BufferOffset = 0;
48}
49
Eugene Zelenko1760dc22016-04-05 20:19:49 +000050namespace {
51
52uint32_t rol32(uint32_t number, uint8_t bits) {
Mehdi Amini4cd57022016-04-01 04:30:16 +000053 return ((number << bits) | (number >> (32 - bits)));
54}
55
Eugene Zelenko1760dc22016-04-05 20:19:49 +000056} // end anonymous namespace
57
Mehdi Amini4cd57022016-04-01 04:30:16 +000058void SHA1::hashBlock() {
59 uint8_t i;
60 uint32_t a, b, c, d, e, t;
61
62 a = InternalState.State[0];
63 b = InternalState.State[1];
64 c = InternalState.State[2];
65 d = InternalState.State[3];
66 e = InternalState.State[4];
67 for (i = 0; i < 80; i++) {
68 if (i >= 16) {
69 t = InternalState.Buffer[(i + 13) & 15] ^
70 InternalState.Buffer[(i + 8) & 15] ^
71 InternalState.Buffer[(i + 2) & 15] ^ InternalState.Buffer[i & 15];
72 InternalState.Buffer[i & 15] = rol32(t, 1);
73 }
74 if (i < 20) {
75 t = (d ^ (b & (c ^ d))) + SHA1_K0;
76 } else if (i < 40) {
77 t = (b ^ c ^ d) + SHA1_K20;
78 } else if (i < 60) {
79 t = ((b & c) | (d & (b | c))) + SHA1_K40;
80 } else {
81 t = (b ^ c ^ d) + SHA1_K60;
82 }
83 t += rol32(a, 5) + e + InternalState.Buffer[i & 15];
84 e = d;
85 d = c;
86 c = rol32(b, 30);
87 b = a;
88 a = t;
89 }
90 InternalState.State[0] += a;
91 InternalState.State[1] += b;
92 InternalState.State[2] += c;
93 InternalState.State[3] += d;
94 InternalState.State[4] += e;
95}
96
97void SHA1::addUncounted(uint8_t data) {
98 uint8_t *const b = (uint8_t *)InternalState.Buffer;
99#ifdef SHA_BIG_ENDIAN
100 b[InternalState.BufferOffset] = data;
101#else
102 b[InternalState.BufferOffset ^ 3] = data;
103#endif
104 InternalState.BufferOffset++;
105 if (InternalState.BufferOffset == BLOCK_LENGTH) {
106 hashBlock();
107 InternalState.BufferOffset = 0;
108 }
109}
110
111void SHA1::writebyte(uint8_t data) {
112 ++InternalState.ByteCount;
113 addUncounted(data);
114}
115
116void SHA1::update(ArrayRef<uint8_t> Data) {
117 for (auto &C : Data)
118 writebyte(C);
119}
120
121void SHA1::pad() {
Mehdi Amini180441f2016-04-01 05:12:24 +0000122 // Implement SHA-1 padding (fips180-2 5.1.1)
Mehdi Amini4cd57022016-04-01 04:30:16 +0000123
124 // Pad with 0x80 followed by 0x00 until the end of the block
125 addUncounted(0x80);
126 while (InternalState.BufferOffset != 56)
127 addUncounted(0x00);
128
129 // Append length in the last 8 bytes
130 addUncounted(0); // We're only using 32 bit lengths
131 addUncounted(0); // But SHA-1 supports 64 bit lengths
132 addUncounted(0); // So zero pad the top bits
133 addUncounted(InternalState.ByteCount >> 29); // Shifting to multiply by 8
134 addUncounted(InternalState.ByteCount >>
135 21); // as SHA-1 supports bitstreams as well as
136 addUncounted(InternalState.ByteCount >> 13); // byte.
137 addUncounted(InternalState.ByteCount >> 5);
138 addUncounted(InternalState.ByteCount << 3);
139}
140
141StringRef SHA1::final() {
142 // Pad to complete the last block
143 pad();
144
145#ifdef SHA_BIG_ENDIAN
146 // Just copy the current state
147 for (int i = 0; i < 5; i++) {
148 HashResult[i] = InternalState.State[i];
149 }
150#else
151 // Swap byte order back
152 for (int i = 0; i < 5; i++) {
153 HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
154 (((InternalState.State[i]) << 8) & 0x00ff0000) |
155 (((InternalState.State[i]) >> 8) & 0x0000ff00) |
156 (((InternalState.State[i]) >> 24) & 0x000000ff);
157 }
158#endif
159
160 // Return pointer to hash (20 characters)
161 return StringRef((char *)HashResult, HASH_LENGTH);
162}
163
164StringRef SHA1::result() {
165 auto StateToRestore = InternalState;
166
167 auto Hash = final();
168
169 // Restore the state
170 InternalState = StateToRestore;
171
172 // Return pointer to hash (20 characters)
173 return Hash;
174}