• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 Google
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.subtle;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import com.google.crypto.tink.InsecureSecretKeyAccess;
23 import com.google.crypto.tink.config.TinkFips;
24 import com.google.crypto.tink.config.internal.TinkFipsUtil;
25 import com.google.crypto.tink.signature.RsaSsaPssParameters;
26 import com.google.crypto.tink.signature.RsaSsaPssPrivateKey;
27 import com.google.crypto.tink.signature.internal.testing.RsaSsaPssTestUtil;
28 import com.google.crypto.tink.subtle.Enums.HashType;
29 import java.security.GeneralSecurityException;
30 import java.security.KeyFactory;
31 import java.security.Security;
32 import java.security.interfaces.RSAPrivateCrtKey;
33 import java.security.spec.RSAPrivateCrtKeySpec;
34 import org.conscrypt.Conscrypt;
35 import org.junit.Assume;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.junit.runners.JUnit4;
40 
41 @RunWith(JUnit4.class)
42 public final class RsaSsaPssSignJceFipsTest {
43 
44   @Before
useConscrypt()45   public void useConscrypt() throws Exception {
46     Assume.assumeTrue(TinkFips.useOnlyFips());
47     Conscrypt.checkAvailability();
48     Security.addProvider(Conscrypt.newProvider());
49   }
50 
51   @Test
create_accepts2048ModulusIfFipsModuleIsAvailable()52   public void create_accepts2048ModulusIfFipsModuleIsAvailable() throws Exception {
53     RsaSsaPssParameters parameters =
54         RsaSsaPssParameters.builder()
55             .setModulusSizeBits(2048)
56             .setSigHashType(RsaSsaPssParameters.HashType.SHA256)
57             .setMgf1HashType(RsaSsaPssParameters.HashType.SHA256)
58             .setVariant(RsaSsaPssParameters.Variant.NO_PREFIX)
59             .setSaltLengthBytes(32)
60             .build();
61     RsaSsaPssPrivateKey privateKey =
62         RsaSsaPssTestUtil.privateKeyFor2048BitParameters(parameters, null);
63 
64     if (TinkFipsUtil.fipsModuleAvailable()) {
65       assertThat(RsaSsaPssSignJce.create(privateKey)).isNotNull();
66     } else {
67       assertThrows(GeneralSecurityException.class, () -> RsaSsaPssSignJce.create(privateKey));
68     }
69   }
70 
71   @Test
constructor_accepts2048ModulusIfFipsModuleIsAvailable()72   public void constructor_accepts2048ModulusIfFipsModuleIsAvailable() throws Exception {
73     RsaSsaPssParameters parameters =
74         RsaSsaPssParameters.builder()
75             .setModulusSizeBits(2048)
76             .setSigHashType(RsaSsaPssParameters.HashType.SHA256)
77             .setMgf1HashType(RsaSsaPssParameters.HashType.SHA256)
78             .setVariant(RsaSsaPssParameters.Variant.NO_PREFIX)
79             .setSaltLengthBytes(32)
80             .build();
81     RsaSsaPssPrivateKey privateKey =
82         RsaSsaPssTestUtil.privateKeyFor2048BitParameters(parameters, null);
83     KeyFactory keyFactory = EngineFactory.KEY_FACTORY.getInstance("RSA");
84     RSAPrivateCrtKey rsaPrivateCrtKey =
85         (RSAPrivateCrtKey)
86             keyFactory.generatePrivate(
87                 new RSAPrivateCrtKeySpec(
88                     privateKey.getPublicKey().getModulus(),
89                     privateKey.getPublicKey().getParameters().getPublicExponent(),
90                     privateKey.getPrivateExponent().getBigInteger(InsecureSecretKeyAccess.get()),
91                     privateKey.getPrimeP().getBigInteger(InsecureSecretKeyAccess.get()),
92                     privateKey.getPrimeQ().getBigInteger(InsecureSecretKeyAccess.get()),
93                     privateKey.getPrimeExponentP().getBigInteger(InsecureSecretKeyAccess.get()),
94                     privateKey.getPrimeExponentQ().getBigInteger(InsecureSecretKeyAccess.get()),
95                     privateKey.getCrtCoefficient().getBigInteger(InsecureSecretKeyAccess.get())));
96 
97     if (TinkFipsUtil.fipsModuleAvailable()) {
98       assertThat(new RsaSsaPssSignJce(rsaPrivateCrtKey, HashType.SHA256, HashType.SHA256, 64))
99           .isNotNull();
100     } else {
101       assertThrows(
102           GeneralSecurityException.class,
103           () -> new RsaSsaPssSignJce(rsaPrivateCrtKey, HashType.SHA256, HashType.SHA256, 64));
104     }
105   }
106 
107   @Test
create_refuses4096Modulus()108   public void create_refuses4096Modulus() throws Exception {
109     RsaSsaPssParameters parameters =
110         RsaSsaPssParameters.builder()
111             .setModulusSizeBits(4096)
112             .setSigHashType(RsaSsaPssParameters.HashType.SHA256)
113             .setMgf1HashType(RsaSsaPssParameters.HashType.SHA256)
114             .setVariant(RsaSsaPssParameters.Variant.NO_PREFIX)
115             .setSaltLengthBytes(32)
116             .build();
117     RsaSsaPssPrivateKey privateKey =
118         RsaSsaPssTestUtil.privateKeyFor4096BitParameters(parameters, null);
119 
120     assertThrows(GeneralSecurityException.class, () -> RsaSsaPssSignJce.create(privateKey));
121   }
122 
123   @Test
constructor_refuses4096Modulus()124   public void constructor_refuses4096Modulus() throws Exception {
125     RsaSsaPssParameters parameters =
126         RsaSsaPssParameters.builder()
127             .setModulusSizeBits(4096)
128             .setSigHashType(RsaSsaPssParameters.HashType.SHA256)
129             .setMgf1HashType(RsaSsaPssParameters.HashType.SHA256)
130             .setVariant(RsaSsaPssParameters.Variant.NO_PREFIX)
131             .setSaltLengthBytes(32)
132             .build();
133     RsaSsaPssPrivateKey privateKey =
134         RsaSsaPssTestUtil.privateKeyFor4096BitParameters(parameters, null);
135     KeyFactory keyFactory = EngineFactory.KEY_FACTORY.getInstance("RSA");
136     RSAPrivateCrtKey rsaPrivateCrtKey =
137         (RSAPrivateCrtKey)
138             keyFactory.generatePrivate(
139                 new RSAPrivateCrtKeySpec(
140                     privateKey.getPublicKey().getModulus(),
141                     privateKey.getPublicKey().getParameters().getPublicExponent(),
142                     privateKey.getPrivateExponent().getBigInteger(InsecureSecretKeyAccess.get()),
143                     privateKey.getPrimeP().getBigInteger(InsecureSecretKeyAccess.get()),
144                     privateKey.getPrimeQ().getBigInteger(InsecureSecretKeyAccess.get()),
145                     privateKey.getPrimeExponentP().getBigInteger(InsecureSecretKeyAccess.get()),
146                     privateKey.getPrimeExponentQ().getBigInteger(InsecureSecretKeyAccess.get()),
147                     privateKey.getCrtCoefficient().getBigInteger(InsecureSecretKeyAccess.get())));
148     assertThrows(
149         GeneralSecurityException.class,
150         () -> new RsaSsaPssSignJce(rsaPrivateCrtKey, HashType.SHA256, HashType.SHA256, 64));
151   }
152 }
153