[email protected] | db163f8 | 2010-04-02 21:01:35 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 301415e | 2008-09-04 19:00:37 | [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 "base/hmac.h" |
| 6 | |
| 7 | #include <CommonCrypto/CommonHMAC.h> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | |
| 11 | namespace base { |
| 12 | |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 13 | struct HMACPlatformData { |
| 14 | std::string key_; |
| 15 | }; |
| 16 | |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 17 | HMAC::HMAC(HashAlgorithm hash_alg) |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 18 | : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
[email protected] | db163f8 | 2010-04-02 21:01:35 | [diff] [blame] | 19 | // Only SHA-1 and SHA-256 hash algorithms are supported now. |
| 20 | DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | bool HMAC::Init(const unsigned char *key, int key_length) { |
| 24 | if (!plat_->key_.empty()) { |
| 25 | // Init must not be called more than once on the same HMAC object. |
| 26 | NOTREACHED(); |
| 27 | return false; |
| 28 | } |
| 29 | |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 30 | plat_->key_.assign(reinterpret_cast<const char*>(key), key_length); |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 31 | |
| 32 | return true; |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | HMAC::~HMAC() { |
| 36 | // Zero out key copy. |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 37 | plat_->key_.assign(plat_->key_.length(), std::string::value_type()); |
| 38 | plat_->key_.clear(); |
| 39 | plat_->key_.reserve(0); |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool HMAC::Sign(const std::string& data, |
| 43 | unsigned char* digest, |
| 44 | int digest_length) { |
| 45 | CCHmacAlgorithm algorithm; |
| 46 | int algorithm_digest_length; |
| 47 | switch (hash_alg_) { |
| 48 | case SHA1: |
| 49 | algorithm = kCCHmacAlgSHA1; |
| 50 | algorithm_digest_length = CC_SHA1_DIGEST_LENGTH; |
| 51 | break; |
[email protected] | db163f8 | 2010-04-02 21:01:35 | [diff] [blame] | 52 | case SHA256: |
| 53 | algorithm = kCCHmacAlgSHA256; |
| 54 | algorithm_digest_length = CC_SHA256_DIGEST_LENGTH; |
| 55 | break; |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 56 | default: |
| 57 | NOTREACHED(); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | if (digest_length < algorithm_digest_length) { |
| 62 | NOTREACHED(); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | CCHmac(algorithm, |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 67 | plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 68 | digest); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | } // namespace base |