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);
22 inline uint32_t f1(uint32_t b, uint32_t c, uint32_t d) {
23 return d ^ (b & (c ^ d));
25 inline uint32_t f2(uint32_t b, uint32_t c, uint32_t d) {
28 inline uint32_t f3(uint32_t b, uint32_t c, uint32_t d) {
29 return (b & c) | (d & (b | c));
32 inline uint32_t left(uint32_t x) {
33 return (x << 1) | (x >> 31);
37 inline void Initialize(uint32_t *s) {
45 const uint32_t
k1 = 0x5A827999ul;
46 const uint32_t
k2 = 0x6ED9EBA1ul;
47 const uint32_t k3 = 0x8F1BBCDCul;
48 const uint32_t k4 = 0xCA62C1D6ul;
51 void Transform(uint32_t *s,
const uint8_t *chunk) {
52 uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4];
53 uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13,
73 Round(e, a, b, c, d, f1(a, b, c),
k1, w0 = left(w0 ^ w13 ^ w8 ^ w2));
74 Round(d, e, a, b, c, f1(e, a, b),
k1, w1 = left(w1 ^ w14 ^ w9 ^ w3));
75 Round(c, d, e, a, b, f1(d, e, a),
k1, w2 = left(w2 ^ w15 ^ w10 ^ w4));
76 Round(b, c, d, e, a, f1(c, d, e),
k1, w3 = left(w3 ^ w0 ^ w11 ^ w5));
77 Round(a, b, c, d, e, f2(b, c, d),
k2, w4 = left(w4 ^ w1 ^ w12 ^ w6));
78 Round(e, a, b, c, d, f2(a, b, c),
k2, w5 = left(w5 ^ w2 ^ w13 ^ w7));
79 Round(d, e, a, b, c, f2(e, a, b),
k2, w6 = left(w6 ^ w3 ^ w14 ^ w8));
80 Round(c, d, e, a, b, f2(d, e, a),
k2, w7 = left(w7 ^ w4 ^ w15 ^ w9));
81 Round(b, c, d, e, a, f2(c, d, e),
k2, w8 = left(w8 ^ w5 ^ w0 ^ w10));
82 Round(a, b, c, d, e, f2(b, c, d),
k2, w9 = left(w9 ^ w6 ^ w1 ^ w11));
83 Round(e, a, b, c, d, f2(a, b, c),
k2, w10 = left(w10 ^ w7 ^ w2 ^ w12));
84 Round(d, e, a, b, c, f2(e, a, b),
k2, w11 = left(w11 ^ w8 ^ w3 ^ w13));
85 Round(c, d, e, a, b, f2(d, e, a),
k2, w12 = left(w12 ^ w9 ^ w4 ^ w14));
86 Round(b, c, d, e, a, f2(c, d, e),
k2, w13 = left(w13 ^ w10 ^ w5 ^ w15));
87 Round(a, b, c, d, e, f2(b, c, d),
k2, w14 = left(w14 ^ w11 ^ w6 ^ w0));
88 Round(e, a, b, c, d, f2(a, b, c),
k2, w15 = left(w15 ^ w12 ^ w7 ^ w1));
90 Round(d, e, a, b, c, f2(e, a, b),
k2, w0 = left(w0 ^ w13 ^ w8 ^ w2));
91 Round(c, d, e, a, b, f2(d, e, a),
k2, w1 = left(w1 ^ w14 ^ w9 ^ w3));
92 Round(b, c, d, e, a, f2(c, d, e),
k2, w2 = left(w2 ^ w15 ^ w10 ^ w4));
93 Round(a, b, c, d, e, f2(b, c, d),
k2, w3 = left(w3 ^ w0 ^ w11 ^ w5));
94 Round(e, a, b, c, d, f2(a, b, c),
k2, w4 = left(w4 ^ w1 ^ w12 ^ w6));
95 Round(d, e, a, b, c, f2(e, a, b),
k2, w5 = left(w5 ^ w2 ^ w13 ^ w7));
96 Round(c, d, e, a, b, f2(d, e, a),
k2, w6 = left(w6 ^ w3 ^ w14 ^ w8));
97 Round(b, c, d, e, a, f2(c, d, e),
k2, w7 = left(w7 ^ w4 ^ w15 ^ w9));
98 Round(a, b, c, d, e, f3(b, c, d), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10));
99 Round(e, a, b, c, d, f3(a, b, c), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11));
100 Round(d, e, a, b, c, f3(e, a, b), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12));
101 Round(c, d, e, a, b, f3(d, e, a), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13));
102 Round(b, c, d, e, a, f3(c, d, e), k3, w12 = left(w12 ^ w9 ^ w4 ^ w14));
103 Round(a, b, c, d, e, f3(b, c, d), k3, w13 = left(w13 ^ w10 ^ w5 ^ w15));
104 Round(e, a, b, c, d, f3(a, b, c), k3, w14 = left(w14 ^ w11 ^ w6 ^ w0));
105 Round(d, e, a, b, c, f3(e, a, b), k3, w15 = left(w15 ^ w12 ^ w7 ^ w1));
107 Round(c, d, e, a, b, f3(d, e, a), k3, w0 = left(w0 ^ w13 ^ w8 ^ w2));
108 Round(b, c, d, e, a, f3(c, d, e), k3, w1 = left(w1 ^ w14 ^ w9 ^ w3));
109 Round(a, b, c, d, e, f3(b, c, d), k3, w2 = left(w2 ^ w15 ^ w10 ^ w4));
110 Round(e, a, b, c, d, f3(a, b, c), k3, w3 = left(w3 ^ w0 ^ w11 ^ w5));
111 Round(d, e, a, b, c, f3(e, a, b), k3, w4 = left(w4 ^ w1 ^ w12 ^ w6));
112 Round(c, d, e, a, b, f3(d, e, a), k3, w5 = left(w5 ^ w2 ^ w13 ^ w7));
113 Round(b, c, d, e, a, f3(c, d, e), k3, w6 = left(w6 ^ w3 ^ w14 ^ w8));
114 Round(a, b, c, d, e, f3(b, c, d), k3, w7 = left(w7 ^ w4 ^ w15 ^ w9));
115 Round(e, a, b, c, d, f3(a, b, c), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10));
116 Round(d, e, a, b, c, f3(e, a, b), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11));
117 Round(c, d, e, a, b, f3(d, e, a), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12));
118 Round(b, c, d, e, a, f3(c, d, e), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13));
119 Round(a, b, c, d, e, f2(b, c, d), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14));
120 Round(e, a, b, c, d, f2(a, b, c), k4, w13 = left(w13 ^ w10 ^ w5 ^ w15));
121 Round(d, e, a, b, c, f2(e, a, b), k4, w14 = left(w14 ^ w11 ^ w6 ^ w0));
122 Round(c, d, e, a, b, f2(d, e, a), k4, w15 = left(w15 ^ w12 ^ w7 ^ w1));
124 Round(b, c, d, e, a, f2(c, d, e), k4, w0 = left(w0 ^ w13 ^ w8 ^ w2));
125 Round(a, b, c, d, e, f2(b, c, d), k4, w1 = left(w1 ^ w14 ^ w9 ^ w3));
126 Round(e, a, b, c, d, f2(a, b, c), k4, w2 = left(w2 ^ w15 ^ w10 ^ w4));
127 Round(d, e, a, b, c, f2(e, a, b), k4, w3 = left(w3 ^ w0 ^ w11 ^ w5));
128 Round(c, d, e, a, b, f2(d, e, a), k4, w4 = left(w4 ^ w1 ^ w12 ^ w6));
129 Round(b, c, d, e, a, f2(c, d, e), k4, w5 = left(w5 ^ w2 ^ w13 ^ w7));
130 Round(a, b, c, d, e, f2(b, c, d), k4, w6 = left(w6 ^ w3 ^ w14 ^ w8));
131 Round(e, a, b, c, d, f2(a, b, c), k4, w7 = left(w7 ^ w4 ^ w15 ^ w9));
132 Round(d, e, a, b, c, f2(e, a, b), k4, w8 = left(w8 ^ w5 ^ w0 ^ w10));
133 Round(c, d, e, a, b, f2(d, e, a), k4, w9 = left(w9 ^ w6 ^ w1 ^ w11));
134 Round(b, c, d, e, a, f2(c, d, e), k4, w10 = left(w10 ^ w7 ^ w2 ^ w12));
135 Round(a, b, c, d, e, f2(b, c, d), k4, w11 = left(w11 ^ w8 ^ w3 ^ w13));
136 Round(e, a, b, c, d, f2(a, b, c), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14));
137 Round(d, e, a, b, c, f2(e, a, b), k4, left(w13 ^ w10 ^ w5 ^ w15));
138 Round(c, d, e, a, b, f2(d, e, a), k4, left(w14 ^ w11 ^ w6 ^ w0));
139 Round(b, c, d, e, a, f2(c, d, e), k4, left(w15 ^ w12 ^ w7 ^ w1));
159 const uint8_t *end = data + len;
160 size_t bufsize =
bytes % 64;
161 if (bufsize && bufsize + len >= 64) {
163 memcpy(
buf + bufsize, data, 64 - bufsize);
164 bytes += 64 - bufsize;
165 data += 64 - bufsize;
169 while (end - data >= 64) {
177 memcpy(
buf + bufsize, data, end - data);
184 static const uint8_t pad[64] = {0x80};
static const uint8_t k1[32]
static const uint8_t k2[32]
void Finalize(uint8_t hash[OUTPUT_SIZE])
CSHA1 & Write(const uint8_t *data, size_t len)
static void WriteBE64(uint8_t *ptr, uint64_t x)
static void WriteBE32(uint8_t *ptr, uint32_t x)
static uint32_t ReadBE32(const uint8_t *ptr)
#define Round(a, b, c, d, e, f, g, h, k, w)
Internal SHA-1 implementation.
void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks)