| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "crypto/hmac.h" | 5 #include "crypto/hmac.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <wincrypt.h> | 8 #include <wincrypt.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // For HMAC-SHA-256 only. | 102 // For HMAC-SHA-256 only. |
| 103 std::vector<unsigned char> raw_key_; | 103 std::vector<unsigned char> raw_key_; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 HMAC::HMAC(HashAlgorithm hash_alg) | 106 HMAC::HMAC(HashAlgorithm hash_alg) |
| 107 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { | 107 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
| 108 // Only SHA-1 and SHA-256 hash algorithms are supported now. | 108 // Only SHA-1 and SHA-256 hash algorithms are supported now. |
| 109 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); | 109 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool HMAC::Init(const unsigned char* key, int key_length) { | 112 bool HMAC::Init(const unsigned char* key, size_t key_length) { |
| 113 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) { | 113 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) { |
| 114 // Init must not be called more than once on the same HMAC object. | 114 // Init must not be called more than once on the same HMAC object. |
| 115 NOTREACHED(); | 115 NOTREACHED(); |
| 116 return false; | 116 return false; |
| 117 } | 117 } |
| 118 | 118 |
| 119 if (hash_alg_ == SHA256) { | 119 if (hash_alg_ == SHA256) { |
| 120 plat_->raw_key_.assign(key, key + key_length); | 120 plat_->raw_key_.assign(key, key + key_length); |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 140 BYTE key_data[1]; | 140 BYTE key_data[1]; |
| 141 }; | 141 }; |
| 142 size_t key_blob_size = std::max(offsetof(KeyBlob, key_data) + key_length, | 142 size_t key_blob_size = std::max(offsetof(KeyBlob, key_data) + key_length, |
| 143 sizeof(KeyBlob)); | 143 sizeof(KeyBlob)); |
| 144 std::vector<BYTE> key_blob_storage = std::vector<BYTE>(key_blob_size); | 144 std::vector<BYTE> key_blob_storage = std::vector<BYTE>(key_blob_size); |
| 145 KeyBlob* key_blob = reinterpret_cast<KeyBlob*>(&key_blob_storage[0]); | 145 KeyBlob* key_blob = reinterpret_cast<KeyBlob*>(&key_blob_storage[0]); |
| 146 key_blob->header.bType = PLAINTEXTKEYBLOB; | 146 key_blob->header.bType = PLAINTEXTKEYBLOB; |
| 147 key_blob->header.bVersion = CUR_BLOB_VERSION; | 147 key_blob->header.bVersion = CUR_BLOB_VERSION; |
| 148 key_blob->header.reserved = 0; | 148 key_blob->header.reserved = 0; |
| 149 key_blob->header.aiKeyAlg = CALG_RC2; | 149 key_blob->header.aiKeyAlg = CALG_RC2; |
| 150 key_blob->key_size = key_length; | 150 key_blob->key_size = static_cast<DWORD>(key_length); |
| 151 memcpy(key_blob->key_data, key, key_length); | 151 memcpy(key_blob->key_data, key, key_length); |
| 152 | 152 |
| 153 if (!CryptImportKey(plat_->provider_, &key_blob_storage[0], | 153 if (!CryptImportKey(plat_->provider_, &key_blob_storage[0], |
| 154 (DWORD)key_blob_storage.size(), 0, | 154 (DWORD)key_blob_storage.size(), 0, |
| 155 CRYPT_IPSEC_HMAC_KEY, plat_->key_.receive())) { | 155 CRYPT_IPSEC_HMAC_KEY, plat_->key_.receive())) { |
| 156 NOTREACHED(); | 156 NOTREACHED(); |
| 157 return false; | 157 return false; |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Destroy the copy of the key. | 160 // Destroy the copy of the key. |
| 161 SecureZeroMemory(key_blob->key_data, key_length); | 161 SecureZeroMemory(key_blob->key_data, key_length); |
| 162 | 162 |
| 163 return true; | 163 return true; |
| 164 } | 164 } |
| 165 | 165 |
| 166 HMAC::~HMAC() { | 166 HMAC::~HMAC() { |
| 167 } | 167 } |
| 168 | 168 |
| 169 bool HMAC::Sign(const base::StringPiece& data, | 169 bool HMAC::Sign(const base::StringPiece& data, |
| 170 unsigned char* digest, | 170 unsigned char* digest, |
| 171 int digest_length) const { | 171 size_t digest_length) const { |
| 172 if (hash_alg_ == SHA256) { | 172 if (hash_alg_ == SHA256) { |
| 173 if (plat_->raw_key_.empty()) | 173 if (plat_->raw_key_.empty()) |
| 174 return false; | 174 return false; |
| 175 ComputeHMACSHA256(&plat_->raw_key_[0], plat_->raw_key_.size(), | 175 ComputeHMACSHA256(&plat_->raw_key_[0], plat_->raw_key_.size(), |
| 176 reinterpret_cast<const unsigned char*>(data.data()), | 176 reinterpret_cast<const unsigned char*>(data.data()), |
| 177 data.size(), digest, digest_length); | 177 data.size(), digest, digest_length); |
| 178 return true; | 178 return true; |
| 179 } | 179 } |
| 180 | 180 |
| 181 if (!plat_->provider_ || !plat_->key_) | 181 if (!plat_->provider_ || !plat_->key_) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 195 memset(&hmac_info, 0, sizeof(hmac_info)); | 195 memset(&hmac_info, 0, sizeof(hmac_info)); |
| 196 hmac_info.HashAlgid = CALG_SHA1; | 196 hmac_info.HashAlgid = CALG_SHA1; |
| 197 if (!CryptSetHashParam(hash, HP_HMAC_INFO, | 197 if (!CryptSetHashParam(hash, HP_HMAC_INFO, |
| 198 reinterpret_cast<BYTE*>(&hmac_info), 0)) | 198 reinterpret_cast<BYTE*>(&hmac_info), 0)) |
| 199 return false; | 199 return false; |
| 200 | 200 |
| 201 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()), | 201 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()), |
| 202 static_cast<DWORD>(data.size()), 0)) | 202 static_cast<DWORD>(data.size()), 0)) |
| 203 return false; | 203 return false; |
| 204 | 204 |
| 205 DWORD sha1_size = digest_length; | 205 DWORD sha1_size = static_cast<DWORD>(digest_length); |
| 206 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0); | 206 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0); |
| 207 } | 207 } |
| 208 | 208 |
| 209 } // namespace crypto | 209 } // namespace crypto |
| OLD | NEW |