Bitcoin ABC  0.29.2
P2P Digital Currency
sha1.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <crypto/sha1.h>
6 
7 #include <crypto/common.h>
8 
9 #include <cstring>
10 
11 // Internal implementation code.
12 namespace {
14 namespace sha1 {
16  inline void Round(uint32_t a, uint32_t &b, uint32_t c, uint32_t d,
17  uint32_t &e, uint32_t f, uint32_t k, uint32_t w) {
18  e += ((a << 5) | (a >> 27)) + f + k + w;
19  b = (b << 30) | (b >> 2);
20  }
21 
22  inline uint32_t f1(uint32_t b, uint32_t c, uint32_t d) {
23  return d ^ (b & (c ^ d));
24  }
25  inline uint32_t f2(uint32_t b, uint32_t c, uint32_t d) { return b ^ c ^ d; }
26  inline uint32_t f3(uint32_t b, uint32_t c, uint32_t d) {
27  return (b & c) | (d & (b | c));
28  }
29 
30  inline uint32_t left(uint32_t x) { return (x << 1) | (x >> 31); }
31 
33  inline void Initialize(uint32_t *s) {
34  s[0] = 0x67452301ul;
35  s[1] = 0xEFCDAB89ul;
36  s[2] = 0x98BADCFEul;
37  s[3] = 0x10325476ul;
38  s[4] = 0xC3D2E1F0ul;
39  }
40 
41  const uint32_t k1 = 0x5A827999ul;
42  const uint32_t k2 = 0x6ED9EBA1ul;
43  const uint32_t k3 = 0x8F1BBCDCul;
44  const uint32_t k4 = 0xCA62C1D6ul;
45 
47  void Transform(uint32_t *s, const uint8_t *chunk) {
48  uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4];
49  uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13,
50  w14, w15;
51 
52  Round(a, b, c, d, e, f1(b, c, d), k1, w0 = ReadBE32(chunk + 0));
53  Round(e, a, b, c, d, f1(a, b, c), k1, w1 = ReadBE32(chunk + 4));
54  Round(d, e, a, b, c, f1(e, a, b), k1, w2 = ReadBE32(chunk + 8));
55  Round(c, d, e, a, b, f1(d, e, a), k1, w3 = ReadBE32(chunk + 12));
56  Round(b, c, d, e, a, f1(c, d, e), k1, w4 = ReadBE32(chunk + 16));
57  Round(a, b, c, d, e, f1(b, c, d), k1, w5 = ReadBE32(chunk + 20));
58  Round(e, a, b, c, d, f1(a, b, c), k1, w6 = ReadBE32(chunk + 24));
59  Round(d, e, a, b, c, f1(e, a, b), k1, w7 = ReadBE32(chunk + 28));
60  Round(c, d, e, a, b, f1(d, e, a), k1, w8 = ReadBE32(chunk + 32));
61  Round(b, c, d, e, a, f1(c, d, e), k1, w9 = ReadBE32(chunk + 36));
62  Round(a, b, c, d, e, f1(b, c, d), k1, w10 = ReadBE32(chunk + 40));
63  Round(e, a, b, c, d, f1(a, b, c), k1, w11 = ReadBE32(chunk + 44));
64  Round(d, e, a, b, c, f1(e, a, b), k1, w12 = ReadBE32(chunk + 48));
65  Round(c, d, e, a, b, f1(d, e, a), k1, w13 = ReadBE32(chunk + 52));
66  Round(b, c, d, e, a, f1(c, d, e), k1, w14 = ReadBE32(chunk + 56));
67  Round(a, b, c, d, e, f1(b, c, d), k1, w15 = ReadBE32(chunk + 60));
68 
69  Round(e, a, b, c, d, f1(a, b, c), k1, w0 = left(w0 ^ w13 ^ w8 ^ w2));
70  Round(d, e, a, b, c, f1(e, a, b), k1, w1 = left(w1 ^ w14 ^ w9 ^ w3));
71  Round(c, d, e, a, b, f1(d, e, a), k1, w2 = left(w2 ^ w15 ^ w10 ^ w4));
72  Round(b, c, d, e, a, f1(c, d, e), k1, w3 = left(w3 ^ w0 ^ w11 ^ w5));
73  Round(a, b, c, d, e, f2(b, c, d), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6));
74  Round(e, a, b, c, d, f2(a, b, c), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7));
75  Round(d, e, a, b, c, f2(e, a, b), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8));
76  Round(c, d, e, a, b, f2(d, e, a), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9));
77  Round(b, c, d, e, a, f2(c, d, e), k2, w8 = left(w8 ^ w5 ^ w0 ^ w10));
78  Round(a, b, c, d, e, f2(b, c, d), k2, w9 = left(w9 ^ w6 ^ w1 ^ w11));
79  Round(e, a, b, c, d, f2(a, b, c), k2, w10 = left(w10 ^ w7 ^ w2 ^ w12));
80  Round(d, e, a, b, c, f2(e, a, b), k2, w11 = left(w11 ^ w8 ^ w3 ^ w13));
81  Round(c, d, e, a, b, f2(d, e, a), k2, w12 = left(w12 ^ w9 ^ w4 ^ w14));
82  Round(b, c, d, e, a, f2(c, d, e), k2, w13 = left(w13 ^ w10 ^ w5 ^ w15));
83  Round(a, b, c, d, e, f2(b, c, d), k2, w14 = left(w14 ^ w11 ^ w6 ^ w0));
84  Round(e, a, b, c, d, f2(a, b, c), k2, w15 = left(w15 ^ w12 ^ w7 ^ w1));
85 
86  Round(d, e, a, b, c, f2(e, a, b), k2, w0 = left(w0 ^ w13 ^ w8 ^ w2));
87  Round(c, d, e, a, b, f2(d, e, a), k2, w1 = left(w1 ^ w14 ^ w9 ^ w3));
88  Round(b, c, d, e, a, f2(c, d, e), k2, w2 = left(w2 ^ w15 ^ w10 ^ w4));
89  Round(a, b, c, d, e, f2(b, c, d), k2, w3 = left(w3 ^ w0 ^ w11 ^ w5));
90  Round(e, a, b, c, d, f2(a, b, c), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6));
91  Round(d, e, a, b, c, f2(e, a, b), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7));
92  Round(c, d, e, a, b, f2(d, e, a), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8));
93  Round(b, c, d, e, a, f2(c, d, e), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9));
94  Round(a, b, c, d, e, f3(b, c, d), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10));
95  Round(e, a, b, c, d, f3(a, b, c), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11));
96  Round(d, e, a, b, c, f3(e, a, b), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12));
97  Round(c, d, e, a, b, f3(d, e, a), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13));
98  Round(b, c, d, e, a, f3(c, d, e), k3, w12 = left(w12 ^ w9 ^ w4 ^ w14));
99  Round(a, b, c, d, e, f3(b, c, d), k3, w13 = left(w13 ^ w10 ^ w5 ^ w15));
100  Round(e, a, b, c, d, f3(a, b, c), k3, w14 = left(w14 ^ w11 ^ w6 ^ w0));
101  Round(d, e, a, b, c, f3(e, a, b), k3, w15 = left(w15 ^ w12 ^ w7 ^ w1));
102 
103  Round(c, d, e, a, b, f3(d, e, a), k3, w0 = left(w0 ^ w13 ^ w8 ^ w2));
104  Round(b, c, d, e, a, f3(c, d, e), k3, w1 = left(w1 ^ w14 ^ w9 ^ w3));
105  Round(a, b, c, d, e, f3(b, c, d), k3, w2 = left(w2 ^ w15 ^ w10 ^ w4));
106  Round(e, a, b, c, d, f3(a, b, c), k3, w3 = left(w3 ^ w0 ^ w11 ^ w5));
107  Round(d, e, a, b, c, f3(e, a, b), k3, w4 = left(w4 ^ w1 ^ w12 ^ w6));
108  Round(c, d, e, a, b, f3(d, e, a), k3, w5 = left(w5 ^ w2 ^ w13 ^ w7));
109  Round(b, c, d, e, a, f3(c, d, e), k3, w6 = left(w6 ^ w3 ^ w14 ^ w8));
110  Round(a, b, c, d, e, f3(b, c, d), k3, w7 = left(w7 ^ w4 ^ w15 ^ w9));
111  Round(e, a, b, c, d, f3(a, b, c), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10));
112  Round(d, e, a, b, c, f3(e, a, b), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11));
113  Round(c, d, e, a, b, f3(d, e, a), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12));
114  Round(b, c, d, e, a, f3(c, d, e), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13));
115  Round(a, b, c, d, e, f2(b, c, d), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14));
116  Round(e, a, b, c, d, f2(a, b, c), k4, w13 = left(w13 ^ w10 ^ w5 ^ w15));
117  Round(d, e, a, b, c, f2(e, a, b), k4, w14 = left(w14 ^ w11 ^ w6 ^ w0));
118  Round(c, d, e, a, b, f2(d, e, a), k4, w15 = left(w15 ^ w12 ^ w7 ^ w1));
119 
120  Round(b, c, d, e, a, f2(c, d, e), k4, w0 = left(w0 ^ w13 ^ w8 ^ w2));
121  Round(a, b, c, d, e, f2(b, c, d), k4, w1 = left(w1 ^ w14 ^ w9 ^ w3));
122  Round(e, a, b, c, d, f2(a, b, c), k4, w2 = left(w2 ^ w15 ^ w10 ^ w4));
123  Round(d, e, a, b, c, f2(e, a, b), k4, w3 = left(w3 ^ w0 ^ w11 ^ w5));
124  Round(c, d, e, a, b, f2(d, e, a), k4, w4 = left(w4 ^ w1 ^ w12 ^ w6));
125  Round(b, c, d, e, a, f2(c, d, e), k4, w5 = left(w5 ^ w2 ^ w13 ^ w7));
126  Round(a, b, c, d, e, f2(b, c, d), k4, w6 = left(w6 ^ w3 ^ w14 ^ w8));
127  Round(e, a, b, c, d, f2(a, b, c), k4, w7 = left(w7 ^ w4 ^ w15 ^ w9));
128  Round(d, e, a, b, c, f2(e, a, b), k4, w8 = left(w8 ^ w5 ^ w0 ^ w10));
129  Round(c, d, e, a, b, f2(d, e, a), k4, w9 = left(w9 ^ w6 ^ w1 ^ w11));
130  Round(b, c, d, e, a, f2(c, d, e), k4, w10 = left(w10 ^ w7 ^ w2 ^ w12));
131  Round(a, b, c, d, e, f2(b, c, d), k4, w11 = left(w11 ^ w8 ^ w3 ^ w13));
132  Round(e, a, b, c, d, f2(a, b, c), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14));
133  Round(d, e, a, b, c, f2(e, a, b), k4, left(w13 ^ w10 ^ w5 ^ w15));
134  Round(c, d, e, a, b, f2(d, e, a), k4, left(w14 ^ w11 ^ w6 ^ w0));
135  Round(b, c, d, e, a, f2(c, d, e), k4, left(w15 ^ w12 ^ w7 ^ w1));
136 
137  s[0] += a;
138  s[1] += b;
139  s[2] += c;
140  s[3] += d;
141  s[4] += e;
142  }
143 
144 } // namespace sha1
145 
146 } // namespace
147 
149 
150 CSHA1::CSHA1() : bytes(0) {
151  sha1::Initialize(s);
152 }
153 
154 CSHA1 &CSHA1::Write(const uint8_t *data, size_t len) {
155  const uint8_t *end = data + len;
156  size_t bufsize = bytes % 64;
157  if (bufsize && bufsize + len >= 64) {
158  // Fill the buffer, and process it.
159  memcpy(buf + bufsize, data, 64 - bufsize);
160  bytes += 64 - bufsize;
161  data += 64 - bufsize;
163  bufsize = 0;
164  }
165  while (end - data >= 64) {
166  // Process full chunks directly from the source.
167  sha1::Transform(s, data);
168  bytes += 64;
169  data += 64;
170  }
171  if (end > data) {
172  // Fill the buffer with what remains.
173  memcpy(buf + bufsize, data, end - data);
174  bytes += end - data;
175  }
176  return *this;
177 }
178 
179 void CSHA1::Finalize(uint8_t hash[OUTPUT_SIZE]) {
180  static const uint8_t pad[64] = {0x80};
181  uint8_t sizedesc[8];
182  WriteBE64(sizedesc, bytes << 3);
183  Write(pad, 1 + ((119 - (bytes % 64)) % 64));
184  Write(sizedesc, 8);
185  WriteBE32(hash, s[0]);
186  WriteBE32(hash + 4, s[1]);
187  WriteBE32(hash + 8, s[2]);
188  WriteBE32(hash + 12, s[3]);
189  WriteBE32(hash + 16, s[4]);
190 }
191 
193  bytes = 0;
194  sha1::Initialize(s);
195  return *this;
196 }
static const uint8_t k1[32]
static const uint8_t k2[32]
A hasher class for SHA1.
Definition: sha1.h:12
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha1.cpp:179
uint8_t buf[64]
Definition: sha1.h:15
CSHA1 & Write(const uint8_t *data, size_t len)
Definition: sha1.cpp:154
CSHA1 & Reset()
Definition: sha1.cpp:192
CSHA1()
Definition: sha1.cpp:150
uint64_t bytes
Definition: sha1.h:16
uint32_t s[5]
Definition: sha1.h:14
static void WriteBE64(uint8_t *ptr, uint64_t x)
Definition: common.h:73
static void WriteBE32(uint8_t *ptr, uint32_t x)
Definition: common.h:68
static uint32_t ReadBE32(const uint8_t *ptr)
Definition: common.h:56
#define Round(a, b, c, d, e, f, g, h, k, w)
Definition: hash_impl.h:24
Internal SHA-1 implementation.
Definition: sha1.cpp:14
void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks)