| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "crypto/hmac.h" | 5 #include "crypto/hmac.h" |
| 6 | 6 |
| 7 #include <openssl/hmac.h> | 7 #include <openssl/hmac.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 std::fill(raw_key.begin(), raw_key.end(), 0); | 56 std::fill(raw_key.begin(), raw_key.end(), 0); |
| 57 return result; | 57 return result; |
| 58 } | 58 } |
| 59 | 59 |
| 60 bool HMAC::Sign(const base::StringPiece& data, | 60 bool HMAC::Sign(const base::StringPiece& data, |
| 61 unsigned char* digest, | 61 unsigned char* digest, |
| 62 size_t digest_length) const { | 62 size_t digest_length) const { |
| 63 DCHECK(initialized_); | 63 DCHECK(initialized_); |
| 64 | 64 |
| 65 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); | 65 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); |
| 66 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), | 66 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), key_.data(), |
| 67 key_.data(), key_.size(), | 67 key_.size(), |
| 68 reinterpret_cast<const unsigned char*>(data.data()), | 68 reinterpret_cast<const unsigned char*>(data.data()), |
| 69 data.size(), result.safe_buffer(), NULL); | 69 data.size(), result.safe_buffer(), nullptr); |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool HMAC::Verify(const base::StringPiece& data, | 72 bool HMAC::Verify(const base::StringPiece& data, |
| 73 const base::StringPiece& digest) const { | 73 const base::StringPiece& digest) const { |
| 74 if (digest.size() != DigestLength()) | 74 if (digest.size() != DigestLength()) |
| 75 return false; | 75 return false; |
| 76 return VerifyTruncated(data, digest); | 76 return VerifyTruncated(data, digest); |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool HMAC::VerifyTruncated(const base::StringPiece& data, | 79 bool HMAC::VerifyTruncated(const base::StringPiece& data, |
| 80 const base::StringPiece& digest) const { | 80 const base::StringPiece& digest) const { |
| 81 if (digest.empty()) | 81 if (digest.empty()) |
| 82 return false; | 82 return false; |
| 83 size_t digest_length = DigestLength(); | 83 size_t digest_length = DigestLength(); |
| 84 std::unique_ptr<unsigned char[]> computed_digest( | 84 std::unique_ptr<unsigned char[]> computed_digest( |
| 85 new unsigned char[digest_length]); | 85 new unsigned char[digest_length]); |
| 86 if (!Sign(data, computed_digest.get(), digest_length)) | 86 if (!Sign(data, computed_digest.get(), digest_length)) |
| 87 return false; | 87 return false; |
| 88 | 88 |
| 89 return SecureMemEqual(digest.data(), computed_digest.get(), | 89 return SecureMemEqual(digest.data(), computed_digest.get(), |
| 90 std::min(digest.size(), digest_length)); | 90 std::min(digest.size(), digest_length)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 } // namespace crypto | 93 } // namespace crypto |
| OLD | NEW |