| Index: base/crypto/symmetric_key_mac.cc
|
| diff --git a/base/crypto/symmetric_key_mac.cc b/base/crypto/symmetric_key_mac.cc
|
| index 574f9d28e28b23aed3f0ea20ed5b8f4a059910d9..862cb0d1511b062603a57a20a5d80ef6b71e0dce 100644
|
| --- a/base/crypto/symmetric_key_mac.cc
|
| +++ b/base/crypto/symmetric_key_mac.cc
|
| @@ -1,4 +1,4 @@
|
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| @@ -32,13 +32,33 @@ CSSM_KEY_TYPE CheckKeyParams(base::SymmetricKey::Algorithm algorithm,
|
| }
|
| }
|
|
|
| -void* CreateRandomBytes(size_t size) {
|
| +inline CSSM_DATA StringToData(const std::string& str) {
|
| + CSSM_DATA data = {
|
| + str.size(),
|
| + reinterpret_cast<uint8_t*>(const_cast<char*>(str.data()))
|
| + };
|
| + return data;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace base {
|
| +
|
| +SymmetricKey::~SymmetricKey() {}
|
| +
|
| +// static
|
| +bool SymmetricKey::GenerateRandomBytes(size_t num_bytes, uint8* out) {
|
| + if (num_bytes == 0)
|
| + return true;
|
| + if (out == NULL)
|
| + return false;
|
| +
|
| CSSM_RETURN err;
|
| CSSM_CC_HANDLE ctx;
|
| err = CSSM_CSP_CreateRandomGenContext(base::GetSharedCSPHandle(),
|
| CSSM_ALGID_APPLE_YARROW,
|
| NULL,
|
| - size, &ctx);
|
| + num_bytes, &ctx);
|
| if (err) {
|
| base::LogCSSMError("CSSM_CSP_CreateRandomGenContext", err);
|
| return NULL;
|
| @@ -50,32 +70,20 @@ void* CreateRandomBytes(size_t size) {
|
| random_data.Data = NULL;
|
| }
|
| CSSM_DeleteContext(ctx);
|
| - return random_data.Data; // Caller responsible for freeing this
|
| -}
|
| -
|
| -inline CSSM_DATA StringToData(const std::string& str) {
|
| - CSSM_DATA data = {
|
| - str.size(),
|
| - reinterpret_cast<uint8_t*>(const_cast<char*>(str.data()))
|
| - };
|
| - return data;
|
| + std::copy(random_data.Data, random_data.Data + num_bytes, out);
|
| + delete random_data.Data;
|
| + return true;
|
| }
|
|
|
| -} // namespace
|
| -
|
| -namespace base {
|
| -
|
| -SymmetricKey::~SymmetricKey() {}
|
| -
|
| // static
|
| SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
|
| size_t key_size_in_bits) {
|
| CheckKeyParams(algorithm, key_size_in_bits);
|
| - void* random_bytes = CreateRandomBytes((key_size_in_bits + 7) / 8);
|
| - if (!random_bytes)
|
| + std::vector<uint8> random_bytes((key_size_in_bits + 7) / 8);
|
| + if (!GenerateRandomBytes(random_bytes.size(), &random_bytes[0]))
|
| return NULL;
|
| - SymmetricKey *key = new SymmetricKey(random_bytes, key_size_in_bits);
|
| - free(random_bytes);
|
| + SymmetricKey* key = new SymmetricKey(&random_bytes[0], key_size_in_bits);
|
| + std::fill(random_bytes.begin(), random_bytes.end(), 0u);
|
| return key;
|
| }
|
|
|
|
|