예제 #1
0
 private void Setup()
 {
     _seed = new byte[100];
     new Random().NextBytes(_seed);
     _parameters = NTRUParamSets.APR2011743;
     _gen = new IndexGenerator(_seed, _parameters);
     _indices = initIndices();
 }
 private void SetUp()
 {
     _parameters = NTRUParamSets.APR2011439FAST;
     _N = _parameters.N;
     _df1 = _parameters.DF1;
     _df2 = _parameters.DF2;
     _df3 = _parameters.DF3;
     _Q = _parameters.Q;
     _rng = new Random();
 }
예제 #3
0
        /// <summary>
        /// Constructs a new instance with a set of encryption parameters
        /// </summary>
        ///
        /// <param name="CipherParams">Encryption parameters</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        ///
        /// <exception cref="CryptoAsymmetricException">Thrown if a Prng that requires pre-initialization is specified; (wrong constructor)</exception>
        public NTRUKeyGenerator(NTRUParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine == Prngs.PBPrng)
            {
                throw new CryptoAsymmetricException("MPKCKeyGenerator:Ctor", "Passphrase based digest and CTR generators must be pre-initialized, use the other constructor!", new ArgumentException());
            }

            this.m_frcLinear          = ParallelUtils.ForceLinear;
            ParallelUtils.ForceLinear = !Parallel;
            this.m_ntruParams         = CipherParams;
            this.m_rndEngine          = this.GetPrng(this.m_ntruParams.RandomEngine);
        }
예제 #4
0
        /// <summary>
        /// Use an initialized prng to generate the key; use this constructor with an Rng that requires pre-initialization, i.e. PBPrng
        /// </summary>
        ///
        /// <param name="CipherParams">The NTRUParameters instance containing the cipher settings</param>
        /// <param name="RngEngine">An initialized Prng instance</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        public NTRUKeyGenerator(NTRUParameters CipherParams, IRandom RngEngine, bool Parallel = true)
        {
            this.m_frcLinear = ParallelUtils.ForceLinear;
            // passphrase gens must be linear processed
            if (RngEngine.GetType().Equals(typeof(PBPRng)))
            {
                ParallelUtils.ForceLinear = true;
            }
            else
            {
                ParallelUtils.ForceLinear = !Parallel;
            }

            this.m_ntruParams = CipherParams;
            // set source of randomness
            this.m_rndEngine = RngEngine;
        }
예제 #5
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!");
            }
        }
예제 #6
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!");
        }
예제 #7
0
        private void Encode()
        {
            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 param in paramSets)
                Encode(param);
        }
예제 #8
0
 /// <summary>
 /// Constructs a new instance with a set of encryption parameters
 /// </summary>
 /// 
 /// <param name="NtruParams">Encryption parameters</param>
 public NTRUEncrypt(NTRUParameters NtruParams)
 {
     _encParams = NtruParams;
     _dgtEngine = GetDigest(_encParams.Digest);
     _rndEngine = GetPrng(_encParams.RandomEngine);
 }
예제 #9
0
 /// <summary>
 /// Constructs a new instance with a set of encryption parameters
 /// </summary>
 ///
 /// <param name="NtruParams">Encryption parameters</param>
 public NTRUEncrypt(NTRUParameters NtruParams)
 {
     _encParams = NtruParams;
     _dgtEngine = GetDigest(_encParams.Digest);
     _rndEngine = GetPrng(_encParams.RandomEngine);
 }
예제 #10
0
        // tests a message that is too long
        private void TooLong(NTRUEncrypt ntru, NTRUKeyPair kp, NTRUParameters param)
        {
            byte[] plainText = new byte[param.MessageMax + 1];
            try
            {
                Array.Copy(ByteUtils.GetBytes("secret encrypted text"), 0, plainText, 0, 21);
                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: maximum length test failed!");
                // should have thrown
                throw new Exception("NtruEncryptTest: maximum exceeded message test failed!");
            }
            catch
            {
            }
        }
