Sync with Rust LDT changes

PiperOrigin-RevId: 490288278
This commit is contained in:
Janusz Sobczak
2022-11-22 11:16:21 -08:00
committed by Copybara-Service
parent a3b6058753
commit 9e61866ee3
3 changed files with 18 additions and 11 deletions
+15 -2
View File
@@ -29,6 +29,9 @@ namespace nearby {
namespace presence {
namespace {
// NP LDT library says that 0 is returned when `NpLdtCreate()` fails.
constexpr NpLdtHandle kInvalidLdtHandle = static_cast<NpLdtHandle>(0);
template <class T>
T FromStringView(absl::string_view data) {
T result{
@@ -54,7 +57,7 @@ NpLdtAesCipherHandle AesCreateCipher(NpLdtAes128Key key) {
int32_t AesCloseCipher(NpLdtAesCipherHandle handle) {
AesContext* ctx = reinterpret_cast<AesContext*>(handle);
free(ctx);
delete ctx;
return 0;
}
@@ -70,6 +73,16 @@ void AesDecrypt(NpLdtAesCipherHandle handle, NpLdtAesBlock* block) {
} // namespace
LdtEncryptor::LdtEncryptor(LdtEncryptor&& other)
: ldt_handle_(other.ldt_handle_) {
other.ldt_handle_ = kInvalidLdtHandle;
}
LdtEncryptor::~LdtEncryptor() {
if (ldt_handle_ != kInvalidLdtHandle) {
NpLdtClose(ldt_handle_);
}
}
absl::StatusOr<LdtEncryptor> LdtEncryptor::Create(
absl::string_view key_seed, absl::string_view known_hmac) {
NpLdtHandle handle =
@@ -79,7 +92,7 @@ absl::StatusOr<LdtEncryptor> LdtEncryptor::Create(
.decrypt = AesDecrypt},
FromStringView<NpLdtKeySeed>(key_seed),
FromStringView<NpMetadataKeyHmac>(known_hmac));
if (handle == nullptr) {
if (handle == kInvalidLdtHandle) {
return absl::UnavailableError("Failed to create LDT encryptor");
}
+2 -8
View File
@@ -29,19 +29,13 @@ namespace presence {
class LdtEncryptor {
public:
LdtEncryptor(const LdtEncryptor&) = delete;
LdtEncryptor(LdtEncryptor&& other) : ldt_handle_(other.ldt_handle_) {
other.ldt_handle_ = nullptr;
}
LdtEncryptor(LdtEncryptor&& other);
LdtEncryptor& operator=(const LdtEncryptor&) = delete;
LdtEncryptor& operator=(LdtEncryptor&& other) {
std::swap(ldt_handle_, other.ldt_handle_);
return *this;
}
~LdtEncryptor() {
if (ldt_handle_ != nullptr) {
NpLdtClose(ldt_handle_);
}
}
~LdtEncryptor();
// Creates an instance of `LdtEncryptor`.
// `key_seed` is used to generate LDT encryption and decryption keys.
+1 -1
View File
@@ -33,7 +33,7 @@ extern "C" {
// decrypting advertisements from a known origin
// Handle for accessing the rust ldt implementation apis
typedef void* NpLdtHandle;
typedef uint64_t NpLdtHandle;
// Key material from the Nearby Presence credential from which keys will be
// derived.