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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 | 52 |
53 uint32 A, B, C, D, E; | 53 uint32 A, B, C, D, E; |
54 | 54 |
55 uint32 H[5]; | 55 uint32 H[5]; |
56 | 56 |
57 union { | 57 union { |
58 uint32 W[80]; | 58 uint32 W[80]; |
59 uint8 M[64]; | 59 uint8 M[64]; |
60 }; | 60 }; |
61 | 61 |
62 uint32 cursor; | 62 uint32 cursor; |
wtc
2014/07/12 01:23:40
Yukawa also suggested that |cursor| should be defi
yukawa
2014/07/12 03:40:01
Makes sense. Thanks.
| |
63 uint32 l; | 63 uint64 l; |
64 }; | 64 }; |
65 | 65 |
66 static inline uint32 f(uint32 t, uint32 B, uint32 C, uint32 D) { | 66 static inline uint32 f(uint32 t, uint32 B, uint32 C, uint32 D) { |
67 if (t < 20) { | 67 if (t < 20) { |
68 return (B & C) | ((~B) & D); | 68 return (B & C) | ((~B) & D); |
69 } else if (t < 40) { | 69 } else if (t < 40) { |
70 return B ^ C ^ D; | 70 return B ^ C ^ D; |
71 } else if (t < 60) { | 71 } else if (t < 60) { |
72 return (B & C) | (B & D) | (C & D); | 72 return (B & C) | (B & D) | (C & D); |
73 } else { | 73 } else { |
(...skipping 11 matching lines...) Expand all Loading... | |
85 } else if (t < 40) { | 85 } else if (t < 40) { |
86 return 0x6ed9eba1; | 86 return 0x6ed9eba1; |
87 } else if (t < 60) { | 87 } else if (t < 60) { |
88 return 0x8f1bbcdc; | 88 return 0x8f1bbcdc; |
89 } else { | 89 } else { |
90 return 0xca62c1d6; | 90 return 0xca62c1d6; |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 static inline void swapends(uint32* t) { | 94 static inline void swapends(uint32* t) { |
95 *t = ((*t & 0xff000000) >> 24) | | 95 *t = (*t >> 24) | ((*t >> 8) & 0xff00) | ((*t & 0xff00) << 8) | (*t << 24); |
96 ((*t & 0xff0000) >> 8) | | |
97 ((*t & 0xff00) << 8) | | |
98 ((*t & 0xff) << 24); | |
99 } | 96 } |
100 | 97 |
101 const int SecureHashAlgorithm::kDigestSizeBytes = 20; | 98 const int SecureHashAlgorithm::kDigestSizeBytes = 20; |
102 | 99 |
103 void SecureHashAlgorithm::Init() { | 100 void SecureHashAlgorithm::Init() { |
104 A = 0; | 101 A = 0; |
105 B = 0; | 102 B = 0; |
106 C = 0; | 103 C = 0; |
107 D = 0; | 104 D = 0; |
108 E = 0; | 105 E = 0; |
(...skipping 28 matching lines...) Expand all Loading... | |
137 M[cursor++] = 0x80; | 134 M[cursor++] = 0x80; |
138 | 135 |
139 if (cursor > 64-8) { | 136 if (cursor > 64-8) { |
140 // pad out to next block | 137 // pad out to next block |
141 while (cursor < 64) | 138 while (cursor < 64) |
142 M[cursor++] = 0; | 139 M[cursor++] = 0; |
143 | 140 |
144 Process(); | 141 Process(); |
145 } | 142 } |
146 | 143 |
147 while (cursor < 64-4) | 144 while (cursor < 64-8) |
148 M[cursor++] = 0; | 145 M[cursor++] = 0; |
149 | 146 |
150 M[64-4] = (l & 0xff000000) >> 24; | 147 M[cursor++] = (l >> 56) & 0xff; |
151 M[64-3] = (l & 0xff0000) >> 16; | 148 M[cursor++] = (l >> 48) & 0xff; |
152 M[64-2] = (l & 0xff00) >> 8; | 149 M[cursor++] = (l >> 40) & 0xff; |
153 M[64-1] = (l & 0xff); | 150 M[cursor++] = (l >> 32) & 0xff; |
151 M[cursor++] = (l >> 24) & 0xff; | |
152 M[cursor++] = (l >> 16) & 0xff; | |
153 M[cursor++] = (l >> 8) & 0xff; | |
154 M[cursor++] = l & 0xff; | |
wtc
2014/07/12 01:23:40
All the & 0xff operations can be omitted.
Mark Mentovai
2014/07/12 14:48:47
wtc wrote:
| |
154 } | 155 } |
155 | 156 |
156 void SecureHashAlgorithm::Process() { | 157 void SecureHashAlgorithm::Process() { |
157 uint32 t; | 158 uint32 t; |
158 | 159 |
159 // Each a...e corresponds to a section in the FIPS 180-3 algorithm. | 160 // Each a...e corresponds to a section in the FIPS 180-3 algorithm. |
160 | 161 |
161 // a. | 162 // a. |
162 // | 163 // |
163 // W and M are in a union, so no need to memcpy. | 164 // W and M are in a union, so no need to memcpy. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 void SHA1HashBytes(const unsigned char* data, size_t len, | 207 void SHA1HashBytes(const unsigned char* data, size_t len, |
207 unsigned char* hash) { | 208 unsigned char* hash) { |
208 SecureHashAlgorithm sha; | 209 SecureHashAlgorithm sha; |
209 sha.Update(data, len); | 210 sha.Update(data, len); |
210 sha.Final(); | 211 sha.Final(); |
211 | 212 |
212 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); | 213 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); |
213 } | 214 } |
214 | 215 |
215 } // namespace base | 216 } // namespace base |
OLD | NEW |