Skip to content

Commit 9a5c220

Browse files
author
gefeili
committed
Refactor in PGPEncryptedDataGenerator.open
1 parent 8b3fdd3 commit 9a5c220

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public PGPEncryptedDataGenerator(PGPDataEncryptorBuilder encryptorBuilder, boole
121121
* Some versions of PGP always expect a session key, this will force use
122122
* of a session key even if a single PBE encryptor is provided.
123123
*
124-
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#section-5.3.1-4">
125-
* RFC9580 - Description of the optional encrypted session key field</a>
126124
* @param forceSessionKey true if a session key should always be used, default is true.
125+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#section-5.3.1-4">
126+
* RFC9580 - Description of the optional encrypted session key field</a>
127127
*/
128128
public void setForceSessionKey(boolean forceSessionKey)
129129
{
@@ -219,41 +219,38 @@ private OutputStream open(
219219
pOut = new BCPGOutputStream(out, !useOldFormat);
220220

221221
byte[] sessionKey; // session key, either protected by - or directly derived from session key encryption mechanism.
222-
byte[] sessionInfo; // sessionKey with prepended alg-id, appended checksum
223-
222+
byte[] sessionInfo = null; // sessionKey with prepended alg-id, appended checksum, null indicates direct use of S2K output as sessionKey/messageKey
224223
byte[] messageKey; // key used to encrypt the message. In OpenPGP v6 this is derived from sessionKey + salt.
225224

226225
boolean directS2K = !forceSessionKey && methods.size() == 1 &&
227-
methods.get(0) instanceof PBEKeyEncryptionMethodGenerator;
228-
boolean isV5StyleAEAD = dataEncryptorBuilder.isV5StyleAEAD();
229-
boolean isSEIPv2 = dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD;
230-
if (directS2K && !isSEIPv2)
231-
{
232-
sessionKey = ((PBEKeyEncryptionMethodGenerator)methods.get(0)).getKey(defAlgorithm);
233-
sessionInfo = null; // null indicates direct use of S2K output as sessionKey/messageKey
234-
}
235-
else
226+
methods.get(0) instanceof PBEKeyEncryptionMethodGenerator; // not public key
227+
boolean isV5StyleAEAD = dataEncryptorBuilder.isV5StyleAEAD(); //v5
228+
if (dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD)
236229
{
237230
sessionKey = PGPUtil.makeRandomKey(defAlgorithm, rand);
238-
// prepend algorithm, append checksum
239-
sessionInfo = createSessionInfo(defAlgorithm, sessionKey);
240-
}
241-
messageKey = sessionKey;
242-
243-
// In OpenPGP v6, we need an additional step to derive a message key and IV from the session info.
244-
// Since we cannot inject the IV into the data encryptor, we append it to the message key.
245-
if (isSEIPv2)
246-
{
231+
// In OpenPGP v6, we need an additional step to derive a message key and IV from the session info.
232+
// Since we cannot inject the IV into the data encryptor, we append it to the message key.
247233
byte[] info = SymmetricEncIntegrityPacket.createAAData(
248234
SymmetricEncIntegrityPacket.VERSION_2,
249235
defAlgorithm,
250236
dataEncryptorBuilder.getAeadAlgorithm(),
251237
dataEncryptorBuilder.getChunkSize());
252-
253238
// messageKey = key and IV, will be separated in the data encryptor
254239
messageKey = AEADUtil.deriveMessageKeyAndIv(
255240
dataEncryptorBuilder.getAeadAlgorithm(), defAlgorithm, sessionKey, salt, info);
256241
}
242+
else if (directS2K)
243+
{
244+
sessionKey = ((PBEKeyEncryptionMethodGenerator)methods.get(0)).getKey(defAlgorithm);
245+
messageKey = sessionKey;
246+
}
247+
else
248+
{
249+
sessionKey = PGPUtil.makeRandomKey(defAlgorithm, rand);
250+
// prepend algorithm, append checksum
251+
sessionInfo = createSessionInfo(defAlgorithm, sessionKey);
252+
messageKey = sessionKey;
253+
}
257254

258255
PGPDataEncryptor dataEncryptor = dataEncryptorBuilder.build(messageKey);
259256
digestCalc = dataEncryptor.getIntegrityCalculator();
@@ -272,6 +269,8 @@ private OutputStream open(
272269
}
273270
else // data is encrypted by v2 SEIPD (AEAD), so write v6 SKESK packet
274271
{
272+
//https://www.rfc-editor.org/rfc/rfc9580.html#section-3.7.2.1 Table 2
273+
//AEAD(HKDF(S2K(passphrase), info), secrets, packetprefix)
275274
writeOpenPGPv6ESKPacket(method, aeadDataEncryptor.getAEADAlgorithm(), sessionKey);
276275
}
277276
}
@@ -323,7 +322,7 @@ private OutputStream open(
323322
{
324323
if (digestCalc != null)
325324
{
326-
encOut = SymmetricEncIntegrityPacket.createVersion1Packet();
325+
encOut = SymmetricEncIntegrityPacket.createVersion1Packet();
327326
if (useOldFormat)
328327
{
329328
throw new PGPException("symmetric-enc-integrity packets not supported in old PGP format");

0 commit comments

Comments
 (0)