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