@@ -143,8 +143,12 @@ public class NewEnvelopedDataTest
143143 private static X509Certificate _reciKemsCert ;
144144 private static KeyPair _reciNtruKP ;
145145 private static X509Certificate _reciNtruCert ;
146- private static KeyPair _reciMLKemKP ;
147- private static X509Certificate _reciMLKemCert ;
146+ private static KeyPair _reciMLKem512KP ;
147+ private static X509Certificate _reciMLKem512Cert ;
148+ private static KeyPair _reciMLKem768KP ;
149+ private static X509Certificate _reciMLKem768Cert ;
150+ private static KeyPair _reciMLKem1024KP ;
151+ private static X509Certificate _reciMLKem1024Cert ;
148152
149153 private static KeyPair _origDhKP ;
150154 private static KeyPair _reciDhKP ;
@@ -609,8 +613,14 @@ private static void init()
609613 _reciNtruKP = CMSTestUtil .makeNtruKeyPair ();
610614 _reciNtruCert = CMSTestUtil .makeCertificate (_reciNtruKP , _reciDN , _signKP , _signDN );
611615
612- _reciMLKemKP = CMSTestUtil .makeMLKemKeyPair ();
613- _reciMLKemCert = CMSTestUtil .makeCertificate (_reciMLKemKP , _reciDN , _signKP , _signDN );
616+ _reciMLKem512KP = CMSTestUtil .makeMLKem512KeyPair ();
617+ _reciMLKem512Cert = CMSTestUtil .makeCertificate (_reciMLKem512KP , _reciDN , _signKP , _signDN );
618+
619+ _reciMLKem768KP = CMSTestUtil .makeMLKem768KeyPair ();
620+ _reciMLKem768Cert = CMSTestUtil .makeCertificate (_reciMLKem768KP , _reciDN , _signKP , _signDN );
621+
622+ _reciMLKem1024KP = CMSTestUtil .makeMLKem1024KeyPair ();
623+ _reciMLKem1024Cert = CMSTestUtil .makeCertificate (_reciMLKem1024KP , _reciDN , _signKP , _signDN );
614624 }
615625 }
616626
@@ -716,7 +726,7 @@ public void testContentType()
716726 }
717727 }
718728
719- public void testMLKem ()
729+ public void testMLKem512 ()
720730 throws Exception
721731 {
722732 byte [] data = "WallaWallaWashington" .getBytes ();
@@ -725,8 +735,8 @@ public void testMLKem()
725735 CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator ();
726736
727737 // note: use cert req ID as key ID, don't want to use issuer/serial in this case!
728- edGen .addRecipientInfoGenerator (new JceKEMRecipientInfoGenerator (_reciMLKemCert , CMSAlgorithm .AES256_WRAP ). setKDF (
729- new AlgorithmIdentifier (NISTObjectIdentifiers . id_shake256 )));
738+ edGen .addRecipientInfoGenerator (new JceKEMRecipientInfoGenerator (_reciMLKem512Cert , CMSAlgorithm .AES128_WRAP )
739+ . setKDF ( new AlgorithmIdentifier (PKCSObjectIdentifiers . id_alg_hkdf_with_sha256 )));
730740
731741 CMSEnvelopedData ed = edGen .generate (
732742 new CMSProcessableByteArray (data ),
@@ -743,17 +753,108 @@ public void testMLKem()
743753 Iterator it = c .iterator ();
744754
745755 int expectedLength = new DefaultKemEncapsulationLengthProvider ().getEncapsulationLength (
746- SubjectPublicKeyInfo .getInstance (_reciMLKemKP .getPublic ().getEncoded ()).getAlgorithm ());
756+ SubjectPublicKeyInfo .getInstance (_reciMLKem512KP .getPublic ().getEncoded ()).getAlgorithm ());
757+
758+ while (it .hasNext ())
759+ {
760+ KEMRecipientInformation recipient = (KEMRecipientInformation )it .next ();
761+
762+ assertEquals (expectedLength , recipient .getEncapsulation ().length );
763+
764+ assertEquals (NISTObjectIdentifiers .id_alg_ml_kem_512 .getId (), recipient .getKeyEncryptionAlgOID ());
765+
766+ CMSTypedStream contentStream = recipient .getContentStream (
767+ new JceKEMEnvelopedRecipient (_reciMLKem512KP .getPrivate ()).setProvider (BC ));
768+
769+ assertEquals (PKCSObjectIdentifiers .data , contentStream .getContentType ());
770+ assertEquals (true , Arrays .equals (data , Streams .readAll (contentStream .getContentStream ())));
771+ }
772+ }
773+
774+ public void testMLKem768 ()
775+ throws Exception
776+ {
777+ byte [] data = "WallaWallaWashington" .getBytes ();
778+
779+ // Send response with encrypted certificate
780+ CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator ();
781+
782+ // note: use cert req ID as key ID, don't want to use issuer/serial in this case!
783+ edGen .addRecipientInfoGenerator (new JceKEMRecipientInfoGenerator (_reciMLKem768Cert , CMSAlgorithm .AES256_WRAP )
784+ .setKDF (new AlgorithmIdentifier (PKCSObjectIdentifiers .id_alg_hkdf_with_sha256 )));
785+
786+ CMSEnvelopedData ed = edGen .generate (
787+ new CMSProcessableByteArray (data ),
788+ new JceCMSContentEncryptorBuilder (CMSAlgorithm .AES256_CBC ).setProvider ("BC" ).build ());
789+
790+ RecipientInformationStore recipients = ed .getRecipientInfos ();
791+
792+ assertEquals (ed .getEncryptionAlgOID (), CMSEnvelopedDataGenerator .AES256_CBC );
793+
794+ Collection c = recipients .getRecipients ();
795+
796+ assertEquals (1 , c .size ());
797+
798+ Iterator it = c .iterator ();
799+
800+ int expectedLength = new DefaultKemEncapsulationLengthProvider ().getEncapsulationLength (
801+ SubjectPublicKeyInfo .getInstance (_reciMLKem768KP .getPublic ().getEncoded ()).getAlgorithm ());
747802
748803 while (it .hasNext ())
749804 {
750805 KEMRecipientInformation recipient = (KEMRecipientInformation )it .next ();
751806
752807 assertEquals (expectedLength , recipient .getEncapsulation ().length );
753-
808+
754809 assertEquals (NISTObjectIdentifiers .id_alg_ml_kem_768 .getId (), recipient .getKeyEncryptionAlgOID ());
755810
756- CMSTypedStream contentStream = recipient .getContentStream (new JceKEMEnvelopedRecipient (_reciMLKemKP .getPrivate ()).setProvider (BC ));
811+ CMSTypedStream contentStream = recipient .getContentStream (
812+ new JceKEMEnvelopedRecipient (_reciMLKem768KP .getPrivate ()).setProvider (BC ));
813+
814+ assertEquals (PKCSObjectIdentifiers .data , contentStream .getContentType ());
815+ assertEquals (true , Arrays .equals (data , Streams .readAll (contentStream .getContentStream ())));
816+ }
817+ }
818+
819+ public void testMLKem1024 ()
820+ throws Exception
821+ {
822+ byte [] data = "WallaWallaWashington" .getBytes ();
823+
824+ // Send response with encrypted certificate
825+ CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator ();
826+
827+ // note: use cert req ID as key ID, don't want to use issuer/serial in this case!
828+ edGen .addRecipientInfoGenerator (new JceKEMRecipientInfoGenerator (_reciMLKem1024Cert , CMSAlgorithm .AES256_WRAP )
829+ .setKDF (new AlgorithmIdentifier (PKCSObjectIdentifiers .id_alg_hkdf_with_sha256 )));
830+
831+ CMSEnvelopedData ed = edGen .generate (
832+ new CMSProcessableByteArray (data ),
833+ new JceCMSContentEncryptorBuilder (CMSAlgorithm .AES256_CBC ).setProvider ("BC" ).build ());
834+
835+ RecipientInformationStore recipients = ed .getRecipientInfos ();
836+
837+ assertEquals (ed .getEncryptionAlgOID (), CMSEnvelopedDataGenerator .AES256_CBC );
838+
839+ Collection c = recipients .getRecipients ();
840+
841+ assertEquals (1 , c .size ());
842+
843+ Iterator it = c .iterator ();
844+
845+ int expectedLength = new DefaultKemEncapsulationLengthProvider ().getEncapsulationLength (
846+ SubjectPublicKeyInfo .getInstance (_reciMLKem1024KP .getPublic ().getEncoded ()).getAlgorithm ());
847+
848+ while (it .hasNext ())
849+ {
850+ KEMRecipientInformation recipient = (KEMRecipientInformation )it .next ();
851+
852+ assertEquals (expectedLength , recipient .getEncapsulation ().length );
853+
854+ assertEquals (NISTObjectIdentifiers .id_alg_ml_kem_1024 .getId (), recipient .getKeyEncryptionAlgOID ());
855+
856+ CMSTypedStream contentStream = recipient .getContentStream (
857+ new JceKEMEnvelopedRecipient (_reciMLKem1024KP .getPrivate ()).setProvider (BC ));
757858
758859 assertEquals (PKCSObjectIdentifiers .data , contentStream .getContentType ());
759860 assertEquals (true , Arrays .equals (data , Streams .readAll (contentStream .getContentStream ())));
0 commit comments