Skip to content

Commit

Permalink
Update kpg behavior and relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sp717 committed Aug 20, 2024
1 parent 915a1e2 commit 928b9a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
22 changes: 21 additions & 1 deletion src/com/amazon/corretto/crypto/provider/EdGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
// SPDX-License-Identifier: Apache-2.0
package com.amazon.corretto.crypto.provider;

import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGeneratorSpi;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

class EdGen extends KeyPairGeneratorSpi {
/** Generates a new Ed25519 key and returns a pointer to it. */
private static native long generateEvpEdKey();

private final AmazonCorrettoCryptoProvider provider_;
private final KeyFactory kf;

EdGen(AmazonCorrettoCryptoProvider provider) {
Loader.checkNativeLibraryAvailability();
provider_ = provider;
try {
kf = KeyFactory.getInstance("EdDSA", "SunEC");
} catch (GeneralSecurityException e) {
throw new RuntimeException("Error setting up KeyPairGenerator", e);
}
}

public void initialize(int keysize, SecureRandom random) {
Expand All @@ -27,6 +39,14 @@ public KeyPair generateKeyPair() {
final EvpEdPublicKey publicKey;
privateKey = new EvpEdPrivateKey(generateEvpEdKey());
publicKey = privateKey.getPublicKey();
return new KeyPair(publicKey, privateKey);
try {
final PKCS8EncodedKeySpec privateKeyPkcs8 = new PKCS8EncodedKeySpec(privateKey.getEncoded());
final X509EncodedKeySpec publicKeyX509 = new X509EncodedKeySpec(publicKey.getEncoded());
final PrivateKey jcePrivateKey = kf.generatePrivate(privateKeyPkcs8);
final PublicKey jcePublicKey = kf.generatePublic(publicKeyX509);
return new KeyPair(jcePublicKey, jcePrivateKey);
} catch (GeneralSecurityException e) {
throw new RuntimeException("Error generating key pair", e);
}
}
}
35 changes: 9 additions & 26 deletions tst/com/amazon/corretto/crypto/provider/test/EdDSATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,16 @@ public void jceInteropValidation() throws GeneralSecurityException {
// Generate keys with ACCP and use JCE KeyFactory to get equivalent JCE Keys
final KeyPair keyPair = nativeGen.generateKeyPair();

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

final KeyFactory kf = KeyFactory.getInstance("Ed25519", "SunEC");
final PrivateKey privateKey = kf.generatePrivate(privateKeyPkcs8);
final PublicKey publicKey = kf.generatePublic(publicKeyX509);
final PrivateKey privateKey = keyPair.getPrivate();
final PublicKey publicKey = keyPair.getPublic();

// 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(keyPair.getPrivate());
nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);
final byte[] signatureACCP = nativeSig.sign();
jceSig.initVerify(publicKey);
Expand All @@ -142,7 +136,7 @@ public void jceInteropValidation() throws GeneralSecurityException {
jceSig.initSign(privateKey);
jceSig.update(message, 0, message.length);
final byte[] signatureJCE = jceSig.sign();
nativeSig.initVerify(keyPair.getPublic());
nativeSig.initVerify(publicKey);
nativeSig.update(message);
assertTrue(nativeSig.verify(signatureJCE), "JCE->Native: Ed25519");

Expand All @@ -157,18 +151,11 @@ public void bcInteropValidation() throws GeneralSecurityException {
final Signature bcSig = Signature.getInstance("Ed25519", BOUNCYCASTLE_PROVIDER);
final KeyPair keyPair = nativeGen.generateKeyPair();

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

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

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

// Sign with ACCP, Verify with BouncyCastle
nativeSig.initSign(keyPair.getPrivate());
nativeSig.initSign(privateKey);
nativeSig.update(message, 0, message.length);
final byte[] signatureACCP = nativeSig.sign();
bcSig.initVerify(publicKey);
Expand All @@ -179,7 +166,7 @@ public void bcInteropValidation() throws GeneralSecurityException {
bcSig.initSign(privateKey);
bcSig.update(message, 0, message.length);
final byte[] signatureBC = bcSig.sign();
nativeSig.initVerify(keyPair.getPublic());
nativeSig.initVerify(publicKey);
nativeSig.update(message);
assertTrue(nativeSig.verify(signatureBC), "BC->Native: Ed25519");

Expand Down Expand Up @@ -229,10 +216,6 @@ public void mismatchSignature() throws GeneralSecurityException {

final KeyPair kp = nativeGen.generateKeyPair();

final X509EncodedKeySpec publicKeyX509 = new X509EncodedKeySpec(kp.getPublic().getEncoded());
final KeyFactory kf = KeyFactory.getInstance("Ed25519", BOUNCYCASTLE_PROVIDER);
final PublicKey pbkJCE = kf.generatePublic(publicKeyX509);

final Signature nativeSig = Signature.getInstance("Ed25519", NATIVE_PROVIDER);
final Signature jceSig = Signature.getInstance("Ed25519", "SunEC");

Expand All @@ -244,7 +227,7 @@ public void mismatchSignature() throws GeneralSecurityException {
nativeSig.update(message2, 0, message2.length);
assertTrue(!nativeSig.verify(signature));

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

0 comments on commit 928b9a0

Please sign in to comment.