1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/hmac.h"
9 #include "base/logging.h"
10 #include "crypto/secure_util.h"
11 #include "crypto/symmetric_key.h"
15 bool HMAC::Init(SymmetricKey
* key
) {
17 bool result
= key
->GetRawKey(&raw_key
) && Init(raw_key
);
18 // Zero out key copy. This might get optimized away, but one can hope.
19 // Using std::string to store key info at all is a larger problem.
20 std::fill(raw_key
.begin(), raw_key
.end(), 0);
24 size_t HMAC::DigestLength() const {
36 bool HMAC::Verify(const base::StringPiece
& data
,
37 const base::StringPiece
& digest
) const {
38 if (digest
.size() != DigestLength())
40 return VerifyTruncated(data
, digest
);
43 bool HMAC::VerifyTruncated(const base::StringPiece
& data
,
44 const base::StringPiece
& digest
) const {
47 size_t digest_length
= DigestLength();
48 scoped_ptr
<unsigned char[]> computed_digest(
49 new unsigned char[digest_length
]);
50 if (!Sign(data
, computed_digest
.get(), digest_length
))
53 return SecureMemEqual(digest
.data(), computed_digest
.get(),
54 std::min(digest
.size(), digest_length
));