Skip to content

Commit e37e7da

Browse files
committed
LMS: Add the RFC 9858 Appendix A test vectors to HSSTests, verifying each and regenerating the public key and signature from the RFC SEED and I for the H=5 cases
1 parent 4ccea45 commit e37e7da

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

‎core/src/test/java/org/bouncycastle/crypto/params/HSSTests.java‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,98 @@ public void testHSSVector_2()
220220

221221
}
222222

223+
/**
224+
* RFC 9858 Appendix A.1, Test Case 1 - SHA-256/192 (LMS_SHA256_M24_H5 / LMOTS_SHA256_N24_W8).
225+
* From https://www.rfc-editor.org/rfc/rfc9858#appendix-A.1
226+
*/
227+
public void testRFC9858Vector_1()
228+
throws Exception
229+
{
230+
checkRFC9858Vector("rfc9858_testcase_1.txt", LMSigParameters.lms_sha256_n24_h5, LMOtsParameters.sha256_n24_w8, true);
231+
}
232+
233+
/**
234+
* RFC 9858 Appendix A.2, Test Vector for SHAKE256/192 (LMS_SHAKE_N24_H5 / LMOTS_SHAKE_N24_W8).
235+
* From https://www.rfc-editor.org/rfc/rfc9858#appendix-A.2
236+
*/
237+
public void testRFC9858Vector_2()
238+
throws Exception
239+
{
240+
checkRFC9858Vector("rfc9858_testcase_2.txt", LMSigParameters.lms_shake256_n24_h5, LMOtsParameters.shake256_n24_w8, true);
241+
}
242+
243+
/**
244+
* RFC 9858 Appendix A.3, Test Vector for SHAKE256/256 (LMS_SHAKE_N32_H5 / LMOTS_SHAKE_N32_W8); the
245+
* RFC's section title says SHA-256/256, but the parameter sets and message are SHAKE256/256.
246+
* From https://www.rfc-editor.org/rfc/rfc9858#appendix-A.3
247+
*/
248+
public void testRFC9858Vector_3()
249+
throws Exception
250+
{
251+
checkRFC9858Vector("rfc9858_testcase_3.txt", LMSigParameters.lms_shake256_n32_h5, LMOtsParameters.shake256_n32_w8, true);
252+
}
253+
254+
/**
255+
* RFC 9858 Appendix A.4, Test Vector for SHA-256/192, W=4 (LMS_SHA256_M24_H20 / LMOTS_SHA256_N24_W4).
256+
* Verification only: regenerating the key means building the 2^20-leaf tree, which takes minutes
257+
* (a little over five here), too long for a unit test.
258+
* From https://www.rfc-editor.org/rfc/rfc9858#appendix-A.4
259+
*/
260+
public void testRFC9858Vector_4()
261+
throws Exception
262+
{
263+
checkRFC9858Vector("rfc9858_testcase_4.txt", LMSigParameters.lms_sha256_n24_h20, LMOtsParameters.sha256_n24_w4, false);
264+
}
265+
266+
/**
267+
* An RFC 9858 Appendix A vector is a single-level HSS key with its private SEED and I, a message and a
268+
* signature, all produced with the RFC 8554 Appendix A key derivation. Three things are checked: the RFC
269+
* signature verifies under the RFC public key; the key regenerated from SEED and I reproduces the RFC
270+
* public key; and signing the message at the signature's q reproduces the RFC signature byte for byte.
271+
*
272+
* @param regenerate whether to do the SEED/I regeneration and re-signing (building the tree costs
273+
* 2^h OTS key generations, so it can be skipped for a tall tree).
274+
*/
275+
private void checkRFC9858Vector(String vector, LMSigParameters sigParams, LMOtsParameters otsParams, boolean regenerate)
276+
throws Exception
277+
{
278+
List<byte[]> blocks = loadVector(vector);
279+
280+
byte[] seed = blocks.get(0);
281+
byte[] I = blocks.get(1);
282+
HSSPublicKeyParameters publicKey = HSSPublicKeyParameters.getInstance(blocks.get(2));
283+
byte[] message = blocks.get(3);
284+
byte[] signature = blocks.get(4);
285+
286+
assertEquals(vector + ": levels", 1, publicKey.getL());
287+
LMSPublicKeyParameters lmsPub = publicKey.getLMSPublicKey();
288+
assertEquals(vector + ": LMS type", sigParams, lmsPub.getSigParameters());
289+
assertEquals(vector + ": LM-OTS type", otsParams, lmsPub.getOtsParameters());
290+
assertTrue(vector + ": I", Arrays.areEqual(I, lmsPub.getI()));
291+
assertEquals(vector + ": SEED length", sigParams.getM(), seed.length);
292+
293+
assertTrue(vector + ": RFC signature verifies", verify(publicKey, signature, message));
294+
295+
// Nspk is 0, so the HSS signature is u32str(0) followed by the LMS signature, which opens with q.
296+
assertEquals(vector + ": Nspk", 0, Pack.bigEndianToInt(signature, 0));
297+
byte[] lmsSignature = Arrays.copyOfRange(signature, 4, signature.length);
298+
assertTrue(vector + ": RFC LMS signature verifies", verifyLms(lmsPub, lmsSignature, message));
299+
300+
if (!regenerate)
301+
{
302+
return;
303+
}
304+
305+
int q = Pack.bigEndianToInt(lmsSignature, 0);
306+
LMSPrivateKeyParameters privateKey = lmsKey(sigParams, otsParams, q, I, seed);
307+
assertTrue(vector + ": public key from SEED and I",
308+
Arrays.areEqual(lmsPub.getEncoded(), privateKey.getPublicKey().getEncoded()));
309+
310+
LMSSigner signer = new LMSSigner();
311+
signer.init(true, privateKey);
312+
assertTrue(vector + ": signature at q from SEED and I",
313+
Arrays.areEqual(lmsSignature, signer.generateSignature(message)));
314+
}
223315

224316
private List<byte[]> loadVector(String vector)
225317
throws Exception

0 commit comments

Comments
 (0)