[email protected] | 2c9f0def | 2012-01-11 23:17:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "crypto/hmac.h" |
| 6 | |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 7 | #include <openssl/hmac.h> |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | baff1d04 | 2011-07-29 23:28:55 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 12 | #include "base/logging.h" |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 13 | #include "base/stl_util.h" |
| 14 | #include "crypto/openssl_util.h" |
[email protected] | 3cdf6d4 | 2011-10-07 17:02:48 | [diff] [blame] | 15 | #include "crypto/secure_util.h" |
[email protected] | 2c9f0def | 2012-01-11 23:17:14 | [diff] [blame] | 16 | #include "crypto/symmetric_key.h" |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 17 | |
| 18 | namespace crypto { |
| 19 | |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 20 | HMAC::HMAC(HashAlgorithm hash_alg) : hash_alg_(hash_alg), initialized_(false) { |
| 21 | // Only SHA-1 and SHA-256 hash algorithms are supported now. |
| 22 | DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); |
| 23 | } |
| 24 | |
| 25 | HMAC::~HMAC() { |
| 26 | // Zero out key copy. |
| 27 | key_.assign(key_.size(), 0); |
skyostil | 8ef165b | 2016-08-12 13:05:00 | [diff] [blame^] | 28 | base::STLClearObject(&key_); |
[email protected] | 2c9f0def | 2012-01-11 23:17:14 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 31 | size_t HMAC::DigestLength() const { |
| 32 | switch (hash_alg_) { |
| 33 | case SHA1: |
| 34 | return 20; |
| 35 | case SHA256: |
| 36 | return 32; |
| 37 | default: |
| 38 | NOTREACHED(); |
| 39 | return 0; |
| 40 | } |
| 41 | } |
| 42 | |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 43 | bool HMAC::Init(const unsigned char* key, size_t key_length) { |
| 44 | // Init must not be called more than once on the same HMAC object. |
| 45 | DCHECK(!initialized_); |
| 46 | initialized_ = true; |
| 47 | key_.assign(key, key + key_length); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool HMAC::Init(SymmetricKey* key) { |
| 52 | std::string raw_key; |
| 53 | bool result = key->GetRawKey(&raw_key) && Init(raw_key); |
| 54 | // Zero out key copy. This might get optimized away, but one can hope. |
| 55 | // Using std::string to store key info at all is a larger problem. |
| 56 | std::fill(raw_key.begin(), raw_key.end(), 0); |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | bool HMAC::Sign(const base::StringPiece& data, |
| 61 | unsigned char* digest, |
| 62 | size_t digest_length) const { |
| 63 | DCHECK(initialized_); |
| 64 | |
| 65 | ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 66 | return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), key_.data(), |
| 67 | key_.size(), |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 68 | reinterpret_cast<const unsigned char*>(data.data()), |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 69 | data.size(), result.safe_buffer(), nullptr); |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 3292f53 | 2011-07-18 00:39:44 | [diff] [blame] | 72 | bool HMAC::Verify(const base::StringPiece& data, |
| 73 | const base::StringPiece& digest) const { |
| 74 | if (digest.size() != DigestLength()) |
| 75 | return false; |
[email protected] | baff1d04 | 2011-07-29 23:28:55 | [diff] [blame] | 76 | return VerifyTruncated(data, digest); |
| 77 | } |
| 78 | |
| 79 | bool HMAC::VerifyTruncated(const base::StringPiece& data, |
| 80 | const base::StringPiece& digest) const { |
| 81 | if (digest.empty()) |
| 82 | return false; |
| 83 | size_t digest_length = DigestLength(); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 84 | std::unique_ptr<unsigned char[]> computed_digest( |
[email protected] | baff1d04 | 2011-07-29 23:28:55 | [diff] [blame] | 85 | new unsigned char[digest_length]); |
[email protected] | 673266c4 | 2012-12-04 00:50:35 | [diff] [blame] | 86 | if (!Sign(data, computed_digest.get(), digest_length)) |
[email protected] | 3292f53 | 2011-07-18 00:39:44 | [diff] [blame] | 87 | return false; |
| 88 | |
[email protected] | 3cdf6d4 | 2011-10-07 17:02:48 | [diff] [blame] | 89 | return SecureMemEqual(digest.data(), computed_digest.get(), |
| 90 | std::min(digest.size(), digest_length)); |
[email protected] | 3292f53 | 2011-07-18 00:39:44 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 93 | } // namespace crypto |