Skip to content
Snippets Groups Projects
Commit 71885739 authored by Quan Nguyen's avatar Quan Nguyen Committed by Thai Duong
Browse files

Fix copy.bara errors

There will be more CLs coming.

Change-Id: Ia53b0f04068e1ea9733a4776b03a2604eca9a4b9
ORIGINAL_AUTHOR=Quan Nguyen <quannguyen@google.com>
GitOrigin-RevId: d7d51f01619c0f24edb21b8816216613d9f2ca43
parent 2cb18c2b
No related branches found
No related tags found
No related merge requests found
......@@ -20,18 +20,16 @@
#include "openssl/ec.h"
using google::protobuf::StringPiece;
using util::StatusOr;
using std::string;
namespace cloud {
namespace crypto {
namespace tink {
EciesHkdfRecipientKemBoringSsl::EciesHkdfRecipientKemBoringSsl(
EllipticCurveType curve, const string& priv)
EllipticCurveType curve, const std::string& priv)
: curve_(curve), priv_(priv) {}
StatusOr<string> EciesHkdfRecipientKemBoringSsl::GenerateKey(
util::StatusOr<std::string> EciesHkdfRecipientKemBoringSsl::GenerateKey(
StringPiece kem_bytes, HashType hash, StringPiece hkdf_salt,
StringPiece hkdf_info, int key_size_in_bytes,
EcPointFormat point_format) const {
......
......@@ -26,8 +26,6 @@ using google::cloud::crypto::tink::HashType;
using google::cloud::crypto::tink::EllipticCurveType;
using google::cloud::crypto::tink::EcPointFormat;
using google::protobuf::StringPiece;
using util::StatusOr;
using std::string;
namespace cloud {
namespace crypto {
......@@ -38,18 +36,19 @@ class EciesHkdfRecipientKemBoringSsl {
// Constructor based on elliptic curve type and private key. The private key
// is big-endian byte array.
explicit EciesHkdfRecipientKemBoringSsl(EllipticCurveType curve,
const string& priv_key);
const std::string& priv_key);
// Computes the ecdh's shared secret from our private key and peer's encoded
// public key, then uses hkdf to derive the symmetric key from the shared
// secret, hkdf info and hkdf salt.
StatusOr<string> GenerateKey(StringPiece kem_bytes, HashType hash,
StringPiece hkdf_salt, StringPiece hkdf_info,
int key_size_in_bytes,
EcPointFormat point_format) const;
util::StatusOr<std::string> GenerateKey(StringPiece kem_bytes, HashType hash,
StringPiece hkdf_salt,
StringPiece hkdf_info,
int key_size_in_bytes,
EcPointFormat point_format) const;
private:
EllipticCurveType curve_;
string priv_;
std::string priv_;
};
} // namespace tink
......
......@@ -32,12 +32,12 @@ struct TestVector {
EllipticCurveType curve;
HashType hash;
EcPointFormat point_format;
string pub_encoded_hex;
string priv_hex;
string salt_hex;
string info_hex;
std::string pub_encoded_hex;
std::string priv_hex;
std::string salt_hex;
std::string info_hex;
int out_len;
string out_key_hex;
std::string out_key_hex;
};
static const std::vector<TestVector> test_vector(
......
......@@ -23,20 +23,20 @@ namespace cloud {
namespace crypto {
namespace tink {
EciesHkdfSenderKemBoringSsl::KemKey::KemKey(const string& kem_bytes,
const string& symmetric_key)
EciesHkdfSenderKemBoringSsl::KemKey::KemKey(const std::string& kem_bytes,
const std::string& symmetric_key)
: kem_bytes_(kem_bytes), symmetric_key_(symmetric_key){};
string EciesHkdfSenderKemBoringSsl::KemKey::KemKey::get_kem_bytes() {
std::string EciesHkdfSenderKemBoringSsl::KemKey::KemKey::get_kem_bytes() {
return kem_bytes_;
}
string EciesHkdfSenderKemBoringSsl::KemKey::KemKey::get_symmetric_key() {
std::string EciesHkdfSenderKemBoringSsl::KemKey::KemKey::get_symmetric_key() {
return symmetric_key_;
}
EciesHkdfSenderKemBoringSsl::EciesHkdfSenderKemBoringSsl(
EllipticCurveType curve, const string& pubx, const string& puby)
EllipticCurveType curve, const std::string& pubx, const std::string& puby)
: curve_(curve), pubx_(pubx), puby_(puby), peer_pub_key_(nullptr) {
auto status_or_ec_point =
SubtleUtilBoringSSL::GetEcPoint(curve_, pubx_, puby_);
......@@ -45,13 +45,14 @@ EciesHkdfSenderKemBoringSsl::EciesHkdfSenderKemBoringSsl(
}
}
StatusOr<EciesHkdfSenderKemBoringSsl::KemKey>
util::StatusOr<EciesHkdfSenderKemBoringSsl::KemKey>
EciesHkdfSenderKemBoringSsl::GenerateKey(HashType hash, StringPiece hkdf_salt,
StringPiece hkdf_info,
int key_size_in_bytes,
EcPointFormat point_format) const {
if (peer_pub_key_.get() == nullptr) {
return Status(util::error::INTERNAL, "peer_pub_key_ wasn't initialized");
return util::Status(util::error::INTERNAL,
"peer_pub_key_ wasn't initialized");
}
auto status_or_ec_group = SubtleUtilBoringSSL::GetEcGroup(curve_);
......
......@@ -27,9 +27,6 @@ using google::cloud::crypto::tink::HashType;
using google::cloud::crypto::tink::EllipticCurveType;
using google::cloud::crypto::tink::EcPointFormat;
using google::protobuf::StringPiece;
using util::StatusOr;
using util::Status;
using std::string;
namespace cloud {
namespace crypto {
......@@ -40,32 +37,35 @@ class EciesHkdfSenderKemBoringSsl {
class KemKey {
public:
KemKey() {}
explicit KemKey(const string& kem_bytes, const string& symmetric_key);
string get_kem_bytes();
explicit KemKey(const std::string& kem_bytes,
const std::string& symmetric_key);
std::string get_kem_bytes();
string get_symmetric_key();
std::string get_symmetric_key();
private:
string kem_bytes_;
string symmetric_key_;
std::string kem_bytes_;
std::string symmetric_key_;
};
// Constructor based on elliptic curve type and peer's public key point. The
// public key's coordinates are big-endian byte array.
explicit EciesHkdfSenderKemBoringSsl(EllipticCurveType curve,
const string& pubx, const string& puby);
const std::string& pubx,
const std::string& puby);
// Generates ephemeral key pairs, computes ecdh's shared secret based on
// generated private key and peer's public key, then uses hkdf to derive the
// symmetric key from the shared secret, hkdf info and hkdf salt.
StatusOr<KemKey> GenerateKey(HashType hash, StringPiece hkdf_salt,
StringPiece hkdf_info, int key_size_in_bytes,
EcPointFormat point_format) const;
util::StatusOr<KemKey> GenerateKey(HashType hash, StringPiece hkdf_salt,
StringPiece hkdf_info,
int key_size_in_bytes,
EcPointFormat point_format) const;
private:
EllipticCurveType curve_;
string pubx_;
string puby_;
std::string pubx_;
std::string puby_;
bssl::UniquePtr<EC_POINT> peer_pub_key_;
};
......
......@@ -33,8 +33,8 @@ struct TestVector {
EllipticCurveType curve;
HashType hash;
EcPointFormat point_format;
string salt_hex;
string info_hex;
std::string salt_hex;
std::string info_hex;
int out_len;
};
......@@ -48,11 +48,12 @@ static const std::vector<TestVector> test_vector(
EcPointFormat::COMPRESSED, "0b0b0b0b", "0b0b0b0b0b0b0b0b", 32,
}});
string bn2str(const BIGNUM* bn) {
std::string bn2str(const BIGNUM* bn) {
size_t bn_size_in_bytes = BN_num_bytes(bn);
std::unique_ptr<uint8_t> res(new uint8_t[bn_size_in_bytes]);
BN_bn2bin(bn, &res.get()[0]);
return string(reinterpret_cast<const char*>(res.get()), bn_size_in_bytes);
return std::string(reinterpret_cast<const char*>(res.get()),
bn_size_in_bytes);
}
TEST_F(EciesHkdfSenderKemBoringSslTest, testSenderRecipientBasic) {
......@@ -68,15 +69,15 @@ TEST_F(EciesHkdfSenderKemBoringSslTest, testSenderRecipientBasic) {
bssl::UniquePtr<BIGNUM> pub1y_bn(BN_new());
EC_POINT_get_affine_coordinates_GFp(group.get(), pub1, pub1x_bn.get(),
pub1y_bn.get(), nullptr);
string pub1x_str = bn2str(pub1x_bn.get());
string pub1y_str = bn2str(pub1y_bn.get());
std::string pub1x_str = bn2str(pub1x_bn.get());
std::string pub1y_str = bn2str(pub1y_bn.get());
EciesHkdfSenderKemBoringSsl ecies_sender(test.curve, pub1x_str, pub1y_str);
auto status_or_kem_key = ecies_sender.GenerateKey(
test.hash, test::HexDecodeOrDie(test.salt_hex),
test::HexDecodeOrDie(test.info_hex), test.out_len, test.point_format);
EciesHkdfSenderKemBoringSsl::KemKey kem_key =
status_or_kem_key.ValueOrDie();
string priv1_str = bn2str(priv1);
std::string priv1_str = bn2str(priv1);
EciesHkdfRecipientKemBoringSsl ecies_recipient(test.curve, priv1_str);
auto status_or_shared_secret = ecies_recipient.GenerateKey(
kem_key.get_kem_bytes(), test.hash, test::HexDecodeOrDie(test.salt_hex),
......
......@@ -22,7 +22,7 @@ namespace crypto {
namespace tink {
// static
StatusOr<EC_GROUP *> SubtleUtilBoringSSL::GetEcGroup(
util::StatusOr<EC_GROUP *> SubtleUtilBoringSSL::GetEcGroup(
EllipticCurveType curve_type) {
switch (curve_type) {
case EllipticCurveType::NIST_P224:
......@@ -40,9 +40,8 @@ StatusOr<EC_GROUP *> SubtleUtilBoringSSL::GetEcGroup(
}
// static
StatusOr<EC_POINT *> SubtleUtilBoringSSL::GetEcPoint(EllipticCurveType curve,
StringPiece pubx,
StringPiece puby) {
util::StatusOr<EC_POINT *> SubtleUtilBoringSSL::GetEcPoint(
EllipticCurveType curve, StringPiece pubx, StringPiece puby) {
bssl::UniquePtr<BIGNUM> bn_x(
BN_bin2bn(reinterpret_cast<const unsigned char *>(pubx.data()),
pubx.size(), nullptr));
......@@ -67,7 +66,8 @@ StatusOr<EC_POINT *> SubtleUtilBoringSSL::GetEcPoint(EllipticCurveType curve,
}
// static
StatusOr<const EVP_MD *> SubtleUtilBoringSSL::EvpHash(HashType hash_type) {
util::StatusOr<const EVP_MD *> SubtleUtilBoringSSL::EvpHash(
HashType hash_type) {
switch (hash_type) {
case HashType::SHA1:
return EVP_sha1();
......@@ -83,7 +83,7 @@ StatusOr<const EVP_MD *> SubtleUtilBoringSSL::EvpHash(HashType hash_type) {
}
// static
StatusOr<string> SubtleUtilBoringSSL::ComputeEcdhSharedSecret(
util::StatusOr<std::string> SubtleUtilBoringSSL::ComputeEcdhSharedSecret(
EllipticCurveType curve, const BIGNUM *priv_key, const EC_POINT *pub_key) {
auto status_or_ec_group = SubtleUtilBoringSSL::GetEcGroup(curve);
if (!status_or_ec_group.ok()) {
......@@ -138,9 +138,8 @@ StatusOr<string> SubtleUtilBoringSSL::ComputeEcdhSharedSecret(
}
// static
StatusOr<EC_POINT *> SubtleUtilBoringSSL::EcPointDecode(EllipticCurveType curve,
EcPointFormat format,
StringPiece encoded) {
util::StatusOr<EC_POINT *> SubtleUtilBoringSSL::EcPointDecode(
EllipticCurveType curve, EcPointFormat format, StringPiece encoded) {
auto status_or_ec_group = GetEcGroup(curve);
if (!status_or_ec_group.ok()) {
return status_or_ec_group.status();
......@@ -184,9 +183,8 @@ StatusOr<EC_POINT *> SubtleUtilBoringSSL::EcPointDecode(EllipticCurveType curve,
}
// static
StatusOr<string> SubtleUtilBoringSSL::EcPointEncode(EllipticCurveType curve,
EcPointFormat format,
const EC_POINT *point) {
util::StatusOr<std::string> SubtleUtilBoringSSL::EcPointEncode(
EllipticCurveType curve, EcPointFormat format, const EC_POINT *point) {
auto status_or_ec_group = GetEcGroup(curve);
if (!status_or_ec_group.ok()) {
return status_or_ec_group.status();
......@@ -206,8 +204,8 @@ StatusOr<string> SubtleUtilBoringSSL::EcPointEncode(EllipticCurveType curve,
if (size != 1 + 2 * curve_size_in_bytes) {
return util::Status(util::error::INTERNAL, "EC_POINT_point2oct failed");
}
return string(reinterpret_cast<const char *>(encoded.get()),
1 + 2 * curve_size_in_bytes);
return std::string(reinterpret_cast<const char *>(encoded.get()),
1 + 2 * curve_size_in_bytes);
}
case EcPointFormat::COMPRESSED: {
std::unique_ptr<uint8_t> encoded(new uint8_t[1 + curve_size_in_bytes]);
......@@ -217,8 +215,8 @@ StatusOr<string> SubtleUtilBoringSSL::EcPointEncode(EllipticCurveType curve,
if (size != 1 + curve_size_in_bytes) {
return util::Status(util::error::INTERNAL, "EC_POINT_point2oct failed");
}
return string(reinterpret_cast<const char *>(encoded.get()),
1 + curve_size_in_bytes);
return std::string(reinterpret_cast<const char *>(encoded.get()),
1 + curve_size_in_bytes);
}
default:
return util::Status(util::error::INTERNAL, "Unsupported point format");
......
......@@ -28,8 +28,7 @@ using google::cloud::crypto::tink::HashType;
using google::cloud::crypto::tink::EllipticCurveType;
using google::cloud::crypto::tink::EcPointFormat;
using google::protobuf::StringPiece;
using util::StatusOr;
using std::string;
namespace cloud {
namespace crypto {
namespace tink {
......@@ -37,12 +36,13 @@ namespace tink {
class SubtleUtilBoringSSL {
public:
// Returns BoringSSL's EC_GROUP constructed from the curve type.
static StatusOr<EC_GROUP *> GetEcGroup(EllipticCurveType curve_type);
static util::StatusOr<EC_GROUP *> GetEcGroup(EllipticCurveType curve_type);
// Returns BoringSSL's EC_POINT constructed from the curve type, big-endian
// representation of public key's x-coordinate and y-coordinate.
static StatusOr<EC_POINT *> GetEcPoint(EllipticCurveType curve,
StringPiece pubx, StringPiece puby);
static util::StatusOr<EC_POINT *> GetEcPoint(EllipticCurveType curve,
StringPiece pubx,
StringPiece puby);
// Returns BoringSSL's EC_POINT constructed from curve type, point format and
// encoded public key's point. The uncompressed point is encoded as
......@@ -50,9 +50,9 @@ class SubtleUtilBoringSSL {
// The compressed point is encoded as 1-byte || x where x is
// curve_size_in_bytes big-endian byte array and if the least significant bit
// of y is 1, the 1st byte is 0x03, otherwise it's 0x02.
static StatusOr<EC_POINT *> EcPointDecode(EllipticCurveType curve,
EcPointFormat format,
StringPiece encoded);
static util::StatusOr<EC_POINT *> EcPointDecode(EllipticCurveType curve,
EcPointFormat format,
StringPiece encoded);
// Returns the encoded public key based on curve type, point format and
// BoringSSL's EC_POINT public key point. The uncompressed point is encoded as
......@@ -60,19 +60,18 @@ class SubtleUtilBoringSSL {
// The compressed point is encoded as 1-byte || x where x is
// curve_size_in_bytes big-endian byte array and if the least significant bit
// of y is 1, the 1st byte is 0x03, otherwise it's 0x02.
static StatusOr<string> EcPointEncode(EllipticCurveType curve,
EcPointFormat format,
const EC_POINT *point);
static util::StatusOr<std::string> EcPointEncode(EllipticCurveType curve,
EcPointFormat format,
const EC_POINT *point);
// Returns the ecdh's shared secret based on our private key and peer's public
// key. Returns error if the public key is not on private key's curve.
static StatusOr<string> ComputeEcdhSharedSecret(EllipticCurveType curve,
const BIGNUM *priv_key,
const EC_POINT *pub_key);
static util::StatusOr<std::string> ComputeEcdhSharedSecret(
EllipticCurveType curve, const BIGNUM *priv_key, const EC_POINT *pub_key);
// Returns an EVP structure for a hash function.
// The EVP_MD instances are sigletons owned by BoringSSL.
static StatusOr<const EVP_MD *> EvpHash(HashType hash_type);
static util::StatusOr<const EVP_MD *> EvpHash(HashType hash_type);
};
} // namespace tink
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment