Skip to content

Commit

Permalink
Add test for interop public key calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
KostasTsiounis committed Dec 9, 2024
1 parent b5bc995 commit e19ac3d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,48 @@
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import sun.security.util.InternalPrivateKey;

public class BaseTestECKeyImportInterop extends BaseTestJunit5Interop {

static final byte[] origMsg = "this is the original message to be signed".getBytes();

@Test
public void testCreateKeyPairECGenParamImportCalculatePublic() throws Exception {
doCreateKeyPairECGenParamImportCalculatePublic(providerName, interopProviderName);
doCreateKeyPairECGenParamImportCalculatePublic(interopProviderName, providerName);
}

public void doCreateKeyPairECGenParamImportCalculatePublic(String generateProviderName,
String importProviderName) throws Exception {

//final String methodName = "testECGenParamImportHardcoded";
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("EC", generateProviderName);

keyPairGen.initialize(256);
KeyPair keyPair = keyPairGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();

// Recreate private key from encoding.
byte[] privKeyBytes = privateKey.getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance("EC", importProviderName);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);

// Get public key bytes from private.
byte[] calculatedPublicKey = ((InternalPrivateKey) privateKey).calculatePublicKey().getEncoded();

// Get public key bytes from original public key.
byte[] publicKeyBytes = publicKey.getEncoded();

// The original and calculated public keys should be the same
Assert.assertArrayEquals(calculatedPublicKey, publicKeyBytes);
}

@Test
public void testCreateKeyPairECGenParamImport() throws Exception {
doCreateKeyPairECGenParamImport(getProviderName(), getInteropProviderName());
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/ibm/jceplus/junit/base/BaseTestXDHInterop.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.EncodedKeySpec;
import java.security.spec.NamedParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import sun.security.util.InternalPrivateKey;

public class BaseTestXDHInterop extends BaseTestJunit5Interop {

Expand All @@ -29,6 +33,39 @@ public class BaseTestXDHInterop extends BaseTestJunit5Interop {
String openJDK_private_X448 = "MEYCAQAwBwYDK2VvBQAEOOJFsgLYxgAIEWuN1FLAGWDzGQRSataAbPLDc1wv5aky4T8hevyWbYdhggc1OCcqQ93gY8rqVTDb";
// OpenJDK does not currently support FFDHE hence interop testing for FFDHE is not possible

@Test
public void testCreateKeyPairXDHGenParamImportCalculatePublic() throws Exception {
doCreateKeyPairXDHGenParamImportCalculatePublic(providerName, interopProviderName);
doCreateKeyPairXDHGenParamImportCalculatePublic(interopProviderName, providerName);
}

public void doCreateKeyPairXDHGenParamImportCalculatePublic(String generateProviderName,
String importProviderName) throws Exception {

//final String methodName = "testECGenParamImportHardcoded";
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("XDH", generateProviderName);

keyPairGen.initialize(255);
KeyPair keyPair = keyPairGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();

// Recreate private key from encoding.
byte[] privKeyBytes = privateKey.getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance("XDH", importProviderName);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);

// Get public key bytes from private.
byte[] calculatedPublicKey = ((InternalPrivateKey) privateKey).calculatePublicKey().getEncoded();

// Get public key bytes from original public key.
byte[] publicKeyBytes = publicKey.getEncoded();

// The original and calculated public keys should be the same
Assert.assertArrayEquals(calculatedPublicKey, publicKeyBytes);
}

@Test
public void testXDHInterop_X25519_OpenJDK() throws Exception {
byte[] openJDK_public_bytes = Base64.getDecoder().decode(openJDK_public_X25519);
Expand Down

0 comments on commit e19ac3d

Please sign in to comment.