Skip to content

Commit 64c951f

Browse files
author
gefeili
committed
Change optSymAlgId from byte[] to byte. Change lambda to anonymous function from JcePublicKeyKeyEncryptionMethodGenerator.
1 parent c7f610d commit 64c951f

File tree

4 files changed

+91
-68
lines changed

4 files changed

+91
-68
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private void writeOpenPGPv4ESKPacket(PGPKeyEncryptionMethodGenerator m, byte[] s
394394
else if (m instanceof PublicKeyKeyEncryptionMethodGenerator)
395395
{
396396
PublicKeyKeyEncryptionMethodGenerator mGen = (PublicKeyKeyEncryptionMethodGenerator) m;
397-
pOut.writePacket(mGen.generateV3(defAlgorithm, sessionInfo));
397+
pOut.writePacket(mGen.generateV3(sessionInfo));
398398
}
399399
}
400400

@@ -423,7 +423,7 @@ private void writeOpenPGPv5ESKPacket(PGPKeyEncryptionMethodGenerator m, byte[] s
423423
else if (m instanceof PublicKeyKeyEncryptionMethodGenerator)
424424
{
425425
PublicKeyKeyEncryptionMethodGenerator mGen = (PublicKeyKeyEncryptionMethodGenerator) m;
426-
pOut.writePacket(mGen.generateV3(defAlgorithm, sessionInfo));
426+
pOut.writePacket(mGen.generateV3(sessionInfo));
427427
}
428428
}
429429

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

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ private byte[] convertToEncodedMPI(byte[] encryptedSessionInfo)
155155
}
156156
}
157157

158-
public ContainedPacket generateV3(int encAlgorithm, byte[] sessionInfo)
159-
throws PGPException
158+
public ContainedPacket generateV3(byte[] sessionInfo)
159+
throws PGPException
160160
{
161161
long keyId;
162162
if (useWildcardRecipient)
@@ -167,7 +167,7 @@ public ContainedPacket generateV3(int encAlgorithm, byte[] sessionInfo)
167167
{
168168
keyId = pubKey.getKeyID();
169169
}
170-
byte[] encryptedSessionInfo = encryptSessionInfoV3(pubKey, sessionInfo);
170+
byte[] encryptedSessionInfo = encryptSessionInfo(pubKey, sessionInfo, sessionInfo, sessionInfo[0]);
171171
byte[][] encodedEncSessionInfo = encodeEncryptedSessionInfo(encryptedSessionInfo);
172172
return PublicKeyEncSessionPacket.createV3PKESKPacket(keyId, pubKey.getAlgorithm(), encodedEncSessionInfo);
173173
}
@@ -187,91 +187,75 @@ public ContainedPacket generateV6(byte[] sessionInfo)
187187
keyFingerprint = pubKey.getFingerprint();
188188
keyVersion = pubKey.getVersion();
189189
}
190-
byte[] encryptedSessionInfo = encryptSessionInfoV6(pubKey, sessionInfo);
190+
// In V6, do not include the symmetric-key algorithm in the session-info
191+
byte[] sessionInfoWithoutAlgId = new byte[sessionInfo.length - 1];
192+
System.arraycopy(sessionInfo, 1, sessionInfoWithoutAlgId, 0, sessionInfoWithoutAlgId.length);
193+
194+
byte[] encryptedSessionInfo = encryptSessionInfo(pubKey, sessionInfo, sessionInfoWithoutAlgId, (byte)0);
191195
byte[][] encodedEncSessionInfo = encodeEncryptedSessionInfo(encryptedSessionInfo);
192196
return PublicKeyEncSessionPacket.createV6PKESKPacket(keyVersion, keyFingerprint, pubKey.getAlgorithm(), encodedEncSessionInfo);
193197
}
194198

195199
/**
196200
* Encrypt a session key using the recipients public key.
197-
* @param pubKey recipients public key
198-
* @param fullSessionInfo full session info (sym-alg-id + session-key + 2 octet checksum)
201+
*
202+
* @param pubKey recipients public key
203+
* @param fullSessionInfo full session info (sym-alg-id + session-key + 2 octet checksum)
199204
* @param sessionInfoToEncrypt for v3: full session info; for v6: just the session-key
200-
* @param optSymAlgId for v3: session key algorithm ID; for v6: empty array
205+
* @param optSymAlgId for v3: session key algorithm ID; for v6: empty array
201206
* @return encrypted session info
202207
* @throws PGPException
203208
*/
204209
protected abstract byte[] encryptSessionInfo(PGPPublicKey pubKey,
205210
byte[] fullSessionInfo,
206211
byte[] sessionInfoToEncrypt,
207-
byte[] optSymAlgId)
212+
byte optSymAlgId)
208213
throws PGPException;
209214

