Skip to content

Commit cb3a8b0

Browse files
author
gefeili
committed
Remove PublicKeyKeyEncryptionMethodGenerator.concatECDHEphKeyWithWrappedSessionKey method
1 parent 118b9ef commit cb3a8b0

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,41 +221,21 @@ protected abstract byte[] encryptSessionInfo(PGPPublicKey pubKey,
221221
byte optSymAlgId)
222222
throws PGPException;
223223

224-
225-
protected static byte[] concatECDHEphKeyWithWrappedSessionKey(byte[] ephPubEncoding, byte[] wrappedSessionKey)
226-
throws IOException
227-
{
228-
// https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
229-
230-
byte[] mpiEncodedEphemeralKey = new MPInteger(new BigInteger(1, ephPubEncoding))
231-
.getEncoded();
232-
byte[] out = new byte[mpiEncodedEphemeralKey.length + 1 + wrappedSessionKey.length];
233-
// eph key
234-
System.arraycopy(mpiEncodedEphemeralKey, 0, out, 0, mpiEncodedEphemeralKey.length);
235-
// enc session-key len
236-
out[mpiEncodedEphemeralKey.length] = (byte)wrappedSessionKey.length;
237-
// enc session-key
238-
System.arraycopy(wrappedSessionKey, 0, out, mpiEncodedEphemeralKey.length + 1, wrappedSessionKey.length);
239-
240-
return out;
241-
}
242-
243224
protected static byte[] getSessionInfo(byte[] ephPubEncoding, byte optSymKeyAlgorithm, byte[] wrappedSessionKey)
244225
{
245-
int len = ephPubEncoding.length + wrappedSessionKey.length + (optSymKeyAlgorithm != 0 ? 2 : 1);
226+
int len = ephPubEncoding.length + wrappedSessionKey.length + (optSymKeyAlgorithm != 0 ? 2 : 1);
246227
byte[] out = new byte[len];
247228
// ephemeral pub key
248229
System.arraycopy(ephPubEncoding, 0, out, 0, ephPubEncoding.length);
249230
// len of two/one next fields
250-
out[ephPubEncoding.length] = (byte)(wrappedSessionKey.length + 1);
231+
out[ephPubEncoding.length] = (byte)(wrappedSessionKey.length + (optSymKeyAlgorithm != 0 ? 1 : 0));
251232
// (optional) sym key alg
252233
if (optSymKeyAlgorithm != 0)
253234
{
254235
out[ephPubEncoding.length + 1] = optSymKeyAlgorithm;
255236
}
256237
// wrapped session key
257238
System.arraycopy(wrappedSessionKey, 0, out, len - wrappedSessionKey.length, wrappedSessionKey.length);
258-
259239
return out;
260240
}
261241
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
88
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
99
import org.bouncycastle.bcpg.HashAlgorithmTags;
10+
import org.bouncycastle.bcpg.MPInteger;
1011
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
1112
import org.bouncycastle.bcpg.PublicKeyPacket;
1213
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
@@ -217,7 +218,9 @@ private byte[] encryptSessionInfoWithECDHKey(byte[] sessionInfo,
217218
byte[] paddedSessionData = PGPPad.padSessionData(sessionInfo, sessionKeyObfuscation);
218219

219220
// wrap the padded session info using the shared-secret public key
220-
return concatECDHEphKeyWithWrappedSessionKey(ephPubEncoding, getWrapper(symmetricKeyAlgorithm, key, paddedSessionData));
221+
// https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
222+
return getSessionInfo(new MPInteger(new BigInteger(1, ephPubEncoding))
223+
.getEncoded(), (byte)0, getWrapper(symmetricKeyAlgorithm, key, paddedSessionData));
221224
}
222225

223226
private byte[] encryptSessionInfoWithX25519X448Key(PublicKeyPacket pubKeyPacket,

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.openpgp.operator.jcajce;
22

33
import java.io.IOException;
4+
import java.math.BigInteger;
45
import java.security.AlgorithmParameters;
56
import java.security.GeneralSecurityException;
67
import java.security.InvalidKeyException;
@@ -22,6 +23,7 @@
2223
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
2324
import org.bouncycastle.asn1.x9.X962Parameters;
2425
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
26+
import org.bouncycastle.bcpg.MPInteger;
2527
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
2628
import org.bouncycastle.bcpg.PublicKeyPacket;
2729
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
@@ -261,7 +263,9 @@ private byte[] encryptSessionInfoWithECDHKey(PublicKeyPacket pubKeyPacket, Strin
261263
byte[] paddedSessionData = PGPPad.padSessionData(sessionInfo, sessionKeyObfuscation);
262264

263265
// wrap the padded session info using the shared-secret public key
264-
return concatECDHEphKeyWithWrappedSessionKey(ephPubEncoding, getWrapper(symmetricKeyAlgorithm, sessionInfo, secret, paddedSessionData));
266+
// https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
267+
return getSessionInfo(new MPInteger(new BigInteger(1, ephPubEncoding))
268+
.getEncoded(), (byte)0, getWrapper(symmetricKeyAlgorithm, sessionInfo, secret, paddedSessionData));
265269
}
266270

267271
/**

0 commit comments

Comments
 (0)