BCrypt: Cleanup

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2025-01-28 10:07:37 +01:00
parent acea29ac6d
commit b25f6de9e5

View File

@ -31,7 +31,7 @@ public: \
private: private:
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
static _Must_inspect_result_ NTSTATUS BCryptSignHash(_In_ BCRYPT_KEY_HANDLE hKey, _In_opt_ VOID *pPaddingInfo, _In_reads_bytes_(cbInput) PUCHAR pbInput, _In_ ULONG cbInput, _Out_ std::vector<_Ty, _Ax> &aOutput, _In_ ULONG dwFlags) static _Must_inspect_result_ NTSTATUS BCryptSignHash(_In_ BCRYPT_KEY_HANDLE hKey, _In_opt_ VOID* pPaddingInfo, _In_reads_bytes_(cbInput) PUCHAR pbInput, _In_ ULONG cbInput, _Out_ std::vector<_Ty, _Ax>& aOutput, _In_ ULONG dwFlags)
{ {
ULONG cbSignature = 0; ULONG cbSignature = 0;
NTSTATUS status = BCryptSignHash(hKey, pPaddingInfo, pbInput, cbInput, NULL, 0, &cbSignature, dwFlags); NTSTATUS status = BCryptSignHash(hKey, pPaddingInfo, pbInput, cbInput, NULL, 0, &cbSignature, dwFlags);
@ -45,7 +45,7 @@ static _Must_inspect_result_ NTSTATUS BCryptSignHash(_In_ BCRYPT_KEY_HANDLE hKey
} }
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
static _Must_inspect_result_ NTSTATUS BCryptExportKey(_In_ BCRYPT_KEY_HANDLE hKey, _In_opt_ BCRYPT_KEY_HANDLE hExportKey, _In_z_ LPCWSTR pszBlobType, _Out_ std::vector<_Ty, _Ax> &aOutput, _In_ ULONG dwFlags) static _Must_inspect_result_ NTSTATUS BCryptExportKey(_In_ BCRYPT_KEY_HANDLE hKey, _In_opt_ BCRYPT_KEY_HANDLE hExportKey, _In_z_ LPCWSTR pszBlobType, _Out_ std::vector<_Ty, _Ax>& aOutput, _In_ ULONG dwFlags)
{ {
DWORD cbBlob = 0; DWORD cbBlob = 0;
NTSTATUS status = BCryptExportKey(hKey, hExportKey, pszBlobType, NULL, 0, &cbBlob, dwFlags); NTSTATUS status = BCryptExportKey(hKey, hExportKey, pszBlobType, NULL, 0, &cbBlob, dwFlags);
@ -111,7 +111,7 @@ namespace winstd
/// ///
bcrypt_handle_with_object<T>(_Inout_ bcrypt_handle_with_object<T>&& h) noexcept : bcrypt_handle_with_object<T>(_Inout_ bcrypt_handle_with_object<T>&& h) noexcept :
handle<T, NULL>(std::move(h)), handle<T, NULL>(std::move(h)),
m_hash_object(std::move(h.m_hash_object)) m_object(std::move(h.m_object))
{ {
} }
@ -142,7 +142,7 @@ namespace winstd
{ {
if (this != std::addressof(h)) { if (this != std::addressof(h)) {
handle<T, NULL>::operator=(std::move(h)); handle<T, NULL>::operator=(std::move(h));
m_hash_object = std::move(h.m_hash_object); m_object = std::move(h.m_object);
} }
return *this; return *this;
} }
@ -156,7 +156,7 @@ namespace winstd
virtual void duplicate_internal(_In_ const bcrypt_handle_with_object<T>& h) = 0; virtual void duplicate_internal(_In_ const bcrypt_handle_with_object<T>& h) = 0;
protected: protected:
std::vector<UCHAR> m_hash_object; std::vector<UCHAR> m_object;
}; };
/// ///
@ -178,8 +178,8 @@ namespace winstd
NTSTATUS status = BCryptGetProperty(hAlgorithm, BCRYPT_OBJECT_LENGTH, reinterpret_cast<PUCHAR>(&hashObjectSize), sizeof(hashObjectSize), &bytesRead, 0); NTSTATUS status = BCryptGetProperty(hAlgorithm, BCRYPT_OBJECT_LENGTH, reinterpret_cast<PUCHAR>(&hashObjectSize), sizeof(hashObjectSize), &bytesRead, 0);
if (status || bytesRead != sizeof(hashObjectSize)) if (status || bytesRead != sizeof(hashObjectSize))
throw ntstatus_error(status, "Failed to get hash object size"); throw ntstatus_error(status, "Failed to get hash object size");
m_hash_object.resize(hashObjectSize); m_object.resize(hashObjectSize);
status = BCryptCreateHash(hAlgorithm, &m_h, m_hash_object.data(), static_cast<DWORD>(m_hash_object.size()), pbSecret, cbSecret, dwFlags); status = BCryptCreateHash(hAlgorithm, &m_h, m_object.data(), static_cast<DWORD>(m_object.size()), pbSecret, cbSecret, dwFlags);
if (status) if (status)
throw ntstatus_error(status, "Failed to create hash"); throw ntstatus_error(status, "Failed to create hash");
} }
@ -214,9 +214,9 @@ namespace winstd
void duplicate_internal(_In_ const bcrypt_handle_with_object<BCRYPT_HASH_HANDLE>& h) override void duplicate_internal(_In_ const bcrypt_handle_with_object<BCRYPT_HASH_HANDLE>& h) override
{ {
auto h2 = reinterpret_cast<const bcrypt_hash*>(&h); auto h2 = reinterpret_cast<const bcrypt_hash*>(&h);
m_hash_object.resize(h2->m_hash_object.size()); m_object.resize(h2->m_object.size());
assert(m_hash_object.size() < ULONG_MAX); assert(m_object.size() < ULONG_MAX);
NTSTATUS status = BCryptDuplicateHash(h2->m_h, &m_h, m_hash_object.data(), static_cast<ULONG>(m_hash_object.size()), 0); NTSTATUS status = BCryptDuplicateHash(h2->m_h, &m_h, m_object.data(), static_cast<ULONG>(m_object.size()), 0);
if (status) if (status)
throw ntstatus_error(status, "Failed to duplicate hash"); throw ntstatus_error(status, "Failed to duplicate hash");
} }