1 //======- SHA1.h - Private copy of the SHA1 implementation ---*- C++ -* ======//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/Host.h"
17 #include "llvm/Support/SHA1.h"
18 #include "llvm/ADT/ArrayRef.h"
24 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
25 #define SHA_BIG_ENDIAN
29 #define SHA1_K0 0x5a827999
30 #define SHA1_K20 0x6ed9eba1
31 #define SHA1_K40 0x8f1bbcdc
32 #define SHA1_K60 0xca62c1d6
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
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;
50 static uint32_t rol32(uint32_t number, uint8_t bits) {
51 return ((number << bits) | (number >> (32 - bits)));
54 void SHA1::hashBlock() {
56 uint32_t a, b, c, d, e, t;
58 a = InternalState.State[0];
59 b = InternalState.State[1];
60 c = InternalState.State[2];
61 d = InternalState.State[3];
62 e = InternalState.State[4];
63 for (i = 0; i < 80; i++) {
65 t = InternalState.Buffer[(i + 13) & 15] ^
66 InternalState.Buffer[(i + 8) & 15] ^
67 InternalState.Buffer[(i + 2) & 15] ^ InternalState.Buffer[i & 15];
68 InternalState.Buffer[i & 15] = rol32(t, 1);
71 t = (d ^ (b & (c ^ d))) + SHA1_K0;
73 t = (b ^ c ^ d) + SHA1_K20;
75 t = ((b & c) | (d & (b | c))) + SHA1_K40;
77 t = (b ^ c ^ d) + SHA1_K60;
79 t += rol32(a, 5) + e + InternalState.Buffer[i & 15];
86 InternalState.State[0] += a;
87 InternalState.State[1] += b;
88 InternalState.State[2] += c;
89 InternalState.State[3] += d;
90 InternalState.State[4] += e;
93 void SHA1::addUncounted(uint8_t data) {
94 uint8_t *const b = (uint8_t *)InternalState.Buffer;
96 b[InternalState.BufferOffset] = data;
98 b[InternalState.BufferOffset ^ 3] = data;
100 InternalState.BufferOffset++;
101 if (InternalState.BufferOffset == BLOCK_LENGTH) {
103 InternalState.BufferOffset = 0;
107 void SHA1::writebyte(uint8_t data) {
108 ++InternalState.ByteCount;
112 void SHA1::update(ArrayRef<uint8_t> Data) {
118 // Implement SHA-1 padding (fips180-2 5.1.1)
120 // Pad with 0x80 followed by 0x00 until the end of the block
122 while (InternalState.BufferOffset != 56)
125 // Append length in the last 8 bytes
126 addUncounted(0); // We're only using 32 bit lengths
127 addUncounted(0); // But SHA-1 supports 64 bit lengths
128 addUncounted(0); // So zero pad the top bits
129 addUncounted(InternalState.ByteCount >> 29); // Shifting to multiply by 8
130 addUncounted(InternalState.ByteCount >>
131 21); // as SHA-1 supports bitstreams as well as
132 addUncounted(InternalState.ByteCount >> 13); // byte.
133 addUncounted(InternalState.ByteCount >> 5);
134 addUncounted(InternalState.ByteCount << 3);
137 StringRef SHA1::final() {
138 // Pad to complete the last block
141 #ifdef SHA_BIG_ENDIAN
142 // Just copy the current state
143 for (int i = 0; i < 5; i++) {
144 HashResult[i] = InternalState.State[i];
147 // Swap byte order back
148 for (int i = 0; i < 5; i++) {
149 HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
150 (((InternalState.State[i]) << 8) & 0x00ff0000) |
151 (((InternalState.State[i]) >> 8) & 0x0000ff00) |
152 (((InternalState.State[i]) >> 24) & 0x000000ff);
156 // Return pointer to hash (20 characters)
157 return StringRef((char *)HashResult, HASH_LENGTH);
160 StringRef SHA1::result() {
161 auto StateToRestore = InternalState;
166 InternalState = StateToRestore;
168 // Return pointer to hash (20 characters)