diff --git a/java_src/src/main/java/com/google/crypto/tink/BUILD.bazel b/java_src/src/main/java/com/google/crypto/tink/BUILD.bazel index 0eebbdf4ef..9dec9ef944 100644 --- a/java_src/src/main/java/com/google/crypto/tink/BUILD.bazel +++ b/java_src/src/main/java/com/google/crypto/tink/BUILD.bazel @@ -247,6 +247,7 @@ java_library( name = "key_type_manager", srcs = ["KeyTypeManager.java"], deps = [ + ":key_template", "//proto:tink_java_proto", "//src/main/java/com/google/crypto/tink/annotations:alpha", "@com_google_protobuf//:protobuf_javalite", @@ -257,6 +258,7 @@ android_library( name = "key_type_manager-android", srcs = ["KeyTypeManager.java"], deps = [ + ":key_template-android", "//proto:tink_java_proto_lite", "//src/main/java/com/google/crypto/tink/annotations:alpha", "@com_google_protobuf//:protobuf_javalite", diff --git a/java_src/src/main/java/com/google/crypto/tink/KeyTypeManager.java b/java_src/src/main/java/com/google/crypto/tink/KeyTypeManager.java index 972b6b0f7b..f125a9dc31 100644 --- a/java_src/src/main/java/com/google/crypto/tink/KeyTypeManager.java +++ b/java_src/src/main/java/com/google/crypto/tink/KeyTypeManager.java @@ -180,6 +180,20 @@ public KeyFactory(Class clazz) { this.clazz = clazz; } + /** + * A container that contains key format and other information that form key templates supported + * by this factory. + */ + public static final class KeyFormat { + public KeyFormatProtoT keyFormat; + public KeyTemplate.OutputPrefixType outputPrefixType; + + public KeyFormat(KeyFormatProtoT keyFormat, KeyTemplate.OutputPrefixType outputPrefixType) { + this.keyFormat = keyFormat; + this.outputPrefixType = outputPrefixType; + } + } + /** * Returns the class corresponding to the key format protobuffer. */ @@ -208,14 +222,14 @@ public abstract KeyFormatProtoT parseKeyFormat(ByteString byteString) public abstract KeyT createKey(KeyFormatProtoT keyFormat) throws GeneralSecurityException; /** - * Derives a new key from a given format, using the given {@param pseudoRandomness}. + * Derives a new key from a given format, using the given {@code pseudoRandomness}. * - *

Implementations need to note that the given paramter {@param pseudoRandomness} may only + *

Implementations need to note that the given paramter {@code pseudoRandomness} may only * produce a finite amount of randomness. Hence, proper implementations will first obtain all * the pseudorandom bytes needed; and only after produce the key. * *

While {@link validateKeyFormat} is called before this method will be called, - * implementations must check the version of the given {@param keyFormat}, as {@link + * implementations must check the version of the given {@code keyFormat}, as {@link * validateKeyFormat} is also called from {@link createKey}. * *

Not every KeyTypeManager needs to implement this; if not implemented a {@link @@ -223,8 +237,12 @@ public abstract KeyFormatProtoT parseKeyFormat(ByteString byteString) */ public KeyT deriveKey(KeyFormatProtoT keyFormat, InputStream pseudoRandomness) throws GeneralSecurityException { - throw new GeneralSecurityException( - "deriveKey not implemented for key of type " + clazz.toString()); + throw new GeneralSecurityException("deriveKey not implemented for key of type " + clazz); + } + + /** Returns supported key formats and their names. */ + public Map> keyFormats() { + return Collections.emptyMap(); } } diff --git a/java_src/src/main/java/com/google/crypto/tink/Registry.java b/java_src/src/main/java/com/google/crypto/tink/Registry.java index 93f253409e..d4b9f32875 100644 --- a/java_src/src/main/java/com/google/crypto/tink/Registry.java +++ b/java_src/src/main/java/com/google/crypto/tink/Registry.java @@ -17,7 +17,6 @@ package com.google.crypto.tink; import com.google.crypto.tink.proto.KeyData; -import com.google.crypto.tink.proto.KeyTemplate; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.MessageLite; @@ -25,6 +24,7 @@ import java.security.GeneralSecurityException; import java.util.Collections; import java.util.Locale; +import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -88,6 +88,9 @@ public final class Registry { private static final ConcurrentMap, PrimitiveWrapper> primitiveWrapperMap = new ConcurrentHashMap<>(); + private static final ConcurrentMap keyTemplateMap = + new ConcurrentHashMap<>(); // name -> KeyTemplate mapping + /** * A container which either is constructed from a {@link KeyTypeManager} or from a {@link * KeyManager}. @@ -328,6 +331,7 @@ static synchronized void reset() { newKeyAllowedMap.clear(); catalogueMap.clear(); primitiveWrapperMap.clear(); + keyTemplateMap.clear(); } /** @@ -428,28 +432,66 @@ public static synchronized

void registerKeyManager(final KeyManager

manag } /** - * Throws a general security exception iff there is already a key manager registered for typeURL, - * and at least one of the following is true: - * * The class implementing the existing key manager differs from the given one. - * * The value of newKeyAllowed currently registered is false, but the input parameter is true. + * Throws a general security exception if one of these conditions holds: + * + *