Skip to content

Commit

Permalink
Add test for conversion of parsed test vector key
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed May 17, 2024
1 parent 3bfa8a4 commit 896774e
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
import org.bouncycastle.bcpg.*;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.jcajce.spec.EdDSAParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyConverter;
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
import org.bouncycastle.util.Pack;
import org.bouncycastle.util.encoders.Hex;

import java.io.IOException;
import java.security.*;
Expand All @@ -29,6 +36,8 @@ public void performTest()
{
testConversionOfJcaKeyPair();
testConversionOfBcKeyPair();

testConversionOfTestVectorKey();
}

private void testConversionOfJcaKeyPair()
Expand Down Expand Up @@ -119,6 +128,27 @@ private void testConversionOfBcKeyPair()
date.getTime(), j2.getPublicKey().getCreationTime().getTime());
}

private void testConversionOfTestVectorKey() throws PGPException, IOException {
JcaPGPKeyConverter jc = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider());
BcPGPKeyConverter bc = new BcPGPKeyConverter();
// ed25519 public key from https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-hashed-data-stream-for-sign
// just adapted to be a version 4 key.
Date creationTime = new Date(Pack.bigEndianToInt(Hex.decode("63877fe3"), 0) * 1000L);
byte[] k = Hex.decode("f94da7bb48d60a61e567706a6587d0331999bb9d891a08242ead84543df895a3");
PGPPublicKey v4k = new PGPPublicKey(
new PublicKeyPacket(PublicKeyAlgorithmTags.Ed25519, creationTime, new Ed25519PublicBCPGKey(k)),
new BcKeyFingerprintCalculator()
);

PublicKey jcpk = jc.getPublicKey(v4k);
PGPPublicKey jck = jc.getPGPPublicKey(PublicKeyAlgorithmTags.Ed25519, jcpk, creationTime);
isEncodingEqual(v4k.getEncoded(), jck.getEncoded());

AsymmetricKeyParameter bcpk = bc.getPublicKey(v4k);
PGPPublicKey bck = bc.getPGPPublicKey(PublicKeyAlgorithmTags.Ed25519, null, bcpk, creationTime);
isEncodingEqual(v4k.getEncoded(), bck.getEncoded());
}

public static void main(String[] args)
{
runTest(new DedicatedEd25519KeyPairTest());
Expand Down

0 comments on commit 896774e

Please sign in to comment.