summaryrefslogtreecommitdiffstats
path: root/base/hmac_win.cc
diff options
context:
space:
mode:
authorwtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-05 23:55:59 +0000
committerwtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-05 23:55:59 +0000
commitd91f84376fe8dd249770ac19b7c08f8fcc20f446 (patch)
tree8d66a665eac06063bfcf50236fca05f2e4f8f1ea /base/hmac_win.cc
parenta92b86497b6ee02c99a2e4b4a8cf7fb458ab6310 (diff)
downloadchromium_src-d91f84376fe8dd249770ac19b7c08f8fcc20f446.zip
chromium_src-d91f84376fe8dd249770ac19b7c08f8fcc20f446.tar.gz
chromium_src-d91f84376fe8dd249770ac19b7c08f8fcc20f446.tar.bz2
Separate the key setting code in the constructor of HMAC class into the Init
method. Overload the Init method for char* and std::string. Add DCHECKs to the destruction methods in ~HMAC in hmac_win.cc. The patch is written by Takeshi Yoshino <tyoshino@google.com>. Original code review: http://codereview.chromium.org/88062 R=wtc http://crbug.com/2297 TEST=base_unittests should pass. Safe browsing should continue to work. Review URL: http://codereview.chromium.org/113001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15353 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/hmac_win.cc')
-rw-r--r--base/hmac_win.cc41
1 files changed, 33 insertions, 8 deletions
diff --git a/base/hmac_win.cc b/base/hmac_win.cc
index d927ac1..2b2e9cc 100644
--- a/base/hmac_win.cc
+++ b/base/hmac_win.cc
@@ -21,11 +21,25 @@ struct HMACPlatformData {
HCRYPTKEY hkey_;
};
-HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length)
+HMAC::HMAC(HashAlgorithm hash_alg)
: hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
+ // Only SHA-1 digest is supported now.
+ DCHECK(hash_alg_ == SHA1);
+}
+
+bool HMAC::Init(const unsigned char *key, int key_length) {
+ if (plat_->provider_ || plat_->hkey_) {
+ // Init must not be called more than once on the same HMAC object.
+ NOTREACHED();
+ return false;
+ }
+
if (!CryptAcquireContext(&plat_->provider_, NULL, NULL,
- PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
+ PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
+ NOTREACHED();
plat_->provider_ = NULL;
+ return false;
+ }
// This code doesn't work on Win2k because PLAINTEXTKEYBLOB and
// CRYPT_IPSEC_HMAC_KEY are not supported on Windows 2000. PLAINTEXTKEYBLOB
@@ -53,20 +67,31 @@ HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length)
if (!CryptImportKey(plat_->provider_, &key_blob_storage[0],
key_blob_storage.size(), 0, CRYPT_IPSEC_HMAC_KEY,
&plat_->hkey_)) {
+ NOTREACHED();
plat_->hkey_ = NULL;
+ return false;
}
// Destroy the copy of the key.
SecureZeroMemory(key_blob->key_data, key_length);
+
+ return true;
}
HMAC::~HMAC() {
- if (plat_->hkey_)
- CryptDestroyKey(plat_->hkey_);
- if (plat_->hash_)
- CryptDestroyHash(plat_->hash_);
- if (plat_->provider_)
- CryptReleaseContext(plat_->provider_, 0);
+ BOOL ok;
+ if (plat_->hkey_) {
+ ok = CryptDestroyKey(plat_->hkey_);
+ DCHECK(ok);
+ }
+ if (plat_->hash_) {
+ ok = CryptDestroyHash(plat_->hash_);
+ DCHECK(ok);
+ }
+ if (plat_->provider_) {
+ ok = CryptReleaseContext(plat_->provider_, 0);
+ DCHECK(ok);
+ }
}
bool HMAC::Sign(const std::string& data,