Skip to content

Commit 15111ec

Browse files
committed
fixed misspelt method, refactored out some dependent code into correct subpackages
1 parent 699336e commit 15111ec

File tree

6 files changed

+95
-39
lines changed

6 files changed

+95
-39
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private byte[] getEncodedPublicKey(
132132
return keyBytes;
133133
}
134134

135-
protected void getAttriubtesHash(PGPUserAttributeSubpacketVector userAttributes)
135+
protected void getAttributesHash(PGPUserAttributeSubpacketVector userAttributes)
136136
throws PGPException
137137
{
138138
//

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ boolean doVerifyCertification(
220220
{
221221
updateWithPublicKey(key);
222222

223-
getAttriubtesHash(userAttributes);
223+
getAttributesHash(userAttributes);
224224

225225
addTrailer();
226226

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public PGPSignature generateCertification(
240240
{
241241
updateWithPublicKey(pubKey);
242242

243-
getAttriubtesHash(userAttributes);
243+
getAttributesHash(userAttributes);
244244

245245
return this.generate();
246246
}

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,7 @@
77
import org.bouncycastle.bcpg.S2K;
88
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
99
import org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket;
10-
import org.bouncycastle.bcpg.SymmetricKeyUtils;
11-
import org.bouncycastle.crypto.InvalidCipherTextException;
12-
import org.bouncycastle.crypto.digests.SHA256Digest;
13-
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
14-
import org.bouncycastle.crypto.modes.AEADCipher;
15-
import org.bouncycastle.crypto.params.AEADParameters;
16-
import org.bouncycastle.crypto.params.HKDFParameters;
17-
import org.bouncycastle.crypto.params.KeyParameter;
1810
import org.bouncycastle.openpgp.PGPException;
19-
import org.bouncycastle.openpgp.operator.bc.BcAEADUtil;
2011
import org.bouncycastle.util.Arrays;
2112

2213
/**
@@ -220,12 +211,7 @@ private ContainedPacket generateV6ESK(int kekAlgorithm, int aeadAlgorithm, byte[
220211
(byte) kekAlgorithm,
221212
(byte) aeadAlgorithm
222213
};
223-
HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
224-
hkdf.init(new HKDFParameters(ikm, null, info));
225-
226-
int kekLen = SymmetricKeyUtils.getKeyLengthInOctets(kekAlgorithm);
227-
byte[] kek = new byte[kekLen];
228-
hkdf.generateBytes(kek, 0, kek.length);
214+
byte[] kek = generateV6KEK(kekAlgorithm, ikm, info);
229215

230216
byte[] iv = new byte[AEADUtils.getIVLength(aeadAlgorithm)];
231217
random.nextBytes(iv);
@@ -238,27 +224,6 @@ private ContainedPacket generateV6ESK(int kekAlgorithm, int aeadAlgorithm, byte[
238224
return SymmetricKeyEncSessionPacket.createV6Packet(kekAlgorithm, aeadAlgorithm, iv, s2k, esk, tag);
239225
}
240226

241-
private byte[] getEskAndTag(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo, byte[] key, byte[] iv, byte[] info)
242-
throws PGPException
243-
{
244-
byte[] sessionKey = new byte[sessionInfo.length - 3];
245-
System.arraycopy(sessionInfo, 1, sessionKey, 0, sessionKey.length);
246-
247-
AEADCipher aeadCipher = BcAEADUtil.createAEADCipher(kekAlgorithm, aeadAlgorithm);
248-
aeadCipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv, info));
249-
int outLen = aeadCipher.getOutputSize(sessionKey.length);
250-
byte[] eskAndTag = new byte[outLen];
251-
int len = aeadCipher.processBytes(sessionKey, 0, sessionKey.length, eskAndTag, 0);
252-
try
253-
{
254-
len += aeadCipher.doFinal(eskAndTag, len);
255-
}
256-
catch (InvalidCipherTextException e)
257-
{
258-
throw new PGPException("cannot encrypt session info", e);
259-
}
260-
return eskAndTag;
261-
}
262227
/**
263228
* Generate a V4 SKESK packet.
264229
*
@@ -288,4 +253,10 @@ public ContainedPacket generate(int encAlgorithm, byte[] sessionInfo)
288253

289254
abstract protected byte[] encryptSessionInfo(int encAlgorithm, byte[] key, byte[] sessionInfo)
290255
throws PGPException;
256+
257+
abstract protected byte[] getEskAndTag(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo, byte[] key, byte[] iv, byte[] info)
258+
throws PGPException;
259+
260+
abstract protected byte[] generateV6KEK(int kekAlgorithm, byte[] ikm, byte[] info)
261+
throws PGPException;
291262
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
import java.security.SecureRandom;
44

55
import org.bouncycastle.bcpg.S2K;
6+
import org.bouncycastle.bcpg.SymmetricKeyUtils;
67
import org.bouncycastle.crypto.BlockCipher;
78
import org.bouncycastle.crypto.BufferedBlockCipher;
89
import org.bouncycastle.crypto.InvalidCipherTextException;
10+
import org.bouncycastle.crypto.digests.SHA256Digest;
11+
import org.bouncycastle.crypto.engines.CamelliaEngine;
12+
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
13+
import org.bouncycastle.crypto.modes.AEADCipher;
14+
import org.bouncycastle.crypto.params.AEADParameters;
15+
import org.bouncycastle.crypto.params.HKDFParameters;
16+
import org.bouncycastle.crypto.params.KeyParameter;
917
import org.bouncycastle.openpgp.PGPException;
1018
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
1119
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
@@ -103,4 +111,38 @@ protected byte[] encryptSessionInfo(int encAlgorithm, byte[] key, byte[] session
103111
throw new PGPException("encryption failed: " + e.getMessage(), e);
104112
}
105113
}
114+
115+
protected byte[] generateV6KEK(int kekAlgorithm, byte[] ikm, byte[] info)
116+
throws PGPException
117+
{
118+
HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
119+
hkdf.init(new HKDFParameters(ikm, null, info));
120+
121+
int kekLen = SymmetricKeyUtils.getKeyLengthInOctets(kekAlgorithm);
122+
byte[] kek = new byte[kekLen];
123+
hkdf.generateBytes(kek, 0, kek.length);
124+
return kek;
125+
}
126+
127+
protected byte[] getEskAndTag(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo, byte[] key, byte[] iv, byte[] info)
128+
throws PGPException
129+
{
130+
byte[] sessionKey = new byte[sessionInfo.length - 3];
131+
System.arraycopy(sessionInfo, 1, sessionKey, 0, sessionKey.length);
132+
133+
AEADCipher aeadCipher = BcAEADUtil.createAEADCipher(kekAlgorithm, aeadAlgorithm);
134+
aeadCipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv, info));
135+
int outLen = aeadCipher.getOutputSize(sessionKey.length);
136+
byte[] eskAndTag = new byte[outLen];
137+
int len = aeadCipher.processBytes(sessionKey, 0, sessionKey.length, eskAndTag, 0);
138+
try
139+
{
140+
len += aeadCipher.doFinal(eskAndTag, len);
141+
}
142+
catch (InvalidCipherTextException e)
143+
{
144+
throw new PGPException("cannot encrypt session info", e);
145+
}
146+
return eskAndTag;
147+
}
106148
}

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcePBEKeyEncryptionMethodGenerator.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@
1313
import javax.crypto.spec.SecretKeySpec;
1414

1515
import org.bouncycastle.bcpg.S2K;
16+
import org.bouncycastle.bcpg.SymmetricKeyUtils;
17+
import org.bouncycastle.crypto.InvalidCipherTextException;
18+
import org.bouncycastle.crypto.digests.SHA256Digest;
19+
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
20+
import org.bouncycastle.crypto.modes.AEADCipher;
21+
import org.bouncycastle.crypto.params.AEADParameters;
22+
import org.bouncycastle.crypto.params.HKDFParameters;
23+
import org.bouncycastle.crypto.params.KeyParameter;
1624
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
1725
import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
1826
import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
1927
import org.bouncycastle.openpgp.PGPException;
2028
import org.bouncycastle.openpgp.PGPUtil;
2129
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
2230
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
31+
import org.bouncycastle.openpgp.operator.bc.BcAEADUtil;
2332

2433
/**
2534
* JCE based generator for password based encryption (PBE) data protection methods.
@@ -139,4 +148,38 @@ protected byte[] encryptSessionInfo(int encAlgorithm, byte[] key, byte[] session
139148
throw new PGPException("key invalid: " + e.getMessage(), e);
140149
}
141150
}
151+
152+
protected byte[] generateV6KEK(int kekAlgorithm, byte[] ikm, byte[] info)
153+
throws PGPException
154+
{
155+
HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
156+
hkdf.init(new HKDFParameters(ikm, null, info));
157+
158+
int kekLen = SymmetricKeyUtils.getKeyLengthInOctets(kekAlgorithm);
159+
byte[] kek = new byte[kekLen];
160+
hkdf.generateBytes(kek, 0, kek.length);
161+
return kek;
162+
}
163+
164+
protected byte[] getEskAndTag(int kekAlgorithm, int aeadAlgorithm, byte[] sessionInfo, byte[] key, byte[] iv, byte[] info)
165+
throws PGPException
166+
{
167+
byte[] sessionKey = new byte[sessionInfo.length - 3];
168+
System.arraycopy(sessionInfo, 1, sessionKey, 0, sessionKey.length);
169+
170+
AEADCipher aeadCipher = BcAEADUtil.createAEADCipher(kekAlgorithm, aeadAlgorithm);
171+
aeadCipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv, info));
172+
int outLen = aeadCipher.getOutputSize(sessionKey.length);
173+
byte[] eskAndTag = new byte[outLen];
174+
int len = aeadCipher.processBytes(sessionKey, 0, sessionKey.length, eskAndTag, 0);
175+
try
176+
{
177+
len += aeadCipher.doFinal(eskAndTag, len);
178+
}
179+
catch (InvalidCipherTextException e)
180+
{
181+
throw new PGPException("cannot encrypt session info", e);
182+
}
183+
return eskAndTag;
184+
}
142185
}

0 commit comments

Comments
 (0)