11 internal static class BCrypt
101 internal unsafe fixed
byte Count[4];
103 internal unsafe fixed
byte Seed[20];
105 internal unsafe fixed
byte q[20];
122 internal unsafe fixed
byte Count[4];
265 result.dwInfoVersion = 1u;
313 this =
default(
Entry);
317 Unsafe.SkipInit(out
int hashSizeInBytes);
334 for (
int i = 0; i < array2.Length; i++)
336 Entry entry = array2[i];
350 Entry entry2 =
new Entry(hashAlgorithmId, flags, phAlgorithm);
351 array[^1] =
new Entry(hashAlgorithmId, flags, phAlgorithm);
365 byte[]
array =
new byte[num];
366 fixed (
byte* ptr =
array)
388 fixed (
byte* pbInput =
input)
390 fixed (
byte* pbOutput = output)
405 fixed (
byte* pbInput =
input)
407 fixed (
byte* pbOutput = output)
421 public unsafe
static extern NTSTATUS BCryptEncrypt(
SafeKeyHandle hKey,
byte* pbInput,
int cbInput,
IntPtr paddingInfo, [In][Out]
byte[] pbIV,
int cbIV,
byte* pbOutput,
int cbOutput, out
int cbResult,
int dwFlags);
424 public unsafe
static extern NTSTATUS BCryptDecrypt(
SafeKeyHandle hKey,
byte* pbInput,
int cbInput,
IntPtr paddingInfo, [In][Out]
byte[] pbIV,
int cbIV,
byte* pbOutput,
int cbOutput, out
int cbResult,
int dwFlags);
438 for (
int i =
offset; i < num; i++)
463 int hr = (int)(ntStatus | (
NTSTATUS)16777216u);
464 return hr.ToCryptographicException();
500 internal unsafe
static extern NTSTATUS BCryptHash(nuint hAlgorithm,
byte* pbSecret,
int cbSecret,
byte* pbInput,
int cbInput,
byte* pbOutput,
int cbOutput);
626 nCryptBuffer.cbBuffer = (hashAlgorithm.Length + 1) * 2;
627 nCryptBuffer.BufferType =
BufferType.KdfHashAlgorithm;
628 nCryptBuffer.pvBuffer = intPtr;
629 span[num] = nCryptBuffer;
631 fixed (
byte* ptr = hmacKey)
633 fixed (
byte* ptr2 = secretPrepend)
635 fixed (
byte* ptr3 = secretAppend)
640 nCryptBuffer2.cbBuffer = hmacKey.Length;
641 nCryptBuffer2.BufferType =
BufferType.KdfHmacKey;
642 nCryptBuffer2.pvBuffer =
new IntPtr(ptr);
643 span[num] = nCryptBuffer2;
649 nCryptBuffer3.cbBuffer = secretPrepend.Length;
650 nCryptBuffer3.BufferType =
BufferType.KdfSecretPrepend;
651 nCryptBuffer3.pvBuffer =
new IntPtr(ptr2);
652 span[num] = nCryptBuffer3;
658 nCryptBuffer4.cbBuffer = secretAppend.Length;
659 nCryptBuffer4.BufferType =
BufferType.KdfSecretAppend;
660 nCryptBuffer4.pvBuffer =
new IntPtr(ptr3);
661 span[num] = nCryptBuffer4;
683 pParameterList.ulVersion = 0;
684 pParameterList.cBuffers = parameters.
Length;
687 if (errorCode != 0 && errorCode !=
ErrorCode.NTE_BUFFER_TOO_SMALL)
689 throw errorCode.ToCryptographicException();
691 byte[]
array =
new byte[pcbResult];
695 throw errorCode.ToCryptographicException();
704 return DeriveKeyMaterial(secretAgreement,
"HASH", hashAlgorithm,
null, secretPrepend, secretAppend, flags);
709 return DeriveKeyMaterial(secretAgreement,
"HMAC", hashAlgorithm, hmacKey, secretPrepend, secretAppend, flags);
715 fixed (
byte*
value = label)
717 fixed (
byte* value2 = seed)
720 nCryptBuffer.cbBuffer = label.Length;
721 nCryptBuffer.BufferType =
BufferType.KdfTlsLabel;
723 span[0] = nCryptBuffer;
725 nCryptBuffer2.cbBuffer = seed.Length;
726 nCryptBuffer2.BufferType =
BufferType.KdfTlsSeed;
727 nCryptBuffer2.pvBuffer =
new IntPtr(value2);
728 span[1] = nCryptBuffer2;
734 [DllImport(
"ncrypt.dll")]
743 throw errorCode.ToCryptographicException();
799 fixed (
int* pbOutput = &result)
859 intPtr = keyType
switch
879 if (fallBackToAllGroups && group != 0)
904 return group ==
OidGroup.KeyDerivationFunction;
917 int pvKey = (int)algId;
929 internal static class Kernel32
931 [DllImport(
"kernel32.dll", BestFitMapping =
true,
CharSet =
CharSet.Unicode, EntryPoint =
"FormatMessageW", ExactSpelling =
true,
SetLastError =
true)]
932 private unsafe
static extern int FormatMessage(
int dwFlags,
IntPtr lpSource, uint dwMessageId,
int dwLanguageId,
void* lpBuffer,
int nSize,
IntPtr arguments);
947 fixed (
char* lpBuffer = span)
971 return $
"Unknown error (0x{errorCode:x})";
977 while (num > 0 &&
buffer[num - 1] <=
' ')
981 return buffer.Slice(0, num).ToString();
static SafeBCryptAlgorithmHandle GetCachedBCryptAlgorithmHandle(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, out int hashSizeInBytes)
static volatile Entry[] _cache
static NTSTATUS BCryptDestroyHash(IntPtr hHash)
static unsafe NTSTATUS BCryptGenerateSymmetricKey(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptKeyHandle phKey, IntPtr pbKeyObject, int cbKeyObject, byte *pbSecret, int cbSecret, uint dwFlags)
static unsafe int BCryptEncrypt(SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
static Exception CreateCryptographicException(NTSTATUS ntStatus)
@ DSA_HASH_ALGORITHM_SHA1
@ DSA_HASH_ALGORITHM_SHA256
@ DSA_HASH_ALGORITHM_SHA512
static NTSTATUS BCryptDuplicateHash(SafeBCryptHashHandle hHash, out SafeBCryptHashHandle phNewHash, IntPtr pbHashObject, int cbHashObject, int dwFlags)
@ STATUS_AUTH_TAG_MISMATCH
@ STATUS_INVALID_PARAMETER
static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags)
static void EmitBigEndian(byte[] blob, ref int offset, int value)
@ BCRYPT_HASH_REUSABLE_FLAG
static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags)
@ NCRYPTBUFFER_ECC_CURVE_NAME
static bool PseudoHandlesSupported
@ BCRYPT_ECC_PRIME_MONTGOMERY_CURVE
@ BCRYPT_ECC_PRIME_SHORT_WEIERSTRASS_CURVE
@ BCRYPT_ECC_PRIME_TWISTED_EDWARDS_CURVE
static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags)
BCryptOpenAlgorithmProviderFlags
@ BCRYPT_ALG_HANDLE_HMAC_FLAG
static NTSTATUS BCryptCloseAlgorithmProvider(IntPtr hAlgorithm, int dwFlags)
static void EmitByte(byte[] blob, ref int offset, byte value, int count=1)
static unsafe SafeKeyHandle BCryptImportKey(SafeAlgorithmHandle hAlg, ReadOnlySpan< byte > key)
static NTSTATUS BCryptDestroyKey(IntPtr hKey)
static NTSTATUS BCryptImportKey(SafeAlgorithmHandle hAlgorithm, IntPtr hImportKey, string pszBlobType, out SafeKeyHandle hKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbInput, int cbInput, int dwFlags)
static unsafe NTSTATUS BCryptEncrypt(SafeKeyHandle hKey, byte *pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte *pbOutput, int cbOutput, out int cbResult, int dwFlags)
static unsafe NTSTATUS BCryptHash(nuint hAlgorithm, byte *pbSecret, int cbSecret, byte *pbInput, int cbInput, byte *pbOutput, int cbOutput)
static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan< byte > secret, int cbSecret, BCryptCreateHashFlags dwFlags)
static byte[] Consume(byte[] blob, ref int offset, int count)
@ BCRYPT_PBKDF2_ALG_HANDLE
@ BCRYPT_HMAC_SHA1_ALG_HANDLE
@ BCRYPT_HMAC_SHA384_ALG_HANDLE
@ BCRYPT_HMAC_MD5_ALG_HANDLE
@ BCRYPT_SHA384_ALG_HANDLE
@ BCRYPT_SHA256_ALG_HANDLE
@ BCRYPT_HMAC_SHA512_ALG_HANDLE
@ BCRYPT_SHA512_ALG_HANDLE
@ BCRYPT_HMAC_SHA256_ALG_HANDLE
static unsafe NTSTATUS BCryptKeyDerivation(SafeBCryptKeyHandle hKey, BCryptBufferDesc *pParameterList, byte *pbDerivedKey, int cbDerivedKey, out uint pcbResult, int dwFlags)
static unsafe NTSTATUS BCryptGenerateSymmetricKey(nuint hAlgorithm, out SafeBCryptKeyHandle phKey, IntPtr pbKeyObject, int cbKeyObject, byte *pbSecret, int cbSecret, uint dwFlags)
static SafeBCryptHashHandle BCryptDuplicateHash(SafeBCryptHashHandle hHash)
static NTSTATUS BCryptOpenAlgorithmProvider(out SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags)
static unsafe int BCryptDecrypt(SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
@ BCRYPT_ECDH_PUBLIC_P521_MAGIC
@ BCRYPT_DSA_PRIVATE_MAGIC
@ BCRYPT_ECDH_PRIVATE_P256_MAGIC
@ BCRYPT_RSAFULLPRIVATE_MAGIC
@ BCRYPT_ECDSA_PUBLIC_P384_MAGIC
@ BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC
@ BCRYPT_ECDH_PUBLIC_P256_MAGIC
@ BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC
@ BCRYPT_ECDH_PRIVATE_P384_MAGIC
@ BCRYPT_ECDH_PRIVATE_P521_MAGIC
@ BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC
@ BCRYPT_ECDSA_PUBLIC_P256_MAGIC
@ BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC
@ BCRYPT_ECDH_PUBLIC_P384_MAGIC
@ BCRYPT_DSA_PUBLIC_MAGIC_V2
@ BCRYPT_DSA_PRIVATE_MAGIC_V2
@ BCRYPT_KEY_DATA_BLOB_MAGIC
@ BCRYPT_DSA_PUBLIC_MAGIC
@ BCRYPT_ECDSA_PUBLIC_P521_MAGIC
@ BCRYPT_ECDSA_PRIVATE_P256_MAGIC
@ BCRYPT_ECDSA_PRIVATE_P521_MAGIC
@ BCRYPT_RSAPRIVATE_MAGIC
@ BCRYPT_ECDSA_PRIVATE_P384_MAGIC
static unsafe NTSTATUS BCryptDecrypt(SafeKeyHandle hKey, byte *pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte *pbOutput, int cbOutput, out int cbResult, int dwFlags)
static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span< byte > pbOutput, int cbOutput, int dwFlags)
static unsafe NTSTATUS BCryptDeriveKeyPBKDF2(SafeBCryptAlgorithmHandle hPrf, byte *pbPassword, int cbPassword, byte *pbSalt, int cbSalt, ulong cIterations, byte *pbDerivedKey, int cbDerivedKey, uint dwFlags)
static unsafe NTSTATUS BCryptGetProperty(SafeBCryptHandle hObject, string pszProperty, void *pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan< byte > pbInput, int cbInput, int dwFlags)
static unsafe NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, byte *pbBuffer, int cbBuffer, int dwFlags)
@ BCRYPT_NO_CURVE_GENERATION_ALG_ID
static void Emit(byte[] blob, ref int offset, byte[] value)
static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
@ CRYPT_OID_INFO_NAME_KEY
@ CRYPT_OID_INFO_CNG_SIGN_KEY
@ CRYPT_OID_INFO_SIGN_KEY
@ CRYPT_OID_INFO_CNG_ALGID_KEY
@ CRYPT_OID_INFO_ALGID_KEY
static IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, ref int pvKey, OidGroup group)
static IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group)
static bool OidGroupWillNotUseActiveDirectory(OidGroup group)
static CRYPT_OID_INFO FindAlgIdOidInfo(BCrypt.ECC_CURVE_ALG_ID_ENUM algId)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
static void SetLastError(int errorCode)
static string GetMessage(int errorCode)
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static string GetAndTrimString(Span< char > buffer)
static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan< byte > pbInput, int cbInput, void *pPaddingInfo, Span< byte > pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
static ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static unsafe ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string pszProperty, ref int result)
static byte[] DeriveKeyMaterialHmac(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags)
static unsafe byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
static unsafe byte[] DeriveKeyMaterialTls(SafeNCryptSecretHandle secretAgreement, byte[] label, byte[] seed, SecretAgreementFlags flags)
static unsafe ErrorCode NCryptSetProperty(SafeNCryptHandle hObject, string pszProperty, [In] void *pbInput, int cbInput, CngPropertyOptions dwFlags)
static unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void *pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
static ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, IntPtr pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags)
static unsafe ErrorCode NCryptGetProperty(SafeNCryptHandle hObject, string pszProperty, [Out] void *pbOutput, int cbOutput, out int pcbResult, CngPropertyOptions dwFlags)
static ErrorCode NCryptSecretAgreement(SafeNCryptKeyHandle hPrivKey, SafeNCryptKeyHandle hPubKey, out SafeNCryptSecretHandle phSecret, int dwFlags)
static ErrorCode NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, string pszProviderName, int dwFlags)
static byte[] DeriveKeyMaterialHash(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
static ErrorCode NCryptCreatePersistedKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszAlgId, string pszKeyName, int dwLegacyKeySpec, CngKeyCreationOptions dwFlags)
static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void *pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
static unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan< byte > pbInput, int cbInput, void *pPaddingInfo, Span< byte > pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
static SafeNCryptSecretHandle DeriveSecretAgreement(SafeNCryptKeyHandle privateKey, SafeNCryptKeyHandle otherPartyPublicKey)
static ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags)
static ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
static ErrorCode NCryptFinalizeKey(SafeNCryptKeyHandle hKey, int dwFlags)
static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan< byte > pbHashValue, int cbHashValue, ReadOnlySpan< byte > pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
static ErrorCode NCryptFreeObject(IntPtr hObject)
static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan< byte > pbHashValue, int cbHashValue, Span< byte > pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags)
static unsafe byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, ReadOnlySpan< NCryptBuffer > parameters, SecretAgreementFlags flags)
static ErrorCode NCryptDeriveKey(SafeNCryptSecretHandle hSharedSecret, string pwszKDF, [In] ref NCryptBufferDesc pParameterList, [Out][MarshalAs(UnmanagedType.LPArray)] byte[] pbDerivedKey, int cbDerivedKey, out int pcbResult, SecretAgreementFlags dwFlags)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
static byte Min(byte val1, byte val2)
static bool IsWindowsVersionAtLeast(int major, int minor=0, int build=0, int revision=0)
static unsafe IntPtr StringToCoTaskMemUni(string? s)
static void FreeHGlobal(IntPtr hglobal)
static void FreeCoTaskMem(IntPtr ptr)
static unsafe? string PtrToStringUni(IntPtr ptr)
static int GetLastWin32Error()
static ? object PtrToStructure(IntPtr ptr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type structureType)
static unsafe IntPtr StringToCoTaskMemAnsi(string? s)
unsafe byte * pbMacContext
static unsafe BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO Create()
unsafe fixed byte Count[4]
DSAFIPSVERSION_ENUM standardVersion
HASHALGORITHM_ENUM hashAlgorithm
unsafe fixed byte Count[4]
unsafe fixed byte Seed[20]
ECC_CURVE_TYPE_ENUM CurveType
ECC_CURVE_ALG_ID_ENUM CurveGenerationAlgId
SafeBCryptAlgorithmHandle Handle
unsafe Entry(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, SafeBCryptAlgorithmHandle handle)
BCryptOpenAlgorithmProviderFlags Flags
CngBufferDescriptors BufferType
unsafe fixed byte rgbSalt[8]
CRYPT_PKCS12_PBE_PARAMS Params
static readonly IntPtr Zero
Span< T > Slice(int start)