Beispiel #1
0
        private void TestSign()
        {
            RLWEParameters     mpar  = RLWEParamSets.RLWEN512Q12289;
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(mpar);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(akp.PublicKey);

                int    sz   = sgn.MaxPlainText;
                byte[] data = new byte[200];
                new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPRng().GetBytes(data);

                byte[] code = sgn.Sign(data, 0, data.Length);

                sgn.Initialize(akp.PrivateKey);
                if (!sgn.Verify(data, 0, data.Length, code))
                {
                    throw new Exception("EncryptionKey: private key comparison test failed!");
                }
                OnProgress(new TestEventArgs("Passed byte sign and verify"));

                sgn.Initialize(akp.PublicKey);
                code = sgn.Sign(new MemoryStream(data));

                sgn.Initialize(akp.PrivateKey);
                if (!sgn.Verify(new MemoryStream(data), code))
                {
                    throw new Exception("EncryptionKey: private key comparison test failed!");
                }
                OnProgress(new TestEventArgs("Passed stream sign and verify"));
            }
        }
Beispiel #2
0
        private void TestKey()
        {
            RLWEParameters     encParams = RLWEParamSets.RLWEN256Q7681;
            RLWEKeyGenerator   keyGen    = new RLWEKeyGenerator(encParams);
            IAsymmetricKeyPair keyPair   = keyGen.GenerateKeyPair();

            byte[] enc, dec, data;

            // encrypt an array
            using (RLWEEncrypt cipher = new RLWEEncrypt(encParams))
            {
                cipher.Initialize(keyPair.PublicKey);
                data = new byte[cipher.MaxPlainText];
                new CSPRng().GetBytes(data);
                enc = cipher.Encrypt(data);
            }

            // decrypt the cipher text
            using (RLWEEncrypt cipher = new RLWEEncrypt(encParams))
            {
                cipher.Initialize(keyPair.PrivateKey);
                dec = cipher.Decrypt(enc);
            }

            if (!Compare.AreEqual(dec, data))
            {
                throw new Exception("TestKey test: decryption failure!");
            }
            OnProgress(new TestEventArgs("Passed sub-key test"));
        }
Beispiel #3
0
        private void TestEncrypt(RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[] enc;

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);

                int    sz   = mpe.MaxPlainText;
                byte[] data = new byte[sz];
                new CSPRng().GetBytes(data);

                enc = mpe.Encrypt(data);

                mpe.Initialize(akp.PrivateKey);
                byte[] dec = mpe.Decrypt(enc);

                if (!Compare.AreEqual(dec, data))
                {
                    throw new Exception("Encryption test: decryption failure!");
                }
                OnProgress(new TestEventArgs(string.Format("Passed N:{0} Q:{1} encryption test", Param.N, Param.Q)));
            }
        }
Beispiel #4
0
        static double Decrypt(int Iterations, RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(Param.N >> 3);
            byte[]    rtext = new byte[Param.N >> 3];
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);
                ctext = mpe.Encrypt(ptext);
                mpe.Initialize(akp.PrivateKey);

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                {
                    rtext = mpe.Decrypt(ctext);
                }
                runTimer.Stop();
            }

            //if (!Compare.AreEqual(ptext, rtext))
            //    throw new Exception("Encryption test: decryption failure!");

            return(runTimer.Elapsed.TotalMilliseconds);
        }
Beispiel #5
0
        private void TestParams()
        {
            RLWEParameters mpar = RLWEParamSets.RLWEN256Q7681;

            byte[] enc = mpar.ToBytes();

            using (RLWEParameters mpar2 = RLWEParameters.From(enc))
            {
                if (!mpar.Equals(mpar2))
                {
                    throw new Exception("EncryptionKey: public key comparison test failed!");
                }
            }
            OnProgress(new TestEventArgs("Passed parameters byte serialization"));

            MemoryStream mstr = mpar.ToStream();

            using (RLWEParameters mpar2 = RLWEParameters.From(mstr))
            {
                if (!mpar.Equals(mpar2))
                {
                    throw new Exception("EncryptionKey: public key comparison test failed!");
                }
            }
            OnProgress(new TestEventArgs("Passed parameters stream serialization"));
        }