210-
/**
211-
* Encrypt a session key for a v3 PKESK.
212-
* @param pubKey recipients public key
213-
* @param sessionInfo session info (sym-alg-id + session-key + 2 octet checksum)
214-
* @return encrypted session info
215-
* @throws PGPException
216-
*/
217-
protected byte[] encryptSessionInfoV3(PGPPublicKey pubKey, byte[] sessionInfo)
218-
throws PGPException
219-
{
220-
return encryptSessionInfo(pubKey, sessionInfo, sessionInfo, new byte[]{sessionInfo[0]});
221-
}
222-
223-
/**
224-
* Encrypt a session key for a v6 PKESK.
225-
* @param pubKey recipients public key
226-
* @param sessionInfo session info (sym-alg-id + session-key + 2 octet checksum)
227-
* @return encrypted session info
228-
* @throws PGPException
229-
*/
230-
protected byte[] encryptSessionInfoV6(PGPPublicKey pubKey, byte[] sessionInfo)
231-
throws PGPException
232-
{
233-
// In V6, do not include the symmetric-key algorithm in the session-info
234-
byte[] sessionInfoWithoutAlgId = new byte[sessionInfo.length - 1];
235-
System.arraycopy(sessionInfo, 1, sessionInfoWithoutAlgId, 0, sessionInfoWithoutAlgId.length);
236-
237-
return encryptSessionInfo(pubKey, sessionInfo, sessionInfoWithoutAlgId, new byte[0]);
238-
}
239215

240216
protected static byte[] concatECDHEphKeyWithWrappedSessionKey(byte[] ephPubEncoding, byte[] wrappedSessionKey)
241217
throws IOException
242218
{
243219
// https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
244220

245221
byte[] mpiEncodedEphemeralKey = new MPInteger(new BigInteger(1, ephPubEncoding))
246-
.getEncoded();
222+
.getEncoded();
247223
byte[] out = new byte[mpiEncodedEphemeralKey.length + 1 + wrappedSessionKey.length];
248224
// eph key
249225
System.arraycopy(mpiEncodedEphemeralKey, 0, out, 0, mpiEncodedEphemeralKey.length);
250226
// enc session-key len
251-
out[mpiEncodedEphemeralKey.length] = (byte) wrappedSessionKey.length;
227+
out[mpiEncodedEphemeralKey.length] = (byte)wrappedSessionKey.length;
252228
// enc session-key
253229
System.arraycopy(wrappedSessionKey, 0, out, mpiEncodedEphemeralKey.length + 1, wrappedSessionKey.length);
254230

255231
return out;
256232
}
257233

258-
// private static byte[] getSessionInfo(byte[] ephPubEncoding, int symmetricKeyAlgorithm, byte[] c)
259-
// {
260-
// return getSessionInfo(ephPubEncoding, new byte[]{(byte) symmetricKeyAlgorithm}, c);
261-
// }
262-
263-
protected static byte[] getSessionInfo(byte[] ephPubEncoding, byte[] optSymKeyAlgorithm, byte[] wrappedSessionKey)
234+
protected static byte[] getSessionInfo(byte[] ephPubEncoding, byte optSymKeyAlgorithm, byte[] wrappedSessionKey)
264235
{
265-
int len = ephPubEncoding.length + 1 + optSymKeyAlgorithm.length + wrappedSessionKey.length;
236+
int len = ephPubEncoding.length + 1 + wrappedSessionKey.length;
237+
if (optSymKeyAlgorithm != 0)
238+
{
239+
len++;
240+
}
266241
byte[] out = new byte[len];
267242
// ephemeral pub key
268243
System.arraycopy(ephPubEncoding, 0, out, 0, ephPubEncoding.length);
269244
// len of two/one next fields
270-
out[ephPubEncoding.length] = (byte) (wrappedSessionKey.length + optSymKeyAlgorithm.length);
245+
out[ephPubEncoding.length] = (byte)(wrappedSessionKey.length + 1);
271246
// (optional) sym key alg
272-
System.arraycopy(optSymKeyAlgorithm, 0, out, ephPubEncoding.length + 1, optSymKeyAlgorithm.length);
273-
// wrapped session key
274-
System.arraycopy(wrappedSessionKey, 0, out, ephPubEncoding.length + 1 + optSymKeyAlgorithm.length, wrappedSessionKey.length);
247+
if (optSymKeyAlgorithm != 0)
248+
{
249+
out[ephPubEncoding.length + 1] = optSymKeyAlgorithm;
250+
// wrapped session key
251+
System.arraycopy(wrappedSessionKey, 0, out, ephPubEncoding.length + 1 + 1, wrappedSessionKey.length);
252+
}
253+
else
254+
{
255+
// wrapped session key
256+
System.arraycopy(wrappedSessionKey, 0, out, ephPubEncoding.length + 1, wrappedSessionKey.length);
257+
}
258+
275259
return out;
276260
}
277261
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public BcPublicKeyKeyEncryptionMethodGenerator setSecureRandom(SecureRandom rand
7474
}
7575

