Skip to content

Commit

Permalink
Use either absl::WrapUnique or std::make_unique
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 564313494
Change-Id: I59eff17a14e104eab0b41714deacfefdf570ae8e
  • Loading branch information
morambro authored and Copybara-Service committed Sep 11, 2023
1 parent 4951d65 commit 203da87
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 46 deletions.
1 change: 1 addition & 0 deletions tink/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ cc_library(
"//tink/util:protobuf_helper",
"//tink/util:status",
"//tink/util:statusor",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
Expand Down
1 change: 1 addition & 0 deletions tink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ tink_cc_library(
binary_keyset_writer.h
DEPS
tink::core::keyset_writer
absl::memory
absl::status
absl::strings
tink::util::errors
Expand Down
1 change: 1 addition & 0 deletions tink/aead/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ cc_library(
"//tink/util:status",
"//tink/util:statusor",
"@com_google_absl//absl/base:endian",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
Expand Down
1 change: 1 addition & 0 deletions tink/aead/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ tink_cc_library(
kms_envelope_aead.h
DEPS
absl::endian
absl::memory
absl::status
absl::strings
tink::core::aead
Expand Down
4 changes: 2 additions & 2 deletions tink/aead/kms_envelope_aead.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <utility>

#include "absl/base/internal/endian.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -72,9 +73,8 @@ util::StatusOr<std::unique_ptr<Aead>> KmsEnvelopeAead::New(
}
auto km_result = Registry::get_key_manager<Aead>(dek_template.type_url());
if (!km_result.ok()) return km_result.status();
std::unique_ptr<Aead> envelope_aead(
return absl::WrapUnique(
new KmsEnvelopeAead(dek_template, std::move(remote_aead)));
return std::move(envelope_aead);
}

util::StatusOr<std::string> KmsEnvelopeAead::Encrypt(
Expand Down
4 changes: 2 additions & 2 deletions tink/core/binary_keyset_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <string>
#include <utility>

#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "tink/util/errors.h"
#include "tink/util/protobuf_helper.h"
Expand Down Expand Up @@ -60,9 +61,8 @@ util::StatusOr<std::unique_ptr<BinaryKeysetWriter>> BinaryKeysetWriter::New(
return util::Status(absl::StatusCode::kInvalidArgument,
"destination_stream must be non-null.");
}
std::unique_ptr<BinaryKeysetWriter> writer(
return absl::WrapUnique(
new BinaryKeysetWriter(std::move(destination_stream)));
return std::move(writer);
}

util::Status BinaryKeysetWriter::Write(const Keyset& keyset) {
Expand Down
7 changes: 2 additions & 5 deletions tink/core/json_keyset_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,13 @@ util::StatusOr<std::unique_ptr<KeysetReader>> JsonKeysetReader::New(
return util::Status(absl::StatusCode::kInvalidArgument,
"keyset_stream must be non-null.");
}
std::unique_ptr<KeysetReader> reader(
new JsonKeysetReader(std::move(keyset_stream)));
return std::move(reader);
return absl::WrapUnique(new JsonKeysetReader(std::move(keyset_stream)));
}

// static
util::StatusOr<std::unique_ptr<KeysetReader>> JsonKeysetReader::New(
absl::string_view serialized_keyset) {
std::unique_ptr<KeysetReader> reader(new JsonKeysetReader(serialized_keyset));
return std::move(reader);
return absl::WrapUnique(new JsonKeysetReader(serialized_keyset));
}

util::StatusOr<std::unique_ptr<Keyset>> JsonKeysetReader::Read() {
Expand Down
5 changes: 2 additions & 3 deletions tink/core/keyset_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ util::StatusOr<std::unique_ptr<Keyset::Key>> ExtractPublicKey(

util::StatusOr<std::unique_ptr<KeysetHandle>>
KeysetHandle::GetPublicKeysetHandle(const KeyGenConfiguration& config) const {
std::unique_ptr<Keyset> public_keyset(new Keyset());
auto public_keyset = std::make_unique<Keyset>();
for (const Keyset::Key& key : get_keyset().key()) {
auto public_key_result = ExtractPublicKey(key, config);
if (!public_key_result.ok()) return public_key_result.status();
Expand All @@ -424,9 +424,8 @@ KeysetHandle::GetPublicKeysetHandle(const KeyGenConfiguration& config) const {
return util::Status(absl::StatusCode::kInternal,
"Error converting keyset proto into key entries.");
}
std::unique_ptr<KeysetHandle> handle(
return absl::WrapUnique<KeysetHandle>(
new KeysetHandle(std::move(public_keyset), *entries));
return std::move(handle);
}

util::StatusOr<std::unique_ptr<KeysetHandle>>
Expand Down
7 changes: 3 additions & 4 deletions tink/streamingaead/decrypting_input_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ StatusOr<std::unique_ptr<InputStream>> DecryptingInputStream::New(
std::shared_ptr<PrimitiveSet<StreamingAead>> primitives,
std::unique_ptr<crypto::tink::InputStream> ciphertext_source,
absl::string_view associated_data) {
std::unique_ptr<DecryptingInputStream> dec_stream(
new DecryptingInputStream());
auto dec_stream = absl::WrapUnique(new DecryptingInputStream());
dec_stream->primitives_ = primitives;
dec_stream->buffered_ct_source_ =
std::make_shared<BufferedInputStream>(std::move(ciphertext_source));
Expand All @@ -75,8 +74,8 @@ util::StatusOr<int> DecryptingInputStream::Next(const void** data) {

for (const StreamingAeadEntry* entry : all_primitives) {
StreamingAead& streaming_aead = entry->get_primitive();
auto shared_ct = absl::make_unique<SharedInputStream>(
buffered_ct_source_.get());
auto shared_ct =
std::make_unique<SharedInputStream>(buffered_ct_source_.get());
auto decrypting_stream_result = streaming_aead.NewDecryptingStream(
std::move(shared_ct), associated_data_);
if (decrypting_stream_result.ok()) {
Expand Down
1 change: 1 addition & 0 deletions tink/subtle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ cc_library(
"//tink/internal:test_file_util",
"//tink/util:status",
"//tink/util:statusor",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@rapidjson",
Expand Down
1 change: 1 addition & 0 deletions tink/subtle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ tink_cc_library(
wycheproof_util.h
DEPS
tink::subtle::common_enums
absl::log
absl::status
absl::strings
rapidjson
Expand Down
20 changes: 8 additions & 12 deletions tink/subtle/ecies_hkdf_sender_kem_boringssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ EciesHkdfNistPCurveSendKemBoringSsl::New(subtle::EllipticCurveType curve,

auto status_or_ec_point = internal::GetEcPoint(curve, pubx, puby);
if (!status_or_ec_point.ok()) return status_or_ec_point.status();
std::unique_ptr<const EciesHkdfSenderKemBoringSsl> sender_kem(
new EciesHkdfNistPCurveSendKemBoringSsl(
curve, pubx, puby, std::move(status_or_ec_point.value())));
return std::move(sender_kem);
return absl::WrapUnique(new EciesHkdfNistPCurveSendKemBoringSsl(
curve, pubx, puby, std::move(status_or_ec_point.value())));
}

util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl::KemKey>>
Expand Down Expand Up @@ -160,9 +158,8 @@ EciesHkdfX25519SendKemBoringSsl::New(subtle::EllipticCurveType curve,
return util::Status(absl::StatusCode::kInternal,
"EVP_PKEY_new_raw_public_key failed");
}
std::unique_ptr<const EciesHkdfSenderKemBoringSsl> sender_kem(
return absl::WrapUnique(
new EciesHkdfX25519SendKemBoringSsl(std::move(peer_public_key)));
return std::move(sender_kem);
}

util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl::KemKey>>
Expand Down Expand Up @@ -197,16 +194,15 @@ EciesHkdfX25519SendKemBoringSsl::GenerateKey(
reinterpret_cast<const char*>((*ephemeral_key)->public_value),
internal::X25519KeyPubKeySize());

util::StatusOr<util::SecretData> symmetric_key_or =
util::StatusOr<util::SecretData> symmetric_key =
Hkdf::ComputeEciesHkdfSymmetricKey(hash, public_key, *shared_secret,
hkdf_salt, hkdf_info,
key_size_in_bytes);
if (!symmetric_key_or.ok()) {
return symmetric_key_or.status();
if (!symmetric_key.ok()) {
return symmetric_key.status();
}
util::SecretData symmetric_key = *symmetric_key_or;
return absl::make_unique<const KemKey>(std::string(public_key),
symmetric_key);
return std::make_unique<const KemKey>(std::string(public_key),
*symmetric_key);
}

} // namespace subtle
Expand Down
9 changes: 4 additions & 5 deletions tink/subtle/wycheproof_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
#include <ostream>
#include <string>

#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "include/rapidjson/document.h"
#include "include/rapidjson/istreamwrapper.h"
#include "include/rapidjson/rapidjson.h"
#include "tink/internal/test_file_util.h"
#include "tink/subtle/common_enums.h"
#include "tink/util/status.h"
Expand Down Expand Up @@ -84,12 +86,9 @@ std::unique_ptr<rapidjson::Document> WycheproofUtil::ReadTestVectors(
std::ifstream input_stream;
input_stream.open(test_vectors_path);
rapidjson::IStreamWrapper input(input_stream);
std::unique_ptr<rapidjson::Document> root(
new rapidjson::Document(rapidjson::kObjectType));
auto root = std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
if (root->ParseStream(input).HasParseError()) {
std::cerr << "Failure parsing of test vectors from "
<< test_vectors_path << std::endl;
exit(1);
LOG(FATAL) << "Failure parsing of test vectors from " << test_vectors_path;
}
return root;
}
Expand Down
1 change: 1 addition & 0 deletions tink/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ cc_library(
"//tink:hybrid_encrypt",
"//tink:input_stream",
"//tink:keyset_handle",
"//tink:keyset_writer",
"//tink:kms_client",
"//tink:mac",
"//tink:output_stream",
Expand Down
1 change: 1 addition & 0 deletions tink/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ tink_cc_library(
tink::core::hybrid_encrypt
tink::core::input_stream
tink::core::keyset_handle
tink::core::keyset_writer
tink::core::kms_client
tink::core::mac
tink::core::output_stream
Expand Down
8 changes: 3 additions & 5 deletions tink/util/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ EciesAeadHkdfPrivateKey GetEciesAesGcmHkdfTestKey(
AesGcmKeyFormat key_format;
key_format.set_key_size(aes_gcm_key_size);
auto aead_dem = params->mutable_dem_params()->mutable_aead_dem();
std::unique_ptr<AesGcmKeyManager> key_manager(new AesGcmKeyManager());
auto key_manager = std::make_unique<AesGcmKeyManager>();
std::string dem_key_type = key_manager->get_key_type();
aead_dem->set_type_url(dem_key_type);
aead_dem->set_value(key_format.SerializeAsString());
Expand Down Expand Up @@ -265,8 +265,7 @@ EciesAeadHkdfPrivateKey GetEciesAesCtrHmacHkdfTestKey(
auto params = ecies_key.mutable_public_key()->mutable_params();
auto aead_dem = params->mutable_dem_params()->mutable_aead_dem();

std::unique_ptr<AesCtrHmacAeadKeyManager> key_manager(
new AesCtrHmacAeadKeyManager());
auto key_manager = std::make_unique<AesCtrHmacAeadKeyManager>();
std::string dem_key_type = key_manager->get_key_type();
aead_dem->set_type_url(dem_key_type);
aead_dem->set_value(key_format.SerializeAsString());
Expand All @@ -283,8 +282,7 @@ EciesAeadHkdfPrivateKey GetEciesXChaCha20Poly1305HkdfTestKey(

google::crypto::tink::XChaCha20Poly1305KeyFormat key_format;
auto aead_dem = params->mutable_dem_params()->mutable_aead_dem();
std::unique_ptr<XChaCha20Poly1305KeyManager> key_manager(
new XChaCha20Poly1305KeyManager());
auto key_manager = std::make_unique<XChaCha20Poly1305KeyManager>();
std::string dem_key_type = key_manager->get_key_type();
aead_dem->set_type_url(dem_key_type);
aead_dem->set_value(key_format.SerializeAsString());
Expand Down
16 changes: 8 additions & 8 deletions tink/util/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <utility>

#include "absl/base/thread_annotations.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/cord.h"
#include "absl/strings/match.h"
Expand All @@ -37,6 +38,7 @@
#include "tink/hybrid_encrypt.h"
#include "tink/input_stream.h"
#include "tink/keyset_handle.h"
#include "tink/keyset_writer.h"
#include "tink/kms_client.h"
#include "tink/mac.h"
#include "tink/output_stream.h"
Expand Down Expand Up @@ -686,21 +688,19 @@ class DummyStatefulMac : public subtle::StatefulMac {
// A dummy implementation of KeysetWriter-interface.
class DummyKeysetWriter : public KeysetWriter {
public:
static crypto::tink::util::StatusOr<std::unique_ptr<DummyKeysetWriter>> New(
static util::StatusOr<std::unique_ptr<DummyKeysetWriter>> New(
std::unique_ptr<std::ostream> destination_stream) {
std::unique_ptr<DummyKeysetWriter> writer(
return absl::WrapUnique(
new DummyKeysetWriter(std::move(destination_stream)));
return std::move(writer);
}

crypto::tink::util::Status Write(
const google::crypto::tink::Keyset& keyset) override {
return crypto::tink::util::OkStatus();
util::Status Write(const google::crypto::tink::Keyset& keyset) override {
return util::OkStatus();
}

crypto::tink::util::Status Write(
util::Status Write(
const google::crypto::tink::EncryptedKeyset& encrypted_keyset) override {
return crypto::tink::util::OkStatus();
return util::OkStatus();
}

private:
Expand Down

0 comments on commit 203da87

Please sign in to comment.