Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage of PGP (v6) keys #1832

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cryptlib.CryptlibObjectIdentifiers;
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.gnu.GNUObjectIdentifiers;
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
import org.bouncycastle.asn1.x9.X9ECParametersHolder;
import org.bouncycastle.bcpg.BCPGKey;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.DSAPublicBCPGKey;
import org.bouncycastle.bcpg.ECPublicBCPGKey;
import org.bouncycastle.bcpg.Ed448PublicBCPGKey;
import org.bouncycastle.bcpg.ElGamalPublicBCPGKey;
import org.bouncycastle.bcpg.OctetArrayBCPGKey;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.bcpg.PublicSubkeyPacket;
Expand All @@ -27,6 +30,7 @@
import org.bouncycastle.bcpg.UserAttributePacket;
import org.bouncycastle.bcpg.UserDataPacket;
import org.bouncycastle.bcpg.UserIDPacket;
import org.bouncycastle.bcpg.X448PublicBCPGKey;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Pack;
Expand Down Expand Up @@ -98,6 +102,14 @@ else if (key instanceof ECPublicBCPGKey)
{
this.keyStrength = 256;
}
else if (curveOID.equals(EdECObjectIdentifiers.id_X448))
{
this.keyStrength = X448PublicBCPGKey.LENGTH * 8;
}
else if (curveOID.equals(EdECObjectIdentifiers.id_Ed448))
{
this.keyStrength = Ed448PublicBCPGKey.LENGTH * 8;
}
else
{
X9ECParametersHolder ecParameters = ECNamedCurveTable.getByOIDLazy(curveOID);
Expand All @@ -112,6 +124,10 @@ else if (key instanceof ECPublicBCPGKey)
}
}
}
else if (key instanceof OctetArrayBCPGKey)
{
this.keyStrength = key.getEncoded().length * 8;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;

import java.security.KeyPair;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public abstract class AbstractPgpKeyPairTest
extends AbstractPacketTest
{

public static Date parseUTCTimestamp(String timestamp)
{
// Not thread safe, so we use a local variable
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try
{
return dateFormat.parse(timestamp);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}

public Date currentTimeRounded()
{
Date now = new Date();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public void performTest()
testV4SigningVerificationWithBcKey();

testConversionOfTestVectorKey();
testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed25519, kp, date);
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4SigningVerificationWithJcaKey();
testV4SigningVerificationWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed448, kp, date);
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4MessageEncryptionDecryptionWithJcaKey();
testV4MessageEncryptionDecryptionWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X25519, kp, date);
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
import org.bouncycastle.jcajce.spec.EdDSAParameterSpec;
import org.bouncycastle.jcajce.spec.XDHParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.*;
Expand Down Expand Up @@ -49,6 +50,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4MessageEncryptionDecryptionWithJcaKey();
testV4MessageEncryptionDecryptionWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X448, kp, date);
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bouncycastle.bcpg.EdSecretBCPGKey;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
Expand Down Expand Up @@ -44,6 +45,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4SigningVerificationWithJcaKey();
testV4SigningVerificationWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testV4SigningVerificationWithJcaKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bouncycastle.bcpg.EdSecretBCPGKey;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed448KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed448KeyGenerationParameters;
Expand Down Expand Up @@ -48,6 +49,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4SigningVerificationWithJcaKey();
testV4SigningVerificationWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
}

private void testV4SigningVerificationWithJcaKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
import org.bouncycastle.bcpg.ECSecretBCPGKey;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
Expand Down Expand Up @@ -48,6 +49,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4MessageEncryptionDecryptionWithJcaKey();
testV4MessageEncryptionDecryptionWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public void performTest()
{
testConversionOfJcaKeyPair();
testConversionOfBcKeyPair();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Loading