示例#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.ChaCha20)
            {
                byte[] pbIV = new byte[12];
                MemUtil.UInt64ToBytesEx((ulong)m_lID, pbIV, 4);
                using (ChaCha20Cipher c = new ChaCha20Cipher(g_pbKey32, pbIV, true))
                {
                    c.Decrypt(m_pbData, 0, m_pbData.Length);
                }
            }
            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 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;
        }
示例#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)
            {
#if NETSTANDARD2_0
                m_pbData = CryptoUtil.ProtectData(m_pbData, null, DataProtectionScope.CurrentUser);
#else
                ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess);
#endif

                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;
                }
            }

            byte[] pbIV = new byte[12];
            MemUtil.UInt64ToBytesEx((ulong)m_lID, pbIV, 4);
            using (ChaCha20Cipher c = new ChaCha20Cipher(pbKey32, pbIV, true))
            {
                c.Encrypt(m_pbData, 0, m_pbData.Length);
            }
            m_mp = PbMemProt.ChaCha20;
        }
示例#5
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 !KPCLib
            // ProtectedMemory is not supported on Android, iOS and UWP
            if (ProtectedBinary.ProtectedMemorySupported)
            {
                ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess);

                m_mp = PbMemProt.ProtectedMemory;
                return;
            }
#endif // KPCLib
            byte[] pbKey32 = g_pbKey32;
            if (pbKey32 == null)
            {
                pbKey32 = GetRandom32();

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

            byte[] pbIV = new byte[12];
            MemUtil.UInt64ToBytesEx((ulong)m_lID, pbIV, 4);
            using (ChaCha20Cipher c = new ChaCha20Cipher(pbKey32, pbIV, true))
            {
                c.Encrypt(m_pbData, 0, m_pbData.Length);
            }
            m_mp = PbMemProt.ChaCha20;
        }
示例#6
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;
        }
示例#7
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;
        }
示例#8
0
        private void Decrypt()
        {
            if (m_pbData.Length == 0)
            {
                return;
            }

            if (m_mp == PbMemProt.ProtectedMemory)
            {
#if NETSTANDARD2_0
                var unprotectedData = CryptoUtil.UnprotectData(m_pbData, null, DataProtectionScope.CurrentUser);
                Array.Clear(m_pbData, 0, m_pbData.Length);
                m_pbData = unprotectedData;
#else
                ProtectedMemory.Unprotect(m_pbData, MemoryProtectionScope.SameProcess);
#endif
            }
            else if (m_mp == PbMemProt.ChaCha20)
            {
                byte[] pbIV = new byte[12];
                MemUtil.UInt64ToBytesEx((ulong)m_lID, pbIV, 4);
                using (ChaCha20Cipher c = new ChaCha20Cipher(g_pbKey32, pbIV, true))
                {
                    c.Decrypt(m_pbData, 0, m_pbData.Length);
                }
            }
            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;
        }