From e23456d8564d050ea610ae2c9c065cd306e9859a Mon Sep 17 00:00:00 2001 From: Ioana Nedelcu Date: Wed, 29 Nov 2023 08:03:23 -0800 Subject: [PATCH] Fix HMAC parameters parsing bug and add a test for this use case. PiperOrigin-RevId: 586343621 Change-Id: I99f91924e825d5f380d57ed268efac7ee67a7779 --- tink/mac/hmac_proto_serialization.cc | 2 +- tink/mac/hmac_proto_serialization_test.cc | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tink/mac/hmac_proto_serialization.cc b/tink/mac/hmac_proto_serialization.cc index aaa6ced4..a52fadf9 100644 --- a/tink/mac/hmac_proto_serialization.cc +++ b/tink/mac/hmac_proto_serialization.cc @@ -156,7 +156,7 @@ util::StatusOr ParseParameters( util::StatusOr hash_type = ToHashType(proto_key_format.params().hash()); - if (!hash_type.ok()) return variant.status(); + if (!hash_type.ok()) return hash_type.status(); return HmacParameters::Create(proto_key_format.key_size(), proto_key_format.params().tag_size(), diff --git a/tink/mac/hmac_proto_serialization_test.cc b/tink/mac/hmac_proto_serialization_test.cc index 82a50f20..5663cf91 100644 --- a/tink/mac/hmac_proto_serialization_test.cc +++ b/tink/mac/hmac_proto_serialization_test.cc @@ -170,6 +170,27 @@ TEST_F(HmacProtoSerializationTest, ParseParametersWithInvalidVersion) { ASSERT_THAT(params.status(), StatusIs(absl::StatusCode::kInvalidArgument)); } +TEST_F(HmacProtoSerializationTest, ParseParametersWithUnknownHashType) { + ASSERT_THAT(RegisterHmacProtoSerialization(), IsOk()); + + HmacKeyFormat key_format_proto; + key_format_proto.set_key_size(16); + key_format_proto.set_version(0); + key_format_proto.mutable_params()->set_tag_size(10); + key_format_proto.mutable_params()->set_hash(HashType::UNKNOWN_HASH); + + util::StatusOr serialization = + internal::ProtoParametersSerialization::Create( + "type.googleapis.com/google.crypto.tink.HmacKey", + OutputPrefixType::RAW, key_format_proto.SerializeAsString()); + ASSERT_THAT(serialization, IsOk()); + + util::StatusOr> params = + internal::MutableSerializationRegistry::GlobalInstance().ParseParameters( + *serialization); + ASSERT_THAT(params.status(), StatusIs(absl::StatusCode::kInvalidArgument)); +} + TEST_F(HmacProtoSerializationTest, ParseParametersWithUnkownOutputPrefix) { ASSERT_THAT(RegisterHmacProtoSerialization(), IsOk());