summary refs log tree commit diff
path: root/crypto/test/src/pqc
diff options
context:
space:
mode:
authorDavid Hook <david.hook@keyfactor.com>2022-10-12 20:43:23 +1100
committerDavid Hook <david.hook@keyfactor.com>2022-10-12 20:43:23 +1100
commit9bc545cf0fcfd4dc03b8c47031ba1bac54279d23 (patch)
treec3dcee4169df32ba2d85b6a9f9a46298d9faacfc /crypto/test/src/pqc
parentadded BIKE cred (diff)
downloadBouncyCastle.NET-ed25519-9bc545cf0fcfd4dc03b8c47031ba1bac54279d23.tar.xz
general cleanups - naming, imports
Diffstat (limited to 'crypto/test/src/pqc')
-rw-r--r--crypto/test/src/pqc/crypto/test/BikeVectorTest.cs135
-rw-r--r--crypto/test/src/pqc/crypto/test/SaberVectorTest.cs70
2 files changed, 170 insertions, 35 deletions
diff --git a/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs b/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs
new file mode 100644
index 000000000..89cc674e7
--- /dev/null
+++ b/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs
@@ -0,0 +1,135 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+using NUnit.Framework;
+using Org.BouncyCastle.Pqc.Crypto.Utilities;
+using Org.BouncyCastle.Pqc.Crypto.Bike;
+
+namespace Org.BouncyCastle.Pqc.Crypto.Tests
+{
+    [TestFixture]
+    public class BikeVectorTest
+    {
+        [Test]
+        public void TestParameters()
+        {
+            Assert.AreEqual(128, BikeParameters.bike128.DefaultKeySize);
+            Assert.AreEqual(192, BikeParameters.bike192.DefaultKeySize);
+            Assert.AreEqual(256, BikeParameters.bike256.DefaultKeySize);
+        }
+        
+        [Test]
+        public void TestVectors()
+        {
+            bool full = false;
+            
+            string[] files;
+            if (full)
+            {
+                files = new []{
+                    "PQCkemKAT_BIKE_3114.rsp",
+                    "PQCkemKAT_BIKE_6198.rsp",
+                    "PQCkemKAT_BIKE_10276.rsp"
+                };
+            }
+            else
+            {
+                files = new []{
+                    "PQCkemKAT_BIKE_3114.rsp"
+                };
+            }
+
+            BikeParameters[] parameters = {
+                    BikeParameters.bike128,
+                    BikeParameters.bike192,
+                    BikeParameters.bike256
+            };
+
+            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.bike." + name));
+
+                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 && !sampler.SkipTest(buf["count"]))
+                        {
+                            string count = buf["count"];
+
+                            Console.Write($"test case: {count}\n");
+                            byte[] seed = Hex.Decode(buf["seed"]); // seed for Cmce 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);
+                            BikeParameters bikeParameters = parameters[fileIndex];
+
+                            BikeKeyPairGenerator kpGen = new BikeKeyPairGenerator();
+                            BikeKeyGenerationParameters genParam = new BikeKeyGenerationParameters(random, bikeParameters);
+                            //
+                            // Generate keys and test.
+                            //
+                            kpGen.Init(genParam);
+                            AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+                            BikePublicKeyParameters pubParams = (BikePublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((BikePublicKeyParameters) kp.Public));
+                            BikePrivateKeyParameters privParams = (BikePrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo((BikePrivateKeyParameters) 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
+                            BikeKemGenerator BikeEncCipher = new BikeKemGenerator(random);
+                            ISecretWithEncapsulation secWenc = BikeEncCipher.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
+                            BikeKemExtractor BikeDecCipher = new BikeKemExtractor(privParams);
+
+                            byte[] dec_key = BikeDecCipher.ExtractSecret(generated_cipher_text);
+
+                            Assert.True(bikeParameters.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");
+                        }
+                        buf.Clear();
+
+                        continue;
+                    }
+
+                    int a = line.IndexOf('=');
+                    if (a > -1)
+                    {
+                        buf[line.Substring(0, a).Trim()] = line.Substring(a + 1).Trim();
+                    }
+
+
+                }
+                Console.Write("testing successful!");
+            }
+
+        }
+    }
+}
\ No newline at end of file
diff --git a/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs b/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs
index bc519614f..efe09dba3 100644
--- a/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs
+++ b/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs
@@ -19,38 +19,38 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
         [Test]
         public void TestParamaters()
         {
-            SABERParameters[] parameters = {
-                    SABERParameters.lightsaberkem128r3,
-                    SABERParameters.saberkem128r3,
-                    SABERParameters.firesaberkem128r3,
-                    SABERParameters.lightsaberkem192r3,
-                    SABERParameters.saberkem192r3,
-                    SABERParameters.firesaberkem192r3,
-                    SABERParameters.lightsaberkem256r3,
-                    SABERParameters.saberkem256r3,
-                    SABERParameters.firesaberkem256r3,
+            SaberParameters[] parameters = {
+                    SaberParameters.lightsaberkem128r3,
+                    SaberParameters.saberkem128r3,
+                    SaberParameters.firesaberkem128r3,
+                    SaberParameters.lightsaberkem192r3,
+                    SaberParameters.saberkem192r3,
+                    SaberParameters.firesaberkem192r3,
+                    SaberParameters.lightsaberkem256r3,
+                    SaberParameters.saberkem256r3,
+                    SaberParameters.firesaberkem256r3,
                 };
 
-            Assert.AreEqual(128, SABERParameters.lightsaberkem128r3.DefaultKeySize);
-            Assert.AreEqual(128, SABERParameters.saberkem128r3.DefaultKeySize);
-            Assert.AreEqual(128, SABERParameters.firesaberkem128r3.DefaultKeySize);
-            Assert.AreEqual(192, SABERParameters.lightsaberkem192r3.DefaultKeySize);
-            Assert.AreEqual(192, SABERParameters.saberkem192r3.DefaultKeySize);
-            Assert.AreEqual(192, SABERParameters.firesaberkem192r3.DefaultKeySize);
-            Assert.AreEqual(256, SABERParameters.lightsaberkem256r3.DefaultKeySize);
-            Assert.AreEqual(256, SABERParameters.saberkem256r3.DefaultKeySize);
-            Assert.AreEqual(256, SABERParameters.firesaberkem256r3.DefaultKeySize);
+            Assert.AreEqual(128, SaberParameters.lightsaberkem128r3.DefaultKeySize);
+            Assert.AreEqual(128, SaberParameters.saberkem128r3.DefaultKeySize);
+            Assert.AreEqual(128, SaberParameters.firesaberkem128r3.DefaultKeySize);
+            Assert.AreEqual(192, SaberParameters.lightsaberkem192r3.DefaultKeySize);
+            Assert.AreEqual(192, SaberParameters.saberkem192r3.DefaultKeySize);
+            Assert.AreEqual(192, SaberParameters.firesaberkem192r3.DefaultKeySize);
+            Assert.AreEqual(256, SaberParameters.lightsaberkem256r3.DefaultKeySize);
+            Assert.AreEqual(256, SaberParameters.saberkem256r3.DefaultKeySize);
+            Assert.AreEqual(256, SaberParameters.firesaberkem256r3.DefaultKeySize);
         }
 
         [Test]
         public void TestVectors()
         {
 
-            SABERParameters[] saberParameters = 
+            SaberParameters[] saberParameters = 
             {
-                SABERParameters.lightsaberkem256r3,
-                SABERParameters.saberkem256r3,
-                SABERParameters.firesaberkem256r3,
+                SaberParameters.lightsaberkem256r3,
+                SaberParameters.saberkem256r3,
+                SaberParameters.firesaberkem256r3,
             };
             String[] files = 
             {
@@ -90,31 +90,31 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
                             byte[] ss = Hex.Decode(buf["ss"]); // session key
 
                             NistSecureRandom random = new NistSecureRandom(seed, null);
-                            SABERParameters parameters = saberParameters[fileIndex];
+                            SaberParameters parameters = saberParameters[fileIndex];
 
-                            SABERKeyPairGenerator kpGen = new SABERKeyPairGenerator();
-                            SABERKeyGenerationParameters
-                                genParam = new SABERKeyGenerationParameters(random, parameters);
+                            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(
+                            SaberPublicKeyParameters pubParams =
+                                (SaberPublicKeyParameters) PublicKeyFactory.CreateKey(
                                     SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(
-                                        (SABERPublicKeyParameters) kp.Public));
-                            SABERPrivateKeyParameters privParams =
-                                (SABERPrivateKeyParameters) PrivateKeyFactory.CreateKey(
-                                    PrivateKeyInfoFactory.CreatePrivateKeyInfo((SABERPrivateKeyParameters) kp.Private));
+                                        (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);
+                            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");
@@ -122,7 +122,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
                             Assert.True(Arrays.AreEqual(ss, 0, secret.Length, secret, 0, secret.Length), name + " " + count + ": kem_enc key");
 
                             // KEM Dec
-                            SABERKEMExtractor SABERDecCipher = new SABERKEMExtractor(privParams);
+                            SaberKemExtractor SABERDecCipher = new SaberKemExtractor(privParams);
 
                             byte[] dec_key = SABERDecCipher.ExtractSecret(generated_cipher_text);