2014-09-15 21:04:03 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS api tests
|
|
|
|
* LICENSE: GPLv2+ - See COPYING in the top level directory
|
|
|
|
* PURPOSE: Test for RtlEncrypt/DecryptMemory
|
|
|
|
* PROGRAMMER: Timo Kreuzer <timo.kreuzer@reactos.org>
|
|
|
|
*/
|
|
|
|
|
2017-12-12 11:44:32 +00:00
|
|
|
#include "precomp.h"
|
2014-09-15 21:04:03 +00:00
|
|
|
|
2014-09-15 21:08:00 +00:00
|
|
|
#include <ntsecapi.h>
|
2014-09-15 21:04:03 +00:00
|
|
|
|
2020-09-13 17:58:06 +00:00
|
|
|
static const CHAR TestData[32] = "This is some test Message!!!";
|
|
|
|
|
|
|
|
void TestEncrypt(ULONG OptionFlags)
|
2014-09-15 21:04:03 +00:00
|
|
|
{
|
2020-09-13 17:58:06 +00:00
|
|
|
CHAR DECLSPEC_ALIGN(8) Buffer[32];
|
2014-09-15 21:04:03 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
2020-09-13 17:58:06 +00:00
|
|
|
/* Size must be aligned to 8 bytes (aka RTL_ENCRYPT_MEMORY_SIZE) */
|
|
|
|
RtlCopyMemory(Buffer, TestData, sizeof(Buffer));
|
|
|
|
Status = RtlEncryptMemory(Buffer, 7, OptionFlags);
|
2014-09-15 21:04:03 +00:00
|
|
|
ok_ntstatus(Status, STATUS_INVALID_PARAMETER);
|
|
|
|
|
2020-09-13 17:58:06 +00:00
|
|
|
/* Buffer can be unaligned */
|
|
|
|
RtlCopyMemory(Buffer, TestData, sizeof(Buffer));
|
|
|
|
Status = RtlEncryptMemory(&Buffer[1], 8, OptionFlags);
|
|
|
|
ok_ntstatus(Status, STATUS_SUCCESS);
|
|
|
|
Status = RtlDecryptMemory(&Buffer[1], 8, OptionFlags);
|
2014-09-15 21:04:03 +00:00
|
|
|
ok_ntstatus(Status, STATUS_SUCCESS);
|
2020-09-13 17:58:06 +00:00
|
|
|
ok(RtlEqualMemory(Buffer, TestData, sizeof(Buffer)) == 1,
|
|
|
|
"OptionFlags=%lu, Buffer='%s', TestData='%s'\n",
|
|
|
|
OptionFlags, Buffer, TestData);
|
2014-09-15 21:04:03 +00:00
|
|
|
|
|
|
|
RtlCopyMemory(Buffer, TestData, sizeof(Buffer));
|
2020-09-13 17:58:06 +00:00
|
|
|
Status = RtlEncryptMemory(Buffer, sizeof(Buffer), OptionFlags);
|
2014-09-15 21:04:03 +00:00
|
|
|
ok_ntstatus(Status, STATUS_SUCCESS);
|
|
|
|
ok_int(RtlEqualMemory(Buffer, TestData, sizeof(Buffer)), 0);
|
2020-09-13 17:58:06 +00:00
|
|
|
Status = RtlDecryptMemory(Buffer, sizeof(Buffer), OptionFlags);
|
2014-09-15 21:04:03 +00:00
|
|
|
ok_ntstatus(Status, STATUS_SUCCESS);
|
|
|
|
ok_int(RtlEqualMemory(Buffer, TestData, sizeof(Buffer)), 1);
|
2020-09-13 17:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCrossProcessDecrypt(PSTR Param)
|
|
|
|
{
|
|
|
|
UCHAR Buffer[32] = { 0 };
|
|
|
|
ULONG OptionFlags;
|
|
|
|
PSTR StrData;
|
|
|
|
ULONG i;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
OptionFlags = Param[3] == '1' ? RTL_ENCRYPT_OPTION_CROSS_PROCESS : RTL_ENCRYPT_OPTION_SAME_LOGON;
|
|
|
|
|
|
|
|
/* Convert the HEX string to binary ('-cp<1|2>:<data>') */
|
|
|
|
StrData = Param + 5;
|
|
|
|
for (i = 0; i < sizeof(Buffer); i++)
|
|
|
|
{
|
|
|
|
UINT x;
|
|
|
|
sscanf(&StrData[2 * i], "%02X", &x);
|
|
|
|
Buffer[i] = (UCHAR)x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt the data */
|
|
|
|
Status = RtlDecryptMemory(Buffer, sizeof(Buffer), OptionFlags);
|
|
|
|
ok_ntstatus(Status, STATUS_SUCCESS);
|
|
|
|
ok_int(RtlEqualMemory(Buffer, TestData, sizeof(Buffer)), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCrossProcessEncrypt(ULONG OptionFlags)
|
|
|
|
{
|
|
|
|
UCHAR Buffer[32];
|
|
|
|
CHAR CmdLine[MAX_PATH];
|
|
|
|
PSTR StrBuffer;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG i;
|
|
|
|
INT result;
|
|
|
|
|
|
|
|
/* Encrypt the test string */
|
|
|
|
RtlCopyMemory(Buffer, TestData, sizeof(Buffer));
|
|
|
|
Status = RtlEncryptMemory(Buffer, sizeof(Buffer), OptionFlags);
|
|
|
|
ok_ntstatus(Status, STATUS_SUCCESS);
|
|
|
|
|
|
|
|
/* Start building a command line */
|
|
|
|
sprintf(CmdLine, "advapi32_apitest.exe RtlEncryptMemory -cp%lu:", OptionFlags);
|
|
|
|
StrBuffer = CmdLine + strlen(CmdLine);
|
|
|
|
|
|
|
|
/* Convert encrypted data into a hex string */
|
|
|
|
for (i = 0; i < sizeof(Buffer); i++)
|
|
|
|
{
|
|
|
|
sprintf(&StrBuffer[2 * i], "%02X", Buffer[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = system(CmdLine);
|
|
|
|
ok_int(result, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(RtlEncryptMemory)
|
|
|
|
{
|
|
|
|
CHAR Buffer[8] = { 0 };
|
|
|
|
PSTR CommandLine, Param;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Check recursive call */
|
|
|
|
CommandLine = GetCommandLineA();
|
|
|
|
Param = strstr(CommandLine, "-cp");
|
|
|
|
if (Param != NULL)
|
|
|
|
{
|
|
|
|
TestCrossProcessDecrypt(Param);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestEncrypt(RTL_ENCRYPT_OPTION_SAME_PROCESS);
|
|
|
|
TestEncrypt(RTL_ENCRYPT_OPTION_CROSS_PROCESS);
|
|
|
|
TestEncrypt(RTL_ENCRYPT_OPTION_SAME_LOGON);
|
|
|
|
|
|
|
|
Status = RtlEncryptMemory(Buffer, sizeof(Buffer), 4);
|
|
|
|
ok_ntstatus(Status, STATUS_INVALID_PARAMETER);
|
2014-09-15 21:04:03 +00:00
|
|
|
|
2020-09-13 17:58:06 +00:00
|
|
|
TestCrossProcessEncrypt(RTL_ENCRYPT_OPTION_CROSS_PROCESS);
|
|
|
|
TestCrossProcessEncrypt(RTL_ENCRYPT_OPTION_SAME_LOGON);
|
2014-09-15 21:04:03 +00:00
|
|
|
}
|