mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Provide a platform abstraction for RandBytes instead of swapping headers
The `RandBytes` functions were being placed in the `crypto` namespace, which collides with Chromium's namespace of the same name. Within, `RandBytes` was defined with almost-the-same API. Then in Chromium builds, the Chromium header would be used instead (though somewhat inconsistently). This creates a lot of pain for Chromium development as there's a third-party repository directly depending on headers from Chromium's source tree, and is against the third-party policies for that reason. There are a number of other headers that mirror Chromium and are swapped out in the Chromium build that will cause similar pain, such as: ``` include "crypto/aead.h" include "crypto/ec_private_key.h" include "crypto/hkdf.h" ``` This CL provides a template for how to get rid of these header swaps and give a platform abstraction in nearby instead. We provide a platform abstraction in `platform/crypto.h` (really in `platform/implementation/crypto.h`) which is implemented in `platform/implementation/shared/crypto.cc`. However that implementation is removed by `#ifdef` when in the Chromium build. Then, in the Chromium repo, we will add (separately) an implementation of the same abstraction in `//third_party/nearby/platform_impl` with GN rules to include it in the build. It will replace the implementation from the nearby repo. Copybara import of the project: -- 6ca8099 by danakj <danakj@chromium.org>: Provide a platform abstraction for RandBytes instead of swapping headers The `RandBytes` functions were being placed in the `crypto` namespace, which collides with Chromium's namespace of the same name. Within, `RandBytes` was defined with almost-the-same API. Then in Chromium builds, the Chromium header would be used instead (though somewhat inconsistently). This creates a lot of pain for Chromium development as there's a third-party repository directly depending on headers from Chromium's source tree, and is against the third-party policies for that reason. There are a number of other headers that mirror Chromium and are swapped out in the Chromium build that will cause similar pain, such as: ``` include "crypto/aead.h" include "crypto/ec_private_key.h" include "crypto/hkdf.h" ``` This CL provides a template for how to get rid of these header swaps and give a platform abstraction in nearby instead. We provide a platform abstraction in `platform/crypto.h` (really in `platform/implementation/crypto.h`) which is implemented in `platform/implementation/shared/crypto.cc`. However that implementation is removed by `#ifdef` when in the Chromium build. Then, in the Chromium repo, we will add (separately) an implementation of the same abstraction in `//third_party/nearby/platform_impl` with GN rules to include it in the build. It will replace the implementation from the nearby repo. -- 9c2654b by danakj <danakj@chromium.org>: Remove CryptoSpan, use absl::Span The header swapping of Chromium crypto libraries is problematic, but absl::Span will convert to base::span so there's no need for the typedef even without removing the header swapping yet. -- df1135d by danakj <danakj@chromium.org>: Add missing files -- ab18a15 by danakj <danakj@chromium.org>: Remove the random_unittest.cc from Swift build The file moved, so the Swift package needs its path updated. -- 2038f78 by danakj <danakj@chromium.org>: Combine crypto unittests into crypto_test.cc -- f7ad176 by danakj <danakj@chromium.org>: Add stdint and stddef includes for uint8_t and size_t -- 9f04590 by danakj <danakj@chromium.org>: Mark the shared crypto implementation compatable_with non_prod -- 18eeafd by danakj <danakj@chromium.org>: Add IWYU pragma for crypto implementation PiperOrigin-RevId: 632150866
This commit is contained in:
committed by
Copybara-Service
parent
112da1a737
commit
f26d25ed01
@@ -27,6 +27,7 @@
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
@@ -34,14 +35,13 @@
|
||||
#include "crypto/aead.h"
|
||||
#include "crypto/ec_private_key.h"
|
||||
#include "crypto/hkdf.h"
|
||||
#include "crypto/random.h"
|
||||
#else
|
||||
#include "internal/crypto_cros/aead.h"
|
||||
#include "internal/crypto_cros/ec_private_key.h"
|
||||
#include "internal/crypto_cros/hkdf.h"
|
||||
#include "internal/crypto_cros/random.h"
|
||||
#endif
|
||||
#include "internal/platform/base64_utils.h"
|
||||
#include "internal/platform/crypto.h"
|
||||
#include "internal/platform/future.h"
|
||||
#include "internal/platform/implementation/credential_callbacks.h"
|
||||
#include "internal/platform/implementation/crypto.h"
|
||||
@@ -214,8 +214,8 @@ CredentialManagerImpl::CreateLocalCredential(
|
||||
|
||||
// Creates an AES key to encrypt the whole broadcast.
|
||||
std::string secret_key(kAuthenticityKeyByteSize, 0);
|
||||
crypto::RandBytes(const_cast<std::string::value_type*>(secret_key.data()),
|
||||
secret_key.size());
|
||||
RandBytes(const_cast<std::string::value_type*>(secret_key.data()),
|
||||
secret_key.size());
|
||||
private_credential.set_key_seed(secret_key);
|
||||
|
||||
// Uses SHA-256 algorithm to generate the credential ID from the
|
||||
@@ -238,8 +238,8 @@ CredentialManagerImpl::CreateLocalCredential(
|
||||
std::string(private_key.begin(), private_key.end()));
|
||||
// Create an AES key to encrypt the device identity metadata.
|
||||
std::string metadata_key(kBaseMetadataSize, 0);
|
||||
crypto::RandBytes(const_cast<std::string::value_type*>(metadata_key.data()),
|
||||
metadata_key.size());
|
||||
RandBytes(const_cast<std::string::value_type*>(metadata_key.data()),
|
||||
metadata_key.size());
|
||||
private_credential.set_metadata_encryption_key_v0(metadata_key);
|
||||
|
||||
// Generate the public credential
|
||||
@@ -315,7 +315,7 @@ std::string CredentialManagerImpl::DecryptDeviceIdentityMetaData(
|
||||
auto result = aead.Open(encrypted_metadata_bytes,
|
||||
/*nonce=*/
|
||||
iv_bytes,
|
||||
/*additional_data=*/CryptoSpan<uint8_t>());
|
||||
/*additional_data=*/absl::Span<uint8_t>());
|
||||
|
||||
return std::string(result.value().begin(), result.value().end());
|
||||
}
|
||||
@@ -340,7 +340,7 @@ std::string CredentialManagerImpl::EncryptDeviceIdentityMetaData(
|
||||
auto encrypted = aead.Seal(metadata_bytes,
|
||||
/*nonce=*/
|
||||
iv_bytes,
|
||||
/*additional_data=*/CryptoSpan<uint8_t>());
|
||||
/*additional_data=*/absl::Span<uint8_t>());
|
||||
|
||||
return std::string(encrypted.begin(), encrypted.end());
|
||||
}
|
||||
@@ -350,8 +350,8 @@ std::vector<uint8_t> CredentialManagerImpl::ExtendMetadataEncryptionKey(
|
||||
return crypto::HkdfSha256(
|
||||
std::vector<uint8_t>(metadata_encryption_key.begin(),
|
||||
metadata_encryption_key.end()),
|
||||
/*salt=*/CryptoSpan<uint8_t>(),
|
||||
/*info=*/CryptoSpan<uint8_t>(), kNearbyPresenceNumBytesAesGcmKeySize);
|
||||
/*salt=*/absl::Span<uint8_t>(),
|
||||
/*info=*/absl::Span<uint8_t>(), kNearbyPresenceNumBytesAesGcmKeySize);
|
||||
}
|
||||
|
||||
void CredentialManagerImpl::GetLocalCredentials(
|
||||
|
||||
Reference in New Issue
Block a user