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 "crypto/hmac.h" | 5 #include "crypto/hmac.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace crypto { | 9 namespace crypto { |
10 | 10 |
11 size_t HMAC::DigestLength() const { | 11 size_t HMAC::DigestLength() const { |
12 switch (hash_alg_) { | 12 switch (hash_alg_) { |
13 case SHA1: | 13 case SHA1: |
14 return 20; | 14 return 20; |
15 case SHA256: | 15 case SHA256: |
16 return 32; | 16 return 32; |
17 default: | 17 default: |
18 NOTREACHED(); | 18 NOTREACHED(); |
19 return 0; | 19 return 0; |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 bool HMAC::Verify(const base::StringPiece& data, | |
24 const base::StringPiece& digest) const { | |
25 if (digest.size() != DigestLength()) | |
26 return false; | |
27 scoped_array<unsigned char> computed_digest( | |
28 new unsigned char[digest.size()]); | |
29 if (!Sign(data, computed_digest.get(), static_cast<int>(digest.size()))) | |
30 return false; | |
31 | |
32 // In order to avoid any timing attacks, this comparison must be constant | |
33 // time with respect to the input. Using a comparison such as memcmp() can | |
34 // cause a short-circuit once a differing byte is found, which can reveal | |
35 // to an attacker which byte (and possibly which bit) of the digest was | |
36 // invalid. See also: | |
37 // http://groups.google.com/group/keyczar-discuss/browse_thread/thread/5571eca 0948b2a13 | |
wtc
2011/07/14 00:49:42
This URL doesn't work for me.
| |
38 const unsigned char* digest_ptr = | |
39 reinterpret_cast<const unsigned char*>(digest.data()); | |
40 const unsigned char* computed_ptr = computed_digest.get(); | |
41 unsigned char tmp = 0; | |
42 for (size_t i = 0; i < digest.size(); ++i) | |
43 tmp |= *digest_ptr++ ^ *computed_ptr++; | |
wtc
2011/07/14 00:00:22
We should add a SecureMemcmp function.
Ryan Sleevi
2011/07/14 00:18:19
Was this remark for a high-level, crypto/ API, or
wtc
2011/07/14 00:49:42
Sorry I wasn't clear. I meant a function that can
| |
44 | |
45 return tmp == 0; | |
46 } | |
47 | |
23 } // namespace crypto | 48 } // namespace crypto |
OLD | NEW |