aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlizatretyakova <lizatretyakova@google.com>2023-08-07 02:48:44 -0700
committerCopybara-Service <copybara-worker@google.com>2023-08-07 02:50:39 -0700
commitcf0e5d94c306c290f346fe8cf45282b176c64920 (patch)
treeb49460b07ef625daccb185c5b05617497d8bb640
parent8f20e13e02d77cff41d94837bdaeac2e146ff143 (diff)
downloadtink-cf0e5d94c306c290f346fe8cf45282b176c64920.tar.gz
Add support for full PRF primitive creation to AesCmacPrfKeyManager.
PiperOrigin-RevId: 554415146
-rw-r--r--java_src/src/main/java/com/google/crypto/tink/prf/AesCmacPrfKeyManager.java8
-rw-r--r--java_src/src/main/java/com/google/crypto/tink/prf/BUILD.bazel6
-rw-r--r--java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java14
-rw-r--r--java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel3
4 files changed, 31 insertions, 0 deletions
diff --git a/java_src/src/main/java/com/google/crypto/tink/prf/AesCmacPrfKeyManager.java b/java_src/src/main/java/com/google/crypto/tink/prf/AesCmacPrfKeyManager.java
index 12a4e5c5d..dc1439dac 100644
--- a/java_src/src/main/java/com/google/crypto/tink/prf/AesCmacPrfKeyManager.java
+++ b/java_src/src/main/java/com/google/crypto/tink/prf/AesCmacPrfKeyManager.java
@@ -19,6 +19,8 @@ package com.google.crypto.tink.prf;
import com.google.crypto.tink.KeyTemplate;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.internal.KeyTypeManager;
+import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
+import com.google.crypto.tink.internal.PrimitiveConstructor;
import com.google.crypto.tink.internal.PrimitiveFactory;
import com.google.crypto.tink.proto.AesCmacPrfKey;
import com.google.crypto.tink.proto.AesCmacPrfKeyFormat;
@@ -52,6 +54,10 @@ public final class AesCmacPrfKeyManager extends KeyTypeManager<AesCmacPrfKey> {
private static final int VERSION = 0;
private static final int KEY_SIZE_IN_BYTES = 32;
+ private static final PrimitiveConstructor<com.google.crypto.tink.prf.AesCmacPrfKey, Prf>
+ PRF_PRIMITIVE_CONSTRUCTOR =
+ PrimitiveConstructor.create(
+ PrfAesCmac::create, com.google.crypto.tink.prf.AesCmacPrfKey.class, Prf.class);
@Override
public String getKeyType() {
@@ -131,6 +137,8 @@ public final class AesCmacPrfKeyManager extends KeyTypeManager<AesCmacPrfKey> {
public static void register(boolean newKeyAllowed) throws GeneralSecurityException {
Registry.registerKeyManager(new AesCmacPrfKeyManager(), newKeyAllowed);
AesCmacPrfProtoSerialization.register();
+ MutablePrimitiveRegistry.globalInstance()
+ .registerPrimitiveConstructor(PRF_PRIMITIVE_CONSTRUCTOR);
}
/**
diff --git a/java_src/src/main/java/com/google/crypto/tink/prf/BUILD.bazel b/java_src/src/main/java/com/google/crypto/tink/prf/BUILD.bazel
index 4ba33985f..0bdb2f7b9 100644
--- a/java_src/src/main/java/com/google/crypto/tink/prf/BUILD.bazel
+++ b/java_src/src/main/java/com/google/crypto/tink/prf/BUILD.bazel
@@ -85,6 +85,7 @@ java_library(
name = "aes_cmac_prf_key_manager",
srcs = ["AesCmacPrfKeyManager.java"],
deps = [
+ ":aes_cmac_prf_key",
":aes_cmac_prf_proto_serialization",
":prf_set",
"//proto:aes_cmac_prf_java_proto",
@@ -92,6 +93,8 @@ java_library(
"//src/main/java/com/google/crypto/tink:key_template",
"//src/main/java/com/google/crypto/tink:registry",
"//src/main/java/com/google/crypto/tink/internal:key_type_manager",
+ "//src/main/java/com/google/crypto/tink/internal:mutable_primitive_registry",
+ "//src/main/java/com/google/crypto/tink/internal:primitive_constructor",
"//src/main/java/com/google/crypto/tink/internal:primitive_factory",
"//src/main/java/com/google/crypto/tink/subtle:prf_aes_cmac",
"//src/main/java/com/google/crypto/tink/subtle:random",
@@ -374,6 +377,7 @@ android_library(
name = "aes_cmac_prf_key_manager-android",
srcs = ["AesCmacPrfKeyManager.java"],
deps = [
+ ":aes_cmac_prf_key-android",
":aes_cmac_prf_proto_serialization-android",
":prf_set-android",
"//proto:aes_cmac_prf_java_proto_lite",
@@ -381,6 +385,8 @@ android_library(
"//src/main/java/com/google/crypto/tink:key_template-android",
"//src/main/java/com/google/crypto/tink:registry-android",
"//src/main/java/com/google/crypto/tink/internal:key_type_manager-android",
+ "//src/main/java/com/google/crypto/tink/internal:mutable_primitive_registry-android",
+ "//src/main/java/com/google/crypto/tink/internal:primitive_constructor-android",
"//src/main/java/com/google/crypto/tink/internal:primitive_factory-android",
"//src/main/java/com/google/crypto/tink/subtle:prf_aes_cmac-android",
"//src/main/java/com/google/crypto/tink/subtle:random-android",
diff --git a/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java b/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java
index f7331e235..690ca746b 100644
--- a/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java
+++ b/java_src/src/test/java/com/google/crypto/tink/prf/AesCmacPrfKeyManagerTest.java
@@ -23,10 +23,12 @@ import static org.junit.Assert.assertThrows;
import com.google.crypto.tink.KeyTemplate;
import com.google.crypto.tink.KeyTemplates;
import com.google.crypto.tink.KeysetHandle;
+import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import com.google.crypto.tink.proto.AesCmacPrfKey;
import com.google.crypto.tink.proto.AesCmacPrfKeyFormat;
import com.google.crypto.tink.subtle.PrfAesCmac;
import com.google.crypto.tink.subtle.Random;
+import com.google.crypto.tink.util.SecretBytes;
import com.google.protobuf.ByteString;
import java.security.GeneralSecurityException;
import org.junit.Before;
@@ -169,4 +171,16 @@ public class AesCmacPrfKeyManagerTest {
assertThat(h.getAt(0).getKey().getParameters())
.isEqualTo(KeyTemplates.get(templateName).toParameters());
}
+
+ @Test
+ public void registersPrfPrimitiveConstructor() throws Exception {
+ Prf prf =
+ MutablePrimitiveRegistry.globalInstance()
+ .getPrimitive(
+ com.google.crypto.tink.prf.AesCmacPrfKey.create(
+ AesCmacPrfParameters.create(32), SecretBytes.randomBytes(32)),
+ Prf.class);
+
+ assertThat(prf).isInstanceOf(PrfAesCmac.class);
+ }
}
diff --git a/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel b/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel
index 368164add..f3e09ef1e 100644
--- a/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel
+++ b/java_src/src/test/java/com/google/crypto/tink/prf/BUILD.bazel
@@ -56,6 +56,8 @@ java_test(
"//src/main/java/com/google/crypto/tink:key_template",
"//src/main/java/com/google/crypto/tink:key_templates",
"//src/main/java/com/google/crypto/tink:registry_cluster",
+ "//src/main/java/com/google/crypto/tink/internal:mutable_primitive_registry",
+ "//src/main/java/com/google/crypto/tink/prf:aes_cmac_prf_key",
"//src/main/java/com/google/crypto/tink/prf:aes_cmac_prf_key_manager",
"//src/main/java/com/google/crypto/tink/prf:aes_cmac_prf_parameters",
"//src/main/java/com/google/crypto/tink/prf:prf_config",
@@ -63,6 +65,7 @@ java_test(
"//src/main/java/com/google/crypto/tink/subtle:prf_aes_cmac",
"//src/main/java/com/google/crypto/tink/subtle:random",
"//src/main/java/com/google/crypto/tink/testing:key_type_manager_test_util",
+ "//src/main/java/com/google/crypto/tink/util:secret_bytes",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_truth_truth",
"@maven//:junit_junit",