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/crypto/symmetric_key.h" | 5 #include "base/crypto/symmetric_key.h" |
6 | 6 |
7 #include <winsock2.h> // For htonl. | 7 #include <winsock2.h> // For htonl. |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 } // namespace | 305 } // namespace |
306 | 306 |
307 SymmetricKey::~SymmetricKey() { | 307 SymmetricKey::~SymmetricKey() { |
308 // TODO(wtc): create a "secure" string type that zeroes itself in the | 308 // TODO(wtc): create a "secure" string type that zeroes itself in the |
309 // destructor. | 309 // destructor. |
310 if (!raw_key_.empty()) | 310 if (!raw_key_.empty()) |
311 SecureZeroMemory(const_cast<char *>(raw_key_.data()), raw_key_.size()); | 311 SecureZeroMemory(const_cast<char *>(raw_key_.data()), raw_key_.size()); |
312 } | 312 } |
313 | 313 |
314 // static | 314 // static |
| 315 bool SymmetricKey::GenerateRandomBytes(size_t num_bytes, uint8* out) { |
| 316 if (num_bytes == 0) |
| 317 return true; |
| 318 if (out == NULL) |
| 319 return false; |
| 320 |
| 321 ScopedHCRYPTPROV provider; |
| 322 // See comment in GenerateAESKey as to why NULL is acceptable for the |
| 323 // container name. |
| 324 if (!CryptAcquireContext( |
| 325 provider.receive(), NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { |
| 326 return false; |
| 327 } |
| 328 if (CryptGenRandom(provider, num_bytes, out)) |
| 329 return true; |
| 330 else |
| 331 return false; |
| 332 } |
| 333 |
| 334 // static |
315 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 335 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
316 size_t key_size_in_bits) { | 336 size_t key_size_in_bits) { |
317 DCHECK_GE(key_size_in_bits, 8); | 337 DCHECK_GE(key_size_in_bits, 8); |
318 | 338 |
319 ScopedHCRYPTPROV provider; | 339 ScopedHCRYPTPROV provider; |
320 ScopedHCRYPTKEY key; | 340 ScopedHCRYPTKEY key; |
321 | 341 |
322 bool ok = false; | 342 bool ok = false; |
323 scoped_array<BYTE> raw_key; | 343 scoped_array<BYTE> raw_key; |
324 | 344 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 HCRYPTKEY key, | 547 HCRYPTKEY key, |
528 const void* key_data, size_t key_size_in_bytes) | 548 const void* key_data, size_t key_size_in_bytes) |
529 : provider_(provider), key_(key) { | 549 : provider_(provider), key_(key) { |
530 if (key_data) { | 550 if (key_data) { |
531 raw_key_.assign(reinterpret_cast<const char*>(key_data), | 551 raw_key_.assign(reinterpret_cast<const char*>(key_data), |
532 key_size_in_bytes); | 552 key_size_in_bytes); |
533 } | 553 } |
534 } | 554 } |
535 | 555 |
536 } // namespace base | 556 } // namespace base |
OLD | NEW |