Skip to content

Commit

Permalink
Update comments and thread storm test
Browse files Browse the repository at this point in the history
  • Loading branch information
sp717 committed Aug 7, 2024
1 parent 45c24cb commit d806610
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 63 deletions.
74 changes: 14 additions & 60 deletions tst/com/amazon/corretto/crypto/provider/test/EdDSATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,49 +69,21 @@ public void keyGenValidation() throws GeneralSecurityException {
final X509EncodedKeySpec publicKeyX509 =
new X509EncodedKeySpec(keyPair.getPublic().getEncoded());

final KeyFactory kf = KeyFactory.getInstance("EdDSA");
final KeyFactory kf = KeyFactory.getInstance("EdDSA", "SunEC");

final PrivateKey privateKey = kf.generatePrivate(privateKeyPkcs8);
final PublicKey publicKey = kf.generatePublic(publicKeyX509);

final Signature eddsa = Signature.getInstance("Ed25519");
final Signature eddsa = Signature.getInstance("Ed25519", "SunEC");

eddsa.initSign(privateKey);
eddsa.update(message, 0, message.length);

final byte[] signature = eddsa.sign();

eddsa.initVerify(publicKey);
eddsa.update(message);
assertTrue(eddsa.verify(signature));
}

@Test
public void reverseKeyFactoryValidation() throws GeneralSecurityException {
final KeyPair keyPair = nativeGen.generateKeyPair();

final byte[] privateKeyACCP = keyPair.getPrivate().getEncoded();
final byte[] publicKeyACCP = keyPair.getPublic().getEncoded();

final PKCS8EncodedKeySpec privateKeyPkcs8 = new PKCS8EncodedKeySpec(privateKeyACCP);
final X509EncodedKeySpec publicKeyX509 = new X509EncodedKeySpec(publicKeyACCP);

final KeyFactory kf = KeyFactory.getInstance("EdDSA");

final byte[] privateKeyJCE = kf.generatePrivate(privateKeyPkcs8).getEncoded();
final byte[] publicKeyJCE = kf.generatePublic(publicKeyX509).getEncoded();

assertTrue(privateKeyACCP.length == privateKeyJCE.length);
for (int i = 0; i < privateKeyACCP.length; i++) {
assertTrue(privateKeyACCP[i] == privateKeyJCE[i]);
}

assertTrue(publicKeyACCP.length == publicKeyJCE.length);
for (int i = 0; i < publicKeyACCP.length; i++) {
assertTrue(publicKeyACCP[i] == publicKeyJCE[i]);
}
}

@Test
public void keyFactoryValidation() throws GeneralSecurityException {
final KeyPair keyPair = jceGen.generateKeyPair();
Expand Down Expand Up @@ -140,7 +112,7 @@ public void keyFactoryValidation() throws GeneralSecurityException {

@Test
public void jceInteropValidation() throws GeneralSecurityException {
// Generate Keys with ACCP and use JCE KeyFactory to get equivalent JCE Keys
// Generate keys with ACCP and use JCE KeyFactory to get equivalent JCE Keys
final KeyPair keyPair = nativeGen.generateKeyPair();

final PKCS8EncodedKeySpec privateKeyPkcs8 =
Expand Down Expand Up @@ -176,7 +148,7 @@ public void jceInteropValidation() throws GeneralSecurityException {

@Test
public void bcInteropValidation() throws GeneralSecurityException {
// Generate Keys & Sign with ACCP, Verify with BouncyCastle
// Generate keys with ACCP and use BC KeyFactory to get equivalent JCE Keys
final byte[] message = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
final Signature nativeSig = Signature.getInstance("Ed25519", NATIVE_PROVIDER);
final Signature bcSig = Signature.getInstance("Ed25519", BOUNCYCASTLE_PROVIDER);
Expand All @@ -192,44 +164,25 @@ public void bcInteropValidation() throws GeneralSecurityException {
final PrivateKey privateKey = kf.generatePrivate(privateKeyPkcs8);
final PublicKey publicKey = kf.generatePublic(publicKeyX509);

// Sign with ACCP, Verify with BouncyCastle
nativeSig.initSign(keyPair.getPrivate());
nativeSig.update(message, 0, message.length);

final byte[] signature = nativeSig.sign();

byte[] signature = nativeSig.sign();
bcSig.initVerify(publicKey);
bcSig.update(message);
assertTrue(bcSig.verify(signature));
}
assertTrue(bcSig.verify(signature), "Native->BC: Ed25519");

@Test
public void bcInteropValidation2() throws GeneralSecurityException {
final byte[] message = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
final Signature nativeSig = Signature.getInstance("Ed25519", NATIVE_PROVIDER);
final KeyPair keyPair = bcGen.generateKeyPair();

final PKCS8EncodedKeySpec privateKeyPkcs8 =
new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded());
final X509EncodedKeySpec publicKeyX509 =
new X509EncodedKeySpec(keyPair.getPublic().getEncoded());

final KeyFactory kf = KeyFactory.getInstance("Ed25519", NATIVE_PROVIDER);

final PrivateKey privateKey = kf.generatePrivate(privateKeyPkcs8);
final PublicKey publicKey = kf.generatePublic(publicKeyX509);

nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);

final byte[] signature = nativeSig.sign();

nativeSig.initVerify(publicKey);
bcSig.initSign(privateKey);
bcSig.update(message, 0, message.length);
signature = bcSig.sign();
nativeSig.initVerify(keyPair.getPublic());
nativeSig.update(message);
assertTrue(nativeSig.verify(signature));
assertTrue(nativeSig.verify(signature), "BC->Native: Ed25519");
}

@Test
public void bcKeyValidation() throws GeneralSecurityException {
// Generate keys with ACCP and use BC KeyFactory to get equivalent JCE Keys
final KeyPair kp = nativeGen.generateKeyPair();
final byte[] pkACCP = kp.getPrivate().getEncoded();
final byte[] pbkACCP = kp.getPublic().getEncoded();
Expand All @@ -242,6 +195,7 @@ public void bcKeyValidation() throws GeneralSecurityException {
final byte[] pkBC = kf.generatePrivate(privateKeyPkcs8).getEncoded();
final byte[] pbkBC = kf.generatePublic(publicKeyX509).getEncoded();

// Confirm that ACCP & BC keys are equivalent
assertTrue(pkACCP.length == pkBC.length);
for (int i = 0; i < pkACCP.length; i++) {
assertTrue(pkACCP[i] == pkBC[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ public void edThreadStorm() throws Throwable {
for (int x = 0; x < threadCount; x++) {
final List<KeyPair> keys = new ArrayList<KeyPair>();
while (keys.size() < 2) {
if (rng.nextBoolean()) {
keys.add(PAIR_ED25519);
}
keys.add(PAIR_ED25519);
}
final TestThread t;
t = new SignatureTestThread("EddsaThread-" + x, rng, iterations, "Ed25519", keys);
Expand Down

0 comments on commit d806610

Please sign in to comment.