diff --git a/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs b/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs
index a25a27fe1..99201901c 100644
--- a/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs
@@ -26,7 +26,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
{
"PQCkemKAT_BIKE_3114.rsp",
"PQCkemKAT_BIKE_6198.rsp",
- "PQCkemKAT_BIKE_10276.rsp"
+ "PQCkemKAT_BIKE_10276.rsp",
};
[Test]
@@ -39,7 +39,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestCaseSource(nameof(TestVectorFiles))]
[Parallelizable(ParallelScope.All)]
- public void TestVectors(string testVectorFile)
+ public void TV(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
@@ -114,16 +114,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
continue;
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs b/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs
index 16b141478..eefdd96df 100644
--- a/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs
@@ -1,4 +1,3 @@
-using System;
using System.Collections.Generic;
using System.IO;
@@ -37,15 +36,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
private static readonly string[] TestVectorFiles =
{
"3488-64-cmce.txt",
- "3488-64-f-cmce.txt",
"4608-96-cmce.txt",
- "4608-96-f-cmce.txt",
"6688-128-cmce.txt",
- "6688-128-f-cmce.txt",
"6960-119-cmce.txt",
- "6960-119-f-cmce.txt",
"8192-128-cmce.txt",
- "8192-128-f-cmce.txt"
+ };
+
+ private static readonly string[] TestVectorFilesFast =
+ {
+ "3488-64-f-cmce.txt",
+ "4608-96-f-cmce.txt",
+ "6688-128-f-cmce.txt",
+ "6960-119-f-cmce.txt",
+ "8192-128-f-cmce.txt",
};
[Test]
@@ -65,7 +68,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestCaseSource(nameof(TestVectorFiles))]
[Parallelizable(ParallelScope.All)]
- public void TestVectors(string testVectorFile)
+ public void TV(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
+ }
+
+ [TestCaseSource(nameof(TestVectorFilesFast))]
+ [Parallelizable(ParallelScope.All)]
+ public void TVFast(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
@@ -137,16 +147,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
continue;
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs b/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs
index 2d682179c..e1121a5e3 100644
--- a/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs
+++ b/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs
@@ -15,34 +15,45 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class CrystalsDilithiumTest
{
- private static readonly Dictionary<string, DilithiumParameters> parameters = new Dictionary<string, DilithiumParameters>()
+ private static readonly Dictionary<string, DilithiumParameters> Parameters = new Dictionary<string, DilithiumParameters>()
{
{ "PQCsignKAT_Dilithium2.rsp", DilithiumParameters.Dilithium2 },
{ "PQCsignKAT_Dilithium3.rsp", DilithiumParameters.Dilithium3 },
{ "PQCsignKAT_Dilithium5.rsp", DilithiumParameters.Dilithium5 },
{ "PQCsignKAT_Dilithium2-AES.rsp", DilithiumParameters.Dilithium2Aes },
{ "PQCsignKAT_Dilithium3-AES.rsp", DilithiumParameters.Dilithium3Aes },
- { "PQCsignKAT_Dilithium5-AES.rsp", DilithiumParameters.Dilithium5Aes }
+ { "PQCsignKAT_Dilithium5-AES.rsp", DilithiumParameters.Dilithium5Aes },
};
- private static readonly string[] TestVectorFilesBasic =
+ private static readonly string[] TestVectorFiles =
{
"PQCsignKAT_Dilithium2.rsp",
"PQCsignKAT_Dilithium3.rsp",
"PQCsignKAT_Dilithium5.rsp",
+ };
+
+ private static readonly string[] TestVectorFilesAes =
+ {
"PQCsignKAT_Dilithium2-AES.rsp",
"PQCsignKAT_Dilithium3-AES.rsp",
"PQCsignKAT_Dilithium5-AES.rsp",
};
- [TestCaseSource(nameof(TestVectorFilesBasic))]
+ [TestCaseSource(nameof(TestVectorFiles))]
+ [Parallelizable(ParallelScope.All)]
+ public void TV(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
+ }
+
+ [TestCaseSource(nameof(TestVectorFilesAes))]
[Parallelizable(ParallelScope.All)]
- public void TestVectorsBasic(string testVectorFile)
+ public void TVAes(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
- private static void TestVectors(string name, IDictionary<string, string> buf)
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
{
string count = buf["count"];
byte[] seed = Hex.Decode(buf["seed"]); // seed for SecureRandom
@@ -54,10 +65,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
byte[] sm = Hex.Decode(buf["sm"]); // signature
NistSecureRandom random = new NistSecureRandom(seed, null);
- DilithiumParameters dilithiumparameters = parameters[name];
+ DilithiumParameters dilithiumparameters = Parameters[name];
DilithiumKeyPairGenerator kpGen = new DilithiumKeyPairGenerator();
- DilithiumKeyGenerationParameters genParams = new DilithiumKeyGenerationParameters(random, dilithiumparameters);
+ DilithiumKeyGenerationParameters genParams =
+ new DilithiumKeyGenerationParameters(random, dilithiumparameters);
//
// Generate keys and test.
@@ -66,8 +78,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
AsymmetricCipherKeyPair ackp = kpGen.GenerateKeyPair();
- DilithiumPublicKeyParameters pubParams = (DilithiumPublicKeyParameters) ackp.Public;
- DilithiumPrivateKeyParameters privParams = (DilithiumPrivateKeyParameters) ackp.Private;
+ DilithiumPublicKeyParameters pubParams = (DilithiumPublicKeyParameters)ackp.Public;
+ DilithiumPrivateKeyParameters privParams = (DilithiumPrivateKeyParameters)ackp.Private;
//Console.WriteLine(string.Format("{0} Expected pk = {1}", pk.Length, Convert.ToHexString(pk)));
//Console.WriteLine(String.Format("{0} Actual Public key = {1}", pubParams.GetEncoded().Length, Convert.ToHexString(pubParams.GetEncoded())));
@@ -105,19 +117,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
Assert.False(vrfyresfail, name + " " + count + " verify passed when should fail");
}
- private static byte[] UInt32_To_LE(uint n)
- {
- byte[] bs = new byte[4];
- bs[0] = (byte)(n);
- bs[1] = (byte)(n >> 8);
- bs[2] = (byte)(n >> 16);
- bs[3] = (byte)(n >> 24);
- return bs;
- }
public static void RunTestVectorFile(string name)
{
var buf = new Dictionary<string, string>();
-
+ TestSampler sampler = new TestSampler();
using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.crystals.dilithium." + name)))
{
string line;
@@ -139,14 +142,21 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
if (buf.Count > 0)
{
- TestVectors(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
if (buf.Count > 0)
{
- TestVectors(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs b/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs
index ea1423192..44ad1cbe9 100644
--- a/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs
+++ b/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs
@@ -1,7 +1,8 @@
-using System;
using System.Collections.Generic;
using System.IO;
+
using NUnit.Framework;
+
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber;
using Org.BouncyCastle.Utilities;
@@ -13,37 +14,47 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class CrystalsKyberTest
{
- private static readonly Dictionary<string, KyberParameters> parameters = new Dictionary<string, KyberParameters>()
+ private static readonly Dictionary<string, KyberParameters> Parameters = new Dictionary<string, KyberParameters>()
{
{ "kyber512.rsp", KyberParameters.kyber512 },
{ "kyber768.rsp", KyberParameters.kyber768 },
{ "kyber1024.rsp", KyberParameters.kyber1024 },
{ "kyber512aes.rsp", KyberParameters.kyber512_aes },
{ "kyber768aes.rsp", KyberParameters.kyber768_aes },
- { "kyber1024aes.rsp", KyberParameters.kyber1024_aes }
+ { "kyber1024aes.rsp", KyberParameters.kyber1024_aes },
};
-
- private static readonly string[] TestVectorFilesBasic =
+
+ private static readonly string[] TestVectorFiles =
{
"kyber512.rsp",
"kyber768.rsp",
"kyber1024.rsp",
+ };
+
+ private static readonly string[] TestVectorFilesAes =
+ {
"kyber512aes.rsp",
"kyber768aes.rsp",
"kyber1024aes.rsp",
};
-
- [TestCaseSource(nameof(TestVectorFilesBasic))]
+
+ [TestCaseSource(nameof(TestVectorFiles))]
[Parallelizable(ParallelScope.All)]
- public void TestVectorsBasic(string testVectorFile)
+ public void TV(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
-
- private static void TestVectors(string name, IDictionary<string, string> buf)
+
+ [TestCaseSource(nameof(TestVectorFilesAes))]
+ [Parallelizable(ParallelScope.All)]
+ public void TVAes(string testVectorFile)
{
- string count = buf["count"];
+ RunTestVectorFile(testVectorFile);
+ }
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
byte[] seed = Hex.Decode(buf["seed"]); // seed for SecureRandom
byte[] pk = Hex.Decode(buf["pk"]); // public key
byte[] sk = Hex.Decode(buf["sk"]); // private key
@@ -51,11 +62,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
byte[] ss = Hex.Decode(buf["ss"]); // session key
NistSecureRandom random = new NistSecureRandom(seed, null);
- KyberParameters kyberparameters = CrystalsKyberTest.parameters[name];
+ KyberParameters kyberparameters = Parameters[name];
KyberKeyPairGenerator kpGen = new KyberKeyPairGenerator();
- KyberKeyGenerationParameters
- genParam = new KyberKeyGenerationParameters(random, kyberparameters);
+ KyberKeyGenerationParameters genParam = new KyberKeyGenerationParameters(random, kyberparameters);
//
// Generate keys and test.
@@ -85,13 +95,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
Assert.True(Arrays.AreEqual(dec_key, ss), name + " " + count + ": kem_dec ss");
Assert.True(Arrays.AreEqual(dec_key, secret), name + " " + count + ": kem_dec key");
-
}
-
+
public static void RunTestVectorFile(string name)
{
var buf = new Dictionary<string, string>();
-
+ TestSampler sampler = new TestSampler();
using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.crystals.kyber." + name)))
{
string line;
@@ -113,14 +122,21 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
if (buf.Count > 0)
{
- TestVectors(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
if (buf.Count > 0)
{
- TestVectors(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/FalconTest.cs b/crypto/test/src/pqc/crypto/test/FalconTest.cs
index 2586e5cb5..b9859759c 100644
--- a/crypto/test/src/pqc/crypto/test/FalconTest.cs
+++ b/crypto/test/src/pqc/crypto/test/FalconTest.cs
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
+
using NUnit.Framework;
+
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Pqc.Crypto.Falcon;
@@ -14,100 +16,130 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class FalconTest
{
+ private static readonly Dictionary<string, FalconParameters> Parameters = new Dictionary<string, FalconParameters>()
+ {
+ { "falcon512-KAT.rsp", FalconParameters.falcon_512 },
+ { "falcon1024-KAT.rsp", FalconParameters.falcon_1024 },
+ };
+
+ private static readonly string[] TestVectorFiles =
+ {
+ "falcon512-KAT.rsp",
+ "falcon1024-KAT.rsp",
+ };
+
[Test]
- public void TestVectors() {
- string[] files = {
- "falcon512-KAT.rsp",
- "falcon1024-KAT.rsp"
- };
- FalconParameters[] parameters = new FalconParameters[]{
- FalconParameters.falcon_512,
- FalconParameters.falcon_1024
- };
+ public void TestParameters()
+ {
+ Assert.AreEqual(9, FalconParameters.falcon_512.LogN);
+ Assert.AreEqual(10, FalconParameters.falcon_1024.LogN);
+ }
+
+ [TestCaseSource(nameof(TestVectorFiles))]
+ [Parallelizable(ParallelScope.All)]
+ public void TV(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
+ }
+
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] seed = Hex.Decode(buf["seed"]); // seed for SecureRandom
+ byte[] pk = Hex.Decode(buf["pk"]); // public key
+ byte[] sk = Hex.Decode(buf["sk"]); // private key
+ byte[] sm = Hex.Decode(buf["sm"]); // sm
+ byte[] msg = Hex.Decode(buf["msg"]); // message
+ uint m_len = uint.Parse(buf["mlen"]); // message length
+ uint sm_len = uint.Parse(buf["smlen"]); // sm length
+
+ NistSecureRandom random = new NistSecureRandom(seed, null);
+ FalconParameters falconParameters = Parameters[name];
+ // keygen
+ FalconKeyGenerationParameters kparam = new FalconKeyGenerationParameters(random, falconParameters);
+ FalconKeyPairGenerator kpg = new FalconKeyPairGenerator();
+ kpg.Init(kparam);
+ AsymmetricCipherKeyPair ackp = kpg.GenerateKeyPair();
+ byte[] respk = ((FalconPublicKeyParameters)ackp.Public).GetEncoded();
+ byte[] ressk = ((FalconPrivateKeyParameters)ackp.Private).GetEncoded();
+
+ //keygen
+ Assert.True(Arrays.AreEqual(respk, 0, respk.Length, pk, 1, pk.Length), name + " " + count + " public key");
+ Assert.True(Arrays.AreEqual(ressk, 0, ressk.Length, sk, 1, sk.Length), name + " " + count + " private key");
+
+ // sign
+ FalconSigner signer = new FalconSigner();
+ ParametersWithRandom skwrand = new ParametersWithRandom(ackp.Private, random);
+ signer.Init(true, skwrand);
+ byte[] sig = signer.GenerateSignature(msg);
+ byte[] ressm = new byte[2 + msg.Length + sig.Length - 1];
+ ressm[0] = (byte)((sig.Length - 40 - 1) >> 8);
+ ressm[1] = (byte)(sig.Length - 40 - 1);
+ Array.Copy(sig, 1, ressm, 2, 40);
+ Array.Copy(msg, 0, ressm, 2 + 40, msg.Length);
+ Array.Copy(sig, 40 + 1, ressm, 2 + 40 + msg.Length, sig.Length - 40 - 1);
+
+ // verify
+ FalconSigner verifier = new FalconSigner();
+ FalconPublicKeyParameters pkparam = (FalconPublicKeyParameters)ackp.Public;
+ verifier.Init(false, pkparam);
+ byte[] noncesig = new byte[sm_len - m_len - 2 + 1];
+ noncesig[0] = (byte)(0x30 + falconParameters.LogN);
+ Array.Copy(sm, 2, noncesig, 1, 40);
+ Array.Copy(sm, 2 + 40 + m_len, noncesig, 40 + 1, sm_len - 2 - 40 - m_len);
+ bool vrfyrespass = verifier.VerifySignature(msg, noncesig);
+ noncesig[42]++; // changing the signature by 1 byte should cause it to fail
+ bool vrfyresfail = verifier.VerifySignature(msg, noncesig);
+
+ //sign
+ Assert.True(Arrays.AreEqual(ressm, sm), name + " " + count + " signature");
+ //verify
+ Assert.True(vrfyrespass, name + " " + count + " verify failed when should pass");
+ Assert.False(vrfyresfail, name + " " + count + " verify passed when should fail");
+ }
+
+ private static void RunTestVectorFile(string name)
+ {
+ var buf = new Dictionary<string, string>();
TestSampler sampler = new TestSampler();
- for (int fileIndex = 0; fileIndex < files.Length; fileIndex++) {
- string name = files[fileIndex];
- Console.Write("testing: " + name);
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.falcon." + name));
- string line = null;
- Dictionary<string, string> buf = new Dictionary<string, string>();
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.falcon." + 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 (line.Length == 0)
+
+ if (buf.Count > 0)
{
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (!sampler.SkipTest(buf["count"]))
{
- string count = buf["count"];
- Console.Write("test case: " + count);
- byte[] seed = Hex.Decode(buf["seed"]); // seed for SecureRandom
- byte[] pk = Hex.Decode(buf["pk"]); // public key
- byte[] sk = Hex.Decode(buf["sk"]); // private key
- byte[] sm = Hex.Decode(buf["sm"]); // sm
- byte[] msg = Hex.Decode(buf["msg"]); // message
- uint m_len = uint.Parse(buf["mlen"]); // message length
- uint sm_len = uint.Parse(buf["smlen"]); // sm length
-
- NistSecureRandom random = new NistSecureRandom(seed, null);
-
- // keygen
- FalconKeyGenerationParameters kparam = new FalconKeyGenerationParameters(random, parameters[fileIndex]);
- FalconKeyPairGenerator kpg = new FalconKeyPairGenerator();
- kpg.Init(kparam);
- AsymmetricCipherKeyPair ackp = kpg.GenerateKeyPair();
- byte[] respk = ((FalconPublicKeyParameters) ackp.Public).GetEncoded();
- byte[] ressk = ((FalconPrivateKeyParameters) ackp.Private).GetEncoded();
-
- //keygen
- Assert.True(Arrays.AreEqual(respk, 0, respk.Length, pk, 1, pk.Length), name + " " + count + " public key");
- Assert.True(Arrays.AreEqual(ressk, 0, ressk.Length, sk, 1, sk.Length), name + " " + count + " private key");
-
- // sign
- FalconSigner signer = new FalconSigner();
- ParametersWithRandom skwrand = new ParametersWithRandom(ackp.Private, random);
- signer.Init(true, skwrand);
- byte[] sig = signer.GenerateSignature(msg);
- byte[] ressm = new byte[2 + msg.Length + sig.Length - 1];
- ressm[0] = (byte)((sig.Length - 40 - 1) >> 8);
- ressm[1] = (byte)(sig.Length - 40 - 1);
- Array.Copy(sig, 1, ressm, 2, 40);
- Array.Copy(msg, 0, ressm, 2 + 40, msg.Length);
- Array.Copy(sig, 40 + 1, ressm, 2 + 40 + msg.Length, sig.Length - 40 - 1);
-
- // verify
- FalconSigner verifier = new FalconSigner();
- FalconPublicKeyParameters pkparam = (FalconPublicKeyParameters)ackp.Public;
- verifier.Init(false, pkparam);
- byte[] noncesig = new byte[sm_len - m_len - 2 + 1];
- noncesig[0] = (byte)(0x30 + parameters[fileIndex].LogN);
- Array.Copy(sm, 2, noncesig, 1, 40);
- Array.Copy(sm, 2 + 40 + m_len, noncesig, 40 + 1, sm_len - 2 - 40 - m_len);
- bool vrfyrespass = verifier.VerifySignature(msg, noncesig);
- noncesig[42]++; // changing the signature by 1 byte should cause it to fail
- bool vrfyresfail = verifier.VerifySignature(msg, noncesig);
-
- //sign
- Assert.True(Arrays.AreEqual(ressm, sm), name + " " + count + " signature");
- //verify
- Assert.True(vrfyrespass, name + " " + count + " verify failed when should pass");
- Assert.False(vrfyresfail, name + " " + count + " verify passed when should fail");
+ RunTestVector(name, buf);
}
buf.Clear();
-
- continue;
}
- int a = line.IndexOf("=");
- if (a > -1) {
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ }
+
+ if (buf.Count > 0)
+ {
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
}
+ buf.Clear();
}
- Console.Write("testing successful!");
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs b/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs
index db72fb225..f7397c442 100644
--- a/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs
@@ -36,7 +36,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
{
"PQCkemKAT_19888_shake.rsp",
"PQCkemKAT_31296_shake.rsp",
- "PQCkemKAT_43088_shake.rsp"
+ "PQCkemKAT_43088_shake.rsp",
};
[Test]
@@ -131,16 +131,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
continue;
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs b/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs
index 0c47d42ce..ea2156e40 100644
--- a/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs
+++ b/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs
@@ -1,7 +1,7 @@
using System;
+
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
-using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Test;
namespace Org.BouncyCastle.Pqc.Crypto.Tests
@@ -14,7 +14,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
private byte[] personalization;
private byte[] key;
private byte[] v;
- int reseed_counuter = 1;
/// <summary>
/// Return a seeded FixedSecureRandom representing the result of processing a
@@ -66,8 +65,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
private void Init(int strength)
{
- randombytes_init(seed, personalization, strength);
- reseed_counuter = 1;
+ RandomBytesInit(seed, personalization, strength);
}
public override void NextBytes(byte[] buf)
@@ -84,15 +82,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
{
for (int j = 15; j >= 0; j--)
{
- if ((v[j] & 0xFF) == 0xff)
- {
- v[j] = 0x00;
- }
- else
- {
- v[j]++;
+ if (++v[j] != 0x00)
break;
- }
}
AES256_ECB(key, v, block, 0);
@@ -111,10 +102,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
}
AES256_CTR_DRBG_Update(null, key, v);
- reseed_counuter++;
}
-
private void AES256_ECB(byte[] key, byte[] ctr, byte[] buffer, int startPosition)
{
try
@@ -130,10 +119,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
}
}
-
private void AES256_CTR_DRBG_Update(byte[] entropy_input, byte[] key, byte[] v)
{
-
byte[] tmp = new byte[48];
for (int i = 0; i < 3; i++)
@@ -141,15 +128,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
//increment V
for (int j = 15; j >= 0; j--)
{
- if ((v[j] & 0xFF) == 0xff)
- {
- v[j] = 0x00;
- }
- else
- {
- v[j]++;
+ if (++v[j] != 0x00)
break;
- }
}
AES256_ECB(key, v, tmp, 16 * i);
@@ -165,12 +145,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
Array.Copy(tmp, 0, key, 0, key.Length);
Array.Copy(tmp, 32, v, 0, v.Length);
-
-
}
-
- private void randombytes_init(byte[] entropyInput, byte[] personalization, int strength)
+ private void RandomBytesInit(byte[] entropyInput, byte[] personalization, int strength)
{
byte[] seedMaterial = new byte[48];
@@ -186,11 +163,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
key = new byte[32];
v = new byte[16];
-
AES256_CTR_DRBG_Update(seedMaterial, key, v);
-
- reseed_counuter = 1;
-
}
}
-}
\ No newline at end of file
+}
diff --git a/crypto/test/src/pqc/crypto/test/NtruPrimeVectorTest.cs b/crypto/test/src/pqc/crypto/test/NtruPrimeVectorTest.cs
index b2750030f..45364363c 100644
--- a/crypto/test/src/pqc/crypto/test/NtruPrimeVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/NtruPrimeVectorTest.cs
@@ -1,7 +1,8 @@
-using System;
using System.Collections.Generic;
using System.IO;
+
using NUnit.Framework;
+
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pqc.Crypto.NtruPrime;
using Org.BouncyCastle.Utilities;
@@ -13,224 +14,242 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class NtruPrimeVectorTest
{
- [Test]
- public void TestVectors()
+ private static readonly Dictionary<string, NtruLPRimeParameters> ParametersNtruLP = new Dictionary<string, NtruLPRimeParameters>()
{
- string[] files =
- {
- "kat_kem_ntrulp_653.rsp",
- "kat_kem_ntrulp_761.rsp",
- "kat_kem_ntrulp_857.rsp",
- "kat_kem_ntrulp_953.rsp",
- "kat_kem_ntrulp_1013.rsp",
- "kat_kem_ntrulp_1277.rsp",
- };
-
- NtruLPRimeParameters[] parameters =
- {
- NtruLPRimeParameters.ntrulpr653,
- NtruLPRimeParameters.ntrulpr761,
- NtruLPRimeParameters.ntrulpr857,
- NtruLPRimeParameters.ntrulpr953,
- NtruLPRimeParameters.ntrulpr1013,
- NtruLPRimeParameters.ntrulpr1277,
- };
+ { "kat_kem_ntrulp_653.rsp", NtruLPRimeParameters.ntrulpr653 },
+ { "kat_kem_ntrulp_761.rsp", NtruLPRimeParameters.ntrulpr761 },
+ { "kat_kem_ntrulp_857.rsp", NtruLPRimeParameters.ntrulpr857 },
+ { "kat_kem_ntrulp_953.rsp", NtruLPRimeParameters.ntrulpr953 },
+ { "kat_kem_ntrulp_1013.rsp", NtruLPRimeParameters.ntrulpr1013 },
+ { "kat_kem_ntrulp_1277.rsp", NtruLPRimeParameters.ntrulpr1277 },
+ };
+
+ private static readonly Dictionary<string, SNtruPrimeParameters> ParametersSNtruP = new Dictionary<string, SNtruPrimeParameters>()
+ {
+ { "kat_kem_sntrup_653.rsp", SNtruPrimeParameters.sntrup653 },
+ { "kat_kem_sntrup_761.rsp", SNtruPrimeParameters.sntrup761 },
+ { "kat_kem_sntrup_857.rsp", SNtruPrimeParameters.sntrup857 },
+ { "kat_kem_sntrup_953.rsp", SNtruPrimeParameters.sntrup953 },
+ { "kat_kem_sntrup_1013.rsp", SNtruPrimeParameters.sntrup1013 },
+ { "kat_kem_sntrup_1277.rsp", SNtruPrimeParameters.sntrup1277 },
+ };
+
+ private static readonly string[] TestVectorFilesNtruLP =
+ {
+ "kat_kem_ntrulp_653.rsp",
+ "kat_kem_ntrulp_761.rsp",
+ "kat_kem_ntrulp_857.rsp",
+ "kat_kem_ntrulp_953.rsp",
+ "kat_kem_ntrulp_1013.rsp",
+ "kat_kem_ntrulp_1277.rsp",
+ };
+
+ private static readonly string[] TestVectorFilesSNtruP =
+ {
+ "kat_kem_sntrup_653.rsp",
+ "kat_kem_sntrup_761.rsp",
+ "kat_kem_sntrup_857.rsp",
+ "kat_kem_sntrup_953.rsp",
+ "kat_kem_sntrup_1013.rsp",
+ "kat_kem_sntrup_1277.rsp",
+ };
+
+ [TestCaseSource(nameof(TestVectorFilesNtruLP))]
+ [Parallelizable(ParallelScope.All)]
+ public void TVNtruLP(string testVectorFile)
+ {
+ RunTestVectorNtruLPFile(testVectorFile);
+ }
+
+ [TestCaseSource(nameof(TestVectorFilesSNtruP))]
+ [Parallelizable(ParallelScope.All)]
+ public void TVSNtruP(string testVectorFile)
+ {
+ RunTestVectorSNtruPFile(testVectorFile);
+ }
+
+ private static void RunTestVectorNtruLP(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] seed = Hex.Decode(buf["seed"]);
+ byte[] pk = Hex.Decode(buf["pk"]);
+ byte[] ct = Hex.Decode(buf["ct"]);
+ byte[] sk = Hex.Decode(buf["sk"]);
+ byte[] ss = Hex.Decode(buf["ss"]);
+
+ NistSecureRandom random = new NistSecureRandom(seed, null);
+ NtruLPRimeParameters ntruPParameters = ParametersNtruLP[name];
+
+ NtruLPRimeKeyPairGenerator kpGen = new NtruLPRimeKeyPairGenerator();
+ NtruLPRimeKeyGenerationParameters genParams = new NtruLPRimeKeyGenerationParameters(random, ntruPParameters);
+
+ // Generate the key pair
+ kpGen.Init(genParams);
+ AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+ NtruLPRimePublicKeyParameters pubParams = (NtruLPRimePublicKeyParameters)kp.Public;
+ NtruLPRimePrivateKeyParameters privParams = (NtruLPRimePrivateKeyParameters)kp.Private;
+
+ // Check public and private key
+ Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), $"{name} {count} : public key");
+ Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), $"{name} {count} : private key");
+
+ // Encapsulation
+ NtruLPRimeKemGenerator ntruPEncCipher = new NtruLPRimeKemGenerator(random);
+ ISecretWithEncapsulation secWenc = ntruPEncCipher.GenerateEncapsulated(pubParams);
+ byte[] generatedCT = secWenc.GetEncapsulation();
+
+ // Check ciphertext
+ Assert.True(Arrays.AreEqual(ct, generatedCT), name + " " + count + ": kem_enc cipher text");
+
+ // Check secret
+ byte[] secret = secWenc.GetSecret();
+ Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc secret");
+
+ // Decapsulation
+ NtruLPRimeKemExtractor ntruDecCipher = new NtruLPRimeKemExtractor(privParams);
+ byte[] dec_key = ntruDecCipher.ExtractSecret(generatedCT);
+
+ // Check decapsulation secret
+ Assert.True(ntruPParameters.DefaultKeySize == dec_key.Length * 8);
+ Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, ss, 0, dec_key.Length), $"{name} {count}: kem_dec ss");
+ Assert.True(Arrays.AreEqual(dec_key, secret), $"{name} {count}: kem_dec key");
+ }
+ private static void RunTestVectorNtruLPFile(string name)
+ {
+ var buf = new Dictionary<string, string>();
TestSampler sampler = new TestSampler();
- for (int fileIndex = 0; fileIndex != files.Length; fileIndex++)
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.ntruprime." + name)))
{
- String name = files[fileIndex];
- Console.Write("Testing " + name + "...");
- Console.WriteLine("pqc.ntruprime."+ name);
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.ntruprime." + name));
- String line = null;
- Dictionary<String, String> buf = new Dictionary<string, string>();
-
+ string line;
while ((line = src.ReadLine()) != null)
{
line = line.Trim();
- if(line.StartsWith("#"))
+ 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 (line.Length == 0)
+ if (buf.Count > 0)
{
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (!sampler.SkipTest(buf["count"]))
{
- String count = buf["count"];
-
- if (!"0".Equals(count))
- {
- // Console.WriteLine("Zero");
- }
-
- byte[] seed = Hex.Decode(buf["seed"]);
- byte[] pk = Hex.Decode(buf["pk"]);
- byte[] ct = Hex.Decode(buf["ct"]);
- byte[] sk = Hex.Decode(buf["sk"]);
- byte[] ss = Hex.Decode(buf["ss"]);
-
-
- NistSecureRandom random = new NistSecureRandom(seed, null);
- NtruLPRimeParameters ntruPParameters = parameters[fileIndex];
-
- NtruLPRimeKeyPairGenerator kpGen = new NtruLPRimeKeyPairGenerator();
- NtruLPRimeKeyGenerationParameters genParams = new NtruLPRimeKeyGenerationParameters(random,ntruPParameters);
-
- // Generate the key pair
- kpGen.Init(genParams);
- AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
-
- NtruLPRimePublicKeyParameters pubParams = (NtruLPRimePublicKeyParameters) kp.Public;
- NtruLPRimePrivateKeyParameters privParams = (NtruLPRimePrivateKeyParameters) kp.Private;
-
- // Check public and private key
- Assert.True(Arrays.AreEqual(pk,pubParams.GetEncoded()), $"{name} {count} : public key");
- Assert.True(Arrays.AreEqual(sk,privParams.GetEncoded()), $"{name} {count} : private key");
-
- // Encapsulation
- NtruLPRimeKemGenerator ntruPEncCipher = new NtruLPRimeKemGenerator(random);
- ISecretWithEncapsulation secWenc = ntruPEncCipher.GenerateEncapsulated(pubParams);
- byte[] generatedCT = secWenc.GetEncapsulation();
-
- // Check ciphertext
- Assert.True(Arrays.AreEqual(ct, generatedCT), name + " " + count + ": kem_enc cipher text");
-
- // Check secret
- byte[] secret = secWenc.GetSecret();
- Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc secret");
-
- // Decapsulation
- NtruLPRimeKemExtractor ntruDecCipher = new NtruLPRimeKemExtractor(privParams);
- byte[] dec_key = ntruDecCipher.ExtractSecret(generatedCT);
-
- // Check decapsulation secret
- Assert.True(ntruPParameters.DefaultKeySize == dec_key.Length * 8);
- Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, ss, 0, dec_key.Length), $"{name} {count}: kem_dec ss");
- Assert.True(Arrays.AreEqual(dec_key, secret),$"{name} {count}: kem_dec key");
+ RunTestVectorNtruLP(name, buf);
}
buf.Clear();
-
- continue;
}
-
- int a = line.IndexOf("=");
- if (a > -1)
+ }
+
+ if (buf.Count > 0)
+ {
+ if (!sampler.SkipTest(buf["count"]))
{
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ RunTestVectorNtruLP(name, buf);
}
+ buf.Clear();
}
- Console.WriteLine("OK");
}
+ }
- files = new string[]
- {
- "kat_kem_sntrup_653.rsp",
- "kat_kem_sntrup_761.rsp",
- "kat_kem_sntrup_857.rsp",
- "kat_kem_sntrup_953.rsp",
- "kat_kem_sntrup_1013.rsp",
- "kat_kem_sntrup_1277.rsp",
- };
-
- SNtruPrimeParameters[] sparameters =
- {
- SNtruPrimeParameters.sntrup653,
- SNtruPrimeParameters.sntrup761,
- SNtruPrimeParameters.sntrup857,
- SNtruPrimeParameters.sntrup953,
- SNtruPrimeParameters.sntrup1013,
- SNtruPrimeParameters.sntrup1277,
- };
-
- for (int fileIndex = 0; fileIndex != files.Length; fileIndex++)
- {
- String name = files[fileIndex];
- Console.Write("Testing " + name + "...");
- Console.WriteLine("pqc.ntruprime." + name);
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.ntruprime." + name));
- String line = null;
- Dictionary<String, String> buf = new Dictionary<string, string>();
+ private static void RunTestVectorSNtruP(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] seed = Hex.Decode(buf["seed"]);
+ byte[] pk = Hex.Decode(buf["pk"]);
+ byte[] ct = Hex.Decode(buf["ct"]);
+ byte[] sk = Hex.Decode(buf["sk"]);
+ byte[] ss = Hex.Decode(buf["ss"]);
+
+ NistSecureRandom random = new NistSecureRandom(seed, null);
+ SNtruPrimeParameters ntruPParameters = ParametersSNtruP[name];
+
+ SNtruPrimeKeyPairGenerator kpGen = new SNtruPrimeKeyPairGenerator();
+ SNtruPrimeKeyGenerationParameters genParams = new SNtruPrimeKeyGenerationParameters(random, ntruPParameters);
+
+ // Generate the key pair
+ kpGen.Init(genParams);
+ AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+ SNtruPrimePublicKeyParameters pubParams = (SNtruPrimePublicKeyParameters)kp.Public;
+ SNtruPrimePrivateKeyParameters privParams = (SNtruPrimePrivateKeyParameters)kp.Private;
+
+ // Check public and private key
+ Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), $"{name} {count} : public key");
+ Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), $"{name} {count} : private key");
+
+ // Encapsulation
+ SNtruPrimeKemGenerator ntruPEncCipher = new SNtruPrimeKemGenerator(random);
+ ISecretWithEncapsulation secWenc = ntruPEncCipher.GenerateEncapsulated(pubParams);
+ byte[] generatedCT = secWenc.GetEncapsulation();
+
+ // Check ciphertext
+ Assert.True(Arrays.AreEqual(ct, generatedCT), name + " " + count + ": kem_enc cipher text");
+
+ // Check secret
+ byte[] secret = secWenc.GetSecret();
+ Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc secret");
+
+ // Decapsulation
+ SNtruPrimeKemExtractor ntruDecCipher = new SNtruPrimeKemExtractor(privParams);
+ byte[] dec_key = ntruDecCipher.ExtractSecret(generatedCT);
+
+ // Check decapsulation secret
+ Assert.True(ntruPParameters.DefaultKeySize == dec_key.Length * 8);
+ Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, ss, 0, dec_key.Length), $"{name} {count}: kem_dec ss");
+ Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, secret, 0, secret.Length), $"{name} {count}: kem_dec key");
+ }
+ private static void RunTestVectorSNtruPFile(string name)
+ {
+ var buf = new Dictionary<string, string>();
+ TestSampler sampler = new TestSampler();
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.ntruprime." + 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 (line.Length == 0)
+ if (buf.Count > 0)
{
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (!sampler.SkipTest(buf["count"]))
{
- String count = buf["count"];
-
- if (!"0".Equals(count))
- {
- // Console.WriteLine("Zero");
- }
-
- byte[] seed = Hex.Decode(buf["seed"]);
- byte[] pk = Hex.Decode(buf["pk"]);
- byte[] ct = Hex.Decode(buf["ct"]);
- byte[] sk = Hex.Decode(buf["sk"]);
- byte[] ss = Hex.Decode(buf["ss"]);
-
-
- NistSecureRandom random = new NistSecureRandom(seed, null);
- SNtruPrimeParameters ntruPParameters = sparameters[fileIndex];
-
- SNtruPrimeKeyPairGenerator kpGen = new SNtruPrimeKeyPairGenerator();
- SNtruPrimeKeyGenerationParameters genParams = new SNtruPrimeKeyGenerationParameters(random, ntruPParameters);
-
- // Generate the key pair
- kpGen.Init(genParams);
- AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
-
- SNtruPrimePublicKeyParameters pubParams = (SNtruPrimePublicKeyParameters)kp.Public;
- SNtruPrimePrivateKeyParameters privParams = (SNtruPrimePrivateKeyParameters)kp.Private;
-
- // Check public and private key
- Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), $"{name} {count} : public key");
- Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), $"{name} {count} : private key");
-
- // Encapsulation
- SNtruPrimeKemGenerator ntruPEncCipher = new SNtruPrimeKemGenerator(random);
- ISecretWithEncapsulation secWenc = ntruPEncCipher.GenerateEncapsulated(pubParams);
- byte[] generatedCT = secWenc.GetEncapsulation();
-
- // Check ciphertext
- Assert.True(Arrays.AreEqual(ct, generatedCT), name + " " + count + ": kem_enc cipher text");
-
- // Check secret
- byte[] secret = secWenc.GetSecret();
- Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc secret");
-
- // Decapsulation
- SNtruPrimeKemExtractor ntruDecCipher = new SNtruPrimeKemExtractor(privParams);
- byte[] dec_key = ntruDecCipher.ExtractSecret(generatedCT);
-
- // Check decapsulation secret
- Assert.True(ntruPParameters.DefaultKeySize == dec_key.Length * 8);
- Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, ss, 0, dec_key.Length), $"{name} {count}: kem_dec ss");
- Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, secret, 0, secret.Length), $"{name} {count}: kem_dec key");
+ RunTestVectorSNtruP(name, buf);
}
buf.Clear();
-
- continue;
}
+ }
- int a = line.IndexOf("=");
- if (a > -1)
+ if (buf.Count > 0)
+ {
+ if (!sampler.SkipTest(buf["count"]))
{
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ RunTestVectorSNtruP(name, buf);
}
+ buf.Clear();
}
- Console.WriteLine("OK");
}
}
-
}
}
diff --git a/crypto/test/src/pqc/crypto/test/NtruVectorTest.cs b/crypto/test/src/pqc/crypto/test/NtruVectorTest.cs
index f04db4855..099380654 100644
--- a/crypto/test/src/pqc/crypto/test/NtruVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/NtruVectorTest.cs
@@ -17,104 +17,113 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class NtruVectorTest
{
- [Test]
- public void TestVectors()
+ private static readonly Dictionary<string, NtruParameters> Parameters = new Dictionary<string, NtruParameters>()
{
- string[] files =
- {
- "PQCkemKAT_935.rsp", // NtruHps2048509
- "PQCkemKAT_1234.rsp", // NtruHps2048677
- "PQCkemKAT_1590.rsp", // NtruHps4096821
- "PQCkemKAT_1450.rsp" // NtruHrss701
- };
+ { "PQCkemKAT_935.rsp", NtruParameters.NtruHps2048509 },
+ { "PQCkemKAT_1234.rsp", NtruParameters.NtruHps2048677 },
+ { "PQCkemKAT_1590.rsp", NtruParameters.NtruHps4096821 },
+ { "PQCkemKAT_1450.rsp", NtruParameters.NtruHrss701 },
+ };
- NtruParameters[] parameters =
- {
- NtruParameters.NtruHps2048509,
- NtruParameters.NtruHps2048677,
- NtruParameters.NtruHps4096821,
- NtruParameters.NtruHrss701
- };
+ private static readonly string[] TestVectorFiles =
+ {
+ "PQCkemKAT_935.rsp",
+ "PQCkemKAT_1234.rsp",
+ "PQCkemKAT_1590.rsp",
+ "PQCkemKAT_1450.rsp",
+ };
+
+ [TestCaseSource(nameof(TestVectorFiles))]
+ [Parallelizable(ParallelScope.All)]
+ public void TV(string testVectorFile)
+ {
+ RunTestVectorFile(testVectorFile);
+ }
+
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] seed = Hex.Decode(buf["seed"]);
+ byte[] pk = Hex.Decode(buf["pk"]);
+ byte[] ct = Hex.Decode(buf["ct"]);
+ byte[] sk = Hex.Decode(buf["sk"]);
+ byte[] ss = Hex.Decode(buf["ss"]);
+
+ NistSecureRandom random = new NistSecureRandom(seed, null);
+ NtruParameters ntruParameters = Parameters[name];
+
+ // Test keygen
+ NtruKeyGenerationParameters keygenParameters =
+ new NtruKeyGenerationParameters(random, ntruParameters);
+
+ NtruKeyPairGenerator keygen = new NtruKeyPairGenerator();
+ keygen.Init(keygenParameters);
+ AsymmetricCipherKeyPair keyPair = keygen.GenerateKeyPair();
+
+ NtruPublicKeyParameters pubParams = (NtruPublicKeyParameters)keyPair.Public;
+ NtruPrivateKeyParameters privParams = (NtruPrivateKeyParameters)keyPair.Private;
+
+ Assert.True(Arrays.AreEqual(pk, pubParams.PublicKey), $"{name} {count} : public key");
+ Assert.True(Arrays.AreEqual(sk, privParams.PrivateKey), $"{name} {count} : private key");
+
+ // Test encapsulate
+ NtruKemGenerator encapsulator = new NtruKemGenerator(random);
+ ISecretWithEncapsulation encapsulation = encapsulator.GenerateEncapsulated(new NtruPublicKeyParameters(ntruParameters, pk));
+ byte[] generatedSecret = encapsulation.GetSecret();
+ byte[] generatedCiphertext = encapsulation.GetEncapsulation();
+ Assert.AreEqual(generatedSecret.Length, ntruParameters.DefaultKeySize / 8);
+ Assert.True(Arrays.AreEqual(ss, 0, generatedSecret.Length, generatedSecret, 0, generatedSecret.Length), $"{name} {count} : shared secret");
+ Assert.True(Arrays.AreEqual(ct, generatedCiphertext), $"{name} {count} : ciphertext");
+
+ // Test decapsulate
+ NtruKemExtractor decapsulator = new NtruKemExtractor(new NtruPrivateKeyParameters(ntruParameters, sk));
+ byte[] extractedSecret = decapsulator.ExtractSecret(ct);
+ Assert.AreEqual(generatedSecret.Length, extractedSecret.Length);
+ Assert.True(Arrays.AreEqual(ss, 0, extractedSecret.Length, extractedSecret, 0, extractedSecret.Length), $"{name} {count} : extract secret");
+ }
+
+ private static void RunTestVectorFile(string name)
+ {
+ var buf = new Dictionary<string, string>();
TestSampler sampler = new TestSampler();
- for (int fileIndex = 0; fileIndex != files.Length; fileIndex++)
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.ntru." + name)))
{
- string name = files[fileIndex];
- Console.Write("Testing " + name + "...");
- Console.WriteLine("pqc.ntru." + name);
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.ntru." + name));
- String line;
- Dictionary<String, String> buf = new Dictionary<string, string>();
-
+ 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 (line.Length == 0)
+ if (buf.Count > 0)
{
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (!sampler.SkipTest(buf["count"]))
{
- String count = buf["count"];
-
- if (!"0".Equals(count))
- {
- // Console.WriteLine("Zero");
- }
-
- byte[] seed = Hex.Decode(buf["seed"]);
- byte[] pk = Hex.Decode(buf["pk"]);
- byte[] ct = Hex.Decode(buf["ct"]);
- byte[] sk = Hex.Decode(buf["sk"]);
- byte[] ss = Hex.Decode(buf["ss"]);
-
- NistSecureRandom random = new NistSecureRandom(seed, null);
- NtruParameters ntruParameters = parameters[fileIndex];
-
- // Test keygen
- NtruKeyGenerationParameters keygenParameters =
- new NtruKeyGenerationParameters(random, ntruParameters);
-
- NtruKeyPairGenerator keygen = new NtruKeyPairGenerator();
- keygen.Init(keygenParameters);
- AsymmetricCipherKeyPair keyPair = keygen.GenerateKeyPair();
-
- NtruPublicKeyParameters pubParams = (NtruPublicKeyParameters)keyPair.Public;
- NtruPrivateKeyParameters privParams = (NtruPrivateKeyParameters)keyPair.Private;
-
- Assert.True(Arrays.AreEqual(pk,pubParams.PublicKey), $"{name} {count} : public key");
- Assert.True(Arrays.AreEqual(sk,privParams.PrivateKey), $"{name} {count} : private key");
-
- // Test encapsulate
- NtruKemGenerator encapsulator = new NtruKemGenerator(random);
- ISecretWithEncapsulation encapsulation = encapsulator.GenerateEncapsulated(new NtruPublicKeyParameters(ntruParameters, pk));
- byte[] generatedSecret = encapsulation.GetSecret();
- byte[] generatedCiphertext = encapsulation.GetEncapsulation();
-
- Assert.AreEqual(generatedSecret.Length, ntruParameters.DefaultKeySize / 8);
- Assert.True(Arrays.AreEqual(ss, 0, generatedSecret.Length, generatedSecret, 0, generatedSecret.Length), $"{name} {count} : shared secret");
- Assert.True(Arrays.AreEqual(ct, generatedCiphertext), $"{name} {count} : ciphertext");
-
- // Test decapsulate
- NtruKemExtractor decapsulator = new NtruKemExtractor(new NtruPrivateKeyParameters(ntruParameters, sk));
- byte[] extractedSecret = decapsulator.ExtractSecret(ct);
- Assert.AreEqual(generatedSecret.Length, extractedSecret.Length);
- Assert.True(Arrays.AreEqual(ss, 0, extractedSecret.Length, extractedSecret, 0, extractedSecret.Length), $"{name} {count} : extract secret");
-
+ RunTestVector(name, buf);
}
buf.Clear();
- continue;
}
- int a = line.IndexOf("=");
- if (a > -1)
+ }
+
+ if (buf.Count > 0)
+ {
+ if (!sampler.SkipTest(buf["count"]))
{
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ RunTestVector(name, buf);
}
+ buf.Clear();
}
- Console.WriteLine("OK");
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs
index c23996e55..e6925da20 100644
--- a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs
@@ -54,14 +54,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestCaseSource(nameof(TestVectorFilesBasic))]
[Parallelizable(ParallelScope.All)]
- public void TestVectorsBasic(string testVectorFile)
+ public void TVBasic(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
[Explicit, TestCaseSource(nameof(TestVectorFilesExtra))]
[Parallelizable(ParallelScope.All)]
- public void TestVectorsExtra(string testVectorFile)
+ public void TVExtra(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
@@ -145,16 +145,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
continue;
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
diff --git a/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs b/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs
index f9d659c1e..5da8828cc 100644
--- a/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs
@@ -16,21 +16,35 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class SaberVectorTest
{
- [Test]
- public void TestParamaters()
+ //SaberParameters[] parameters = {
+ // SaberParameters.lightsaberkem128r3,
+ // SaberParameters.saberkem128r3,
+ // SaberParameters.firesaberkem128r3,
+ // SaberParameters.lightsaberkem192r3,
+ // SaberParameters.saberkem192r3,
+ // SaberParameters.firesaberkem192r3,
+ // SaberParameters.lightsaberkem256r3,
+ // SaberParameters.saberkem256r3,
+ // SaberParameters.firesaberkem256r3,
+ //};
+
+ private static readonly Dictionary<string, SaberParameters> Parameters = new Dictionary<string, SaberParameters>()
+ {
+ { "lightsaber.rsp", SaberParameters.lightsaberkem256r3 },
+ { "saber.rsp", SaberParameters.saberkem256r3 },
+ { "firesaber.rsp", SaberParameters.firesaberkem256r3 },
+ };
+
+ private static readonly string[] TestVectorFiles =
{
- SaberParameters[] parameters = {
- SaberParameters.lightsaberkem128r3,
- SaberParameters.saberkem128r3,
- SaberParameters.firesaberkem128r3,
- SaberParameters.lightsaberkem192r3,
- SaberParameters.saberkem192r3,
- SaberParameters.firesaberkem192r3,
- SaberParameters.lightsaberkem256r3,
- SaberParameters.saberkem256r3,
- SaberParameters.firesaberkem256r3,
- };
+ "lightsaber.rsp",
+ "saber.rsp",
+ "firesaber.rsp",
+ };
+ [Test]
+ public void TestParameters()
+ {
Assert.AreEqual(128, SaberParameters.lightsaberkem128r3.DefaultKeySize);
Assert.AreEqual(128, SaberParameters.saberkem128r3.DefaultKeySize);
Assert.AreEqual(128, SaberParameters.firesaberkem128r3.DefaultKeySize);
@@ -42,107 +56,101 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
Assert.AreEqual(256, SaberParameters.firesaberkem256r3.DefaultKeySize);
}
- [Test]
- public void TestVectors()
+ [TestCaseSource(nameof(TestVectorFiles))]
+ [Parallelizable(ParallelScope.All)]
+ public void TV(string testVectorFile)
{
+ RunTestVectorFile(testVectorFile);
+ }
- SaberParameters[] saberParameters =
- {
- SaberParameters.lightsaberkem256r3,
- SaberParameters.saberkem256r3,
- SaberParameters.firesaberkem256r3,
- };
- String[] files =
- {
- "lightsaber.rsp",
- "saber.rsp",
- "firesaber.rsp"
- };
+ private static void RunTestVector(string name, IDictionary<string, string> buf)
+ {
+ string count = buf["count"];
+ byte[] seed = Hex.Decode(buf["seed"]); // seed for SecureRandom
+ byte[] pk = Hex.Decode(buf["pk"]); // public key
+ byte[] sk = Hex.Decode(buf["sk"]); // private key
+ byte[] ct = Hex.Decode(buf["ct"]); // ciphertext
+ byte[] ss = Hex.Decode(buf["ss"]); // session key
+
+ NistSecureRandom random = new NistSecureRandom(seed, null);
+ SaberParameters parameters = Parameters[name];
+
+ SaberKeyPairGenerator kpGen = new SaberKeyPairGenerator();
+ SaberKeyGenerationParameters genParam = new SaberKeyGenerationParameters(random, parameters);
+ //
+ // Generate keys and test.
+ //
+ kpGen.Init(genParam);
+ AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+ SaberPublicKeyParameters pubParams = (SaberPublicKeyParameters)PublicKeyFactory.CreateKey(
+ SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((SaberPublicKeyParameters)kp.Public));
+ SaberPrivateKeyParameters privParams = (SaberPrivateKeyParameters)PrivateKeyFactory.CreateKey(
+ PrivateKeyInfoFactory.CreatePrivateKeyInfo((SaberPrivateKeyParameters)kp.Private));
+
+ Assert.True(Arrays.AreEqual(pk, pubParams.PublicKey), name + " " + count + ": public key");
+ Assert.True(Arrays.AreEqual(sk, privParams.GetPrivateKey()), name + " " + count + ": secret key");
+
+ // KEM Enc
+ SaberKemGenerator SABEREncCipher = new SaberKemGenerator(random);
+ ISecretWithEncapsulation secWenc = SABEREncCipher.GenerateEncapsulated(pubParams);
+ byte[] generated_cipher_text = secWenc.GetEncapsulation();
+ Assert.True(Arrays.AreEqual(ct, generated_cipher_text), name + " " + count + ": kem_enc cipher text");
+ byte[] secret = secWenc.GetSecret();
+ Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc key");
+
+ // KEM Dec
+ SaberKemExtractor SABERDecCipher = new SaberKemExtractor(privParams);
+
+ byte[] dec_key = SABERDecCipher.ExtractSecret(generated_cipher_text);
+
+ Assert.True(parameters.DefaultKeySize == dec_key.Length * 8);
+ Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, ss, 0, dec_key.Length), name + " " + count + ": kem_dec ss");
+ Assert.True(Arrays.AreEqual(dec_key, secret), name + " " + count + ": kem_dec key");
+ }
+ private static void RunTestVectorFile(string name)
+ {
+ var buf = new Dictionary<string, string>();
TestSampler sampler = new TestSampler();
- for (int fileIndex = 0; fileIndex != files.Length; fileIndex++)
+ using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.saber." + name)))
{
- String name = files[fileIndex];
- StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.saber." + name));
-
-
- String line = null;
- Dictionary<string, string> buf = new Dictionary<string, string>();
+ 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 (line.Length == 0)
+ if (buf.Count > 0)
{
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (!sampler.SkipTest(buf["count"]))
{
- String count = buf["count"];
-
- byte[] seed = Hex.Decode(buf["seed"]); // seed for SecureRandom
- byte[] pk = Hex.Decode(buf["pk"]); // public key
- byte[] sk = Hex.Decode(buf["sk"]); // private key
- byte[] ct = Hex.Decode(buf["ct"]); // ciphertext
- byte[] ss = Hex.Decode(buf["ss"]); // session key
-
- NistSecureRandom random = new NistSecureRandom(seed, null);
- SaberParameters parameters = saberParameters[fileIndex];
-
- SaberKeyPairGenerator kpGen = new SaberKeyPairGenerator();
- SaberKeyGenerationParameters
- genParam = new SaberKeyGenerationParameters(random, parameters);
- //
- // Generate keys and test.
- //
- kpGen.Init(genParam);
- AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
-
- SaberPublicKeyParameters pubParams =
- (SaberPublicKeyParameters) PublicKeyFactory.CreateKey(
- SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(
- (SaberPublicKeyParameters) kp.Public));
- SaberPrivateKeyParameters privParams =
- (SaberPrivateKeyParameters) PrivateKeyFactory.CreateKey(
- PrivateKeyInfoFactory.CreatePrivateKeyInfo((SaberPrivateKeyParameters) kp.Private));
-
-
- Assert.True(Arrays.AreEqual(pk, pubParams.PublicKey), name + " " + count + ": public key");
- Assert.True(Arrays.AreEqual(sk, privParams.GetPrivateKey()), name + " " + count + ": secret key");
-
- // KEM Enc
- SaberKemGenerator SABEREncCipher = new SaberKemGenerator(random);
- ISecretWithEncapsulation secWenc = SABEREncCipher.GenerateEncapsulated(pubParams);
- byte[] generated_cipher_text = secWenc.GetEncapsulation();
- Assert.True(Arrays.AreEqual(ct, generated_cipher_text), name + " " + count + ": kem_enc cipher text");
- byte[] secret = secWenc.GetSecret();
- Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc key");
-
- // KEM Dec
- SaberKemExtractor SABERDecCipher = new SaberKemExtractor(privParams);
-
- byte[] dec_key = SABERDecCipher.ExtractSecret(generated_cipher_text);
-
- Assert.True(parameters.DefaultKeySize == dec_key.Length * 8);
- Assert.True(Arrays.AreEqual(dec_key, 0, dec_key.Length, ss, 0, dec_key.Length), name + " " + count + ": kem_dec ss");
- Assert.True(Arrays.AreEqual(dec_key, secret), name + " " + count + ": kem_dec key");
+ RunTestVector(name, buf);
}
-
buf.Clear();
-
- continue;
}
+ }
- int a = line.IndexOf("=");
- if (a > -1)
+ if (buf.Count > 0)
+ {
+ if (!sampler.SkipTest(buf["count"]))
{
- buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+ RunTestVector(name, buf);
}
+ buf.Clear();
}
}
}
}
-}
\ No newline at end of file
+}
diff --git a/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs b/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs
index 8b8c6bc50..0643120bf 100644
--- a/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs
@@ -16,8 +16,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestFixture]
public class SikeVectorTest
{
-
- private static readonly Dictionary<string, SIKEParameters> parameters = new Dictionary<string, SIKEParameters>()
+ private static readonly Dictionary<string, SIKEParameters> Parameters = new Dictionary<string, SIKEParameters>()
{
{ "PQCkemKAT_374.rsp" , SIKEParameters.sikep434 },
{ "PQCkemKAT_434.rsp" , SIKEParameters.sikep503 },
@@ -26,7 +25,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
{ "PQCkemKAT_350.rsp" , SIKEParameters.sikep434_compressed },
{ "PQCkemKAT_407.rsp" , SIKEParameters.sikep503_compressed },
{ "PQCkemKAT_491.rsp" , SIKEParameters.sikep610_compressed },
- { "PQCkemKAT_602.rsp" , SIKEParameters.sikep751_compressed }
+ { "PQCkemKAT_602.rsp" , SIKEParameters.sikep751_compressed },
};
private static readonly string[] TestVectorFilesBasic =
@@ -47,14 +46,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
[TestCaseSource(nameof(TestVectorFilesBasic))]
[Parallelizable(ParallelScope.All)]
- public void TestVectorsBasic(string testVectorFile)
+ public void TVBasic(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
[TestCaseSource(nameof(TestVectorFilesCompressed))]
[Parallelizable(ParallelScope.All)]
- public void TestVectorCompressed(string testVectorFile)
+ public void TVCompressed(string testVectorFile)
{
RunTestVectorFile(testVectorFile);
}
@@ -69,7 +68,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
byte[] ss = Hex.Decode(buf["ss"]); // session key
NistSecureRandom random = new NistSecureRandom(seed, null);
- SIKEParameters SIKEParameters = parameters[name];
+ SIKEParameters SIKEParameters = Parameters[name];
SIKEKeyPairGenerator kpGen = new SIKEKeyPairGenerator();
SIKEKeyGenerationParameters genParams = new SIKEKeyGenerationParameters(random, SIKEParameters);
@@ -81,8 +80,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
// todo
- SIKEPublicKeyParameters pubParams = (SIKEPublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public));
- SIKEPrivateKeyParameters privParams = (SIKEPrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private));
+ SIKEPublicKeyParameters pubParams = (SIKEPublicKeyParameters)PublicKeyFactory.CreateKey(
+ SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public));
+ SIKEPrivateKeyParameters privParams = (SIKEPrivateKeyParameters)PrivateKeyFactory.CreateKey(
+ PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private));
// SIKEPublicKeyParameters pubParams = (SIKEPublicKeyParameters)kp.Public;
// SIKEPrivateKeyParameters privParams = (SIKEPrivateKeyParameters)kp.Private;
@@ -98,14 +99,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
byte[] generated_cipher_text = secWenc.GetEncapsulation();
-// System.out.println(Hex.toHexString(ct));
-// System.out.println(Hex.toHexString(generated_cipher_text));
+ //System.out.println(Hex.toHexString(ct));
+ //System.out.println(Hex.toHexString(generated_cipher_text));
Assert.True(Arrays.AreEqual(ct, generated_cipher_text), name + " " + count + ": kem_enc cipher text");
byte[] secret = secWenc.GetSecret();
-// System.out.println(Hex.toHexString(ss).toUpperCase());
-// System.out.println(Hex.toHexString(secret).toUpperCase());
+ //System.out.println(Hex.toHexString(ss).toUpperCase());
+ //System.out.println(Hex.toHexString(secret).toUpperCase());
Assert.True(Arrays.AreEqual(ss, secret), name + " " + count + ": kem_enc key");
// KEM Dec
@@ -113,13 +114,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
byte[] dec_key = sikeDecCipher.ExtractSecret(generated_cipher_text);
- // System.out.println(Hex.toHexString(dec_key).toUpperCase());
- // System.out.println(Hex.toHexString(ss).toUpperCase());
+ //System.out.println(Hex.toHexString(dec_key).toUpperCase());
+ //System.out.println(Hex.toHexString(ss).toUpperCase());
Assert.True(SIKEParameters.DefaultKeySize == dec_key.Length * 8);
Assert.True(Arrays.AreEqual(dec_key, ss), name + " " + count + ": kem_dec ss" );
Assert.True(Arrays.AreEqual(dec_key, secret), name + " " + count + ": kem_dec key" );
-
}
@@ -146,18 +146,25 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
continue;
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
}
-}
\ 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 0c3acf365..edaf33740 100644
--- a/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs
+++ b/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs
@@ -36,6 +36,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
"haraka-192f-robust.rsp",
"haraka-256f-robust.rsp",
};
+
private static readonly string[] TestVectorFilesRobustSlow =
{
"sha2-128s-robust.rsp",
@@ -48,6 +49,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
"haraka-192s-robust.rsp",
"haraka-256s-robust.rsp",
};
+
private static readonly string[] TestVectorFilesSimpleFast =
{
"sha2-128f-simple.rsp",
@@ -60,6 +62,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
"haraka-192f-simple.rsp",
"haraka-256f-simple.rsp",
};
+
private static readonly string[] TestVectorFilesSimpleSlow =
{
"sha2-128s-simple.rsp",
@@ -498,16 +501,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
continue;
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
buf.Clear();
}
}
- if (buf.Count > 0 && !sampler.SkipTest(buf["count"]))
+ if (buf.Count > 0)
{
- RunTestVector(name, buf);
+ if (!sampler.SkipTest(buf["count"]))
+ {
+ RunTestVector(name, buf);
+ }
+ buf.Clear();
}
}
}
|