7676
@Override
77-
protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] fullSessionInfo, byte[] sessionInfoToEncrypt, byte[] optSymAlgId)
77+
protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] fullSessionInfo, byte[] sessionInfoToEncrypt, byte optSymAlgId)
7878
throws PGPException
7979
{
8080
try
@@ -231,7 +231,7 @@ private byte[] encryptSessionInfoWithX25519X448Key(PublicKeyPacket pubKeyPacket,
231231
AsymmetricKeyParameter cryptoPublicKey,
232232
int keySize,
233233
EphPubEncodingOperation ephPubEncodingOperation,
234-
byte[] optSymAlgId)
234+
byte optSymAlgId)
235235
throws PGPException
236236
{
237237
AsymmetricCipherKeyPair ephKp = getAsymmetricCipherKeyPair(gen, parameters);

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

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public JcePublicKeyKeyEncryptionMethodGenerator setSecureRandom(SecureRandom ran
9191
protected byte[] encryptSessionInfo(PGPPublicKey pubKey,
9292
byte[] fullSessionInfo,
9393
byte[] sessionInfoToEncrypt,
94-
byte[] optSymAlgId)
94+
byte optSymAlgId)
9595
throws PGPException
9696
{
9797
try
@@ -109,53 +109,92 @@ protected byte[] encryptSessionInfo(PGPPublicKey pubKey,
109109
if (JcaJcePGPUtil.isX25519(ecKey.getCurveOID()))
110110
{
111111
return encryptSessionInfoWithECDHKey(pubKeyPacket, "X25519", cryptoPublicKey, keyEncryptionOID,
112-
ecKey.getSymmetricKeyAlgorithm(), sessionInfoToEncrypt, RFC6637Utils.getXDHAlgorithm(pubKeyPacket),
113-
(kpGen) -> kpGen.initialize(255, random),
114-
(ephPubEncoding) -> Arrays.prepend(ephPubEncoding, X_HDR));
112+
ecKey.getSymmetricKeyAlgorithm(), sessionInfoToEncrypt, RFC6637Utils.getXDHAlgorithm(pubKeyPacket),
113+
new KeyPairGeneratorOperation()
114+
{
115+
@Override
116+
public void initialize(KeyPairGenerator kpGen)
117+
throws GeneralSecurityException, IOException
118+
{
119+
kpGen.initialize(255, random);
120+
}
121+
},
122+
new EphPubEncoding()
123+
{
124+
@Override
125+
public byte[] getEphPubEncoding(byte[] publicKeyData)
126+
{
127+
return Arrays.prepend(publicKeyData, X_HDR);
128+
}
129+
});
115130
}
116131

117132
// Legacy X448
118133
else if (ecKey.getCurveOID().equals(EdECObjectIdentifiers.id_X448))
119134
{
120135
return encryptSessionInfoWithECDHKey(pubKeyPacket, "X448", cryptoPublicKey, keyEncryptionOID,
121-
ecKey.getSymmetricKeyAlgorithm(), sessionInfoToEncrypt, RFC6637Utils.getXDHAlgorithm(pubKeyPacket),
122-
(kpGen) -> kpGen.initialize(448, random),
123-
(ephPubEncoding) -> Arrays.prepend(ephPubEncoding, X_HDR));
136+
ecKey.getSymmetricKeyAlgorithm(), sessionInfoToEncrypt, RFC6637Utils.getXDHAlgorithm(pubKeyPacket),
137+
new KeyPairGeneratorOperation()
138+
{
139+
@Override
140+
public void initialize(KeyPairGenerator kpGen)
141+
throws GeneralSecurityException, IOException
142+
{
143+
kpGen.initialize(448, random);
144+
}
145+
},
146+
new EphPubEncoding()
147+
{
148+
@Override
149+
public byte[] getEphPubEncoding(byte[] publicKeyData)
150+
{
151+
return Arrays.prepend(publicKeyData, X_HDR);
152+
}
153+
});
124154
}
125155

126156
// Other ECDH curves
127157
else
128158
{
129159
return encryptSessionInfoWithECDHKey(pubKeyPacket, "EC", cryptoPublicKey, keyEncryptionOID,
130-
ecKey.getSymmetricKeyAlgorithm(), sessionInfoToEncrypt, RFC6637Utils.getAgreementAlgorithm(pubKeyPacket),
131-
(kpGen) ->
160+
ecKey.getSymmetricKeyAlgorithm(), sessionInfoToEncrypt, RFC6637Utils.getAgreementAlgorithm(pubKeyPacket),
161+
new KeyPairGeneratorOperation()
162+
{
163+
@Override
164+
public void initialize(KeyPairGenerator kpGen)
165+
throws GeneralSecurityException, IOException
132166
{
133167
AlgorithmParameters ecAlgParams = helper.createAlgorithmParameters("EC");
134168
ecAlgParams.init(new X962Parameters(ecKey.getCurveOID()).getEncoded());
135169
kpGen.initialize(ecAlgParams.getParameterSpec(AlgorithmParameterSpec.class), random);
136-
}, (ephPubEncoding) ->
170+
}
171+
}, new EphPubEncoding()
172+
{
173+
@Override
174+
public byte[] getEphPubEncoding(byte[] ephPubEncoding)
137175
{
138176
if (null == ephPubEncoding || ephPubEncoding.length < 1 || ephPubEncoding[0] != 0x04)
139177
{
140178
ephPubEncoding = JcaJcePGPUtil.getX9Parameters(ecKey.getCurveOID()).getCurve().decodePoint(ephPubEncoding).getEncoded(false);
141179
}
142180
return ephPubEncoding;
143-
});
181+
}
182+
});
144183
}
145184
}
146185

