Esempio n. 1
0
        private void IsValid()
        {
            // test valid key pairs
            NTRUParameters[] paramSets = new NTRUParameters[] 
            { 
                (NTRUParameters)NTRUParamSets.APR2011439.Clone(),
                (NTRUParameters)NTRUParamSets.APR2011439FAST.Clone(),
                (NTRUParameters)NTRUParamSets.APR2011743.Clone(),
                (NTRUParameters)NTRUParamSets.APR2011743FAST.Clone(),
                (NTRUParameters)NTRUParamSets.EES1087EP2.Clone(),
                (NTRUParameters)NTRUParamSets.EES1087EP2FAST.Clone(),
                (NTRUParameters)NTRUParamSets.EES1499EP1.Clone(),
                (NTRUParameters)NTRUParamSets.EES1499EP1FAST.Clone(),
            };

            foreach (NTRUParameters ep in paramSets)
            {
                NTRUKeyGenerator ntru = new NTRUKeyGenerator(ep);
                NTRUKeyPair kp1 = (NTRUKeyPair)ntru.GenerateKeyPair();
                if (!Compare.True(kp1.IsValid()))
                    throw new Exception("NtruKeyPair generated key pair is invalid!");
            }
        }
Esempio n. 2
0
        private void Encode(NTRUParameters param)
        {
            NTRUKeyGenerator ntru = new NTRUKeyGenerator(param);
            NTRUKeyPair kp = (NTRUKeyPair)ntru.GenerateKeyPair();
            byte[] priv = ((NTRUPrivateKey)kp.PrivateKey).ToBytes();
            byte[] pub = ((NTRUPublicKey)kp.PublicKey).ToBytes();
            NTRUKeyPair kp2 = new NTRUKeyPair(new NTRUPublicKey(pub), new NTRUPrivateKey(priv));
            if (!Compare.Equals(kp.PublicKey, kp2.PublicKey))
                throw new Exception("EncryptionKey: public key comparison test failed!");
            if (!Compare.Equals(kp.PrivateKey, kp2.PrivateKey))
                throw new Exception("EncryptionKey: private key comparison test failed!");

            MemoryStream bos1 = new MemoryStream();
            MemoryStream bos2 = new MemoryStream();
            ((NTRUPrivateKey)kp.PrivateKey).WriteTo(bos1);
            ((NTRUPublicKey)kp.PublicKey).WriteTo(bos2);
            MemoryStream bis1 = new MemoryStream(bos1.ToArray());
            MemoryStream bis2 = new MemoryStream(bos2.ToArray());
            NTRUKeyPair kp3 = new NTRUKeyPair(new NTRUPublicKey(bis2), new NTRUPrivateKey(bis1));
            if (!Compare.Equals(kp.PublicKey, kp3.PublicKey))
                throw new Exception("EncryptionKey: public key comparison test failed!");
            if (!Compare.Equals(kp.PrivateKey, kp3.PrivateKey))
                throw new Exception("EncryptionKey: private key comparison test failed!");
        }
Esempio n. 3
0
        /// <summary>
        /// Get the asymmetric generator instance
        /// </summary>
        /// 
        /// <param name="Parameters">The cipher parameters</param>
        /// 
        /// <returns>The generator instance</returns>
        private IAsymmetricGenerator GetAsymmetricGenerator(IAsymmetricParameters Parameters)
        {
            IAsymmetricGenerator gen = null;

            try
            {
                if (Parameters.GetType().Equals(typeof(NTRUParameters)))
                    gen = new NTRUKeyGenerator((NTRUParameters)Parameters);
                else if (Parameters.GetType().Equals(typeof(MPKCParameters)))
                    gen = new MPKCKeyGenerator((MPKCParameters)Parameters);
                else if (Parameters.GetType().Equals(typeof(RLWEParameters)))
                    gen = new RLWEKeyGenerator((RLWEParameters)Parameters);

                return gen;
            }
            catch (Exception ex)
            {
                throw new CryptoProcessingException("DtmKex:GetAsymmetricGenerator", "The generator could not be loaded!", ex);
            }
        }
Esempio n. 4
0
        private void DigestTest()
        {
            CSPRng rng = new CSPRng();
            NTRUParameters param = (NTRUParameters)NTRUParamSets.EES1087EP2.DeepCopy();

            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                    param.Digest = Digests.Blake512;//blake512
                else if (i == 1)
                    param.Digest = Digests.Keccak512;//keccak512
                else
                    param.Digest = Digests.Skein512;//skein512

                NTRUKeyPair kp;
                using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param))
                    kp = (NTRUKeyPair)kg.GenerateKeyPair();

                using (NTRUEncrypt ntru = new NTRUEncrypt(param))
                {
                    byte[] plainText = rng.GetBytes(32);
                    ntru.Initialize(kp.PublicKey);
                    byte[] encrypted = ntru.Encrypt(plainText);
                    ntru.Initialize(kp);
                    byte[] decrypted = ntru.Decrypt(encrypted);

                    if (!Compare.AreEqual(plainText, decrypted))
                        throw new Exception("NtruEncryptTest: digest test failed!");
                }
            }
        }
