Skip to content

Commit acf48ff

Browse files
committed
Merge branch '1832-openpgp-test-coverage' into 'main'
1832 openpgp test coverage See merge request root/bc-java!36
2 parents 5bb48a0 + c962d60 commit acf48ff

13 files changed

+442
-13
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKey.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010

1111
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
1212
import org.bouncycastle.asn1.cryptlib.CryptlibObjectIdentifiers;
13+
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
1314
import org.bouncycastle.asn1.gnu.GNUObjectIdentifiers;
1415
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
1516
import org.bouncycastle.asn1.x9.X9ECParametersHolder;
1617
import org.bouncycastle.bcpg.BCPGKey;
1718
import org.bouncycastle.bcpg.BCPGOutputStream;
1819
import org.bouncycastle.bcpg.DSAPublicBCPGKey;
1920
import org.bouncycastle.bcpg.ECPublicBCPGKey;
21+
import org.bouncycastle.bcpg.Ed448PublicBCPGKey;
2022
import org.bouncycastle.bcpg.ElGamalPublicBCPGKey;
2123
import org.bouncycastle.bcpg.KeyIdentifier;
24+
import org.bouncycastle.bcpg.OctetArrayBCPGKey;
2225
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
2326
import org.bouncycastle.bcpg.PublicKeyPacket;
2427
import org.bouncycastle.bcpg.PublicSubkeyPacket;
@@ -28,6 +31,7 @@
2831
import org.bouncycastle.bcpg.UserAttributePacket;
2932
import org.bouncycastle.bcpg.UserDataPacket;
3033
import org.bouncycastle.bcpg.UserIDPacket;
34+
import org.bouncycastle.bcpg.X448PublicBCPGKey;
3135
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
3236
import org.bouncycastle.util.Arrays;
3337

@@ -90,6 +94,14 @@ else if (key instanceof ECPublicBCPGKey)
9094
{
9195
this.keyStrength = 256;
9296
}
97+
else if (curveOID.equals(EdECObjectIdentifiers.id_X448))
98+
{
99+
this.keyStrength = X448PublicBCPGKey.LENGTH * 8;
100+
}
101+
else if (curveOID.equals(EdECObjectIdentifiers.id_Ed448))
102+
{
103+
this.keyStrength = Ed448PublicBCPGKey.LENGTH * 8;
104+
}
93105
else
94106
{
95107
X9ECParametersHolder ecParameters = ECNamedCurveTable.getByOIDLazy(curveOID);
@@ -104,6 +116,10 @@ else if (key instanceof ECPublicBCPGKey)
104116
}
105117
}
106118
}
119+
else if (key instanceof OctetArrayBCPGKey)
120+
{
121+
this.keyStrength = key.getEncoded().length * 8;
122+
}
107123
}
108124
}
109125

pg/src/test/java/org/bouncycastle/openpgp/test/AbstractPgpKeyPairTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,30 @@
1010
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
1111

1212
import java.security.KeyPair;
13+
import java.text.ParseException;
14+
import java.text.SimpleDateFormat;
1315
import java.util.Date;
16+
import java.util.TimeZone;
1417

1518
public abstract class AbstractPgpKeyPairTest
1619
extends AbstractPacketTest
1720
{
1821

22+
public static Date parseUTCTimestamp(String timestamp)
23+
{
24+
// Not thread safe, so we use a local variable
25+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
26+
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
27+
try
28+
{
29+
return dateFormat.parse(timestamp);
30+
}
31+
catch (ParseException e)
32+
{
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
1937
public Date currentTimeRounded()
2038
{
2139
Date now = new Date();

pg/src/test/java/org/bouncycastle/openpgp/test/DedicatedEd25519KeyPairTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public void performTest()
5959
testV4SigningVerificationWithBcKey();
6060

6161
testConversionOfTestVectorKey();
62+
testBitStrength();
63+
}
64+
65+
private void testBitStrength()
66+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
67+
{
68+
Date date = currentTimeRounded();
69+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
70+
gen.initialize(new EdDSAParameterSpec("Ed25519"));
71+
KeyPair kp = gen.generateKeyPair();
72+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed25519, kp, date);
73+
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
6274
}
6375

6476
private void testConversionOfJcaKeyPair()

pg/src/test/java/org/bouncycastle/openpgp/test/DedicatedEd448KeyPairTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public void performTest()
4949
testConversionOfBcKeyPair();
5050
testV4SigningVerificationWithJcaKey();
5151
testV4SigningVerificationWithBcKey();
52+
53+
testBitStrength();
54+
}
55+
56+
private void testBitStrength()
57+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
58+
{
59+
Date date = currentTimeRounded();
60+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
61+
gen.initialize(new EdDSAParameterSpec("Ed448"));
62+
KeyPair kp = gen.generateKeyPair();
63+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed448, kp, date);
64+
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
5265
}
5366

5467
private void testConversionOfJcaKeyPair()

