Skip to content

Commit 9afb944

Browse files
committed
CMS: Expand ML-KEM tests
- adjust KDF, wrap, encryption algorithms
1 parent ed2d439 commit 9afb944

File tree

2 files changed

+129
-14
lines changed

2 files changed

+129
-14
lines changed

pkix/src/test/java/org/bouncycastle/cms/test/CMSTestUtil.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public class CMSTestUtil
6161
public static KeyPairGenerator ecDsaKpg;
6262
public static KeyPairGenerator ed25519Kpg;
6363
public static KeyPairGenerator ed448Kpg;
64-
public static KeyPairGenerator mlKemKpg;
64+
public static KeyPairGenerator mlKem512Kpg;
65+
public static KeyPairGenerator mlKem768Kpg;
66+
public static KeyPairGenerator mlKem1024Kpg;
6567
public static KeyPairGenerator ntruKpg;
6668
public static KeyGenerator aes192kg;
6769
public static KeyGenerator desede128kg;
@@ -168,7 +170,9 @@ public class CMSTestUtil
168170
ed448Kpg = KeyPairGenerator.getInstance("Ed448", "BC");
169171

170172
ntruKpg = KeyPairGenerator.getInstance(BCObjectIdentifiers.ntruhps2048509.getId(), "BC");
171-
mlKemKpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
173+
mlKem512Kpg = KeyPairGenerator.getInstance("ML-KEM-512", "BC");
174+
mlKem768Kpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
175+
mlKem1024Kpg = KeyPairGenerator.getInstance("ML-KEM-1024", "BC");
172176

173177
aes192kg = KeyGenerator.getInstance("AES", "BC");
174178
aes192kg.init(192, rand);
@@ -281,9 +285,19 @@ public static KeyPair makeNtruKeyPair()
281285
return ntruKpg.generateKeyPair();
282286
}
283287

284-
public static KeyPair makeMLKemKeyPair()
288+
public static KeyPair makeMLKem512KeyPair()
285289
{
286-
return mlKemKpg.generateKeyPair();
290+
return mlKem512Kpg.generateKeyPair();
291+
}
292+
293+
public static KeyPair makeMLKem768KeyPair()
294+
{
295+
return mlKem768Kpg.generateKeyPair();
296+
}
297+
298+
public static KeyPair makeMLKem1024KeyPair()
299+
{
300+
return mlKem1024Kpg.generateKeyPair();
287301
}
288302

289303
public static SecretKey makeDesede128Key()

pkix/src/test/java/org/bouncycastle/cms/test/NewEnvelopedDataTest.java

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)