Skip to content

Commit a8240b8

Browse files
author
gefeili
committed
Merge remote-tracking branch 'pgpainless/pkeskv6Encryption' into 1938-v6-PKESK-encryption
# Conflicts: # pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java
2 parents cb3a8b0 + 4b5f3d1 commit a8240b8

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ else if (m instanceof PublicKeyKeyEncryptionMethodGenerator)
401401
/**
402402
* Write out a {@link org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket#VERSION_5 v5 SKESK} or
403403
* {@link org.bouncycastle.bcpg.PublicKeyEncSessionPacket#VERSION_3 v3 PKESK} packet,
404-
* depending on the method generator. This method is used by what can be referred to as OpenPGP v5.
404+
* depending on the method generator. This method is used by LibrePGP only.
405405
*
406406
* @param m session key encryption method generator.
407407
* @param sessionInfo session info

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* </p>
2323
*/
2424
public abstract class PBEKeyEncryptionMethodGenerator
25-
extends PGPKeyEncryptionMethodGenerator
25+
implements PGPKeyEncryptionMethodGenerator
2626
{
2727
private char[] passPhrase;
2828
private PGPDigestCalculator s2kDigestCalculator;
@@ -161,38 +161,57 @@ public byte[] getKey(int encAlgorithm)
161161
}
162162

163163
/**
164-
* Generates a version 4 Public-Key-Encrypted-Session-Key (PKESK) packet, encoding the encrypted
164+
* Generates a version 4 Symmetric-Key-Encrypted-Session-Key (SKESK) packet, encoding the encrypted
165165
* session-key for this method.
166-
* PKESKv4 packets are used by Symmetrically-Encrypted-Integrity-Protected-Data (SEIPD) packets
166+
* SKESKv4 packets are used by Symmetrically-Encrypted-Integrity-Protected-Data (SEIPD) packets
167167
* of version 1, or by (deprecated) Symmetrically-Encrypted-Data (SED) packets.
168-
* You can use PKESKv4 packets with OpenPGP v4 keys, but MUST NOT use them when producing
169-
* SEIPDv2 packets (use {@link #generateV6(int, int, byte[])} instead in that case).
168+
* You MUST NOT use them when producing SEIPDv2 packets (use {@link #generateV6(int, int, byte[])}
169+
* instead in that case).
170170
*
171-
* @param encAlgorithm the {@link SymmetricKeyAlgorithmTags encryption algorithm} being used
171+
* @param kekAlgorithm the {@link SymmetricKeyAlgorithmTags encryption algorithm} being used to
172+
* wrap the session key
172173
* @param sessionInfo session data generated by the encrypted data generator.
173174
* @return a packet encoding the provided information and the configuration of this instance.
174175
*
175176
* @throws PGPException if an error occurs constructing the packet.
177+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-4-symmetric-key-enc">
178+
* RFC9580 - Symmetric-Key Encrypted Session-Key Packet version 4</a>
176179
*/
177-
public ContainedPacket generateV4(int encAlgorithm, byte[] sessionInfo)
180+
public ContainedPacket generateV4(int kekAlgorithm, byte[] sessionInfo)
178181
throws PGPException
179182
{
180183
if (sessionInfo == null)
181184
{
182-
return SymmetricKeyEncSessionPacket.createV4Packet(encAlgorithm, s2k, null);
185+
return SymmetricKeyEncSessionPacket.createV4Packet(kekAlgorithm, s2k, null);
183186
}
184187

185-
byte[] key = getKey(encAlgorithm);
188+
byte[] key = getKey(kekAlgorithm);
186189
//
187190
// the passed in session info has the an RSA/ElGamal checksum added to it, for PBE this is not included.
188191
//
189192
byte[] nSessionInfo = new byte[sessionInfo.length - 2];
190193

191194
System.arraycopy(sessionInfo, 0, nSessionInfo, 0, nSessionInfo.length);
192195

193-
return SymmetricKeyEncSessionPacket.createV4Packet(encAlgorithm, s2k, encryptSessionInfo(encAlgorithm, key, nSessionInfo));
196+
return SymmetricKeyEncSessionPacket.createV4Packet(kekAlgorithm, s2k, encryptSessionInfo(kekAlgorithm, key, nSessionInfo));
194197
}
195198

