1
1
package org .bouncycastle .openpgp .operator ;
2
2
3
+ import org .bouncycastle .bcpg .PublicKeyAlgorithmTags ;
3
4
import org .bouncycastle .openpgp .PGPException ;
4
5
import org .bouncycastle .openpgp .PGPKeyPair ;
5
6
@@ -14,6 +15,14 @@ public abstract class PGPKeyPairGenerator
14
15
protected final int version ;
15
16
protected SecureRandom random ;
16
17
18
+ /**
19
+ * Create an instance of the key pair generator.
20
+ *
21
+ * @param version public key version ({@link org.bouncycastle.bcpg.PublicKeyPacket#VERSION_4}
22
+ * or {@link org.bouncycastle.bcpg.PublicKeyPacket#VERSION_6}).
23
+ * @param creationTime key creation time
24
+ * @param random secure random number generator
25
+ */
17
26
public PGPKeyPairGenerator (int version , Date creationTime , SecureRandom random )
18
27
{
19
28
this .creationTime = new Date ((creationTime .getTime () / 1000 ) * 1000 );
@@ -26,7 +35,7 @@ public PGPKeyPairGenerator(int version, Date creationTime, SecureRandom random)
26
35
* A primary key MUST use a signing-capable public key algorithm.
27
36
*
28
37
* @return primary key pair
29
- * @throws PGPException
38
+ * @throws PGPException if the key pair cannot be generated
30
39
*/
31
40
public PGPKeyPair generatePrimaryKey ()
32
41
throws PGPException
@@ -39,7 +48,7 @@ public PGPKeyPair generatePrimaryKey()
39
48
* An encryption subkey MUST use an encryption-capable public key algorithm.
40
49
*
41
50
* @return encryption subkey pair
42
- * @throws PGPException
51
+ * @throws PGPException if the key pair cannot be generated
43
52
*/
44
53
public PGPKeyPair generateEncryptionSubkey ()
45
54
throws PGPException
@@ -52,38 +61,116 @@ public PGPKeyPair generateEncryptionSubkey()
52
61
* A signing subkey MUST use a signing-capable public key algorithm.
53
62
*
54
63
* @return signing subkey pair
55
- * @throws PGPException
64
+ * @throws PGPException if the key pair cannot be generated
56
65
*/
57
66
public PGPKeyPair generateSigningSubkey ()
58
67
throws PGPException
59
68
{
60
69
return generateEd25519KeyPair ();
61
70
}
62
71
72
+ /**
73
+ * Generate a RSA key pair with the given bit-strength.
74
+ * It is recommended to use at least 2048 bits or more.
75
+ * The key will be generated over the default exponent <pre>65537</pre>.
76
+ * RSA keys are deprecated for OpenPGP v6.
77
+ *
78
+ * @param bitStrength strength of the key pair in bits
79
+ * @return rsa key pair
80
+ * @throws PGPException if the key pair cannot be generated
81
+ */
63
82
public PGPKeyPair generateRsaKeyPair (int bitStrength )
64
83
throws PGPException
65
84
{
66
85
return generateRsaKeyPair (BigInteger .valueOf (0x10001 ), bitStrength );
67
86
}
68
87
88
+ /**
89
+ * Generate a RSA key pair with the given bit-strength over a custom exponent.
90
+ * It is recommended to use at least 2048 bits or more.
91
+ * RSA keys are deprecated for OpenPGP v6.
92
+ *
93
+ * @param exponent RSA exponent <pre>e</pre>
94
+ * @param bitStrength strength of the key pair in bits
95
+ * @return rsa key pair
96
+ * @throws PGPException if the key pair cannot be generated
97
+ */
69
98
public abstract PGPKeyPair generateRsaKeyPair (BigInteger exponent , int bitStrength )
70
99
throws PGPException ;
71
100
101
+ /**
102
+ * Generate an elliptic curve signing key over the twisted Edwards curve25519.
103
+ * The key will use {@link PublicKeyAlgorithmTags#Ed25519} which was introduced with RFC9580.
104
+ * For legacy Ed25519 keys use {@link #generateLegacyEd25519KeyPair()}.
105
+ *
106
+ * @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms">
107
+ * RFC9580 - Public Key Algorithms</a>
108
+ * @return Ed25519 key pair
109
+ * @throws PGPException if the key pair cannot be generated
110
+ */
72
111
public abstract PGPKeyPair generateEd25519KeyPair ()
73
112
throws PGPException ;
74
113
114
+ /**
115
+ * Generate an elliptic curve signing key over the twisted Edwards curve448.
116
+ * The key will use {@link PublicKeyAlgorithmTags#Ed448} which was introduced with RFC9580.
117
+ *
118
+ * @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms">
119
+ * RFC9580 - Public Key Algorithms</a>
120
+ * @return Ed448 signing key pair
121
+ * @throws PGPException if the key pair cannot be generated
122
+ */
75
123
public abstract PGPKeyPair generateEd448KeyPair ()
76
124
throws PGPException ;
77
125
126
+ /**
127
+ * Generate an elliptic curve Diffie-Hellman encryption key over curve25519.
128
+ * THe key will use {@link PublicKeyAlgorithmTags#X25519} which was introduced with RFC9580.
129
+ * For legacy X25519 keys use {@link #generateLegacyX25519KeyPair()} instead.
130
+ *
131
+ * @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms">
132
+ * RFC9580 - Public Key Algorithms</a>
133
+ * @return X25519 encryption key pair
134
+ * @throws PGPException if the key pair cannot be generated
135
+ */
78
136
public abstract PGPKeyPair generateX25519KeyPair ()
79
137
throws PGPException ;
80
138
139
+ /**
140
+ * Generate an elliptic curve Diffie-Hellman encryption key over curve448.
141
+ * THe key will use {@link PublicKeyAlgorithmTags#X448} which was introduced with RFC9580.
142
+ *
143
+ * @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms">
144
+ * RFC9580 - Public Key Algorithms</a>
145
+ * @return X448 encryption key pair
146
+ * @throws PGPException if the key pair cannot be generated
147
+ */
81
148
public abstract PGPKeyPair generateX448KeyPair ()
82
149
throws PGPException ;
83
150
151
+ /**
152
+ * Generate a legacy elliptic curve signing key pair over the twisted Edwards curve25519.
153
+ * Legacy keys have good application support, but MUST NOT be used as OpenPGP v6 keys.
154
+ * The key will use {@link PublicKeyAlgorithmTags#EDDSA_LEGACY} as algorithm ID.
155
+ * For OpenPGP v6 (RFC9580) use {@link #generateEd25519KeyPair()} instead.
156
+ *
157
+ * @see <a href="https://datatracker.ietf.org/doc/html/draft-koch-eddsa-for-openpgp-04">
158
+ * Legacy Draft: EdDSA for OpenPGP</a>
159
+ * @return legacy Ed25519 key pair
160
+ * @throws PGPException if the key pair cannot be generated
161
+ */
84
162
public abstract PGPKeyPair generateLegacyEd25519KeyPair ()
85
163
throws PGPException ;
86
164
165
+ /**
166
+ * Generate a legacy elliptic curve Diffie-Hellman encryption key pair over curve25519.
167
+ * Legacy keys have good application support, but MUST NOT be used as OpenPGP v6 keys.
168
+ * The key will use {@link PublicKeyAlgorithmTags#ECDH} as algorithm ID.
169
+ * For OpenPGP v6 (RFC9580) use {@link #generateX25519KeyPair()} instead.
170
+ *
171
+ * @return legacy X25519 key pair
172
+ * @throws PGPException if the key pair cannot be generated
173
+ */
87
174
public abstract PGPKeyPair generateLegacyX25519KeyPair ()
88
175
throws PGPException ;
89
176
}
0 commit comments