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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "crypto/secure_util.h" | 10 #include "crypto/secure_util.h" |
11 #include "crypto/symmetric_key.h" | |
11 | 12 |
12 namespace crypto { | 13 namespace crypto { |
13 | 14 |
15 bool HMAC::Init(SymmetricKey* key) { | |
16 std::string raw_key; | |
17 bool result = key->GetRawKey(&raw_key) && Init(raw_key); | |
18 // Zero out key copy. This probably just gets optimized away, | |
kmixter1
2012/01/05 23:27:53
I don't think the stuff after fill will have any e
Greg Spencer (Chromium)
2012/01/05 23:41:06
OK, I'll do that. Why is mine different (other th
kmixter1
2012/01/06 17:55:59
I was trying to use data() to get at the actual in
| |
19 // but one can hope. Using std::string to store key info at all is a larger | |
20 // problem. | |
21 std::fill(raw_key.begin(), raw_key.end(), 0); | |
22 // Trying to keep the call above from being optimized away by assigning info | |
23 // from the object to a volatile. | |
24 volatile char *optimization_blocker = const_cast<char*>(raw_key.c_str()); | |
25 optimization_blocker = NULL; | |
26 return result; | |
27 } | |
28 | |
14 size_t HMAC::DigestLength() const { | 29 size_t HMAC::DigestLength() const { |
15 switch (hash_alg_) { | 30 switch (hash_alg_) { |
16 case SHA1: | 31 case SHA1: |
17 return 20; | 32 return 20; |
18 case SHA256: | 33 case SHA256: |
19 return 32; | 34 return 32; |
20 default: | 35 default: |
21 NOTREACHED(); | 36 NOTREACHED(); |
22 return 0; | 37 return 0; |
23 } | 38 } |
(...skipping 14 matching lines...) Expand all Loading... | |
38 scoped_array<unsigned char> computed_digest( | 53 scoped_array<unsigned char> computed_digest( |
39 new unsigned char[digest_length]); | 54 new unsigned char[digest_length]); |
40 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) | 55 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) |
41 return false; | 56 return false; |
42 | 57 |
43 return SecureMemEqual(digest.data(), computed_digest.get(), | 58 return SecureMemEqual(digest.data(), computed_digest.get(), |
44 std::min(digest.size(), digest_length)); | 59 std::min(digest.size(), digest_length)); |
45 } | 60 } |
46 | 61 |
47 } // namespace crypto | 62 } // namespace crypto |
OLD | NEW |