Skip to content

Commit bb265da

Browse files
committed
CMS: Prepare ML-DSA tests (not working yet)
1 parent 9afb944 commit bb265da

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class CMSTestUtil
6161
public static KeyPairGenerator ecDsaKpg;
6262
public static KeyPairGenerator ed25519Kpg;
6363
public static KeyPairGenerator ed448Kpg;
64+
public static KeyPairGenerator mlDsa44Kpg;
65+
public static KeyPairGenerator mlDsa65Kpg;
66+
public static KeyPairGenerator mlDsa87Kpg;
6467
public static KeyPairGenerator mlKem512Kpg;
6568
public static KeyPairGenerator mlKem768Kpg;
6669
public static KeyPairGenerator mlKem1024Kpg;
@@ -170,6 +173,11 @@ public class CMSTestUtil
170173
ed448Kpg = KeyPairGenerator.getInstance("Ed448", "BC");
171174

172175
ntruKpg = KeyPairGenerator.getInstance(BCObjectIdentifiers.ntruhps2048509.getId(), "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+
173181
mlKem512Kpg = KeyPairGenerator.getInstance("ML-KEM-512", "BC");
174182
mlKem768Kpg = KeyPairGenerator.getInstance("ML-KEM-768", "BC");
175183
mlKem1024Kpg = KeyPairGenerator.getInstance("ML-KEM-1024", "BC");
@@ -300,6 +308,21 @@ public static KeyPair makeMLKem1024KeyPair()
300308
return mlKem1024Kpg.generateKeyPair();
301309
}
302310

311+
public static KeyPair makeMLDsa44KeyPair()
312+
{
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();
324+
}
325+
303326
public static SecretKey makeDesede128Key()
304327
{
305328
return desede128kg.generateKey();

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

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ public class NewSignedDataTest
144144
private static KeyPair _signEd448KP;
145145
private static X509Certificate _signEd448Cert;
146146

147+
private static KeyPair _signMLDsa44KP;
148+
private static X509Certificate _signMLDsa44Cert;
149+
private static KeyPair _signMLDsa65KP;
150+
private static X509Certificate _signMLDsa65Cert;
151+
private static KeyPair _signMLDsa87KP;
152+
private static X509Certificate _signMLDsa87Cert;
153+
147154
private static String _reciDN;
148155
private static KeyPair _reciKP;
149156
private static X509Certificate _reciCert;
@@ -704,6 +711,9 @@ public class NewSignedDataTest
704711
noParams.add(NISTObjectIdentifiers.id_ecdsa_with_sha3_512);
705712
noParams.add(EdECObjectIdentifiers.id_Ed25519);
706713
noParams.add(EdECObjectIdentifiers.id_Ed448);
714+
noParams.add(NISTObjectIdentifiers.id_ml_dsa_44);
715+
noParams.add(NISTObjectIdentifiers.id_ml_dsa_65);
716+
noParams.add(NISTObjectIdentifiers.id_ml_dsa_87);
707717
}
708718

709719
public NewSignedDataTest(String name)
@@ -776,6 +786,15 @@ private static void init()
776786
_signEd448KP = CMSTestUtil.makeEd448KeyPair();
777787
_signEd448Cert = CMSTestUtil.makeCertificate(_signEd448KP, _signDN, _origKP, _origDN);
778788

789+
_signMLDsa44KP = CMSTestUtil.makeMLDsa44KeyPair();
790+
_signMLDsa44Cert = CMSTestUtil.makeCertificate(_signMLDsa44KP, _signDN, _origKP, _origDN);
791+
792+
_signMLDsa65KP = CMSTestUtil.makeMLDsa65KeyPair();
793+
_signMLDsa65Cert = CMSTestUtil.makeCertificate(_signMLDsa65KP, _signDN, _origKP, _origDN);
794+
795+
_signMLDsa87KP = CMSTestUtil.makeMLDsa87KeyPair();
796+
_signMLDsa87Cert = CMSTestUtil.makeCertificate(_signMLDsa87KP, _signDN, _origKP, _origDN);
797+
779798
_reciDN = "CN=Doug, OU=Sales, O=Bouncy Castle, C=AU";
780799
_reciKP = CMSTestUtil.makeKeyPair();
781800
_reciCert = CMSTestUtil.makeCertificate(_reciKP, _reciDN, _signKP, _signDN);
@@ -1789,13 +1808,32 @@ public void testSHA512_256ithRSADigest()
17891808
public void testEd25519()
17901809
throws Exception
17911810
{
1792-
encapsulatedTest(_signEd25519KP, _signEd25519Cert, "Ed25519", EdECObjectIdentifiers.id_Ed25519, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512));
1811+
/*
1812+
* RFC 8419 3.1. When signing with Ed25519, the digestAlgorithm MUST be id-sha512, and the algorithm
1813+
* parameters field MUST be absent.
1814+
*
1815+
* We confirm here that our implementation defaults to SHA-512 for the digest algorithm.
1816+
*/
1817+
AlgorithmIdentifier expectedDigAlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512);
1818+
1819+
encapsulatedTest(_signEd25519KP, _signEd25519Cert, "Ed25519", EdECObjectIdentifiers.id_Ed25519,
1820+
expectedDigAlgId);
17931821
}
17941822

