Skip to content

Commit f997cf8

Browse files
committed
Merge branch '1911-2-keypairgenerator' into 'main'
update PGPKeyPairGeneratorTest See merge request root/bc-java!97
2 parents e3692ee + 2bc0300 commit f997cf8

File tree

4 files changed

+421
-1
lines changed

4 files changed

+421
-1
lines changed

pg/src/main/java/org/bouncycastle/openpgp/operator/PGPKeyPairGenerator.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.openpgp.operator;
22

3+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
4+
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
35
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
46
import org.bouncycastle.openpgp.PGPException;
57
import org.bouncycastle.openpgp.PGPKeyPair;
@@ -178,4 +180,114 @@ public abstract PGPKeyPair generateLegacyEd25519KeyPair()
178180
*/
179181
public abstract PGPKeyPair generateLegacyX25519KeyPair()
180182
throws PGPException;
183+
184+
/**
185+
* Generate an ECDH elliptic curve encryption key over the NIST p-256 curve.
186+
*
187+
* @return NIST p-256 ECDSA encryption key pair
188+
* @throws PGPException if the key pair cannot be generated
189+
*
190+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6637.html">
191+
* RFC6637 - Elliptic Curve Cryptography in OpenPGP</a>
192+
*/
193+
public PGPKeyPair generateNistP256ECDHKeyPair()
194+
throws PGPException
195+
{
196+
return generateECDHKeyPair(SECObjectIdentifiers.secp256r1);
197+
}
198+
199+
/**
200+
* Generate an ECDH elliptic curve encryption key over the NIST p-384 curve.
201+
*
202+
* @return NIST p-384 ECDSA encryption key pair
203+
* @throws PGPException if the key pair cannot be generated
204+
*
205+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6637.html">
206+
* RFC6637 - Elliptic Curve Cryptography in OpenPGP</a>
207+
*/
208+
public PGPKeyPair generateNistP384ECDHKeyPair()
209+
throws PGPException
210+
{
211+
return generateECDHKeyPair(SECObjectIdentifiers.secp384r1);
212+
}
213+
214+
/**
215+
* Generate an ECDH elliptic curve encryption key over the NIST p-521 curve.
216+
*
217+
* @return NIST p-521 ECDSA encryption key pair
218+
* @throws PGPException if the key pair cannot be generated
219+
*
220+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6637.html">
221+
* RFC6637 - Elliptic Curve Cryptography in OpenPGP</a>
222+
*/
223+
public PGPKeyPair generateNistP521ECDHKeyPair()
224+
throws PGPException
225+
{
226+
return generateECDHKeyPair(SECObjectIdentifiers.secp521r1);
227+
}
228+
229+
/**
230+
* Generate an ECDSA elliptic curve signing key over the NIST p-256 curve.
231+
*
232+
* @return NIST p-256 ECDSA signing key pair
233+
* @throws PGPException if the key pair cannot be generated
234+
*
235+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6637.html">
236+
* RFC6637 - Elliptic Curve Cryptography in OpenPGP</a>
237+
*/
238+
public PGPKeyPair generateNistP256ECDSAKeyPair()
239+
throws PGPException
240+
{
241+
return generateECDSAKeyPair(SECObjectIdentifiers.secp256r1);
242+
}
243+
244+
/**
245+
* Generate an ECDSA elliptic curve signing key over the NIST p-384 curve.
246+
*
247+
* @return NIST p-384 ECDSA signing key pair
248+
* @throws PGPException if the key pair cannot be generated
249+
*
250+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6637.html">
251+
* RFC6637 - Elliptic Curve Cryptography in OpenPGP</a>
252+
*/
253+
public PGPKeyPair generateNistP384ECDSAKeyPair()
254+
throws PGPException
255+
{
256+
return generateECDSAKeyPair(SECObjectIdentifiers.secp384r1);
257+
}
258+
259+
/**
260+
* Generate an ECDSA elliptic curve signing key over the NIST p-521 curve.
261+
*
262+
* @return NIST p-521 ECDSA signing key pair
263+
* @throws PGPException if the key pair cannot be generated
264+
*
265+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6637.html">
266+
* RFC6637 - Elliptic Curve Cryptography in OpenPGP</a>
267+
*/
268+
public PGPKeyPair generateNistP521ECDSAKeyPair()
269+
throws PGPException
270+
{
271+
return generateECDSAKeyPair(SECObjectIdentifiers.secp521r1);
272+
}
273+
274+
/**
275+
* Generate an elliptic curve Diffie-Hellman encryption key pair over the curve identified by the given OID.
276+
*
277+
* @param curveOID OID of the elliptic curve
278+
* @return PGP key pair
279+
* @throws PGPException if the key pair cannot be generated
280+
*/
281+
public abstract PGPKeyPair generateECDHKeyPair(ASN1ObjectIdentifier curveOID)
282+
throws PGPException;
283+
284+
/**
285+
* Generate an elliptic curve signing key over the curve identified by the given OID.
286+
*
287+
* @param curveOID OID of the elliptic curve
288+
* @return PGP key pair
289+
* @throws PGPException if the key pair cannot be generated
290+
*/
291+
public abstract PGPKeyPair generateECDSAKeyPair(ASN1ObjectIdentifier curveOID)
292+
throws PGPException;
181293
}

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPGPKeyPairGeneratorProvider.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package org.bouncycastle.openpgp.operator.bc;
22

