Ejemplo n.º 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"));
            }
        }
Ejemplo n.º 2
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)));
            }
        }
Ejemplo n.º 3
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"));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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!");
                }
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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();
        }