Skip to content

Commit c8dfa3b

Browse files
author
gefeili
committed
Merge branch 'main' into pqc-snova
2 parents 59d91b9 + eb646df commit c8dfa3b

File tree

4 files changed

+287
-51
lines changed

4 files changed

+287
-51
lines changed

pkix/src/main/java/org/bouncycastle/cms/CMSSignedDataStreamGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ else if (tagged.getTagNo() == 3)
372372
return new ASN1Integer(1);
373373
}
374374

375-
private boolean checkForVersion3(List signerInfos, List signerInfoGens)
375+
private static boolean checkForVersion3(List signerInfos, List signerInfoGens)
376376
{
377377
for (Iterator it = signerInfos.iterator(); it.hasNext();)
378378
{
379-
SignerInfo s = SignerInfo.getInstance(((SignerInformation)it.next()).toASN1Structure());
379+
SignerInfo s = ((SignerInformation)it.next()).toASN1Structure();
380380

381-
if (s.getVersion().intValueExact() == 3)
381+
if (s.getVersion().hasValue(3))
382382
{
383383
return true;
384384
}

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ 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 mlDsa44Kpg;
65+
public static KeyPairGenerator mlDsa65Kpg;
66+
public static KeyPairGenerator mlDsa87Kpg;
67+
public static KeyPairGenerator mlKem512Kpg;
68+
public static KeyPairGenerator mlKem768Kpg;
69+
public static KeyPairGenerator mlKem1024Kpg;
6570
public static KeyPairGenerator ntruKpg;
6671
public static KeyGenerator aes192kg;
6772
public static KeyGenerator desede128kg;
@@ -168,7 +173,14 @@ public class CMSTestUtil
168173
ed448Kpg = KeyPairGenerator.getInstance("Ed448", "BC");
169174

170175
ntruKpg = KeyPairGenerator.getInstance(BCObjectIdentifiers.ntruhps2048509.getId(), "BC");
171-
mlKemKpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
176+
177+
mlDsa44Kpg = KeyPairGenerator.getInstance("ML-DSA-44", "BC");
178+
mlDsa65Kpg = KeyPairGenerator.getInstance("ML-DSA-65", "BC");
179+
mlDsa87Kpg = KeyPairGenerator.getInstance("ML-DSA-87", "BC");
180+
181+
mlKem512Kpg = KeyPairGenerator.getInstance("ML-KEM-512", "BC");
182+
mlKem768Kpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
183+
mlKem1024Kpg = KeyPairGenerator.getInstance("ML-KEM-1024", "BC");
172184

173185
aes192kg = KeyGenerator.getInstance("AES", "BC");
174186
aes192kg.init(192, rand);
@@ -281,9 +293,34 @@ public static KeyPair makeNtruKeyPair()
281293
return ntruKpg.generateKeyPair();
282294
}
283295

284-
public static KeyPair makeMLKemKeyPair()
296+
public static KeyPair makeMLKem512KeyPair()
297+
{
298+
return mlKem512Kpg.generateKeyPair();
299+
}
300+
301+
public static KeyPair makeMLKem768KeyPair()
302+
{
303+
return mlKem768Kpg.generateKeyPair();
304+
}
305+
306+
public static KeyPair makeMLKem1024KeyPair()
307+
{
308+
return mlKem1024Kpg.generateKeyPair();
309+
}
310+
311+
public static KeyPair makeMLDsa44KeyPair()
285312
{
286-
return mlKemKpg.generateKeyPair();
313+
return mlDsa44Kpg.generateKeyPair();
314+
}
315+
316+
public static KeyPair makeMLDsa65KeyPair()
317+
{
318+
return mlDsa65Kpg.generateKeyPair();
319+
}
320+
321+
public static KeyPair makeMLDsa87KeyPair()
322+
{
323+
return mlDsa87Kpg.generateKeyPair();
287324
}
288325

289326
public static SecretKey makeDesede128Key()
@@ -504,6 +541,10 @@ public static X509Certificate makeOaepCertificate(KeyPair subKP, String _subDN,
504541

505542
private static JcaContentSignerBuilder makeContentSignerBuilder(PublicKey issPub)
506543
{
544+
/*
545+
* NOTE: Current ALL test certificates are issued under a SHA1withRSA root, so this list is mostly
546+
* redundant (and also incomplete in that it doesn't handle EdDSA or ML-DSA issuers).
547+
*/
507548
JcaContentSignerBuilder contentSignerBuilder;
508549
if (issPub instanceof RSAPublicKey)
509550
{
@@ -521,10 +562,14 @@ else if (issPub.getAlgorithm().equals("ECGOST3410"))
521562
{
522563
contentSignerBuilder = new JcaContentSignerBuilder("GOST3411withECGOST3410");
523564
}
524-
else
565+
else if (issPub.getAlgorithm().equals("GOST3410"))
525566
{
526567
contentSignerBuilder = new JcaContentSignerBuilder("GOST3411WithGOST3410");
527568
}
569+
else
570+
{
571+
throw new UnsupportedOperationException("Algorithm handlers incomplete");
572+
}
528573

529574
contentSignerBuilder.setProvider(BouncyCastleProvider.PROVIDER_NAME);
530575

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)