How to calculate authentication tag DPAPI BCrypt

Viewed 33

I'm trying to decrypt encrypted buffers with BCrypt but i get an error like: STATUS_AUTH_TAG_MISMATCH The computed authentication tag did not match the value supplied in the pPaddingInfo parameter.

This is the code:

public byte[] Decrypt(byte[] key, byte[] iv, byte[] aad, byte[] cipherText, byte[] authTag)
        {
            var hAlg = OpenAlgorithmProvider(CbCrypt.BCRYPT_AES_ALGORITHM, CbCrypt.MsPrimitiveProvider,
                CbCrypt.BCRYPT_CHAIN_MODE_GCM);
            var keyDataBuffer = ImportKey(hAlg, key, out var hKey);

            byte[] plainText;

            var authInfo = new CbCrypt.AuthenticatedCipherModeInfo(iv, aad, authTag);

            using (authInfo)
            {
                var ivData = new byte[MaxAuthTagSize(hAlg)];

                var plainTextSize = 0;

                var status = CbCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref authInfo, ivData,
                    ivData.Length, null, 0, ref plainTextSize, 0x0);

                if (status != CbCrypt.ErrorSuccess)
                    throw new CryptographicException(
                        $"BCrypt.BCryptDecrypt() (get size) failed with status code: {status}");

                plainText = new byte[plainTextSize];

                status = CbCrypt.BCryptDecrypt(hKey, cipherText, cipherText.Length, ref authInfo, ivData, ivData.Length,
                    plainText, plainText.Length, ref plainTextSize, 0x0);

                if (status == CbCrypt.StatusAuthTagMismatch) // <--- This gets called
                    throw new CryptographicException("BCrypt.BCryptDecrypt(): authentication tag mismatch");

                if (status != CbCrypt.ErrorSuccess)
                    throw new CryptographicException($"BCrypt.BCryptDecrypt() failed with status code:{status}");
            }

            CbCrypt.BCryptDestroyKey(hKey);
            Marshal.FreeHGlobal(keyDataBuffer);
            CbCrypt.BCryptCloseAlgorithmProvider(hAlg, 0x0);

            return plainText;
        }       

How would i calculate the right authentication tag so it will continue without problems?

EDIT this is the function that generates the tag:

public static byte[] DecryptWithKey(byte[] bEncryptedData, byte[] bMasterKey)
{
    byte[] bIv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Array.Copy(bEncryptedData, 3, bIv, 0, 12);

    try
    {
        var bBuffer = new byte[bEncryptedData.Length - 15];
        Array.Copy(bEncryptedData, 15, bBuffer, 0, bEncryptedData.Length - 15);
        var bTag = new byte[16];
        var bData = new byte[bBuffer.Length - bTag.Length];
        Array.Copy(bBuffer, bBuffer.Length - 16, bTag, 0, 16);
        Array.Copy(bBuffer, 0, bData, 0, bBuffer.Length - bTag.Length);
        var aDecryptor = new CAesGcm();
        return aDecryptor.Decrypt(bMasterKey, bIv, null, bData, bTag);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
        return null;
    }
}
0 Answers
Related