Beispiel #6
0
        private void TestParams()
        {
            RLWEParameters mpar = (RLWEParameters)RLWEParamSets.RLWEN256Q7681.DeepCopy();

            byte[] enc = mpar.ToBytes();

            using (RLWEParameters mpar2 = RLWEParameters.From(enc))
            {
                if (!mpar.Equals(mpar2))
                {
                    throw new Exception("Parameters: public key comparison test failed!");
                }
                if (mpar.GetHashCode() != mpar2.GetHashCode())
                {
                    throw new Exception("Parameters: parameters hash test failed!");
                }
            }
            OnProgress(new TestEventArgs("Passed parameters byte serialization"));

            MemoryStream mstr = mpar.ToStream();

            using (RLWEParameters mpar2 = RLWEParameters.From(mstr))
            {
                if (!mpar.Equals(mpar2))
                {
                    throw new Exception("Parameters: public key comparison test failed!");
                }
            }
            OnProgress(new TestEventArgs("Passed parameters stream serialization"));
        }
Beispiel #7
0
        //Generates a Key pair using the ED25519 algorithms
        //initializes internal public and private keys, which can then be exported as required
        public bool GenerateKeyPair()
        {
            RLWEParameters     mpar    = RLWEParamSets.RLWEN512Q12289;
            RLWEKeyGenerator   mkgen   = new RLWEKeyGenerator(mpar);
            IAsymmetricKeyPair keyPair = mkgen.GenerateKeyPair();

            RLWEPublicKey  pubKey  = (RLWEPublicKey)keyPair.PublicKey;
            RLWEPrivateKey privKey = (RLWEPrivateKey)keyPair.PrivateKey;

            //Private and public keys appear reversed in this signing inplementation
            _privateKey = new PrivateKey((RLWEPublicKey)keyPair.PublicKey);
            _publicKey  = new PublicKey((RLWEPrivateKey)keyPair.PrivateKey);

            return(true);
        }
Beispiel #8
0
        //signs a message using the internal private and public keys
        public string SignMessage(string message)
        {
            RLWEParameters mpar      = RLWEParamSets.RLWEN512Q12289;
            string         signature = "";

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(_privateKey.Key);
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);

                byte[] signatureBytes = sgn.Sign(messageBytes, 0, messageBytes.Length);
                signature = Convert.ToBase64String(signatureBytes);
            }

            return(signature);
        }
Beispiel #9
0
        static double KeyGenerator(int Iterations, RLWEParameters Param)
        {
            // new SP20Prng(SeedGenerators.CSPRsg, 16384, 32, 10) // salsa20
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param, new CTRPrng(BlockCiphers.RDX, SeedGenerators.CSPRsg, 16384, 16)); // aes128
            IAsymmetricKeyPair akp;
            Stopwatch          runTimer = new Stopwatch();

            runTimer.Start();
            for (int i = 0; i < Iterations; i++)
            {
                akp = mkgen.GenerateKeyPair();
            }
            runTimer.Stop();

            return(runTimer.Elapsed.TotalMilliseconds);
        }
Beispiel #10
0
        //verify provided message with the signature and public key provided
        public bool VerifySignature(string message, string signature, IPublicKey publicKey)
        {
            bool result = false;

            RLWEParameters mpar = RLWEParamSets.RLWEN512Q12289;

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(((PublicKey)publicKey).Key);
                byte[] messageBytes   = Encoding.UTF8.GetBytes(message);
                byte[] signatureBytes = Convert.FromBase64String(signature);

                result = sgn.Verify(messageBytes, 0, messageBytes.Length, signatureBytes);
            }

            return(result);
        }
