1 // Copyright 2017 Google Inc. 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.mac; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.crypto.tink.KeyTemplate; 23 import com.google.crypto.tink.KeyTemplates; 24 import com.google.crypto.tink.KeysetHandle; 25 import com.google.crypto.tink.Mac; 26 import com.google.crypto.tink.Parameters; 27 import com.google.crypto.tink.RegistryConfiguration; 28 import com.google.crypto.tink.internal.KeyManagerRegistry; 29 import com.google.crypto.tink.util.SecretBytes; 30 import java.security.GeneralSecurityException; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.experimental.theories.DataPoints; 34 import org.junit.experimental.theories.FromDataPoints; 35 import org.junit.experimental.theories.Theories; 36 import org.junit.experimental.theories.Theory; 37 import org.junit.runner.RunWith; 38 39 /** Test for AesCmacKeyManager. */ 40 @RunWith(Theories.class) 41 public class AesCmacKeyManagerTest { 42 @Before register()43 public void register() throws Exception { 44 MacConfig.register(); 45 } 46 47 @Test testKeyManagerRegistered()48 public void testKeyManagerRegistered() throws Exception { 49 assertThat( 50 KeyManagerRegistry.globalInstance() 51 .getKeyManager("type.googleapis.com/google.crypto.tink.AesCmacKey", Mac.class)) 52 .isNotNull(); 53 } 54 55 @Test testKeyCreationWorks()56 public void testKeyCreationWorks() throws Exception { 57 Parameters validParameters = 58 AesCmacParameters.builder() 59 .setKeySizeBytes(32) 60 .setTagSizeBytes(16) 61 .setVariant(AesCmacParameters.Variant.TINK) 62 .build(); 63 assertThat(KeysetHandle.generateNew(validParameters).getAt(0).getKey().getParameters()) 64 .isEqualTo(validParameters); 65 } 66 67 @Test testKeyCreation_invalidParameters_fails()68 public void testKeyCreation_invalidParameters_fails() throws Exception { 69 // These parameters can be created but aren't accepted by the key manager. 70 Parameters validParameters = 71 AesCmacParameters.builder() 72 .setKeySizeBytes(16) 73 .setTagSizeBytes(16) 74 .setVariant(AesCmacParameters.Variant.TINK) 75 .build(); 76 assertThrows(GeneralSecurityException.class, () -> KeysetHandle.generateNew(validParameters)); 77 } 78 79 @Test testMacCreation_succeeds()80 public void testMacCreation_succeeds() throws Exception { 81 AesCmacParameters validParameters = 82 AesCmacParameters.builder() 83 .setKeySizeBytes(32) 84 .setTagSizeBytes(16) 85 .setVariant(AesCmacParameters.Variant.TINK) 86 .build(); 87 AesCmacKey validKey = 88 AesCmacKey.builder() 89 .setParameters(validParameters) 90 .setAesKeyBytes(SecretBytes.randomBytes(validParameters.getKeySizeBytes())) 91 .setIdRequirement(739) 92 .build(); 93 KeysetHandle keyset = 94 KeysetHandle.newBuilder().addEntry(KeysetHandle.importKey(validKey).makePrimary()).build(); 95 assertThat(keyset.getPrimitive(RegistryConfiguration.get(), Mac.class)).isNotNull(); 96 } 97 98 @Test testMacCreation_invalidKey_throws()99 public void testMacCreation_invalidKey_throws() throws Exception { 100 // These parameters can be created but aren't accepted by the key manager. 101 AesCmacParameters validParameters = 102 AesCmacParameters.builder() 103 .setKeySizeBytes(16) 104 .setTagSizeBytes(16) 105 .setVariant(AesCmacParameters.Variant.TINK) 106 .build(); 107 AesCmacKey validKey = 108 AesCmacKey.builder() 109 .setParameters(validParameters) 110 .setAesKeyBytes(SecretBytes.randomBytes(validParameters.getKeySizeBytes())) 111 .setIdRequirement(739) 112 .build(); 113 KeysetHandle keyset = 114 KeysetHandle.newBuilder().addEntry(KeysetHandle.importKey(validKey).makePrimary()).build(); 115 assertThrows( 116 GeneralSecurityException.class, 117 () -> keyset.getPrimitive(RegistryConfiguration.get(), Mac.class)); 118 } 119 120 @Test testChunkedMacCreation_succeeds()121 public void testChunkedMacCreation_succeeds() throws Exception { 122 // These parameters can be created but aren't accepted by the key manager. 123 AesCmacParameters validParameters = 124 AesCmacParameters.builder() 125 .setKeySizeBytes(32) 126 .setTagSizeBytes(16) 127 .setVariant(AesCmacParameters.Variant.TINK) 128 .build(); 129 AesCmacKey validKey = 130 AesCmacKey.builder() 131 .setParameters(validParameters) 132 .setAesKeyBytes(SecretBytes.randomBytes(validParameters.getKeySizeBytes())) 133 .setIdRequirement(1023) 134 .build(); 135 KeysetHandle keyset = 136 KeysetHandle.newBuilder().addEntry(KeysetHandle.importKey(validKey).makePrimary()).build(); 137 assertThat(keyset.getPrimitive(RegistryConfiguration.get(), ChunkedMac.class)).isNotNull(); 138 } 139 140 @Test testChunkedMacCreation_invalidKey_throws()141 public void testChunkedMacCreation_invalidKey_throws() throws Exception { 142 // These parameters can be created but aren't accepted by the key manager. 143 AesCmacParameters validParameters = 144 AesCmacParameters.builder() 145 .setKeySizeBytes(16) 146 .setTagSizeBytes(16) 147 .setVariant(AesCmacParameters.Variant.TINK) 148 .build(); 149 AesCmacKey validKey = 150 AesCmacKey.builder() 151 .setParameters(validParameters) 152 .setAesKeyBytes(SecretBytes.randomBytes(validParameters.getKeySizeBytes())) 153 .setIdRequirement(739) 154 .build(); 155 KeysetHandle keyset = 156 KeysetHandle.newBuilder().addEntry(KeysetHandle.importKey(validKey).makePrimary()).build(); 157 assertThrows( 158 GeneralSecurityException.class, 159 () -> keyset.getPrimitive(RegistryConfiguration.get(), ChunkedMac.class)); 160 } 161 162 @Test testAes256CmacTemplate()163 public void testAes256CmacTemplate() throws Exception { 164 KeyTemplate template = AesCmacKeyManager.aes256CmacTemplate(); 165 assertThat(template.toParameters()) 166 .isEqualTo( 167 AesCmacParameters.builder() 168 .setKeySizeBytes(32) 169 .setTagSizeBytes(16) 170 .setVariant(AesCmacParameters.Variant.TINK) 171 .build()); 172 } 173 174 @Test testRawAes256CmacTemplate()175 public void testRawAes256CmacTemplate() throws Exception { 176 KeyTemplate template = AesCmacKeyManager.rawAes256CmacTemplate(); 177 assertThat(template.toParameters()) 178 .isEqualTo( 179 AesCmacParameters.builder() 180 .setKeySizeBytes(32) 181 .setTagSizeBytes(16) 182 .setVariant(AesCmacParameters.Variant.NO_PREFIX) 183 .build()); 184 } 185 186 @Test testKeyTemplatesWork()187 public void testKeyTemplatesWork() throws Exception { 188 Parameters p = AesCmacKeyManager.aes256CmacTemplate().toParameters(); 189 assertThat(KeysetHandle.generateNew(p).getAt(0).getKey().getParameters()).isEqualTo(p); 190 191 p = AesCmacKeyManager.rawAes256CmacTemplate().toParameters(); 192 assertThat(KeysetHandle.generateNew(p).getAt(0).getKey().getParameters()).isEqualTo(p); 193 } 194 195 @DataPoints("templateNames") 196 public static final String[] KEY_TEMPLATES = new String[] {"AES256_CMAC", "AES256_CMAC_RAW"}; 197 198 @Theory testTemplates(@romDataPoints"templateNames") String templateName)199 public void testTemplates(@FromDataPoints("templateNames") String templateName) throws Exception { 200 KeysetHandle h = KeysetHandle.generateNew(KeyTemplates.get(templateName)); 201 assertThat(h.size()).isEqualTo(1); 202 assertThat(h.getAt(0).getKey().getParameters()) 203 .isEqualTo(KeyTemplates.get(templateName).toParameters()); 204 } 205 } 206