예제 #11
0
        // encrypts and decrypts text
        private void TextTest(NTRUEncrypt ntru, NTRUKeyPair kp, NTRUParameters param)
        {
            byte[] plainText = ByteUtils.GetBytes("text to encrypt");
            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: text encryption test failed!");
        }
예제 #12
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!");
            }
        }
예제 #13
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;
        }
예제 #14
0
        /// <summary>
        /// Constructs a new instance with a set of encryption parameters
        /// </summary>
        /// 
        /// <param name="CipherParams">Encryption parameters</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        /// 
        /// <exception cref="CryptoAsymmetricException">Thrown if a Prng that requires pre-initialization is specified; (wrong constructor)</exception>
        public NTRUKeyGenerator(NTRUParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine == Prngs.PBPrng)
                throw new CryptoAsymmetricException("MPKCKeyGenerator:Ctor", "Passphrase based digest and CTR generators must be pre-initialized, use the other constructor!", new ArgumentException());

            _frcLinear = ParallelUtils.ForceLinear;
            ParallelUtils.ForceLinear = !Parallel;
            _ntruParams = CipherParams;
            _rndEngine = GetPrng(_ntruParams.RandomEngine);
        }
예제 #15
0
        /// <summary>
        /// Constructs a new instance with a set of encryption parameters
        /// </summary>
        /// 
        /// <param name="CipherParams">Encryption parameters</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        public NTRUKeyGenerator(NTRUParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine != Prngs.CTRPrng || CipherParams.RandomEngine != Prngs.CSPRng)
                _isParallel = false;
            else
                _isParallel = Parallel;

            _ntruParams = CipherParams;
            _rndEngine = GetPrng(_ntruParams.RandomEngine);
        }
