Skip to content

Commit

Permalink
Merge pull request #60 from pushkarnk/ka-improve-test
Browse files Browse the repository at this point in the history
test: ensure KeyAgreement objects could be reused
  • Loading branch information
pushkarnk authored Jan 8, 2025
2 parents 28f0394 + 743e788 commit 781a0d3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/test/java/KeyAgreementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ public static void addProvider() {
}

private void runTest(KeyPairGenerator kpg, String algo) throws Exception {
KeyPair aliceKp = kpg.generateKeyPair();
KeyPair bobKp = kpg.generateKeyPair();
KeyAgreement aliceAgreement = KeyAgreement.getInstance(algo, "OpenSSLFIPSProvider");
KeyAgreement bobAgreement = KeyAgreement.getInstance(algo, "OpenSSLFIPSProvider");
aliceAgreement.init(aliceKp.getPrivate());
aliceAgreement.doPhase(bobKp.getPublic(), true);
bobAgreement.init(bobKp.getPrivate());
bobAgreement.doPhase(aliceKp.getPublic(), true);
byte[] aliceSecret = aliceAgreement.generateSecret();
byte[] bobSecret = bobAgreement.generateSecret();
assertArrayEquals("Key Agreement test for " + algo + " failed", aliceSecret, bobSecret);

for (int i = 0; i < 2; i++) {
KeyPair aliceKp = kpg.generateKeyPair();
KeyPair bobKp = kpg.generateKeyPair();
aliceAgreement.init(aliceKp.getPrivate());
aliceAgreement.doPhase(bobKp.getPublic(), true);
bobAgreement.init(bobKp.getPrivate());
bobAgreement.doPhase(aliceKp.getPublic(), true);
byte[] aliceSecret = aliceAgreement.generateSecret();
byte[] bobSecret = bobAgreement.generateSecret();
assertArrayEquals("Key Agreement test for " + algo + " failed", aliceSecret, bobSecret);

// make sure generateSecret() resets the KeyAgreement object
// and can be reused for another agreement
}
}

@Test
Expand Down

0 comments on commit 781a0d3

Please sign in to comment.