3+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
34
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
45
import org.bouncycastle.bcpg.PublicKeyPacket;
56
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
67
import org.bouncycastle.crypto.CryptoServicesRegistrar;
8+
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
79
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
810
import org.bouncycastle.crypto.generators.Ed448KeyPairGenerator;
911
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
1012
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
1113
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
14+
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
15+
import org.bouncycastle.crypto.params.ECNamedDomainParameters;
1216
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
1317
import org.bouncycastle.crypto.params.Ed448KeyGenerationParameters;
1418
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
1519
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
1620
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
21+
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
1722
import org.bouncycastle.openpgp.PGPException;
1823
import org.bouncycastle.openpgp.PGPKeyPair;
1924
import org.bouncycastle.openpgp.operator.PGPKeyPairGenerator;
@@ -24,7 +29,7 @@
2429
import java.util.Date;
2530

2631
public class BcPGPKeyPairGeneratorProvider
27-
extends PGPKeyPairGeneratorProvider
32+
extends PGPKeyPairGeneratorProvider
2833
{
2934
private SecureRandom random = CryptoServicesRegistrar.getSecureRandom();
3035

@@ -128,5 +133,31 @@ public PGPKeyPair generateLegacyX25519KeyPair()
128133
AsymmetricCipherKeyPair keyPair = gen.generateKeyPair();
129134
return new BcPGPKeyPair(version, PublicKeyAlgorithmTags.ECDH, keyPair, creationTime);
130135
}
136+
137+
@Override
138+
public PGPKeyPair generateECDHKeyPair(ASN1ObjectIdentifier curveOID)
139+
throws PGPException
140+
{
141+
ECKeyPairGenerator gen = new ECKeyPairGenerator();
142+
gen.init(new ECKeyGenerationParameters(
143+
new ECNamedDomainParameters(curveOID, ECUtil.getNamedCurveByOid(curveOID)),
144+
CryptoServicesRegistrar.getSecureRandom()));
145+
146+
AsymmetricCipherKeyPair keyPair = gen.generateKeyPair();
147+
return new BcPGPKeyPair(version, PublicKeyAlgorithmTags.ECDH, keyPair, creationTime);
148+
}
149+
150+
@Override
151+
public PGPKeyPair generateECDSAKeyPair(ASN1ObjectIdentifier curveOID)
152+
throws PGPException
153+
{
154+
ECKeyPairGenerator gen = new ECKeyPairGenerator();
155+
gen.init(new ECKeyGenerationParameters(
156+
new ECNamedDomainParameters(curveOID, ECUtil.getNamedCurveByOid(curveOID)),
157+
CryptoServicesRegistrar.getSecureRandom()));
158+
159+
AsymmetricCipherKeyPair keyPair = gen.generateKeyPair();
160+
return new BcPGPKeyPair(version, PublicKeyAlgorithmTags.ECDSA, keyPair, creationTime);
161+
}
131162
}
132163
}

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPKeyPairGeneratorProvider.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.bouncycastle.openpgp.operator.jcajce;
22

3+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
34
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
45
import org.bouncycastle.bcpg.PublicKeyPacket;
56
import org.bouncycastle.crypto.CryptoServicesRegistrar;
7+
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
68
import org.bouncycastle.jcajce.spec.EdDSAParameterSpec;
79
import org.bouncycastle.jcajce.spec.XDHParameterSpec;
810
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
911
import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
1012
import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
13+
import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
1114
import org.bouncycastle.openpgp.PGPException;
1215
import org.bouncycastle.openpgp.PGPKeyPair;
1316
import org.bouncycastle.openpgp.operator.PGPKeyPairGenerator;
@@ -212,5 +215,41 @@ public PGPKeyPair generateLegacyX25519KeyPair()
212215
throw new PGPException("Cannot generate LegacyX25519 key pair.", e);
213216
}
214217
}
218+
219+
@Override
220+
public PGPKeyPair generateECDHKeyPair(ASN1ObjectIdentifier curveOID)
221+
throws PGPException
222+
{
223+
try
224+
{
225+
KeyPairGenerator gen = helper.createKeyPairGenerator("ECDH");
226+
String curveName = ECUtil.getCurveName(curveOID);
227+
gen.initialize(new ECNamedCurveGenParameterSpec(curveName));
228+
KeyPair keyPair = gen.generateKeyPair();
229+
return new JcaPGPKeyPair(version, PublicKeyAlgorithmTags.ECDH, keyPair, creationTime);
230+
}
231+
catch (GeneralSecurityException e)
232+
{
233+
throw new PGPException("Cannot generate ECDH key pair.", e);
234+
}
235+
}
236+
237+
@Override
238+
public PGPKeyPair generateECDSAKeyPair(ASN1ObjectIdentifier curveOID)
239+
throws PGPException
240+
{
241+
try
242+
{
243+
KeyPairGenerator gen = helper.createKeyPairGenerator("ECDSA");
244+
String curveName = ECUtil.getCurveName(curveOID);
245+
gen.initialize(new ECNamedCurveGenParameterSpec(curveName));
246+
KeyPair keyPair = gen.generateKeyPair();
247+
return new JcaPGPKeyPair(version, PublicKeyAlgorithmTags.ECDSA, keyPair, creationTime);
248+
}
249+
catch (GeneralSecurityException e)
250+
{
251+
throw new PGPException("Cannot generate ECDSA key pair.", e);
252+
}
253+
}
215254
}
216255
}

0 commit comments

Comments
 (0)