Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Unified Diff: base/hmac_win.cc

Issue 88062: Separate the initialization code in the constructor of HMAC class into Init f... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/hmac_unittest.cc ('k') | chrome/browser/safe_browsing/safe_browsing_util.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/hmac_win.cc
===================================================================
--- base/hmac_win.cc (revision 15317)
+++ base/hmac_win.cc (working copy)
@@ -21,11 +21,25 @@
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 @@
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,
« no previous file with comments | « base/hmac_unittest.cc ('k') | chrome/browser/safe_browsing/safe_browsing_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698