IteratorUsage tests: Add some canary nulled space before begin.
Hardening against write-before-begin.
This commit is contained in:
@@ -117,18 +117,19 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest)
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 44> out;
|
||||
std::array<uint8_t, 47> out;
|
||||
out.fill(0);
|
||||
|
||||
std::array<uint8_t, 44> expected =
|
||||
std::array<uint8_t, 47> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Encrypt(out.begin(), data.begin(), 22);
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 22);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -138,18 +139,19 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest)
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 44> out;
|
||||
std::array<uint8_t, 47> out;
|
||||
out.fill(0);
|
||||
|
||||
std::array<uint8_t, 44> expected =
|
||||
std::array<uint8_t, 47> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0xef, 0xa5, 0xfa, 0xe4, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Encrypt(out.begin(), data.begin(), 27);
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 27);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -165,7 +167,7 @@ TEST(Arc4CryptTests, EncryptOutIteratorUsageTest)
|
||||
std::array<uint8_t, 44> expected;
|
||||
expected.fill(0);
|
||||
|
||||
crypt.Encrypt(out.begin(), data.begin(), 0);
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -180,16 +182,17 @@ TEST(Arc4CryptTests, DecryptOutIteratorUsageTest)
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 14> out;
|
||||
std::array<uint8_t, 17> out;
|
||||
out.fill(0);
|
||||
|
||||
std::array<uint8_t, 14> expected =
|
||||
std::array<uint8_t, 17> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
'A', 't', 't', 'a', 'c', 'k', ' ', 'a', 't', ' ', 'd', 'a',
|
||||
0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Decrypt(out.begin(), data.begin(), 12);
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 12);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -197,16 +200,17 @@ TEST(Arc4CryptTests, DecryptOutIteratorUsageTest)
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 14> out;
|
||||
std::array<uint8_t, 17> out;
|
||||
out.fill(0);
|
||||
|
||||
std::array<uint8_t, 14> expected =
|
||||
std::array<uint8_t, 17> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
'A', 't', 't', 'a', 'c', 'k', ' ',
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Decrypt(out.begin(), data.begin(), 7);
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 7);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -220,7 +224,7 @@ TEST(Arc4CryptTests, DecryptOutIteratorUsageTest)
|
||||
std::array<uint8_t, 14> expected;
|
||||
expected.fill(0);
|
||||
|
||||
crypt.Decrypt(out.begin(), data.begin(), 0);
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
@@ -360,17 +360,18 @@ TEST(Arc4GenTests, GenerateOutIteratorUsageTest)
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 20> out;
|
||||
std::array<uint8_t, 23> out;
|
||||
out.fill(0);
|
||||
|
||||
std::array<uint8_t, 20> expected =
|
||||
std::array<uint8_t, 23> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
gen.Generate(out.begin(), 11);
|
||||
gen.Generate(out.begin() + 3, 11);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -379,17 +380,18 @@ TEST(Arc4GenTests, GenerateOutIteratorUsageTest)
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 20> out;
|
||||
std::array<uint8_t, 23> out;
|
||||
out.fill(0);
|
||||
|
||||
std::array<uint8_t, 20> expected =
|
||||
std::array<uint8_t, 23> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
|
||||
0x69, 0x82, 0x00, 0x00
|
||||
};
|
||||
|
||||
gen.Generate(out.begin(), 18);
|
||||
gen.Generate(out.begin() + 3, 18);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
@@ -404,7 +406,7 @@ TEST(Arc4GenTests, GenerateOutIteratorUsageTest)
|
||||
std::array<uint8_t, 20> expected;
|
||||
expected.fill(0);
|
||||
|
||||
gen.Generate(out.begin(), 0);
|
||||
gen.Generate(out.begin() + 3, 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
@@ -381,13 +381,13 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
||||
std::array<uint8_t, DesCrypt::BlockSize> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
|
||||
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, 8> fact = {};
|
||||
// Last 3 bytes should be untouched.
|
||||
std::array<uint8_t, 8> expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 };
|
||||
std::array<uint8_t, 11> fact = {};
|
||||
// First and last 3 bytes should be untouched.
|
||||
std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end());
|
||||
enc.EncryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
@@ -396,13 +396,13 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
||||
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, 8> fact = {};
|
||||
// Last 4 bytes should be untouched.
|
||||
std::array<uint8_t, 8> expected = { 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 };
|
||||
std::array<uint8_t, 12> fact = {};
|
||||
// First and last 4 bytes should be untouched.
|
||||
std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end());
|
||||
enc.EncryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
@@ -414,13 +414,13 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
|
||||
std::array<uint8_t, DesCrypt::BlockSize> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
|
||||
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, 8> fact = {};
|
||||
// Last 3 bytes should be untouched.
|
||||
std::array<uint8_t, 8> expected = { 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 };
|
||||
std::array<uint8_t, 11> fact = {};
|
||||
// First and last 3 bytes should be untouched.
|
||||
std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(fact.begin(), fact.end() - 3, data.begin(), data.end());
|
||||
dec.DecryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
@@ -429,13 +429,13 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
|
||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
||||
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, 8> fact = {};
|
||||
// Last 4 bytes should be untouched.
|
||||
std::array<uint8_t, 8> expected = { 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 };
|
||||
std::array<uint8_t, 12> fact = {};
|
||||
// First and last 4 bytes should be untouched.
|
||||
std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(fact.begin(), fact.end() - 4, data.begin(), data.end());
|
||||
dec.DecryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user