aboutsummaryrefslogtreecommitdiff
path: root/win_runtime.Tests/crypto_tests.cc
diff options
context:
space:
mode:
authorjmpoep <OriginalEntryPoint@qq.com>2023-12-07 16:51:07 +0800
committerjmpoep <OriginalEntryPoint@qq.com>2023-12-07 16:51:07 +0800
commit28008a746a31abb7909dd86cb0cd413ac8943b0b (patch)
treea30b74b8cad548048c3c1551d652828ab76fa9bd /win_runtime.Tests/crypto_tests.cc
downloadvmprotect-3.5.1-28008a746a31abb7909dd86cb0cd413ac8943b0b.tar
vmprotect-3.5.1-28008a746a31abb7909dd86cb0cd413ac8943b0b.tar.gz
vmprotect-3.5.1-28008a746a31abb7909dd86cb0cd413ac8943b0b.tar.bz2
vmprotect-3.5.1-28008a746a31abb7909dd86cb0cd413ac8943b0b.zip
first commitHEADmaster
Diffstat (limited to 'win_runtime.Tests/crypto_tests.cc')
-rw-r--r--win_runtime.Tests/crypto_tests.cc170
1 files changed, 170 insertions, 0 deletions
diff --git a/win_runtime.Tests/crypto_tests.cc b/win_runtime.Tests/crypto_tests.cc
new file mode 100644
index 0000000..5ddc237
--- /dev/null
+++ b/win_runtime.Tests/crypto_tests.cc
@@ -0,0 +1,170 @@
+#include "../runtime/common.h"
+#include "../runtime/crypto.h"
+
+TEST(CryptoContainerTests, GetRange)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(1, vKey);
+ for (size_t i = 1; i < 100; i++)
+ {
+ EXPECT_EQ(255, cont.GetByte(i));
+ EXPECT_EQ(255, cont.GetByte(-1*i));
+ }
+}
+
+TEST(CryptoContainerTests, GetSetByte)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(1, vKey);
+ ASSERT_NO_THROW(cont.SetByte(0, 123));
+ EXPECT_EQ(123, cont.GetByte(0));
+}
+
+TEST(CryptoContainerTests, GetSetNBytes)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ const int N = 256;
+ CryptoContainer cont(N, vKey);
+ for (int i = 0; i < N; i++)
+ ASSERT_NO_THROW(cont.SetByte(i, i));
+ for (int i = 0; i < N; i++)
+ EXPECT_EQ(i, cont.GetByte(i));
+}
+
+TEST(CryptoContainerTests, CryptedBuffer)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+ // crypt buffer
+ uint32_t vData[256];
+ {
+ CryptoContainer cont((uint8_t *)vData, sizeof(vData), vKey);
+ for (size_t i = 0; i < _countof(vData); i++) cont.SetDWord(i * sizeof(uint32_t), static_cast<uint32_t>(123456 * i));
+ }
+
+ // read data
+ {
+ CryptoContainer cont((uint8_t *)vData, sizeof(vData), vKey);
+ for (size_t i = 0; i < _countof(vData); i++)
+ {
+ EXPECT_EQ(static_cast<uint32_t>(123456 * i), cont.GetDWord(i * sizeof(uint32_t)));
+
+ // check there is no uint32_t(0) and uint32(123456) inside the buffer
+ EXPECT_NE((uint32_t)0, vData[i]);
+ EXPECT_NE((uint32_t)123456, vData[i]);
+ }
+ }
+
+
+}
+
+TEST(CryptoContainerTests, GetSetWordInSingleBlock)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(8, vKey);
+ ASSERT_NO_THROW(cont.SetWord(3, 12345));
+ EXPECT_EQ(12345, cont.GetWord(3));
+}
+
+TEST(CryptoContainerTests, GetSetWordInTwoBlocks)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(16, vKey);
+ ASSERT_NO_THROW(cont.SetWord(7, 0x1234));
+ EXPECT_EQ(0x1234, cont.GetWord(7));
+}
+
+TEST(CryptoContainerTests, GetSetWordOutOfRange)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(8, vKey);
+ ASSERT_NO_THROW(cont.SetByte(7, 1));
+ EXPECT_EQ(65535, cont.GetWord(7));
+
+ ASSERT_NO_THROW(cont.SetByte(7, 0));
+ ASSERT_NO_THROW(cont.SetWord(7, 12345));
+ EXPECT_EQ(0, cont.GetByte(7));
+}
+
+TEST(CryptoContainerTests, GetSetDWordInSingleBlock)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(8, vKey);
+ ASSERT_NO_THROW(cont.SetDWord(3, 0x12345678));
+ EXPECT_EQ((uint32_t)0x12345678, cont.GetDWord(3));
+}
+
+TEST(CryptoContainerTests, GetSetDWordInTwoBlocks)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(16, vKey);
+ uint32_t vValues[4] = {0x12345678, 0xA0B0C0D0, 0x01020304, 0x1A2B3C4D};
+ for (size_t i = 0; i < 4; i++)
+ {
+ ASSERT_NO_THROW(cont.SetDWord(5 + i, vValues[i]));
+ EXPECT_EQ(vValues[i], cont.GetDWord(5 + i));
+ }
+}
+
+TEST(CryptoContainerTests, GetSetDWordOutOfRange)
+{
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(8, vKey);
+ for (size_t i = 5; i < 8; i++) ASSERT_NO_THROW(cont.SetByte(i, 1));
+ for (size_t i = 5; i < 8; i++) EXPECT_EQ(0xFFFFFFFF, cont.GetDWord(i));
+
+ for (size_t i = 5; i < 8; i++) ASSERT_NO_THROW(cont.SetByte(i, 0));
+ for (size_t i = 5; i < 8; i++) ASSERT_NO_THROW(cont.SetDWord(i, 0x12345678));
+ for (size_t i = 5; i < 8; i++) EXPECT_EQ(0, cont.GetByte(i));
+}
+
+TEST(CryptoContainerTests, SHA1)
+{
+ std::vector<uint8_t> vData;
+ srand((unsigned int)__rdtsc());
+ for (size_t i = 0; i < 1111; i++) vData.push_back(rand() % 255);
+
+ SHA1 hash1, hash2;
+
+ hash1.Input(&vData[0], vData.size());
+
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(vData.size(), vKey);
+ for (size_t i = 0; i < vData.size(); i++) cont.SetByte(i, vData[i]);
+ hash2.Input(cont, 0, cont.size());
+
+ const uint8_t *p1 = hash1.Result();
+ const uint8_t *p2 = hash2.Result();
+ for (int i = 0; i < 20; i++) ASSERT_EQ(p1[i], p2[i]);
+}
+
+TEST(CryptoContainerTests, SHA1_CRC)
+{
+ std::vector<uint8_t> vData;
+ srand((unsigned int)__rdtsc());
+ for (size_t i = 0; i < 1111; i++) vData.push_back(rand() % 255);
+
+ SHA1 hash1, hash2;
+
+ hash1.Input(&vData[0], vData.size() - 4);
+ memcpy(&vData[vData.size() - 4], hash1.Result(), 4);
+
+ uint8_t vKey[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+ CryptoContainer cont(vData.size(), vKey);
+ for (size_t i = 0; i < vData.size(); i++) cont.SetByte(i, vData[i]);
+ hash2.Input(cont, 0, cont.size() - 4);
+ EXPECT_TRUE(hash1 == hash2);
+}
+
+TEST(CryptoContainerTests, GetByte)
+{
+ uint8_t vKey[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ const size_t nMax = 20;
+ CryptoContainer cont(nMax, vKey);
+ for (size_t i = 0; i < nMax; i++) cont.SetByte(i, static_cast<uint8_t>(i));
+
+ for(size_t i = 0; i < nMax; i++)
+ {
+ EXPECT_EQ(cont.GetByte(i), i);
+ }
+}