예제 #16
0
        /// <summary>
        /// Get a serialized NTRUParameters class from a parameter name.
        /// <para>Can optionally randomize Db, MinIGFHashCalls, and MinMGFHashCalls fields using the Multiplier parameters.</para>
        /// </summary>
        ///
        /// <param name="ParamName">The NTRU Parameters set name</param>
        ///
        /// <param name="DbMaxReductionFactor">If set between <c>0.1</c> and <c>0.4</c>, randomizes the Db field length, if <c>0.0</c> (default), returns the serialized parameter set default value.
        /// <para>The Db field is set to a ranged random number from the default parameter set <c>value</c> to <c>value - (value * multiplier)</c>.
        /// The Db field determines how many bytes of random are prepended to the message before encryption.
        /// Recommended range is minimum 0.1, to a maximum of 0.4.</para></param>
        ///
        /// <param name="IgfMaxAccretionFactor">If set between <c>0.1</c> and <c>0.5</c>, the MinIGFHashCalls value is randomized, if <c>0.0</c> (default), returns the parameter set default value.
        /// <para>The MinIGFHashCalls is set to a random value ranged between the parameter sets default <c>value</c>, up to <c>value + (value * multiplier)</c>.
        /// The MinIGFHashCalls value determine the number of times the hashing function is cycled during the igf polynomial generation.
        /// Recommended range is minimum 0.1, to a maximum of 0.2; larger values significantly impact processing times.</para></param>
        ///
        /// <param name="MgfMaxAccretionFactor">If set between <c>0.1</c> and <c>0.5</c>, the MinMGFHashCalls value is randomized, if <c>0.0</c> (default), returns the parameter set default value.
        /// <para>The MinMGFHashCalls is set to a random value ranged between the parameter sets default <c>value</c>, up to <c>value + (value * multiplier)</c>.
        /// The MinMGFHashCalls value determine the number of times the hashing function is cycled during the igf mask polynomial generation.
        /// Recommended range is minimum 0.1, to a maximum of 0.2; larger values significantly impact processing times.</para></param>
        ///
        /// <returns>The serialized NTRUParameters set</returns>
        ///
        /// <exception cref="CryptoAsymmetricException">Thrown if the input value is out of range</exception>
        public static byte[] GetFormatted(NTRUParamNames ParamName, double DbMaxReductionFactor = 0.0, double IgfMaxAccretionFactor = 0.0, double MgfMaxAccretionFactor = 0.0)
        {
            if (DbMaxReductionFactor > 0.4 || DbMaxReductionFactor < 0.0)
            {
                throw new CryptoAsymmetricException("NTRUParameters:GetFormatted", "The DbMaxReductionFactor value can not be less than 0.0 and cannot exceed 0.4!", new ArgumentOutOfRangeException());
            }
            if (IgfMaxAccretionFactor > 0.5 || IgfMaxAccretionFactor < 0.0)
            {
                throw new CryptoAsymmetricException("NTRUParameters:GetFormatted", "The IgfMaxAccretionFactor value can not be less than 0.0 and cannot exceed 0.5!", new ArgumentOutOfRangeException());
            }
            if (MgfMaxAccretionFactor > 0.5 || MgfMaxAccretionFactor < 0.0)
            {
                throw new CryptoAsymmetricException("NTRUParameters:GetFormatted", "The MgfMaxAccretionFactor value can not be less than 0.0 and cannot exceed 0.5!", new ArgumentOutOfRangeException());
            }

            if (DbMaxReductionFactor == 0.0 && IgfMaxAccretionFactor == 0.0 && MgfMaxAccretionFactor == 0.0)
            {
                return(FromName(ParamName).ToBytes());
            }
            else
            {
                NTRUParameters param = FromName(ParamName);
                var            prng  = new Prng.CSPPrng();
                int            diff;

                if (DbMaxReductionFactor > 0.0)
                {
                    diff = (int)Math.Abs(param.Db * DbMaxReductionFactor);

                    // set the db to a random in range of param value, to value - value * DbReduceMultiplier
                    if (diff > 0)
                    {
                        // get a random int
                        int pnum = prng.Next(param.Db - diff, param.Db);
                        // round it, must be divisible by 8
                        param.Db = pnum - pnum % 8;
                    }
                }

                if (IgfMaxAccretionFactor > 0.0)
                {
                    // raise igf calls to default +* IgfIncreaseThreshold
                    diff = (int)Math.Abs(param.MinIGFHashCalls * IgfMaxAccretionFactor);
                    if (diff > 0)
                    {
                        param.MinIGFHashCalls = prng.Next(param.MinIGFHashCalls, param.MinIGFHashCalls + diff);
                    }
                }

                if (MgfMaxAccretionFactor > 0.0)
                {
                    // raise mgf calls to default +* MgfIncreaseThreshold
                    diff = (int)Math.Abs(param.MinMGFHashCalls * MgfMaxAccretionFactor);
                    if (diff > 0)
                    {
                        param.MinMGFHashCalls = prng.Next(param.MinMGFHashCalls, param.MinMGFHashCalls + diff);
                    }
                }
                prng.Dispose();

                return(param.ToBytes());
            }
        }
