From d31014e2c52158de7791c2015f040717d8bb47f0 Mon Sep 17 00:00:00 2001 From: John Jiang Date: Fri, 13 Oct 2023 13:09:31 +0800 Subject: [PATCH] TKSS-452: Tests should not depend on provider-internal classes --- .../com/tencent/kona/crypto/TestUtils.java | 34 ++++++++++++--- .../kona/crypto/provider/SM2CipherTest.java | 8 +++- .../crypto/provider/SM2KeyAgreementTest.java | 30 ++++++------- .../crypto/provider/SM2SignatureTest.java | 8 +++- .../kona/crypto/provider/SM2WithBCTest.java | 43 ++++++------------- .../crypto/provider/SM4ParametersTest.java | 6 +-- 6 files changed, 71 insertions(+), 58 deletions(-) diff --git a/kona-crypto/src/test/java/com/tencent/kona/crypto/TestUtils.java b/kona-crypto/src/test/java/com/tencent/kona/crypto/TestUtils.java index 935d9203..c54f5c4c 100644 --- a/kona-crypto/src/test/java/com/tencent/kona/crypto/TestUtils.java +++ b/kona-crypto/src/test/java/com/tencent/kona/crypto/TestUtils.java @@ -19,11 +19,16 @@ package com.tencent.kona.crypto; -import com.tencent.kona.crypto.provider.SM2PrivateKey; -import com.tencent.kona.crypto.provider.SM2PublicKey; +import com.tencent.kona.crypto.spec.SM2PrivateKeySpec; +import com.tencent.kona.crypto.spec.SM2PublicKeySpec; +import java.security.KeyFactory; import java.security.KeyPair; +import java.security.PrivateKey; +import java.security.PublicKey; import java.security.Security; +import java.security.interfaces.ECPrivateKey; +import java.security.interfaces.ECPublicKey; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; @@ -111,10 +116,29 @@ public static byte[] dataMB(int sizeInMB) { } public static KeyPair keyPair(String publicKeyHex, String privateKeyHex) { - SM2PublicKey publicKeySpec = new SM2PublicKey(toBytes(publicKeyHex)); - SM2PrivateKey privateKeySpec = new SM2PrivateKey(toBytes(privateKeyHex)); + try { + KeyFactory keyFactory = KeyFactory.getInstance("SM2"); + + PrivateKey privateKey = keyFactory.generatePrivate( + new SM2PrivateKeySpec(toBytes(privateKeyHex))); + PublicKey publicKey = keyFactory.generatePublic( + new SM2PublicKeySpec(toBytes(publicKeyHex))); + return new KeyPair(publicKey, privateKey); + } catch (Exception e) { + throw new RuntimeException("Create key pair failed", e); + } + } + + public static ECPrivateKey privateKey(String hex) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance("SM2"); + return (ECPrivateKey) keyFactory.generatePrivate( + new SM2PrivateKeySpec(toBytes(hex))); + } - return new KeyPair(publicKeySpec, privateKeySpec); + public static ECPublicKey publicKey(String hex) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance("SM2"); + return (ECPublicKey) keyFactory.generatePublic( + new SM2PublicKeySpec(toBytes(hex))); } @FunctionalInterface diff --git a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2CipherTest.java b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2CipherTest.java index d581d1ce..759466c8 100644 --- a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2CipherTest.java +++ b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2CipherTest.java @@ -183,12 +183,16 @@ public void testKeyRange() throws Exception { } private void testKeyRange(int orderOffset) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance("SM2"); + BigInteger privateKeyS = ECOperator.SM2.getOrder().subtract( BigInteger.valueOf(orderOffset)); - ECPrivateKey privateKey = new SM2PrivateKey(privateKeyS); + ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate( + new SM2PrivateKeySpec(privateKeyS.toByteArray())); ECPoint publicPoint = ECOperator.SM2.multiply(privateKeyS); - ECPublicKey publicKey = new SM2PublicKey(publicPoint); + ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic( + new SM2PublicKeySpec(publicPoint)); Cipher cipher = Cipher.getInstance("SM2", PROVIDER); diff --git a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2KeyAgreementTest.java b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2KeyAgreementTest.java index dc423fe7..a6722745 100644 --- a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2KeyAgreementTest.java +++ b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2KeyAgreementTest.java @@ -52,8 +52,8 @@ public static void setup() { @Test public void testKeyAgreementInit() throws Exception { - ECPrivateKey priKey = new SM2PrivateKey(toBytes(PRI_KEY)); - ECPublicKey pubKey = new SM2PublicKey(toBytes(PUB_KEY)); + ECPrivateKey priKey = TestUtils.privateKey(PRI_KEY); + ECPublicKey pubKey = TestUtils.publicKey(PUB_KEY); SM2KeyAgreementParamSpec paramSpec = new SM2KeyAgreementParamSpec( ID, @@ -70,7 +70,7 @@ public void testKeyAgreementInit() throws Exception { @Test public void testKeyAgreementInitWithoutParams() throws Exception { - ECPrivateKey priKey = new SM2PrivateKey(toBytes(PRI_KEY)); + ECPrivateKey priKey = TestUtils.privateKey(PRI_KEY); KeyAgreement keyAgreement = KeyAgreement.getInstance("SM2", PROVIDER); Assertions.assertThrows( UnsupportedOperationException.class, @@ -79,8 +79,8 @@ public void testKeyAgreementInitWithoutParams() throws Exception { @Test public void testKeyAgreementDoPhase() throws Exception { - ECPrivateKey priKey = new SM2PrivateKey(toBytes(PRI_KEY)); - ECPublicKey pubKey = new SM2PublicKey(toBytes(PUB_KEY)); + ECPrivateKey priKey = TestUtils.privateKey(PRI_KEY); + ECPublicKey pubKey = TestUtils.publicKey(PUB_KEY); SM2KeyAgreementParamSpec paramSpec = new SM2KeyAgreementParamSpec( ID, @@ -153,15 +153,15 @@ private void testSM2KeyAgreementKeySize(int keySize, byte[] expectedSharedKey) // Generate shared secret by the local endpoint SM2KeyAgreementParamSpec paramSpec = new SM2KeyAgreementParamSpec( toBytes(idHex), - new SM2PrivateKey(toBytes(priKeyHex)), - new SM2PublicKey(toBytes(pubKeyHex)), + TestUtils.privateKey(priKeyHex), + TestUtils.publicKey(pubKeyHex), toBytes(peerIdHex), - new SM2PublicKey(toBytes(peerPubKeyHex)), + TestUtils.publicKey(peerPubKeyHex), true, keySize); KeyAgreement keyAgreement = KeyAgreement.getInstance("SM2", PROVIDER); - keyAgreement.init(new SM2PrivateKey(toBytes(tmpPriKeyHex)), paramSpec); - keyAgreement.doPhase(new SM2PublicKey(toBytes(peerTmpPubKeyHex)), true); + keyAgreement.init(TestUtils.privateKey(tmpPriKeyHex), paramSpec); + keyAgreement.doPhase(TestUtils.publicKey(peerTmpPubKeyHex), true); SecretKey sharedKey = keyAgreement.generateSecret("SM2SharedSecret"); Assertions.assertEquals(keySize, sharedKey.getEncoded().length); @@ -172,15 +172,15 @@ private void testSM2KeyAgreementKeySize(int keySize, byte[] expectedSharedKey) // Generate shared secret by the remote endpoint SM2KeyAgreementParamSpec peerParamSpec = new SM2KeyAgreementParamSpec( toBytes(peerIdHex), - new SM2PrivateKey(toBytes(peerPriKeyHex)), - new SM2PublicKey(toBytes(peerPubKeyHex)), + TestUtils.privateKey(peerPriKeyHex), + TestUtils.publicKey(peerPubKeyHex), toBytes(idHex), - new SM2PublicKey(toBytes(pubKeyHex)), + TestUtils.publicKey(pubKeyHex), false, keySize); KeyAgreement peerKeyAgreement = KeyAgreement.getInstance("SM2", PROVIDER); - peerKeyAgreement.init(new SM2PrivateKey(toBytes(peerTmpPriKeyHex)), peerParamSpec); - peerKeyAgreement.doPhase(new SM2PublicKey(toBytes(tmpPubKeyHex)), true); + peerKeyAgreement.init(TestUtils.privateKey(peerTmpPriKeyHex), peerParamSpec); + peerKeyAgreement.doPhase(TestUtils.publicKey(tmpPubKeyHex), true); SecretKey peerSharedKey = peerKeyAgreement.generateSecret("SM2SharedSecret"); Assertions.assertArrayEquals(sharedKey.getEncoded(), peerSharedKey.getEncoded()); diff --git a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2SignatureTest.java b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2SignatureTest.java index 809748b3..45a2e53e 100644 --- a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2SignatureTest.java +++ b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2SignatureTest.java @@ -161,12 +161,16 @@ public void testKeyRange() throws Exception { // orderOffset: the relative offset to the order private void testKeyRange(int orderOffset) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance("SM2"); + BigInteger privateKeyS = ECOperator.SM2.getOrder().subtract( BigInteger.valueOf(orderOffset)); - ECPrivateKey privateKey = new SM2PrivateKey(privateKeyS); + ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate( + new SM2PrivateKeySpec(privateKeyS.toByteArray())); ECPoint publicPoint = ECOperator.SM2.multiply(privateKeyS); - ECPublicKey publicKey = new SM2PublicKey(publicPoint); + ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic( + new SM2PublicKeySpec(publicPoint)); Signature signer = Signature.getInstance("SM2", PROVIDER); signer.initSign(privateKey); diff --git a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2WithBCTest.java b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2WithBCTest.java index 119d711a..6876ed31 100644 --- a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2WithBCTest.java +++ b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM2WithBCTest.java @@ -76,19 +76,15 @@ public static void setup() { @Test public void testKonaCipherWithBCKeyPair() throws Exception { - KeyPair keyPairBC = keyPairBC(); - KeyPair keyPair = toKonaKeyPair(keyPairBC); - testCipher(keyPair, keyPairBC); + testCipher(keyPairBC()); } @Test public void testBCCipherWithKonaKeyPair() throws Exception { - KeyPair keyPair = keyPair(); - KeyPair keyPairBC = toBCKeyPair(keyPair); - testCipher(keyPair, keyPairBC); + testCipher(keyPair()); } - private void testCipher(KeyPair keyPair, KeyPair keyPairBC) + private void testCipher(KeyPair keyPair) throws Exception { Cipher cipher = Cipher.getInstance("SM2", PROVIDER); cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); @@ -97,7 +93,7 @@ private void testCipher(KeyPair keyPair, KeyPair keyPairBC) byte[] ciphertext = cipher.doFinal(MESSAGE); Cipher cipherBC = Cipher.getInstance("SM2", "BC"); - cipherBC.init(Cipher.ENCRYPT_MODE, keyPairBC.getPublic()); + cipherBC.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); // Ciphertext produced by BC // The format is C1||C2||C3 byte[] ciphertextBC = cipherBC.doFinal(MESSAGE); @@ -112,7 +108,7 @@ private void testCipher(KeyPair keyPair, KeyPair keyPairBC) byte[] cleartext = cipher.doFinal(derC1C3C2); Assertions.assertArrayEquals(MESSAGE, cleartext); - cipherBC.init(Cipher.DECRYPT_MODE, keyPairBC.getPrivate()); + cipherBC.init(Cipher.DECRYPT_MODE, keyPair.getPrivate()); // Convert Kona ciphertext to BC ciphertext byte[] rawC1C2C3 = SM2Ciphertext.builder() .format(SM2Ciphertext.Format.DER_C1C3C2) @@ -125,20 +121,15 @@ private void testCipher(KeyPair keyPair, KeyPair keyPairBC) @Test public void testKonaSignatureWithBCKeyPair() throws Exception { - KeyPair keyPairBC = keyPairBC(); - KeyPair keyPair = toKonaKeyPair(keyPairBC); - testSignature(keyPair, keyPairBC); + testSignature(keyPairBC()); } @Test public void testBCSignatureWithKonaKeyPair() throws Exception { - KeyPair keyPair = keyPair(); - KeyPair keyPairBC = toBCKeyPair(keyPair); - testSignature(keyPair, keyPairBC); + testSignature(keyPair()); } - private void testSignature(KeyPair keyPair, KeyPair keyPairBC) - throws Exception { + private void testSignature(KeyPair keyPair) throws Exception { Signature signature = Signature.getInstance("SM2", PROVIDER); SM2SignatureParameterSpec paramSpec = new SM2SignatureParameterSpec( Constants.defaultId(), @@ -154,11 +145,12 @@ private void testSignature(KeyPair keyPair, KeyPair keyPairBC) SM2ParameterSpec paramSpecBC = new SM2ParameterSpec( Constants.defaultId()); signatureBC.setParameter(paramSpecBC); - signatureBC.initSign(keyPairBC.getPrivate()); + signatureBC.initSign(keyPair.getPrivate()); signatureBC.update(MESSAGE); // Signature produced by BC byte[] signBC = signatureBC.sign(); + signature = Signature.getInstance("SM2", PROVIDER); signature.setParameter(paramSpec); signature.initVerify(keyPair.getPublic()); signature.update(MESSAGE); @@ -166,7 +158,7 @@ private void testSignature(KeyPair keyPair, KeyPair keyPairBC) Assertions.assertTrue(signature.verify(signBC)); signatureBC.setParameter(paramSpecBC); - signatureBC.initVerify(keyPairBC.getPublic()); + signatureBC.initVerify(keyPair.getPublic()); signatureBC.update(MESSAGE); // BC verifies the signature produced by Kona Assertions.assertTrue(signatureBC.verify(sign)); @@ -237,18 +229,9 @@ private void testKeyAgreement(boolean konaIsInitiator) throws Exception { Assertions.assertArrayEquals(sharedKey, sharedKeyBC); } - private KeyPair toKonaKeyPair(KeyPair keyPairBC) { - BCECPublicKey pubKeyBC = (BCECPublicKey) keyPairBC.getPublic(); - BCECPrivateKey priKeyBC = (BCECPrivateKey) keyPairBC.getPrivate(); - - return new KeyPair( - new SM2PublicKey(pubKeyBC.getW()), - new SM2PrivateKey(priKeyBC.getS())); - } - private KeyPair toBCKeyPair(KeyPair keyPair) { - SM2PublicKey pubKey = (SM2PublicKey) keyPair.getPublic(); - SM2PrivateKey priKey = (SM2PrivateKey) keyPair.getPrivate(); + ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic(); + ECPrivateKey priKey = (ECPrivateKey) keyPair.getPrivate(); BCECPublicKey pubKeyBC = new BCECPublicKey(pubKey, null); BCECPrivateKey priKeyBC = new BCECPrivateKey(priKey, null); diff --git a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM4ParametersTest.java b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM4ParametersTest.java index 9440ddcc..907718a3 100644 --- a/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM4ParametersTest.java +++ b/kona-crypto/src/test/java/com/tencent/kona/crypto/provider/SM4ParametersTest.java @@ -29,12 +29,10 @@ import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; import java.security.AlgorithmParameters; import java.security.spec.InvalidParameterSpecException; import static com.tencent.kona.crypto.CryptoUtils.toBytes; -import static com.tencent.kona.crypto.CryptoUtils.toHex; /** * The test for the AlgorithmParameters on SM4. @@ -42,8 +40,8 @@ public class SM4ParametersTest { private static final byte[] KEY = toBytes("0123456789abcdef0123456789abcdef"); - private static final byte[] IV = "0123456789abcdef".getBytes(StandardCharsets.UTF_8); - private static final byte[] GCM_IV = "0123456789ab".getBytes(StandardCharsets.UTF_8); + private static final byte[] IV = toBytes("30313233343536373839616263646566"); + private static final byte[] GCM_IV = toBytes("303132333435363738396162"); private static final byte[] ENCODED_PARAMS = toBytes("041030313233343536373839616263646566"); private static final byte[] GCM_ENCODED_PARAMS = toBytes("3011040c303132333435363738396162020110");