diff options
-rw-r--r-- | crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs | 194 | ||||
-rw-r--r-- | crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs | 4 |
2 files changed, 102 insertions, 96 deletions
diff --git a/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs b/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs index 1c8c768d2..e969a6407 100644 --- a/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs @@ -15,18 +15,33 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests [TestFixture] public class FrodoVectorTest { + private static readonly Dictionary<string, FrodoParameters> Parameters = new Dictionary<string, FrodoParameters>() + { + { "PQCkemKAT_19888.rsp", FrodoParameters.frodokem19888r3 }, + { "PQCkemKAT_31296.rsp", FrodoParameters.frodokem31296r3 }, + { "PQCkemKAT_43088.rsp", FrodoParameters.frodokem43088r3 }, + { "PQCkemKAT_19888_shake.rsp", FrodoParameters.frodokem19888shaker3 }, + { "PQCkemKAT_31296_shake.rsp", FrodoParameters.frodokem31296shaker3 }, + { "PQCkemKAT_43088_shake.rsp", FrodoParameters.frodokem43088shaker3 }, + }; + + private static readonly string[] TestVectorFilesAes = + { + "PQCkemKAT_19888.rsp", + "PQCkemKAT_31296.rsp", + "PQCkemKAT_43088.rsp", + }; + + private static readonly string[] TestVectorFilesShake = + { + "PQCkemKAT_19888_shake.rsp", + "PQCkemKAT_31296_shake.rsp", + "PQCkemKAT_43088_shake.rsp" + }; + [Test] public void TestParameters() { - FrodoParameters[] parameters = { - FrodoParameters.frodokem19888r3, - FrodoParameters.frodokem19888shaker3, - FrodoParameters.frodokem31296r3, - FrodoParameters.frodokem31296shaker3, - FrodoParameters.frodokem43088r3, - FrodoParameters.frodokem43088shaker3 - }; - Assert.AreEqual(128, FrodoParameters.frodokem19888r3.DefaultKeySize); Assert.AreEqual(128, FrodoParameters.frodokem19888shaker3.DefaultKeySize); Assert.AreEqual(192, FrodoParameters.frodokem31296r3.DefaultKeySize); @@ -34,109 +49,100 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests Assert.AreEqual(256, FrodoParameters.frodokem43088r3.DefaultKeySize); Assert.AreEqual(256, FrodoParameters.frodokem43088shaker3.DefaultKeySize); } - - [Test] - public void TestVectors() + + [TestCaseSource(nameof(TestVectorFilesAes))] + [Parallelizable(ParallelScope.All)] + public void TVAes(string testVectorFile) { - string[] files = { - "PQCkemKAT_19888.rsp", - "PQCkemKAT_31296.rsp", - "PQCkemKAT_43088.rsp", - "PQCkemKAT_19888_shake.rsp", - "PQCkemKAT_31296_shake.rsp", - "PQCkemKAT_43088_shake.rsp" - }; - - FrodoParameters[] parameters = { - FrodoParameters.frodokem19888r3, - FrodoParameters.frodokem31296r3, - FrodoParameters.frodokem43088r3, - FrodoParameters.frodokem19888shaker3, - FrodoParameters.frodokem31296shaker3, - FrodoParameters.frodokem43088shaker3 - }; + RunTestVectorFile(testVectorFile); + } + [TestCaseSource(nameof(TestVectorFilesShake))] + [Parallelizable(ParallelScope.All)] + public void TVShake(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 nist secure random + 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); + FrodoParameters frodoParameters = Parameters[name]; + + FrodoKeyPairGenerator kpGen = new FrodoKeyPairGenerator(); + FrodoKeyGenerationParameters genParams = new FrodoKeyGenerationParameters(random, frodoParameters); + // + // Generate keys and test. + // + kpGen.Init(genParams); + AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); + + FrodoPublicKeyParameters pubParams = (FrodoPublicKeyParameters)kp.Public; + FrodoPrivateKeyParameters privParams = (FrodoPrivateKeyParameters)kp.Private; + + Assert.True(Arrays.AreEqual(pk, pubParams.PublicKey), $"{name} {count} : public key"); + Assert.True(Arrays.AreEqual(sk, privParams.PrivateKey), $"{name} {count} : secret key"); + + // kem_enc + FrodoKEMGenerator frodoEncCipher = new FrodoKEMGenerator(random); + ISecretWithEncapsulation secWenc = frodoEncCipher.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, secret), name + " " + count + ": kem_enc key"); + + // kem_dec + FrodoKEMExtractor frodoDecCipher = new FrodoKEMExtractor(privParams); + + byte[] dec_key = frodoDecCipher.ExtractSecret(generated_cipher_text); + + Assert.True(frodoParameters.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"); + } + + 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.frodo." + name))) { - String name = files[fileIndex]; - Console.Write($"testing: {name}\n"); - StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.frodo." + name)); - - String line = null; - Dictionary<String, String> buf = new Dictionary<string, string>(); - // Random rnd = new Random(System.currentTimeMillis()); + string line = null; while ((line = src.ReadLine()) != null) { line = line.Trim(); - if (line.StartsWith("#")) - { continue; - } - if (line.Length == 0) + + if (line.Length > 0) { - if (buf.Count > 0) + int a = line.IndexOf("="); + if (a > -1) { - string count = buf["count"]; - if (sampler.SkipTest(count)) - continue; - - Console.Write($"test case: {count}"); - - byte[] seed = Hex.Decode(buf["seed"]); // seed for nist secure random - 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); - FrodoParameters frodoParameters = parameters[fileIndex]; - - FrodoKeyPairGenerator kpGen = new FrodoKeyPairGenerator(); - FrodoKeyGenerationParameters genParams = new FrodoKeyGenerationParameters(random, frodoParameters); - // - // Generate keys and test. - // - kpGen.Init(genParams); - AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - - FrodoPublicKeyParameters pubParams = (FrodoPublicKeyParameters) kp.Public; - FrodoPrivateKeyParameters privParams = (FrodoPrivateKeyParameters) kp.Private; - - Assert.True(Arrays.AreEqual(pk, pubParams.PublicKey), $"{name} {count} : public key"); - Assert.True( Arrays.AreEqual(sk, privParams.PrivateKey),$"{name} {count} : secret key"); - - // kem_enc - FrodoKEMGenerator frodoEncCipher = new FrodoKEMGenerator(random); - ISecretWithEncapsulation secWenc = frodoEncCipher.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, secret), name + " " + count + ": kem_enc key"); - - // kem_dec - FrodoKEMExtractor frodoDecCipher = new FrodoKEMExtractor(privParams); - - byte[] dec_key = frodoDecCipher.ExtractSecret(generated_cipher_text); - - Assert.True(frodoParameters.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"); + buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim(); } - buf.Clear(); - continue; } - int a = line.IndexOf("="); - if (a > -1) + if (buf.Count > 0 && !sampler.SkipTest(buf["count"])) { - buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim(); + RunTestVector(name, buf); + buf.Clear(); } } - Console.Write("testing successful!"); + + if (buf.Count > 0) + { + RunTestVector(name, buf); + } } } } -} \ No newline at end of file +} diff --git a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs index 153d9ceb1..57eff0f13 100644 --- a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs @@ -16,7 +16,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests [TestFixture] public class PicnicVectorTest { - private static readonly Dictionary<string, PicnicParameters> parameters = new Dictionary<string, PicnicParameters>() + private static readonly Dictionary<string, PicnicParameters> Parameters = new Dictionary<string, PicnicParameters>() { { "picnicl1fs.rsp", PicnicParameters.picnicl1fs }, { "picnicl1ur.rsp", PicnicParameters.picnicl1ur }, @@ -78,7 +78,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] sigExpected = Hex.Decode(buf["sm"]); // signature NistSecureRandom random = new NistSecureRandom(seed, null); - PicnicParameters picnicParameters = parameters[name]; + PicnicParameters picnicParameters = Parameters[name]; PicnicKeyPairGenerator kpGen = new PicnicKeyPairGenerator(); PicnicKeyGenerationParameters genParams = new PicnicKeyGenerationParameters(random, picnicParameters); |