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 "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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 } | 97 } |
98 | 98 |
99 bool HMAC::Init(const unsigned char* key, int key_length) { | 99 bool HMAC::Init(const unsigned char* key, int key_length) { |
100 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) { | 100 if (plat_->provider_ || plat_->key_ || !plat_->raw_key_.empty()) { |
101 // Init must not be called more than once on the same HMAC object. | 101 // Init must not be called more than once on the same HMAC object. |
102 NOTREACHED(); | 102 NOTREACHED(); |
103 return false; | 103 return false; |
104 } | 104 } |
105 | 105 |
106 if (hash_alg_ == SHA256) { | 106 if (hash_alg_ == SHA256) { |
107 if (key_length < SHA256_LENGTH / 2) | |
108 return false; // Key is too short. | |
109 plat_->raw_key_.assign(key, key + key_length); | 107 plat_->raw_key_.assign(key, key + key_length); |
110 return true; | 108 return true; |
111 } | 109 } |
112 | 110 |
113 if (!CryptAcquireContext(plat_->provider_.receive(), NULL, NULL, | 111 if (!CryptAcquireContext(plat_->provider_.receive(), NULL, NULL, |
114 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { | 112 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { |
115 NOTREACHED(); | 113 NOTREACHED(); |
116 return false; | 114 return false; |
117 } | 115 } |
118 | 116 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 | 187 |
190 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()), | 188 if (!CryptHashData(hash, reinterpret_cast<const BYTE*>(data.data()), |
191 static_cast<DWORD>(data.size()), 0)) | 189 static_cast<DWORD>(data.size()), 0)) |
192 return false; | 190 return false; |
193 | 191 |
194 DWORD sha1_size = digest_length; | 192 DWORD sha1_size = digest_length; |
195 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0); | 193 return !!CryptGetHashParam(hash, HP_HASHVAL, digest, &sha1_size, 0); |
196 } | 194 } |
197 | 195 |
198 } // namespace crypto | 196 } // namespace crypto |
OLD | NEW |