blob: 24213338cc0b2a1bbae3bf24edadd48463b9a635 [file] [log] [blame]
[email protected]2c9f0def2012-01-11 23:17:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]301415e2008-09-04 19:00:374
initial.commitd7cae122008-07-26 21:49:385// 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]4b559b4d2011-04-14 17:37:148#ifndef CRYPTO_HMAC_H_
9#define CRYPTO_HMAC_H_
initial.commitd7cae122008-07-26 21:49:3810
avidd373b8b2015-12-21 21:34:4311#include <stddef.h>
12
thakisd1a18472016-04-08 22:30:4113#include <memory>
svaldez9c641462016-05-02 20:49:0514#include <vector>
thakisd1a18472016-04-08 22:30:4115
[email protected]6df5b9e2011-07-30 05:18:0116#include "base/compiler_specific.h"
avidd373b8b2015-12-21 21:34:4317#include "base/macros.h"
[email protected]daf079a2013-04-17 21:42:4018#include "base/strings/string_piece.h"
[email protected]d613a9902011-08-05 20:59:1119#include "crypto/crypto_export.h"
initial.commitd7cae122008-07-26 21:49:3820
[email protected]4b559b4d2011-04-14 17:37:1421namespace crypto {
[email protected]301415e2008-09-04 19:00:3722
[email protected]fbcfafe2008-09-08 13:58:1023// Simplify the interface and reduce includes by abstracting out the internals.
[email protected]2c9f0def2012-01-11 23:17:1424class SymmetricKey;
[email protected]fbcfafe2008-09-08 13:58:1025
[email protected]d613a9902011-08-05 20:59:1126class CRYPTO_EXPORT HMAC {
initial.commitd7cae122008-07-26 21:49:3827 public:
28 // The set of supported hash functions. Extend as required.
29 enum HashAlgorithm {
[email protected]1b47ce22010-03-31 16:18:3030 SHA1,
31 SHA256,
initial.commitd7cae122008-07-26 21:49:3832 };
33
[email protected]d91f8432009-05-05 23:55:5934 explicit HMAC(HashAlgorithm hash_alg);
initial.commitd7cae122008-07-26 21:49:3835 ~HMAC();
36
[email protected]ff24f492011-05-06 20:19:1037 // Returns the length of digest that this HMAC will create.
[email protected]a5aec2e2011-04-30 18:55:1838 size_t DigestLength() const;
39
40 // TODO(abarth): Add a PreferredKeyLength() member function.
41
[email protected]d91f8432009-05-05 23:55:5942 // 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]5739bd52012-03-14 17:41:4644 //
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]673266c42012-12-04 00:50:3552 bool Init(const unsigned char* key, size_t key_length) WARN_UNUSED_RESULT;
[email protected]d91f8432009-05-05 23:55:5953
[email protected]2c9f0def2012-01-11 23:17:1454 // 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]d91f8432009-05-05 23:55:5958 // Initializes this instance using |key|. Call Init only once. It returns
59 // false on the second or later calls.
[email protected]6df5b9e2011-07-30 05:18:0160 bool Init(const base::StringPiece& key) WARN_UNUSED_RESULT {
[email protected]d91f8432009-05-05 23:55:5961 return Init(reinterpret_cast<const unsigned char*>(key.data()),
[email protected]673266c42012-12-04 00:50:3562 key.size());
[email protected]d91f8432009-05-05 23:55:5963 }
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]c28986ee2011-06-06 22:00:1168 bool Sign(const base::StringPiece& data, unsigned char* digest,
[email protected]673266c42012-12-04 00:50:3569 size_t digest_length) const WARN_UNUSED_RESULT;
initial.commitd7cae122008-07-26 21:49:3870
[email protected]3292f532011-07-18 00:39:4471 // 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]baff1d042011-07-29 23:28:5576 // undermine the cryptographic integrity. |digest| must be exactly
77 // |DigestLength()| bytes long.
[email protected]3292f532011-07-18 00:39:4478 bool Verify(const base::StringPiece& data,
[email protected]6df5b9e2011-07-30 05:18:0179 const base::StringPiece& digest) const WARN_UNUSED_RESULT;
[email protected]1b47ce22010-03-31 16:18:3080
[email protected]baff1d042011-07-29 23:28:5581 // Verifies a truncated HMAC, behaving identical to Verify(), except
82 // that |digest| is allowed to be smaller than |DigestLength()|.
[email protected]6df5b9e2011-07-30 05:18:0183 bool VerifyTruncated(
84 const base::StringPiece& data,
85 const base::StringPiece& digest) const WARN_UNUSED_RESULT;
[email protected]baff1d042011-07-29 23:28:5586
initial.commitd7cae122008-07-26 21:49:3887 private:
[email protected]301415e2008-09-04 19:00:3788 HashAlgorithm hash_alg_;
svaldez9c641462016-05-02 20:49:0589 bool initialized_;
90 std::vector<unsigned char> key_;
initial.commitd7cae122008-07-26 21:49:3891
[email protected]301415e2008-09-04 19:00:3792 DISALLOW_COPY_AND_ASSIGN(HMAC);
initial.commitd7cae122008-07-26 21:49:3893};
94
[email protected]4b559b4d2011-04-14 17:37:1495} // namespace crypto
initial.commitd7cae122008-07-26 21:49:3896
[email protected]4b559b4d2011-04-14 17:37:1497#endif // CRYPTO_HMAC_H_