api: opencode bin2hex in same manner as random directory creation

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-10-31 13:03:14 +01:00 committed by Simon Rozman
parent 62f21f2fdd
commit 26c5478042

View File

@ -9,6 +9,7 @@
#include <Windows.h>
#include <bcrypt.h>
#include <wchar.h>
static BOOL HasInitialized = FALSE;
static CRITICAL_SECTION Initializing;
@ -34,17 +35,6 @@ NormalizeStringAlloc(_In_ NORM_FORM NormForm, _In_z_ const WCHAR *Source)
}
}
static void
Bin2Hex(_In_bytecount_(Size) const void *Source, size_t Size, _Out_capcount_(Size * 2) WCHAR *Destination)
{
for (size_t i = 0; i < Size; ++i)
{
static const WCHAR nibble[] = L"0123456789ABCDEF";
*(Destination++) = nibble[(((unsigned char *)Source)[i] & 0xf0) >> 4];
*(Destination++) = nibble[(((unsigned char *)Source)[i] & 0x0f)];
}
}
static WINTUN_STATUS
NamespaceRuntimeInit(void)
{
@ -134,10 +124,10 @@ NamespaceTakeMutex(_In_z_ const WCHAR *Pool)
if (!BCRYPT_SUCCESS(BCryptFinishHash(Sha256, Hash, sizeof(Hash), 0)))
goto cleanupPoolNorm;
static const WCHAR MutexNamePrefix[] = L"Wintun\\Wintun-Name-Mutex-";
WCHAR MutexName[_countof(MutexNamePrefix) /*<= incl. terminator*/ + sizeof(Hash) * 2];
memcpy(MutexName, MutexNamePrefix, sizeof(MutexNamePrefix) - sizeof(WCHAR));
Bin2Hex(Hash, sizeof(Hash), MutexName + _countof(MutexNamePrefix) - 1);
MutexName[_countof(MutexName) - 1] = 0;
WCHAR MutexName[_countof(MutexNamePrefix) + sizeof(Hash) * 2];
memcpy(MutexName, MutexNamePrefix, sizeof(MutexNamePrefix));
for (size_t i = 0; i < sizeof(Hash); ++i)
swprintf_s(&MutexName[_countof(MutexNamePrefix) - 1 + i * 2], 3, L"%02x", Hash[i]);
Mutex = CreateMutexW(&SecurityAttributes, FALSE, MutexName);
if (!Mutex)
goto cleanupPoolNorm;