示例#1
0
        private void Decrypt()
        {
            if (m_pbData.Length == 0)
            {
                return;
            }

            if (m_mp == PbMemProt.ProtectedMemory)
            {
                ProtectedMemory.Unprotect(m_pbData, MemoryProtectionScope.SameProcess);
            }
            else if (m_mp == PbMemProt.Salsa20)
            {
                Salsa20Cipher s = new Salsa20Cipher(g_pbKey32,
                                                    BitConverter.GetBytes(m_lID));
                s.Encrypt(m_pbData, m_pbData.Length, true);
                s.Dispose();
            }
            else if (m_mp == PbMemProt.ExtCrypt)
            {
                m_fExtCrypt(m_pbData, PbCryptFlags.Decrypt, m_lID);
            }
            else
            {
                Debug.Assert(m_mp == PbMemProt.None);
            }

            m_mp = PbMemProt.None;
        }
示例#2
0
        private void Decrypt()
        {
            if (passwordData.Length == 0)
            {
                return;
            }

            if (passwordMemoryProtection == PbMemProt.ProtectedMemory)
            {
                ProtectedMemory.Unprotect(passwordData, MemoryProtectionScope.SameProcess);
            }
            else if (passwordMemoryProtection == PbMemProt.Salsa20)
            {
                var cipher = new Salsa20Cipher(keyBytes32, BitConverter.GetBytes(longId));
                cipher.Encrypt(passwordData, passwordData.Length, true);
                cipher.Dispose();
            }
            else if (passwordMemoryProtection == PbMemProt.ExtCrypt)
            {
                m_fExtCrypt(passwordData, PbCryptFlags.Decrypt, longId);
            }
            else
            {
                Debug.Assert(passwordMemoryProtection == PbMemProt.None);
            }

            passwordMemoryProtection = PbMemProt.None;
        }
示例#3
0
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (m_crsAlgorithm == CrsAlgorithm.ChaCha20)
                {
                    m_chacha20.Dispose();
                }
                else if (m_crsAlgorithm == CrsAlgorithm.Salsa20)
                {
                    m_salsa20.Dispose();
                }
                else if (m_crsAlgorithm == CrsAlgorithm.ArcFourVariant)
                {
                    MemUtil.ZeroByteArray(m_pbState);
                    m_i = 0;
                    m_j = 0;
                }
                else
                {
                    Debug.Assert(false);
                }

                m_bDisposed = true;
            }
        }
示例#4
0
        private void Encrypt()
        {
            Debug.Assert(m_mp == PbMemProt.None);

            // Nothing to do if caller didn't request protection
            if (!m_bProtected)
            {
                return;
            }

            // ProtectedMemory.Protect throws for data size == 0
            if (m_pbData.Length == 0)
            {
                return;
            }

            PbCryptDelegate f = g_fExtCrypt;

            if (f != null)
            {
                f(m_pbData, PbCryptFlags.Encrypt, m_lID);

                m_fExtCrypt = f;
                m_mp        = PbMemProt.ExtCrypt;
                return;
            }

            if (ProtectedBinary.ProtectedMemorySupported)
            {
                ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess);

                m_mp = PbMemProt.ProtectedMemory;
                return;
            }

            byte[] pbKey32 = g_pbKey32;
            if (pbKey32 == null)
            {
                pbKey32 = CryptoRandom.Instance.GetRandomBytes(32);

                byte[] pbUpd = Interlocked.Exchange <byte[]>(ref g_pbKey32, pbKey32);
                if (pbUpd != null)
                {
                    pbKey32 = pbUpd;
                }
            }

            Salsa20Cipher s = new Salsa20Cipher(pbKey32,
                                                BitConverter.GetBytes(m_lID));

            s.Encrypt(m_pbData, m_pbData.Length, true);
            s.Dispose();
            m_mp = PbMemProt.Salsa20;
        }
示例#5
0
        private void Encrypt()
        {
            Debug.Assert(passwordMemoryProtection == PbMemProt.None);

            if (!isProtected)
            {
                return;
            }

            if (passwordData.Length == 0)
            {
                return;
            }

            var f = encryptionDelegate;

            if (f != null)
            {
                f(passwordData, PbCryptFlags.Encrypt, longId);

                m_fExtCrypt = f;
                passwordMemoryProtection = PbMemProt.ExtCrypt;
                return;
            }

            if (ProtectedMemorySupported)
            {
                ProtectedMemory.Protect(passwordData, MemoryProtectionScope.SameProcess);

                passwordMemoryProtection = PbMemProt.ProtectedMemory;
                return;
            }

            var pbKey32 = keyBytes32;

            if (pbKey32 == null)
            {
                pbKey32 = CryptoRandom.Instance.GetRandomBytes(32);

                var pbUpd = Interlocked.Exchange(ref keyBytes32, pbKey32);
                if (pbUpd != null)
                {
                    pbKey32 = pbUpd;
                }
            }

            var cipher = new Salsa20Cipher(pbKey32, BitConverter.GetBytes(longId));

            cipher.Encrypt(passwordData, passwordData.Length, true);
            cipher.Dispose();
            passwordMemoryProtection = PbMemProt.Salsa20;
        }