From 68001ec8bc3bf7b5c93a3c44af0dd2886beb347f Mon Sep 17 00:00:00 2001 From: Elly <ellyjones@chromium.org> Date: Thu, 27 Mar 2025 08:23:05 -0700 Subject: [PATCH] crypto/symmetric_key: remove Delete crypto::SymmetricKey and its tests. This change also removes a few remaining includes of its header files. Fixed: 370724578 Change-Id: I9764215c4a27b3f012251b9354dae897dafc0d20 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6397925 Reviewed-by: Haihan Chen <haihan@google.com> Reviewed-by: Fergal Daly <fergal@chromium.org> Commit-Queue: Elly FJ <ellyjones@chromium.org> Cr-Commit-Position: refs/heads/main@{#1438762} --- .../enterprise/obfuscation/core/utils.cc | 1 - .../browser/cache_storage/cache_storage.cc | 2 - .../cache_storage/cache_storage_cache.cc | 1 - .../cache_storage_cache_unittest.cc | 1 - crypto/BUILD.gn | 3 - crypto/hmac.cc | 5 -- crypto/hmac.h | 7 -- crypto/kdf.h | 2 +- crypto/symmetric_key.cc | 64 ---------------- crypto/symmetric_key.h | 73 ------------------- crypto/symmetric_key_unittest.cc | 39 ---------- device/fido/mac/credential_metadata.h | 1 - 12 files changed, 1 insertion(+), 198 deletions(-) delete mode 100644 crypto/symmetric_key.cc delete mode 100644 crypto/symmetric_key.h delete mode 100644 crypto/symmetric_key_unittest.cc diff --git a/components/enterprise/obfuscation/core/utils.cc b/components/enterprise/obfuscation/core/utils.cc index 6c0d210cca3fa..f2e02f96605c1 100644 --- a/components/enterprise/obfuscation/core/utils.cc +++ b/components/enterprise/obfuscation/core/utils.cc @@ -17,7 +17,6 @@ #include "crypto/aead.h" #include "crypto/hkdf.h" #include "crypto/random.h" -#include "crypto/symmetric_key.h" namespace enterprise_obfuscation { diff --git a/content/browser/cache_storage/cache_storage.cc b/content/browser/cache_storage/cache_storage.cc index 9e9705ad29d48..ad6cf28ddc31c 100644 --- a/content/browser/cache_storage/cache_storage.cc +++ b/content/browser/cache_storage/cache_storage.cc @@ -48,7 +48,6 @@ #include "content/browser/cache_storage/cache_storage_scheduler.h" #include "content/browser/cache_storage/cache_storage_trace_utils.h" #include "content/common/background_fetch/background_fetch_types.h" -#include "crypto/symmetric_key.h" #include "net/base/directory_lister.h" #include "net/base/net_errors.h" #include "storage/browser/blob/blob_storage_context.h" @@ -58,7 +57,6 @@ using blink::mojom::CacheStorageError; using blink::mojom::StorageType; -using crypto::SymmetricKey; namespace content { diff --git a/content/browser/cache_storage/cache_storage_cache.cc b/content/browser/cache_storage/cache_storage_cache.cc index cf93bcd69ba29..fbb3431fc1d44 100644 --- a/content/browser/cache_storage/cache_storage_cache.cc +++ b/content/browser/cache_storage/cache_storage_cache.cc @@ -44,7 +44,6 @@ #include "content/browser/cache_storage/cache_storage_trace_utils.h" #include "content/common/background_fetch/background_fetch_types.h" #include "crypto/hmac.h" -#include "crypto/symmetric_key.h" #include "mojo/public/cpp/bindings/remote.h" #include "net/base/completion_repeating_callback.h" #include "net/base/io_buffer.h" diff --git a/content/browser/cache_storage/cache_storage_cache_unittest.cc b/content/browser/cache_storage/cache_storage_cache_unittest.cc index 0b90ffef18786..13678f6c8000b 100644 --- a/content/browser/cache_storage/cache_storage_cache_unittest.cc +++ b/content/browser/cache_storage/cache_storage_cache_unittest.cc @@ -53,7 +53,6 @@ #include "content/public/test/browser_task_environment.h" #include "content/public/test/test_browser_context.h" #include "content/public/test/test_utils.h" -#include "crypto/symmetric_key.h" #include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/system/data_pipe.h" diff --git a/crypto/BUILD.gn b/crypto/BUILD.gn index beab7ecc83a48..99d259450eeac 100644 --- a/crypto/BUILD.gn +++ b/crypto/BUILD.gn @@ -59,8 +59,6 @@ component("crypto") { "signature_verifier.h", "subtle_passkey.cc", "subtle_passkey.h", - "symmetric_key.cc", - "symmetric_key.h", "unexportable_key.cc", "unexportable_key.h", "unexportable_key_metrics.cc", @@ -187,7 +185,6 @@ test("crypto_unittests") { "sha2_unittest.cc", "signature_creator_unittest.cc", "signature_verifier_unittest.cc", - "symmetric_key_unittest.cc", "unexportable_key_unittest.cc", ] diff --git a/crypto/hmac.cc b/crypto/hmac.cc index e6a700bb87ed9..7143174e75d7e 100644 --- a/crypto/hmac.cc +++ b/crypto/hmac.cc @@ -20,7 +20,6 @@ #include "base/stl_util.h" #include "crypto/openssl_util.h" #include "crypto/secure_util.h" -#include "crypto/symmetric_key.h" #include "third_party/boringssl/src/include/openssl/hmac.h" namespace crypto { @@ -55,10 +54,6 @@ bool HMAC::Init(const unsigned char* key, size_t key_length) { return true; } -bool HMAC::Init(const SymmetricKey* key) { - return Init(key->key()); -} - bool HMAC::Sign(std::string_view data, unsigned char* digest, size_t digest_length) const { diff --git a/crypto/hmac.h b/crypto/hmac.h index ec534bf2f1fca..69308fe5e8711 100644 --- a/crypto/hmac.h +++ b/crypto/hmac.h @@ -22,9 +22,6 @@ namespace crypto { -// Simplify the interface and reduce includes by abstracting out the internals. -class SymmetricKey; - // TODO(https://issues.chromium.org/issues/374334448): Rework this interface and // delete much of it. class CRYPTO_EXPORT HMAC { @@ -59,10 +56,6 @@ class CRYPTO_EXPORT HMAC { // Init() may fail. [[nodiscard]] bool Init(const unsigned char* key, size_t key_length); - // Initializes this instance using |key|. Call Init - // only once. It returns false on the second or later calls. - [[nodiscard]] bool Init(const SymmetricKey* key); - // Initializes this instance using |key|. Call Init only once. It returns // false on the second or later calls. [[nodiscard]] bool Init(std::string_view key) { diff --git a/crypto/kdf.h b/crypto/kdf.h index 2823017355fa5..df0162835b595 100644 --- a/crypto/kdf.h +++ b/crypto/kdf.h @@ -5,9 +5,9 @@ #ifndef CRYPTO_KDF_H_ #define CRYPTO_KDF_H_ +#include "base/containers/span.h" #include "crypto/crypto_export.h" #include "crypto/subtle_passkey.h" -#include "crypto/symmetric_key.h" namespace crypto::kdf { diff --git a/crypto/symmetric_key.cc b/crypto/symmetric_key.cc deleted file mode 100644 index b03162818782e..0000000000000 --- a/crypto/symmetric_key.cc +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2011 The Chromium Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "crypto/symmetric_key.h" - -#include <stddef.h> -#include <stdint.h> - -#include <algorithm> -#include <utility> - -#include "crypto/openssl_util.h" -#include "crypto/random.h" - -namespace crypto { - -namespace { - -bool IsValidKeySize(size_t key_size_in_bytes) { - // Nobody should ever be using other symmetric key sizes without consulting - // with CRYPTO_OWNERS first, who can modify this check if need be. - return key_size_in_bytes == 16 || key_size_in_bytes == 32; -} - -} // namespace - -SymmetricKey::SymmetricKey(base::span<const uint8_t> key_bytes) - : key_(base::as_string_view(key_bytes)) {} - -SymmetricKey::SymmetricKey(const SymmetricKey& other) = default; -SymmetricKey& SymmetricKey::operator=(const SymmetricKey& other) = default; - -SymmetricKey::~SymmetricKey() { - std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. -} - -// static -std::unique_ptr<SymmetricKey> SymmetricKey::GenerateRandomKey( - Algorithm, - size_t key_size_in_bits) { - return std::make_unique<SymmetricKey>(RandomKey(key_size_in_bits)); -} - -// static -SymmetricKey SymmetricKey::RandomKey(size_t key_size_in_bits) { - CHECK(!(key_size_in_bits % 8)); - - const size_t key_size_in_bytes = key_size_in_bits / 8; - CHECK(IsValidKeySize(key_size_in_bytes)); - - return SymmetricKey(crypto::RandBytesAsVector(key_size_in_bytes)); -} - -// static -std::unique_ptr<SymmetricKey> SymmetricKey::Import(Algorithm, - const std::string& raw_key) { - if (!IsValidKeySize(raw_key.size())) { - return nullptr; - } - return std::make_unique<SymmetricKey>(base::as_byte_span(raw_key)); -} - -} // namespace crypto diff --git a/crypto/symmetric_key.h b/crypto/symmetric_key.h deleted file mode 100644 index 27020b4385c2a..0000000000000 --- a/crypto/symmetric_key.h +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2012 The Chromium Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CRYPTO_SYMMETRIC_KEY_H_ -#define CRYPTO_SYMMETRIC_KEY_H_ - -#include <stddef.h> - -#include <memory> -#include <string> - -#include "base/containers/span.h" -#include "build/build_config.h" -#include "crypto/crypto_export.h" - -namespace crypto { - -// A SymmetricKey is an array of bytes which is used for symmetric cryptography -// (encryption only). -// -// This whole type is deprecated: prefer to use raw std::array<uint8_t>, -// std::vector<uint8_t>, or base::span<uint8_t> instead. This type has no -// behavior or particular meaning. -// -// TODO(https://issues.chromium.org/issues/370724578): get rid of this. -class CRYPTO_EXPORT SymmetricKey { - public: - // Defines the algorithm that a key will be used with. - enum Algorithm { - AES, - }; - - SymmetricKey() = delete; - - // Wrap the given span of bytes as a SymmetricKey. - explicit SymmetricKey(base::span<const uint8_t> key_bytes); - virtual ~SymmetricKey(); - - SymmetricKey(const SymmetricKey&); - SymmetricKey& operator=(const SymmetricKey&); - - // Generates a random key suitable to be used with |algorithm| and of - // |key_size_in_bits| bits. |key_size_in_bits| must be a multiple of 8. - // - // Deprecated: use the value version below that does not take an algorithm. - static std::unique_ptr<SymmetricKey> GenerateRandomKey( - Algorithm algorithm, - size_t key_size_in_bits); - - static SymmetricKey RandomKey(size_t key_size_in_bits); - - // Imports an array of key bytes in |raw_key|. The raw key must be of a valid - // size - see IsValidKeySize() in the source for details, although in general - // you should not need to choose key sizes yourself. Returns nullptr if the - // key is not of valid size. - // - // Deprecated: use the regular constructor that accepts a span of bytes, and - // validate that the key is of whatever length your client code expects before - // doing so. - static std::unique_ptr<SymmetricKey> Import(Algorithm algorithm, - const std::string& raw_key); - - // Returns the internal key storage. - const std::string& key() const { return key_; } - - private: - std::string key_; -}; - -} // namespace crypto - -#endif // CRYPTO_SYMMETRIC_KEY_H_ diff --git a/crypto/symmetric_key_unittest.cc b/crypto/symmetric_key_unittest.cc deleted file mode 100644 index 5565b338c8f0f..0000000000000 --- a/crypto/symmetric_key_unittest.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2011 The Chromium Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "crypto/symmetric_key.h" - -#include <memory> -#include <string> - -#include "base/strings/string_number_conversions.h" -#include "base/strings/string_util.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(SymmetricKeyTest, GenerateRandomKey) { - std::unique_ptr<crypto::SymmetricKey> key( - crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); - ASSERT_TRUE(key); - EXPECT_EQ(32U, key->key().size()); - - // Do it again and check that the keys are different. - // (Note: this has a one-in-10^77 chance of failure!) - std::unique_ptr<crypto::SymmetricKey> key2( - crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); - ASSERT_TRUE(key2); - EXPECT_EQ(32U, key2->key().size()); - EXPECT_NE(key->key(), key2->key()); -} - -TEST(SymmetricKeyTest, ImportGeneratedKey) { - std::unique_ptr<crypto::SymmetricKey> key1( - crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); - ASSERT_TRUE(key1); - - std::unique_ptr<crypto::SymmetricKey> key2( - crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key1->key())); - ASSERT_TRUE(key2); - - EXPECT_EQ(key1->key(), key2->key()); -} diff --git a/device/fido/mac/credential_metadata.h b/device/fido/mac/credential_metadata.h index 38668b6de80c1..63cd8410ae67c 100644 --- a/device/fido/mac/credential_metadata.h +++ b/device/fido/mac/credential_metadata.h @@ -18,7 +18,6 @@ #include "base/feature_list.h" #include "crypto/aead.h" #include "crypto/hmac.h" -#include "crypto/symmetric_key.h" #include "device/fido/features.h" namespace device {