Skip to content

Commit

Permalink
TKSS-452: Tests should not depend on provider-internal classes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Oct 13, 2023
1 parent 20fa21a commit 8204667
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 58 deletions.
34 changes: 29 additions & 5 deletions kona-crypto/src/test/java/com/tencent/kona/crypto/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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(),
Expand All @@ -154,19 +145,20 @@ 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);
// Kona verifies the signature produced by BC
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));
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@
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.
*/
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");

Expand Down

0 comments on commit 8204667

Please sign in to comment.