Skip to content

Commit

Permalink
Sha3 - 256 and 512
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsNT committed Jan 10, 2023
1 parent 4bf3f53 commit 6a8b954
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 299 deletions.
3 changes: 3 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SHA3_256_ALGORITHM L"SHA3-256"
#define SHA3_512_ALGORITHM L"SHA3-512"
const wchar_t* ProviderB = L"Michael Chourdakis CNG SHA-3 Implementation";
197 changes: 187 additions & 10 deletions dll/dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <bcrypt_provider.h>
#include <ncrypt_provider.h>
#include <vector>
#include <map>
#include <memory>


#pragma comment (lib,"C:\\Windows Kits\\10\\Cryptographic Provider Development Kit\\Lib\\x64\\bcrypt_provider.lib")
Expand All @@ -26,20 +28,65 @@ BOOL WINAPI DllMain(


#pragma comment(lib,"bcrypt.lib")
#pragma comment(lib,"ncrypt.lib")
#pragma comment(lib,"crypt32.lib")



#include <ntstatus.h>
#include "..\\common.h"

#define SHA3_ALGORITHM L"SHA3"
const wchar_t* ProviderB = L"Michael Chourdakis CNG SHA-3 Implementation";
#include "sha3.hpp"

class BOP
{
public:

virtual bool un() = 0;

};

class HASH
{
public:

sha3_ctx ctx;
std::vector<unsigned char> rr;

virtual void init() = 0;
};

class BOP256 : public BOP
{
public:

virtual bool un() { return 1; }

};
class BOP512 : public BOP
{
public:
virtual bool un() { return 1; }
};

class HASH256 : public HASH
{
public:

virtual void init()
{
rhash_sha3_256_init(&ctx);
rr.resize(32);
}
};
class HASH512 : public HASH
{
public:

virtual void init()
{
rhash_sha3_512_init(&ctx);
rr.resize(64);
}
};

NTSTATUS WINAPI GetHashInterface(
Expand All @@ -51,7 +98,7 @@ NTSTATUS WINAPI GetHashInterface(
{
if (!ppFunctionTable)
return -1;
if (pszAlgId != SHA3_ALGORITHM)
if (wcsicmp(pszAlgId,SHA3_256_ALGORITHM) != 0 && wcsicmp(pszAlgId, SHA3_512_ALGORITHM) != 0)
return -1;
BCRYPT_HASH_FUNCTION_TABLE m;
*ppFunctionTable = &m;
Expand All @@ -62,7 +109,13 @@ NTSTATUS WINAPI GetHashInterface(
_In_ ULONG dwFlags
) -> NTSTATUS
{
BOP* b = new BOP;
BOP* b = 0;
if (wcsicmp(pszAlgId,SHA3_256_ALGORITHM) == 0)
b = new BOP256;
if (wcsicmp(pszAlgId, SHA3_512_ALGORITHM) == 0)
b = new BOP512;
if (!b)
return (NTSTATUS)-1;
*phAlgorithm = b;
return 0;
};
Expand All @@ -82,6 +135,60 @@ NTSTATUS WINAPI GetHashInterface(
_In_ ULONG dwFlags
) -> NTSTATUS
{
if (wcsicmp(pszProperty, BCRYPT_OBJECT_LENGTH) == 0)
{
if (!pcbResult)
return (NTSTATUS)-1;
if (pbOutput == 0)
*pcbResult = 4;
else
{
DWORD r = sizeof(HASH);
if (dynamic_cast<BOP256*>((BOP*)hObject))
r = sizeof(BOP256);
if (dynamic_cast<BOP512*>((BOP*)hObject))
r = sizeof(BOP512);
if (dynamic_cast<HASH256*>((HASH*)hObject))
r = sizeof(HASH256);
if (dynamic_cast<HASH512*>((HASH*)hObject))
r = sizeof(HASH512);

memcpy(pbOutput, &r, 4);
*pcbResult = 4;
}
return ERROR_SUCCESS;
}
if (wcsicmp(pszProperty, BCRYPT_HASH_LENGTH) == 0)
{
if (!pcbResult)
return (NTSTATUS)-1;
if (pbOutput == 0)
*pcbResult = 4;
else
{
DWORD r = 32;
if (dynamic_cast<HASH512*>((HASH*)hObject))
r = 64;
memcpy(pbOutput, &r, 4);
*pcbResult = 4;
}
return ERROR_SUCCESS;
}
if (wcsicmp(pszProperty, BCRYPT_HASH_BLOCK_LENGTH) == 0)
{
if (!pcbResult)
return (NTSTATUS)-1;
if (pbOutput == 0)
*pcbResult = 4;
else
{
DWORD r = 32;
memcpy(pbOutput, &r, 4);
*pcbResult = 4;
}

return ERROR_SUCCESS;
}
return STATUS_NOT_SUPPORTED;
};
m.SetProperty = [](_Inout_ BCRYPT_HANDLE hObject,
Expand All @@ -106,11 +213,79 @@ NTSTATUS WINAPI GetHashInterface(
{
if (!hAlgorithm)
return (NTSTATUS) - 1;
if (pbSecret)
return (NTSTATUS)-1;
BOP* b = (BOP*)hAlgorithm;
HASH* ctx = 0;
if (dynamic_cast<BOP256*>(b))
ctx = new HASH256;
if (dynamic_cast<BOP512*>(b))
ctx = new HASH512;
if (!ctx)
return (NTSTATUS)-1;
ctx->init();
*phHash = (BCRYPT_HASH_HANDLE)ctx;
return (NTSTATUS)STATUS_SUCCESS;
};

m.HashData = [](_Inout_ BCRYPT_HASH_HANDLE hHash,
_In_ PUCHAR pbInput,
_In_ ULONG cbInput,
_In_ ULONG dwFlags
)
{
if (!hHash)
return (NTSTATUS)-1;
HASH* ctx = (HASH*)hHash;
rhash_sha3_update(&ctx->ctx, pbInput, cbInput);
return (NTSTATUS)STATUS_SUCCESS;
};

m.FinishHash = [](_Inout_ BCRYPT_HASH_HANDLE hHash,
_Out_writes_bytes_all_(cbOutput) PUCHAR pbOutput,
_In_ ULONG cbOutput,
_In_ ULONG dwFlags)
{
if (!hHash || !pbOutput)
return (NTSTATUS)-1;
HASH* ctx = (HASH*)hHash;
rhash_sha3_final(&ctx->ctx, ctx->rr.data());
memcpy(pbOutput, ctx->rr.data(), cbOutput);
ctx->init();
return (NTSTATUS)STATUS_SUCCESS;
};

m.DuplicateHash = [](_In_ BCRYPT_HASH_HANDLE hHash,
_Out_ BCRYPT_HASH_HANDLE* phNewHash,
_Out_writes_bytes_all_(cbHashObject) PUCHAR pbHashObject,
_In_ ULONG cbHashObject,
_In_ ULONG dwFlags)
{
if (!hHash)
return (NTSTATUS)-1;
HASH* ctx = (HASH*)hHash;
HASH* a1 = 0;
if (dynamic_cast<HASH256*>(ctx))
a1 = new HASH256;
if (dynamic_cast<HASH512*>(ctx))
a1 = new HASH512;
a1->ctx = ctx->ctx;
*phNewHash = (BCRYPT_HASH_HANDLE)a1;
return (NTSTATUS)STATUS_SUCCESS;
};

m.DestroyHash = [](BCRYPT_HASH_HANDLE hHash)
{
if (!hHash)
return (NTSTATUS)-1;
HASH* ctx = (HASH*)hHash;
delete ctx;
return (NTSTATUS)STATUS_SUCCESS;
};

m.CreateMultiHash = 0;
m.ProcessMultiOperations = 0;

m.Version = BCRYPT_HASH_INTERFACE_VERSION_1;
return ERROR_SUCCESS;
}
Expand All @@ -137,9 +312,9 @@ HRESULT __stdcall DllRegisterServer()
reg.pUM->rgpInterfaces = rif;
rif[0] = &r1;
r1.dwInterface = BCRYPT_HASH_INTERFACE;
PWSTR u[] = { (PWSTR)SHA3_ALGORITHM };
PWSTR u[] = { (PWSTR)SHA3_256_ALGORITHM,(PWSTR)SHA3_512_ALGORITHM};
r1.rgpszFunctions = u;
r1.cFunctions = 1;
r1.cFunctions = 2;


if (1)
Expand All @@ -153,7 +328,8 @@ HRESULT __stdcall DllRegisterServer()
return E_FAIL;
}

auto st = BCryptAddContextFunctionProvider(CRYPT_LOCAL, 0, BCRYPT_HASH_INTERFACE, SHA3_ALGORITHM, ProviderB, CRYPT_PRIORITY_TOP);
BCryptAddContextFunctionProvider(CRYPT_LOCAL, 0, BCRYPT_HASH_INTERFACE, SHA3_256_ALGORITHM, ProviderB, CRYPT_PRIORITY_TOP);
BCryptAddContextFunctionProvider(CRYPT_LOCAL, 0, BCRYPT_HASH_INTERFACE, SHA3_512_ALGORITHM, ProviderB, CRYPT_PRIORITY_TOP);
return S_OK;
}

Expand All @@ -163,9 +339,10 @@ HRESULT __stdcall DllUnregisterServer()
GetSystemDirectory(y.data(), 1000);
wcscat_s(y.data(), 1000, L"\\cngsha3.dll");

BCryptRemoveContextFunctionProvider(CRYPT_LOCAL, ProviderB, BCRYPT_HASH_INTERFACE, SHA3_ALGORITHM, ProviderB);
BCryptRemoveContextFunctionProvider(CRYPT_LOCAL, ProviderB, BCRYPT_HASH_INTERFACE, SHA3_512_ALGORITHM, ProviderB);
BCryptRemoveContextFunctionProvider(CRYPT_LOCAL, ProviderB, BCRYPT_HASH_INTERFACE, SHA3_256_ALGORITHM, ProviderB);

auto st = BCryptUnregisterProvider(ProviderB);
BCryptUnregisterProvider(ProviderB);
DeleteFile(y.data());
return S_OK;
}
1 change: 0 additions & 1 deletion dll/exports.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ LIBRARY

EXPORTS
GetHashInterface=GetHashInterface
GetKeyStorageInterface=GetKeyStorageInterface
DllRegisterServer=DllRegisterServer
DllUnregisterServer=DllUnregisterServer
Loading

0 comments on commit 6a8b954

Please sign in to comment.