diff options
author | rsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-29 23:28:55 +0000 |
---|---|---|
committer | rsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-29 23:28:55 +0000 |
commit | baff1d0461a2d10eb9f99b64d3fd724e7b363a44 (patch) | |
tree | a23fbf71576e47f26200a29bef0e514a54ded89a /crypto/hmac.cc | |
parent | f94d52bc3311375e96a1a0b620f46054c7cc0d6a (diff) | |
download | chromium_src-baff1d0461a2d10eb9f99b64d3fd724e7b363a44.zip chromium_src-baff1d0461a2d10eb9f99b64d3fd724e7b363a44.tar.gz chromium_src-baff1d0461a2d10eb9f99b64d3fd724e7b363a44.tar.bz2 |
Add a routine for truncated HMAC verification
BUG=none
TEST=crypto_unittests --gtest_filter=HMACTest.*
Review URL: http://codereview.chromium.org/7532020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@94794 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto/hmac.cc')
-rw-r--r-- | crypto/hmac.cc | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/crypto/hmac.cc b/crypto/hmac.cc index 588cb9e..74d1f91 100644 --- a/crypto/hmac.cc +++ b/crypto/hmac.cc @@ -4,6 +4,8 @@ #include "crypto/hmac.h" +#include <algorithm> + #include "base/logging.h" namespace crypto { @@ -43,12 +45,21 @@ bool HMAC::Verify(const base::StringPiece& data, const base::StringPiece& digest) const { if (digest.size() != DigestLength()) return false; + return VerifyTruncated(data, digest); +} + +bool HMAC::VerifyTruncated(const base::StringPiece& data, + const base::StringPiece& digest) const { + if (digest.empty()) + return false; + size_t digest_length = DigestLength(); scoped_array<unsigned char> computed_digest( - new unsigned char[digest.size()]); - if (!Sign(data, computed_digest.get(), static_cast<int>(digest.size()))) + new unsigned char[digest_length]); + if (!Sign(data, computed_digest.get(), static_cast<int>(digest_length))) return false; - return SecureMemcmp(digest.data(), computed_digest.get(), digest.size()); + return SecureMemcmp(digest.data(), computed_digest.get(), + std::min(digest.size(), digest_length)); } } // namespace crypto |