1 // Copyright 2024 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.streamingaead; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.crypto.tink.KeysetHandle; 23 import com.google.crypto.tink.StreamingAead; 24 import com.google.crypto.tink.config.internal.TinkFipsUtil; 25 import com.google.crypto.tink.streamingaead.internal.AesCtrHmacStreamingProtoSerialization; 26 import com.google.crypto.tink.streamingaead.internal.AesGcmHkdfStreamingProtoSerialization; 27 import com.google.crypto.tink.util.SecretBytes; 28 import java.security.GeneralSecurityException; 29 import org.junit.Assume; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 @RunWith(JUnit4.class) 35 public class StreamingAeadConfigurationV0Test { 36 @Test config_throwsIfInFipsMode()37 public void config_throwsIfInFipsMode() throws Exception { 38 Assume.assumeTrue(TinkFipsUtil.useOnlyFips()); 39 40 assertThrows(GeneralSecurityException.class, StreamingAeadConfigurationV0::get); 41 } 42 43 @Test config_containsAesGcmHkdfStreamingAead()44 public void config_containsAesGcmHkdfStreamingAead() throws Exception { 45 Assume.assumeFalse(TinkFipsUtil.useOnlyFips()); 46 47 AesGcmHkdfStreamingProtoSerialization.register(); 48 AesGcmHkdfStreamingParameters parameters = 49 AesGcmHkdfStreamingParameters.builder() 50 .setKeySizeBytes(32) 51 .setDerivedAesGcmKeySizeBytes(32) 52 .setCiphertextSegmentSizeBytes(100) 53 .setHkdfHashType(AesGcmHkdfStreamingParameters.HashType.SHA256) 54 .build(); 55 AesGcmHkdfStreamingKey key = 56 AesGcmHkdfStreamingKey.create(parameters, SecretBytes.randomBytes(32)); 57 KeysetHandle keysetHandle = 58 KeysetHandle.newBuilder() 59 .addEntry(KeysetHandle.importKey(key).withRandomId().makePrimary()) 60 .build(); 61 62 assertThat(keysetHandle.getPrimitive(StreamingAeadConfigurationV0.get(), StreamingAead.class)) 63 .isNotNull(); 64 } 65 66 @Test config_containsAesCtrHmacStreamingAead()67 public void config_containsAesCtrHmacStreamingAead() throws Exception { 68 Assume.assumeFalse(TinkFipsUtil.useOnlyFips()); 69 70 AesCtrHmacStreamingProtoSerialization.register(); 71 AesCtrHmacStreamingParameters parameters = 72 AesCtrHmacStreamingParameters.builder() 73 .setKeySizeBytes(48) 74 .setDerivedKeySizeBytes(32) 75 .setHkdfHashType(AesCtrHmacStreamingParameters.HashType.SHA256) 76 .setHmacHashType(AesCtrHmacStreamingParameters.HashType.SHA256) 77 .setHmacTagSizeBytes(16) 78 .setCiphertextSegmentSizeBytes(60) 79 .build(); 80 AesCtrHmacStreamingKey key = 81 AesCtrHmacStreamingKey.create(parameters, SecretBytes.randomBytes(48)); 82 KeysetHandle keysetHandle = 83 KeysetHandle.newBuilder() 84 .addEntry(KeysetHandle.importKey(key).withRandomId().makePrimary()) 85 .build(); 86 87 assertThat(keysetHandle.getPrimitive(StreamingAeadConfigurationV0.get(), StreamingAead.class)) 88 .isNotNull(); 89 } 90 } 91