Skip to content

Commit

Permalink
test/memcpy: Use BCryptGenRandom
Browse files Browse the repository at this point in the history
(cherry picked from commit 79caf85)
Signed-off-by: LIU Hao <lh_mouse@126.com>
  • Loading branch information
lhmouse committed Oct 30, 2024
1 parent 9055a07 commit a0e62c8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions test/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
#include <stdio.h>
#include <string.h>
#include <winbase.h>
#include <ntsecapi.h>

static char comp[100];
static char data[100];

int
main(void)
{
HMODULE bcrypt = LoadLibraryW(L"bcrypt.dll");
assert(bcrypt);

typedef NTSTATUS __stdcall BCryptGenRandom_t(HANDLE, void*, ULONG, ULONG);
BCryptGenRandom_t* pBCryptGenRandom = (BCryptGenRandom_t*)(intptr_t) GetProcAddress(bcrypt, "BCryptGenRandom");
assert(pBCryptGenRandom);

HMODULE msvcrt = LoadLibraryW(L"msvcrt.dll");
assert(msvcrt);

Expand All @@ -34,7 +40,7 @@ main(void)
assert(pmemcmp);

// __MCF_mfill
RtlGenRandom(comp, sizeof(comp));
pBCryptGenRandom(NULL, comp, sizeof(comp), 2);
pmemmove(data, comp, sizeof(comp));

pmemset(comp + 20, 'b', 30);
Expand All @@ -49,7 +55,7 @@ main(void)
assert(memcmp(comp, data, sizeof(comp)) == 0);

// __MCF_mzero
RtlGenRandom(comp, sizeof(comp));
pBCryptGenRandom(NULL, comp, sizeof(comp), 2);
pmemmove(data, comp, sizeof(comp));

pmemset(comp + 20, 0, 30);
Expand All @@ -64,7 +70,7 @@ main(void)
assert(memcmp(comp, data, sizeof(comp)) == 0);

// __MCF_mcopy
RtlGenRandom(comp, sizeof(comp));
pBCryptGenRandom(NULL, comp, sizeof(comp), 2);
pmemmove(data, comp, sizeof(comp));

pmemmove(comp + 10, comp + 20, 40);
Expand All @@ -75,7 +81,7 @@ main(void)
assert(memcmp(comp, data, sizeof(comp)) == 0);

// __MCF_mcopy_backward
RtlGenRandom(comp, sizeof(comp));
pBCryptGenRandom(NULL, comp, sizeof(comp), 2);
pmemmove(data, comp, sizeof(comp));

pmemmove(comp + 20, comp + 10, 40);
Expand All @@ -86,7 +92,7 @@ main(void)
assert(memcmp(comp, data, sizeof(comp)) == 0);

// __MCF_mcomp (equal)
RtlGenRandom(comp, sizeof(comp));
pBCryptGenRandom(NULL, comp, sizeof(comp), 2);
pmemmove(data, comp, sizeof(comp));

assert(pmemcmp(comp, data, sizeof(comp)) == 0);
Expand Down

0 comments on commit a0e62c8

Please sign in to comment.