diff options
author | royb <roy.basmacier@primekey.com> | 2022-09-28 18:12:41 -0400 |
---|---|---|
committer | royb <roy.basmacier@primekey.com> | 2022-09-28 18:12:41 -0400 |
commit | d9c526408daf0c379a588d0a10e72ec71b490e0e (patch) | |
tree | dc8eaaeac7d113c331f201f87ef6fe2c9cc044ae /crypto/test/src | |
parent | Added Dilithium AES support + some optimizations (diff) | |
download | BouncyCastle.NET-ed25519-d9c526408daf0c379a588d0a10e72ec71b490e0e.tar.xz |
Added Kyber AES support
Diffstat (limited to 'crypto/test/src')
-rw-r--r-- | crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs | 191 |
1 files changed, 91 insertions, 100 deletions
diff --git a/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs b/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs index 856fe1b7a..0e363d124 100644 --- a/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs +++ b/crypto/test/src/pqc/crypto/test/CrystalsKyberTest.cs @@ -13,124 +13,115 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests [TestFixture] public class CrystalsKyberTest { - [Test] - public void TestVectors() + 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 } + }; + + private static readonly string[] TestVectorFilesBasic = + { + "kyber512.rsp", + "kyber768.rsp", + "kyber1024.rsp", + "kyber512aes.rsp", + "kyber768aes.rsp", + "kyber1024aes.rsp", + }; + + [TestCaseSource(nameof(TestVectorFilesBasic))] + [Parallelizable(ParallelScope.All)] + public void TestVectorsBasic(string testVectorFile) + { + RunTestVectorFile(testVectorFile); + } + + private static void TestVectors(string name, IDictionary<string, string> buf) + { + string count = buf["count"]; + + byte[] seed = Hex.Decode(buf["seed"]); // seed for Kyber 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); + KyberParameters kyberparameters = CrystalsKyberTest.parameters[name]; + + KyberKeyPairGenerator kpGen = new KyberKeyPairGenerator(); + KyberKeyGenerationParameters + genParam = new KyberKeyGenerationParameters(random, kyberparameters); + + // + // Generate keys and test. + // + kpGen.Init(genParam); + AsymmetricCipherKeyPair ackp = kpGen.GenerateKeyPair(); + + + KyberPublicKeyParameters pubParams = (KyberPublicKeyParameters)ackp.Public; + KyberPrivateKeyParameters privParams = (KyberPrivateKeyParameters)ackp.Private; + + Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key"); + Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key"); + + // KEM Enc + KyberKemGenerator KyberEncCipher = new KyberKemGenerator(random); + ISecretWithEncapsulation secWenc = KyberEncCipher.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 + KyberKemExtractor KyberDecCipher = new KyberKemExtractor(privParams); + + byte[] dec_key = KyberDecCipher.ExtractSecret(generated_cipher_text); + + 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>(); - KyberParameters[] KyberParams = - { - KyberParameters.kyber512, - KyberParameters.kyber768, - KyberParameters.kyber1024, - }; - String[] files = - { - "kyber512.rsp", - "kyber768.rsp", - "kyber1024.rsp" - }; - - TestSampler sampler = new TestSampler(); - for (int fileIndex = 0; fileIndex != files.Length; fileIndex++) + using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.crystals.kyber." + name))) { - String name = files[fileIndex]; - StreamReader src = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.crystals.kyber." + 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) + if (line.Length > 0) { - if (buf.Count > 0 && !sampler.SkipTest(buf["count"])) + int a = line.IndexOf('='); + if (a > -1) { - String count = buf["count"]; - - byte[] seed = Hex.Decode(buf["seed"]); // seed for Kyber 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); - KyberParameters parameters = KyberParams[fileIndex]; - - KyberKeyPairGenerator kpGen = new KyberKeyPairGenerator(); - KyberKeyGenerationParameters - genParam = new KyberKeyGenerationParameters(random, parameters); - - Console.WriteLine(string.Format("seed = {0}", Hex.ToHexString(seed))); - - // - // Generate keys and test. - // - kpGen.Init(genParam); - AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - - - KyberPublicKeyParameters pubParams = (KyberPublicKeyParameters)kp.Public; - KyberPrivateKeyParameters privParams = (KyberPrivateKeyParameters)kp.Private; - - //Console.WriteLine(string.Format("pk = {0}", Convert.ToHexString(pk))); - //Console.WriteLine(String.Format("Public key = {0}", Convert.ToHexString(pubParams.publicKey))); - Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key"); - - Console.WriteLine(string.Format("sk = {0}", Hex.ToHexString(sk))); - Console.WriteLine(String.Format("sk bytes = {0}", sk.Length)); - Console.WriteLine(String.Format("Secret key = {0}", Hex.ToHexString(privParams.GetEncoded()))); - Console.WriteLine(String.Format("secret key bytes = {0}", privParams.GetEncoded().Length)); - - Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key"); - - // KEM Enc - KyberKemGenerator KyberEncCipher = new KyberKemGenerator(random); - ISecretWithEncapsulation secWenc = KyberEncCipher.GenerateEncapsulated(pubParams); - byte[] generated_cipher_text = secWenc.GetEncapsulation(); - - - //Console.WriteLine(string.Format("ct = {0}", Convert.ToHexString(ct))); - //Console.WriteLine(String.Format("ct bytes = {0}", ct.Length)); - //Console.WriteLine(String.Format("Cipher Text = {0}", Convert.ToHexString(generated_cipher_text))); - //Console.WriteLine(String.Format("Cipher Text bytes = {0}", generated_cipher_text.Length)); - - //Console.WriteLine(string.Format("ss = {0}", Convert.ToHexString(ss))); - //Console.WriteLine(String.Format("ss bytes = {0}", ss.Length)); - //Console.WriteLine(String.Format("Shared Secret = {0}", Convert.ToHexString(secWenc.GetSecret()))); - //Console.WriteLine(String.Format("Shared Secret bytes = {0}", secWenc.GetSecret().Length)); - - 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 - KyberKemExtractor KyberDecCipher = new KyberKemExtractor(privParams); - - byte[] dec_key = KyberDecCipher.ExtractSecret(generated_cipher_text); - - Assert.AreEqual(dec_key.Length * 8, parameters.DefaultKeySize); - 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"); + 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) { - buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim(); + TestVectors(name, buf); + buf.Clear(); } } + + if (buf.Count > 0) + { + TestVectors(name, buf); + } } } } |