@@ -92,7 +92,8 @@ public JcePublicKeyKeyEncryptionMethodGenerator setSecureRandom(SecureRandom ran
9292
9393 protected byte [] encryptSessionInfo (PGPPublicKey pubKey ,
9494 byte [] sessionKey ,
95- byte optSymAlgId )
95+ byte optSymAlgId ,
96+ boolean isV3 )
9697 throws PGPException
9798 {
9899 try
@@ -102,7 +103,7 @@ protected byte[] encryptSessionInfo(PGPPublicKey pubKey,
102103 // ECDH
103104 if (pubKey .getAlgorithm () == PublicKeyAlgorithmTags .ECDH )
104105 {
105- byte [] sessionInfo = createSessionInfo (optSymAlgId , sessionKey );
106+ byte [] sessionInfo = createSessionInfo (isV3 ? optSymAlgId : ( byte ) 0 , sessionKey );
106107 ECDHPublicBCPGKey ecKey = (ECDHPublicBCPGKey )pubKey .getPublicKeyPacket ().getKey ();
107108 String keyEncryptionOID = RFC6637Utils .getKeyEncryptionOID (ecKey .getSymmetricKeyAlgorithm ()).getId ();
108109 PublicKeyPacket pubKeyPacket = pubKey .getPublicKeyPacket ();
@@ -111,7 +112,7 @@ protected byte[] encryptSessionInfo(PGPPublicKey pubKey,
111112 if (JcaJcePGPUtil .isX25519 (ecKey .getCurveOID ()))
112113 {
113114 return encryptSessionInfoWithECDHKey (pubKeyPacket , "X25519" , cryptoPublicKey , keyEncryptionOID ,
114- ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getXDHAlgorithm (pubKeyPacket ),
115+ ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getXDHAlgorithm (pubKeyPacket ), optSymAlgId ,
115116 new KeyPairGeneratorOperation ()
116117 {
117118 @ Override
@@ -135,7 +136,7 @@ public byte[] getEphPubEncoding(byte[] publicKeyData)
135136 else if (ecKey .getCurveOID ().equals (EdECObjectIdentifiers .id_X448 ))
136137 {
137138 return encryptSessionInfoWithECDHKey (pubKeyPacket , "X448" , cryptoPublicKey , keyEncryptionOID ,
138- ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getXDHAlgorithm (pubKeyPacket ),
139+ ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getXDHAlgorithm (pubKeyPacket ), optSymAlgId ,
139140 new KeyPairGeneratorOperation ()
140141 {
141142 @ Override
@@ -159,7 +160,7 @@ public byte[] getEphPubEncoding(byte[] publicKeyData)
159160 else
160161 {
161162 return encryptSessionInfoWithECDHKey (pubKeyPacket , "EC" , cryptoPublicKey , keyEncryptionOID ,
162- ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getAgreementAlgorithm (pubKeyPacket ),
163+ ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getAgreementAlgorithm (pubKeyPacket ), optSymAlgId ,
163164 new KeyPairGeneratorOperation ()
164165 {
165166 @ Override
@@ -189,14 +190,14 @@ public byte[] getEphPubEncoding(byte[] ephPubEncoding)
189190 else if (pubKey .getAlgorithm () == PublicKeyAlgorithmTags .X25519 )
190191 {
191192 return encryptSessionInfoWithX25519X448Key (pubKey , "X25519" , cryptoPublicKey , NISTObjectIdentifiers .id_aes128_wrap .getId (),
192- SymmetricKeyAlgorithmTags .AES_128 , sessionKey , "X25519withSHA256HKDF" , 255 , optSymAlgId );
193+ SymmetricKeyAlgorithmTags .AES_128 , sessionKey , "X25519withSHA256HKDF" , 255 , optSymAlgId , isV3 );
193194 }
194195
195196 // X448
196197 else if (pubKey .getAlgorithm () == PublicKeyAlgorithmTags .X448 )
197198 {
198199 return encryptSessionInfoWithX25519X448Key (pubKey , "X448" , cryptoPublicKey , NISTObjectIdentifiers .id_aes256_wrap .getId (),
199- SymmetricKeyAlgorithmTags .AES_256 , sessionKey , "X448withSHA512HKDF" , 448 , optSymAlgId );
200+ SymmetricKeyAlgorithmTags .AES_256 , sessionKey , "X448withSHA512HKDF" , 448 , optSymAlgId , isV3 );
200201 }
201202
202203 // RSA / ElGamal etc.
@@ -205,7 +206,7 @@ else if (pubKey.getAlgorithm() == PublicKeyAlgorithmTags.X448)
205206 Cipher c = helper .createPublicKeyCipher (pubKey .getAlgorithm ());
206207
207208 c .init (Cipher .ENCRYPT_MODE , cryptoPublicKey , random );
208- byte [] sessionInfo = createSessionInfo (optSymAlgId , sessionKey );
209+ byte [] sessionInfo = createSessionInfo (isV3 ? optSymAlgId : ( byte ) 0 , sessionKey );
209210 return c .doFinal (sessionInfo );
210211 }
211212 }
@@ -245,7 +246,7 @@ private interface EphPubEncoding
245246 }
246247
247248 private byte [] encryptSessionInfoWithECDHKey (PublicKeyPacket pubKeyPacket , String algorithmName , PublicKey cryptoPublicKey , String keyEncryptionOID ,
248- int symmetricKeyAlgorithm , byte [] sessionInfo , String agreementName , KeyPairGeneratorOperation kpOperation ,
249+ int symmetricKeyAlgorithm , byte [] sessionInfo , String agreementName , byte symAlgId , KeyPairGeneratorOperation kpOperation ,
249250 EphPubEncoding getEncoding )
250251 throws GeneralSecurityException , IOException , PGPException
251252 {
@@ -264,8 +265,8 @@ private byte[] encryptSessionInfoWithECDHKey(PublicKeyPacket pubKeyPacket, Strin
264265
265266 // wrap the padded session info using the shared-secret public key
266267 // https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
267- return getSessionInfo (new MPInteger (new BigInteger (1 , ephPubEncoding ))
268- . getEncoded () , getWrapper (symmetricKeyAlgorithm , sessionInfo [ 0 ] , secret , paddedSessionData ));
268+ return getSessionInfo (new MPInteger (new BigInteger (1 , ephPubEncoding )). getEncoded (),
269+ ( byte ) 0 , getWrapper (symmetricKeyAlgorithm , symAlgId , secret , paddedSessionData ));
269270 }
270271
271272 /**
@@ -279,7 +280,7 @@ private byte[] encryptSessionInfoWithECDHKey(PublicKeyPacket pubKeyPacket, Strin
279280 */
280281 private byte [] encryptSessionInfoWithX25519X448Key (PGPPublicKey pgpPublicKey , String algorithmName , PublicKey cryptoPublicKey , String keyEncryptionOID ,
281282 int symmetricKeyAlgorithm , byte [] sessionKey , String agreementAlgorithmName , int keySize ,
282- byte optSymAlgId )
283+ byte optSymAlgId , boolean isV3 )
283284 throws GeneralSecurityException , IOException , PGPException
284285 {
285286 KeyPairGenerator kpGen = helper .createKeyPairGenerator (algorithmName );
@@ -289,11 +290,7 @@ private byte[] encryptSessionInfoWithX25519X448Key(PGPPublicKey pgpPublicKey, St
289290 byte [] ephPubEncoding = SubjectPublicKeyInfo .getInstance (ephKP .getPublic ().getEncoded ()).getPublicKeyData ().getBytes ();
290291 HybridValueParameterSpec ukmSpec = JcaJcePGPUtil .getHybridValueParameterSpecWithPrepend (ephPubEncoding , pgpPublicKey .getPublicKeyPacket (), algorithmName );
291292 Key secret = JcaJcePGPUtil .getSecret (helper , cryptoPublicKey , keyEncryptionOID , agreementAlgorithmName , ukmSpec , ephKP .getPrivate ());
292- if (optSymAlgId == 0 )
293- {
294- return getSessionInfo (ephPubEncoding , getWrapper (symmetricKeyAlgorithm , optSymAlgId , secret , sessionKey ));
295- }
296- return getSessionInfo (ephPubEncoding , optSymAlgId , getWrapper (symmetricKeyAlgorithm , optSymAlgId , secret , sessionKey ));
293+ return getSessionInfo (ephPubEncoding , isV3 ? optSymAlgId : (byte )0 , getWrapper (symmetricKeyAlgorithm , optSymAlgId , secret , sessionKey ));
297294 }
298295
299296 private byte [] getWrapper (int symmetricKeyAlgorithm , byte optSymAlgId , Key secret , byte [] sessionData )
0 commit comments