diff options
author | lizatretyakova <lizatretyakova@google.com> | 2023-07-26 08:48:01 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-07-26 08:49:03 -0700 |
commit | 2df7af936e4eaa2a37c0af67599f5bec7473e74c (patch) | |
tree | 98cd98ae5bf8d54379149dae4b7da0cbd2a4e0ca | |
parent | 366baf7bc977d9bab82a4af6f2590d00812d3cf0 (diff) | |
download | tink-2df7af936e4eaa2a37c0af67599f5bec7473e74c.tar.gz |
Make use of the new utility function for proto conversions.
PiperOrigin-RevId: 551218413
3 files changed, 55 insertions, 80 deletions
diff --git a/java_src/src/main/java/com/google/crypto/tink/mac/internal/BUILD.bazel b/java_src/src/main/java/com/google/crypto/tink/mac/internal/BUILD.bazel index 634f029cd..a09438772 100644 --- a/java_src/src/main/java/com/google/crypto/tink/mac/internal/BUILD.bazel +++ b/java_src/src/main/java/com/google/crypto/tink/mac/internal/BUILD.bazel @@ -249,6 +249,7 @@ android_library( "//proto:tink_java_proto_lite", "//src/main/java/com/google/crypto/tink:accesses_partial_key-android", "//src/main/java/com/google/crypto/tink:secret_key_access-android", + "//src/main/java/com/google/crypto/tink/internal:enum_type_proto_converter-android", "//src/main/java/com/google/crypto/tink/internal:key_parser-android", "//src/main/java/com/google/crypto/tink/internal:key_serializer-android", "//src/main/java/com/google/crypto/tink/internal:mutable_serialization_registry-android", @@ -275,6 +276,7 @@ java_library( "//proto:tink_java_proto", "//src/main/java/com/google/crypto/tink:accesses_partial_key", "//src/main/java/com/google/crypto/tink:secret_key_access", + "//src/main/java/com/google/crypto/tink/internal:enum_type_proto_converter", "//src/main/java/com/google/crypto/tink/internal:key_parser", "//src/main/java/com/google/crypto/tink/internal:key_serializer", "//src/main/java/com/google/crypto/tink/internal:mutable_serialization_registry", diff --git a/java_src/src/main/java/com/google/crypto/tink/mac/internal/HmacProtoSerialization.java b/java_src/src/main/java/com/google/crypto/tink/mac/internal/HmacProtoSerialization.java index c537df0c6..a68f26e14 100644 --- a/java_src/src/main/java/com/google/crypto/tink/mac/internal/HmacProtoSerialization.java +++ b/java_src/src/main/java/com/google/crypto/tink/mac/internal/HmacProtoSerialization.java @@ -20,6 +20,7 @@ import static com.google.crypto.tink.internal.Util.toBytesFromPrintableAscii; import com.google.crypto.tink.AccessesPartialKey; import com.google.crypto.tink.SecretKeyAccess; +import com.google.crypto.tink.internal.EnumTypeProtoConverter; import com.google.crypto.tink.internal.KeyParser; import com.google.crypto.tink.internal.KeySerializer; import com.google.crypto.tink.internal.MutableSerializationRegistry; @@ -47,6 +48,23 @@ import javax.annotation.Nullable; public final class HmacProtoSerialization { private static final String TYPE_URL = "type.googleapis.com/google.crypto.tink.HmacKey"; private static final Bytes TYPE_URL_BYTES = toBytesFromPrintableAscii(TYPE_URL); + private static final EnumTypeProtoConverter<OutputPrefixType, HmacParameters.Variant> + OUTPUT_PREFIX_TYPE_CONVERTER = + EnumTypeProtoConverter.<OutputPrefixType, HmacParameters.Variant>builder() + .add(OutputPrefixType.RAW, HmacParameters.Variant.NO_PREFIX) + .add(OutputPrefixType.TINK, HmacParameters.Variant.TINK) + .add(OutputPrefixType.LEGACY, HmacParameters.Variant.LEGACY) + .add(OutputPrefixType.CRUNCHY, HmacParameters.Variant.CRUNCHY) + .build(); + private static final EnumTypeProtoConverter<HashType, HmacParameters.HashType> + HASH_TYPE_CONVERTER = + EnumTypeProtoConverter.<HashType, HmacParameters.HashType>builder() + .add(HashType.SHA1, HmacParameters.HashType.SHA1) + .add(HashType.SHA224, HmacParameters.HashType.SHA224) + .add(HashType.SHA256, HmacParameters.HashType.SHA256) + .add(HashType.SHA384, HmacParameters.HashType.SHA384) + .add(HashType.SHA512, HmacParameters.HashType.SHA512) + .build(); private static final ParametersSerializer<HmacParameters, ProtoParametersSerialization> PARAMETERS_SERIALIZER = @@ -69,84 +87,11 @@ public final class HmacProtoSerialization { KeyParser.create( HmacProtoSerialization::parseKey, TYPE_URL_BYTES, ProtoKeySerialization.class); - private static OutputPrefixType toProtoOutputPrefixType(HmacParameters.Variant variant) - throws GeneralSecurityException { - if (HmacParameters.Variant.TINK.equals(variant)) { - return OutputPrefixType.TINK; - } - if (HmacParameters.Variant.CRUNCHY.equals(variant)) { - return OutputPrefixType.CRUNCHY; - } - if (HmacParameters.Variant.NO_PREFIX.equals(variant)) { - return OutputPrefixType.RAW; - } - if (HmacParameters.Variant.LEGACY.equals(variant)) { - return OutputPrefixType.LEGACY; - } - throw new GeneralSecurityException("Unable to serialize variant: " + variant); - } - - private static HashType toProtoHashType(HmacParameters.HashType hashType) - throws GeneralSecurityException { - if (HmacParameters.HashType.SHA1.equals(hashType)) { - return HashType.SHA1; - } - if (HmacParameters.HashType.SHA224.equals(hashType)) { - return HashType.SHA224; - } - if (HmacParameters.HashType.SHA256.equals(hashType)) { - return HashType.SHA256; - } - if (HmacParameters.HashType.SHA384.equals(hashType)) { - return HashType.SHA384; - } - if (HmacParameters.HashType.SHA512.equals(hashType)) { - return HashType.SHA512; - } - throw new GeneralSecurityException("Unable to serialize HashType " + hashType); - } - - private static HmacParameters.HashType toHashType(HashType hashType) - throws GeneralSecurityException { - switch (hashType) { - case SHA1: - return HmacParameters.HashType.SHA1; - case SHA224: - return HmacParameters.HashType.SHA224; - case SHA256: - return HmacParameters.HashType.SHA256; - case SHA384: - return HmacParameters.HashType.SHA384; - case SHA512: - return HmacParameters.HashType.SHA512; - default: - throw new GeneralSecurityException( - "Unable to parse HashType: " + hashType.getNumber()); - } - } - - private static HmacParameters.Variant toVariant(OutputPrefixType outputPrefixType) - throws GeneralSecurityException { - switch (outputPrefixType) { - case TINK: - return HmacParameters.Variant.TINK; - case CRUNCHY: - return HmacParameters.Variant.CRUNCHY; - case LEGACY: - return HmacParameters.Variant.LEGACY; - case RAW: - return HmacParameters.Variant.NO_PREFIX; - default: - throw new GeneralSecurityException( - "Unable to parse OutputPrefixType: " + outputPrefixType.getNumber()); - } - } - private static com.google.crypto.tink.proto.HmacParams getProtoParams(HmacParameters parameters) throws GeneralSecurityException { return com.google.crypto.tink.proto.HmacParams.newBuilder() .setTagSize(parameters.getCryptographicTagSizeBytes()) - .setHash(toProtoHashType(parameters.getHashType())) + .setHash(HASH_TYPE_CONVERTER.toProtoEnum(parameters.getHashType())) .build(); } @@ -161,7 +106,7 @@ public final class HmacProtoSerialization { .setKeySize(parameters.getKeySizeBytes()) .build() .toByteString()) - .setOutputPrefixType(toProtoOutputPrefixType(parameters.getVariant())) + .setOutputPrefixType(OUTPUT_PREFIX_TYPE_CONVERTER.toProtoEnum(parameters.getVariant())) .build()); } @@ -177,7 +122,7 @@ public final class HmacProtoSerialization { .build() .toByteString(), KeyMaterialType.SYMMETRIC, - toProtoOutputPrefixType(key.getParameters().getVariant()), + OUTPUT_PREFIX_TYPE_CONVERTER.toProtoEnum(key.getParameters().getVariant()), key.getIdRequirementOrNull()); } @@ -203,8 +148,10 @@ public final class HmacProtoSerialization { return HmacParameters.builder() .setKeySizeBytes(format.getKeySize()) .setTagSizeBytes(format.getParams().getTagSize()) - .setHashType(toHashType(format.getParams().getHash())) - .setVariant(toVariant(serialization.getKeyTemplate().getOutputPrefixType())) + .setHashType(HASH_TYPE_CONVERTER.fromProtoEnum(format.getParams().getHash())) + .setVariant( + OUTPUT_PREFIX_TYPE_CONVERTER.fromProtoEnum( + serialization.getKeyTemplate().getOutputPrefixType())) .build(); } @@ -227,8 +174,9 @@ public final class HmacProtoSerialization { HmacParameters.builder() .setKeySizeBytes(protoKey.getKeyValue().size()) .setTagSizeBytes(protoKey.getParams().getTagSize()) - .setHashType(toHashType(protoKey.getParams().getHash())) - .setVariant(toVariant(serialization.getOutputPrefixType())) + .setHashType(HASH_TYPE_CONVERTER.fromProtoEnum(protoKey.getParams().getHash())) + .setVariant( + OUTPUT_PREFIX_TYPE_CONVERTER.fromProtoEnum(serialization.getOutputPrefixType())) .build(); return HmacKey.builder() .setParameters(parameters) diff --git a/java_src/src/test/java/com/google/crypto/tink/mac/internal/BUILD.bazel b/java_src/src/test/java/com/google/crypto/tink/mac/internal/BUILD.bazel index 34660fda5..7731ee4bf 100644 --- a/java_src/src/test/java/com/google/crypto/tink/mac/internal/BUILD.bazel +++ b/java_src/src/test/java/com/google/crypto/tink/mac/internal/BUILD.bazel @@ -84,3 +84,28 @@ java_test( "@maven//:junit_junit", ], ) + +java_test( + name = "HmacProtoSerializationTest", + size = "small", + srcs = ["HmacProtoSerializationTest.java"], + deps = [ + "//proto:common_java_proto", + "//proto:hmac_java_proto", + "//proto:tink_java_proto", + "//src/main/java/com/google/crypto/tink:insecure_secret_key_access", + "//src/main/java/com/google/crypto/tink:key", + "//src/main/java/com/google/crypto/tink:parameters", + "//src/main/java/com/google/crypto/tink/internal:mutable_serialization_registry", + "//src/main/java/com/google/crypto/tink/internal:proto_key_serialization", + "//src/main/java/com/google/crypto/tink/internal:proto_parameters_serialization", + "//src/main/java/com/google/crypto/tink/internal/testing:asserts", + "//src/main/java/com/google/crypto/tink/mac:hmac_key", + "//src/main/java/com/google/crypto/tink/mac:hmac_parameters", + "//src/main/java/com/google/crypto/tink/mac/internal:hmac_proto_serialization", + "//src/main/java/com/google/crypto/tink/util:secret_bytes", + "@maven//:com_google_protobuf_protobuf_java", + "@maven//:com_google_truth_truth", + "@maven//:junit_junit", + ], +) |