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.hybrid; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.crypto.tink.KeyTemplates; 23 import com.google.crypto.tink.KeysetHandle; 24 import com.google.crypto.tink.config.TinkFips; 25 import com.google.crypto.tink.config.internal.TinkFipsUtil; 26 import java.security.GeneralSecurityException; 27 import org.junit.Assume; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 /** Tests for HybridConfig. */ 33 @RunWith(JUnit4.class) 34 public class HybridConfigTest { 35 36 @Test notOnlyFips_shouldRegisterAllKeyTypes()37 public void notOnlyFips_shouldRegisterAllKeyTypes() throws Exception { 38 Assume.assumeFalse(TinkFips.useOnlyFips()); 39 40 HybridConfig.register(); 41 42 assertThat(KeysetHandle.generateNew(KeyTemplates.get("ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM"))) 43 .isNotNull(); 44 assertThat( 45 KeysetHandle.generateNew( 46 KeyTemplates.get("DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_128_GCM"))) 47 .isNotNull(); 48 } 49 50 @Test onlyFips_shouldNotRegisterNonFipsKeyTypes()51 public void onlyFips_shouldNotRegisterNonFipsKeyTypes() throws Exception { 52 Assume.assumeTrue(TinkFips.useOnlyFips()); 53 Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); 54 55 HybridConfig.register(); 56 57 assertThrows( 58 GeneralSecurityException.class, 59 () -> KeysetHandle.generateNew(KeyTemplates.get("ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM"))); 60 assertThrows( 61 GeneralSecurityException.class, 62 () -> 63 KeysetHandle.generateNew( 64 KeyTemplates.get("DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_128_GCM"))); 65 } 66 } 67