199+
/**
200+
* Generates a version 5 Symmetric-Key-Encrypted-Session-Key (SKESK) packet, encoding the encrypted
201+
* session-key for this method.
202+
* SKESKv5 packets are used with {@link org.bouncycastle.bcpg.AEADEncDataPacket OCB-Encrypted Data (OED) packets}
203+
* only.
204+
*
205+
* @param kekAlgorithm the {@link SymmetricKeyAlgorithmTags encryption algorithm} being used to
206+
* wrap the session key
207+
* @param aeadAlgorithm AEAD algorithm ID (MUST be {@link org.bouncycastle.bcpg.AEADAlgorithmTags#OCB})
208+
* @param sessionInfo session data generated by the encrypted data generator.
209+
* @return a packet encoding the provided information and the configuration of this instance.
210+
*
211+
* @throws PGPException if an error occurs constructing the packet.
212+
* @see <a href="https://www.ietf.org/archive/id/draft-koch-librepgp-02.html#section-5.3-8">
213+
* LibrePGP - Symmetric-Key Encrypted Session-Key Packet version 5</a>
214+
*/
196215
public ContainedPacket generateV5(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo)
197216
throws PGPException
198217
{
@@ -216,6 +235,23 @@ public ContainedPacket generateV5(int kekAlgorithm, int aeadAlgorithm, byte[] se
216235
return SymmetricKeyEncSessionPacket.createV5Packet(kekAlgorithm, aeadAlgorithm, iv, s2k, esk, tag);
217236
}
218237

238+
/**
239+
* Generates a version 6 Symmetric-Key-Encrypted-Session-Key (SKESK) packet, encoding the encrypted
240+
* session-key for this method.
241+
* SKESKv6 packets are used with Symmetrically-Encrypted Integrity-Protected Data (SEIPD) packets of
242+
* version 2 only.
243+
* A SKESKv6 packet MUST NOT precede a SEIPDv1, OED or SED packet.
244+
*
245+
* @param kekAlgorithm the {@link SymmetricKeyAlgorithmTags encryption algorithm} being used to
246+
* wrap the session key
247+
* @param aeadAlgorithm AEAD algorithm ID
248+
* @param sessionInfo session data generated by the encrypted data generator.
249+
* @return a packet encoding the provided information and the configuration of this instance.
250+
*
251+
* @throws PGPException if an error occurs constructing the packet.
252+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-6-symmetric-key-enc">
253+
* RFC9580 - Symmetric-Key Encrypted Session-Key Packet version 6</a>
254+
*/
219255
public ContainedPacket generateV6(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo)
220256
throws PGPException
221257
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* An encryption method that can be applied to encrypt data in a {@link PGPEncryptedDataGenerator}.
77
*/
8-
public abstract class PGPKeyEncryptionMethodGenerator
8+
public interface PGPKeyEncryptionMethodGenerator
99
{
1010

1111
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* The purpose of this class is to allow subclasses to decide, which implementation to use.
1818
*/
1919
public abstract class PublicKeyKeyEncryptionMethodGenerator
20-
extends PGPKeyEncryptionMethodGenerator
20+
implements PGPKeyEncryptionMethodGenerator
2121
{
2222
public static final String SESSION_KEY_OBFUSCATION_PROPERTY = "org.bouncycastle.openpgp.session_key_obfuscation";
2323
public static final long WILDCARD_KEYID = 0L;
@@ -164,8 +164,22 @@ private byte[] convertToEncodedMPI(byte[] encryptedSessionInfo)
164164
}
165165
}
166166

167+
/**
168+
* Generate a Public-Key Encrypted Session-Key (PKESK) packet of version 3.
169+
* PKESKv3 packets are used with Symmetrically-Encrypted-Integrity-Protected Data (SEIPD) packets of
170+
* version 1 or with Symmetrically-Encrypted Data (SED) packets and MUST NOT be used with SEIPDv2 packets.
171+
* PKESKv3 packets are used with keys that do not support {@link org.bouncycastle.bcpg.sig.Features#FEATURE_SEIPD_V2}
172+
* or as a fallback.
173+
*
174+
* @param sessionInfo session-key algorithm + session-key + checksum
175+
* @return version 3 PKESK packet
176+
*
177+
* @throws PGPException
178+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-3-public-key-encryp">
179+
* RFC9580 - Version 3 Public Key Encrypted Session Key Packet</a>
180+
*/
167181
public ContainedPacket generateV3(byte[] sessionInfo)
168-
throws PGPException
182+
throws PGPException
169183
{
170184
long keyId;
171185
if (useWildcardRecipient)
@@ -181,6 +195,19 @@ public ContainedPacket generateV3(byte[] sessionInfo)
181195
return PublicKeyEncSessionPacket.createV3PKESKPacket(keyId, pubKey.getAlgorithm(), encodedEncSessionInfo);
182196
}
183197

198+
/**
199+
* Generate a Public-Key Encrypted Session-Key (PKESK) packet of version 6.
200+
* PKESKv6 packets are used with Symmetrically-Encrypted Integrity-Protected Data (SEIPD) packets
201+
* of version 2 only.
202+
* PKESKv6 packets are used with keys that support {@link org.bouncycastle.bcpg.sig.Features#FEATURE_SEIPD_V2}.
203+
*
204+
* @param sessionInfo session-key algorithm id + session-key + checksum
205+
* @return PKESKv6 packet
206+
*
207+
* @throws PGPException if the PKESK packet cannot be generated
208+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-6-public-key-encryp">
209+
* RFC9580 - Version 6 Public Key Encrypted Session Key Packet</a>
210+
*/
184211
public ContainedPacket generateV6(byte[] sessionInfo)
185212
throws PGPException
186213
{

0 commit comments

Comments
 (0)