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