Skip to content

Commit 99c3646

Browse files
committed
modified builders to make setV5/V6 order independent.
1 parent f923a32 commit 99c3646

File tree

4 files changed

+13
-89
lines changed

4 files changed

+13
-89
lines changed

Diff for: pg/src/main/java/org/bouncycastle/openpgp/PGPEncryptedDataGenerator.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ private OutputStream open(
242242

243243
// In OpenPGP v6, we need an additional step to derive a message key and IV from the session info.
244244
// Since we cannot inject the IV into the data encryptor, we append it to the message key.
245-
if (dataEncryptorBuilder.getAeadAlgorithm() != -1 && !dataEncryptorBuilder.isV5StyleAEAD())
245+
boolean isV5StyleAEAD = dataEncryptorBuilder.isV5StyleAEAD();
246+
if (dataEncryptorBuilder.getAeadAlgorithm() != -1 && !isV5StyleAEAD)
246247
{
247248
byte[] info = SymmetricEncIntegrityPacket.createAAData(
248249
SymmetricEncIntegrityPacket.VERSION_2,
@@ -266,7 +267,7 @@ private OutputStream open(
266267
{
267268
PGPAEADDataEncryptor aeadDataEncryptor = (PGPAEADDataEncryptor) dataEncryptor;
268269
// data is encrypted by AEAD Encrypted Data packet (rfc4880bis10), so write v5 SKESK packet
269-
if (aeadDataEncryptor.isV5StyleAEAD())
270+
if (isV5StyleAEAD)
270271
{
271272
writeOpenPGPv5ESKPacket(method, sessionInfo);
272273
}
@@ -289,7 +290,7 @@ private OutputStream open(
289290
PGPAEADDataEncryptor encryptor = (PGPAEADDataEncryptor)dataEncryptor;
290291

291292
// OpenPGP V5 style AEAD
292-
if (encryptor.isV5StyleAEAD())
293+
if (isV5StyleAEAD)
293294
{
294295
byte[] iv = encryptor.getIV();
295296

Diff for: pg/src/main/java/org/bouncycastle/openpgp/operator/PGPAEADDataEncryptor.java

-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ public interface PGPAEADDataEncryptor
1919

2020
byte[] getIV();
2121

22-
boolean isV5StyleAEAD();
2322
}

Diff for: pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPGPDataEncryptorBuilder.java

-37
Original file line numberDiff line numberDiff line change
@@ -88,38 +88,6 @@ public BcPGPDataEncryptorBuilder setUseV6AEAD()
8888
*/
8989
@Override
9090
public BcPGPDataEncryptorBuilder setWithAEAD(int aeadAlgorithm, int chunkSize)
91-
{
92-
if (isV5StyleAEAD)
93-
{
94-
return setWithV5AEAD(aeadAlgorithm, chunkSize);
95-
}
96-
else
97-
{
98-
return setWithV6AEAD(aeadAlgorithm, chunkSize);
99-
}
100-
}
101-
102-
private BcPGPDataEncryptorBuilder setWithV5AEAD(int aeadAlgorithm, int chunkSize)
103-
{
104-
if (encAlgorithm != SymmetricKeyAlgorithmTags.AES_128
105-
&& encAlgorithm != SymmetricKeyAlgorithmTags.AES_192
106-
&& encAlgorithm != SymmetricKeyAlgorithmTags.AES_256)
107-
{
108-
throw new IllegalStateException("AEAD algorithms can only be used with AES");
109-
}
110-
111-
if (chunkSize < 6)
112-
{
113-
throw new IllegalArgumentException("minimum chunkSize is 6");
114-
}
115-
116-
this.aeadAlgorithm = aeadAlgorithm;
117-
this.chunkSize = chunkSize - 6;
118-
119-
return this;
120-
}
121-
122-
private BcPGPDataEncryptorBuilder setWithV6AEAD(int aeadAlgorithm, int chunkSize)
12391
{
12492
if (encAlgorithm != SymmetricKeyAlgorithmTags.AES_128
12593
&& encAlgorithm != SymmetricKeyAlgorithmTags.AES_192
@@ -323,10 +291,5 @@ public byte[] getIV()
323291
return Arrays.clone(iv);
324292
}
325293

326-
@Override
327-
public boolean isV5StyleAEAD()
328-
{
329-
return isV5StyleAEAD;
330-
}
331294
}
332295
}

Diff for: pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcePGPDataEncryptorBuilder.java

+9-48
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.bouncycastle.openpgp.operator.PGPDataEncryptor;
2222
import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder;
2323
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
24-
import org.bouncycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder;
2524
import org.bouncycastle.util.Arrays;
2625

2726
/**
@@ -77,34 +76,6 @@ public JcePGPDataEncryptorBuilder setWithIntegrityPacket(boolean withIntegrityPa
7776

7877
@Override
7978
public JcePGPDataEncryptorBuilder setWithAEAD(int aeadAlgorithm, int chunkSize)
80-
{
81-
if (isV5StyleAEAD)
82-
{
83-
return setWithV5AEAD(aeadAlgorithm, chunkSize);
84-
}
85-
else
86-
{
87-
return setWithV6AEAD(aeadAlgorithm, chunkSize);
88-
}
89-
}
90-
91-
@Override
92-
public JcePGPDataEncryptorBuilder setUseV5AEAD()
93-
{
94-
this.isV5StyleAEAD = true;
95-
96-
return this;
97-
}
98-
99-
@Override
100-
public JcePGPDataEncryptorBuilder setUseV6AEAD()
101-
{
102-
this.isV5StyleAEAD = false;
103-
104-
return this;
105-
}
106-
107-
private JcePGPDataEncryptorBuilder setWithV5AEAD(int aeadAlgorithm, int chunkSize)
10879
{
10980
if (encAlgorithm != SymmetricKeyAlgorithmTags.AES_128
11081
&& encAlgorithm != SymmetricKeyAlgorithmTags.AES_192
@@ -124,23 +95,18 @@ private JcePGPDataEncryptorBuilder setWithV5AEAD(int aeadAlgorithm, int chunkSiz
12495
return this;
12596
}
12697

127-
private JcePGPDataEncryptorBuilder setWithV6AEAD(int aeadAlgorithm, int chunkSize)
98+
@Override
99+
public JcePGPDataEncryptorBuilder setUseV5AEAD()
128100
{
129-
this.isV5StyleAEAD = false;
130-
if (encAlgorithm != SymmetricKeyAlgorithmTags.AES_128
131-
&& encAlgorithm != SymmetricKeyAlgorithmTags.AES_192
132-
&& encAlgorithm != SymmetricKeyAlgorithmTags.AES_256)
133-
{
134-
throw new IllegalStateException("AEAD algorithms can only be used with AES");
135-
}
101+
this.isV5StyleAEAD = true;
136102

137-
if (chunkSize < 6)
138-
{
139-
throw new IllegalArgumentException("minimum chunkSize is 6");
140-
}
103+
return this;
104+
}
141105

142-
this.aeadAlgorithm = aeadAlgorithm;
143-
this.chunkSize = chunkSize - 6;
106+
@Override
107+
public JcePGPDataEncryptorBuilder setUseV6AEAD()
108+
{
109+
this.isV5StyleAEAD = false;
144110

145111
return this;
146112
}
@@ -367,10 +333,5 @@ public byte[] getIV()
367333
return Arrays.clone(iv);
368334
}
369335

370-
@Override
371-
public boolean isV5StyleAEAD()
372-
{
373-
return isV5StyleAEAD;
374-
}
375336
}
376337
}

0 commit comments

Comments
 (0)