1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.prf; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.crypto.tink.TinkProtoParametersFormat; 22 import com.google.crypto.tink.proto.HashType; 23 import com.google.crypto.tink.proto.HkdfPrfKeyFormat; 24 import com.google.crypto.tink.proto.KeyTemplate; 25 import com.google.crypto.tink.proto.OutputPrefixType; 26 import com.google.protobuf.ExtensionRegistryLite; 27 import org.junit.BeforeClass; 28 import org.junit.Test; 29 import org.junit.experimental.theories.DataPoints; 30 import org.junit.experimental.theories.FromDataPoints; 31 import org.junit.experimental.theories.Theories; 32 import org.junit.experimental.theories.Theory; 33 import org.junit.runner.RunWith; 34 35 /** Tests forPrfKeyTemplates */ 36 @SuppressWarnings("deprecation") // Testing a deprecated class. 37 @RunWith(Theories.class) 38 public final class PrfKeyTemplatesTest { 39 @BeforeClass setUp()40 public static void setUp() throws Exception { 41 PrfConfig.register(); 42 } 43 44 @Test hkdfSha256()45 public void hkdfSha256() throws Exception { 46 assertThat(PrfKeyTemplates.HKDF_SHA256.getTypeUrl()).isEqualTo(HkdfPrfKeyManager.getKeyType()); 47 assertThat(PrfKeyTemplates.HKDF_SHA256.getOutputPrefixType()).isEqualTo(OutputPrefixType.RAW); 48 } 49 50 @Test hkdfSha256Values()51 public void hkdfSha256Values() throws Exception { 52 HkdfPrfKeyFormat format = 53 HkdfPrfKeyFormat.parseFrom( 54 PrfKeyTemplates.HKDF_SHA256.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 55 assertThat(format.getKeySize()).isEqualTo(32); 56 assertThat(format.getParams().getHash()).isEqualTo(HashType.SHA256); 57 } 58 59 public static class Pair { Pair(KeyTemplate template, PrfParameters parameters)60 public Pair(KeyTemplate template, PrfParameters parameters) { 61 this.template = template; 62 this.parameters = parameters; 63 } 64 65 KeyTemplate template; 66 PrfParameters parameters; 67 } 68 69 @DataPoints("EquivalentPairs") 70 public static final Pair[] TEMPLATES = 71 new Pair[] { 72 new Pair(PrfKeyTemplates.HKDF_SHA256, PredefinedPrfParameters.HKDF_SHA256), 73 new Pair(PrfKeyTemplates.HMAC_SHA256_PRF, PredefinedPrfParameters.HMAC_SHA256_PRF), 74 new Pair(PrfKeyTemplates.HMAC_SHA512_PRF, PredefinedPrfParameters.HMAC_SHA512_PRF), 75 new Pair(PrfKeyTemplates.AES_CMAC_PRF, PredefinedPrfParameters.AES_CMAC_PRF) 76 }; 77 78 @Theory testParametersEqualsKeyTemplate(@romDataPoints"EquivalentPairs") Pair p)79 public void testParametersEqualsKeyTemplate(@FromDataPoints("EquivalentPairs") Pair p) 80 throws Exception { 81 assertThat(TinkProtoParametersFormat.parse(p.template.toByteArray())).isEqualTo(p.parameters); 82 } 83 } 84