Skip to content

Commit

Permalink
Test conversion of Ed448 keys with leading zero
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Jun 2, 2024
1 parent 7bbee18 commit 7cb20f8
Showing 1 changed file with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.bouncycastle.openpgp.test;

import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.test.AbstractPacketTest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
import org.bouncycastle.util.encoders.Hex;

import java.io.IOException;
import java.security.*;
import java.security.spec.*;
import java.util.Date;

public class Ed448ConversionWithLeadingZero
extends AbstractPacketTest
{
@Override
public String getName()
{
return "Ed448LeadingZero";
}

private static final String ED448_KEY_WITH_LEADING_ZERO = "308183020101300506032b6571043b0439fe2c82fd07b0e8b5da002ee4964e55a357bfdd2192fe43a40b150e6c5a8f8202f140dd34ede17dc10fef9a98bf8188425c14bd1a76a308cfb7813a0000728cbb07c590e2cb282834cc22d7a1f775f729986c4754e7035695dee34057403e98e94cf5012007c3236f4894af039e668acb746fcf8a00";
private static final String ED448_PUB_WITH_LEADING_ZERO = "3043300506032b6571033a0000728cbb07c590e2cb282834cc22d7a1f775f729986c4754e7035695dee34057403e98e94cf5012007c3236f4894af039e668acb746fcf8a00";

@Override
public void performTest()
throws Exception
{
testWithEd448KeyWithLeadingZero();
}

private void testWithEd448KeyWithLeadingZero()
throws NoSuchAlgorithmException, InvalidKeySpecException, PGPException, InvalidKeyException, SignatureException
{
JcaPGPKeyConverter jcaPGPKeyConverter = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider());

KeyFactory factory = KeyFactory.getInstance("EdDSA", new BouncyCastleProvider());

PublicKey pubKey = factory.generatePublic(new X509EncodedKeySpec(Hex.decode(ED448_PUB_WITH_LEADING_ZERO)));
PrivateKey privKey = factory.generatePrivate(new PKCS8EncodedKeySpec(Hex.decode(ED448_KEY_WITH_LEADING_ZERO)));
KeyPair keyPair = new KeyPair(pubKey, privKey);

Date creationDate = new Date();
PGPKeyPair jcaPgpPair = new JcaPGPKeyPair(PublicKeyAlgorithmTags.Ed448, keyPair, creationDate);
isTrue("public key encoding before conversion MUST have leading 0",
jcaPgpPair.getPublicKey().getPublicKeyPacket().getKey().getEncoded()[0] == 0); // leading 0

PublicKey cPubKey = jcaPGPKeyConverter.getPublicKey(jcaPgpPair.getPublicKey());
PrivateKey cPrivKey = jcaPGPKeyConverter.getPrivateKey(jcaPgpPair.getPrivateKey());

testSignature(cPrivKey, pubKey);
testSignature(privKey, cPubKey);

jcaPgpPair = new JcaPGPKeyPair(PublicKeyAlgorithmTags.Ed448, new KeyPair(cPubKey, cPrivKey), creationDate);
isTrue("public key encoding after conversion MUST have leading 0",
jcaPgpPair.getPublicKey().getPublicKeyPacket().getKey().getEncoded()[0] == 0); // leading 0 is preserved
}

private void testSignature(PrivateKey privateKey, PublicKey publicKey)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException
{
Signature signature = Signature.getInstance("Ed448", new BouncyCastleProvider());
signature.initSign(privateKey);
signature.update("Hello, World!\n".getBytes());
byte[] sig = signature.sign();

signature.initVerify(publicKey);
signature.update("Hello, World!\n".getBytes());
isTrue("Signature MUST verify", signature.verify(sig));
}

public static void main(String[] args)
{
Security.addProvider(new BouncyCastleProvider());
runTest(new Ed448ConversionWithLeadingZero());
}
}

0 comments on commit 7cb20f8

Please sign in to comment.