Chromium Code Reviews| 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(const SymmetricKey& key) { | |
| 16 std::string raw_key; | |
| 17 key.GetRawKey(&raw_key); | |
|
Ryan Sleevi
2012/01/04 01:33:43
GetRawKey can fail (eg: non-exportable)
bool resu
| |
| 18 bool result = Init(raw_key); | |
| 19 // Zero out key copy. | |
| 20 raw_key.assign(raw_key.length(), std::string::value_type()); | |
| 21 raw_key.clear(); | |
| 22 raw_key.reserve(0); | |
|
Ryan Sleevi
2012/01/04 01:33:43
Unfortunately, it's possible (and in practice, lik
Greg Spencer (Chromium)
2012/01/05 22:18:03
I took this code from the HMAC destructor on mac,
Ryan Sleevi
2012/01/05 22:34:11
std::fill() just gets turned into a memset(), due
| |
| 23 return result; | |
| 24 } | |
| 25 | |
| 14 size_t HMAC::DigestLength() const { | 26 size_t HMAC::DigestLength() const { |
| 15 switch (hash_alg_) { | 27 switch (hash_alg_) { |
| 16 case SHA1: | 28 case SHA1: |
| 17 return 20; | 29 return 20; |
| 18 case SHA256: | 30 case SHA256: |
| 19 return 32; | 31 return 32; |
| 20 default: | 32 default: |
| 21 NOTREACHED(); | 33 NOTREACHED(); |
| 22 return 0; | 34 return 0; |
| 23 } | 35 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 38 scoped_array<unsigned char> computed_digest( | 50 scoped_array<unsigned char> computed_digest( |
| 39 new unsigned char[digest_length]); | 51 new unsigned char[digest_length]); |
| 40 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) | 52 if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) |
| 41 return false; | 53 return false; |
| 42 | 54 |
| 43 return SecureMemEqual(digest.data(), computed_digest.get(), | 55 return SecureMemEqual(digest.data(), computed_digest.get(), |
| 44 std::min(digest.size(), digest_length)); | 56 std::min(digest.size(), digest_length)); |
| 45 } | 57 } |
| 46 | 58 |
| 47 } // namespace crypto | 59 } // namespace crypto |
| OLD | NEW |