OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 bool HMAC::Init(const unsigned char* key, size_t key_length) { | 44 bool HMAC::Init(const unsigned char* key, size_t key_length) { |
45 // Init must not be called more than once on the same HMAC object. | 45 // Init must not be called more than once on the same HMAC object. |
46 DCHECK(!initialized_); | 46 DCHECK(!initialized_); |
47 initialized_ = true; | 47 initialized_ = true; |
48 key_.assign(key, key + key_length); | 48 key_.assign(key, key + key_length); |
49 return true; | 49 return true; |
50 } | 50 } |
51 | 51 |
52 bool HMAC::Init(const SymmetricKey* key) { | 52 bool HMAC::Init(const SymmetricKey* key) { |
53 std::string raw_key; | 53 return Init(key->key()); |
54 bool result = key->GetRawKey(&raw_key) && Init(raw_key); | |
55 // Zero out key copy. This might get optimized away, but one can hope. | |
56 // Using std::string to store key info at all is a larger problem. | |
57 std::fill(raw_key.begin(), raw_key.end(), 0); | |
58 return result; | |
59 } | 54 } |
60 | 55 |
61 bool HMAC::Sign(const base::StringPiece& data, | 56 bool HMAC::Sign(const base::StringPiece& data, |
62 unsigned char* digest, | 57 unsigned char* digest, |
63 size_t digest_length) const { | 58 size_t digest_length) const { |
64 DCHECK(initialized_); | 59 DCHECK(initialized_); |
65 | 60 |
66 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); | 61 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); |
67 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), key_.data(), | 62 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), key_.data(), |
68 key_.size(), | 63 key_.size(), |
(...skipping 16 matching lines...) Expand all Loading... |
85 std::unique_ptr<unsigned char[]> computed_digest( | 80 std::unique_ptr<unsigned char[]> computed_digest( |
86 new unsigned char[digest_length]); | 81 new unsigned char[digest_length]); |
87 if (!Sign(data, computed_digest.get(), digest_length)) | 82 if (!Sign(data, computed_digest.get(), digest_length)) |
88 return false; | 83 return false; |
89 | 84 |
90 return SecureMemEqual(digest.data(), computed_digest.get(), | 85 return SecureMemEqual(digest.data(), computed_digest.get(), |
91 std::min(digest.size(), digest_length)); | 86 std::min(digest.size(), digest_length)); |
92 } | 87 } |
93 | 88 |
94 } // namespace crypto | 89 } // namespace crypto |
OLD | NEW |