blob: c9bae65d519d7af25c46c2e1af1ae4b6208567c7 [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
initial.commitd7cae122008-07-26 21:49:3811#include "base/basictypes.h"
[email protected]6df5b9e2011-07-30 05:18:0112#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
[email protected]c28986ee2011-06-06 22:00:1114#include "base/string_piece.h"
[email protected]d613a9902011-08-05 20:59:1115#include "crypto/crypto_export.h"
initial.commitd7cae122008-07-26 21:49:3816
[email protected]4b559b4d2011-04-14 17:37:1417namespace crypto {
[email protected]301415e2008-09-04 19:00:3718
[email protected]fbcfafe2008-09-08 13:58:1019// Simplify the interface and reduce includes by abstracting out the internals.
20struct HMACPlatformData;
[email protected]2c9f0def2012-01-11 23:17:1421class SymmetricKey;
[email protected]fbcfafe2008-09-08 13:58:1022
[email protected]d613a9902011-08-05 20:59:1123class CRYPTO_EXPORT HMAC {
initial.commitd7cae122008-07-26 21:49:3824 public:
25 // The set of supported hash functions. Extend as required.
26 enum HashAlgorithm {
[email protected]1b47ce22010-03-31 16:18:3027 SHA1,
28 SHA256,
initial.commitd7cae122008-07-26 21:49:3829 };
30
[email protected]d91f8432009-05-05 23:55:5931 explicit HMAC(HashAlgorithm hash_alg);
initial.commitd7cae122008-07-26 21:49:3832 ~HMAC();
33
[email protected]ff24f492011-05-06 20:19:1034 // Returns the length of digest that this HMAC will create.
[email protected]a5aec2e2011-04-30 18:55:1835 size_t DigestLength() const;
36
37 // TODO(abarth): Add a PreferredKeyLength() member function.
38
[email protected]d91f8432009-05-05 23:55:5939 // 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]a5aec2e2011-04-30 18:55:1841 // TODO(abarth): key_length should be a size_t.
[email protected]5739bd52012-03-14 17:41:4642 //
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]6df5b9e2011-07-30 05:18:0150 bool Init(const unsigned char* key, int key_length) WARN_UNUSED_RESULT;
[email protected]d91f8432009-05-05 23:55:5951
[email protected]2c9f0def2012-01-11 23:17:1452 // 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]d91f8432009-05-05 23:55:5956 // Initializes this instance using |key|. Call Init only once. It returns
57 // false on the second or later calls.
[email protected]6df5b9e2011-07-30 05:18:0158 bool Init(const base::StringPiece& key) WARN_UNUSED_RESULT {
[email protected]d91f8432009-05-05 23:55:5959 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]a5aec2e2011-04-30 18:55:1866 // TODO(abarth): digest_length should be a size_t.
[email protected]c28986ee2011-06-06 22:00:1167 bool Sign(const base::StringPiece& data, unsigned char* digest,
[email protected]6df5b9e2011-07-30 05:18:0168 int digest_length) const WARN_UNUSED_RESULT;
initial.commitd7cae122008-07-26 21:49:3869
[email protected]3292f532011-07-18 00:39:4470 // 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]baff1d042011-07-29 23:28:5575 // undermine the cryptographic integrity. |digest| must be exactly
76 // |DigestLength()| bytes long.
[email protected]3292f532011-07-18 00:39:4477 bool Verify(const base::StringPiece& data,
[email protected]6df5b9e2011-07-30 05:18:0178 const base::StringPiece& digest) const WARN_UNUSED_RESULT;
[email protected]1b47ce22010-03-31 16:18:3079
[email protected]baff1d042011-07-29 23:28:5580 // Verifies a truncated HMAC, behaving identical to Verify(), except
81 // that |digest| is allowed to be smaller than |DigestLength()|.
[email protected]6df5b9e2011-07-30 05:18:0182 bool VerifyTruncated(
83 const base::StringPiece& data,
84 const base::StringPiece& digest) const WARN_UNUSED_RESULT;
[email protected]baff1d042011-07-29 23:28:5585
initial.commitd7cae122008-07-26 21:49:3886 private:
[email protected]301415e2008-09-04 19:00:3787 HashAlgorithm hash_alg_;
[email protected]c7f4b6272008-09-30 20:50:5188 scoped_ptr<HMACPlatformData> plat_;
initial.commitd7cae122008-07-26 21:49:3889
[email protected]301415e2008-09-04 19:00:3790 DISALLOW_COPY_AND_ASSIGN(HMAC);
initial.commitd7cae122008-07-26 21:49:3891};
92
[email protected]4b559b4d2011-04-14 17:37:1493} // namespace crypto
initial.commitd7cae122008-07-26 21:49:3894
[email protected]4b559b4d2011-04-14 17:37:1495#endif // CRYPTO_HMAC_H_