[email protected] | 2c9f0def | 2012-01-11 23:17:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 4 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 5 | // Utility class for calculating the HMAC for a given message. We currently |
| 6 | // only support SHA1 for the hash algorithm, but this can be extended easily. |
| 7 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 8 | #ifndef CRYPTO_HMAC_H_ |
| 9 | #define CRYPTO_HMAC_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | c28986ee | 2011-06-06 22:00:11 | [diff] [blame] | 14 | #include "base/string_piece.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 15 | #include "crypto/crypto_export.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 17 | namespace crypto { |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 18 | |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 19 | // Simplify the interface and reduce includes by abstracting out the internals. |
| 20 | struct HMACPlatformData; |
[email protected] | 2c9f0def | 2012-01-11 23:17:14 | [diff] [blame] | 21 | class SymmetricKey; |
[email protected] | fbcfafe | 2008-09-08 13:58:10 | [diff] [blame] | 22 | |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 23 | class CRYPTO_EXPORT HMAC { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | public: |
| 25 | // The set of supported hash functions. Extend as required. |
| 26 | enum HashAlgorithm { |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 27 | SHA1, |
| 28 | SHA256, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | }; |
| 30 | |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 31 | explicit HMAC(HashAlgorithm hash_alg); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | ~HMAC(); |
| 33 | |
[email protected] | ff24f49 | 2011-05-06 20:19:10 | [diff] [blame] | 34 | // Returns the length of digest that this HMAC will create. |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 35 | size_t DigestLength() const; |
| 36 | |
| 37 | // TODO(abarth): Add a PreferredKeyLength() member function. |
| 38 | |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 39 | // Initializes this instance using |key| of the length |key_length|. Call Init |
| 40 | // only once. It returns false on the second or later calls. |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 41 | // TODO(abarth): key_length should be a size_t. |
[email protected] | 5739bd5 | 2012-03-14 17:41:46 | [diff] [blame] | 42 | // |
| 43 | // NOTE: the US Federal crypto standard FIPS 198, Section 3 says: |
| 44 | // The size of the key, K, shall be equal to or greater than L/2, where L |
| 45 | // is the size of the hash function output. |
| 46 | // In FIPS 198-1 (and SP-800-107, which describes key size recommendations), |
| 47 | // this requirement is gone. But a system crypto library may still enforce |
| 48 | // this old requirement. If the key is shorter than this recommended value, |
| 49 | // Init() may fail. |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 50 | bool Init(const unsigned char* key, int key_length) WARN_UNUSED_RESULT; |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 51 | |
[email protected] | 2c9f0def | 2012-01-11 23:17:14 | [diff] [blame] | 52 | // Initializes this instance using |key|. Call Init |
| 53 | // only once. It returns false on the second or later calls. |
| 54 | bool Init(SymmetricKey* key) WARN_UNUSED_RESULT; |
| 55 | |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 56 | // Initializes this instance using |key|. Call Init only once. It returns |
| 57 | // false on the second or later calls. |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 58 | bool Init(const base::StringPiece& key) WARN_UNUSED_RESULT { |
[email protected] | d91f843 | 2009-05-05 23:55:59 | [diff] [blame] | 59 | return Init(reinterpret_cast<const unsigned char*>(key.data()), |
| 60 | static_cast<int>(key.size())); |
| 61 | } |
| 62 | |
| 63 | // Calculates the HMAC for the message in |data| using the algorithm supplied |
| 64 | // to the constructor and the key supplied to the Init method. The HMAC is |
| 65 | // returned in |digest|, which has |digest_length| bytes of storage available. |
[email protected] | a5aec2e | 2011-04-30 18:55:18 | [diff] [blame] | 66 | // TODO(abarth): digest_length should be a size_t. |
[email protected] | c28986ee | 2011-06-06 22:00:11 | [diff] [blame] | 67 | bool Sign(const base::StringPiece& data, unsigned char* digest, |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 68 | int digest_length) const WARN_UNUSED_RESULT; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | |
[email protected] | 3292f53 | 2011-07-18 00:39:44 | [diff] [blame] | 70 | // Verifies that the HMAC for the message in |data| equals the HMAC provided |
| 71 | // in |digest|, using the algorithm supplied to the constructor and the key |
| 72 | // supplied to the Init method. Use of this method is strongly recommended |
| 73 | // over using Sign() with a manual comparison (such as memcmp), as such |
| 74 | // comparisons may result in side-channel disclosures, such as timing, that |
[email protected] | baff1d04 | 2011-07-29 23:28:55 | [diff] [blame] | 75 | // undermine the cryptographic integrity. |digest| must be exactly |
| 76 | // |DigestLength()| bytes long. |
[email protected] | 3292f53 | 2011-07-18 00:39:44 | [diff] [blame] | 77 | bool Verify(const base::StringPiece& data, |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 78 | const base::StringPiece& digest) const WARN_UNUSED_RESULT; |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 79 | |
[email protected] | baff1d04 | 2011-07-29 23:28:55 | [diff] [blame] | 80 | // Verifies a truncated HMAC, behaving identical to Verify(), except |
| 81 | // that |digest| is allowed to be smaller than |DigestLength()|. |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 82 | bool VerifyTruncated( |
| 83 | const base::StringPiece& data, |
| 84 | const base::StringPiece& digest) const WARN_UNUSED_RESULT; |
[email protected] | baff1d04 | 2011-07-29 23:28:55 | [diff] [blame] | 85 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | private: |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 87 | HashAlgorithm hash_alg_; |
[email protected] | c7f4b627 | 2008-09-30 20:50:51 | [diff] [blame] | 88 | scoped_ptr<HMACPlatformData> plat_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | |
[email protected] | 301415e | 2008-09-04 19:00:37 | [diff] [blame] | 90 | DISALLOW_COPY_AND_ASSIGN(HMAC); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | }; |
| 92 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 93 | } // namespace crypto |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 95 | #endif // CRYPTO_HMAC_H_ |