Esempio n. 5
0
        private void GetOutputLength()
        {
            NTRUParameters[] paramSets = new NTRUParameters[]
            {
                (NTRUParameters)NTRUParamSets.APR2011439.DeepCopy(),
                (NTRUParameters)NTRUParamSets.APR2011439FAST.DeepCopy(),
                (NTRUParameters)NTRUParamSets.APR2011743.DeepCopy(),
                (NTRUParameters)NTRUParamSets.APR2011743FAST.DeepCopy(),
                (NTRUParameters)NTRUParamSets.EES1087EP2.DeepCopy(),
                (NTRUParameters)NTRUParamSets.EES1171EP1.DeepCopy(),
                (NTRUParameters)NTRUParamSets.EES1499EP1.DeepCopy(),
            };

            byte[] plainText = ByteUtils.GetBytes("secret encrypted text");

            foreach (NTRUParameters param in paramSets)
            {
                NTRUKeyPair kp;
                using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param))
                    kp = (NTRUKeyPair)kg.GenerateKeyPair();

                NTRUEncrypt ntru = new NTRUEncrypt(param);
                ntru.Initialize(kp.PublicKey);
                byte[] encrypted = ntru.Encrypt(plainText);
                if (!Compare.Equals(param.GetOutputLength(), encrypted.Length))
                    throw new Exception("NtruEncryptTest: output length test failed!");
            }
        }
Esempio n. 6
0
        // tests key pair generation from a passphrase
        private void GenerateKeyPair()
        {
            NTRUParameters param = (NTRUParameters)NTRUParamSets.EES1087EP2FAST.DeepCopy();
            byte[] passphrase = Encoding.Unicode.GetBytes("password123");
            byte[] salt = new CSPRng().GetBytes(16);

            NTRUKeyPair kp1;
            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false)) // note: parallel must be turned off with passphrase prng
                kp1 = (NTRUKeyPair)kg.GenerateKeyPair(passphrase, salt);
            NTRUKeyPair kp2;
            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false))
                kp2 = (NTRUKeyPair)kg.GenerateKeyPair(passphrase, salt);

            NTRUEncrypt ntru = new NTRUEncrypt(param);
            NTRUEncrypt ntru2 = new NTRUEncrypt(NTRUParamSets.EES1087EP2FAST);

            if (!kp1.Equals(kp2))
                throw new Exception("NtruEncryptTest: key pair generation test failed!");

            salt = ntru.GenerateSalt();
            NTRUKeyPair kp3;
            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false))
                kp3 = (NTRUKeyPair)kg.GenerateKeyPair(passphrase, salt);

            if (!Compare.False(kp1.Equals(kp3)))
                throw new Exception("NtruEncryptTest: key pair generation test failed!");
        }
Esempio n. 7
0
        // params must have df1..df3 and dr1..dr3 set as well as df and dr
        private void EncryptDecrypt(NTRUParameters param)
        {
            NTRUKeyPair kp;
            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param))
                kp = (NTRUKeyPair)kg.GenerateKeyPair();

            NTRUEncrypt ntru = new NTRUEncrypt(param);
            TextTest(ntru, kp, param);
            // sparse/dense
            param.Sparse = !param.Sparse;
            TextTest(ntru, kp, param);
            param.Sparse = !param.Sparse;
            EmptyTest(ntru, kp, param);
            MaxLength(ntru, kp, param);
            TooLong(ntru, kp, param);

            kp.Dispose();
            ntru.Dispose();
        }
Esempio n. 8
0
        static double Encrypt(int Iterations, NTRUParameters Param)
        {
            NTRUKeyGenerator mkgen = new NTRUKeyGenerator(Param);
            IAsymmetricKeyPair akp = mkgen.GenerateKeyPair();
            byte[] ptext = new CSPRng().GetBytes(64);
            byte[] ctext;
            Stopwatch runTimer = new Stopwatch();

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

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

            return runTimer.Elapsed.TotalMilliseconds;
        }
Esempio n. 9
0
        static double Decrypt(int Iterations, NTRUParameters Param)
        {
            NTRUKeyGenerator mkgen = new NTRUKeyGenerator(Param);
            IAsymmetricKeyPair akp = mkgen.GenerateKeyPair();
            byte[] ptext = new CSPRng().GetBytes(64);
            byte[] rtext = new byte[64];
            byte[] ctext;
            Stopwatch runTimer = new Stopwatch();

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

                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;
        }
Esempio n. 10
0
        static double KeyGenerator(int Iterations, NTRUParameters Param)
        {
            NTRUKeyGenerator mkgen = new NTRUKeyGenerator(Param);
            IAsymmetricKeyPair akp;
            Stopwatch runTimer = new Stopwatch();

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

            return runTimer.Elapsed.TotalMilliseconds;
        }
Esempio n. 11
0
        static void FullCycle()
        {
            NTRUParameters mpar = NTRUParamSets.APR2011439FAST; //APR2011743FAST
            NTRUKeyGenerator mkgen = new NTRUKeyGenerator(mpar);
            IAsymmetricKeyPair akp = mkgen.GenerateKeyPair();
            byte[] enc;

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

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

                if (!Compare.AreEqual(dec, data))
                    throw new Exception("Encryption test: decryption failure!");
            }
        }
Esempio n. 12
0
        private void Encode(NTRUParameters param)
        {
            NTRUKeyPair kp;
            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false))
                kp = (NTRUKeyPair)kg.GenerateKeyPair();

            // encode to byte[] and reconstruct
            byte[] enc = kp.ToBytes();
            NTRUKeyPair kp2 = new NTRUKeyPair(enc);
            if (!Compare.Equals(kp, kp2))
                throw new Exception("NtruKeyPair encoding test failed!");

            // encode to OutputStream and reconstruct
            MemoryStream bos = new MemoryStream();
            kp.WriteTo(bos);
            MemoryStream bis = new MemoryStream(bos.ToArray());
            NTRUKeyPair kp3 = new NTRUKeyPair(bis);
            if (!Compare.Equals(kp, kp3))
                throw new Exception("NtruKeyPair encoding test failed!");
        }