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, |
| 19 // but one can hope. Using std::string to store key info at all is a larger |
| 20 // problem. This trys to keep the fill call from being optimized away by |
| 21 // creating an alias to internal data with a volatile. |
| 22 volatile char *optimization_blocker = const_cast<char*>(raw_key.data()); |
| 23 std::fill(raw_key.begin(), raw_key.end(), 0); |
| 24 optimization_blocker = NULL; |
| 25 return result; |
| 26 } |
| 27 |
14 size_t HMAC::DigestLength() const { | 28 size_t HMAC::DigestLength() const { |
15 switch (hash_alg_) { | 29 switch (hash_alg_) { |
16 case SHA1: | 30 case SHA1: |
17 return 20; | 31 return 20; |
18 case SHA256: | 32 case SHA256: |
19 return 32; | 33 return 32; |
20 default: | 34 default: |
21 NOTREACHED(); | 35 NOTREACHED(); |
22 return 0; | 36 return 0; |
23 } | 37 } |
(...skipping 14 matching lines...) Expand all Loading... |
38 scoped_array<unsigned char> computed_digest( | 52 scoped_array<unsigned char> computed_digest( |
39 new unsigned char[digest_length]); | 53 new unsigned char[digest_length]); |
40 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) | 54 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) |
41 return false; | 55 return false; |
42 | 56 |
43 return SecureMemEqual(digest.data(), computed_digest.get(), | 57 return SecureMemEqual(digest.data(), computed_digest.get(), |
44 std::min(digest.size(), digest_length)); | 58 std::min(digest.size(), digest_length)); |
45 } | 59 } |
46 | 60 |
47 } // namespace crypto | 61 } // namespace crypto |
OLD | NEW |