| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/hmac.h" | 5 #include "crypto/hmac.h" |
| 6 | 6 |
| 7 #include <nss.h> | 7 #include <nss.h> |
| 8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
| 9 | 9 |
| 10 #include "base/crypto/scoped_nss_types.h" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/nss_util.h" | 12 #include "crypto/nss_util.h" |
| 13 #include "crypto/scoped_nss_types.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace crypto { |
| 16 | 16 |
| 17 struct HMACPlatformData { | 17 struct HMACPlatformData { |
| 18 CK_MECHANISM_TYPE mechanism_; | 18 CK_MECHANISM_TYPE mechanism_; |
| 19 ScopedPK11Slot slot_; | 19 ScopedPK11Slot slot_; |
| 20 ScopedPK11SymKey sym_key_; | 20 ScopedPK11SymKey sym_key_; |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 HMAC::HMAC(HashAlgorithm hash_alg) | 23 HMAC::HMAC(HashAlgorithm hash_alg) |
| 24 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { | 24 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
| 25 // Only SHA-1 and SHA-256 hash algorithms are supported. | 25 // Only SHA-1 and SHA-256 hash algorithms are supported. |
| 26 switch (hash_alg_) { | 26 switch (hash_alg_) { |
| 27 case SHA1: | 27 case SHA1: |
| 28 plat_->mechanism_ = CKM_SHA_1_HMAC; | 28 plat_->mechanism_ = CKM_SHA_1_HMAC; |
| 29 break; | 29 break; |
| 30 case SHA256: | 30 case SHA256: |
| 31 plat_->mechanism_ = CKM_SHA256_HMAC; | 31 plat_->mechanism_ = CKM_SHA256_HMAC; |
| 32 break; | 32 break; |
| 33 default: | 33 default: |
| 34 NOTREACHED() << "Unsupported hash algorithm"; | 34 NOTREACHED() << "Unsupported hash algorithm"; |
| 35 break; | 35 break; |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 | 38 |
| 39 HMAC::~HMAC() { | 39 HMAC::~HMAC() { |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool HMAC::Init(const unsigned char *key, int key_length) { | 42 bool HMAC::Init(const unsigned char *key, int key_length) { |
| 43 base::EnsureNSSInit(); | 43 EnsureNSSInit(); |
| 44 | 44 |
| 45 if (plat_->slot_.get()) { | 45 if (plat_->slot_.get()) { |
| 46 // Init must not be called more than twice on the same HMAC object. | 46 // Init must not be called more than twice on the same HMAC object. |
| 47 NOTREACHED(); | 47 NOTREACHED(); |
| 48 return false; | 48 return false; |
| 49 } | 49 } |
| 50 | 50 |
| 51 plat_->slot_.reset(PK11_GetBestSlot(plat_->mechanism_, NULL)); | 51 plat_->slot_.reset(PK11_GetBestSlot(plat_->mechanism_, NULL)); |
| 52 if (!plat_->slot_.get()) { | 52 if (!plat_->slot_.get()) { |
| 53 NOTREACHED(); | 53 NOTREACHED(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 unsigned int len = 0; | 107 unsigned int len = 0; |
| 108 if (PK11_DigestFinal(context.get(), | 108 if (PK11_DigestFinal(context.get(), |
| 109 digest, &len, digest_length) != SECSuccess) { | 109 digest, &len, digest_length) != SECSuccess) { |
| 110 NOTREACHED(); | 110 NOTREACHED(); |
| 111 return false; | 111 return false; |
| 112 } | 112 } |
| 113 | 113 |
| 114 return true; | 114 return true; |
| 115 } | 115 } |
| 116 | 116 |
| 117 } // namespace base | 117 } // namespace crypto |
| OLD | NEW |