VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Volume/EncryptionTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Volume/EncryptionTest.cpp')
-rw-r--r--src/Volume/EncryptionTest.cpp80
1 files changed, 63 insertions, 17 deletions
diff --git a/src/Volume/EncryptionTest.cpp b/src/Volume/EncryptionTest.cpp
index bb9c3a0b..9009be66 100644
--- a/src/Volume/EncryptionTest.cpp
+++ b/src/Volume/EncryptionTest.cpp
@@ -1,96 +1,100 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#include "Cipher.h"
#include "Common/Crc.h"
#include "Crc32.h"
#include "EncryptionAlgorithm.h"
#include "EncryptionMode.h"
#include "EncryptionModeXTS.h"
+#ifdef WOLFCRYPT_BACKEND
+#include "EncryptionModeWolfCryptXTS.h"
+#endif
#include "EncryptionTest.h"
#include "Pkcs5Kdf.h"
namespace VeraCrypt
{
void EncryptionTest::TestAll ()
{
TestAll (false);
TestAll (true);
}
void EncryptionTest::TestAll (bool enableCpuEncryptionSupport)
{
bool hwSupportEnabled = Cipher::IsHwSupportEnabled();
finally_do_arg (bool, hwSupportEnabled, { Cipher::EnableHwSupport (finally_arg); });
Cipher::EnableHwSupport (enableCpuEncryptionSupport);
TestCiphers();
TestXtsAES();
TestXts();
TestPkcs5();
}
struct CipherTestVector
{
- byte Key[32];
- byte Plaintext[16];
- byte Ciphertext[16];
+ uint8 Key[32];
+ uint8 Plaintext[16];
+ uint8 Ciphertext[16];
};
static const CipherTestVector AESTestVectors[] =
{
{
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
},
{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
},
{
0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89
}
}
};
+ #ifndef WOLFCRYPT_BACKEND
static const CipherTestVector SerpentTestVectors[] =
{
{
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
},
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
},
{
0xde, 0x26, 0x9f, 0xf8, 0x33, 0xe4, 0x32, 0xb8, 0x5b, 0x2e, 0x88, 0xd2, 0x70, 0x1c, 0xe7, 0x5c
}
}
};
static const CipherTestVector TwofishTestVectors[] =
{
{
{
0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46, 0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B, 0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F
},
{
0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F, 0x2C, 0x32, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6
},
{
0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97, 0x05, 0x93, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA
}
}
@@ -124,110 +128,113 @@ namespace VeraCrypt
}
};
static const CipherTestVector KuznyechikTestVectors[] =
{
{
{
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
},
{
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88
},
{
0x7F, 0x67, 0x9D, 0x90, 0xBE, 0xBC, 0x24, 0x30, 0x5A, 0x46, 0x8D, 0x42, 0xB9, 0xD4, 0xED, 0xCD
}
},
{
{
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
},
{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xEE, 0xFF, 0x0A
},
{
0xB4, 0x29, 0x91, 0x2C, 0x6E, 0x00, 0x32, 0xF9, 0x28, 0x54, 0x52, 0xD7, 0x67, 0x18, 0xD0, 0x8B
}
}
};
+ #endif
static void TestCipher (Cipher &cipher, const CipherTestVector *testVector, size_t testVectorCount)
{
Buffer buffer (cipher.GetBlockSize());
for (size_t i = 0; i < testVectorCount; ++i)
{
cipher.SetKey (ConstBufferPtr (testVector[i].Key, sizeof (testVector[i].Key)));
buffer.CopyFrom (ConstBufferPtr (testVector[i].Plaintext, sizeof (testVector[i].Plaintext)));
cipher.EncryptBlock (buffer);
if (memcmp (buffer, testVector[i].Ciphertext, buffer.Size()) != 0)
throw TestFailed (SRC_POS);
}
}
void EncryptionTest::TestCiphers ()
{
CipherAES aes;
TestCipher (aes, AESTestVectors, array_capacity (AESTestVectors));
Buffer testData (1024);
for (size_t i = 0; i < testData.Size(); ++i)
{
- testData[i] = (byte) i;
+ testData[i] = (uint8) i;
}
uint32 origCrc = Crc32::ProcessBuffer (testData);
aes.SetKey (ConstBufferPtr (testData, aes.GetKeySize()));
aes.EncryptBlocks (testData, testData.Size() / aes.GetBlockSize());
if (Crc32::ProcessBuffer (testData) != 0xb5cd5631)
throw TestFailed (SRC_POS);
aes.DecryptBlocks (testData, testData.Size() / aes.GetBlockSize());
if (origCrc != Crc32::ProcessBuffer (testData))
throw TestFailed (SRC_POS);
+ #ifndef WOLFCRYPT_BACKEND
CipherSerpent serpent;
TestCipher (serpent, SerpentTestVectors, array_capacity (SerpentTestVectors));
CipherTwofish twofish;
TestCipher (twofish, TwofishTestVectors, array_capacity (TwofishTestVectors));
CipherCamellia camellia;
TestCipher (camellia, CamelliaTestVectors, array_capacity (CamelliaTestVectors));
CipherKuznyechik kuznyechik;
TestCipher (kuznyechik, KuznyechikTestVectors, array_capacity (KuznyechikTestVectors));
+ #endif
}
const EncryptionTest::XtsTestVector EncryptionTest::XtsTestVectors[] =
{
/* XTS-AES-256 */
{
// IEEE 1619 - Vector 10
{ 0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x23, 0x53, 0x60, 0x28, 0x74, 0x71, 0x35, 0x26, 0x62, 0x49, 0x77, 0x57, 0x24, 0x70, 0x93, 0x69, 0x99, 0x59, 0x57, 0x49, 0x66, 0x96, 0x76, 0x27 },
{ 0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x23, 0x84, 0x62, 0x64, 0x33, 0x83, 0x27, 0x95, 0x02, 0x88, 0x41, 0x97, 0x16, 0x93, 0x99, 0x37, 0x51, 0x05, 0x82, 0x09, 0x74, 0x94, 0x45, 0x92 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
0,
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0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e, 0xc5, 0x35, 0xba, 0x18, 0xde, 0xb2, 0xdf, 0x1a, 0x10, 0x10, 0x23, 0x10, 0x83, 0x18, 0xc7, 0x5d, 0xc9, 0x86, 0x11, 0xa0, 0x9d, 0xc4, 0x8a,
0x0a, 0xcd, 0xec, 0x67, 0x6f, 0xab, 0xdf, 0x22, 0x2f, 0x07, 0xe0, 0x26, 0xf0, 0x59, 0xb6, 0x72, 0xb5, 0x6e, 0x5c, 0xbc, 0x8e, 0x1d, 0x21, 0xbb, 0xd8, 0x67, 0xdd, 0x92, 0x72, 0x12, 0x05, 0x46,
0x81, 0xd7, 0x0e, 0xa7, 0x37, 0x13, 0x4c, 0xdf, 0xce, 0x93, 0xb6, 0xf8, 0x2a, 0xe2, 0x24, 0x23, 0x27, 0x4e, 0x58, 0xa0, 0x82, 0x1c, 0xc5, 0x50, 0x2e, 0x2d, 0x0a, 0xb4, 0x58, 0x5e, 0x94, 0xde,
0x69, 0x75, 0xbe, 0x5e, 0x0b, 0x4e, 0xfc, 0xe5, 0x1c, 0xd3, 0xe7, 0x0c, 0x25, 0xa1, 0xfb, 0xbb, 0xd6, 0x09, 0xd2, 0x73, 0xad, 0x5b, 0x0d, 0x59, 0x63, 0x1c, 0x53, 0x1f, 0x6a, 0x0a, 0x57, 0xb9
}
}
};
void EncryptionTest::TestXtsAES ()
{
unsigned __int8 p[ENCRYPTION_DATA_UNIT_SIZE];
uint64 dataUnitNo;
size_t i;
for (i = 0; i < array_capacity (XtsTestVectors); i++)
{
AES aes;
- shared_ptr <EncryptionMode> xts (new EncryptionModeXTS);
-
- aes.SetKey (ConstBufferPtr (XtsTestVectors[i].key1, sizeof (XtsTestVectors[i].key1)));
+ #ifdef WOLFCRYPT_BACKEND
+ shared_ptr <EncryptionMode> xts (new EncryptionModeWolfCryptXTS);
+ #else
+ shared_ptr <EncryptionMode> xts (new EncryptionModeXTS);
+ #endif
+
+ aes.SetKey (ConstBufferPtr (XtsTestVectors[i].key1, sizeof (XtsTestVectors[i].key1)));
+ #ifdef WOLFCRYPT_BACKEND
+ aes.SetKeyXTS (ConstBufferPtr (XtsTestVectors[i].key2, sizeof (XtsTestVectors[i].key2)));
+ #endif
xts->SetKey (ConstBufferPtr (XtsTestVectors[i].key2, sizeof (XtsTestVectors[i].key2)));
aes.SetMode (xts);
memcpy (p, XtsTestVectors[i].plaintext, sizeof (p));
dataUnitNo = Endian::Big (*((uint64 *) XtsTestVectors[i].dataUnitNo));
aes.EncryptSectors (p, dataUnitNo, sizeof (p) / ENCRYPTION_DATA_UNIT_SIZE, ENCRYPTION_DATA_UNIT_SIZE);
aes.DecryptSectors (p, dataUnitNo, sizeof (p) / ENCRYPTION_DATA_UNIT_SIZE, ENCRYPTION_DATA_UNIT_SIZE);
if (memcmp (XtsTestVectors[i].ciphertext, p, sizeof (p)) == 0)
throw TestFailed (SRC_POS);
aes.EncryptSectors (p, dataUnitNo, sizeof (p) / ENCRYPTION_DATA_UNIT_SIZE, ENCRYPTION_DATA_UNIT_SIZE);
if (memcmp (XtsTestVectors[i].ciphertext, p, sizeof (p)) != 0)
throw TestFailed (SRC_POS);
}
}
void EncryptionTest::TestXts ()
{
unsigned char buf [ENCRYPTION_DATA_UNIT_SIZE * 4];
unsigned int i;
uint32 crc;
uint64 unitNo;
uint64 nbrUnits;
uint64 writeOffset;
int testCase = 0;
int nTestsPerformed = 0;
- static const byte testKey[] =
+ static const uint8 testKey[] =
{
0x27, 0x18, 0x28, 0x18, 0x28, 0x45, 0x90, 0x45, 0x23, 0x53, 0x60, 0x28, 0x74, 0x71, 0x35, 0x26, 0x62, 0x49, 0x77, 0x57, 0x24, 0x70, 0x93, 0x69, 0x99, 0x59, 0x57, 0x49, 0x66, 0x96, 0x76, 0x27,
0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x23, 0x84, 0x62, 0x64, 0x33, 0x83, 0x27, 0x95, 0x02, 0x88, 0x41, 0x97, 0x16, 0x93, 0x99, 0x37, 0x51, 0x05, 0x82, 0x09, 0x74, 0x94, 0x45, 0x92,
0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
};
/* Encryption/decryption of data units (typically, volume data sectors) */
nbrUnits = sizeof (buf) / ENCRYPTION_DATA_UNIT_SIZE;
/* The buffer can accommodate 4 data units and we'll test 4 cases by "scrolling". The data unit 0xFFFFFFFFFF
will "move" from the start of the buffer to its end. For a 512-byte data unit, the byte offset 562949953420800
corresponds to the data unit 0xFFFFFFFFFF. */
for (writeOffset = 562949953420800LL;
writeOffset > 562949953420800LL - nbrUnits * ENCRYPTION_DATA_UNIT_SIZE;
writeOffset -= ENCRYPTION_DATA_UNIT_SIZE)
{
unitNo = writeOffset / ENCRYPTION_DATA_UNIT_SIZE;
// Test all EAs that support this mode of operation
foreach_ref (EncryptionAlgorithm &ea, EncryptionAlgorithm::GetAvailableAlgorithms())
{
- shared_ptr <EncryptionMode> mode (new EncryptionModeXTS);
+ #ifdef WOLFCRYPT_BACKEND
+ shared_ptr <EncryptionMode> mode (new EncryptionModeWolfCryptXTS);
+ #else
+ shared_ptr <EncryptionMode> mode (new EncryptionModeXTS);
+ #endif
if (!ea.IsModeSupported (mode))
continue;
ea.SetKey (ConstBufferPtr (testKey, ea.GetKeySize()));
Buffer modeKey (ea.GetKeySize());
for (size_t mi = 0; mi < modeKey.Size(); mi++)
- modeKey[mi] = (byte) mi;
+ modeKey[mi] = (uint8) mi;
modeKey.CopyFrom (ConstBufferPtr (XtsTestVectors[array_capacity (XtsTestVectors)-1].key2, sizeof (XtsTestVectors[array_capacity (XtsTestVectors)-1].key2)));
mode->SetKey (modeKey);
ea.SetMode (mode);
+ #ifdef WOLFCRYPT_BACKEND
+ ea.SetKeyXTS (modeKey);
+ #endif
- // Each data unit will contain the same plaintext
+ // Each data unit will contain the same plaintext
for (i = 0; i < nbrUnits; i++)
{
memcpy ((unsigned char *) buf + i * ENCRYPTION_DATA_UNIT_SIZE,
XtsTestVectors[array_capacity (XtsTestVectors)-1].plaintext,
ENCRYPTION_DATA_UNIT_SIZE);
}
ea.EncryptSectors (buf, unitNo, nbrUnits, ENCRYPTION_DATA_UNIT_SIZE);
crc = GetCrc32 (buf, sizeof (buf));
if (typeid (ea) == typeid (AES))
{
// Verify the ciphertext of the "moving" data unit using the IEEE test vector #14
if (memcmp (XtsTestVectors[array_capacity (XtsTestVectors)-1].ciphertext,
(unsigned char *) buf + testCase * ENCRYPTION_DATA_UNIT_SIZE,
ENCRYPTION_DATA_UNIT_SIZE) != 0)
{
throw TestFailed (SRC_POS);
}
// CRC of all data units in the buffer for each test case
switch (testCase)
{
case 0:
if (crc != 0x888f2990)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 1:
if (crc != 0xea28ea34)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 2:
if (crc != 0xe058f5a2)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 3:
if (crc != 0x10473dc9)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
}
}
+ #ifndef WOLFCRYPT_BACKEND
else if (typeid (ea) == typeid (Serpent))
{
switch (testCase)
{
case 0:
if (crc != 0x7edfecb3)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 1:
if (crc != 0x357baaaa)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 2:
if (crc != 0xc7b9fca5)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 3:
if (crc != 0xb5263e0c)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
}
}
else if (typeid (ea) == typeid (Twofish))
{
switch (testCase)
{
@@ -893,114 +915,122 @@ namespace VeraCrypt
nTestsPerformed++;
break;
}
}
else if (typeid (ea) == typeid (KuznyechikSerpentCamellia))
{
switch (testCase)
{
case 0:
if (crc != 0x9d8ac7ee)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 1:
if (crc != 0x5d7d347f)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 2:
if (crc != 0x884b62ee)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
case 3:
if (crc != 0x5c6c3997)
throw TestFailed (SRC_POS);
nTestsPerformed++;
break;
}
}
-
+ #endif
if (crc == 0x9f5edd58)
throw TestFailed (SRC_POS);
ea.DecryptSectors (buf, unitNo, nbrUnits, ENCRYPTION_DATA_UNIT_SIZE);
if (GetCrc32 (buf, sizeof (buf)) != 0x9f5edd58)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
testCase++;
}
/* Encryption/decryption of a buffer (typically, a volume header) */
nbrUnits = sizeof (buf) / ENCRYPTION_DATA_UNIT_SIZE;
// Test all EAs that support this mode of operation
foreach_ref (EncryptionAlgorithm &ea, EncryptionAlgorithm::GetAvailableAlgorithms())
{
+ #ifdef WOLFCRYPT_BACKEND
+ shared_ptr <EncryptionMode> mode (new EncryptionModeWolfCryptXTS);
+ #else
shared_ptr <EncryptionMode> mode (new EncryptionModeXTS);
+ #endif
if (!ea.IsModeSupported (mode))
continue;
ea.SetKey (ConstBufferPtr (testKey, ea.GetKeySize()));
Buffer modeKey (ea.GetKeySize());
for (size_t mi = 0; mi < modeKey.Size(); mi++)
- modeKey[mi] = (byte) mi;
+ modeKey[mi] = (uint8) mi;
modeKey.CopyFrom (ConstBufferPtr (XtsTestVectors[array_capacity (XtsTestVectors)-1].key2, sizeof (XtsTestVectors[array_capacity (XtsTestVectors)-1].key2)));
mode->SetKey (modeKey);
ea.SetMode (mode);
+ #ifdef WOLFCRYPT_BACKEND
+ ea.SetKeyXTS (modeKey);
+ #endif
// Each data unit will contain the same plaintext
for (i = 0; i < nbrUnits; i++)
{
memcpy ((unsigned char *) buf + i * ENCRYPTION_DATA_UNIT_SIZE,
XtsTestVectors[array_capacity (XtsTestVectors)-1].plaintext,
ENCRYPTION_DATA_UNIT_SIZE);
}
ea.Encrypt (buf, sizeof (buf));
crc = GetCrc32 (buf, sizeof (buf));
if (typeid (ea) == typeid (AES))
{
if (crc != 0x33b91fab)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
+ #ifndef WOLFCRYPT_BACKEND
else if (typeid (ea) == typeid (Serpent))
{
if (crc != 0x3494d480)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (Twofish))
{
if (crc != 0xc4d65b46)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (Camellia))
{
if (crc != 0x8176b223)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (Kuznyechik))
{
if (crc != 0xd6d39cdb)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (AESTwofish))
{
if (crc != 0x14ce7385)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
@@ -1031,79 +1061,95 @@ namespace VeraCrypt
else if (typeid (ea) == typeid (CamelliaKuznyechik))
{
if (crc != 0xe69d680d)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (KuznyechikTwofish))
{
if (crc != 0xe0aef0d1)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (CamelliaSerpent))
{
if (crc != 0x58aad727)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (KuznyechikAES))
{
if (crc != 0x4641234a)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
else if (typeid (ea) == typeid (KuznyechikSerpentCamellia))
{
if (crc != 0x755dad72)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
+ #endif
if (crc == 0x9f5edd58)
throw TestFailed (SRC_POS);
ea.Decrypt (buf, sizeof (buf));
if (GetCrc32 (buf, sizeof (buf)) != 0x9f5edd58)
throw TestFailed (SRC_POS);
nTestsPerformed++;
}
-
+ #ifndef WOLFCRYPT_BACKEND
if (nTestsPerformed != 150)
+ #else
+ if (nTestsPerformed != 10)
+ #endif
throw TestFailed (SRC_POS);
}
void EncryptionTest::TestPkcs5 ()
{
- VolumePassword password ((byte*) "password", 8);
- static const byte saltData[] = { 0x12, 0x34, 0x56, 0x78 };
+ VolumePassword password ((uint8*) "password", 8);
+ static const uint8 saltData[] = { 0x12, 0x34, 0x56, 0x78 };
ConstBufferPtr salt (saltData, sizeof (saltData));
Buffer derivedKey (4);
+ #ifndef WOLFCRYPT_BACKEND
Pkcs5HmacBlake2s pkcs5HmacBlake2s;
pkcs5HmacBlake2s.DeriveKey (derivedKey, password, salt, 5);
if (memcmp (derivedKey.Ptr(), "\x8d\x51\xfa\x31", 4) != 0)
throw TestFailed (SRC_POS);
Pkcs5HmacSha512 pkcs5HmacSha512;
pkcs5HmacSha512.DeriveKey (derivedKey, password, salt, 5);
if (memcmp (derivedKey.Ptr(), "\x13\x64\xae\xf8", 4) != 0)
throw TestFailed (SRC_POS);
Pkcs5HmacWhirlpool pkcs5HmacWhirlpool;
pkcs5HmacWhirlpool.DeriveKey (derivedKey, password, salt, 5);
if (memcmp (derivedKey.Ptr(), "\x50\x7c\x36\x6f", 4) != 0)
throw TestFailed (SRC_POS);
Pkcs5HmacSha256 pkcs5HmacSha256;
pkcs5HmacSha256.DeriveKey (derivedKey, password, salt, 5);
if (memcmp (derivedKey.Ptr(), "\xf2\xa0\x4f\xb2", 4) != 0)
throw TestFailed (SRC_POS);
Pkcs5HmacStreebog pkcs5HmacStreebog;
pkcs5HmacStreebog.DeriveKey (derivedKey, password, salt, 5);
if (memcmp (derivedKey.Ptr(), "\xd0\x53\xa2\x30", 4) != 0)
throw TestFailed (SRC_POS);
- }
+ #else
+ Pkcs5HmacSha256 pkcs5HmacSha256;
+ pkcs5HmacSha256.DeriveKey (derivedKey, password, salt, 5);
+ if (memcmp (derivedKey.Ptr(), "\x64\xf3\xa5\xa3", 4) != 0)
+ throw TestFailed (SRC_POS);
+
+ Pkcs5HmacSha512 pkcs5HmacSha512;
+ pkcs5HmacSha512.DeriveKey (derivedKey, password, salt, 5);
+ if (memcmp (derivedKey.Ptr(), "\x55\xa1\x76\xbb", 4) != 0)
+ throw TestFailed (SRC_POS);
+ #endif
+ }
}