diff --git a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs
index 37ac3b2cd..99b649e6a 100644
--- a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs
@@ -16,158 +16,135 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class PicnicVectorTest
{
- [Test]
- public void TestVectors()
+ private static readonly Dictionary<string, PicnicParameters> parameters = new Dictionary<string, PicnicParameters>()
{
- // bool full = System.getProperty("test.full", "false").equals("true");
- bool full = false;
- string[] files;
- PicnicParameters[] parameters;
- if (full)
+ { "picnicl1fs.rsp", PicnicParameters.picnicl1fs },
+ { "picnicl1ur.rsp", PicnicParameters.picnicl1ur },
+ { "picnicl3fs.rsp", PicnicParameters.picnicl3fs },
+ { "picnicl3ur.rsp", PicnicParameters.picnicl3ur },
+ { "picnicl5fs.rsp", PicnicParameters.picnicl5fs },
+ { "picnicl5ur.rsp", PicnicParameters.picnicl5ur },
+ { "picnic3l1.rsp", PicnicParameters.picnic3l1 },
+ { "picnic3l3.rsp", PicnicParameters.picnic3l3 },
+ { "picnic3l5.rsp", PicnicParameters.picnic3l5 },
+ { "picnicl1full.rsp", PicnicParameters.picnicl1full },
+ { "picnicl3full.rsp", PicnicParameters.picnicl3full },
+ { "picnicl5full.rsp", PicnicParameters.picnicl5full },
+ };
+
+ private static readonly string[] TestVectorFilesBasic =
{
- files = new []{
- "picnicl1fs.rsp",
- "picnicl1ur.rsp",
- "picnicl3fs.rsp",
- "picnicl3ur.rsp",
- "picnicl5fs.rsp",
- "picnicl5ur.rsp",
- "picnic3l1.rsp",
- "picnic3l3.rsp",
- "picnic3l5.rsp",
- "picnicl1full.rsp",
- "picnicl3full.rsp",
- "picnicl5full.rsp",
-
- };
- parameters = new []{
- PicnicParameters.picnicl1fs,
- PicnicParameters.picnicl1ur,
- PicnicParameters.picnicl3fs,
- PicnicParameters.picnicl3ur,
- PicnicParameters.picnicl5fs,
- PicnicParameters.picnicl5ur,
- PicnicParameters.picnic3l1,
- PicnicParameters.picnic3l3,
- PicnicParameters.picnic3l5,
- PicnicParameters.picnicl1full,
- PicnicParameters.picnicl3full,
- PicnicParameters.picnicl5full
- };
+ "picnicl1fs.rsp",
+ "picnicl3ur.rsp",
+ "picnic3l1.rsp",
+ "picnicl1full.rsp",
+ };
+
+ private static readonly string[] TestVectorFilesExtra =
+ {
+ "picnicl1ur.rsp",
+ "picnicl3fs.rsp",
+ "picnicl5fs.rsp",
+ "picnicl5ur.rsp",
+ "picnic3l3.rsp",
+ "picnic3l5.rsp",
+ "picnicl3full.rsp",
+ "picnicl5full.rsp",
+ };
+
+ [TestCaseSource(nameof(TestVectorFilesBasic))]
+ //[Parallelizable(ParallelScope.All)]
+ public void TestVectorsBasic(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
}
- else
+
+ [Explicit, TestCaseSource(nameof(TestVectorFilesExtra))]
+ //[Parallelizable(ParallelScope.All)]
+ public void TestVectorsExtra(string testVectorFile)
{
- files = new []{
- "picnicl1fs.rsp",
- "picnic3l1.rsp",
- "picnicl3ur.rsp",
- "picnicl1full.rsp",
- };
- parameters = new PicnicParameters[]{
- PicnicParameters.picnicl1fs,
- PicnicParameters.picnic3l1,
- PicnicParameters.picnicl3ur,
- PicnicParameters.picnicl1full,
- };
+ RunTestVectorFile(testVectorFile);
}
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] seed = Hex.Decode(buf["seed"]); // seed for picnic secure random
+ int mlen = int.Parse(buf["mlen"]); // message length
+ byte[] msg = Hex.Decode(buf["msg"]); // message
+ byte[] pk = Hex.Decode(buf["pk"]); // public key
+ byte[] sk = Hex.Decode(buf["sk"]); // private key
+ int smlen = int.Parse(buf["smlen"]); // signature length
+ byte[] sigExpected = Hex.Decode(buf["sm"]); // signature
+
+ NistSecureRandom random = new NistSecureRandom(seed, null);
+ PicnicParameters picnicParameters = parameters[name];
+
+ PicnicKeyPairGenerator kpGen = new PicnicKeyPairGenerator();
+ PicnicKeyGenerationParameters genParams = new PicnicKeyGenerationParameters(random, picnicParameters);
+
+ //
+ // Generate keys and test.
+ //
+ kpGen.Init(genParams);
+ AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+
+ PicnicPublicKeyParameters pubParams = (PicnicPublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public));
+ PicnicPrivateKeyParameters privParams = (PicnicPrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private));
+
+ Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key");
+ Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key");
+
+ //
+ // Signature test
+ //
+ PicnicSigner signer = new PicnicSigner(random);
+
+ signer.Init(true, privParams);
+ byte[] sigGenerated = signer.GenerateSignature(msg);
+ Assert.True(smlen == sigGenerated.Length, name + " " + count + ": signature length");
+
+ signer.Init(false, pubParams);
+ Assert.True(signer.VerifySignature(msg, sigGenerated), (name + " " + count + ": signature verify"));
+ Assert.True(Arrays.AreEqual(sigExpected, sigGenerated), name + " " + count + ": signature gen match");
+ }
- for (int fileIndex = 0; fileIndex != files.Length; fileIndex++)
+ private static void RunTestVectorFile(string name)
{
- String name = files[fileIndex];
- Console.Write("testing: " + name);
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.picnic." + name));
-
- String line = null;
- Dictionary<String, String> buf = new Dictionary<string, string>();
- // Random rnd = new Random();
- while ((line = src.ReadLine()) != null)
- {
- line = line.Trim();
+ var buf = new Dictionary<string, string>();
- if (line.StartsWith("#"))
- {
- continue;
- }
- if (line.Length == 0)
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.picnic." + name)))
+ {
+ string line;
+ while ((line = src.ReadLine()) != null)
{
- if (buf.Count > 0)
+ line = line.Trim();
+ if (line.StartsWith("#"))
+ continue;
+
+ if (line.Length > 0)
{
- String count = buf["count"];
- if (!"0".Equals(count))
+ int a = line.IndexOf('=');
+ if (a > -1)
{
- // randomly skip tests after zero.
- // if (rnd.NextDouble())
- // {
- // continue;
- // }
+ buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
}
- Console.Write($"test case: {count}\n");
- byte[] seed = Hex.Decode(buf["seed"]); // seed for picnic secure random
- int mlen = Int32.Parse(buf["mlen"]); // message length
- byte[] msg = Hex.Decode(buf["msg"]); // message
- byte[] pk = Hex.Decode(buf["pk"]); // public key
- byte[] sk = Hex.Decode(buf["sk"]); // private key
- int smlen = Int32.Parse(buf["smlen"]); // signature length
- byte[] sigExpected = Hex.Decode(buf["sm"]); // signature
-
-// System.out.println("message: " + Hex.toHexString(msg));
- NistSecureRandom random = new NistSecureRandom(seed, null);
- PicnicParameters picnicParameters = parameters[fileIndex];
-
-
- PicnicKeyPairGenerator kpGen = new PicnicKeyPairGenerator();
- PicnicKeyGenerationParameters genParams = new PicnicKeyGenerationParameters(random, picnicParameters);
- //
- // Generate keys and test.
- //
- kpGen.Init(genParams);
- AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
-
-
- PicnicPublicKeyParameters pubParams = (PicnicPublicKeyParameters) PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public));
- PicnicPrivateKeyParameters privParams = (PicnicPrivateKeyParameters) PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private));
-
-// System.out.println("pk = " + Hex.toHexString(pubParams.getEncoded()).toUpperCase());
-// System.out.println("sk = " + Hex.toHexString(privParams.getEncoded()).toUpperCase());
-
- Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key");
- Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key");
-
-
- //
- // Signature test
- //
- PicnicSigner signer = new PicnicSigner(random);
-
- signer.Init(true, privParams);
-
- byte[] sigGenerated = signer.GenerateSignature(msg);
-
- // Console.WriteLine("expected:\t" + Hex.ToHexString(sigExpected));
- // Console.WriteLine("generated:\t" + Hex.ToHexString(sigGenerated));
-
- Assert.True(smlen == sigGenerated.Length, name + " " + count + ": signature length");
-
- signer.Init(false, pubParams);
-
- Assert.True(signer.VerifySignature(msg, sigGenerated), (name + " " + count + ": signature verify"));
- Assert.True(Arrays.AreEqual(sigExpected, sigGenerated), name + " " + count + ": signature gen match");
-
+ continue;
}
- buf.Clear();
- continue;
+ if (buf.Count > 0)
+ {
+ RunTestVector(name, buf);
+ buf.Clear();
+ }
}
- int a = line.IndexOf('=');
- if (a > -1)
+ if (buf.Count > 0)
{
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ RunTestVector(name, buf);
}
}
- Console.Write("testing successful!");
}
}
- }
-}
\ No newline at end of file
+}
diff --git a/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs b/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs
index 4a93a3c3c..19eac4b5d 100644
--- a/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs
+++ b/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs
@@ -1,5 +1,4 @@
using System;
-using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -25,159 +24,51 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class SphincsPlusTest
{
- [Test]
- public void TestVectors()
+ private static readonly string[] TestVectorFilesRobust =
{
+ "sha2-128f-robust.rsp",
+ "sha2-192f-robust.rsp",
+ "sha2-256f-robust.rsp",
+ "shake-128f-robust.rsp",
+ "shake-192f-robust.rsp",
+ "shake-256f-robust.rsp",
+ "sha2-128s-robust.rsp",
+ "sha2-192s-robust.rsp",
+ "sha2-256s-robust.rsp",
+ "shake-128s-robust.rsp",
+ "shake-192s-robust.rsp",
+ "shake-256s-robust.rsp",
+ };
+ private static readonly string[] TestVectorFilesSimple =
+ {
+ "sha2-128f-simple.rsp",
+ "sha2-192f-simple.rsp",
+ "sha2-256f-simple.rsp",
+ "shake-128f-simple.rsp",
+ "shake-192f-simple.rsp",
+ "shake-256f-simple.rsp",
+ "sha2-128s-simple.rsp",
+ "sha2-192s-simple.rsp",
+ "sha2-256s-simple.rsp",
+ "shake-128s-simple.rsp",
+ "shake-192s-simple.rsp",
+ "shake-256s-simple.rsp",
+ };
+
+ [Explicit, TestCaseSource(nameof(TestVectorFilesRobust))]
+ [Parallelizable(ParallelScope.All)]
+ public void TestVectorsRobust(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
+ }
- // bool full = System.GetProperty("test.full","false").equals("true");
- bool full = false;
-
- string files = "sha2-128f-robust.rsp sha2-192f-robust.rsp sha2-256f-robust.rsp shake-128f-robust.rsp shake-192f-robust.rsp" +
- " shake-256f-robust.rsp sha2-128f-simple.rsp sha2-192f-simple.rsp sha2-256f-simple.rsp shake-128f-simple.rsp" +
- " shake-192f-simple.rsp shake-256f-simple.rsp sha2-128s-robust.rsp sha2-192s-robust.rsp sha2-256s-robust.rsp" +
- " shake-128s-robust.rsp shake-192s-robust.rsp shake-256s-robust.rsp sha2-128s-simple.rsp sha2-192s-simple.rsp" +
- " sha2-256s-simple.rsp shake-128s-simple.rsp shake-192s-simple.rsp shake-256s-simple.rsp";
-
-
- string[] fileList = splitOn(files, ' ');
- for (int i = 0; i != fileList.Length; i++)
- {
- string name = fileList[i];
- if ( full || name.Contains("-128s-") || name.Contains("-128f-"))
- {
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.sphincsplus.subset_" + name));
-
- // BufferedReader bin = new BufferedReader(new InputStreamReader(src));
-
- string line = null;
- Dictionary<string, string> buf = new Dictionary<string, string>();
- while ((line = src.ReadLine()) != null)
- {
-
- line = line.Trim();
-
- if (line.StartsWith("#"))
- {
- continue;
- }
- if (line.Length == 0)
- {
- if (buf.Count > 0)
- {
- string count = buf["count"];
- byte[] sk = Hex.Decode(buf["sk"]);
- byte[] pk = Hex.Decode(buf["pk"]);
- byte[] msg = Hex.Decode(buf["msg"]);
- byte[] sigExpected = Hex.Decode(buf["sm"]);
- byte[] oprR = Hex.Decode(buf["optrand"]);
-
- SPHINCSPlusKeyPairGenerator kpGen = new SPHINCSPlusKeyPairGenerator();
-
- FixedSecureRandom.Source[] source = {new FixedSecureRandom.Source(sk)};
- SecureRandom random = new FixedSecureRandom(source);
-
- SPHINCSPlusParameters parameters;
-
- string[] nameParts = splitOn(name, '-');
- bool sha2 = nameParts[0].Equals("sha2");
- bool shake = nameParts[0].Equals("shake");
- int size = Int32.Parse(nameParts[1].Substring(0, 3));
- bool fast = nameParts[1].EndsWith("f");
- bool slow = nameParts[1].EndsWith("s");
- bool simple = nameParts[2].Equals("simple.rsp");
- bool robust = nameParts[2].Equals("robust.rsp");
-
- StringBuilder b = new StringBuilder();
- if (sha2)
- {
- b.Append("sha2");
- }
- else if (shake)
- {
- b.Append("shake");
- }
- else
- {
- throw new ArgumentException("unknown digest");
- }
-
- b.Append("_");
- b.Append(size);
-
- if (fast)
- {
- b.Append("f");
- }
- else if (slow)
- {
- b.Append("s");
- }
- else
- {
- throw new ArgumentException("unknown speed");
- }
-
- if (robust)
- {
- // nothing.
- }
- else if (simple)
- {
- b.Append("_simple");
- }
- else
- {
- throw new ArgumentException("unknown complexity");
- }
-
- parameters = (SPHINCSPlusParameters)typeof(SPHINCSPlusParameters).GetField(b.ToString()).GetValue(null);//todo unsure
-
- //
- // Generate keys and test.
- //
- kpGen.Init(new SPHINCSPlusKeyGenerationParameters(random, parameters));
- AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
-
- SPHINCSPlusPublicKeyParameters pubParams = (SPHINCSPlusPublicKeyParameters)kp.Public;
- SPHINCSPlusPrivateKeyParameters privParams = (SPHINCSPlusPrivateKeyParameters)kp.Private;
-
- Assert.True(Arrays.AreEqual(Arrays.Concatenate(pubParams.GetParameters().GetEncoded(), pk), pubParams.GetEncoded()), name + " " + count + ": public key");
- Assert.True(Arrays.AreEqual(Arrays.Concatenate(privParams.GetParameters().GetEncoded(), sk), privParams.GetEncoded()), name + " " + count + ": secret key");
-
- //
- // Signature test
- //
-
- SPHINCSPlusSigner signer = new SPHINCSPlusSigner();
-
- FixedSecureRandom.Source[] s1 = {new FixedSecureRandom.Source(oprR)};
- signer.Init(true, new ParametersWithRandom(privParams, new FixedSecureRandom(s1)));
-
- byte[] sigGenerated = signer.GenerateSignature(msg);
- byte[] attachedSig = Arrays.Concatenate(sigGenerated, msg);
-
-
- signer.Init(false, pubParams);
-
- Assert.True(signer.VerifySignature(msg, sigGenerated), name + " " + count + ": signature verify");
- Assert.True(Arrays.AreEqual(sigExpected, attachedSig), name + " " + count + ": signature gen match");
- }
- buf.Clear();
-
- continue;
- }
-
- int a = line.IndexOf("=");
- if (a > -1)
- {
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
- }
- }
- src.Close();
- }
- }
+ [TestCaseSource(nameof(TestVectorFilesSimple))]
+ [Parallelizable(ParallelScope.All)]
+ public void TestVectorsSimple(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
}
-
+
[Test]
public void TestBasicKeyGeneration()
{
@@ -425,10 +316,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
Assert.True(signer.VerifySignature(msg, sig));
}
- private static string[] splitOn(string input, char c)
+ private static string[] SplitOn(string input, char c)
{
string s = input.Trim();
- ArrayList l = new ArrayList();
+ var l = new List<string>();
int idx = s.IndexOf(c);
while (idx > 0)
@@ -442,8 +333,146 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
{
l.Add(s);
}
-
- return l.ToArray(typeof(string)) as string[];
+
+ return l.ToArray();
+ }
+
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] sk = Hex.Decode(buf["sk"]);
+ byte[] pk = Hex.Decode(buf["pk"]);
+ byte[] msg = Hex.Decode(buf["msg"]);
+ byte[] sigExpected = Hex.Decode(buf["sm"]);
+ byte[] oprR = Hex.Decode(buf["optrand"]);
+
+ SPHINCSPlusKeyPairGenerator kpGen = new SPHINCSPlusKeyPairGenerator();
+
+ FixedSecureRandom.Source[] source = { new FixedSecureRandom.Source(sk) };
+ SecureRandom random = new FixedSecureRandom(source);
+
+ SPHINCSPlusParameters parameters;
+
+ string[] nameParts = SplitOn(name, '-');
+ bool sha2 = nameParts[0].Equals("sha2");
+ bool shake = nameParts[0].Equals("shake");
+ int size = Int32.Parse(nameParts[1].Substring(0, 3));
+ bool fast = nameParts[1].EndsWith("f");
+ bool slow = nameParts[1].EndsWith("s");
+ bool simple = nameParts[2].Equals("simple.rsp");
+ bool robust = nameParts[2].Equals("robust.rsp");
+
+ StringBuilder b = new StringBuilder();
+ if (sha2)
+ {
+ b.Append("sha2");
+ }
+ else if (shake)
+ {
+ b.Append("shake");
+ }
+ else
+ {
+ throw new ArgumentException("unknown digest");
+ }
+
+ b.Append("_");
+ b.Append(size);
+
+ if (fast)
+ {
+ b.Append("f");
+ }
+ else if (slow)
+ {
+ b.Append("s");
+ }
+ else
+ {
+ throw new ArgumentException("unknown speed");
+ }
+
+ if (robust)
+ {
+ // nothing.
+ }
+ else if (simple)
+ {
+ b.Append("_simple");
+ }
+ else
+ {
+ throw new ArgumentException("unknown complexity");
+ }
+
+ parameters = (SPHINCSPlusParameters)typeof(SPHINCSPlusParameters).GetField(b.ToString()).GetValue(null);//todo unsure
+
+ //
+ // Generate keys and test.
+ //
+ kpGen.Init(new SPHINCSPlusKeyGenerationParameters(random, parameters));
+ AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+ SPHINCSPlusPublicKeyParameters pubParams = (SPHINCSPlusPublicKeyParameters)kp.Public;
+ SPHINCSPlusPrivateKeyParameters privParams = (SPHINCSPlusPrivateKeyParameters)kp.Private;
+
+ Assert.True(Arrays.AreEqual(Arrays.Concatenate(pubParams.GetParameters().GetEncoded(), pk), pubParams.GetEncoded()), name + " " + count + ": public key");
+ Assert.True(Arrays.AreEqual(Arrays.Concatenate(privParams.GetParameters().GetEncoded(), sk), privParams.GetEncoded()), name + " " + count + ": secret key");
+
+ //
+ // Signature test
+ //
+
+ SPHINCSPlusSigner signer = new SPHINCSPlusSigner();
+
+ FixedSecureRandom.Source[] s1 = { new FixedSecureRandom.Source(oprR) };
+ signer.Init(true, new ParametersWithRandom(privParams, new FixedSecureRandom(s1)));
+
+ byte[] sigGenerated = signer.GenerateSignature(msg);
+ byte[] attachedSig = Arrays.Concatenate(sigGenerated, msg);
+
+
+ signer.Init(false, pubParams);
+
+ Assert.True(signer.VerifySignature(msg, sigGenerated), name + " " + count + ": signature verify");
+ Assert.True(Arrays.AreEqual(sigExpected, attachedSig), name + " " + count + ": signature gen match");
+ }
+
+ private static void RunTestVectorFile(string name)
+ {
+ var buf = new Dictionary<string, string>();
+
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.sphincsplus.subset_" + name)))
+ {
+ string line;
+ while ((line = src.ReadLine()) != null)
+ {
+ line = line.Trim();
+ if (line.StartsWith("#"))
+ continue;
+
+ if (line.Length > 0)
+ {
+ int a = line.IndexOf("=");
+ if (a > -1)
+ {
+ buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ }
+ continue;
+ }
+
+ if (buf.Count > 0)
+ {
+ RunTestVector(name, buf);
+ buf.Clear();
+ }
+ }
+
+ if (buf.Count > 0)
+ {
+ RunTestVector(name, buf);
+ }
+ }
}
}
-}
\ No newline at end of file
+}
|