Skip to content

Commit 7c86b38

Browse files
committed
added secondary name lookups for lightweight names.
added additional key pair tests for ML-DSA SLH-DSA
1 parent 313833c commit 7c86b38

File tree

4 files changed

+131
-26
lines changed

4 files changed

+131
-26
lines changed

prov/src/main/java/org/bouncycastle/jcajce/spec/SLHDSAParameterSpec.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public class SLHDSAParameterSpec
6464
parameters.put(SLHDSAParameterSpec.slh_dsa_sha2_256f.getName(), SLHDSAParameterSpec.slh_dsa_sha2_256f);
6565
parameters.put(SLHDSAParameterSpec.slh_dsa_sha2_256s.getName(), SLHDSAParameterSpec.slh_dsa_sha2_256s);
6666

67-
parameters.put("slh-dsa-sha2-128f", SLHDSAParameterSpec.slh_dsa_sha2_128f);
68-
parameters.put("slh-dsa-sha2-128s", SLHDSAParameterSpec.slh_dsa_sha2_128s);
69-
parameters.put("slh-dsa-sha2-192f", SLHDSAParameterSpec.slh_dsa_sha2_192f);
70-
parameters.put("slh-dsa-sha2-192s", SLHDSAParameterSpec.slh_dsa_sha2_192s);
71-
parameters.put("slh-dsa-sha2-256f", SLHDSAParameterSpec.slh_dsa_sha2_256f);
72-
parameters.put("slh-dsa-sha2-256s", SLHDSAParameterSpec.slh_dsa_sha2_256s);
67+
parameters.put("sha2-128f", SLHDSAParameterSpec.slh_dsa_sha2_128f);
68+
parameters.put("sha2-128s", SLHDSAParameterSpec.slh_dsa_sha2_128s);
69+
parameters.put("sha2-192f", SLHDSAParameterSpec.slh_dsa_sha2_192f);
70+
parameters.put("sha2-192s", SLHDSAParameterSpec.slh_dsa_sha2_192s);
71+
parameters.put("sha2-256f", SLHDSAParameterSpec.slh_dsa_sha2_256f);
72+
parameters.put("sha2-256s", SLHDSAParameterSpec.slh_dsa_sha2_256s);
7373

7474
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_128f.getName(), SLHDSAParameterSpec.slh_dsa_shake_128f);
7575
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_128s.getName(), SLHDSAParameterSpec.slh_dsa_shake_128s);
@@ -78,12 +78,12 @@ public class SLHDSAParameterSpec
7878
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_256f.getName(), SLHDSAParameterSpec.slh_dsa_shake_256f);
7979
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_256s.getName(), SLHDSAParameterSpec.slh_dsa_shake_256s);
8080

81-
parameters.put("slh-dsa-shake-128f", SLHDSAParameterSpec.slh_dsa_shake_128f);
82-
parameters.put("slh-dsa-shake-128s", SLHDSAParameterSpec.slh_dsa_shake_128s);
83-
parameters.put("slh-dsa-shake-192f", SLHDSAParameterSpec.slh_dsa_shake_192f);
84-
parameters.put("slh-dsa-shake-192s", SLHDSAParameterSpec.slh_dsa_shake_192s);
85-
parameters.put("slh-dsa-shake-256f", SLHDSAParameterSpec.slh_dsa_shake_256f);
86-
parameters.put("slh-dsa-shake-256s", SLHDSAParameterSpec.slh_dsa_shake_256s);
81+
parameters.put("shake-128f", SLHDSAParameterSpec.slh_dsa_shake_128f);
82+
parameters.put("shake-128s", SLHDSAParameterSpec.slh_dsa_shake_128s);
83+
parameters.put("shake-192f", SLHDSAParameterSpec.slh_dsa_shake_192f);
84+
parameters.put("shake-192s", SLHDSAParameterSpec.slh_dsa_shake_192s);
85+
parameters.put("shake-256f", SLHDSAParameterSpec.slh_dsa_shake_256f);
86+
parameters.put("shake-256s", SLHDSAParameterSpec.slh_dsa_shake_256s);
8787

8888
parameters.put(SLHDSAParameterSpec.slh_dsa_sha2_128f_with_sha256.getName(), SLHDSAParameterSpec.slh_dsa_sha2_128f);
8989
parameters.put(SLHDSAParameterSpec.slh_dsa_sha2_128s_with_sha256.getName(), SLHDSAParameterSpec.slh_dsa_sha2_128s);
@@ -92,12 +92,12 @@ public class SLHDSAParameterSpec
9292
parameters.put(SLHDSAParameterSpec.slh_dsa_sha2_256f_with_sha512.getName(), SLHDSAParameterSpec.slh_dsa_sha2_256f);
9393
parameters.put(SLHDSAParameterSpec.slh_dsa_sha2_256s_with_sha512.getName(), SLHDSAParameterSpec.slh_dsa_sha2_256s);
9494

