| 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 might get optimized away, but one can hope. |
| 19 // Using std::string to store key info at all is a larger problem. |
| 20 std::fill(raw_key.begin(), raw_key.end(), 0); |
| 21 return result; |
| 22 } |
| 23 |
| 14 size_t HMAC::DigestLength() const { | 24 size_t HMAC::DigestLength() const { |
| 15 switch (hash_alg_) { | 25 switch (hash_alg_) { |
| 16 case SHA1: | 26 case SHA1: |
| 17 return 20; | 27 return 20; |
| 18 case SHA256: | 28 case SHA256: |
| 19 return 32; | 29 return 32; |
| 20 default: | 30 default: |
| 21 NOTREACHED(); | 31 NOTREACHED(); |
| 22 return 0; | 32 return 0; |
| 23 } | 33 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 scoped_array<unsigned char> computed_digest( | 48 scoped_array<unsigned char> computed_digest( |
| 39 new unsigned char[digest_length]); | 49 new unsigned char[digest_length]); |
| 40 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) | 50 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) |
| 41 return false; | 51 return false; |
| 42 | 52 |
| 43 return SecureMemEqual(digest.data(), computed_digest.get(), | 53 return SecureMemEqual(digest.data(), computed_digest.get(), |
| 44 std::min(digest.size(), digest_length)); | 54 std::min(digest.size(), digest_length)); |
| 45 } | 55 } |
| 46 | 56 |
| 47 } // namespace crypto | 57 } // namespace crypto |
| OLD | NEW |