Skip to content

Commit c83aa87

Browse files
committed
Fix symmetric v6 message encryption
Commits 7d95b08 and ea31631 introduced an error where the plain session key was passed in the wrong format causing the session-key wrapper to fail due to an invalid block size.
1 parent 9fb27b7 commit c83aa87

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,13 @@ private OutputStream open(
230230

231231
boolean directS2K = !forceSessionKey && methods.size() == 1 &&
232232
methods.get(0) instanceof PBEKeyEncryptionMethodGenerator; // not public key
233+
233234
boolean isV5StyleAEAD = dataEncryptorBuilder.isV5StyleAEAD(); //v5
234-
if (dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD)
235+
boolean isSeipdV2 = dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD;
236+
if (isSeipdV2)
235237
{
236238
sessionKey = PGPUtil.makeRandomKey(defAlgorithm, rand);
239+
sessionInfo = createSessionInfo(defAlgorithm, sessionKey);
237240
// In OpenPGP v6, we need an additional step to derive a message key and IV from the session info.
238241
// Since we cannot inject the IV into the data encryptor, we append it to the message key.
239242
byte[] info = SymmetricEncIntegrityPacket.createAAData(
@@ -282,7 +285,7 @@ else if (directS2K)
282285
{
283286
//https://www.rfc-editor.org/rfc/rfc9580.html#section-3.7.2.1 Table 2
284287
//AEAD(HKDF(S2K(passphrase), info), secrets, packetprefix)
285-
writeOpenPGPv6ESKPacket(method, aeadDataEncryptor.getAEADAlgorithm(), sessionKey);
288+
writeOpenPGPv6ESKPacket(method, aeadDataEncryptor.getAEADAlgorithm(), sessionInfo);
286289
}
287290
}
288291
// OpenPGP v4

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private ContainedPacket generateV5ESK(int kekAlgorithm, int aeadAlgorithm, byte[
201201
return SymmetricKeyEncSessionPacket.createV5Packet(kekAlgorithm, aeadAlgorithm, iv, s2k, esk, tag);
202202
}
203203

204-
private ContainedPacket generateV6ESK(int kekAlgorithm, int aeadAlgorithm, byte[] sessionKey)
204+
private ContainedPacket generateV6ESK(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo)
205205
throws PGPException
206206
{
207207
byte[] ikm = getKey(kekAlgorithm);
@@ -217,6 +217,7 @@ private ContainedPacket generateV6ESK(int kekAlgorithm, int aeadAlgorithm, byte[
217217
random.nextBytes(iv);
218218

219219
int tagLen = AEADUtils.getAuthTagLength(aeadAlgorithm);
220+
byte[] sessionKey = getSessionKey(sessionInfo);
220221
byte[] eskAndTag = getEskAndTag(kekAlgorithm, aeadAlgorithm, sessionKey, kek, iv, info);
221222
byte[] esk = Arrays.copyOfRange(eskAndTag, 0, eskAndTag.length - tagLen);
222223
byte[] tag = Arrays.copyOfRange(eskAndTag, esk.length, eskAndTag.length);

0 commit comments

Comments
 (0)