|
| 1 | +package org.bouncycastle.pqc.jcajce.provider.test; |
| 2 | + |
| 3 | +import java.security.AlgorithmParameters; |
| 4 | +import java.security.KeyPair; |
| 5 | +import java.security.KeyPairGenerator; |
| 6 | +import java.security.ProviderException; |
| 7 | +import java.security.Security; |
| 8 | +import java.security.Signature; |
| 9 | + |
| 10 | +import junit.framework.TestCase; |
| 11 | +import org.bouncycastle.jcajce.spec.ContextParameterSpec; |
| 12 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 13 | +import org.bouncycastle.util.Arrays; |
| 14 | +import org.bouncycastle.util.Strings; |
| 15 | + |
| 16 | +/** |
| 17 | + * Signature.setParameter(...) on the ML-DSA and SLH-DSA services, which take a signature context |
| 18 | + * through a {@link ContextParameterSpec}. |
| 19 | + * <p> |
| 20 | + * The context may be set either side of initSign / initVerify, as it may be for the RSASSA-PSS |
| 21 | + * parameters: setting it while the signature already has a key re-initialises the underlying signer |
| 22 | + * at once, and setting it beforehand records it for the key to arrive. Setting it first used to let |
| 23 | + * a NullPointerException out of a method declared to throw InvalidAlgorithmParameterException, |
| 24 | + * from dereferencing the key that was not there yet (github #2396). |
| 25 | + */ |
| 26 | +public class SignatureSetParameterTest |
| 27 | + extends TestCase |
| 28 | +{ |
| 29 | + private static final byte[] MSG = Strings.toByteArray("the quick brown fox"); |
| 30 | + private static final byte[] CONTEXT = Strings.toByteArray("Hello, world!"); |
| 31 | + |
| 32 | + private static final String[] SIGNATURE_ALGORITHMS = |
| 33 | + { |
| 34 | + "ML-DSA", |
| 35 | + "ML-DSA-65", |
| 36 | + "ML-DSA-65-WITH-SHA512", |
| 37 | + "HASH-ML-DSA", |
| 38 | + "SLH-DSA", |
| 39 | + "SLH-DSA-SHA2-128F", |
| 40 | + "SLH-DSA-SHA2-128F-WITH-SHA256", |
| 41 | + "HASH-SLH-DSA" |
| 42 | + }; |
| 43 | + |
| 44 | + private static final String[] KEY_ALGORITHMS = |
| 45 | + { |
| 46 | + "ML-DSA-65", |
| 47 | + "ML-DSA-65", |
| 48 | + "ML-DSA-65", |
| 49 | + "ML-DSA-65", |
| 50 | + "SLH-DSA-SHA2-128F", |
| 51 | + "SLH-DSA-SHA2-128F", |
| 52 | + "SLH-DSA-SHA2-128F", |
| 53 | + "SLH-DSA-SHA2-128F" |
| 54 | + }; |
| 55 | + |
| 56 | + public void setUp() |
| 57 | + { |
| 58 | + if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) |
| 59 | + { |
| 60 | + Security.addProvider(new BouncyCastleProvider()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Setting the context before initialising has to reach the signer, not be dropped on the way - |
| 66 | + * so the signature it produces must be the one the established after-init order produces, and |
| 67 | + * must not verify against a signature service given no context at all. |
| 68 | + */ |
| 69 | + public void testSetParameterBeforeInit() |
| 70 | + throws Exception |
| 71 | + { |
| 72 | + for (int i = 0; i != SIGNATURE_ALGORITHMS.length; i++) |
| 73 | + { |
| 74 | + String algorithm = SIGNATURE_ALGORITHMS[i]; |
| 75 | + |
| 76 | + KeyPair kp = KeyPairGenerator.getInstance(KEY_ALGORITHMS[i], "BC").generateKeyPair(); |
| 77 | + |
| 78 | + Signature before = Signature.getInstance(algorithm, "BC"); |
| 79 | + |
| 80 | + before.setParameter(new ContextParameterSpec(CONTEXT)); |
| 81 | + before.initSign(kp.getPrivate()); |
| 82 | + before.update(MSG); |
| 83 | + |
| 84 | + byte[] sigBefore = before.sign(); |
| 85 | + |
| 86 | + Signature after = Signature.getInstance(algorithm, "BC"); |
| 87 | + |
| 88 | + after.initSign(kp.getPrivate()); |
| 89 | + after.setParameter(new ContextParameterSpec(CONTEXT)); |
| 90 | + after.update(MSG); |
| 91 | + |
| 92 | + assertTrue(algorithm + ": context set before init did not reach the signer", |
| 93 | + Arrays.areEqual(sigBefore, after.sign())); |
| 94 | + |
| 95 | + Signature verifier = Signature.getInstance(algorithm, "BC"); |
| 96 | + |
| 97 | + verifier.setParameter(new ContextParameterSpec(CONTEXT)); |
| 98 | + verifier.initVerify(kp.getPublic()); |
| 99 | + verifier.update(MSG); |
| 100 | + |
| 101 | + assertTrue(algorithm, verifier.verify(sigBefore)); |
| 102 | + |
| 103 | + Signature noContext = Signature.getInstance(algorithm, "BC"); |
| 104 | + |
| 105 | + noContext.initVerify(kp.getPublic()); |
| 106 | + noContext.update(MSG); |
| 107 | + |
| 108 | + assertFalse(algorithm + ": verified without the context", noContext.verify(sigBefore)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void testSetParameterAfterInit() |
| 113 | + throws Exception |
| 114 | + { |
| 115 | + for (int i = 0; i != SIGNATURE_ALGORITHMS.length; i++) |
| 116 | + { |
| 117 | + String algorithm = SIGNATURE_ALGORITHMS[i]; |
| 118 | + |
| 119 | + KeyPair kp = KeyPairGenerator.getInstance(KEY_ALGORITHMS[i], "BC").generateKeyPair(); |
| 120 | + |
| 121 | + Signature signer = Signature.getInstance(algorithm, "BC"); |
| 122 | + |
| 123 | + signer.initSign(kp.getPrivate()); |
| 124 | + signer.setParameter(new ContextParameterSpec(CONTEXT)); |
| 125 | + signer.update(MSG); |
| 126 | + |
| 127 | + byte[] sig = signer.sign(); |
| 128 | + |
| 129 | + Signature verifier = Signature.getInstance(algorithm, "BC"); |
| 130 | + |
| 131 | + verifier.initVerify(kp.getPublic()); |
| 132 | + verifier.setParameter(new ContextParameterSpec(CONTEXT)); |
| 133 | + verifier.update(MSG); |
| 134 | + |
| 135 | + assertTrue(algorithm, verifier.verify(sig)); |
| 136 | + |
| 137 | + Signature wrongContext = Signature.getInstance(algorithm, "BC"); |
| 138 | + |
| 139 | + wrongContext.initVerify(kp.getPublic()); |
| 140 | + wrongContext.setParameter(new ContextParameterSpec(Strings.toByteArray("other context"))); |
| 141 | + wrongContext.update(MSG); |
| 142 | + |
| 143 | + assertFalse(algorithm, wrongContext.verify(sig)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * The null spec resolves to the empty context, which is what a signature with no context set |
| 149 | + * uses anyway, so it is accepted before initialisation as well. |
| 150 | + */ |
| 151 | + public void testSetNullParameterBeforeInit() |
| 152 | + throws Exception |
| 153 | + { |
| 154 | + KeyPair kp = KeyPairGenerator.getInstance("ML-DSA-65", "BC").generateKeyPair(); |
| 155 | + |
| 156 | + Signature sig = Signature.getInstance("ML-DSA", "BC"); |
| 157 | + |
| 158 | + sig.setParameter(null); |
| 159 | + sig.initSign(kp.getPrivate()); |
| 160 | + sig.update(MSG); |
| 161 | + |
| 162 | + byte[] s = sig.sign(); |
| 163 | + |
| 164 | + Signature verifier = Signature.getInstance("ML-DSA", "BC"); |
| 165 | + |
| 166 | + verifier.initVerify(kp.getPublic()); |
| 167 | + verifier.update(MSG); |
| 168 | + |
| 169 | + assertTrue(verifier.verify(s)); |
| 170 | + } |
| 171 | + |
| 172 | + public void testGetParametersReflectsContextSetBeforeInit() |
| 173 | + throws Exception |
| 174 | + { |
| 175 | + KeyPair kp = KeyPairGenerator.getInstance("ML-DSA-65", "BC").generateKeyPair(); |
| 176 | + |
| 177 | + Signature sig = Signature.getInstance("ML-DSA", "BC"); |
| 178 | + |
| 179 | + sig.setParameter(new ContextParameterSpec(CONTEXT)); |
| 180 | + sig.initSign(kp.getPrivate()); |
| 181 | + |
| 182 | + AlgorithmParameters params = sig.getParameters(); |
| 183 | + |
| 184 | + assertNotNull(params); |
| 185 | + assertTrue(Arrays.areEqual(CONTEXT, |
| 186 | + params.getParameterSpec(ContextParameterSpec.class).getContext())); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Re-setting the context replaces the previous one rather than being reported through a stale |
| 191 | + * cached AlgorithmParameters. |
| 192 | + */ |
| 193 | + public void testResetContext() |
| 194 | + throws Exception |
| 195 | + { |
| 196 | + byte[] second = Strings.toByteArray("second context"); |
| 197 | + |
| 198 | + KeyPair kp = KeyPairGenerator.getInstance("ML-DSA-65", "BC").generateKeyPair(); |
| 199 | + |
| 200 | + Signature sig = Signature.getInstance("ML-DSA", "BC"); |
| 201 | + |
| 202 | + sig.setParameter(new ContextParameterSpec(CONTEXT)); |
| 203 | + sig.initSign(kp.getPrivate()); |
| 204 | + sig.setParameter(new ContextParameterSpec(second)); |
| 205 | + |
| 206 | + assertTrue(Arrays.areEqual(second, |
| 207 | + sig.getParameters().getParameterSpec(ContextParameterSpec.class).getContext())); |
| 208 | + |
| 209 | + sig.update(MSG); |
| 210 | + |
| 211 | + byte[] s = sig.sign(); |
| 212 | + |
| 213 | + Signature verifier = Signature.getInstance("ML-DSA", "BC"); |
| 214 | + |
| 215 | + verifier.setParameter(new ContextParameterSpec(second)); |
| 216 | + verifier.initVerify(kp.getPublic()); |
| 217 | + verifier.update(MSG); |
| 218 | + |
| 219 | + assertTrue(verifier.verify(s)); |
| 220 | + } |
| 221 | + |
| 222 | + public void testSetParameterMidUpdateStillRejected() |
| 223 | + throws Exception |
| 224 | + { |
| 225 | + KeyPair kp = KeyPairGenerator.getInstance("ML-DSA-65", "BC").generateKeyPair(); |
| 226 | + |
| 227 | + Signature sig = Signature.getInstance("ML-DSA", "BC"); |
| 228 | + |
| 229 | + sig.initSign(kp.getPrivate()); |
| 230 | + sig.update(MSG); |
| 231 | + |
| 232 | + try |
| 233 | + { |
| 234 | + sig.setParameter(new ContextParameterSpec(CONTEXT)); |
| 235 | + fail("no exception"); |
| 236 | + } |
| 237 | + catch (ProviderException e) |
| 238 | + { |
| 239 | + assertEquals("cannot call setParameter in the middle of update", e.getMessage()); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments