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.streamingaead; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertEquals; 21 22 import com.google.crypto.tink.TinkProtoParametersFormat; 23 import com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat; 24 import com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat; 25 import com.google.crypto.tink.proto.HashType; 26 import com.google.crypto.tink.proto.KeyTemplate; 27 import com.google.crypto.tink.proto.OutputPrefixType; 28 import com.google.protobuf.ExtensionRegistryLite; 29 import org.junit.BeforeClass; 30 import org.junit.Test; 31 import org.junit.experimental.theories.DataPoints; 32 import org.junit.experimental.theories.FromDataPoints; 33 import org.junit.experimental.theories.Theories; 34 import org.junit.experimental.theories.Theory; 35 import org.junit.runner.RunWith; 36 37 /** Tests for StreamingAeadKeyTemplates. */ 38 @RunWith(Theories.class) 39 public class StreamingAeadKeyTemplatesTest { 40 @BeforeClass setUp()41 public static void setUp() throws Exception { 42 StreamingAeadConfig.register(); 43 } 44 45 @Test testAes128CtrHmacSha256_4KB()46 public void testAes128CtrHmacSha256_4KB() throws Exception { 47 KeyTemplate template = StreamingAeadKeyTemplates.AES128_CTR_HMAC_SHA256_4KB; 48 assertEquals(AesCtrHmacStreamingKeyManager.getKeyType(), template.getTypeUrl()); 49 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 50 AesCtrHmacStreamingKeyFormat format = 51 AesCtrHmacStreamingKeyFormat.parseFrom( 52 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 53 54 assertEquals(16, format.getKeySize()); 55 assertEquals(16, format.getParams().getDerivedKeySize()); 56 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 57 assertEquals(4096, format.getParams().getCiphertextSegmentSize()); 58 assertEquals(HashType.SHA256, format.getParams().getHmacParams().getHash()); 59 assertEquals(32, format.getParams().getHmacParams().getTagSize()); 60 } 61 62 @Test testAes128CtrHmacSha256_1MB()63 public void testAes128CtrHmacSha256_1MB() throws Exception { 64 KeyTemplate template = StreamingAeadKeyTemplates.AES128_CTR_HMAC_SHA256_1MB; 65 assertEquals(AesCtrHmacStreamingKeyManager.getKeyType(), template.getTypeUrl()); 66 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 67 AesCtrHmacStreamingKeyFormat format = 68 AesCtrHmacStreamingKeyFormat.parseFrom( 69 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 70 71 assertEquals(16, format.getKeySize()); 72 assertEquals(16, format.getParams().getDerivedKeySize()); 73 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 74 assertEquals(1048576, format.getParams().getCiphertextSegmentSize()); 75 assertEquals(HashType.SHA256, format.getParams().getHmacParams().getHash()); 76 assertEquals(32, format.getParams().getHmacParams().getTagSize()); 77 } 78 79 @Test testAes256CtrHmacSha256_4KB()80 public void testAes256CtrHmacSha256_4KB() throws Exception { 81 KeyTemplate template = StreamingAeadKeyTemplates.AES256_CTR_HMAC_SHA256_4KB; 82 assertEquals(AesCtrHmacStreamingKeyManager.getKeyType(), template.getTypeUrl()); 83 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 84 AesCtrHmacStreamingKeyFormat format = 85 AesCtrHmacStreamingKeyFormat.parseFrom( 86 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 87 88 assertEquals(32, format.getKeySize()); 89 assertEquals(32, format.getParams().getDerivedKeySize()); 90 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 91 assertEquals(4096, format.getParams().getCiphertextSegmentSize()); 92 assertEquals(HashType.SHA256, format.getParams().getHmacParams().getHash()); 93 assertEquals(32, format.getParams().getHmacParams().getTagSize()); 94 } 95 96 @Test testAes256CtrHmacSha256_1MB()97 public void testAes256CtrHmacSha256_1MB() throws Exception { 98 KeyTemplate template = StreamingAeadKeyTemplates.AES256_CTR_HMAC_SHA256_1MB; 99 assertEquals(AesCtrHmacStreamingKeyManager.getKeyType(), template.getTypeUrl()); 100 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 101 AesCtrHmacStreamingKeyFormat format = 102 AesCtrHmacStreamingKeyFormat.parseFrom( 103 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 104 105 assertEquals(32, format.getKeySize()); 106 assertEquals(32, format.getParams().getDerivedKeySize()); 107 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 108 assertEquals(1048576, format.getParams().getCiphertextSegmentSize()); 109 assertEquals(HashType.SHA256, format.getParams().getHmacParams().getHash()); 110 assertEquals(32, format.getParams().getHmacParams().getTagSize()); 111 } 112 113 @Test testAes128GcmHkdf_4KB()114 public void testAes128GcmHkdf_4KB() throws Exception { 115 KeyTemplate template = StreamingAeadKeyTemplates.AES128_GCM_HKDF_4KB; 116 assertEquals(AesGcmHkdfStreamingKeyManager.getKeyType(), template.getTypeUrl()); 117 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 118 AesGcmHkdfStreamingKeyFormat format = 119 AesGcmHkdfStreamingKeyFormat.parseFrom( 120 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 121 122 assertEquals(16, format.getKeySize()); 123 assertEquals(16, format.getParams().getDerivedKeySize()); 124 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 125 assertEquals(4096, format.getParams().getCiphertextSegmentSize()); 126 } 127 128 @Test testAes128GcmHkdf_1MB()129 public void testAes128GcmHkdf_1MB() throws Exception { 130 KeyTemplate template = StreamingAeadKeyTemplates.AES128_GCM_HKDF_1MB; 131 assertEquals(AesGcmHkdfStreamingKeyManager.getKeyType(), template.getTypeUrl()); 132 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 133 AesGcmHkdfStreamingKeyFormat format = 134 AesGcmHkdfStreamingKeyFormat.parseFrom( 135 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 136 137 assertEquals(16, format.getKeySize()); 138 assertEquals(16, format.getParams().getDerivedKeySize()); 139 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 140 assertEquals(1048576, format.getParams().getCiphertextSegmentSize()); 141 } 142 143 @Test testAes256GcmHkdf_4KB()144 public void testAes256GcmHkdf_4KB() throws Exception { 145 KeyTemplate template = StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB; 146 assertEquals(AesGcmHkdfStreamingKeyManager.getKeyType(), template.getTypeUrl()); 147 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 148 AesGcmHkdfStreamingKeyFormat format = 149 AesGcmHkdfStreamingKeyFormat.parseFrom( 150 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 151 152 assertEquals(32, format.getKeySize()); 153 assertEquals(32, format.getParams().getDerivedKeySize()); 154 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 155 assertEquals(4096, format.getParams().getCiphertextSegmentSize()); 156 } 157 158 @Test testAes256GcmHkdf_1MB()159 public void testAes256GcmHkdf_1MB() throws Exception { 160 KeyTemplate template = StreamingAeadKeyTemplates.AES256_GCM_HKDF_1MB; 161 assertEquals(AesGcmHkdfStreamingKeyManager.getKeyType(), template.getTypeUrl()); 162 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 163 AesGcmHkdfStreamingKeyFormat format = 164 AesGcmHkdfStreamingKeyFormat.parseFrom( 165 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 166 167 assertEquals(32, format.getKeySize()); 168 assertEquals(32, format.getParams().getDerivedKeySize()); 169 assertEquals(HashType.SHA256, format.getParams().getHkdfHashType()); 170 assertEquals(1048576, format.getParams().getCiphertextSegmentSize()); 171 } 172 173 @Test testCreateAesCtrHmacStreamingKeyTemplate()174 public void testCreateAesCtrHmacStreamingKeyTemplate() throws Exception { 175 // Intentionally using "weird" or invalid values for parameters, 176 // to test that the function correctly puts them in the resulting template. 177 int mainKeySize = 42; 178 int derivedKeySize = 24; 179 int tagSize = 45; 180 int ciphertextSegmentSize = 12345; 181 HashType hkdfHashType = HashType.SHA512; 182 HashType macHashType = HashType.UNKNOWN_HASH; 183 KeyTemplate template = StreamingAeadKeyTemplates.createAesCtrHmacStreamingKeyTemplate( 184 mainKeySize, hkdfHashType, derivedKeySize, 185 macHashType, tagSize, ciphertextSegmentSize); 186 assertEquals(AesCtrHmacStreamingKeyManager.getKeyType(), template.getTypeUrl()); 187 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 188 AesCtrHmacStreamingKeyFormat format = 189 AesCtrHmacStreamingKeyFormat.parseFrom( 190 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 191 192 assertEquals(mainKeySize, format.getKeySize()); 193 assertEquals(derivedKeySize, format.getParams().getDerivedKeySize()); 194 assertEquals(hkdfHashType, format.getParams().getHkdfHashType()); 195 assertEquals(ciphertextSegmentSize, format.getParams().getCiphertextSegmentSize()); 196 assertEquals(macHashType, format.getParams().getHmacParams().getHash()); 197 assertEquals(tagSize, format.getParams().getHmacParams().getTagSize()); 198 } 199 200 @Test testCreateAesGcmHkdfStreamingKeyTemplate()201 public void testCreateAesGcmHkdfStreamingKeyTemplate() throws Exception { 202 // Intentionally using "weird" or invalid values for parameters, 203 // to test that the function correctly puts them in the resulting template. 204 int mainKeySize = 42; 205 int derivedKeySize = 24; 206 int ciphertextSegmentSize = 12345; 207 HashType hkdfHashType = HashType.SHA512; 208 KeyTemplate template = StreamingAeadKeyTemplates.createAesGcmHkdfStreamingKeyTemplate( 209 mainKeySize, hkdfHashType, derivedKeySize, ciphertextSegmentSize); 210 assertEquals(AesGcmHkdfStreamingKeyManager.getKeyType(), template.getTypeUrl()); 211 assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType()); 212 AesGcmHkdfStreamingKeyFormat format = 213 AesGcmHkdfStreamingKeyFormat.parseFrom( 214 template.getValue(), ExtensionRegistryLite.getEmptyRegistry()); 215 216 assertEquals(mainKeySize, format.getKeySize()); 217 assertEquals(derivedKeySize, format.getParams().getDerivedKeySize()); 218 assertEquals(hkdfHashType, format.getParams().getHkdfHashType()); 219 assertEquals(ciphertextSegmentSize, format.getParams().getCiphertextSegmentSize()); 220 } 221 222 public static class Pair { Pair(KeyTemplate template, StreamingAeadParameters parameters)223 public Pair(KeyTemplate template, StreamingAeadParameters parameters) { 224 this.template = template; 225 this.parameters = parameters; 226 } 227 228 KeyTemplate template; 229 StreamingAeadParameters parameters; 230 } 231 232 @DataPoints("EquivalentPairs") 233 public static final Pair[] TEMPLATES = 234 new Pair[] { 235 new Pair( 236 StreamingAeadKeyTemplates.AES128_CTR_HMAC_SHA256_4KB, 237 PredefinedStreamingAeadParameters.AES128_CTR_HMAC_SHA256_4KB), 238 new Pair( 239 StreamingAeadKeyTemplates.AES128_CTR_HMAC_SHA256_1MB, 240 PredefinedStreamingAeadParameters.AES128_CTR_HMAC_SHA256_1MB), 241 new Pair( 242 StreamingAeadKeyTemplates.AES256_CTR_HMAC_SHA256_4KB, 243 PredefinedStreamingAeadParameters.AES256_CTR_HMAC_SHA256_4KB), 244 new Pair( 245 StreamingAeadKeyTemplates.AES256_CTR_HMAC_SHA256_1MB, 246 PredefinedStreamingAeadParameters.AES256_CTR_HMAC_SHA256_1MB), 247 new Pair( 248 StreamingAeadKeyTemplates.AES128_GCM_HKDF_4KB, 249 PredefinedStreamingAeadParameters.AES128_GCM_HKDF_4KB), 250 new Pair( 251 StreamingAeadKeyTemplates.AES128_GCM_HKDF_1MB, 252 PredefinedStreamingAeadParameters.AES128_GCM_HKDF_1MB), 253 new Pair( 254 StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB, 255 PredefinedStreamingAeadParameters.AES256_GCM_HKDF_4KB), 256 new Pair( 257 StreamingAeadKeyTemplates.AES256_GCM_HKDF_1MB, 258 PredefinedStreamingAeadParameters.AES256_GCM_HKDF_1MB) 259 }; 260 261 @Theory testParametersEqualsKeyTemplate(@romDataPoints"EquivalentPairs") Pair p)262 public void testParametersEqualsKeyTemplate(@FromDataPoints("EquivalentPairs") Pair p) 263 throws Exception { 264 assertThat(TinkProtoParametersFormat.parse(p.template.toByteArray())).isEqualTo(p.parameters); 265 } 266 } 267