Ejemplo n.º 1
0
        private void Dispose(bool Disposing)
        {
            if (!m_isDisposed && Disposing)
            {
                try
                {
                    if (m_dgtEngine != null)
                    {
                        m_dgtEngine.Dispose();
                        m_dgtEngine = null;
                    }
                    if (m_asyCipher != null)
                    {
                        m_asyCipher.Dispose();
                        m_asyCipher = null;
                    }
                }
                catch { }

                m_isDisposed = true;
            }
        }
Ejemplo n.º 2
0
        private void Dispose(bool Disposing)
        {
            if (!_isDisposed && Disposing)
            {
                try
                {
                    if (_dgtEngine != null)
                    {
                        _dgtEngine.Dispose();
                        _dgtEngine = null;
                    }
                    if (_asyCipher != null)
                    {
                        _asyCipher.Dispose();
                        _asyCipher = null;
                    }
                }
                catch { }

                _isDisposed = true;
            }
        }
Ejemplo n.º 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)));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get the asymmetric cipher instance
        /// </summary>
        /// 
        /// <param name="Parameters">The cipher parameters</param>
        /// 
        /// <returns>The cipher instance</returns>
        private IAsymmetricCipher GetAsymmetricCipher(IAsymmetricParameters Parameters)
        {
            IAsymmetricCipher cipher = null;

            try
            {
                if (Parameters.GetType().Equals(typeof(NTRUParameters)))
                    cipher = new NTRUEncrypt((NTRUParameters)Parameters);
                else if (Parameters.GetType().Equals(typeof(MPKCParameters)))
                    cipher = new MPKCEncrypt((MPKCParameters)Parameters);
                else if (Parameters.GetType().Equals(typeof(RLWEParameters)))
                    cipher = new RLWEEncrypt((RLWEParameters)Parameters);

                return cipher;
            }
            catch (Exception ex)
            {
                throw new CryptoProcessingException("DtmKex:GetAsymmetricCipher", "The cipher could not be loaded!", ex);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 ///
 /// <param name="CipherParams">The RLWE cipher used to encrypt the hash</param>
 /// <param name="Digest">The type of digest engine used</param>
 public RLWESign(RLWEParameters CipherParams, Digests Digest = Digests.SHA512)
 {
     m_asyCipher = new RLWEEncrypt(CipherParams);
     m_dgtEngine = GetDigest(Digest);
 }
Ejemplo n.º 6
0
        private void TestKey()
        {
            RLWEParameters encParams = (RLWEParameters)RLWEParamSets.RLWEN256Q7681.DeepCopy();
            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.º 7
0
        private void TestEncode()
        {
            RLWEParameters mpar = (RLWEParameters)RLWEParamSets.RLWEN256Q7681.DeepCopy();
            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();
        }
Ejemplo n.º 8
0
        private void Dispose(bool Disposing)
        {
            if (!_isDisposed && Disposing)
            {
                try
                {
                    if (_dgtEngine != null)
                    {
                        _dgtEngine.Dispose();
                        _dgtEngine = null;
                    }
                    if (_asyCipher != null)
                    {
                        _asyCipher.Dispose();
                        _asyCipher = null;
                    }
                }
                catch { }

                _isDisposed = true;
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 /// 
 /// <param name="CipherParams">The RLWE cipher used to encrypt the hash</param>
 /// <param name="Digest">The type of digest engine used</param>
 public RLWESign(RLWEParameters CipherParams, Digests Digest = Digests.SHA512)
 {
     _asyCipher = new RLWEEncrypt(CipherParams);
     _dgtEngine = GetDigest(Digest);
 }