예제 #17
0
        /// <summary>
        /// Compare this object instance with another
        /// </summary>
        ///
        /// <param name="Obj">Object to compare</param>
        ///
        /// <returns>True if equal, otherwise false</returns>
        public override bool Equals(Object Obj)
        {
            if (this == Obj)
            {
                return(true);
            }
            if (Obj == null)
            {
                return(false);
            }

            NTRUParameters other = (NTRUParameters)Obj;

            if (N != other.N)
            {
                return(false);
            }
            if (BufferLenBits != other.BufferLenBits)
            {
                return(false);
            }
            if (_bufferLenTrits != other._bufferLenTrits)
            {
                return(false);
            }
            if (CBits != other.CBits)
            {
                return(false);
            }
            if (Db != other.Db)
            {
                return(false);
            }
            if (DF != other.DF)
            {
                return(false);
            }
            if (DF1 != other.DF1)
            {
                return(false);
            }
            if (DF2 != other.DF2)
            {
                return(false);
            }
            if (DF3 != other.DF3)
            {
                return(false);
            }
            if (Dg != other.Dg)
            {
                return(false);
            }
            if (Dm0 != other.Dm0)
            {
                return(false);
            }
            if (MaxM1 != other.MaxM1)
            {
                return(false);
            }
            if (DR != other.DR)
            {
                return(false);
            }
            if (DR1 != other.DR1)
            {
                return(false);
            }
            if (DR2 != other.DR2)
            {
                return(false);
            }
            if (DR3 != other.DR3)
            {
                return(false);
            }
            if (FastFp != other.FastFp)
            {
                return(false);
            }
            if (!Digest.Equals(other.Digest))
            {
                return(false);
            }
            if (!RandomEngine.Equals(other.RandomEngine))
            {
                return(false);
            }
            if (HashSeed != other.HashSeed)
            {
                return(false);
            }
            if (Length != other.Length)
            {
                return(false);
            }
            if (MessageMax != other.MessageMax)
            {
                return(false);
            }
            if (MinMGFHashCalls != other.MinMGFHashCalls)
            {
                return(false);
            }
            if (MinIGFHashCalls != other.MinIGFHashCalls)
            {
                return(false);
            }
            if (!Compare.AreEqual(OId, other.OId))
            {
                return(false);
            }
            if (PkLen != other.PkLen)
            {
                return(false);
            }
            if (!PolyType.Equals(other.PolyType))
            {
                return(false);
            }
            if (Q != other.Q)
            {
                return(false);
            }
            if (Sparse != other.Sparse)
            {
                return(false);
            }

            return(true);
        }
예제 #18
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;
        }
예제 #19
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;
        }
예제 #20
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();
        }
예제 #21
0
        // tests a message of the maximum allowed length
        private void MaxLength(NTRUEncrypt ntru, NTRUKeyPair kp, NTRUParameters param)
        {
            byte[] plainText = new byte[param.MaxMsgLenBytes];
            Array.Copy(ByteUtils.GetBytes("secret encrypted text"), 0, plainText, 0, 21);
            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: maximum length test failed!");
        }
예제 #22
0
        /// <summary>
        /// Use an initialized prng to generate the key; use this constructor with an Rng that requires pre-initialization, i.e. PBPrng
        /// </summary>
        /// 
        /// <param name="CipherParams">The NTRUParameters instance containing the cipher settings</param>
        /// <param name="RngEngine">An initialized Prng instance</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        public NTRUKeyGenerator(NTRUParameters CipherParams, IRandom RngEngine, bool Parallel = true)
        {
            _frcLinear = ParallelUtils.ForceLinear;
            // passphrase gens must be linear processed
            if (RngEngine.GetType().Equals(typeof(PBPRng)))
                ParallelUtils.ForceLinear = true;
            else
                ParallelUtils.ForceLinear = !Parallel;

            _ntruParams = CipherParams;
            // set source of randomness
            _rndEngine = RngEngine;
        }
예제 #23
0
파일: DtmKex.cs 프로젝트: modulexcite/CEX
        /// <summary>
        /// Get the asymmetric parameters from a byte array
        /// </summary>
        /// 
        /// <param name="Data">The encoded parameters</param>
        /// 
        /// <returns>The asymmetric parameters</returns>
        private IAsymmetricParameters GetAsymmetricParams(byte[] Data)
        {
            IAsymmetricParameters param = null;

            try
            {
                if (Data.Length > 4)
                {
                    if (Data[0] == (byte)AsymmetricEngines.McEliece)
                        param = new MPKCParameters(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.NTRU)
                        param = new NTRUParameters(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.RingLWE)
                        param = new RLWEParameters(Data);
                }
                else
                {
                    if (Data[0] == (byte)AsymmetricEngines.McEliece)
                        param = MPKCParamSets.FromId(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.NTRU)
                        param = NTRUParamSets.FromId(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.RingLWE)
                        param = RLWEParamSets.FromId(Data);
                }

                return param;
            }
            catch (Exception ex)
            {
                throw new CryptoProcessingException("DtmKex:GetAsymmetricParams", "The param set is unknown!", ex);
            }
        }
예제 #24
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!");
        }