pg/src/test/java/org/bouncycastle/openpgp/test/DedicatedX25519KeyPairTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ public void performTest()
6464
testConversionOfBcKeyPair();
6565
testV4MessageEncryptionDecryptionWithJcaKey();
6666
testV4MessageEncryptionDecryptionWithBcKey();
67+
68+
testBitStrength();
69+
}
70+
71+
private void testBitStrength()
72+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
73+
{
74+
Date date = currentTimeRounded();
75+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
76+
gen.initialize(new XDHParameterSpec("X25519"));
77+
KeyPair kp = gen.generateKeyPair();
78+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X25519, kp, date);
79+
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
6780
}
6881

6982
private void testConversionOfJcaKeyPair()

pg/src/test/java/org/bouncycastle/openpgp/test/DedicatedX448KeyPairTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
2121
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
2222
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
23+
import org.bouncycastle.jcajce.spec.EdDSAParameterSpec;
2324
import org.bouncycastle.jcajce.spec.XDHParameterSpec;
2425
import org.bouncycastle.jce.provider.BouncyCastleProvider;
2526
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
@@ -64,6 +65,19 @@ public void performTest()
6465
testConversionOfBcKeyPair();
6566
testV4MessageEncryptionDecryptionWithJcaKey();
6667
testV4MessageEncryptionDecryptionWithBcKey();
68+
69+
testBitStrength();
70+
}
71+
72+
private void testBitStrength()
73+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
74+
{
75+
Date date = currentTimeRounded();
76+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
77+
gen.initialize(new XDHParameterSpec("X448"));
78+
KeyPair kp = gen.generateKeyPair();
79+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X448, kp, date);
80+
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
6781
}
6882

6983
private void testConversionOfJcaKeyPair()

pg/src/test/java/org/bouncycastle/openpgp/test/LegacyEd25519KeyPairTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bouncycastle.bcpg.EdSecretBCPGKey;
1313
import org.bouncycastle.bcpg.HashAlgorithmTags;
1414
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
15+
import org.bouncycastle.bcpg.PublicKeyPacket;
1516
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
1617
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
1718
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
@@ -48,6 +49,19 @@ public void performTest()
4849
testConversionOfBcKeyPair();
4950
testV4SigningVerificationWithJcaKey();
5051
testV4SigningVerificationWithBcKey();
52+
53+
testBitStrength();
54+
}
55+
56+
private void testBitStrength()
57+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
58+
{
59+
Date date = currentTimeRounded();
60+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
61+
gen.initialize(new EdDSAParameterSpec("Ed25519"));
62+
KeyPair kp = gen.generateKeyPair();
63+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
64+
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
5165
}
5266

5367
private void testV4SigningVerificationWithJcaKey()

pg/src/test/java/org/bouncycastle/openpgp/test/LegacyEd448KeyPairTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bouncycastle.bcpg.EdSecretBCPGKey;
1313
import org.bouncycastle.bcpg.HashAlgorithmTags;
1414
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
15+
import org.bouncycastle.bcpg.PublicKeyPacket;
1516
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
1617
import org.bouncycastle.crypto.generators.Ed448KeyPairGenerator;
1718
import org.bouncycastle.crypto.params.Ed448KeyGenerationParameters;
@@ -48,6 +49,19 @@ public void performTest()
4849
testConversionOfBcKeyPair();
4950
testV4SigningVerificationWithJcaKey();
5051
testV4SigningVerificationWithBcKey();
52+
53+
testBitStrength();
54+
}
55+
56+
private void testBitStrength()
57+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
58+
{
59+
Date date = currentTimeRounded();
60+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
61+
gen.initialize(new EdDSAParameterSpec("Ed448"));
62+
KeyPair kp = gen.generateKeyPair();
63+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
64+
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
5165
}
5266

5367
private void testV4SigningVerificationWithJcaKey()

pg/src/test/java/org/bouncycastle/openpgp/test/LegacyX25519KeyPairTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
1616
import org.bouncycastle.bcpg.ECSecretBCPGKey;
1717
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
18+
import org.bouncycastle.bcpg.PublicKeyPacket;
1819
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
1920
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
2021
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
@@ -63,6 +64,19 @@ public void performTest()
6364
testConversionOfBcKeyPair();
6465
testV4MessageEncryptionDecryptionWithJcaKey();
6566
testV4MessageEncryptionDecryptionWithBcKey();
67+
68+
testBitStrength();
69+
}
70+
71+
private void testBitStrength()
72+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
73+
{
74+
Date date = currentTimeRounded();
75+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
76+
gen.initialize(new XDHParameterSpec("X25519"));
77+
KeyPair kp = gen.generateKeyPair();
78+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
79+
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
6680
}
6781

6882
private void testConversionOfJcaKeyPair()

pg/src/test/java/org/bouncycastle/openpgp/test/LegacyX448KeyPairTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ public void performTest()
2929
{
3030
testConversionOfJcaKeyPair();
3131
testConversionOfBcKeyPair();
32+
33+
testBitStrength();
34+
}
35+
36+
private void testBitStrength()
37+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
38+
{
39+
Date date = currentTimeRounded();
40+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
41+
gen.initialize(new XDHParameterSpec("X448"));
42+
KeyPair kp = gen.generateKeyPair();
43+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
44+
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
3245
}
3346

3447
private void testConversionOfJcaKeyPair()

0 commit comments

Comments
 (0)