17951823
public void testEd448()
17961824
throws Exception
17971825
{
1798-
encapsulatedTest(_signEd448KP, _signEd448Cert, "Ed448", EdECObjectIdentifiers.id_Ed448, new AlgorithmIdentifier(NISTObjectIdentifiers.id_shake256_len, new ASN1Integer(512)));
1826+
/*
1827+
* RFC 8419 3.1. When signing with Ed448, the digestAlgorithm MUST be id-shake256-len, the algorithm
1828+
* parameters field MUST be present, and the parameter MUST contain 512, encoded as a positive integer
1829+
* value.
1830+
*
1831+
* We confirm here that our implementation defaults to id-shake256-len/512 for the digest algorithm.
1832+
*/
1833+
AlgorithmIdentifier expectedDigAlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_shake256_len,
1834+
new ASN1Integer(512));
1835+
1836+
encapsulatedTest(_signEd448KP, _signEd448Cert, "Ed448", EdECObjectIdentifiers.id_Ed448, expectedDigAlgId);
17991837
}
18001838

18011839
public void testDetachedEd25519()
@@ -2270,6 +2308,57 @@ public SignerInformationVerifier get(SignerId signerId)
22702308
assertTrue(digAlgs.contains(new AlgorithmIdentifier(TeleTrusTObjectIdentifiers.ripemd160, DERNull.INSTANCE)));
22712309
}
22722310

2311+
// public void testMLDsa44()
2312+
// throws Exception
2313+
// {
2314+
// /*
2315+
// * draft-ietf-lamps-cms-ml-dsa-02 3.3. SHA-512 [FIPS180] MUST be supported for use with the variants
2316+
// * of ML-DSA in this document; however, other hash functions MAY also be supported. When SHA-512 is
2317+
// * used, the id-sha512 [RFC5754] digest algorithm identifier is used and the parameters field MUST be
2318+
// * omitted.
2319+
// *
2320+
// * We confirm here that our implementation defaults to SHA-512 for the digest algorithm.
2321+
// */
2322+
// AlgorithmIdentifier expectedDigAlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512);
2323+
//
2324+
// encapsulatedTest(_signMLDsa44KP, _signMLDsa44Cert, "ML-DSA-44", NISTObjectIdentifiers.id_ml_dsa_44,
2325+
// expectedDigAlgId);
2326+
// }
2327+
//
2328+
// public void testMLDsa65()
2329+
// throws Exception
2330+
// {
2331+
// /*
2332+
// * draft-ietf-lamps-cms-ml-dsa-02 3.3. SHA-512 [FIPS180] MUST be supported for use with the variants
2333+
// * of ML-DSA in this document; however, other hash functions MAY also be supported. When SHA-512 is
2334+
// * used, the id-sha512 [RFC5754] digest algorithm identifier is used and the parameters field MUST be
2335+
// * omitted.
2336+
// *
2337+
// * We confirm here that our implementation defaults to SHA-512 for the digest algorithm.
2338+
// */
2339+
// AlgorithmIdentifier expectedDigAlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512);
2340+
//
2341+
// encapsulatedTest(_signMLDsa65KP, _signMLDsa65Cert, "ML-DSA-65", NISTObjectIdentifiers.id_ml_dsa_65,
2342+
// expectedDigAlgId);
2343+
// }
2344+
//
2345+
// public void testMLDsa87()
2346+
// throws Exception
2347+
// {
2348+
// /*
2349+
// * draft-ietf-lamps-cms-ml-dsa-02 3.3. SHA-512 [FIPS180] MUST be supported for use with the variants
2350+
// * of ML-DSA in this document; however, other hash functions MAY also be supported. When SHA-512 is
2351+
// * used, the id-sha512 [RFC5754] digest algorithm identifier is used and the parameters field MUST be
2352+
// * omitted.
2353+
// *
2354+
// * We confirm here that our implementation defaults to SHA-512 for the digest algorithm.
2355+
// */
2356+
// AlgorithmIdentifier expectedDigAlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512);
2357+
//
2358+
// encapsulatedTest(_signMLDsa87KP, _signMLDsa87Cert, "ML-DSA-87", NISTObjectIdentifiers.id_ml_dsa_87,
2359+
// expectedDigAlgId);
2360+
// }
2361+
22732362
private void rsaPSSTest(String signatureAlgorithmName)
22742363
throws Exception
22752364
{

0 commit comments

Comments
 (0)