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