-
Notifications
You must be signed in to change notification settings - Fork 20
Add Check for PQCPrivateKey in Decapsulator and add tests. #1042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| package ibm.jceplus.junit.base; | ||
|
|
||
| import java.security.InvalidKeyException; | ||
| import java.security.KeyFactory; | ||
| import java.security.KeyPair; | ||
| import java.security.KeyPairGenerator; | ||
|
|
@@ -20,6 +21,7 @@ | |
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| public class BaseTestKEM extends BaseTestJunit5 { | ||
|
|
@@ -35,8 +37,6 @@ public void testKEM(String Algorithm) throws Exception { | |
| KEM kem = KEM.getInstance(Algorithm, getProviderName()); | ||
|
|
||
| KeyPair pqcKeyPair = generateKeyPair(Algorithm); | ||
| pqcKeyPair.getPublic(); | ||
| pqcKeyPair.getPrivate(); | ||
|
|
||
| KEM.Encapsulator encr = kem.newEncapsulator(pqcKeyPair.getPublic()); | ||
| KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); | ||
|
|
@@ -56,8 +56,6 @@ public void testKEMEmptyNoToFrom(String Algorithm) throws Exception { | |
| KEM kem = KEM.getInstance(Algorithm, getProviderName()); | ||
|
|
||
| KeyPair pqcKeyPair = generateKeyPair(Algorithm); | ||
| pqcKeyPair.getPublic(); | ||
| pqcKeyPair.getPrivate(); | ||
|
|
||
| KEM.Encapsulator encr = kem.newEncapsulator(pqcKeyPair.getPublic()); | ||
| KEM.Encapsulated enc = encr.encapsulate(); | ||
|
|
@@ -78,8 +76,6 @@ public void testKEMError(String Algorithm) throws Exception { | |
| KEM kem = KEM.getInstance(Algorithm, getProviderName()); | ||
|
|
||
| KeyPair pqcKeyPair = generateKeyPair(Algorithm); | ||
| pqcKeyPair.getPublic(); | ||
| pqcKeyPair.getPrivate(); | ||
|
|
||
| KEM.Encapsulator encr = kem.newEncapsulator(pqcKeyPair.getPublic()); | ||
| for (int i =0; i < 4; i++) { | ||
|
|
@@ -161,8 +157,6 @@ public void testKEMSmallerSecret(String Algorithm) throws Exception { | |
| KEM kem = KEM.getInstance(Algorithm, getProviderName()); | ||
|
|
||
| KeyPair pqcKeyPair = generateKeyPair(Algorithm); | ||
| pqcKeyPair.getPublic(); | ||
| pqcKeyPair.getPrivate(); | ||
|
|
||
| KEM.Encapsulator encr = kem.newEncapsulator(pqcKeyPair.getPublic()); | ||
| KEM.Encapsulated enc = encr.encapsulate(0, 16, "AES"); | ||
|
|
@@ -175,6 +169,47 @@ public void testKEMSmallerSecret(String Algorithm) throws Exception { | |
| assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({"ML-KEM", "ML-KEM-512", "ML_KEM_768", "ML_KEM_1024"}) | ||
| public void testKEMKeys(String Algorithm) throws Exception { | ||
|
|
||
| KEM kem = KEM.getInstance(Algorithm, getProviderName()); | ||
|
|
||
| KeyPair pqcKeyPair = generateKeyPair("RSA"); | ||
|
|
||
| try { | ||
| kem.newEncapsulator(pqcKeyPair.getPublic()); | ||
| fail("testKEMKeys failed - RSA Public key did not cause an Invalid Key Excepton."); | ||
| } catch (InvalidKeyException ike) { | ||
| assertTrue(ike.getMessage().equals("unsupported key")); | ||
| } | ||
|
|
||
| try { | ||
| kem.newDecapsulator(pqcKeyPair.getPrivate()); | ||
| fail("testKEMKeys failed - RSA Private key did not cause an Invalid Key Excepton."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a typo. Saying |
||
| } catch (InvalidKeyException ike) { | ||
| assertTrue(ike.getMessage().equals("unsupported key")); | ||
| } | ||
|
|
||
| // Test null keys | ||
| PublicKey pub = null; | ||
| PrivateKey priv = null; | ||
|
|
||
| try { | ||
| kem.newEncapsulator(pub); | ||
| fail("testKEMKeys failed - NULL Public key did not cause an Invalid Key Excepton."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a typo. Saying |
||
| } catch (InvalidKeyException ike) { | ||
| assertTrue(ike.getMessage().equals("Key is null.")); | ||
| } | ||
|
|
||
| try { | ||
| kem.newDecapsulator(priv); | ||
| fail("testKEMKeys failed - NULL Private key did not cause an Invalid Key Excepton."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a typo. Saying |
||
| } catch (InvalidKeyException ike) { | ||
| assertTrue(ike.getMessage().equals("Key is null.")); | ||
| } | ||
| } | ||
|
|
||
| protected KeyPair generateKeyPair(String Algorithm) throws Exception { | ||
| pqcKeyPairGen = KeyPairGenerator.getInstance(Algorithm, getProviderName()); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
|
|
@@ -74,6 +75,38 @@ public void testPQCKeyGenKEM_PlusToInterop() throws Exception { | |
| assertTrue(same); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPQCKeyGenKEMAutoKeyConvertion() throws Exception { | ||
| String pqcAlgorithm = "ML-KEM-512"; | ||
|
|
||
| if (getProviderName().equals("OpenJCEPlusFIPS") || | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could substitute this with: |
||
| getInteropProviderName().equals(Utils.PROVIDER_BC)) { | ||
| //This is not in the FIPS provider yet and Boucy Castle does not support this test. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return; | ||
| } | ||
|
|
||
| KEM kemInterop = KEM.getInstance(pqcAlgorithm, getProviderName()); | ||
|
|
||
| KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(pqcAlgorithm, getInteropProviderName()); | ||
| KeyPair keyPair = generateKeyPair(keyPairGen); | ||
|
|
||
| PublicKey publicKey = keyPair.getPublic(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| KEM.Encapsulator encr = kemInterop.newEncapsulator(publicKey); | ||
| KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); | ||
| if (enc == null){ | ||
| System.out.println("enc = null"); | ||
| fail("KEMPlusCreatesInteropGet failed no enc."); | ||
| } | ||
| SecretKey keyE = enc.key(); | ||
|
|
||
| KEM.Decapsulator decr = kemInterop.newDecapsulator(privateKey); | ||
| SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); | ||
|
|
||
| assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPQCKeyGenKEM_Interop() throws Exception { | ||
| String pqcAlgorithm = "ML-KEM-512"; | ||
|
|
@@ -337,7 +370,7 @@ public void testSignInteropKeysPlusSignVerify(String algorithm) { | |
| assertTrue(verifyingPlus.verify(signedBytesInterop), "Signature verification failed"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "SignInteropAndVerifyPlus failed"); | ||
| fail("SignInteropAndVerifyPlus failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -372,7 +405,7 @@ public void testSignPlusKeysInteropSignVerify(String algorithm) { | |
| assertTrue(verifyingPlus.verify(signedBytesInterop), "Signature verification failed"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "SignInteropAndVerifyPlus failed"); | ||
| fail("SignInteropAndVerifyPlus failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -408,7 +441,7 @@ public void testSignPlusAndVerifyInterop(String algorithm) { | |
| assertTrue(verifyingPlus.verify(signedBytesPlus), "Signature verification failed"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "SignPlusAndVerifyInterop failed"); | ||
| fail("SignPlusAndVerifyInterop failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -440,17 +473,17 @@ public void testKEMPlusKeyInteropAll(String Algorithm) { | |
| KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); | ||
| if (enc == null){ | ||
| System.out.println("enc = null"); | ||
| assertTrue(false, "KEMPlusCreatesInteropGet failed no enc."); | ||
| fail("KEMPlusCreatesInteropGet failed no enc."); | ||
| } | ||
| SecretKey keyE = enc.key(); | ||
|
|
||
| KEM.Decapsulator decr = kemInterop.newDecapsulator(privateKeyInterop); | ||
| SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); | ||
|
|
||
| assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); | ||
| assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "KEMPlusCreatesInteropGet failed"); | ||
| fail("KEMPlusCreatesInteropGet failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -482,17 +515,17 @@ public void testKEMInteropKeyPlusAll(String Algorithm) { | |
| KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); | ||
| if (enc == null){ | ||
| System.out.println("enc = null"); | ||
| assertTrue(false, "KEMPlusCreatesInteropGet failed no enc."); | ||
| fail("KEMPlusCreatesInteropGet failed no enc."); | ||
| } | ||
| SecretKey keyE = enc.key(); | ||
|
|
||
| KEM.Decapsulator decr = kemPlus.newDecapsulator(privateKeyPlus); | ||
| SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); | ||
|
|
||
| assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); | ||
| assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "KEMPlusCreatesInteropGet failed"); | ||
| fail("KEMPlusCreatesInteropGet failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -522,17 +555,17 @@ public void testKEMPlusCreatesInteropGet(String Algorithm) { | |
| KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); | ||
| if (enc == null){ | ||
| System.out.println("enc = null"); | ||
| assertTrue(false, "KEMPlusCreatesInteropGet failed no enc."); | ||
| fail("KEMPlusCreatesInteropGet failed no enc."); | ||
| } | ||
| SecretKey keyE = enc.key(); | ||
|
|
||
| KEM.Decapsulator decr = kemPlus.newDecapsulator(privateKeyPlus); | ||
| SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); | ||
|
|
||
| assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); | ||
| assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "KEMPlusCreatesInteropGet failed"); | ||
| fail("KEMPlusCreatesInteropGet failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -566,10 +599,10 @@ public void testKEMInteropCreatesPlusGet(String Algorithm) { | |
|
|
||
| SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); | ||
|
|
||
| assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); | ||
| assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| assertTrue(false, "KEMInteropCreatesPlusGet failed"); | ||
| fail("KEMInteropCreatesPlusGet failed"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a typo. Saying
Invalid Key Exceptoninstead ofInvalidKeyException.