147186
// X25519
148187
else if (pubKey.getAlgorithm() == PublicKeyAlgorithmTags.X25519)
149188
{
150189
return encryptSessionInfoWithX25519X448Key(pubKey, "X25519", cryptoPublicKey, NISTObjectIdentifiers.id_aes128_wrap.getId(),
151-
SymmetricKeyAlgorithmTags.AES_128, fullSessionInfo, "X25519withSHA256HKDF", 255, optSymAlgId);
190+
SymmetricKeyAlgorithmTags.AES_128, fullSessionInfo, "X25519withSHA256HKDF", 255, optSymAlgId);
152191
}
153192

154193
// X448
155194
else if (pubKey.getAlgorithm() == PublicKeyAlgorithmTags.X448)
156195
{
157196
return encryptSessionInfoWithX25519X448Key(pubKey, "X448", cryptoPublicKey, NISTObjectIdentifiers.id_aes256_wrap.getId(),
158-
SymmetricKeyAlgorithmTags.AES_256, fullSessionInfo, "X448withSHA512HKDF", 448, optSymAlgId);
197+
SymmetricKeyAlgorithmTags.AES_256, fullSessionInfo, "X448withSHA512HKDF", 448, optSymAlgId);
159198
}
160199

161200
// RSA / ElGamal etc.
@@ -236,7 +275,7 @@ private byte[] encryptSessionInfoWithECDHKey(PublicKeyPacket pubKeyPacket, Strin
236275
*/
237276
private byte[] encryptSessionInfoWithX25519X448Key(PGPPublicKey pgpPublicKey, String algorithmName, PublicKey cryptoPublicKey, String keyEncryptionOID,
238277
int symmetricKeyAlgorithm, byte[] sessionInfo, String agreementAlgorithmName, int keySize,
239-
byte[] optSymAlgId)
278+
byte optSymAlgId)
240279
throws GeneralSecurityException, IOException, PGPException
241280
{
242281
KeyPairGenerator kpGen = helper.createKeyPairGenerator(algorithmName);

0 commit comments

Comments
 (0)