[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [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 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/hmac.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 6 | |
| 7 | #include <openssl/hmac.h> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 14 | #include "base/stl_util.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 15 | #include "crypto/openssl_util.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 16 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 17 | namespace crypto { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 18 | |
| 19 | struct HMACPlatformData { |
| 20 | std::vector<unsigned char> key; |
| 21 | }; |
| 22 | |
| 23 | HMAC::HMAC(HashAlgorithm hash_alg) |
| 24 | : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
| 25 | // Only SHA-1 and SHA-256 hash algorithms are supported now. |
| 26 | DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); |
| 27 | } |
| 28 | |
[email protected] | 673266c4 | 2012-12-04 00:50:35 | [diff] [blame] | 29 | bool HMAC::Init(const unsigned char* key, size_t key_length) { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 30 | // Init must not be called more than once on the same HMAC object. |
| 31 | DCHECK(plat_->key.empty()); |
| 32 | |
| 33 | plat_->key.assign(key, key + key_length); |
[email protected] | 0e83fe8c | 2014-04-10 21:16:59 | [diff] [blame] | 34 | if (key_length == 0) { |
| 35 | // Special-case: if the key is empty, use a key with one zero |
| 36 | // byte. OpenSSL's HMAC function breaks when passed a NULL key. (It calls |
| 37 | // HMAC_Init_ex which treats a NULL key as having already been initialized |
| 38 | // with a key previously.) HMAC pads keys with zeros, so this key is |
| 39 | // equivalent. |
| 40 | plat_->key.push_back(0); |
| 41 | } |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | HMAC::~HMAC() { |
| 46 | // Zero out key copy. |
| 47 | plat_->key.assign(plat_->key.size(), 0); |
| 48 | STLClearObject(&plat_->key); |
| 49 | } |
| 50 | |
[email protected] | c28986ee | 2011-06-06 22:00:11 | [diff] [blame] | 51 | bool HMAC::Sign(const base::StringPiece& data, |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 52 | unsigned char* digest, |
[email protected] | 673266c4 | 2012-12-04 00:50:35 | [diff] [blame] | 53 | size_t digest_length) const { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 54 | DCHECK(!plat_->key.empty()); // Init must be called before Sign. |
| 55 | |
| 56 | ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); |
| 57 | return ::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), |
| 58 | &plat_->key[0], plat_->key.size(), |
| 59 | reinterpret_cast<const unsigned char*>(data.data()), |
| 60 | data.size(), |
| 61 | result.safe_buffer(), NULL); |
| 62 | } |
| 63 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 64 | } // namespace crypto |