11package org .bouncycastle .jcajce .provider .asymmetric .frodokem ;
22
33import java .security .SecureRandom ;
4- import java .util .Objects ;
54
65import javax .crypto .KEM ;
76import javax .crypto .KEMSpi ;
8- import javax .crypto .SecretKey ;
9- import javax .security .auth .DestroyFailedException ;
107
11- import org .bouncycastle .crypto .SecretWithEncapsulation ;
128import org .bouncycastle .crypto .kems .FrodoKEMGenerator ;
13- import org .bouncycastle .jcajce .provider .asymmetric .util .KdfUtil ;
149import org .bouncycastle .jcajce .spec .KTSParameterSpec ;
10+ import org .bouncycastle .jcajce .util .SpiUtil ;
1511
1612/*
1713 * NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -37,35 +33,7 @@ class FrodoKEMEncapsulatorSpi
3733 @ Override
3834 public KEM .Encapsulated engineEncapsulate (int from , int to , String algorithm )
3935 {
40- Objects .checkFromToIndex (from , to , engineSecretSize ());
41- Objects .requireNonNull (algorithm , "null algorithm" );
42-
43- algorithm = KdfUtil .resolveAlgorithm (parameterSpec , algorithm );
44-
45- SecretWithEncapsulation secEnc = kemGen .generateEncapsulated (publicKey .getKeyParams ());
46-
47- try
48- {
49- // getEncapsulation()/getSecret() hand back clones, so the originals have to be
50- // destroyed as well - KdfUtil.makeSecretKey only clears the secret clone it is passed.
51- byte [] encapsulation = secEnc .getEncapsulation ();
52-
53- SecretKey secretKey = KdfUtil .makeSecretKey (parameterSpec , secEnc .getSecret (),
54- from , to , algorithm );
55-
56- return new KEM .Encapsulated (secretKey , encapsulation , null );
57- }
58- finally
59- {
60- try
61- {
62- secEnc .destroy ();
63- }
64- catch (DestroyFailedException e )
65- {
66- // ignore
67- }
68- }
36+ return SpiUtil .buildEncapsulated (from , to , algorithm , engineSecretSize (), kemGen , publicKey .getKeyParams (), parameterSpec );
6937 }
7038
7139 @ Override
0 commit comments