• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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.prf;
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.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.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 /** Tests for PrfConfig. */
32 @RunWith(JUnit4.class)
33 public class PrfConfigTest {
34 
35   @Test
notOnlyFips_shouldRegisterAllKeyTypes()36   public void notOnlyFips_shouldRegisterAllKeyTypes() throws Exception {
37     Assume.assumeFalse(TinkFips.useOnlyFips());
38 
39     PrfConfig.register();
40 
41     assertThat(KeysetHandle.generateNew(PredefinedPrfParameters.HMAC_SHA256_PRF)).isNotNull();
42     assertThat(KeysetHandle.generateNew(PredefinedPrfParameters.HKDF_SHA256)).isNotNull();
43     assertThat(KeysetHandle.generateNew(PredefinedPrfParameters.AES_CMAC_PRF)).isNotNull();
44   }
45 
46   @Test
onlyFips_shouldRegisterFipsKeyTypes()47   public void onlyFips_shouldRegisterFipsKeyTypes() throws Exception {
48     Assume.assumeTrue(TinkFips.useOnlyFips());
49     Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable());
50 
51     PrfConfig.register();
52 
53     assertThat(KeysetHandle.generateNew(PredefinedPrfParameters.HMAC_SHA256_PRF)).isNotNull();
54   }
55 
56   @Test
onlyFips_shouldNotRegisterNonFipsKeyTypes()57   public void onlyFips_shouldNotRegisterNonFipsKeyTypes() throws Exception {
58     Assume.assumeTrue(TinkFips.useOnlyFips());
59     Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable());
60 
61     PrfConfig.register();
62     assertThrows(
63         GeneralSecurityException.class,
64         () -> KeysetHandle.generateNew(PredefinedPrfParameters.HKDF_SHA256));
65     assertThrows(
66         GeneralSecurityException.class,
67         () -> KeysetHandle.generateNew(PredefinedPrfParameters.AES_CMAC_PRF));
68   }
69 }
70