Skip to content

Commit 14b8718

Browse files
committed
Check that the BCPQC signature services refuse setParameter the same way at every stage of a signature's life and stay usable afterwards, sweeping every registered service as well as one per family, relates to github #2396.
1 parent 897c379 commit 14b8718

2 files changed

Lines changed: 306 additions & 0 deletions

File tree

‎prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static Test suite()
6969
suite.addTestSuite(NamedKeyPairGeneratorTest.class);
7070
suite.addTestSuite(NamedKeyFactoryTest.class);
7171
suite.addTestSuite(PreHashKeyInteropTest.class);
72+
suite.addTestSuite(SignatureSetParameterTest.class);
7273
suite.addTestSuite(MayoKeyPairGeneratorTest.class);
7374
suite.addTestSuite(MayoTest.class);
7475
suite.addTestSuite(SnovaTest.class);
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
package org.bouncycastle.pqc.jcajce.provider.test;
2+
3+
import java.security.KeyPair;
4+
import java.security.KeyPairGenerator;
5+
import java.security.Provider;
6+
import java.security.SecureRandom;
7+
import java.security.Security;
8+
import java.security.Signature;
9+
import java.security.spec.AlgorithmParameterSpec;
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.Iterator;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.TreeSet;
16+
17+
import junit.framework.TestCase;
18+
import org.bouncycastle.jcajce.spec.ContextParameterSpec;
19+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
20+
import org.bouncycastle.pqc.crypto.lms.LMOtsParameters;
21+
import org.bouncycastle.pqc.crypto.lms.LMSigParameters;
22+
import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
23+
import org.bouncycastle.pqc.jcajce.spec.LMSKeyGenParameterSpec;
24+
import org.bouncycastle.pqc.jcajce.spec.SPHINCS256KeyGenParameterSpec;
25+
import org.bouncycastle.pqc.jcajce.spec.XMSSMTParameterSpec;
26+
import org.bouncycastle.pqc.jcajce.spec.XMSSParameterSpec;
27+
import org.bouncycastle.util.Strings;
28+
29+
/**
30+
* Signature.setParameter(...) on the BCPQC signature services - none of which take parameters, as
31+
* opposed to the ML-DSA and SLH-DSA services in the BC provider, which take a signature context
32+
* through a {@link ContextParameterSpec} (see the sibling
33+
* org.bouncycastle.jcajce.provider.test.SignatureSetParameterTest).
34+
* <p>
35+
* Every BCPQC signature SPI leaves engineSetParameter / engineGetParameters at the refusal the JCA
36+
* defines for a service with no parameters, so setParameter has to fail the same way at every point
37+
* in the signature's life - before initSign / initVerify, after it, and in the middle of update -
38+
* and has to leave the signature able to go on and produce a signature that verifies. A family that
39+
* grew a half-implemented setParameter would show up here as a different exception, or as one
40+
* thrown at some stages but not others.
41+
*/
42+
public class SignatureSetParameterTest
43+
extends TestCase
44+
{
45+
private static final byte[] MSG = Strings.toByteArray("the quick brown fox");
46+
private static final byte[] CONTEXT = Strings.toByteArray("Hello, world!");
47+
48+
/**
49+
* One signature algorithm per BCPQC signature family, with the key algorithm to generate for it.
50+
*/
51+
private static final String[][] SIGNATURE_ALGORITHMS =
52+
{
53+
{ "SHA512WITHSPHINCS256", "SPHINCS256" },
54+
{ "XMSS-SHA256", "XMSS" },
55+
{ "XMSSMT-SHA256", "XMSSMT" },
56+
{ "LMS", "LMS" },
57+
{ "FALCON-512", "FALCON-512" },
58+
{ "MAYO-1", "MAYO-1" },
59+
{ "SNOVA_24_5_4_SSK", "SNOVA_24_5_4_SSK" },
60+
{ "sqisign_lvl1", "sqisign_lvl1" },
61+
{ "UOV-IS", "UOV-IS" },
62+
{ "QRUOV1Q127L3V156M54", "QRUOV1Q127L3V156M54" },
63+
{ "HAETAE-2", "HAETAE-2" },
64+
{ "AIMer-128f", "AIMer-128f" },
65+
{ "FAEST_128F", "FAEST_128F" },
66+
{ "MQOM2-CAT1-GF256-FAST-R3", "MQOM2-CAT1-GF256-FAST-R3" },
67+
{ "SDITH-HYPERCUBE-CAT1-GF256", "SDITH-HYPERCUBE-CAT1-GF256" }
68+
};
69+
70+
/**
71+
* A drop in the number of registered signature services means the sweep below stopped covering
72+
* families rather than that the provider is clean, so it is asserted against a floor.
73+
*/
74+
private static final int MINIMUM_SIGNATURE_SERVICES = 150;
75+
76+
private static final Map keyPairs = new HashMap();
77+
78+
public void setUp()
79+
{
80+
if (Security.getProvider(BouncyCastlePQCProvider.PROVIDER_NAME) == null)
81+
{
82+
Security.addProvider(new BouncyCastlePQCProvider());
83+
}
84+
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null)
85+
{
86+
Security.addProvider(new BouncyCastleProvider());
87+
}
88+
}
89+
90+
/**
91+
* The refusal has to be the same at every stage, and has to leave the signature usable - a
92+
* caller that probes setParameter still gets a signature out of the object afterwards.
93+
*/
94+
public void testSetParameterRejectedAtEveryStage()
95+
throws Exception
96+
{
97+
for (int i = 0; i != SIGNATURE_ALGORITHMS.length; i++)
98+
{
99+
String algorithm = SIGNATURE_ALGORITHMS[i][0];
100+
101+
KeyPair kp = getKeyPair(SIGNATURE_ALGORITHMS[i][1]);
102+
103+
Signature signer = Signature.getInstance(algorithm, "BCPQC");
104+
105+
checkRejected(algorithm + ": before initSign", signer, new ContextParameterSpec(CONTEXT));
106+
107+
signer.initSign(kp.getPrivate());
108+
109+
checkRejected(algorithm + ": after initSign", signer, new ContextParameterSpec(CONTEXT));
110+
111+
signer.update(MSG);
112+
113+
checkRejected(algorithm + ": mid update", signer, new ContextParameterSpec(CONTEXT));
114+
115+
byte[] sig = signer.sign();
116+
117+
Signature verifier = Signature.getInstance(algorithm, "BCPQC");
118+
119+
checkRejected(algorithm + ": before initVerify", verifier, new ContextParameterSpec(CONTEXT));
120+
121+
verifier.initVerify(kp.getPublic());
122+
123+
checkRejected(algorithm + ": after initVerify", verifier, new ContextParameterSpec(CONTEXT));
124+
125+
verifier.update(MSG);
126+
127+
assertTrue(algorithm + ": signature lost to a rejected setParameter", verifier.verify(sig));
128+
}
129+
}
130+
131+
/**
132+
* A null spec is the way a caller says "no parameters", which is what these services use anyway,
133+
* but the JCA still leaves refusing it to the service - so it is refused like any other spec
134+
* rather than quietly accepted.
135+
*/
136+
public void testSetNullParameterRejected()
137+
throws Exception
138+
{
139+
for (int i = 0; i != SIGNATURE_ALGORITHMS.length; i++)
140+
{
141+
String algorithm = SIGNATURE_ALGORITHMS[i][0];
142+
143+
checkRejected(algorithm + ": null spec",
144+
Signature.getInstance(algorithm, "BCPQC"), null);
145+
}
146+
}
147+
148+
public void testGetParametersUnsupported()
149+
throws Exception
150+
{
151+
for (int i = 0; i != SIGNATURE_ALGORITHMS.length; i++)
152+
{
153+
String algorithm = SIGNATURE_ALGORITHMS[i][0];
154+
155+
Signature sig = Signature.getInstance(algorithm, "BCPQC");
156+
157+
try
158+
{
159+
sig.getParameters();
160+
fail(algorithm + ": no exception");
161+
}
162+
catch (UnsupportedOperationException e)
163+
{
164+
// expected - the service has no parameters to report
165+
}
166+
}
167+
}
168+
169+
/**
170+
* The table above names one algorithm per family; this covers the rest of the parameter sets,
171+
* and any family added since, by asking the provider itself what it registers. Failures are
172+
* collected so one new service that behaves differently reports as itself rather than as
173+
* whichever one the sweep happened to reach first.
174+
*/
175+
public void testAllRegisteredSignatureServicesRejectSetParameter()
176+
throws Exception
177+
{
178+
Provider prov = Security.getProvider(BouncyCastlePQCProvider.PROVIDER_NAME);
179+
180+
List failures = new ArrayList();
181+
int count = 0;
182+
183+
for (Iterator it = new TreeSet(getSignatureAlgorithms(prov)).iterator(); it.hasNext();)
184+
{
185+
String algorithm = (String)it.next();
186+
187+
count++;
188+
189+
Signature sig = Signature.getInstance(algorithm, prov);
190+
191+
try
192+
{
193+
sig.setParameter(new ContextParameterSpec(CONTEXT));
194+
failures.add(algorithm + ": setParameter accepted");
195+
}
196+
catch (UnsupportedOperationException e)
197+
{
198+
// expected
199+
}
200+
catch (Exception e)
201+
{
202+
failures.add(algorithm + ": setParameter threw " + e.getClass().getName());
203+
}
204+
205+
try
206+
{
207+
sig.getParameters();
208+
failures.add(algorithm + ": getParameters answered");
209+
}
210+
catch (UnsupportedOperationException e)
211+
{
212+
// expected
213+
}
214+
catch (Exception e)
215+
{
216+
failures.add(algorithm + ": getParameters threw " + e.getClass().getName());
217+
}
218+
}
219+
220+
assertTrue("only " + count + " signature services found", count >= MINIMUM_SIGNATURE_SERVICES);
221+
assertEquals(failures.toString(), 0, failures.size());
222+
}
223+
224+
private void checkRejected(String message, Signature sig, AlgorithmParameterSpec spec)
225+
{
226+
try
227+
{
228+
sig.setParameter(spec);
229+
fail(message + ": no exception");
230+
}
231+
catch (UnsupportedOperationException e)
232+
{
233+
// expected - the service takes no parameters
234+
}
235+
catch (Exception e)
236+
{
237+
fail(message + ": " + e.getClass().getName() + " - " + e.getMessage());
238+
}
239+
}
240+
241+
/**
242+
* The OID registrations name the same services as the algorithm names do, so they are left out
243+
* to keep the sweep's failure messages readable.
244+
*/
245+
private List getSignatureAlgorithms(Provider prov)
246+
{
247+
List algorithms = new ArrayList();
248+
249+
for (Iterator it = prov.getServices().iterator(); it.hasNext();)
250+
{
251+
Provider.Service service = (Provider.Service)it.next();
252+
253+
if (!"Signature".equals(service.getType()))
254+
{
255+
continue;
256+
}
257+
258+
String algorithm = service.getAlgorithm();
259+
260+
if (algorithm.indexOf('.') < 0)
261+
{
262+
algorithms.add(algorithm);
263+
}
264+
}
265+
266+
return algorithms;
267+
}
268+
269+
private KeyPair getKeyPair(String algorithm)
270+
throws Exception
271+
{
272+
KeyPair kp = (KeyPair)keyPairs.get(algorithm);
273+
274+
if (kp != null)
275+
{
276+
return kp;
277+
}
278+
279+
KeyPairGenerator kpGen = KeyPairGenerator.getInstance(algorithm, "BCPQC");
280+
281+
if (algorithm.equals("SPHINCS256"))
282+
{
283+
kpGen.initialize(new SPHINCS256KeyGenParameterSpec(), new SecureRandom());
284+
}
285+
else if (algorithm.equals("XMSS"))
286+
{
287+
kpGen.initialize(new XMSSParameterSpec(4, XMSSParameterSpec.SHA256), new SecureRandom());
288+
}
289+
else if (algorithm.equals("XMSSMT"))
290+
{
291+
kpGen.initialize(new XMSSMTParameterSpec(4, 2, XMSSMTParameterSpec.SHA256), new SecureRandom());
292+
}
293+
else if (algorithm.equals("LMS"))
294+
{
295+
kpGen.initialize(new LMSKeyGenParameterSpec(
296+
LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w1), new SecureRandom());
297+
}
298+
299+
kp = kpGen.generateKeyPair();
300+
301+
keyPairs.put(algorithm, kp);
302+
303+
return kp;
304+
}
305+
}

0 commit comments

Comments
 (0)