Ejemplo n.º 1
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 CSPPrng().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 (!Evaluate.False(kp1.Equals(kp3)))
            {
                throw new Exception("NtruEncryptTest: key pair generation test failed!");
            }
        }
Ejemplo n.º 2
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 (!Evaluate.Equals(param.GetOutputLength(), encrypted.Length))
                {
                    throw new Exception("NtruEncryptTest: output length test failed!");
                }
            }
        }
Ejemplo n.º 3
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();
            }

            return(runTimer.Elapsed.TotalMilliseconds);
        }
Ejemplo n.º 4
0
        private void IsValid()
        {
            // test valid key pairs
            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.EES1087EP2FAST.DeepCopy(),
                (NTRUParameters)NTRUParamSets.EES1499EP1.DeepCopy(),
                (NTRUParameters)NTRUParamSets.EES1499EP1FAST.DeepCopy(),
            };

            foreach (NTRUParameters ep in paramSets)
            {
                NTRUKeyGenerator ntru = new NTRUKeyGenerator(ep);
                NTRUKeyPair      kp1  = (NTRUKeyPair)ntru.GenerateKeyPair();
                if (!Evaluate.True(kp1.IsValid()))
                {
                    throw new Exception("NtruKeyPair generated key pair is invalid!");
                }
            }
        }
Ejemplo n.º 5
0
        private void Encode(NTRUParameters param)
        {
            NTRUKeyPair kp;

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

            // encode to byte[] and reconstruct
            byte[]      enc = kp.ToBytes();
            NTRUKeyPair kp2 = new NTRUKeyPair(enc);

            if (!Evaluate.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 (!Evaluate.Equals(kp, kp3))
            {
                throw new Exception("NtruKeyPair encoding test failed!");
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a NTRU Keypair based on your seed and one address
        /// </summary>
        /// <param name="seed">String of your seed</param>
        /// <param name="saltAddress">String of your address</param>
        /// <returns>Key Pair</returns>
        public IAsymmetricKeyPair CreateAsymmetricKeyPair(string seed, string saltAddress)
        {
            var passphrase = Encoding.UTF8.GetBytes(seed);
            var salt       = Encoding.UTF8.GetBytes(saltAddress);

            var keyGen = new NTRUKeyGenerator(this.ntruParameters, false);

            var keys = keyGen.GenerateKeyPair(passphrase, salt);

            return(keys);
        }
Ejemplo n.º 7
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 (!Evaluate.Equals(kp.PublicKey, kp2.PublicKey))
            {
                throw new Exception("EncryptionKey: public key comparison test failed!");
            }
            if (kp.PublicKey.GetHashCode() != kp2.PublicKey.GetHashCode())
            {
                throw new Exception("EncryptionKey: public key hash test failed!");
            }
            if (!Evaluate.Equals(kp.PrivateKey, kp2.PrivateKey))
            {
                throw new Exception("EncryptionKey: private key comparison test failed!");
            }
            if (kp.PrivateKey.GetHashCode() != kp2.PrivateKey.GetHashCode())
            {
                throw new Exception("EncryptionKey: private key hash 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 (!Evaluate.Equals(kp.PublicKey, kp3.PublicKey))
            {
                throw new Exception("EncryptionKey: public key comparison test failed!");
            }
            if (kp.PublicKey.GetHashCode() != kp3.PublicKey.GetHashCode())
            {
                throw new Exception("EncryptionKey: public key hash test failed!");
            }
            if (!Evaluate.Equals(kp.PrivateKey, kp3.PrivateKey))
            {
                throw new Exception("EncryptionKey: private key comparison test failed!");
            }
            if (kp.PrivateKey.GetHashCode() != kp3.PrivateKey.GetHashCode())
            {
                throw new Exception("EncryptionKey: private key hash test failed!");
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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();
        }
Ejemplo n.º 10
0
        private void DigestTest()
        {
            CSPPrng        rng   = new CSPPrng();
            NTRUParameters param = (NTRUParameters)NTRUParamSets.EES1087EP2.DeepCopy();

            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    param.Digest = Digests.Blake2B512;//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 (!Evaluate.AreEqual(plainText, decrypted))
                    {
                        throw new Exception("NtruEncryptTest: digest test failed!");
                    }
                }
            }
        }
Ejemplo 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("Тест шифрования: отказ дешифрования!");
                }
            }
        }
Ejemplo n.º 12
0
        private readonly NTRUParameters encParams = NTRUParamSets.APR2011743FAST; // N743, q2048 , EES743EP1

        public IAsymmetricKeyPair CreateAsymmetricKeyPair()
        {
            var keyGen = new NTRUKeyGenerator(this.encParams);

            return(keyGen.GenerateKeyPair());
        }