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

Unified Diff: base/hmac_nss.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_mac.cc ('k') | base/hmac_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/hmac_nss.cc
===================================================================
--- base/hmac_nss.cc (revision 15317)
+++ base/hmac_nss.cc (working copy)
@@ -42,14 +42,31 @@
ScopedNSSSymKey sym_key_;
};
-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) {
base::EnsureNSSInit();
+ if (hash_alg_ != SHA1) {
+ NOTREACHED();
+ return false;
+ }
+
+ if (plat_->slot_.get() || plat_->slot_.get()) {
+ // Init must not be called more than twice on the same HMAC object.
+ NOTREACHED();
+ return false;
+ }
+
plat_->slot_.reset(PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL));
- CHECK(plat_->slot_.get());
+ if (!plat_->slot_.get()) {
+ NOTREACHED();
+ return false;
+ }
SECItem key_item;
key_item.type = siBuffer;
@@ -62,7 +79,12 @@
CKA_SIGN,
&key_item,
NULL));
- CHECK(plat_->sym_key_.get());
+ if (!plat_->sym_key_.get()) {
+ NOTREACHED();
+ return false;
+ }
+
+ return true;
}
HMAC::~HMAC() {
@@ -71,6 +93,12 @@
bool HMAC::Sign(const std::string& data,
unsigned char* digest,
int digest_length) {
+ if (!plat_->sym_key_.get()) {
+ // Init has not been called before Sign.
+ NOTREACHED();
+ return false;
+ }
+
SECItem param = { siBuffer, NULL, 0 };
ScopedNSSContext context(PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
CKA_SIGN,
« no previous file with comments | « base/hmac_mac.cc ('k') | base/hmac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698