Skip to content

Commit 46588f7

Browse files
author
gefeili
committed
Merge branch 'pg-refactor-BcUtil-createStreamCipher' into pg-refactor-BcAEADUtil
# Conflicts: # pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPBEDataDecryptorFactory.java # pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPBESecretKeyDecryptorBuilder.java
2 parents 6bcb85d + c6cfe18 commit 46588f7

File tree

6 files changed

+22
-39
lines changed

6 files changed

+22
-39
lines changed

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPBEDataDecryptorFactory.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class BcPBEDataDecryptorFactory
2727
* @param pass the passphrase to use as the primary source of key material.
2828
* @param calculatorProvider a digest calculator provider to provide calculators to support the key generation calculation required.
2929
*/
30-
public BcPBEDataDecryptorFactory(char[] pass, BcPGPDigestCalculatorProvider calculatorProvider)
30+
public BcPBEDataDecryptorFactory(char[] pass, PGPDigestCalculatorProvider calculatorProvider)
3131
{
3232
super(pass, calculatorProvider);
3333
}
@@ -51,15 +51,7 @@ public byte[] recoverSessionData(int keyAlgorithm, byte[] key, byte[] secKeyData
5151
if (secKeyData != null && secKeyData.length > 0)
5252
{
5353
BlockCipher engine = BcImplProvider.createBlockCipher(keyAlgorithm);
54-
BufferedBlockCipher cipher = BcUtil.createSymmetricKeyWrapper(false, engine, key, new byte[engine.getBlockSize()]);
55-
56-
byte[] out = new byte[secKeyData.length];
57-
58-
int len = cipher.processBytes(secKeyData, 0, secKeyData.length, out, 0);
59-
60-
len += cipher.doFinal(out, len);
61-
62-
return out;
54+
return BcUtil.processBufferedBlockCipher(false, engine, key, new byte[engine.getBlockSize()], secKeyData, 0, secKeyData.length);
6355
}
6456
else
6557
{

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPBEKeyEncryptionMethodGenerator.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.bouncycastle.bcpg.S2K;
66
import org.bouncycastle.bcpg.SymmetricKeyUtils;
77
import org.bouncycastle.crypto.BlockCipher;
8-
import org.bouncycastle.crypto.BufferedBlockCipher;
98
import org.bouncycastle.crypto.InvalidCipherTextException;
109
import org.bouncycastle.openpgp.PGPException;
1110
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
@@ -89,15 +88,7 @@ protected byte[] encryptSessionInfo(int encAlgorithm, byte[] key, byte[] session
8988
try
9089
{
9190
BlockCipher engine = BcImplProvider.createBlockCipher(encAlgorithm);
92-
BufferedBlockCipher cipher = BcUtil.createSymmetricKeyWrapper(true, engine, key, new byte[engine.getBlockSize()]);
93-
94-
byte[] out = new byte[sessionInfo.length];
95-
96-
int len = cipher.processBytes(sessionInfo, 0, sessionInfo.length, out, 0);
97-
98-
len += cipher.doFinal(out, len);
99-
100-
return out;
91+
return BcUtil.processBufferedBlockCipher(true, engine, key, new byte[engine.getBlockSize()], sessionInfo, 0, sessionInfo.length);
10192
}
10293
catch (InvalidCipherTextException e)
10394
{

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPBESecretKeyDecryptorBuilder.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bouncycastle.openpgp.operator.bc;
22

3-
import org.bouncycastle.crypto.BufferedBlockCipher;
43
import org.bouncycastle.crypto.InvalidCipherTextException;
54
import org.bouncycastle.openpgp.PGPException;
65
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
@@ -24,14 +23,7 @@ public byte[] recoverKeyData(int encAlgorithm, byte[] key, byte[] iv, byte[] key
2423
{
2524
try
2625
{
27-
BufferedBlockCipher c = BcUtil.createSymmetricKeyWrapper(false, BcImplProvider.createBlockCipher(encAlgorithm), key, iv);
28-
29-
byte[] out = new byte[keyLen];
30-
int outLen = c.processBytes(keyData, keyOff, keyLen, out, 0);
31-
32-
outLen += c.doFinal(out, outLen);
33-
34-
return out;
26+
return BcUtil.processBufferedBlockCipher(false, BcImplProvider.createBlockCipher(encAlgorithm), key, iv, keyData, keyOff, keyLen);
3527
}
3628
catch (InvalidCipherTextException e)
3729
{

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPBESecretKeyEncryptorBuilder.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.security.SecureRandom;
44

55
import org.bouncycastle.crypto.BlockCipher;
6-
import org.bouncycastle.crypto.BufferedBlockCipher;
76
import org.bouncycastle.crypto.InvalidCipherTextException;
87
import org.bouncycastle.openpgp.PGPException;
98
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
@@ -117,15 +116,7 @@ public byte[] encryptKeyData(byte[] key, byte[] iv, byte[] keyData, int keyOff,
117116

118117
this.random.nextBytes(iv);
119118
}
120-
121-
BufferedBlockCipher c = BcUtil.createSymmetricKeyWrapper(true, engine, key, iv);
122-
123-
byte[] out = new byte[keyLen];
124-
int outLen = c.processBytes(keyData, keyOff, keyLen, out, 0);
125-
126-
outLen += c.doFinal(out, outLen);
127-
128-
return out;
119+
return BcUtil.processBufferedBlockCipher(true, engine, key, iv, keyData, keyOff, keyLen);
129120
}
130121
catch (InvalidCipherTextException e)
131122
{

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.bouncycastle.crypto.BlockCipher;
1414
import org.bouncycastle.crypto.BufferedBlockCipher;
1515
import org.bouncycastle.crypto.DefaultBufferedBlockCipher;
16+
import org.bouncycastle.crypto.InvalidCipherTextException;
1617
import org.bouncycastle.crypto.RawAgreement;
1718
import org.bouncycastle.crypto.ec.CustomNamedCurves;
1819
import org.bouncycastle.crypto.io.CipherInputStream;
@@ -102,6 +103,21 @@ public static BufferedBlockCipher createSymmetricKeyWrapper(boolean forEncryptio
102103
return c;
103104
}
104105

106+
static byte[] processBufferedBlockCipher(boolean forEncryption, BlockCipher engine, byte[] key, byte[] iv, byte[] msg, int msgOff, int msgLen)
107+
throws InvalidCipherTextException
108+
109+
{
110+
BufferedBlockCipher cipher = BcUtil.createSymmetricKeyWrapper(forEncryption, engine, key, iv);
111+
112+
byte[] out = new byte[msgLen];
113+
114+
int len = cipher.processBytes(msg, msgOff, msgLen, out, 0);
115+
116+
len += cipher.doFinal(out, len);
117+
118+
return out;
119+
}
120+
105121
static X9ECParameters getX9Parameters(ASN1ObjectIdentifier curveOID)
106122
{
107123
X9ECParameters x9 = CustomNamedCurves.getByOID(curveOID);

pkix/src/test/java/org/bouncycastle/operator/test/AllTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public void testAgainstKnownList()
220220
new Object[]{BCObjectIdentifiers.sphincsPlus_haraka_256s_r3_simple, "SPHINCS+"},
221221
new Object[]{BCObjectIdentifiers.sphincsPlus_haraka_256f_r3_simple, "SPHINCS+"},
222222
new Object[]{GNUObjectIdentifiers.Tiger_192, "Tiger"},
223+
new Object[]{PKCSObjectIdentifiers.id_alg_hss_lms_hashsig, "LMS"},
223224

224225
new Object[]{PKCSObjectIdentifiers.RC2_CBC, "RC2/CBC"},
225226
new Object[]{PKCSObjectIdentifiers.des_EDE3_CBC, "DESEDE-3KEY/CBC"},

0 commit comments

Comments
 (0)