diff options
author | Karn Seth <karn@google.com> | 2021-01-14 16:06:54 +0000 |
---|---|---|
committer | Karn Seth <karn@google.com> | 2021-01-14 16:06:54 +0000 |
commit | 842f43b08cecba36f8e6c2d94d7467c3b7338397 (patch) | |
tree | ba6f4a46fc7b69ac045a249c9c802f16560f5bd3 | |
parent | 52c605f88b976d3ec386b09af0e72dec1e40d9a4 (diff) | |
download | private-join-and-compute-842f43b08cecba36f8e6c2d94d7467c3b7338397.tar.gz |
adds hash_type to ec_point_util
-rw-r--r-- | crypto/BUILD | 1 | ||||
-rw-r--r-- | crypto/ec_commutative_cipher.h | 3 | ||||
-rw-r--r-- | crypto/ec_point_util.cc | 19 | ||||
-rw-r--r-- | crypto/ec_point_util.h | 6 |
4 files changed, 24 insertions, 5 deletions
diff --git a/crypto/BUILD b/crypto/BUILD index 94e186f..58656be 100644 --- a/crypto/BUILD +++ b/crypto/BUILD @@ -140,6 +140,7 @@ cc_library( ], deps = [ ":bn_util", + ":ec_commutative_cipher", ":ec_util", "//util:status_includes", "@com_github_glog_glog//:glog", diff --git a/crypto/ec_commutative_cipher.h b/crypto/ec_commutative_cipher.h index 09b3a2d..c5f840d 100644 --- a/crypto/ec_commutative_cipher.h +++ b/crypto/ec_commutative_cipher.h @@ -87,7 +87,8 @@ namespace private_join_and_compute { class ECCommutativeCipher { public: - // The hash function used by the ECCommutativeCipher. + // The hash function used by the ECCommutativeCipher in order to hash strings + // to EC curve points. The suggested default is the SHA256 option. enum HashType { SHA256, SHA512, diff --git a/crypto/ec_point_util.cc b/crypto/ec_point_util.cc index 494b975..784c7a8 100644 --- a/crypto/ec_point_util.cc +++ b/crypto/ec_point_util.cc @@ -23,6 +23,7 @@ #include "crypto/ec_group.h" #include "crypto/ec_point.h" #include "util/status.inc" +#include "crypto/ec_commutative_cipher.h" #include "absl/strings/string_view.h" namespace private_join_and_compute { @@ -43,9 +44,21 @@ StatusOr<std::string> ECPointUtil::GetRandomCurvePoint() { return point.ToBytesCompressed(); } -StatusOr<std::string> ECPointUtil::HashToCurve(absl::string_view input) { - ASSIGN_OR_RETURN(ECPoint point, group_.GetPointByHashingToCurveSha512(input)); - return point.ToBytesCompressed(); +StatusOr<std::string> ECPointUtil::HashToCurve( + absl::string_view input, ECCommutativeCipher::HashType hash_type) { + if (hash_type == ECCommutativeCipher::HashType::SHA512) { + ASSIGN_OR_RETURN(ECPoint point, + group_.GetPointByHashingToCurveSha512(input)); + return point.ToBytesCompressed(); + } + + if (hash_type == ECCommutativeCipher::HashType::SHA256) { + ASSIGN_OR_RETURN(ECPoint point, + group_.GetPointByHashingToCurveSha256(input)); + return point.ToBytesCompressed(); + } + + return InvalidArgumentError("Invalid hash type."); } bool ECPointUtil::IsCurvePoint(absl::string_view input) { diff --git a/crypto/ec_point_util.h b/crypto/ec_point_util.h index ef2b53c..467c80c 100644 --- a/crypto/ec_point_util.h +++ b/crypto/ec_point_util.h @@ -25,6 +25,7 @@ #include "crypto/ec_group.h" #include "crypto/ec_point.h" #include "util/status.inc" +#include "crypto/ec_commutative_cipher.h" #include "absl/strings/string_view.h" namespace private_join_and_compute { @@ -47,7 +48,10 @@ class ECPointUtil { StatusOr<std::string> GetRandomCurvePoint(); // Hashes the given string to the curve. - StatusOr<std::string> HashToCurve(absl::string_view input); + // + // Suggested default hash_type is ECCommutativeCipher::HashType::Sha256. + StatusOr<std::string> HashToCurve(absl::string_view input, + ECCommutativeCipher::HashType hash_type); // Checks if a string represents a curve point. // May give a false negative if an internal error occurs. |