0

Use RandBytesAsVector

Replaces

std::vector<uint8_t> var(len);
crypto::RandBytes(var);

with

std::vector<uint8_t> var = crypto::RandBytesAsVector(len);

Bug: N/A
Change-Id: Ic0b05cff1d5547ad2e006649851f33f129ce13e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5856042
Commit-Queue: Alex Gough <ajgo@chromium.org>
Reviewed-by: Nina Satragno <nsatragno@chromium.org>
Reviewed-by: Dominique Fauteux-Chapleau <domfc@chromium.org>
Reviewed-by: Will Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1354872}
This commit is contained in:
Alex Gough
2024-09-12 22:55:06 +00:00
committed by Chromium LUCI CQ
parent 1c16c77f54
commit 79d992f3bd
4 changed files with 15 additions and 25 deletions
chrome/browser/os_crypt
components
enterprise
obfuscation
os_crypt
device/fido/enclave

@ -173,9 +173,8 @@ void AppBoundEncryptionProviderWin::GetKey(KeyCallback callback) {
return;
}
std::vector<uint8_t> random_key(
const auto random_key = crypto::RandBytesAsVector(
os_crypt_async::Encryptor::Key::kAES256GCMKeySize);
crypto::RandBytes(random_key);
// Take a copy of the key. This will be returned as the unencrypted key for
// the provider, once the encryption operation is complete.
std::vector<uint8_t> decrypted_key(random_key.cbegin(), random_key.cend());

@ -41,11 +41,8 @@ static constexpr size_t kChunkSizePrefixSize = 4u;
// Generates a random 256 bit AES key.
const std::vector<uint8_t>& GetSymmetricKey() {
static const base::NoDestructor<std::vector<uint8_t>> kSymmetricKey([]() {
std::vector<uint8_t> key(kKeySize);
crypto::RandBytes(key);
return key;
}());
static const base::NoDestructor<std::vector<uint8_t>> kSymmetricKey(
crypto::RandBytesAsVector(kKeySize));
return *kSymmetricKey;
}

@ -121,14 +121,11 @@ class EncryptorTestBase : public ::testing::Test {
return Encryptor(std::move(keys), provider_for_encryption);
}
static std::vector<uint8_t> GenerateRandomTestKey(size_t length) {
return crypto::RandBytesAsVector(length);
}
static Encryptor::Key GenerateRandomAES256TestKey(
bool is_os_crypt_sync_compatible = false) {
Encryptor::Key key(GenerateRandomTestKey(Encryptor::Key::kAES256GCMKeySize),
mojom::Algorithm::kAES256GCM);
Encryptor::Key key(
crypto::RandBytesAsVector(Encryptor::Key::kAES256GCMKeySize),
mojom::Algorithm::kAES256GCM);
key.is_os_crypt_sync_compatible_ = is_os_crypt_sync_compatible;
return key;
}
@ -564,7 +561,7 @@ TEST_F(EncryptorTestBase, IsEncryptionAvailable) {
TEST_F(EncryptorTestBase, AlgorithmDecryptCompatibility) {
std::string ciphertext;
std::string ciphertext16;
auto random_key = GenerateRandomTestKey(kKeyLength);
const auto random_key = crypto::RandBytesAsVector(kKeyLength);
// Set the OSCrypt key to the fixed key.
OSCrypt::SetRawEncryptionKey(
std::string(random_key.begin(), random_key.end()));
@ -612,7 +609,7 @@ TEST_F(EncryptorTestBase, AlgorithmDecryptCompatibility) {
// OSCrypt.
TEST_F(EncryptorTestBase, AlgorithmEncryptCompatibility) {
// From os_crypt_win.cc
auto random_key = GenerateRandomTestKey(kKeyLength);
const auto random_key = crypto::RandBytesAsVector(kKeyLength);
// Set up a test Encryptor that can encrypt the data.
Encryptor::KeyRing key_ring;
@ -753,11 +750,10 @@ class EncryptorTraitsTest : public EncryptorTestBase {};
TEST_F(EncryptorTraitsTest, TraitsRoundTrip) {
{
std::vector<uint8_t> test_key1(Encryptor::Key::kAES256GCMKeySize);
crypto::RandBytes(test_key1);
std::vector<uint8_t> test_key2(Encryptor::Key::kAES256GCMKeySize);
crypto::RandBytes(test_key2);
const auto test_key1 =
crypto::RandBytesAsVector(Encryptor::Key::kAES256GCMKeySize);
const auto test_key2 =
crypto::RandBytesAsVector(Encryptor::Key::kAES256GCMKeySize);
Encryptor::KeyRing key_ring;
key_ring.emplace("TEST1",

@ -389,11 +389,9 @@ ParseMakeCredentialResponse(cbor::Value response_value,
}
}
std::vector<uint8_t> credential_id(kCredentialIdSize);
crypto::RandBytes(credential_id);
std::vector<uint8_t> sync_id(kSyncIdSize);
crypto::RandBytes(sync_id);
std::vector<uint8_t> credential_id =
crypto::RandBytesAsVector(kCredentialIdSize);
std::vector<uint8_t> sync_id = crypto::RandBytesAsVector(kSyncIdSize);
sync_pb::WebauthnCredentialSpecifics entity;