Skip to content

Commit 5acc6f4

Browse files
author
gefeili
committed
1832 Improve test coverage of PGP (v6) keys
2 parents 61dd2bb + 3d0540d commit 5acc6f4

13 files changed

+444
-15
lines changed

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

+18-2
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

@@ -47,7 +51,7 @@ public class PGPPublicKey
4751
List<List<PGPSignature>> idSigs = new ArrayList<List<PGPSignature>>();
4852

4953
List<PGPSignature> subSigs = null;
50-
54+
5155
private KeyIdentifier keyIdentifier;
5256
private int keyStrength;
5357

@@ -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

@@ -150,7 +166,7 @@ public PGPPublicKey(PublicKeyPacket publicKeyPacket, KeyFingerPrintCalculator fi
150166
this.publicPk = key.publicPk;
151167
this.trustPk = trust;
152168
this.subSigs = subSigs;
153-
169+
154170
this.keyStrength = key.keyStrength;
155171
this.keyIdentifier = key.keyIdentifier;
156172
}

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

+18
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

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public void performTest()
4242
testV4SigningVerificationWithBcKey();
4343

4444
testConversionOfTestVectorKey();
45+
testBitStrength();
46+
}
47+
48+
private void testBitStrength()
49+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
50+
{
51+
Date date = currentTimeRounded();
52+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
53+
gen.initialize(new EdDSAParameterSpec("Ed25519"));
54+
KeyPair kp = gen.generateKeyPair();
55+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed25519, kp, date);
56+
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
4557
}
4658

4759
private void testConversionOfJcaKeyPair()

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

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public void performTest()
4545
testConversionOfBcKeyPair();
4646
testV4SigningVerificationWithJcaKey();
4747
testV4SigningVerificationWithBcKey();
48+
49+
testBitStrength();
50+
}
51+
52+
private void testBitStrength()
53+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
54+
{
55+
Date date = currentTimeRounded();
56+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
57+
gen.initialize(new EdDSAParameterSpec("Ed448"));
58+
KeyPair kp = gen.generateKeyPair();
59+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed448, kp, date);
60+
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
4861
}
4962

5063
private void testConversionOfJcaKeyPair()

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

+13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public void performTest()
4949
testConversionOfBcKeyPair();
5050
testV4MessageEncryptionDecryptionWithJcaKey();
5151
testV4MessageEncryptionDecryptionWithBcKey();
52+
53+
testBitStrength();
54+
}
55+
56+
private void testBitStrength()
57+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
58+
{
59+
Date date = currentTimeRounded();
60+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
61+
gen.initialize(new XDHParameterSpec("X25519"));
62+
KeyPair kp = gen.generateKeyPair();
63+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X25519, kp, date);
64+
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
5265
}
5366

5467
private void testConversionOfJcaKeyPair()

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

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
99
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
1010
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
11+
import org.bouncycastle.jcajce.spec.EdDSAParameterSpec;
1112
import org.bouncycastle.jcajce.spec.XDHParameterSpec;
1213
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1314
import org.bouncycastle.openpgp.*;
@@ -49,6 +50,19 @@ public void performTest()
4950
testConversionOfBcKeyPair();
5051
testV4MessageEncryptionDecryptionWithJcaKey();
5152
testV4MessageEncryptionDecryptionWithBcKey();
53+
54+
testBitStrength();
55+
}
56+
57+
private void testBitStrength()
58+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
59+
{
60+
Date date = currentTimeRounded();
61+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
62+
gen.initialize(new XDHParameterSpec("X448"));
63+
KeyPair kp = gen.generateKeyPair();
64+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X448, kp, date);
65+
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
5266
}
5367

5468
private void testConversionOfJcaKeyPair()

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

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bouncycastle.bcpg.EdSecretBCPGKey;
55
import org.bouncycastle.bcpg.HashAlgorithmTags;
66
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
7+
import org.bouncycastle.bcpg.PublicKeyPacket;
78
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
89
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
910
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
@@ -44,6 +45,19 @@ public void performTest()
4445
testConversionOfBcKeyPair();
4546
testV4SigningVerificationWithJcaKey();
4647
testV4SigningVerificationWithBcKey();
48+
49+
testBitStrength();
50+
}
51+
52+
private void testBitStrength()
53+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
54+
{
55+
Date date = currentTimeRounded();
56+
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
57+
gen.initialize(new EdDSAParameterSpec("Ed25519"));
58+
KeyPair kp = gen.generateKeyPair();
59+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
60+
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
4761
}
4862

4963
private void testV4SigningVerificationWithJcaKey()

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

+14
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

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
44
import org.bouncycastle.bcpg.ECSecretBCPGKey;
55
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
6+
import org.bouncycastle.bcpg.PublicKeyPacket;
67
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
78
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
89
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
@@ -48,6 +49,19 @@ public void performTest()
4849
testConversionOfBcKeyPair();
4950
testV4MessageEncryptionDecryptionWithJcaKey();
5051
testV4MessageEncryptionDecryptionWithBcKey();
52+
53+
testBitStrength();
54+
}
55+
56+
private void testBitStrength()
57+
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
58+
{
59+
Date date = currentTimeRounded();
60+
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
61+
gen.initialize(new XDHParameterSpec("X25519"));
62+
KeyPair kp = gen.generateKeyPair();
63+
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
64+
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
5165
}
5266

5367
private void testConversionOfJcaKeyPair()

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

+13
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)