OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sha1.h" | 5 #include "base/sha1.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
| 11 #include "base/sys_byteorder.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 // Implementation of SHA-1. Only handles data in byte-sized blocks, | 15 // Implementation of SHA-1. Only handles data in byte-sized blocks, |
15 // which simplifies the code a fair bit. | 16 // which simplifies the code a fair bit. |
16 | 17 |
17 // Identifier names follow notation in FIPS PUB 180-3, where you'll | 18 // Identifier names follow notation in FIPS PUB 180-3, where you'll |
18 // also find a description of the algorithm: | 19 // also find a description of the algorithm: |
19 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf | 20 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf |
20 | 21 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 return 0x5a827999; | 86 return 0x5a827999; |
86 } else if (t < 40) { | 87 } else if (t < 40) { |
87 return 0x6ed9eba1; | 88 return 0x6ed9eba1; |
88 } else if (t < 60) { | 89 } else if (t < 60) { |
89 return 0x8f1bbcdc; | 90 return 0x8f1bbcdc; |
90 } else { | 91 } else { |
91 return 0xca62c1d6; | 92 return 0xca62c1d6; |
92 } | 93 } |
93 } | 94 } |
94 | 95 |
95 static inline void swapends(uint32_t* t) { | |
96 *t = (*t >> 24) | ((*t >> 8) & 0xff00) | ((*t & 0xff00) << 8) | (*t << 24); | |
97 } | |
98 | |
99 const int SecureHashAlgorithm::kDigestSizeBytes = 20; | 96 const int SecureHashAlgorithm::kDigestSizeBytes = 20; |
100 | 97 |
101 void SecureHashAlgorithm::Init() { | 98 void SecureHashAlgorithm::Init() { |
102 A = 0; | 99 A = 0; |
103 B = 0; | 100 B = 0; |
104 C = 0; | 101 C = 0; |
105 D = 0; | 102 D = 0; |
106 E = 0; | 103 E = 0; |
107 cursor = 0; | 104 cursor = 0; |
108 l = 0; | 105 l = 0; |
109 H[0] = 0x67452301; | 106 H[0] = 0x67452301; |
110 H[1] = 0xefcdab89; | 107 H[1] = 0xefcdab89; |
111 H[2] = 0x98badcfe; | 108 H[2] = 0x98badcfe; |
112 H[3] = 0x10325476; | 109 H[3] = 0x10325476; |
113 H[4] = 0xc3d2e1f0; | 110 H[4] = 0xc3d2e1f0; |
114 } | 111 } |
115 | 112 |
116 void SecureHashAlgorithm::Final() { | 113 void SecureHashAlgorithm::Final() { |
117 Pad(); | 114 Pad(); |
118 Process(); | 115 Process(); |
119 | 116 |
120 for (int t = 0; t < 5; ++t) | 117 for (int t = 0; t < 5; ++t) |
121 swapends(&H[t]); | 118 H[t] = ByteSwap(H[t]); |
122 } | 119 } |
123 | 120 |
124 void SecureHashAlgorithm::Update(const void* data, size_t nbytes) { | 121 void SecureHashAlgorithm::Update(const void* data, size_t nbytes) { |
125 const uint8_t* d = reinterpret_cast<const uint8_t*>(data); | 122 const uint8_t* d = reinterpret_cast<const uint8_t*>(data); |
126 while (nbytes--) { | 123 while (nbytes--) { |
127 M[cursor++] = *d++; | 124 M[cursor++] = *d++; |
128 if (cursor >= 64) | 125 if (cursor >= 64) |
129 Process(); | 126 Process(); |
130 l += 8; | 127 l += 8; |
131 } | 128 } |
(...skipping 26 matching lines...) Expand all Loading... |
158 void SecureHashAlgorithm::Process() { | 155 void SecureHashAlgorithm::Process() { |
159 uint32_t t; | 156 uint32_t t; |
160 | 157 |
161 // Each a...e corresponds to a section in the FIPS 180-3 algorithm. | 158 // Each a...e corresponds to a section in the FIPS 180-3 algorithm. |
162 | 159 |
163 // a. | 160 // a. |
164 // | 161 // |
165 // W and M are in a union, so no need to memcpy. | 162 // W and M are in a union, so no need to memcpy. |
166 // memcpy(W, M, sizeof(M)); | 163 // memcpy(W, M, sizeof(M)); |
167 for (t = 0; t < 16; ++t) | 164 for (t = 0; t < 16; ++t) |
168 swapends(&W[t]); | 165 W[t] = ByteSwap(W[t]); |
169 | 166 |
170 // b. | 167 // b. |
171 for (t = 16; t < 80; ++t) | 168 for (t = 16; t < 80; ++t) |
172 W[t] = S(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]); | 169 W[t] = S(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]); |
173 | 170 |
174 // c. | 171 // c. |
175 A = H[0]; | 172 A = H[0]; |
176 B = H[1]; | 173 B = H[1]; |
177 C = H[2]; | 174 C = H[2]; |
178 D = H[3]; | 175 D = H[3]; |
(...skipping 29 matching lines...) Expand all Loading... |
208 void SHA1HashBytes(const unsigned char* data, size_t len, | 205 void SHA1HashBytes(const unsigned char* data, size_t len, |
209 unsigned char* hash) { | 206 unsigned char* hash) { |
210 SecureHashAlgorithm sha; | 207 SecureHashAlgorithm sha; |
211 sha.Update(data, len); | 208 sha.Update(data, len); |
212 sha.Final(); | 209 sha.Final(); |
213 | 210 |
214 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); | 211 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); |
215 } | 212 } |
216 | 213 |
217 } // namespace base | 214 } // namespace base |
OLD | NEW |