Skip to content

Commit

Permalink
Fix HMAC parameters parsing bug and add a test for this use case.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 586343621
Change-Id: I99f91924e825d5f380d57ed268efac7ee67a7779
  • Loading branch information
ioannanedelcu committed Nov 30, 2023
1 parent 1eb500e commit e23456d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tink/mac/hmac_proto_serialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ util::StatusOr<HmacParameters> ParseParameters(

util::StatusOr<HmacParameters::HashType> 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(),
Expand Down
21 changes: 21 additions & 0 deletions tink/mac/hmac_proto_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<internal::ProtoParametersSerialization> serialization =
internal::ProtoParametersSerialization::Create(
"type.googleapis.com/google.crypto.tink.HmacKey",
OutputPrefixType::RAW, key_format_proto.SerializeAsString());
ASSERT_THAT(serialization, IsOk());

util::StatusOr<std::unique_ptr<Parameters>> params =
internal::MutableSerializationRegistry::GlobalInstance().ParseParameters(
*serialization);
ASSERT_THAT(params.status(), StatusIs(absl::StatusCode::kInvalidArgument));
}

TEST_F(HmacProtoSerializationTest, ParseParametersWithUnkownOutputPrefix) {
ASSERT_THAT(RegisterHmacProtoSerialization(), IsOk());

Expand Down

0 comments on commit e23456d

Please sign in to comment.