|
| 1 | +// Copyright 2025 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.signature.internal; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | + |
| 22 | +import com.google.crypto.tink.PublicKeySign; |
| 23 | +import com.google.crypto.tink.PublicKeyVerify; |
| 24 | +import com.google.crypto.tink.config.TinkFips; |
| 25 | +import com.google.crypto.tink.config.internal.TinkFipsUtil; |
| 26 | +import com.google.crypto.tink.signature.EcdsaPrivateKey; |
| 27 | +import com.google.crypto.tink.signature.EcdsaPublicKey; |
| 28 | +import com.google.crypto.tink.signature.internal.testing.EcdsaTestUtil; |
| 29 | +import com.google.crypto.tink.signature.internal.testing.SignatureTestVector; |
| 30 | +import com.google.crypto.tink.subtle.EllipticCurves; |
| 31 | +import com.google.crypto.tink.subtle.EllipticCurves.EcdsaEncoding; |
| 32 | +import com.google.crypto.tink.subtle.Enums.HashType; |
| 33 | +import java.security.GeneralSecurityException; |
| 34 | +import java.security.KeyPair; |
| 35 | +import java.security.KeyPairGenerator; |
| 36 | +import java.security.Security; |
| 37 | +import java.security.interfaces.ECPrivateKey; |
| 38 | +import java.security.interfaces.ECPublicKey; |
| 39 | +import org.conscrypt.Conscrypt; |
| 40 | +import org.junit.Assume; |
| 41 | +import org.junit.Before; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.junit.runners.JUnit4; |
| 45 | + |
| 46 | +/** |
| 47 | + * Unit tests for EcdsaSignJce and EcdsaVerifyJce in FIPS mode. |
| 48 | + * |
| 49 | + * <p>This test should be run with the <code>--use_only_fips=true</code>, both with the BoringCrypto |
| 50 | + * FIPS module enabled and disabled with <code>--define=BORINGSSL_FIPS=0</code>. |
| 51 | + */ |
| 52 | +@RunWith(JUnit4.class) |
| 53 | +public final class EcdsaSignVerifyFipsTest { |
| 54 | + |
| 55 | + @Before |
| 56 | + public void useConscrypt() throws Exception { |
| 57 | + Assume.assumeTrue(TinkFips.useOnlyFips()); |
| 58 | + Conscrypt.checkAvailability(); |
| 59 | + Security.addProvider(Conscrypt.newProvider()); |
| 60 | + } |
| 61 | + |
| 62 | + private static final SignatureTestVector[] testVectors = EcdsaTestUtil.createEcdsaTestVectors(); |
| 63 | + |
| 64 | + @Test |
| 65 | + public void createWorksIfFipsModuleAvailable() throws Exception { |
| 66 | + Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); |
| 67 | + for (SignatureTestVector testVector : testVectors) { |
| 68 | + PublicKeySign signer = EcdsaSignJce.create((EcdsaPrivateKey) testVector.getPrivateKey()); |
| 69 | + byte[] signature = signer.sign(testVector.getMessage()); |
| 70 | + PublicKeyVerify verifier = |
| 71 | + EcdsaVerifyJce.create((EcdsaPublicKey) testVector.getPrivateKey().getPublicKey()); |
| 72 | + verifier.verify(signature, testVector.getMessage()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void createThrowsIfFipsModuleNotAvailable() throws Exception { |
| 78 | + Assume.assumeFalse(TinkFipsUtil.fipsModuleAvailable()); |
| 79 | + for (SignatureTestVector testVector : testVectors) { |
| 80 | + assertThrows( |
| 81 | + GeneralSecurityException.class, |
| 82 | + () -> EcdsaSignJce.create((EcdsaPrivateKey) testVector.getPrivateKey())); |
| 83 | + assertThrows( |
| 84 | + GeneralSecurityException.class, |
| 85 | + () -> EcdsaVerifyJce.create((EcdsaPublicKey) testVector.getPrivateKey().getPublicKey())); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void constructorsWorkIfFipsModuleAvailable() throws Exception { |
| 91 | + Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); |
| 92 | + |
| 93 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); |
| 94 | + keyGen.initialize(EllipticCurves.getNistP256Params()); |
| 95 | + KeyPair keyPair = keyGen.generateKeyPair(); |
| 96 | + ECPublicKey pub = (ECPublicKey) keyPair.getPublic(); |
| 97 | + ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate(); |
| 98 | + |
| 99 | + byte[] message = "Hello".getBytes(UTF_8); |
| 100 | + |
| 101 | + EcdsaSignJce signer = new EcdsaSignJce(priv, HashType.SHA256, EcdsaEncoding.DER); |
| 102 | + byte[] signature = signer.sign(message); |
| 103 | + |
| 104 | + EcdsaVerifyJce verifier = new EcdsaVerifyJce(pub, HashType.SHA256, EcdsaEncoding.DER); |
| 105 | + verifier.verify(signature, message); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void constructorsDontValidateHashFunctionType() throws Exception { |
| 110 | + Assume.assumeTrue(TinkFipsUtil.fipsModuleAvailable()); |
| 111 | + // Using Curve P521 with SHA256 is not allowed by the FIPS 140-2, see |
| 112 | + // https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf Page 21. |
| 113 | + // |
| 114 | + // This is currently not enforced. |
| 115 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); |
| 116 | + keyGen.initialize(EllipticCurves.getNistP521Params()); |
| 117 | + KeyPair keyPair = keyGen.generateKeyPair(); |
| 118 | + ECPublicKey pub = (ECPublicKey) keyPair.getPublic(); |
| 119 | + ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate(); |
| 120 | + |
| 121 | + byte[] message = "Hello".getBytes(UTF_8); |
| 122 | + |
| 123 | + EcdsaSignJce signer = new EcdsaSignJce(priv, HashType.SHA256, EcdsaEncoding.DER); |
| 124 | + byte[] signature = signer.sign(message); |
| 125 | + |
| 126 | + EcdsaVerifyJce verifier = new EcdsaVerifyJce(pub, HashType.SHA256, EcdsaEncoding.DER); |
| 127 | + verifier.verify(signature, message); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void constructorThrowsIfFipsModuleNotAvailable() throws Exception { |
| 132 | + Assume.assumeFalse(TinkFipsUtil.fipsModuleAvailable()); |
| 133 | + |
| 134 | + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); |
| 135 | + keyGen.initialize(EllipticCurves.getNistP256Params()); |
| 136 | + KeyPair keyPair = keyGen.generateKeyPair(); |
| 137 | + ECPublicKey pub = (ECPublicKey) keyPair.getPublic(); |
| 138 | + ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate(); |
| 139 | + |
| 140 | + assertThrows( |
| 141 | + GeneralSecurityException.class, |
| 142 | + () -> new EcdsaSignJce(priv, HashType.SHA256, EcdsaEncoding.DER)); |
| 143 | + assertThrows( |
| 144 | + GeneralSecurityException.class, |
| 145 | + () -> new EcdsaVerifyJce(pub, HashType.SHA256, EcdsaEncoding.DER)); |
| 146 | + } |
| 147 | +} |
0 commit comments