summaryrefslogtreecommitdiffstats
path: root/base/hmac_win.cc
diff options
context:
space:
mode:
authordeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-08 13:58:10 +0000
committerdeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-08 13:58:10 +0000
commitfbcfafe4cd6d4f69ad0e1e3d921fb05bb5fcadfd (patch)
tree546a86df37312e589e6a9c0da1e94a42b40b0e36 /base/hmac_win.cc
parent9795ec11c994c7750a19aeab79cbf094df0d90dd (diff)
downloadchromium_src-fbcfafe4cd6d4f69ad0e1e3d921fb05bb5fcadfd.zip
chromium_src-fbcfafe4cd6d4f69ad0e1e3d921fb05bb5fcadfd.tar.gz
chromium_src-fbcfafe4cd6d4f69ad0e1e3d921fb05bb5fcadfd.tar.bz2
Remove the ifdef platform jungle from hmac.h, abstracting the platform specific data into a platform specific heap-allocated structure.
Review URL: http://codereview.chromium.org/1614 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1836 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/hmac_win.cc')
-rw-r--r--base/hmac_win.cc88
1 files changed, 43 insertions, 45 deletions
diff --git a/base/hmac_win.cc b/base/hmac_win.cc
index 83ec26b..d611993 100644
--- a/base/hmac_win.cc
+++ b/base/hmac_win.cc
@@ -4,6 +4,9 @@
#include "base/hmac.h"
+#include <windows.h>
+#include <wincrypt.h>
+
#include <algorithm>
#include <vector>
@@ -11,42 +14,19 @@
namespace base {
+struct HMACPlatformData {
+ // Windows Crypt API resources.
+ HCRYPTPROV provider_;
+ HCRYPTHASH hash_;
+ HCRYPTKEY hkey_;
+};
+
HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length)
- : hash_alg_(hash_alg),
- provider_(NULL),
- hash_(NULL),
- hkey_(NULL) {
- if (!CryptAcquireContext(&provider_, NULL, NULL,
+ : hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
+ if (!CryptAcquireContext(&plat_->provider_, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
- provider_ = NULL;
- ImportKey(key, key_length);
-}
-
-HMAC::~HMAC() {
- if (hkey_)
- CryptDestroyKey(hkey_);
- if (hash_)
- CryptDestroyHash(hash_);
- if (provider_)
- CryptReleaseContext(provider_, 0);
-}
-
-bool HMAC::Sign(const std::string& data,
- unsigned char* digest,
- int digest_length) {
- if (!provider_ || !hkey_)
- return false;
-
- switch (hash_alg_) {
- case SHA1:
- return SignWithSHA1(data, digest, digest_length);
- default:
- NOTREACHED();
- return false;
- }
-}
+ plat_->provider_ = NULL;
-void HMAC::ImportKey(const unsigned char* key, int key_length) {
// This code doesn't work on Win2k because PLAINTEXTKEYBLOB and
// CRYPT_IPSEC_HMAC_KEY are not supported on Windows 2000. PLAINTEXTKEYBLOB
// allows the import of an unencrypted key. For Win2k support, a cubmbersome
@@ -70,38 +50,56 @@ void HMAC::ImportKey(const unsigned char* key, int key_length) {
key_blob->key_size = key_length;
memcpy(key_blob->key_data, key, key_length);
- if (!CryptImportKey(provider_, &key_blob_storage[0], key_blob_storage.size(),
- 0, CRYPT_IPSEC_HMAC_KEY, &hkey_)) {
- hkey_ = NULL;
+ if (!CryptImportKey(plat_->provider_, &key_blob_storage[0],
+ key_blob_storage.size(), 0, CRYPT_IPSEC_HMAC_KEY,
+ &plat_->hkey_)) {
+ plat_->hkey_ = NULL;
}
// Destroy the copy of the key.
SecureZeroMemory(key_blob->key_data, key_length);
}
-bool HMAC::SignWithSHA1(const std::string& data,
- unsigned char* digest,
- int digest_length) {
- DCHECK(provider_);
- DCHECK(hkey_);
+HMAC::~HMAC() {
+ if (plat_->hkey_)
+ CryptDestroyKey(plat_->hkey_);
+ if (plat_->hash_)
+ CryptDestroyHash(plat_->hash_);
+ if (plat_->provider_)
+ CryptReleaseContext(plat_->provider_, 0);
+
+ delete plat_;
+}
+
+bool HMAC::Sign(const std::string& data,
+ unsigned char* digest,
+ int digest_length) {
+ if (!plat_->provider_ || !plat_->hkey_)
+ return false;
+
+ if (hash_alg_ != SHA1) {
+ NOTREACHED();
+ return false;
+ }
- if (!CryptCreateHash(provider_, CALG_HMAC, hkey_, 0, &hash_))
+ if (!CryptCreateHash(
+ plat_->provider_, CALG_HMAC, plat_->hkey_, 0, &plat_->hash_))
return false;
HMAC_INFO hmac_info;
memset(&hmac_info, 0, sizeof(hmac_info));
hmac_info.HashAlgid = CALG_SHA1;
- if (!CryptSetHashParam(hash_, HP_HMAC_INFO,
+ if (!CryptSetHashParam(plat_->hash_, HP_HMAC_INFO,
reinterpret_cast<BYTE*>(&hmac_info), 0))
return false;
- if (!CryptHashData(hash_,
+ if (!CryptHashData(plat_->hash_,
reinterpret_cast<const BYTE*>(data.data()),
static_cast<DWORD>(data.size()), 0))
return false;
DWORD sha1_size = digest_length;
- if (!CryptGetHashParam(hash_, HP_HASHVAL, digest, &sha1_size, 0))
+ if (!CryptGetHashParam(plat_->hash_, HP_HASHVAL, digest, &sha1_size, 0))
return false;
return true;