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.aead; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.crypto.tink.KeysetHandle; 23 import com.google.crypto.tink.config.TinkFips; 24 import com.google.crypto.tink.config.internal.TinkFipsUtil; 25 import java.security.GeneralSecurityException; 26 import org.junit.Assume; 27 import org.junit.FixMethodOrder; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 import org.junit.runners.MethodSorters; 32 33 /** 34 * Tests for AeadConfig. Using FixedMethodOrder to ensure that aaaTestInitialization runs first, as 35 * it tests execution of a static block within AeadConfig-class. 36 */ 37 @RunWith(JUnit4.class) 38 @FixMethodOrder(MethodSorters.NAME_ASCENDING) 39 public class AeadConfigTest { 40 41 @Test withoutFips_allAeadKeyTypesAreRegistered()42 public void withoutFips_allAeadKeyTypesAreRegistered() throws Exception { 43 Assume.assumeFalse(TinkFips.useOnlyFips()); 44 45 AeadConfig.register(); 46 47 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.AES128_CTR_HMAC_SHA256)); 48 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.AES128_GCM)); 49 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.AES128_EAX)); 50 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.CHACHA20_POLY1305)); 51 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.XCHACHA20_POLY1305)); 52 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_192_BIT_NONCE)); 53 assertNotNull( 54 KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_160_BIT_NONCE_NO_PREFIX)); 55 } 56 57 @Test withFips_fipsKeyTypesAreRegistered()58 public void withFips_fipsKeyTypesAreRegistered() throws Exception { 59 Assume.assumeTrue(TinkFips.useOnlyFips()); 60 Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); 61 62 AeadConfig.register(); 63 64 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.AES128_CTR_HMAC_SHA256)); 65 assertNotNull(KeysetHandle.generateNew(PredefinedAeadParameters.AES128_GCM)); 66 } 67 68 @Test withFips_nonFipsKeyTypesAreNotRegistered()69 public void withFips_nonFipsKeyTypesAreNotRegistered() throws Exception { 70 Assume.assumeTrue(TinkFips.useOnlyFips()); 71 Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); 72 73 AeadConfig.register(); 74 75 assertThrows( 76 GeneralSecurityException.class, 77 () -> KeysetHandle.generateNew(PredefinedAeadParameters.AES128_EAX)); 78 assertThrows( 79 GeneralSecurityException.class, 80 () -> KeysetHandle.generateNew(AesGcmSivParameters.builder().setKeySizeBytes(16).build())); 81 assertThrows( 82 GeneralSecurityException.class, 83 () -> KeysetHandle.generateNew(PredefinedAeadParameters.CHACHA20_POLY1305)); 84 assertThrows( 85 GeneralSecurityException.class, 86 () -> KeysetHandle.generateNew(PredefinedAeadParameters.XCHACHA20_POLY1305)); 87 assertThrows( 88 GeneralSecurityException.class, 89 () -> KeysetHandle.generateNew(PredefinedAeadParameters.XAES_256_GCM_192_BIT_NONCE)); 90 assertThrows( 91 GeneralSecurityException.class, 92 () -> KeysetHandle.generateNew(PredefinedAeadParameters.X_AES_GCM_8_BYTE_SALT_NO_PREFIX)); 93 } 94 } 95