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 <openssl/hmac.h> | |
7 #include <stddef.h> | 8 #include <stddef.h> |
8 | 9 |
9 #include <algorithm> | 10 #include <algorithm> |
10 #include <memory> | 11 #include <memory> |
12 #include <vector> | |
11 | 13 |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/stl_util.h" | |
16 #include "crypto/openssl_util.h" | |
13 #include "crypto/secure_util.h" | 17 #include "crypto/secure_util.h" |
14 #include "crypto/symmetric_key.h" | 18 #include "crypto/symmetric_key.h" |
15 | 19 |
16 namespace crypto { | 20 namespace crypto { |
17 | 21 |
18 bool HMAC::Init(SymmetricKey* key) { | 22 struct HMACPlatformData { |
19 std::string raw_key; | 23 std::vector<unsigned char> key; |
20 bool result = key->GetRawKey(&raw_key) && Init(raw_key); | 24 }; |
Ryan Sleevi
2016/04/28 21:06:58
Can't we get rid of this entirely, since we're get
svaldez
2016/04/28 21:14:50
Done.
| |
21 // Zero out key copy. This might get optimized away, but one can hope. | 25 |
22 // Using std::string to store key info at all is a larger problem. | 26 HMAC::HMAC(HashAlgorithm hash_alg) : hash_alg_(hash_alg) { |
23 std::fill(raw_key.begin(), raw_key.end(), 0); | 27 // Only SHA-1 and SHA-256 hash algorithms are supported now. |
24 return result; | 28 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); |
29 } | |
30 | |
31 HMAC::~HMAC() { | |
32 if (plat_) { | |
33 // Zero out key copy. | |
34 plat_->key.assign(plat_->key.size(), 0); | |
35 STLClearObject(&plat_->key); | |
36 } | |
25 } | 37 } |
26 | 38 |
27 size_t HMAC::DigestLength() const { | 39 size_t HMAC::DigestLength() const { |
28 switch (hash_alg_) { | 40 switch (hash_alg_) { |
29 case SHA1: | 41 case SHA1: |
30 return 20; | 42 return 20; |
31 case SHA256: | 43 case SHA256: |
32 return 32; | 44 return 32; |
33 default: | 45 default: |
34 NOTREACHED(); | 46 NOTREACHED(); |
35 return 0; | 47 return 0; |
36 } | 48 } |
37 } | 49 } |
38 | 50 |
51 bool HMAC::Init(const unsigned char* key, size_t key_length) { | |
52 // Init must not be called more than once on the same HMAC object. | |
53 DCHECK(!plat_); | |
54 plat_.reset(new HMACPlatformData()); | |
55 plat_->key.assign(key, key + key_length); | |
56 return true; | |
57 } | |
58 | |
59 bool HMAC::Init(SymmetricKey* key) { | |
60 std::string raw_key; | |
61 bool result = key->GetRawKey(&raw_key) && Init(raw_key); | |
62 // Zero out key copy. This might get optimized away, but one can hope. | |
63 // Using std::string to store key info at all is a larger problem. | |
64 std::fill(raw_key.begin(), raw_key.end(), 0); | |
65 return result; | |
66 } | |
67 | |
68 bool HMAC::Sign(const base::StringPiece& data, | |
69 unsigned char* digest, | |
70 size_t digest_length) const { | |
71 DCHECK(plat_); // Init must be called before Sign. | |
72 | |
73 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); | |
74 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), | |
75 plat_->key.data(), plat_->key.size(), | |
76 reinterpret_cast<const unsigned char*>(data.data()), | |
77 data.size(), result.safe_buffer(), NULL); | |
78 } | |
79 | |
39 bool HMAC::Verify(const base::StringPiece& data, | 80 bool HMAC::Verify(const base::StringPiece& data, |
40 const base::StringPiece& digest) const { | 81 const base::StringPiece& digest) const { |
41 if (digest.size() != DigestLength()) | 82 if (digest.size() != DigestLength()) |
42 return false; | 83 return false; |
43 return VerifyTruncated(data, digest); | 84 return VerifyTruncated(data, digest); |
44 } | 85 } |
45 | 86 |
46 bool HMAC::VerifyTruncated(const base::StringPiece& data, | 87 bool HMAC::VerifyTruncated(const base::StringPiece& data, |
47 const base::StringPiece& digest) const { | 88 const base::StringPiece& digest) const { |
48 if (digest.empty()) | 89 if (digest.empty()) |
49 return false; | 90 return false; |
50 size_t digest_length = DigestLength(); | 91 size_t digest_length = DigestLength(); |
51 std::unique_ptr<unsigned char[]> computed_digest( | 92 std::unique_ptr<unsigned char[]> computed_digest( |
52 new unsigned char[digest_length]); | 93 new unsigned char[digest_length]); |
53 if (!Sign(data, computed_digest.get(), digest_length)) | 94 if (!Sign(data, computed_digest.get(), digest_length)) |
54 return false; | 95 return false; |
55 | 96 |
56 return SecureMemEqual(digest.data(), computed_digest.get(), | 97 return SecureMemEqual(digest.data(), computed_digest.get(), |
57 std::min(digest.size(), digest_length)); | 98 std::min(digest.size(), digest_length)); |
58 } | 99 } |
59 | 100 |
60 } // namespace crypto | 101 } // namespace crypto |
OLD | NEW |