95-
parameters.put("slh-dsa-sha2-128f-with-sha256", SLHDSAParameterSpec.slh_dsa_sha2_128f_with_sha256);
96-
parameters.put("slh-dsa-sha2-128s-with-sha256", SLHDSAParameterSpec.slh_dsa_sha2_128s_with_sha256);
97-
parameters.put("slh-dsa-sha2-192f-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_192f_with_sha512);
98-
parameters.put("slh-dsa-sha2-192s-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_192s_with_sha512);
99-
parameters.put("slh-dsa-sha2-256f-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_256f_with_sha512);
100-
parameters.put("slh-dsa-sha2-256s-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_256s_with_sha512);
95+
parameters.put("sha2-128f-with-sha256", SLHDSAParameterSpec.slh_dsa_sha2_128f_with_sha256);
96+
parameters.put("sha2-128s-with-sha256", SLHDSAParameterSpec.slh_dsa_sha2_128s_with_sha256);
97+
parameters.put("sha2-192f-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_192f_with_sha512);
98+
parameters.put("sha2-192s-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_192s_with_sha512);
99+
parameters.put("sha2-256f-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_256f_with_sha512);
100+
parameters.put("sha2-256s-with-sha512", SLHDSAParameterSpec.slh_dsa_sha2_256s_with_sha512);
101101

102102
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_128f_with_shake128.getName(), SLHDSAParameterSpec.slh_dsa_shake_128f_with_shake128);
103103
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_128s_with_shake128.getName(), SLHDSAParameterSpec.slh_dsa_shake_128s_with_shake128);
@@ -106,12 +106,12 @@ public class SLHDSAParameterSpec
106106
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_256f_with_shake256.getName(), SLHDSAParameterSpec.slh_dsa_shake_256f_with_shake256);
107107
parameters.put(SLHDSAParameterSpec.slh_dsa_shake_256s_with_shake256.getName(), SLHDSAParameterSpec.slh_dsa_shake_256s_with_shake256);
108108

109-
parameters.put("slh-dsa-shake-128f-with-shake128", SLHDSAParameterSpec.slh_dsa_shake_128f_with_shake128);
110-
parameters.put("slh-dsa-shake-128s-with-shake128", SLHDSAParameterSpec.slh_dsa_shake_128s_with_shake128);
111-
parameters.put("slh-dsa-shake-192f-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_192f_with_shake256);
112-
parameters.put("slh-dsa-shake-192s-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_192s_with_shake256);
113-
parameters.put("slh-dsa-shake-256f-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_256f_with_shake256);
114-
parameters.put("slh-dsa-shake-256s-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_256s_with_shake256);
109+
parameters.put("shake-128f-with-shake128", SLHDSAParameterSpec.slh_dsa_shake_128f_with_shake128);
110+
parameters.put("shake-128s-with-shake128", SLHDSAParameterSpec.slh_dsa_shake_128s_with_shake128);
111+
parameters.put("shake-192f-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_192f_with_shake256);
112+
parameters.put("shake-192s-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_192s_with_shake256);
113+
parameters.put("shake-256f-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_256f_with_shake256);
114+
parameters.put("shake-256s-with-shake256", SLHDSAParameterSpec.slh_dsa_shake_256s_with_shake256);
115115

116116
}
117117

prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/MLDSAKeyPairGeneratorTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* KeyFactory/KeyPairGenerator tests for MLDSA with BC provider.
2020
*/
2121
public class MLDSAKeyPairGeneratorTest
22-
extends KeyPairGeneratorTest
22+
extends MainProvKeyPairGeneratorTest
2323
{
2424
protected void setUp()
2525
{
@@ -105,6 +105,10 @@ public void testKeyPairEncoding()
105105
kpg.initialize(params[i], new SecureRandom());
106106
KeyPair keyPair = kpg.generateKeyPair();
107107
performKeyPairEncodingTest(keyPair);
108+
performKeyPairEncodingTest(params[i].getName(), keyPair);
109+
performKeyPairEncodingTest(oids[i].getId(), keyPair);
110+
assertNotNull(((MLDSAPrivateKey)keyPair.getPrivate()).getParameterSpec());
111+
assertNotNull(((MLDSAPublicKey)keyPair.getPublic()).getParameterSpec());
108112
assertEquals(oids[i], SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()).getAlgorithm().getAlgorithm());
109113
assertTrue(oids[i].toString(), Arrays.areEqual(((MLDSAPublicKey)keyPair.getPublic()).getPublicData(), ((MLDSAPrivateKey)keyPair.getPrivate()).getPublicKey().getPublicData()));
110114
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.bouncycastle.pqc.jcajce.provider.test;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.ObjectInputStream;
7+
import java.io.ObjectOutputStream;
8+
import java.security.Key;
9+
import java.security.KeyFactory;
10+
import java.security.KeyPair;
11+
import java.security.KeyPairGenerator;
12+
import java.security.PrivateKey;
13+
import java.security.PublicKey;
14+
import java.security.spec.PKCS8EncodedKeySpec;
15+
import java.security.spec.X509EncodedKeySpec;
16+
17+
public abstract class MainProvKeyPairGeneratorTest
18+
extends FlexiTest
19+
{
20+
21+
protected KeyPairGenerator kpg;
22+
23+
protected KeyFactory kf;
24+
25+
protected final void performKeyPairEncodingTest(KeyPair keyPair)
26+
{
27+
performKeyPairEncodingTest(null, keyPair);
28+
}
29+
30+
protected final void performKeyPairEncodingTest(String name, KeyPair keyPair)
31+
{
32+
try
33+
{
34+
PublicKey pubKey = keyPair.getPublic();
35+
PrivateKey privKey = keyPair.getPrivate();
36+
37+
byte[] encPubKey = pubKey.getEncoded();
38+
byte[] encPrivKey = privKey.getEncoded();
39+
40+
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encPubKey);
41+
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(encPrivKey);
42+
43+
PublicKey decPubKey = kf.generatePublic(pubKeySpec);
44+
PrivateKey decPrivKey = kf.generatePrivate(privKeySpec);
45+
46+
assertEquals(pubKey, decPubKey);
47+
assertEquals(pubKey.getAlgorithm(), decPubKey.getAlgorithm());
48+
assertEquals(pubKey.hashCode(), decPubKey.hashCode());
49+
50+
assertEquals(privKey, decPrivKey);
51+
assertEquals(privKey.getAlgorithm(), decPrivKey.getAlgorithm());
52+
assertEquals(privKey.hashCode(), decPrivKey.hashCode());
53+
54+
if (name != null)
55+
{
56+
KeyFactory nkf = KeyFactory.getInstance(name, "BC");
57+
58+
decPubKey = nkf.generatePublic(pubKeySpec);
59+
decPrivKey = nkf.generatePrivate(privKeySpec);
60+
61+
assertEquals(pubKey, decPubKey);
62+
assertEquals(pubKey.getAlgorithm(), decPubKey.getAlgorithm());
63+
assertEquals(pubKey.hashCode(), decPubKey.hashCode());
64+
65+
assertEquals(privKey, decPrivKey);
66+
assertEquals(privKey.getAlgorithm(), decPrivKey.getAlgorithm());
67+
assertEquals(privKey.hashCode(), decPrivKey.hashCode());
68+
}
69+
checkSerialisation(pubKey);
70+
checkSerialisation(privKey);
71+
}
72+
catch (Exception e)
73+
{
74+
e.printStackTrace();
75+
fail(e);
76+
}
77+
}
78+
79+
private void checkSerialisation(Key key)
80+
throws IOException, ClassNotFoundException
81+
{
82+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
83+
ObjectOutputStream oOut = new ObjectOutputStream(bOut);
84+
85+
oOut.writeObject(key);
86+
oOut.close();
87+
88+
ObjectInputStream oIn = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
89+
90+
Key inKey = (Key)oIn.readObject();
91+
92+
assertEquals(key, inKey);
93+
assertEquals(key.getAlgorithm(), inKey.getAlgorithm());
94+
assertEquals(key.hashCode(), inKey.hashCode());
95+
}
96+
97+
}

prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/SLHDSAKeyPairGeneratorTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* KeyFactory/KeyPairGenerator tests for SLHDSA with the BC provider.
2525
*/
2626
public class SLHDSAKeyPairGeneratorTest
27-
extends KeyPairGeneratorTest
27+
extends MainProvKeyPairGeneratorTest
2828
{
2929

3030
protected void setUp()
@@ -151,6 +151,10 @@ public void testKeyPairEncoding()
151151
kpg.initialize(params[i], new SecureRandom());
152152
KeyPair keyPair = kpg.generateKeyPair();
153153
performKeyPairEncodingTest(keyPair);
154+
performKeyPairEncodingTest(params[i].getName(), keyPair);
155+
performKeyPairEncodingTest(oids[i].getId(), keyPair);
156+
assertNotNull(((SLHDSAPrivateKey)keyPair.getPrivate()).getParameterSpec());
157+
assertNotNull(((SLHDSAPublicKey)keyPair.getPublic()).getParameterSpec());
154158
assertEquals(oids[i], SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()).getAlgorithm().getAlgorithm());
155159
assertTrue(oids[i].toString(), Arrays.areEqual(((SLHDSAPublicKey)keyPair.getPublic()).getPublicData(), ((SLHDSAPrivateKey)keyPair.getPrivate()).getPublicKey().getPublicData()));
156160
}

0 commit comments

Comments
 (0)