Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update advertisement encoding logic to use the Rust NP library #2625

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/proto/credential.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ message SharedCredential {
bytes encrypted_metadata_bytes_v0 = 5;

// The tag for verifying metadata_encryption_key for a V0 advertisement.
bytes metadata_encryption_key_tag_v0 = 6;
bytes identity_token_tag_v0 = 6;

// The public key is used to create a secure connection with the device.
bytes connection_signature_verification_key = 7;
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/local_credential.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ message LocalCredential {
int64 end_time_millis = 4;

// The 14 bytes aes key to encrypt metadata in PublicCredential.
bytes metadata_encryption_key_v0 = 5;
bytes identity_token_v0 = 5;

// It is used for signing advertisement.
PrivateKey advertisement_signing_key = 6;
Expand Down
17 changes: 3 additions & 14 deletions presence/broadcast_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
#include <string>
#include <vector>

#include "absl/types/variant.h"
#include "internal/proto/credential.pb.h"
#include "presence/data_element.h"
#include "presence/power_mode.h"

namespace nearby {
namespace presence {

// Broadcast parameter for presence features.
struct PresenceBroadcast {
// Nearby Presence advertisement request options.
struct BroadcastRequest {
struct BroadcastSection {
// Presence identity type.
::nearby::internal::IdentityType identity =
Expand All @@ -52,24 +51,14 @@ struct PresenceBroadcast {
std::string manager_app_id;
};

std::vector<BroadcastSection> sections;
};

// Broadcast request for legacy Android T, which needs to provide credential
// and salt in the broadcast parameters.
// TODO(b/243443813) - Support Legacy Broadcast Request
struct LegacyPresenceBroadcast {};

// Nearby Presence advertisement request options.
struct BroadcastRequest {
// Calibrated TX power. The broadcast recipient uses it to calculate the
// distance between both devices.
int tx_power;

// The broadcast frequency hint.
PowerMode power_mode;

absl::variant<PresenceBroadcast, LegacyPresenceBroadcast> variant;
std::vector<BroadcastSection> sections;
};

} // namespace presence
Expand Down
19 changes: 13 additions & 6 deletions presence/implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ filegroup(
name = "presence_internal_common_srcs",
srcs = [
"action_factory.cc",
"advertisement_factory.cc",
"advertisement_filter.cc",
"base_broadcast_request.cc",
"broadcast_manager.cc",
Expand All @@ -36,7 +35,6 @@ filegroup(
"action_factory.h",
"advertisement_decoder.h",
"advertisement_decoder_impl.h",
"advertisement_factory.h",
"advertisement_filter.h",
"base_broadcast_request.h",
"broadcast_manager.h",
Expand All @@ -55,10 +53,12 @@ cc_library(
name = "internal",
srcs = [
"advertisement_decoder_rust_impl.cc",
"advertisement_factory.cc",
":presence_internal_common_srcs",
],
hdrs = [
"advertisement_decoder_rust_impl.h",
"advertisement_factory.h",
":presence_internal_common_hdrs",
],
defines = ["USE_RUST_DECODER=1"],
Expand Down Expand Up @@ -106,10 +106,12 @@ cc_library(
name = "internal_deprecated",
srcs = [
"advertisement_decoder_impl.cc",
"advertisement_factory_deprecated.cc",
":presence_internal_common_srcs",
],
hdrs = [
"advertisement_decoder_impl.h",
"advertisement_factory_deprecated.h",
":presence_internal_common_hdrs",
],
visibility = [
Expand Down Expand Up @@ -272,10 +274,10 @@ cc_test(
"//internal/proto:credential_cc_proto",
"//presence:types",
"//presence/implementation/mediums",
"@com_github_protobuf_matchers//protobuf-matchers",
"//testing/base/public:gunit_main_no_googleheapcheck",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
] + select({
"@platforms//os:windows": [
"//internal/platform/implementation/windows",
Expand All @@ -296,9 +298,12 @@ cc_test(
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/proto:credential_cc_proto",
"//presence:types",
"//presence/implementation/mediums",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
"//testing/base/public:gunit_main_no_googleheapcheck",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
] + select({
"@platforms//os:windows": [
"//internal/platform/implementation/windows",
Expand Down Expand Up @@ -338,6 +343,8 @@ cc_test(
"//internal/proto:credential_cc_proto",
"//presence:types",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:variant",
"@com_google_googletest//:gtest_main",
] + select({
Expand Down
4 changes: 3 additions & 1 deletion presence/implementation/action_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#include "presence/implementation/action_factory.h"

#include <algorithm>
#include <cstdint>
#include <vector>

#include "absl/types/optional.h"
#include "internal/platform/logging.h"
#include "presence/data_element.h"
#include "presence/implementation/base_broadcast_request.h"

namespace nearby {
namespace presence {
Expand Down
10 changes: 5 additions & 5 deletions presence/implementation/advertisement_decoder_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ constexpr uint8_t kDataTypeMask =
constexpr int kAdvertisementVersion = 0;

constexpr int kEncryptedIdentityAdditionalLength =
kSaltSize + kBaseMetadataSize;
kSaltSize + kV0IdentityTokenSize;
constexpr int kEddystoneAdditionalLength = 20;

uint8_t GetDataElementType(uint8_t header) { return header & kDataTypeMask; }
Expand Down Expand Up @@ -181,15 +181,15 @@ absl::StatusOr<std::string> DecryptLdt(
}
for (const auto& credential : credentials) {
absl::StatusOr<LdtEncryptor> encryptor = LdtEncryptor::Create(
credential.key_seed(), credential.metadata_encryption_key_tag_v0());
credential.key_seed(), credential.identity_token_tag_v0());
if (encryptor.ok()) {
absl::StatusOr<std::string> result =
encryptor->DecryptAndVerify(encrypted_contents, salt);
if (result.ok() && result->size() > kBaseMetadataSize) {
if (result.ok() && result->size() > kV0IdentityTokenSize) {
decoded_advertisement.public_credential = credential;
decoded_advertisement.metadata_key =
result->substr(0, kBaseMetadataSize);
return result->substr(kBaseMetadataSize);
result->substr(0, kV0IdentityTokenSize);
return result->substr(kV0IdentityTokenSize);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ TEST(AdvertisementDecoderImpl, DecodeEncryptedAdvertisement) {
0xB8, 0xEA, 0x67, 0xD1, 0x1C, 0x3E, 0x36, 0xFD});
SharedCredential public_credential;
public_credential.set_key_seed(seed.AsStringView());
public_credential.set_metadata_encryption_key_tag_v0(
public_credential.set_identity_token_tag_v0(
known_mac.AsStringView());
absl::flat_hash_map<IdentityType, std::vector<internal::SharedCredential>>
credentials;
Expand Down
2 changes: 1 addition & 1 deletion presence/implementation/advertisement_decoder_rust_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ AdvertisementDecoderImpl::InitializeCredentialBook(
std::array<uint8_t, 32> key_seed_array;
std::copy(key_seed.begin(), key_seed.end(), key_seed_array.data());

auto tag = credential.metadata_encryption_key_tag_v0();
auto tag = credential.identity_token_tag_v0();
std::array<uint8_t, 32> tag_array;
std::copy(tag.begin(), tag.end(), tag_array.data());

Expand Down
3 changes: 1 addition & 2 deletions presence/implementation/advertisement_decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ SharedCredential GetPublicCredential() {
0xCA, 0x25, 0x4C, 0x35, 0x54, 0xDC, 0xE5, 0x0E});
SharedCredential public_credential;
public_credential.set_key_seed(seed.AsStringView());
public_credential.set_metadata_encryption_key_tag_v0(
known_mac.AsStringView());
public_credential.set_identity_token_tag_v0(known_mac.AsStringView());
return public_credential;
}

Expand Down
Loading
Loading