Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sp717 committed Aug 23, 2024
1 parent 928b9a0 commit 40c3f96
Showing 1 changed file with 64 additions and 47 deletions.
111 changes: 64 additions & 47 deletions tst/com/amazon/corretto/crypto/provider/test/EdDSATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
package com.amazon.corretto.crypto.provider.test;

import static com.amazon.corretto.crypto.provider.test.TestUtil.NATIVE_PROVIDER;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Random;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -61,8 +64,8 @@ public void uniqueKeyGen() {
final byte[] pbk1 = kp1.getPublic().getEncoded();
final byte[] pbk2 = kp2.getPublic().getEncoded();

assertTrue(!Arrays.equals(pk1, pk2));
assertTrue(!Arrays.equals(pbk1, pbk2));
assertFalse(Arrays.equals(pk1, pk2));
assertFalse(Arrays.equals(pbk1, pbk2));
}

@Test
Expand Down Expand Up @@ -107,8 +110,8 @@ public void keyFactoryValidation() throws GeneralSecurityException {
final byte[] publicKeyACCP = kf.generatePublic(publicKeyX509).getEncoded();

// Confirm that ACCP & SunEC keys are equivalent
assertTrue(Arrays.equals(privateKeyACCP, privateKeyJCE));
assertTrue(Arrays.equals(publicKeyACCP, publicKeyJCE));
assertArrayEquals(privateKeyACCP, privateKeyJCE);
assertArrayEquals(publicKeyACCP, publicKeyJCE);
}

@Test
Expand All @@ -122,55 +125,69 @@ public void jceInteropValidation() throws GeneralSecurityException {
// Set up ACCP and JCE Signature Instances
final Signature nativeSig = Signature.getInstance("Ed25519", NATIVE_PROVIDER);
final Signature jceSig = Signature.getInstance("Ed25519", "SunEC");

// Sign with ACCP and verify with SunEC
final byte[] message = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);
final byte[] signatureACCP = nativeSig.sign();
jceSig.initVerify(publicKey);
jceSig.update(message);
assertTrue(jceSig.verify(signatureACCP), "Native->JCE: Ed25519");

// Sign with SunEC and verify with ACCP
jceSig.initSign(privateKey);
jceSig.update(message, 0, message.length);
final byte[] signatureJCE = jceSig.sign();
nativeSig.initVerify(publicKey);
nativeSig.update(message);
assertTrue(nativeSig.verify(signatureJCE), "JCE->Native: Ed25519");

assertTrue(Arrays.equals(signatureJCE, signatureACCP));
byte[] message, signatureACCP, signatureJCE;
Random random = new Random();

for (int messageLength = 1; messageLength <= 1024; messageLength++) {
message = new byte[messageLength];
random.nextBytes(message);
// Sign with ACCP and verify with SunEC
nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);
signatureACCP = nativeSig.sign();
jceSig.initVerify(publicKey);
jceSig.update(message);
assertTrue(
jceSig.verify(signatureACCP),
"Native->JCE: Ed25519 (message length: " + messageLength + ")");

// Sign with SunEC and verify with ACCP
jceSig.initSign(privateKey);
jceSig.update(message, 0, message.length);
signatureJCE = jceSig.sign();
nativeSig.initVerify(publicKey);
nativeSig.update(message);
assertTrue(
nativeSig.verify(signatureJCE),
"JCE->Native: Ed25519 (message length: " + messageLength + ")");

assertArrayEquals(signatureJCE, signatureACCP);
}
}

@Test
public void bcInteropValidation() throws GeneralSecurityException {
// 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);
final KeyPair keyPair = nativeGen.generateKeyPair();

final PrivateKey privateKey = keyPair.getPrivate();
final PublicKey publicKey = keyPair.getPublic();

// Sign with ACCP, Verify with BouncyCastle
nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);
final byte[] signatureACCP = nativeSig.sign();
bcSig.initVerify(publicKey);
bcSig.update(message);
assertTrue(bcSig.verify(signatureACCP), "Native->BC: Ed25519");

// Sign with BouncyCastle, Verify with ACCP
bcSig.initSign(privateKey);
bcSig.update(message, 0, message.length);
final byte[] signatureBC = bcSig.sign();
nativeSig.initVerify(publicKey);
nativeSig.update(message);
assertTrue(nativeSig.verify(signatureBC), "BC->Native: Ed25519");

assertTrue(Arrays.equals(signatureBC, signatureACCP));
byte[] message, signatureACCP, signatureBC;
Random random = new Random();

for (int messageLength = 1; messageLength <= 1024; messageLength++) {
message = new byte[messageLength];
random.nextBytes(message);
// Sign with ACCP, Verify with BouncyCastle
nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);
signatureACCP = nativeSig.sign();
bcSig.initVerify(publicKey);
bcSig.update(message);
assertTrue(bcSig.verify(signatureACCP), "Native->BC: Ed25519");

// Sign with BouncyCastle, Verify with ACCP
bcSig.initSign(privateKey);
bcSig.update(message, 0, message.length);
signatureBC = bcSig.sign();
nativeSig.initVerify(publicKey);
nativeSig.update(message);
assertTrue(nativeSig.verify(signatureBC), "BC->Native: Ed25519");

assertArrayEquals(signatureBC, signatureACCP);
}
}

@Test
Expand All @@ -189,8 +206,8 @@ public void bcKeyValidation() throws GeneralSecurityException {
final byte[] pbkBC = kf.generatePublic(publicKeyX509).getEncoded();

// Confirm that ACCP & BC keys are equivalent
assertTrue(Arrays.equals(pkACCP, pkBC));
assertTrue(Arrays.equals(pbkACCP, pbkBC));
assertArrayEquals(pkACCP, pkBC);
assertArrayEquals(pbkACCP, pbkBC);
}

@Test
Expand Down Expand Up @@ -225,11 +242,11 @@ public void mismatchSignature() throws GeneralSecurityException {

nativeSig.initVerify(kp.getPublic());
nativeSig.update(message2, 0, message2.length);
assertTrue(!nativeSig.verify(signature));
assertFalse(nativeSig.verify(signature));

jceSig.initVerify(kp.getPublic());
jceSig.update(message2, 0, message2.length);
assertTrue(!jceSig.verify(signature));
assertFalse(jceSig.verify(signature));
}

@Test
Expand All @@ -256,7 +273,7 @@ public void testNullInputs() throws GeneralSecurityException {
TestUtil.assertThrows(NullPointerException.class, () -> jceSig.update((byte[]) null));
// Test with null signature
jceSig.initVerify(keyPair.getPublic());
assertTrue(!jceSig.verify(null));
assertFalse(jceSig.verify(null));

// Test BouncyCastle behavior
KeyPair keyPair2 = bcGen.generateKeyPair();
Expand Down

0 comments on commit 40c3f96

Please sign in to comment.