Exemplo n.º 1
0
        // Token: 0x0600000E RID: 14 RVA: 0x000022E8 File Offset: 0x000004E8
        private IntPtr OpenAlgorithmProvider(string alg, string provider, string chainingMode)
        {
            IntPtr zero = IntPtr.Zero;
            uint   num  = BCrypt.BCryptOpenAlgorithmProvider(out zero, alg, provider, 0U);
            bool   flag = num > 0U;

            if (flag)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptOpenAlgorithmProvider() failed with status code:{0}", num));
            }
            byte[] bytes = Encoding.Unicode.GetBytes(chainingMode);
            num = BCrypt.BCryptSetAlgorithmProperty(zero, BCrypt.BCRYPT_CHAINING_MODE, bytes, bytes.Length, 0);
            bool flag2 = num > 0U;

            if (flag2)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptSetAlgorithmProperty(BCrypt.BCRYPT_CHAINING_MODE, BCrypt.BCRYPT_CHAIN_MODE_GCM) failed with status code:{0}", num));
            }
            return(zero);
        }
Exemplo n.º 2
0
        // Token: 0x06000010 RID: 16 RVA: 0x00002408 File Offset: 0x00000608
        private byte[] GetProperty(IntPtr hAlg, string name)
        {
            int  num  = 0;
            uint num2 = BCrypt.BCryptGetProperty(hAlg, name, null, 0, ref num, 0U);
            bool flag = num2 > 0U;

            if (flag)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptGetProperty() (get size) failed with status code:{0}", num2));
            }
            byte[] array = new byte[num];
            num2 = BCrypt.BCryptGetProperty(hAlg, name, array, array.Length, ref num, 0U);
            bool flag2 = num2 > 0U;

            if (flag2)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptGetProperty() failed with status code:{0}", num2));
            }
            return(array);
        }
Exemplo n.º 3
0
        private IntPtr OpenAlgorithmProvider(string alg, string provider, string chainingMode)
        {
            IntPtr hAlg = IntPtr.Zero;

            uint status = BCrypt.BCryptOpenAlgorithmProvider(out hAlg, alg, provider, 0x0);

            if (status != BCrypt.ERROR_SUCCESS)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptOpenAlgorithmProvider() failed with status code:{0}", status));
            }

            byte[] chainMode = Encoding.Unicode.GetBytes(chainingMode);
            status = BCrypt.BCryptSetAlgorithmProperty(hAlg, BCrypt.BCRYPT_CHAINING_MODE, chainMode, chainMode.Length, 0x0);

            if (status != BCrypt.ERROR_SUCCESS)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptSetAlgorithmProperty(BCrypt.BCRYPT_CHAINING_MODE, BCrypt.BCRYPT_CHAIN_MODE_GCM) failed with status code:{0}", status));
            }

            return(hAlg);
        }
Exemplo n.º 4
0
        // Token: 0x0600000F RID: 15 RVA: 0x0000236C File Offset: 0x0000056C
        private IntPtr ImportKey(IntPtr hAlg, byte[] key, out IntPtr hKey)
        {
            byte[] property = this.GetProperty(hAlg, BCrypt.BCRYPT_OBJECT_LENGTH);
            int    num      = BitConverter.ToInt32(property, 0);
            IntPtr intPtr   = Marshal.AllocHGlobal(num);

            byte[] array = this.Concat(new byte[][]
            {
                BCrypt.BCRYPT_KEY_DATA_BLOB_MAGIC,
                BitConverter.GetBytes(1),
                BitConverter.GetBytes(key.Length),
                key
            });
            uint num2 = BCrypt.BCryptImportKey(hAlg, IntPtr.Zero, BCrypt.BCRYPT_KEY_DATA_BLOB, out hKey, intPtr, num, array, array.Length, 0U);
            bool flag = num2 > 0U;

            if (flag)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptImportKey() failed with status code:{0}", num2));
            }
            return(intPtr);
        }
Exemplo n.º 5
0
        private byte[] GetProperty(IntPtr hAlg, string name)
        {
            int size = 0;

            uint status = BCrypt.BCryptGetProperty(hAlg, name, null, 0, ref size, 0x0);

            if (status != BCrypt.ERROR_SUCCESS)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptGetProperty() (get size) failed with status code:{0}", status));
            }

            byte[] value = new byte[size];

            status = BCrypt.BCryptGetProperty(hAlg, name, value, value.Length, ref size, 0x0);

            if (status != BCrypt.ERROR_SUCCESS)
            {
                throw new CryptographicException(string.Format("BCrypt.BCryptGetProperty() failed with status code:{0}", status));
            }

            return(value);
        }
Exemplo n.º 6
0
        // Token: 0x0600000C RID: 12 RVA: 0x00002174 File Offset: 0x00000374
        public byte[] Decrypt(byte[] key, byte[] iv, byte[] aad, byte[] cipherText, byte[] authTag)
        {
            IntPtr intPtr = this.OpenAlgorithmProvider(BCrypt.BCRYPT_AES_ALGORITHM, BCrypt.MS_PRIMITIVE_PROVIDER, BCrypt.BCRYPT_CHAIN_MODE_GCM);
            IntPtr hKey;
            IntPtr hglobal = this.ImportKey(intPtr, key, out hKey);

            BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO bcrypt_AUTHENTICATED_CIPHER_MODE_INFO = new BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO(iv, aad, authTag);
            byte[] array2;
            using (bcrypt_AUTHENTICATED_CIPHER_MODE_INFO)
            {
                byte[] array = new byte[this.MaxAuthTagSize(intPtr)];
                int    num   = 0;
                uint   num2  = BCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref bcrypt_AUTHENTICATED_CIPHER_MODE_INFO, array, array.Length, null, 0, ref num, 0);
                bool   flag  = num2 > 0U;
                if (flag)
                {
                    throw new CryptographicException(string.Format("BCrypt.BCryptDecrypt() (get size) failed with status code: {0}", num2));
                }
                array2 = new byte[num];
                num2   = BCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref bcrypt_AUTHENTICATED_CIPHER_MODE_INFO, array, array.Length, array2, array2.Length, ref num, 0);
                bool flag2 = num2 == BCrypt.STATUS_AUTH_TAG_MISMATCH;
                if (flag2)
                {
                    throw new CryptographicException("BCrypt.BCryptDecrypt(): authentication tag mismatch");
                }
                bool flag3 = num2 > 0U;
                if (flag3)
                {
                    throw new CryptographicException(string.Format("BCrypt.BCryptDecrypt() failed with status code:{0}", num2));
                }
            }
            BCrypt.BCryptDestroyKey(hKey);
            Marshal.FreeHGlobal(hglobal);
            BCrypt.BCryptCloseAlgorithmProvider(intPtr, 0U);
            return(array2);
        }