Beispiel #11
0
        //signs a message using the private and public keys provided
        public string SignMessage(string message, IPrivateKey privateKey, IPublicKey publicKey = null)
        {
            //NB: public key isn't required by this implementation, so is initialized to null
            RLWEParameters mpar      = RLWEParamSets.RLWEN512Q12289;
            string         signature = "";

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(((PrivateKey)(privateKey)).Key);
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);

                byte[] signatureBytes = sgn.Sign(messageBytes, 0, messageBytes.Length);
                signature = Convert.ToBase64String(signatureBytes);
            }

            return(signature);
        }
Beispiel #12
0
        static void CycleTest(int Iterations, RLWEParameters Param)
        {
            Stopwatch runTimer = new Stopwatch();

            runTimer.Start();
            for (int i = 0; i < Iterations; i++)
            {
                FullCycle(Param);
            }
            runTimer.Stop();

            double elapsed = runTimer.Elapsed.TotalMilliseconds;

            Console.WriteLine(string.Format("{0} cycles completed in: {1} ms", Iterations, elapsed));
            Console.WriteLine(string.Format("Average cycle time: {0} ms", elapsed / Iterations));
            Console.WriteLine("");
        }
Beispiel #13
0
        static void FullCycle(RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[] enc;

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);

                byte[] data = new byte[mpe.MaxPlainText];
                enc = mpe.Encrypt(data);
                mpe.Initialize(akp.PrivateKey);
                byte[] dec = mpe.Decrypt(enc);

                if (!Compare.AreEqual(dec, data))
                {
                    throw new Exception("Encryption test: decryption failure!");
                }
            }
        }
Beispiel #14
0
        static double Encrypt(int Iterations, RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(Param.N >> 3);
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                {
                    ctext = mpe.Encrypt(ptext);
                }
                runTimer.Stop();
            }

            return(runTimer.Elapsed.TotalMilliseconds);
        }
Beispiel #15
0
        private void TestEncode()
        {
            RLWEParameters     mpar  = RLWEParamSets.RLWEN256Q7681;
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(mpar);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            RLWEPublicKey pub = (RLWEPublicKey)akp.PublicKey;

            byte[] enc = pub.ToBytes();
            using (RLWEPublicKey pub2 = RLWEPublicKey.From(enc))
            {
                if (!pub.Equals(pub2))
                {
                    throw new Exception("EncryptionKey: public key comparison test failed!");
                }
            }
            OnProgress(new TestEventArgs("Passed public key serialization"));

            MemoryStream pubstr = pub.ToStream();

            using (RLWEPublicKey pub2 = RLWEPublicKey.From(pubstr))
            {
                if (!pub.Equals(pub2))
                {
                    throw new Exception("EncryptionKey: public key comparison test failed!");
                }
            }
            pubstr.Dispose();
            OnProgress(new TestEventArgs("Passed public key stream test"));

            RLWEPrivateKey pri = (RLWEPrivateKey)akp.PrivateKey;

            enc = pri.ToBytes();
            using (RLWEPrivateKey pri2 = RLWEPrivateKey.From(enc))
            {
                if (!pri.Equals(pri2))
                {
                    throw new Exception("EncryptionKey: private key comparison test failed!");
                }
            }
            OnProgress(new TestEventArgs("Passed private key serialization"));

            MemoryStream pristr = pri.ToStream();

            using (RLWEPrivateKey pri2 = RLWEPrivateKey.From(pristr))
            {
                if (!pri.Equals(pri2))
                {
                    throw new Exception("EncryptionKey: private key comparison test failed!");
                }
            }
            pristr.Dispose();
            OnProgress(new TestEventArgs("Passed private key stream test"));

            using (RLWEEncrypt mpe = new RLWEEncrypt(mpar))
            {
                mpe.Initialize(akp.PublicKey);

                int    sz   = mpe.MaxPlainText;
                byte[] data = new byte[sz];
                new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPRng().GetBytes(data);

                enc = mpe.Encrypt(data);

                mpe.Initialize(akp.PrivateKey);
                byte[] dec = mpe.Decrypt(enc);

                if (!Compare.AreEqual(dec, data))
                {
                    throw new Exception("EncryptionKey: decryption failure!");
                }
                OnProgress(new TestEventArgs("Passed encryption test"));
            }

            pri.Dispose();
            pub.Dispose();
        }