From 9db40dc997b48b26896e1fa893b97ffed412578a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 20 Oct 2022 14:22:08 +0700 Subject: Refactoring in Pqc.Crypto.Lms (tests) --- crypto/test/src/pqc/crypto/lms/HSSTests.cs | 35 ---------------------- crypto/test/src/pqc/crypto/lms/LMSKeyGenTests.cs | 12 ++++---- crypto/test/src/pqc/crypto/lms/LMSTests.cs | 8 ++--- crypto/test/src/pqc/crypto/lms/LMSVectorUtils.cs | 5 ++-- crypto/test/src/pqc/crypto/lms/TypeTests.cs | 37 ++++++++++++------------ 5 files changed, 27 insertions(+), 70 deletions(-) (limited to 'crypto') diff --git a/crypto/test/src/pqc/crypto/lms/HSSTests.cs b/crypto/test/src/pqc/crypto/lms/HSSTests.cs index 240e29c77..691c51914 100644 --- a/crypto/test/src/pqc/crypto/lms/HSSTests.cs +++ b/crypto/test/src/pqc/crypto/lms/HSSTests.cs @@ -29,7 +29,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms FixedSecureRandom.Source[] source = {new FixedSecureRandom.Source(fixedSource)}; SecureRandom rand = new FixedSecureRandom(source); - HSSPrivateKeyParameters generatedPrivateKey = HSS.GenerateHssKeyPair( new HSSKeyGenerationParameters(new LMSParameters[] { @@ -45,7 +44,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms HSSPrivateKeyParameters reconstructedPrivateKey = HSSPrivateKeyParameters.GetInstance(keyPairEnc); Assert.True(reconstructedPrivateKey.Equals(generatedPrivateKey)); - reconstructedPrivateKey.GetPublicKey(); generatedPrivateKey.GetPublicKey(); @@ -60,15 +58,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // Assert.True(HSS.VerifySignature(reconstructedPrivateKey.GetPublicKey(), sigFromGeneratedPrivateKey, Hex.Decode("ABCDEF"))); - } - /** * Test Case 1 Signature * From https://tools.ietf.org/html/rfc8554#appendix-F - * - * @ */ [Test] public void TestHSSVector_1() @@ -84,8 +78,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms /** * Test Case 1 Signature * From https://tools.ietf.org/html/rfc8554#appendix-F - * - * @ */ [Test] public void TestHSSVector_2() @@ -133,12 +125,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return blocks; } - /** * Test the generation of public keys from private key SEED and I. * Level 0 - * - * @ */ [Test] public void TestGenPublicKeys_L0() @@ -156,8 +145,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms /** * Test the generation of public keys from private key SEED and I. * Level 1; - * - * @ */ [Test] public void TestGenPublicKeys_L1() @@ -180,7 +167,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // then use that to verify it compares with a value from the same reference implementation. // Then check components of it serialize and deserialize properly. // - byte[] fixedSource = new byte[8192]; for (int t = 0; t < fixedSource.Length; t++) { @@ -197,7 +183,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), }, rand)); - // // Generated from reference implementation. // check the encoded form of the public key matches. @@ -214,7 +199,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms Assert.True(keyPair.GetPublicKey().Equals(HSSPublicKeyParameters.GetInstance(Hex.Decode(expectedPk))), "HSSPrivateKeyParameterss equal are deserialization"); - // // Generate, hopefully the same HSSKetPair for the same entropy. // This is a sanity test @@ -230,7 +214,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), }, rand1)); - Assert.True( Arrays.AreEqual(regenKeyPair.GetPublicKey().GetEncoded(), keyPair.GetPublicKey().GetEncoded()), "Both generated keys are the same"); @@ -276,12 +259,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms }, rand1) ); - Assert.False( Arrays.AreEqual(differentKey.GetPublicKey().GetEncoded(), keyPair.GetPublicKey().GetEncoded()), "Both generated keys are not the same"); - for (int t = 0; t < keyPair.GetKeys().Count; t++) { // @@ -298,14 +279,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms LMSPrivateKeyParameters pk2O = LMSPrivateKeyParameters.GetInstance(pk2); Assert.False(pk1O.Equals(pk2O), "LmsPrivateKey not suddenly equal after deserialization"); - } - } - } - /** * This test takes in a series of vectors generated by adding print statements to code called by * the "test_sign.c" test in the reference implementation. @@ -410,7 +387,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms Assert.True(vectorSourcedPubKey.Equals(generatedPubKey)); } - // // Generate a signature using the keypair we generated. // @@ -440,12 +416,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // have value equality. Assert.True(signatureFromVector.Equals(sig)); - // // Other tests vandalise HSS signatures to check they Assert.Fail when tampered with // we won't do that again here. // - d = 0; lmOtsParameters.Clear(); lmsParameters.Clear(); @@ -627,10 +601,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms Assert.True(5 == keyPair.GetIndex()); // Next key is at index 5! - Assert.True(1024 - 5 == keyPair.GetUsagesRemaining()); - HSSPrivateKeyParameters shard = keyPair.ExtractKeyShard(10); Assert.True(15 == shard.IndexLimit); @@ -650,7 +622,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // We have used 32 keys. Assert.True(1024 - 32 == keyPair.GetUsagesRemaining()); - HSS.GenerateSignature(keyPair, Encoding.ASCII.GetBytes("Foo")); // @@ -750,7 +721,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms [Test] public void TestSignUnitExhaustion() { - HSSSecureRandom rand = new HSSSecureRandom(); HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( @@ -778,7 +748,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { while (ctr < 32769) // Just a number.. { - if (ctr % mod == 0) { // @@ -838,7 +807,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } - { // // Vandalise hss message @@ -849,7 +817,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms Assert.False(HSS.VerifySignature(pk, sig, newMsg)); } - { // // Vandalise public key @@ -867,7 +834,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } ctr++; - } //System.out.Println(ctr); @@ -879,7 +845,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms Assert.True(ctr == 32768); Assert.True(ex.Message.Contains("hss private key is exhausted")); } - } private static void Pack_Int32_To_BE(int n, byte[] bs, int off) diff --git a/crypto/test/src/pqc/crypto/lms/LMSKeyGenTests.cs b/crypto/test/src/pqc/crypto/lms/LMSKeyGenTests.cs index 30df5d5d1..4dd417d01 100644 --- a/crypto/test/src/pqc/crypto/lms/LMSKeyGenTests.cs +++ b/crypto/test/src/pqc/crypto/lms/LMSKeyGenTests.cs @@ -1,6 +1,7 @@ using System; + using NUnit.Framework; -using Org.BouncyCastle.Pqc.Crypto.Lms; + using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; @@ -40,7 +41,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms LMSPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); // From the vector. - String pkEnc = "0000000500000004215f83b7ccb9acbcd08db97b0d04dc2ba1cd035833e0e90059603f26e07ad2aad152338e7a5e5984bcd5f7bb4eba40b7"; + string pkEnc = "0000000500000004215f83b7ccb9acbcd08db97b0d04dc2ba1cd035833e0e90059603f26e07ad2aad152338e7a5e5984bcd5f7bb4eba40b7"; // Test public key encoded matched vector. Assert.True(Arrays.AreEqual(Hex.Decode(pkEnc), publicKey.GetEncoded())); @@ -53,7 +54,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms LMSSignature signature = LMS.GenerateSign(lmsPrivateKey, msg); // The expected signature as encoded. - String sigEnc = "00000004\n" + + string sigEnc = "00000004\n" + "00000004\n" + "0eb1ed54a2460d512388cad533138d24\n" + "0534e97b1e82d33bd927d201dfc24ebb\n" + @@ -137,14 +138,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms "e4041d95398a6f7f3e0ee97cc1591849\n" + "d4ed236338b147abde9f51ef9fd4e1c1"; - // Check generated signature matches vector. Assert.True(Arrays.AreEqual(Hex.Decode(sigEnc), signature.GetEncoded())); - // Sanity test Assert.True(LMS.VerifySignature(publicKey, signature, msg)); - } } -} \ No newline at end of file +} diff --git a/crypto/test/src/pqc/crypto/lms/LMSTests.cs b/crypto/test/src/pqc/crypto/lms/LMSTests.cs index 721d58160..7f7b4c089 100644 --- a/crypto/test/src/pqc/crypto/lms/LMSTests.cs +++ b/crypto/test/src/pqc/crypto/lms/LMSTests.cs @@ -1,13 +1,9 @@ using System; -using System.Reflection; + using NUnit.Framework; -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Parameters; -using Org.BouncyCastle.Crypto.Tests; -using Org.BouncyCastle.Pqc.Crypto.Lms; + using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; -using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Pqc.Crypto.Lms { diff --git a/crypto/test/src/pqc/crypto/lms/LMSVectorUtils.cs b/crypto/test/src/pqc/crypto/lms/LMSVectorUtils.cs index 821a86aba..642e29f2d 100644 --- a/crypto/test/src/pqc/crypto/lms/LMSVectorUtils.cs +++ b/crypto/test/src/pqc/crypto/lms/LMSVectorUtils.cs @@ -1,4 +1,5 @@ using System.IO; + using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Pqc.Crypto.Lms @@ -7,7 +8,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { public static byte[] ExtractPrefixedBytes(string vectorFromRFC) { - MemoryStream bos = new MemoryStream(); byte[] hexByte; foreach (string line in vectorFromRFC.Split('\n')) @@ -32,7 +32,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } return bos.ToArray(); - } } -} \ No newline at end of file +} diff --git a/crypto/test/src/pqc/crypto/lms/TypeTests.cs b/crypto/test/src/pqc/crypto/lms/TypeTests.cs index 28d76aa2b..ca64737b8 100644 --- a/crypto/test/src/pqc/crypto/lms/TypeTests.cs +++ b/crypto/test/src/pqc/crypto/lms/TypeTests.cs @@ -1,68 +1,67 @@ -using System; +using System.Collections.Generic; using NUnit.Framework; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - [TestFixture] public class TypeTests { - /** * Get instance methods are expected to return the instance passed to them if it is the same type. * * @throws Exception */ [Test] - public void TestTypeForType() { LMSSignature dummySig = new LMSSignature(0, null, null, null); - //todo - // { - // Object o = new HSSPrivateKeyParameters(0, - // new ArrayList(new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, new byte[32])), - // new ArrayList(dummySig), 1, 2); - // Assert.True(o.Equals(HSSPrivateKeyParameters.GetInstance(o))); - // } + { + var keys = new List(); + keys.Add(new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, new byte[32])); + + var sig = new List(); + sig.Add(dummySig); + + object o = new HSSPrivateKeyParameters(0, keys, sig, 1, 2); + Assert.True(o.Equals(HSSPrivateKeyParameters.GetInstance(o))); + } { - Object o = new HSSPublicKeyParameters(0, null); + object o = new HSSPublicKeyParameters(0, null); Assert.True(o.Equals(HSSPublicKeyParameters.GetInstance(o))); } { - Object o = new HSSSignature(0, null, null); + object o = new HSSSignature(0, null, null); Assert.True(o.Equals(HSSSignature.GetInstance(o, 0))); } { - Object o = new LMOtsPublicKey(null, null, 0, null); + object o = new LMOtsPublicKey(null, null, 0, null); Assert.True(o.Equals(LMOtsPublicKey.GetInstance(o))); } { - Object o = new LMOtsSignature(null, null, null); + object o = new LMOtsSignature(null, null, null); Assert.True(o.Equals(LMOtsSignature.GetInstance(o))); } { - Object o = new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, null); + object o = new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, null); Assert.True(o.Equals(LMSPrivateKeyParameters.GetInstance(o))); } { - Object o = new LMSPublicKeyParameters(null, null, null, null); + object o = new LMSPublicKeyParameters(null, null, null, null); Assert.True(o.Equals(LMSPublicKeyParameters.GetInstance(o))); } { - Object o = new LMSSignature(0, null, null, null); + object o = new LMSSignature(0, null, null, null); Assert.True(o.Equals(LMSSignature.GetInstance(o))); } - } } } -- cgit 1.4.1