@@ -111,17 +111,8 @@ protected byte[] encryptSessionInfo(PGPPublicKey pubKey,
111111 // Legacy X25519
112112 if (JcaJcePGPUtil .isX25519 (ecKey .getCurveOID ()))
113113 {
114- return encryptSessionInfoWithECDHKey (pubKeyPacket , "X25519" , cryptoPublicKey , keyEncryptionOID ,
114+ return encryptSessionInfoWithECDHKey (getKeyPair ( "X25519" , 255 ), pubKeyPacket , cryptoPublicKey , keyEncryptionOID ,
115115 ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getXDHAlgorithm (pubKeyPacket ), optSymAlgId ,
116- new KeyPairGeneratorOperation ()
117- {
118- @ Override
119- public void initialize (KeyPairGenerator kpGen )
120- throws GeneralSecurityException , IOException
121- {
122- kpGen .initialize (255 , random );
123- }
124- },
125116 new EphPubEncoding ()
126117 {
127118 @ Override
@@ -135,17 +126,8 @@ public byte[] getEphPubEncoding(byte[] publicKeyData)
135126 // Legacy X448
136127 else if (ecKey .getCurveOID ().equals (EdECObjectIdentifiers .id_X448 ))
137128 {
138- return encryptSessionInfoWithECDHKey (pubKeyPacket , "X448" , cryptoPublicKey , keyEncryptionOID ,
129+ return encryptSessionInfoWithECDHKey (getKeyPair ( "X448" , 448 ), pubKeyPacket , cryptoPublicKey , keyEncryptionOID ,
139130 ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getXDHAlgorithm (pubKeyPacket ), optSymAlgId ,
140- new KeyPairGeneratorOperation ()
141- {
142- @ Override
143- public void initialize (KeyPairGenerator kpGen )
144- throws GeneralSecurityException , IOException
145- {
146- kpGen .initialize (448 , random );
147- }
148- },
149131 new EphPubEncoding ()
150132 {
151133 @ Override
@@ -159,19 +141,14 @@ public byte[] getEphPubEncoding(byte[] publicKeyData)
159141 // Other ECDH curves
160142 else
161143 {
162- return encryptSessionInfoWithECDHKey (pubKeyPacket , "EC" , cryptoPublicKey , keyEncryptionOID ,
144+ KeyPairGenerator kpGen = helper .createKeyPairGenerator ("EC" );
145+ AlgorithmParameters ecAlgParams = helper .createAlgorithmParameters ("EC" );
146+ ecAlgParams .init (new X962Parameters (ecKey .getCurveOID ()).getEncoded ());
147+ kpGen .initialize (ecAlgParams .getParameterSpec (AlgorithmParameterSpec .class ), random );
148+ KeyPair ephKP = kpGen .generateKeyPair ();
149+ return encryptSessionInfoWithECDHKey (ephKP , pubKeyPacket , cryptoPublicKey , keyEncryptionOID ,
163150 ecKey .getSymmetricKeyAlgorithm (), sessionInfo , RFC6637Utils .getAgreementAlgorithm (pubKeyPacket ), optSymAlgId ,
164- new KeyPairGeneratorOperation ()
165- {
166- @ Override
167- public void initialize (KeyPairGenerator kpGen )
168- throws GeneralSecurityException , IOException
169- {
170- AlgorithmParameters ecAlgParams = helper .createAlgorithmParameters ("EC" );
171- ecAlgParams .init (new X962Parameters (ecKey .getCurveOID ()).getEncoded ());
172- kpGen .initialize (ecAlgParams .getParameterSpec (AlgorithmParameterSpec .class ), random );
173- }
174- }, new EphPubEncoding ()
151+ new EphPubEncoding ()
175152 {
176153 @ Override
177154 public byte [] getEphPubEncoding (byte [] ephPubEncoding )
@@ -232,28 +209,18 @@ else if (pubKey.getAlgorithm() == PublicKeyAlgorithmTags.X448)
232209 }
233210 }
234211
235- @ FunctionalInterface
236- private interface KeyPairGeneratorOperation
237- {
238- void initialize (KeyPairGenerator kpGen )
239- throws GeneralSecurityException , IOException ;
240- }
241-
242212 @ FunctionalInterface
243213 private interface EphPubEncoding
244214 {
245215 byte [] getEphPubEncoding (byte [] publicKeyData );
246216 }
247217
248- private byte [] encryptSessionInfoWithECDHKey (PublicKeyPacket pubKeyPacket , String algorithmName , PublicKey cryptoPublicKey , String keyEncryptionOID ,
249- int symmetricKeyAlgorithm , byte [] sessionInfo , String agreementName , byte symAlgId , KeyPairGeneratorOperation kpOperation ,
218+ private byte [] encryptSessionInfoWithECDHKey (KeyPair ephKP , PublicKeyPacket pubKeyPacket , PublicKey cryptoPublicKey , String keyEncryptionOID ,
219+ int symmetricKeyAlgorithm , byte [] sessionInfo , String agreementName , byte symAlgId ,
250220 EphPubEncoding getEncoding )
251221 throws GeneralSecurityException , IOException , PGPException
252222 {
253223 // Prepare shared-secret public key
254- KeyPairGenerator kpGen = helper .createKeyPairGenerator (algorithmName );
255- kpOperation .initialize (kpGen );
256- KeyPair ephKP = kpGen .generateKeyPair ();
257224 UserKeyingMaterialSpec ukmSpec = new UserKeyingMaterialSpec (RFC6637Utils .createUserKeyingMaterial (pubKeyPacket ,
258225 new JcaKeyFingerprintCalculator ()));
259226 Key secret = JcaJcePGPUtil .getSecret (helper , cryptoPublicKey , keyEncryptionOID , agreementName , ukmSpec , ephKP .getPrivate ());
@@ -283,16 +250,21 @@ private byte[] encryptSessionInfoWithX25519X448Key(PGPPublicKey pgpPublicKey, St
283250 byte optSymAlgId , boolean isV3 )
284251 throws GeneralSecurityException , IOException , PGPException
285252 {
286- KeyPairGenerator kpGen = helper .createKeyPairGenerator (algorithmName );
287- kpGen .initialize (keySize , random );
288- KeyPair ephKP = kpGen .generateKeyPair ();
289-
253+ KeyPair ephKP = getKeyPair (algorithmName , keySize );
290254 byte [] ephPubEncoding = SubjectPublicKeyInfo .getInstance (ephKP .getPublic ().getEncoded ()).getPublicKeyData ().getBytes ();
291255 HybridValueParameterSpec ukmSpec = JcaJcePGPUtil .getHybridValueParameterSpecWithPrepend (ephPubEncoding , pgpPublicKey .getPublicKeyPacket (), algorithmName );
292256 Key secret = JcaJcePGPUtil .getSecret (helper , cryptoPublicKey , keyEncryptionOID , agreementAlgorithmName , ukmSpec , ephKP .getPrivate ());
293257 return getSessionInfo (ephPubEncoding , isV3 ? optSymAlgId : (byte )0 , getWrapper (symmetricKeyAlgorithm , optSymAlgId , secret , sessionKey ));
294258 }
295259
260+ private KeyPair getKeyPair (String algorithmName , int keySize )
261+ throws GeneralSecurityException
262+ {
263+ KeyPairGenerator kpGen = helper .createKeyPairGenerator (algorithmName );
264+ kpGen .initialize (keySize , random );
265+ return kpGen .generateKeyPair ();
266+ }
267+
296268 private byte [] getWrapper (int symmetricKeyAlgorithm , byte optSymAlgId , Key secret , byte [] sessionData )
297269 throws PGPException , InvalidKeyException , IllegalBlockSizeException
298270 {
0 commit comments