Skip to content

Commit 270bf12

Browse files
committed
Updated SLH-DSA test using new vector sets.
1 parent f1a299b commit 270bf12

File tree

2 files changed

+244
-27
lines changed

2 files changed

+244
-27
lines changed

core/src/test/java/org/bouncycastle/pqc/crypto/test/AllTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static Test suite()
4747
suite.addTestSuite(GeMSSTest.class);
4848
suite.addTestSuite(XWingTest.class);
4949
suite.addTestSuite(AllTests.SimpleTestTest.class);
50+
suite.addTestSuite(SLHDSATest.class);
5051

5152
return new BCTestSetup(suite);
5253
}

core/src/test/java/org/bouncycastle/pqc/crypto/test/SLHDSATest.java

Lines changed: 243 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.ArrayList;
99
import java.util.HashMap;
1010
import java.util.List;
11+
import java.util.Map;
1112

1213
import junit.framework.TestCase;
1314
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
@@ -26,8 +27,221 @@
2627
import org.bouncycastle.util.encoders.Hex;
2728

2829
public class SLHDSATest
29-
extends TestCase
30+
extends TestCase
3031
{
32+
33+
private static Map<String, SLHDSAParameters> parametersMap = new HashMap<String, SLHDSAParameters>()
34+
{
35+
{
36+
put("SLH-DSA-SHA2-128s", SLHDSAParameters.sha2_128s);
37+
put("SLH-DSA-SHA2-128f", SLHDSAParameters.sha2_128f);
38+
put("SLH-DSA-SHA2-192s", SLHDSAParameters.sha2_192s);
39+
put("SLH-DSA-SHA2-192f", SLHDSAParameters.sha2_192f);
40+
put("SLH-DSA-SHA2-256s", SLHDSAParameters.sha2_256s);
41+
put("SLH-DSA-SHA2-256f", SLHDSAParameters.sha2_256f);
42+
43+
put("SLH-DSA-SHAKE-128s", SLHDSAParameters.shake_128s);
44+
put("SLH-DSA-SHAKE-128f", SLHDSAParameters.shake_128f);
45+
put("SLH-DSA-SHAKE-192s", SLHDSAParameters.shake_192s);
46+
put("SLH-DSA-SHAKE-192f", SLHDSAParameters.shake_192f);
47+
put("SLH-DSA-SHAKE-256s", SLHDSAParameters.shake_256s);
48+
put("SLH-DSA-SHAKE-256f", SLHDSAParameters.shake_256f);
49+
}
50+
};
51+
52+
53+
public void testKeyGenSingleFile() throws IOException
54+
{
55+
56+
57+
58+
TestSampler sampler = new TestSampler();
59+
60+
String name ="SLH-DSA-keyGen.txt";
61+
// System.out.println("testing: " + name);
62+
InputStream src = TestResourceFinder.findTestResource("pqc/crypto/slhdsa/", name);
63+
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
64+
65+
String line = null;
66+
HashMap<String, String> buf = new HashMap<String, String>();
67+
while ((line = bin.readLine()) != null)
68+
{
69+
line = line.trim();
70+
71+
if (line.startsWith("#"))
72+
{
73+
continue;
74+
}
75+
if (line.length() == 0)
76+
{
77+
if (buf.size() > 0)
78+
{
79+
byte[] skSeed = Hex.decode((String) buf.get("skSeed"));
80+
byte[] skPrf = Hex.decode((String) buf.get("skPrf"));
81+
byte[] pkSeed = Hex.decode((String) buf.get("pkSeed"));
82+
byte[] pk = Hex.decode((String) buf.get("pk"));
83+
byte[] sk = Hex.decode((String) buf.get("sk"));
84+
85+
SLHDSAParameters parameters = parametersMap.get(buf.get("parameterSet"));
86+
87+
SLHDSAKeyPairGenerator kpGen = new SLHDSAKeyPairGenerator();
88+
SLHDSAKeyGenerationParameters genParam = new SLHDSAKeyGenerationParameters(new SecureRandom(), parameters);
89+
//
90+
// Generate keys and test.
91+
//
92+
kpGen.init(genParam);
93+
AsymmetricCipherKeyPair kp = kpGen.internalGenerateKeyPair(skSeed, skPrf, pkSeed);
94+
95+
SLHDSAPublicKeyParameters pubParams = (SLHDSAPublicKeyParameters) PublicKeyFactory.createKey(
96+
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo((SLHDSAPublicKeyParameters) kp.getPublic()));
97+
SLHDSAPrivateKeyParameters privParams = (SLHDSAPrivateKeyParameters) PrivateKeyFactory.createKey(
98+
PrivateKeyInfoFactory.createPrivateKeyInfo((SLHDSAPrivateKeyParameters) kp.getPrivate()));
99+
100+
assertTrue(name + ": public key", Arrays.areEqual(pk, pubParams.getEncoded()));
101+
assertTrue(name + ": secret key", Arrays.areEqual(sk, privParams.getEncoded()));
102+
103+
}
104+
buf.clear();
105+
106+
continue;
107+
}
108+
109+
int a = line.indexOf("=");
110+
if (a > -1)
111+
{
112+
buf.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
113+
}
114+
}
115+
// System.out.println("testing successful!");
116+
117+
}
118+
119+
120+
public void testSigGenSingleFile() throws IOException
121+
{
122+
123+
String name ="SLH-DSA-sigGen.txt";
124+
// System.out.println("testing: " + name);
125+
InputStream src = TestResourceFinder.findTestResource("pqc/crypto/slhdsa", name);
126+
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
127+
128+
String line = null;
129+
HashMap<String, String> buf = new HashMap<String, String>();
130+
while ((line = bin.readLine()) != null)
131+
{
132+
line = line.trim();
133+
134+
if (line.startsWith("#"))
135+
{
136+
continue;
137+
}
138+
if (line.length() == 0)
139+
{
140+
if (buf.size() > 0)
141+
{
142+
boolean deterministic = !buf.containsKey("additionalRandomness");
143+
byte[] sk = Hex.decode((String) buf.get("sk"));
144+
int messageLength = Integer.parseInt((String) buf.get("messageLength"));
145+
byte[] message = Hex.decode((String) buf.get("message"));
146+
byte[] signature = Hex.decode((String) buf.get("signature"));
147+
byte[] rnd = null;
148+
149+
if (!deterministic)
150+
{
151+
rnd = Hex.decode((String) buf.get("additionalRandomness"));
152+
}
153+
154+
SLHDSAParameters parameters = parametersMap.get(buf.get("parameterSet"));
155+
156+
SLHDSAPrivateKeyParameters privParams = new SLHDSAPrivateKeyParameters(parameters, sk);
157+
158+
// sign
159+
SLHDSASigner signer = new SLHDSASigner();
160+
161+
signer.init(true, privParams);
162+
byte[] sigGenerated = signer.internalGenerateSignature(message, rnd);
163+
assertTrue(Arrays.areEqual(sigGenerated, signature));
164+
}
165+
buf.clear();
166+
167+
continue;
168+
}
169+
170+
int a = line.indexOf("=");
171+
if (a > -1)
172+
{
173+
buf.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
174+
}
175+
}
176+
// System.out.println("testing successful!");
177+
178+
}
179+
180+
181+
public void testSigVerSingleFile() throws IOException
182+
{
183+
String name ="SLH-DSA-sigVer.txt";
184+
// System.out.println("testing: " + name);
185+
InputStream src = TestResourceFinder.findTestResource("pqc/crypto/slhdsa", name);
186+
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
187+
188+
String line = null;
189+
HashMap<String, String> buf = new HashMap<String, String>();
190+
while ((line = bin.readLine()) != null)
191+
{
192+
line = line.trim();
193+
194+
if (line.startsWith("#"))
195+
{
196+
continue;
197+
}
198+
if (line.length() == 0)
199+
{
200+
if (buf.size() > 0)
201+
{
202+
boolean testPassed = Boolean.parseBoolean((String) buf.get("testPassed"));
203+
boolean deterministic = !buf.containsKey("additionalRandomness");
204+
String reason = buf.get("reason");
205+
206+
byte[] pk = Hex.decode((String) buf.get("pk"));
207+
byte[] message = Hex.decode((String) buf.get("message"));
208+
byte[] signature = Hex.decode((String) buf.get("signature"));
209+
210+
byte[] rnd = null;
211+
if (!deterministic)
212+
{
213+
rnd = Hex.decode((String) buf.get("additionalRandomness"));
214+
}
215+
216+
SLHDSAParameters parameters = parametersMap.get(buf.get("parameterSet"));
217+
218+
SLHDSAPublicKeyParameters pubParams = new SLHDSAPublicKeyParameters(parameters, pk);
219+
220+
221+
222+
SLHDSASigner verifier = new SLHDSASigner();
223+
verifier.init(false, pubParams);
224+
boolean ver = verifier.internalVerifySignature(message, signature);
225+
assertEquals("expected " + testPassed + " " + reason, ver, testPassed);
226+
}
227+
buf.clear();
228+
229+
continue;
230+
}
231+
232+
int a = line.indexOf("=");
233+
if (a > -1)
234+
{
235+
buf.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
236+
}
237+
}
238+
// System.out.println("testing successful!");
239+
240+
}
241+
242+
243+
244+
31245
public void testKeyGen() throws IOException
32246
{
33247
String[] files = new String[]{
@@ -66,11 +280,11 @@ public void testKeyGen() throws IOException
66280
{
67281
if (buf.size() > 0)
68282
{
69-
byte[] skSeed = Hex.decode((String)buf.get("skSeed"));
70-
byte[] skPrf = Hex.decode((String)buf.get("skPrf"));
71-
byte[] pkSeed = Hex.decode((String)buf.get("pkSeed"));
72-
byte[] pk = Hex.decode((String)buf.get("pk"));
73-
byte[] sk = Hex.decode((String)buf.get("sk"));
283+
byte[] skSeed = Hex.decode((String) buf.get("skSeed"));
284+
byte[] skPrf = Hex.decode((String) buf.get("skPrf"));
285+
byte[] pkSeed = Hex.decode((String) buf.get("pkSeed"));
286+
byte[] pk = Hex.decode((String) buf.get("pk"));
287+
byte[] sk = Hex.decode((String) buf.get("sk"));
74288

75289
SLHDSAParameters parameters = params[fileIndex];
76290

@@ -82,10 +296,10 @@ public void testKeyGen() throws IOException
82296
kpGen.init(genParam);
83297
AsymmetricCipherKeyPair kp = kpGen.internalGenerateKeyPair(skSeed, skPrf, pkSeed);
84298

85-
SLHDSAPublicKeyParameters pubParams = (SLHDSAPublicKeyParameters)PublicKeyFactory.createKey(
86-
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo((SLHDSAPublicKeyParameters)kp.getPublic()));
87-
SLHDSAPrivateKeyParameters privParams = (SLHDSAPrivateKeyParameters)PrivateKeyFactory.createKey(
88-
PrivateKeyInfoFactory.createPrivateKeyInfo((SLHDSAPrivateKeyParameters)kp.getPrivate()));
299+
SLHDSAPublicKeyParameters pubParams = (SLHDSAPublicKeyParameters) PublicKeyFactory.createKey(
300+
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo((SLHDSAPublicKeyParameters) kp.getPublic()));
301+
SLHDSAPrivateKeyParameters privParams = (SLHDSAPrivateKeyParameters) PrivateKeyFactory.createKey(
302+
PrivateKeyInfoFactory.createPrivateKeyInfo((SLHDSAPrivateKeyParameters) kp.getPrivate()));
89303

90304
assertTrue(name + ": public key", Arrays.areEqual(pk, pubParams.getEncoded()));
91305
assertTrue(name + ": secret key", Arrays.areEqual(sk, privParams.getEncoded()));
@@ -105,6 +319,7 @@ public void testKeyGen() throws IOException
105319
// System.out.println("testing successful!");
106320
}
107321
}
322+
108323
public void testSigGen() throws IOException
109324
{
110325

@@ -116,7 +331,7 @@ public void testSigGen() throws IOException
116331
"sigGen_SLH-DSA-SHAKE-256f.txt",
117332
};
118333

119-
SLHDSAParameters[] params = new SLHDSAParameters []{
334+
SLHDSAParameters[] params = new SLHDSAParameters[]{
120335
SLHDSAParameters.sha2_192s,
121336
SLHDSAParameters.sha2_256f,
122337
SLHDSAParameters.shake_128f,
@@ -147,15 +362,15 @@ public void testSigGen() throws IOException
147362
if (buf.size() > 0)
148363
{
149364
boolean deterministic = !buf.containsKey("additionalRandomness");
150-
byte[] sk = Hex.decode((String)buf.get("sk"));
151-
int messageLength = Integer.parseInt((String)buf.get("messageLength"));
152-
byte[] message = Hex.decode((String)buf.get("message"));
153-
byte[] signature = Hex.decode((String)buf.get("signature"));
365+
byte[] sk = Hex.decode((String) buf.get("sk"));
366+
int messageLength = Integer.parseInt((String) buf.get("messageLength"));
367+
byte[] message = Hex.decode((String) buf.get("message"));
368+
byte[] signature = Hex.decode((String) buf.get("signature"));
154369
byte[] rnd = null;
155370

156-
if(!deterministic)
371+
if (!deterministic)
157372
{
158-
rnd = Hex.decode((String)buf.get("additionalRandomness"));
373+
rnd = Hex.decode((String) buf.get("additionalRandomness"));
159374
}
160375

161376
SLHDSAParameters parameters = params[fileIndex];
@@ -183,6 +398,7 @@ public void testSigGen() throws IOException
183398
// System.out.println("testing successful!");
184399
}
185400
}
401+
186402
public void testSigVer() throws IOException
187403
{
188404
String[] files = new String[]{
@@ -223,19 +439,19 @@ public void testSigVer() throws IOException
223439
{
224440
if (buf.size() > 0)
225441
{
226-
boolean testPassed = Boolean.parseBoolean((String)buf.get("testPassed"));
442+
boolean testPassed = Boolean.parseBoolean((String) buf.get("testPassed"));
227443
boolean deterministic = !buf.containsKey("additionalRandomness");
228444
String reason = buf.get("reason");
229445

230-
byte[] pk = Hex.decode((String)buf.get("pk"));
231-
byte[] sk = Hex.decode((String)buf.get("sk"));
232-
byte[] message = Hex.decode((String)buf.get("message"));
233-
byte[] signature = Hex.decode((String)buf.get("signature"));
446+
byte[] pk = Hex.decode((String) buf.get("pk"));
447+
byte[] sk = Hex.decode((String) buf.get("sk"));
448+
byte[] message = Hex.decode((String) buf.get("message"));
449+
byte[] signature = Hex.decode((String) buf.get("signature"));
234450

235451
byte[] rnd = null;
236-
if(!deterministic)
452+
if (!deterministic)
237453
{
238-
rnd = Hex.decode((String)buf.get("additionalRandomness"));
454+
rnd = Hex.decode((String) buf.get("additionalRandomness"));
239455
}
240456

241457
SLHDSAParameters parameters = params[fileIndex];
@@ -462,8 +678,8 @@ public void testBasicKeyGenerationSha2128sSimple()
462678
kpGen.init(genParam);
463679
AsymmetricCipherKeyPair kp = kpGen.internalGenerateKeyPair(skSeed, skPrf, pkSeed);
464680

465-
SLHDSAPublicKeyParameters pubParams = (SLHDSAPublicKeyParameters)kp.getPublic();
466-
SLHDSAPrivateKeyParameters privParams = (SLHDSAPrivateKeyParameters)kp.getPrivate();
681+
SLHDSAPublicKeyParameters pubParams = (SLHDSAPublicKeyParameters) kp.getPublic();
682+
SLHDSAPrivateKeyParameters privParams = (SLHDSAPrivateKeyParameters) kp.getPrivate();
467683

468684
assertTrue("public key", Arrays.areEqual(pk, pubParams.getEncoded()));
469685
assertTrue("secret key", Arrays.areEqual(sk, privParams.getEncoded()));
@@ -506,6 +722,6 @@ private static String[] splitOn(String input, char c)
506722
l.add(s);
507723
}
508724

509-
return (String[])l.toArray(new String[0]);
725+
return (String[]) l.toArray(new String[0]);
510726
}
511727
}

0 commit comments

Comments
 (0)