OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 // A straightforward SHA-1 implementation based on RFC 3174. | |
32 // http://www.ietf.org/rfc/rfc3174.txt | |
33 // The names of functions and variables (such as "a", "b", and "f") follow notat
ions in RFC 3174. | |
34 | |
35 #include "config.h" | |
36 #include "SHA1.h" | |
37 | |
38 #include "Assertions.h" | |
39 | |
40 #include "StringExtras.h" | |
41 #include "text/CString.h" | |
42 | |
43 namespace WTF { | |
44 | |
45 #ifdef NDEBUG | |
46 static inline void testSHA1() { } | |
47 #else | |
48 static bool isTestSHA1Done; | |
49 | |
50 static void expectSHA1(CString input, int repeat, CString expected) | |
51 { | |
52 SHA1 sha1; | |
53 for (int i = 0; i < repeat; ++i) | |
54 sha1.addBytes(input); | |
55 CString actual = sha1.computeHexDigest(); | |
56 ASSERT_WITH_MESSAGE(actual == expected, "input: %s, repeat: %d, actual: %s,
expected: %s", input.data(), repeat, actual.data(), expected.data()); | |
57 } | |
58 | |
59 static void testSHA1() | |
60 { | |
61 if (isTestSHA1Done) | |
62 return; | |
63 isTestSHA1Done = true; | |
64 | |
65 // Examples taken from sample code in RFC 3174. | |
66 expectSHA1("abc", 1, "A9993E364706816ABA3E25717850C26C9CD0D89D"); | |
67 expectSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, "8
4983E441C3BD26EBAAE4AA1F95129E5E54670F1"); | |
68 expectSHA1("a", 1000000, "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F"); | |
69 expectSHA1("0123456701234567012345670123456701234567012345670123456701234567
", 10, "DEA356A2CDDD90C7A7ECEDC5EBB563934F460452"); | |
70 } | |
71 #endif | |
72 | |
73 static inline uint32_t f(int t, uint32_t b, uint32_t c, uint32_t d) | |
74 { | |
75 ASSERT(t >= 0 && t < 80); | |
76 if (t < 20) | |
77 return (b & c) | ((~b) & d); | |
78 if (t < 40) | |
79 return b ^ c ^ d; | |
80 if (t < 60) | |
81 return (b & c) | (b & d) | (c & d); | |
82 return b ^ c ^ d; | |
83 } | |
84 | |
85 static inline uint32_t k(int t) | |
86 { | |
87 ASSERT(t >= 0 && t < 80); | |
88 if (t < 20) | |
89 return 0x5a827999; | |
90 if (t < 40) | |
91 return 0x6ed9eba1; | |
92 if (t < 60) | |
93 return 0x8f1bbcdc; | |
94 return 0xca62c1d6; | |
95 } | |
96 | |
97 static inline uint32_t rotateLeft(int n, uint32_t x) | |
98 { | |
99 ASSERT(n >= 0 && n < 32); | |
100 return (x << n) | (x >> (32 - n)); | |
101 } | |
102 | |
103 SHA1::SHA1() | |
104 { | |
105 // FIXME: Move unit tests somewhere outside the constructor. See bug 55853. | |
106 testSHA1(); | |
107 reset(); | |
108 } | |
109 | |
110 void SHA1::addBytes(const uint8_t* input, size_t length) | |
111 { | |
112 while (length--) { | |
113 ASSERT(m_cursor < 64); | |
114 m_buffer[m_cursor++] = *input++; | |
115 ++m_totalBytes; | |
116 if (m_cursor == 64) | |
117 processBlock(); | |
118 } | |
119 } | |
120 | |
121 void SHA1::computeHash(Vector<uint8_t, 20>& digest) | |
122 { | |
123 finalize(); | |
124 | |
125 digest.clear(); | |
126 digest.resize(20); | |
127 for (size_t i = 0; i < 5; ++i) { | |
128 // Treat hashValue as a big-endian value. | |
129 uint32_t hashValue = m_hash[i]; | |
130 for (int j = 0; j < 4; ++j) { | |
131 digest[4 * i + (3 - j)] = hashValue & 0xFF; | |
132 hashValue >>= 8; | |
133 } | |
134 } | |
135 | |
136 reset(); | |
137 } | |
138 | |
139 CString SHA1::hexDigest(const Vector<uint8_t, 20>& digest) | |
140 { | |
141 char* start = 0; | |
142 CString result = CString::newUninitialized(40, start); | |
143 char* buffer = start; | |
144 for (size_t i = 0; i < 20; ++i) { | |
145 snprintf(buffer, 3, "%02X", digest.at(i)); | |
146 buffer += 2; | |
147 } | |
148 return result; | |
149 } | |
150 | |
151 CString SHA1::computeHexDigest() | |
152 { | |
153 Vector<uint8_t, 20> digest; | |
154 computeHash(digest); | |
155 return hexDigest(digest); | |
156 } | |
157 | |
158 void SHA1::finalize() | |
159 { | |
160 ASSERT(m_cursor < 64); | |
161 m_buffer[m_cursor++] = 0x80; | |
162 if (m_cursor > 56) { | |
163 // Pad out to next block. | |
164 while (m_cursor < 64) | |
165 m_buffer[m_cursor++] = 0x00; | |
166 processBlock(); | |
167 } | |
168 | |
169 for (size_t i = m_cursor; i < 56; ++i) | |
170 m_buffer[i] = 0x00; | |
171 | |
172 // Write the length as a big-endian 64-bit value. | |
173 uint64_t bits = m_totalBytes * 8; | |
174 for (int i = 0; i < 8; ++i) { | |
175 m_buffer[56 + (7 - i)] = bits & 0xFF; | |
176 bits >>= 8; | |
177 } | |
178 m_cursor = 64; | |
179 processBlock(); | |
180 } | |
181 | |
182 void SHA1::processBlock() | |
183 { | |
184 ASSERT(m_cursor == 64); | |
185 | |
186 uint32_t w[80] = { 0 }; | |
187 for (int t = 0; t < 16; ++t) | |
188 w[t] = (m_buffer[t * 4] << 24) | (m_buffer[t * 4 + 1] << 16) | (m_buffer
[t * 4 + 2] << 8) | m_buffer[t * 4 + 3]; | |
189 for (int t = 16; t < 80; ++t) | |
190 w[t] = rotateLeft(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]); | |
191 | |
192 uint32_t a = m_hash[0]; | |
193 uint32_t b = m_hash[1]; | |
194 uint32_t c = m_hash[2]; | |
195 uint32_t d = m_hash[3]; | |
196 uint32_t e = m_hash[4]; | |
197 | |
198 for (int t = 0; t < 80; ++t) { | |
199 uint32_t temp = rotateLeft(5, a) + f(t, b, c, d) + e + w[t] + k(t); | |
200 e = d; | |
201 d = c; | |
202 c = rotateLeft(30, b); | |
203 b = a; | |
204 a = temp; | |
205 } | |
206 | |
207 m_hash[0] += a; | |
208 m_hash[1] += b; | |
209 m_hash[2] += c; | |
210 m_hash[3] += d; | |
211 m_hash[4] += e; | |
212 | |
213 m_cursor = 0; | |
214 } | |
215 | |
216 void SHA1::reset() | |
217 { | |
218 m_cursor = 0; | |
219 m_totalBytes = 0; | |
220 m_hash[0] = 0x67452301; | |
221 m_hash[1] = 0xefcdab89; | |
222 m_hash[2] = 0x98badcfe; | |
223 m_hash[3] = 0x10325476; | |
224 m_hash[4] = 0xc3d2e1f0; | |
225 | |
226 // Clear the buffer after use in case it's sensitive. | |
227 memset(m_buffer, 0, sizeof(m_buffer)); | |
228 } | |
229 | |
230 } // namespace WTF | |
OLD | NEW |