Move dependencies to the top level.

This commit is contained in:
jakcron
2022-04-16 18:27:14 +08:00
parent b67980d3d2
commit 5d62e839e7
844 changed files with 74 additions and 114431 deletions
@@ -1,671 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include <sstream>
#include "ByteData_TestClass.h"
//---------------------------------------------------------
void ByteData_TestClass::runAllTests(void)
{
std::cout << "[tc::ByteData] START" << std::endl;
test_Constructor_DefaultConstructor();
test_Constructor_InitializerList();
test_Constructor_CreateZeroSized();
test_Constructor_CreateSmallSized();
test_Constructor_CreateLargeSized();
test_Constructor_ThrowExceptForBadAlloc();
test_Constructor_CreateFromPtr();
test_ImplicitCopy_CopyInSameScope();
test_ImplicitCopy_CopyOntoInitiallisedByteData();
test_ImplicitMove_CopyInSameScope();
test_ImplicitMove_MoveOntoInitiallisedByteData();
test_EqualityOperator();
test_InequalityOperator();
std::cout << "[tc::ByteData] END" << std::endl;
}
//---------------------------------------------------------
void ByteData_TestClass::test_Constructor_DefaultConstructor()
{
std::cout << "[tc::ByteData] test_Constructor_DefaultConstructor : " << std::flush;
try
{
try
{
tc::ByteData data;
if (data.data() != nullptr)
{
throw tc::Exception(".data() did not return nullptr when ByteData was constructed with default constructor");
}
if (data.size() != 0)
{
throw tc::Exception(".size() did not return 0 when ByteData was constructed with default constructor");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_Constructor_CreateZeroSized()
{
std::cout << "[tc::ByteData] test_Constructor_CreateZeroSized : " << std::flush;
try
{
try
{
tc::ByteData data(0);
if (data.data() != nullptr)
{
throw tc::Exception(".data() did not return nullptr when ByteData was constructed with size 0");
}
if (data.size() != 0)
{
throw tc::Exception(".size() did not return 0 when ByteData was constructed with size 0");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_Constructor_InitializerList()
{
std::cout << "[tc::ByteData] test_Constructor_InitializerList : " << std::flush;
try
{
try
{
std::stringstream error_ss;
static const size_t expected_data_size = 0x10;
byte_t expected_data[expected_data_size] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
tc::ByteData data({0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f});
if (data.data() == nullptr)
{
error_ss << ".data() returned nullptr when ByteData was constructed with an initializer list";
throw tc::Exception(error_ss.str());
}
if (data.size() != expected_data_size)
{
error_ss << ".size() did not return " << expected_data_size << " when ByteData was constructed with an initializer list";
throw tc::Exception(error_ss.str());
}
if (memcmp(data.data(), expected_data, expected_data_size) != 0)
{
error_ss << ".data() did not contain expected data";
throw tc::Exception(error_ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_Constructor_CreateSmallSized()
{
std::cout << "[tc::ByteData] test_Constructor_CreateSmallSized : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = 1271;
tc::ByteData data(data_size);
if (data.data() == nullptr)
{
error_ss << ".data() returned nullptr when ByteData was constructed with size " << data_size;
throw tc::Exception(error_ss.str());
}
if (data.size() != data_size)
{
error_ss << ".size() did not return " << data_size << " when ByteData was constructed with size " << data_size;
throw tc::Exception(error_ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_Constructor_CreateLargeSized()
{
std::cout << "[tc::ByteData] test_Constructor_CreateLargeSized : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = 0x1000000;
tc::ByteData data(data_size);
if (data.data() == nullptr)
{
error_ss << ".data() returned nullptr when ByteData was constructed with size " << data_size;
throw tc::Exception(error_ss.str());
}
if (data.size() != data_size)
{
error_ss << ".size() did not return " << data_size << " when ByteData was constructed with size " << data_size;
throw tc::Exception(error_ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_Constructor_ThrowExceptForBadAlloc()
{
std::cout << "[tc::ByteData] test_Constructor_ThrowExceptForBadAlloc : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = -1;
tc::ByteData data(data_size);
std::cout << "FAIL (Did not throw exception where it should be impossible to allocate the memory)" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "PASS (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_Constructor_CreateFromPtr()
{
std::cout << "[tc::ByteData] test_Constructor_CreateFromPtr : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t src_data_size = 0x1000;
tc::ByteData src_data(src_data_size);
memset(src_data.data(), 0xe0, src_data.size());
tc::ByteData data(src_data.data(), src_data.size());
if (data.data() == nullptr)
{
error_ss << ".data() returned nullptr when ByteData was constructed with size " << src_data_size;
throw tc::Exception(error_ss.str());
}
if (data.size() != src_data_size)
{
error_ss << ".size() did not return " << src_data_size << " when ByteData was constructed with size " << src_data_size;
throw tc::Exception(error_ss.str());
}
if (memcmp(data.data(), src_data.data(), src_data_size) != 0)
{
error_ss << ".data() did not have expected contents, when compared to source data";
throw tc::Exception(error_ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_ImplicitCopy_CopyInSameScope()
{
std::cout << "[tc::ByteData] test_ImplicitCopy_CopyInSameScope : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = 0x20;
// create data with allocating constructor
tc::ByteData data(data_size);
// create data2 as a copy of data using implicit copy constructor
tc::ByteData data2(data);
if (data.size() != data2.size())
{
throw tc::Exception("data2 after being constructed by copy, did not have the same size");
}
if (data.data() == data2.data())
{
throw tc::Exception("data2 after being constructed by copy, had the same pointer");
}
tc::ByteData data3 = data;
if (data.size() != data3.size())
{
throw tc::Exception("data3 after being constructed by copy assignment, did not have the same size");
}
if (data.data() == data3.data())
{
throw tc::Exception("data3 after being constructed by copy assignment, had the same pointer");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_ImplicitCopy_CopyOntoInitiallisedByteData()
{
std::cout << "[tc::ByteData] test_ImplicitCopy_CopyOntoInitiallisedByteData : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = 0x20;
const size_t data2_size = 0x30;
// create data with allocating constructor
tc::ByteData data(data_size);
// create data2 with allocating constructor
tc::ByteData data2(data2_size);
data2 = data;
if (data.size() != data2.size())
{
throw tc::Exception("data2 after being assigned by copy, did not have the same size");
}
if (data.data() == data2.data())
{
throw tc::Exception("data2 after being assigned by copy, had the same pointer");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_ImplicitMove_CopyInSameScope()
{
std::cout << "[tc::ByteData] test_ImplicitMove_CopyInSameScope : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = 0x20;
// create data with allocating constructor
tc::ByteData data(data_size);
// mark buffer[0] so we can see if buffer ptr is being moved properly
data.data()[0] = 0xff;
// create data2 as a copy of data using implicit move constructor
tc::ByteData data2(std::move(data));
if (data2.data()[0] != 0xff)
{
throw tc::Exception("data2 did not have expected byte at buffer()[0]");
}
if (data.data() != nullptr)
{
throw tc::Exception("data after being moved to data2 retained it's old pointer");
}
if (data.size() == data2.size())
{
throw tc::Exception("data2 after being constructed by move from data, had the same size");
}
tc::ByteData data3 = std::move(data2);
if (data3.data()[0] != 0xff)
{
throw tc::Exception("data3 did not have expected byte at buffer()[0]");
}
if (data2.data() != nullptr)
{
throw tc::Exception("data2 after being moved to data3 retained it's old pointer");
}
if (data.size() == data3.size())
{
throw tc::Exception("data3 after being constructed by move assignment from data2, has the same size");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_ImplicitMove_MoveOntoInitiallisedByteData()
{
std::cout << "[tc::ByteData] test_ImplicitMove_MoveOntoInitiallisedByteData : " << std::flush;
try
{
try
{
std::stringstream error_ss;
const size_t data_size = 0x20;
const size_t data2_size = 0x30;
// create data with allocating constructor
tc::ByteData data(data_size);
// mark buffer[0] so we can see if buffer ptr is being copied properly
data.data()[0] = 0xff;
// create data2 with allocating constructor
tc::ByteData data2(data2_size);
// move data to data2 by assignment
data2 = std::move(data);
if (data.data() != nullptr)
{
throw tc::Exception("data after being moved to data2 retained it's old pointer");
}
if (data.size() == data2.size())
{
throw tc::Exception("data2 after being assigned by copy, did not have the same size");
}
if (data.size() == data2.size())
{
throw tc::Exception("data2 after being constructed by move from data, had the same size");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_EqualityOperator()
{
std::cout << "[tc::ByteData] test_EqualityOperator : " << std::flush;
try
{
try
{
std::stringstream error_ss;
static const size_t kControlDataSize = 0x10;
const byte_t kControlData[kControlDataSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
auto control_data = tc::ByteData(kControlData, kControlDataSize);
// test 1 - data is the same
{
auto same_data = tc::ByteData(kControlData, kControlDataSize);
if ((control_data == same_data) != true)
{
throw tc::Exception("operator==(control, test) returned false for identical ByteData");
}
if ((same_data == control_data) != true)
{
throw tc::Exception("operator==(test, control) returned false for identical ByteData");
}
}
// test 2 - truncated ByteData
{
auto smaller = tc::ByteData(control_data.data(), control_data.size()-1);
if ((control_data == smaller) != false)
{
throw tc::Exception("operator==(control, test) returned true for when compared to truncated ByteData");
}
if ((smaller == control_data) != false)
{
throw tc::Exception("operator==(test, control) returned true for when compared to truncated ByteData");
}
}
// test 3 - same size different ByteData
{
auto different = tc::ByteData(control_data);
different[different.size()-1] = 0xff;
if ((control_data == different) != false)
{
throw tc::Exception("operator==(control, test) returned true for when compared to same size but different ByteData");
}
if ((different == control_data) != false)
{
throw tc::Exception("operator==(test, control) returned true for when compared to same size but different ByteData");
}
}
// test 4 - null ByteData
{
auto empty_data = tc::ByteData();
if ((control_data == empty_data) != false)
{
throw tc::Exception("operator==(control, test) returned true for when compared to empty ByteData");
}
if ((empty_data == control_data) != false)
{
throw tc::Exception("operator==(test, control) returned true for when compared to empty ByteData");
}
}
// test 5 - null to null
{
auto empty_data = tc::ByteData();
auto empty_data2 = tc::ByteData();
if ((empty_data2 == empty_data) != true)
{
throw tc::Exception("operator==(empty, empty2) returned true for when comparing two empty ByteData");
}
if ((empty_data == empty_data2) != true)
{
throw tc::Exception("operator==(empty2, empty) returned true for when comparing two empty ByteData");
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void ByteData_TestClass::test_InequalityOperator()
{
std::cout << "[tc::ByteData] test_InequalityOperator : " << std::flush;
try
{
try
{
std::stringstream error_ss;
static const size_t kControlDataSize = 0x10;
const byte_t kControlData[kControlDataSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
auto control_data = tc::ByteData(kControlData, kControlDataSize);
// test 1 - data is the same
{
auto same_data = tc::ByteData(kControlData, kControlDataSize);
if ((control_data != same_data) != false)
{
throw tc::Exception("operator!=(control, test) returned false for identical ByteData");
}
if ((same_data != control_data) != false)
{
throw tc::Exception("operator!=(test, control) returned false for identical ByteData");
}
}
// test 2 - truncated ByteData
{
auto smaller = tc::ByteData(control_data.data(), control_data.size()-1);
if ((control_data != smaller) != true)
{
throw tc::Exception("operator!=(control, test) returned true for when compared to truncated ByteData");
}
if ((smaller != control_data) != true)
{
throw tc::Exception("operator!=(test, control) returned true for when compared to truncated ByteData");
}
}
// test 3 - same size different ByteData
{
auto different = tc::ByteData(control_data);
different[different.size()-1] = 0xff;
if ((control_data != different) != true)
{
throw tc::Exception("operator!=(control, test) returned true for when compared to same size but different ByteData");
}
if ((different != control_data) != true)
{
throw tc::Exception("operator!=(test, control) returned true for when compared to same size but different ByteData");
}
}
// test 4 - null ByteData
{
auto empty_data = tc::ByteData();
if ((control_data != empty_data) != true)
{
throw tc::Exception("operator!=(control, test) returned true for when compared to empty ByteData");
}
if ((empty_data != control_data) != true)
{
throw tc::Exception("operator!=(test, control) returned true for when compared to empty ByteData");
}
}
// test 5 - null to null
{
auto empty_data = tc::ByteData();
auto empty_data2 = tc::ByteData();
if ((empty_data2 != empty_data) != false)
{
throw tc::Exception("operator!=(empty, empty2) returned true for when comparing two empty ByteData");
}
if ((empty_data != empty_data2) != false)
{
throw tc::Exception("operator!=(empty2, empty) returned true for when comparing two empty ByteData");
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,23 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/ByteData.h>
class ByteData_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constructor_DefaultConstructor();
void test_Constructor_InitializerList();
void test_Constructor_CreateZeroSized();
void test_Constructor_CreateSmallSized();
void test_Constructor_CreateLargeSized();
void test_Constructor_ThrowExceptForBadAlloc();
void test_Constructor_CreateFromPtr();
void test_ImplicitCopy_CopyInSameScope();
void test_ImplicitCopy_CopyOntoInitiallisedByteData();
void test_ImplicitMove_CopyInSameScope();
void test_ImplicitMove_MoveOntoInitiallisedByteData();
void test_EqualityOperator();
void test_InequalityOperator();
};
@@ -1,3 +0,0 @@
#include "FileSystemTestUtil.h"
const std::string FileSystemTestUtil::DummyFileSystemBase::kClassName = "DummyFileSystemBase";
@@ -1,79 +0,0 @@
#pragma once
#include <tc/io.h>
#include <tc/NotImplementedException.h>
class FileSystemTestUtil
{
public:
class DummyFileSystemBase : public tc::io::IFileSystem
{
public:
DummyFileSystemBase()
{
init();
}
void init()
{
dispose();
mCurDir = std::make_shared<tc::io::Path>(tc::io::Path("/some/initial/path/"));
mState.set(tc::RESFLAG_READY);
}
virtual tc::ResourceStatus state()
{
return mState;
}
virtual void dispose()
{
mState.reset();
mCurDir.reset();
}
virtual void createFile(const tc::io::Path& path)
{
throw tc::NotImplementedException(kClassName, "createFile() not implemented");
}
virtual void removeFile(const tc::io::Path& path)
{
throw tc::NotImplementedException(kClassName, "removeFile() not implemented");
}
virtual void openFile(const tc::io::Path& path, tc::io::FileMode mode, tc::io::FileAccess access, std::shared_ptr<tc::io::IStream>& stream)
{
throw tc::NotImplementedException(kClassName, "openFile() not implemented");
}
virtual void createDirectory(const tc::io::Path& path)
{
throw tc::NotImplementedException(kClassName, "createDirectory() not implemented");
}
virtual void removeDirectory(const tc::io::Path& path)
{
throw tc::NotImplementedException(kClassName, "removeDirectory() not implemented");
}
virtual void getWorkingDirectory(tc::io::Path& path)
{
path = *mCurDir;
}
virtual void setWorkingDirectory(const tc::io::Path& path)
{
*mCurDir = path;
}
virtual void getDirectoryListing(const tc::io::Path& path, tc::io::sDirectoryListing& info)
{
throw tc::NotImplementedException(kClassName, "getDirectoryListing() not implemented");
}
private:
static const std::string kClassName;
tc::ResourceStatus mState;
std::shared_ptr<tc::io::Path> mCurDir;
};
};
@@ -1,8 +0,0 @@
#pragma once
class ITestClass
{
public:
virtual ~ITestClass() = default;
virtual void runAllTests() = 0;
};
@@ -1,300 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include "Optional_TestClass.h"
void Optional_TestClass::runAllTests()
{
std::cout << "[tc::Optional] START" << std::endl;
testDefaultConstructor();
testWrapConstructor();
testCopyConstructorFromNullOptional();
testCopyConstructorFromExistingOptional();
testWrapOperator();
testCopyOperatorFromNullOptional();
testCopyOperatorFromExistingOptional();
testMakeNullOnNullOptional();
testMakeNullOnExistingOptional();
std::cout << "[tc::Optional] END" << std::endl;
}
void Optional_TestClass::testDefaultConstructor()
{
std::cout << "[tc::Optional] testDefaultConstructor : " << std::flush;
try
{
{
tc::Optional<int> foo;
if (foo.isNull() == false)
{
throw tc::Exception("Default constructor created an object with null state, but isNull() returned false");
}
if (foo.isSet() == true)
{
throw tc::Exception("Default constructor created an object with null state, but isSet() returned true");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testWrapConstructor()
{
std::cout << "[tc::Optional] testWrapConstructor : " << std::flush;
try
{
{
int testInt = 42;
tc::Optional<int> foo(testInt);
if (foo.isNull() == true)
{
throw tc::Exception("Wrapping constructor created an object with a valid state, but isNull() returned true");
}
if (foo.isSet() == false)
{
throw tc::Exception("Wrapping constructor created an object with a valid state, but isSet() returned false");
}
if (foo.get() != testInt)
{
throw tc::Exception("Wrapping constructor created an object with an incorrect value");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testCopyConstructorFromNullOptional()
{
std::cout << "[tc::Optional] testCopyConstructorFromNullOptional : " << std::flush;
try
{
{
tc::Optional<int> foo;
tc::Optional<int> bar(foo);
if (bar.isNull() == false)
{
throw tc::Exception("Copy constructor created an object with a null state, but isNull() returned false");
}
if (bar.isSet() == true)
{
throw tc::Exception("Copy constructor created an object with a null state, but isSet() returned true");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testCopyConstructorFromExistingOptional()
{
std::cout << "[tc::Optional] testCopyConstructorFromExistingOptional : " << std::flush;
try
{
{
int testInt = 42;
tc::Optional<int> foo(testInt);
tc::Optional<int> bar(foo);
if (bar.isNull() == true)
{
throw tc::Exception("Copy constructor created an object with a set state, but isNull() returned true");
}
if (bar.isSet() == false)
{
throw tc::Exception("Copy constructor created an object with a set state, but isSet() returned true");
}
if (bar.get() != testInt)
{
throw tc::Exception("Copy constructor created an object where the wrapped value was unexpected");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testWrapOperator()
{
std::cout << "[tc::Optional] testWrapOperator : " << std::flush;
try
{
{
int testInt = 42;
tc::Optional<int> foo;
foo = testInt;
if (foo.isNull() == true)
{
throw tc::Exception("Wrap operator created an object with a set state, but isNull() returned true");
}
if (foo.isSet() == false)
{
throw tc::Exception("Wrap operator created an object with a set state, but isSet() returned false");
}
if (foo.get() != testInt)
{
throw tc::Exception("Wrap operator created an object where the wrapped value was unexpected");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testCopyOperatorFromNullOptional()
{
std::cout << "[tc::Optional] testCopyOperatorFromNullOptional : " << std::flush;
try
{
{
tc::Optional<int> foo;
tc::Optional<int> bar = foo;
if (bar.isNull() == false)
{
throw tc::Exception("Copy operator created an object with a null state, but isNull() returned false");
}
if (bar.isSet() == true)
{
throw tc::Exception("Copy operator created an object with a null state, but isSet() returned false");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testCopyOperatorFromExistingOptional()
{
std::cout << "[tc::Optional] testCopyOperatorFromExistingOptional : " << std::flush;
try
{
{
int testInt = 42;
tc::Optional<int> foo(testInt);
tc::Optional<int> bar = foo;
if (bar.isNull() == true)
{
throw tc::Exception("Copy operator created an object with a set state, but isNull() returned true");
}
if (bar.isSet() == false)
{
throw tc::Exception("Copy operator created an object with a set state, but isSet() returned false");
}
if (bar.get() != testInt)
{
throw tc::Exception("Copy operator created an object where the wrapped value was unexpected");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testMakeNullOnNullOptional()
{
std::cout << "[tc::Optional] testMakeNullOnNullOptional : " << std::flush;
try
{
{
tc::Optional<int> foo;
foo.makeNull();
if (foo.isNull() == false)
{
throw tc::Exception("A null Object was nulled by makeNull(), but isNull() returned false");
}
if (foo.isSet() == true)
{
throw tc::Exception("A null Object was nulled by makeNull() but isSet() returned true");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
void Optional_TestClass::testMakeNullOnExistingOptional()
{
std::cout << "[tc::Optional] testMakeNullOnExistingOptional : " << std::flush;
try
{
{
int testInt = 42;
tc::Optional<int> foo(testInt);
foo.makeNull();
if (foo.isNull() == false)
{
throw tc::Exception("A set Object was nulled by makeNull(), but isNull() returned false");
}
if (foo.isSet() == true)
{
throw tc::Exception("A set Object was nulled by makeNull(), but isSet() returned true");
}
}
std::cout << "PASS" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "FAIL (" << e.what() << ")" << std::endl;
}
}
@@ -1,20 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/Optional.h>
class Optional_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void testDefaultConstructor();
void testWrapConstructor();
void testCopyConstructorFromNullOptional();
void testCopyConstructorFromExistingOptional();
void testWrapOperator();
void testCopyOperatorFromNullOptional();
void testCopyOperatorFromExistingOptional();
void testMakeNullOnNullOptional();
void testMakeNullOnExistingOptional();
};
@@ -1,245 +0,0 @@
#include "PbkdfUtil.h"
#include <tc/cli/FormatUtil.h>
void PbkdfUtil::generatePbkdf1TestVectors_Custom(std::vector<PbkdfUtil::TestVector>& test_list, PbkdfUtil::HashAlgo hash_algo)
{
std::array<PbkdfUtil::TestVector, 6> tests;
// test 0
tests[0].test_name = "test 1";
tests[0].in_password = "password";
tests[0].in_salt = "salt";
tests[0].in_rounds = 1;
// test 1
tests[1].test_name = "test 2";
tests[1].in_password = "password";
tests[1].in_salt = "salt";
tests[1].in_rounds = 2;
// test 2
tests[2].test_name = "test 3";
tests[2].in_password = "password";
tests[2].in_salt = "salt";
tests[2].in_rounds = 4096;
// test 3
tests[3].test_name = "test 4";
tests[3].in_password = "password";
tests[3].in_salt = "salt";
tests[3].in_rounds = 16777216;
// test 4
tests[4].test_name = "test 5";
tests[4].in_password = "passwordPASSWORDpassword";
tests[4].in_salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt";
tests[4].in_rounds = 4096;
// test 5
tests[5].test_name = "test 6";
tests[5].in_password = std::string("pass\0word", 9);
tests[5].in_salt = std::string("sa\0lt", 5);
tests[5].in_rounds = 4096;
if (hash_algo == HashAlgo::MD5)
{
tests[0].in_dk_len = 16;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("B305CADBB3BCE54F3AA59C64FEC00DEA");
tests[1].in_dk_len = 16;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("5B6DAD229782C6547D1B20D5668EB834");
tests[2].in_dk_len = 16;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("C8C1EA2F5E2357447AE9244725FAABB9");
tests[3].in_dk_len = 16;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("6BE21BD5D64E926E78F543DC119875E7");
tests[4].in_dk_len = 16;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("DE806293ACB904A2B63DE565C014A6C3");
tests[5].in_dk_len = 8;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("921F9BB3A34D998B");
}
else if (hash_algo == HashAlgo::SHA1)
{
tests[0].in_dk_len = 20;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("C88E9C67041A74E0357BEFDFF93F87DDE0904214");
tests[1].in_dk_len = 20;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("47E97E39E2B32B15EB9278E53F7BFCA57F8E6B2C");
tests[2].in_dk_len = 20;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("2DE33AD2137E4650EA29FB13136F967B0A4508D9");
tests[3].in_dk_len = 20;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("4287903B55F17B04B5D1E769A3AFB8290B9F3D50");
tests[4].in_dk_len = 20;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("EBABF2EF0353667E5581021DD84B1342B6BCC8FA");
tests[5].in_dk_len = 8;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("2B664B45A9A129DB");
}
else
{
// no case for provided hash algo
return;
}
// copy populated tests to output
for (size_t i = 0; i < tests.size(); i++)
test_list.push_back(tests[i]);
}
void PbkdfUtil::generatePbkdf2TestVectors_RFC6070(std::vector<PbkdfUtil::TestVector>& test_list, PbkdfUtil::HashAlgo hash_algo)
{
std::array<PbkdfUtil::TestVector, 6> tests;
// test 0
tests[0].test_name = "test 1";
tests[0].in_password = "password";
tests[0].in_salt = "salt";
tests[0].in_rounds = 1;
// test 1
tests[1].test_name = "test 2";
tests[1].in_password = "password";
tests[1].in_salt = "salt";
tests[1].in_rounds = 2;
// test 2
tests[2].test_name = "test 3";
tests[2].in_password = "password";
tests[2].in_salt = "salt";
tests[2].in_rounds = 4096;
// test 3
tests[3].test_name = "test 4";
tests[3].in_password = "password";
tests[3].in_salt = "salt";
tests[3].in_rounds = 16777216;
// test 4
tests[4].test_name = "test 5";
tests[4].in_password = "passwordPASSWORDpassword";
tests[4].in_salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt";
tests[4].in_rounds = 4096;
// test 5
tests[5].test_name = "test 6";
tests[5].in_password = std::string("pass\0word", 9);
tests[5].in_salt = std::string("sa\0lt", 5);
tests[5].in_rounds = 4096;
if (hash_algo == HashAlgo::SHA1)
{
tests[0].in_dk_len = 20;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("0c60c80f961f0e71f3a9b524af6012062fe037a6");
tests[1].in_dk_len = 20;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957");
tests[2].in_dk_len = 20;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("4b007901b765489abead49d926f721d065a429c1");
tests[3].in_dk_len = 20;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("eefe3d61cd4da4e4e9945b3d6ba2158c2634e984");
tests[4].in_dk_len = 25;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038");
tests[5].in_dk_len = 16;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("56fa6aa75548099dcc37d7f03425e0c3");
}
else if (hash_algo == HashAlgo::SHA224)
{
tests[0].in_dk_len = 28;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("3c198cbdb9464b7857966bd05b7bc92bc1cc4e6e63155d4e490557fd");
tests[1].in_dk_len = 28;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("93200ffa96c5776d38fa10abdf8f5bfc0054b9718513df472d2331d2");
tests[2].in_dk_len = 28;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("218c453bf90635bd0a21a75d172703ff6108ef603f65bb821aedade1");
tests[3].in_dk_len = 28;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("b49925184cb4b559f365e94fcafcd4cdb9f7aef4a8ca8fcb4bd1ec53");
tests[4].in_dk_len = 35;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("056c4ba438ded91fc14e0594e6f52b87e1f3690c0dc0fbc05784ed9a754ca780e6c017");
tests[5].in_dk_len = 16;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("9b4011b641f40a2a500a31d4a392d15c");
}
else if (hash_algo == HashAlgo::SHA256)
{
tests[0].in_dk_len = 32;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b");
tests[1].in_dk_len = 32;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43");
tests[2].in_dk_len = 32;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a");
tests[3].in_dk_len = 32;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("cf81c66fe8cfc04d1f31ecb65dab4089f7f179e89b3b0bcb17ad10e3ac6eba46");
tests[4].in_dk_len = 40;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9");
tests[5].in_dk_len = 16;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("89b69d0516f829893c696226650a8687");
}
else if (hash_algo == HashAlgo::SHA384)
{
tests[0].in_dk_len = 48;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("c0e14f06e49e32d73f9f52ddf1d0c5c7191609233631dadd76a567db42b78676b38fc800cc53ddb642f5c74442e62be4");
tests[1].in_dk_len = 48;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("54f775c6d790f21930459162fc535dbf04a939185127016a04176a0730c6f1f4fb48832ad1261baadd2cedd50814b1c8");
tests[2].in_dk_len = 48;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("559726be38db125bc85ed7895f6e3cf574c7a01c080c3447db1e8a76764deb3c307b94853fbe424f6488c5f4f1289626");
tests[3].in_dk_len = 48;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("a7fdb349ba2bfa6bf647bb0161bae1320df27e640a04e8f11148c81229e2131af179c3b3423b38abf763dfe208c1fb67");
tests[4].in_dk_len = 60;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("819143ad66df9a552559b9e131c52ae6c5c1b0eed18f4d283b8c5c9eaeb92b392c147cc2d2869d58ffe2f7da13d15f8d925721f0ed1afafa24480d55");
tests[5].in_dk_len = 16;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("a3f00ac8657e095f8e0823d232fc60b3");
}
else if (hash_algo == HashAlgo::SHA512)
{
tests[0].in_dk_len = 64;
tests[0].out_dk = tc::cli::FormatUtil::hexStringToBytes("867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce");
tests[1].in_dk_len = 64;
tests[1].out_dk = tc::cli::FormatUtil::hexStringToBytes("e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e");
tests[2].in_dk_len = 64;
tests[2].out_dk = tc::cli::FormatUtil::hexStringToBytes("d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5");
tests[3].in_dk_len = 64;
tests[3].out_dk = tc::cli::FormatUtil::hexStringToBytes("6180a3ceabab45cc3964112c811e0131bca93a35d17e833ebc221a40bd758ae8328802896e40a54d2be21c7dd8e665f6490abbbdff6c5590bc656c9d0b3dad2a");
tests[4].in_dk_len = 80;
tests[4].out_dk = tc::cli::FormatUtil::hexStringToBytes("8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b804f75bdd41494fa324cab24bcc680fb3");
tests[5].in_dk_len = 16;
tests[5].out_dk = tc::cli::FormatUtil::hexStringToBytes("9d9e9c4cd21fe4be24d5b8244c759665");
}
else
{
// no case for provided hash algo
return;
}
// copy populated tests to output
for (size_t i = 0; i < tests.size(); i++)
test_list.push_back(tests[i]);
}
@@ -1,34 +0,0 @@
#pragma once
#include <tc/types.h>
#include <tc/ByteData.h>
class PbkdfUtil
{
public:
struct TestVector
{
std::string test_name;
std::string in_password;
std::string in_salt;
size_t in_rounds;
size_t in_dk_len;
tc::ByteData out_dk;
};
enum HashAlgo
{
MD4,
MD5,
SHA1,
SHA224,
SHA256,
SHA384,
SHA512
};
static void generatePbkdf1TestVectors_Custom(std::vector<PbkdfUtil::TestVector>& test_list, PbkdfUtil::HashAlgo hash_algo);
/// Implementation of https://gist.github.com/jakcron/ab05a200e5a6b53c77f28ebfc6342885
static void generatePbkdf2TestVectors_RFC6070(std::vector<PbkdfUtil::TestVector>& test_list, PbkdfUtil::HashAlgo hash_algo);
};
@@ -1,172 +0,0 @@
#include "RsaOaepUtil.h"
#include <tc/cli/FormatUtil.h>
#include <tc/crypto/Sha1Generator.h>
#include <tc/crypto/Sha256Generator.h>
#include <tc/crypto/Sha512Generator.h>
void RsaOaepUtil::generateRsaOaepTestVectors_Custom(std::vector<RsaOaepUtil::TestVector>& test_list, size_t key_size, RsaOaepUtil::HashAlgo hash_algo)
{
std::array<RsaOaepUtil::TestVector, 4> tests;
// test 0
tests[0].test_name = "test 1 (small message, raw label)";
tests[0].label = tc::ByteData((const byte_t*)"This is my label", strlen("This is my label"));
tests[0].label_is_digested = false;
tests[0].dec_message = tc::cli::FormatUtil::hexStringToBytes("D1EFB44DD179C3691A74AE3AA46E2DC1");
// test 1
tests[1].test_name = "test 2 (larger message, raw label)";
tests[1].label = tests[0].label;
tests[1].label_is_digested = tests[0].label_is_digested;
tests[1].dec_message = tc::cli::FormatUtil::hexStringToBytes("12269A9630054F054E79A9BE10EC27DD4BED6A3435DE2B764BCDEDA173D14C16");
// test 2
tests[2].test_name = "test 2 (small message, digested label)";
tests[2].label = tc::ByteData((const byte_t*)"This is my label", strlen("This is my label"));
tests[2].label_is_digested = true;
tests[2].dec_message = tc::cli::FormatUtil::hexStringToBytes("D1EFB44DD179C3691A74AE3AA46E2DC1");
// test 3
tests[3].test_name = "test 3 (larger message, digested label)";
tests[3].label = tests[0].label;
tests[3].label_is_digested = tests[0].label_is_digested;
tests[3].dec_message = tc::cli::FormatUtil::hexStringToBytes("12269A9630054F054E79A9BE10EC27DD4BED6A3435DE2B764BCDEDA173D14C16");
// set keys
auto modulus = tc::ByteData();
auto privexp = tc::ByteData();
switch (key_size)
{
case (1024):
modulus = tc::cli::FormatUtil::hexStringToBytes("A2A451678C1DFDB0A505DEC45FD34815054AEF603D8169B79743D8562D7F197021BBE7C9D07F67590E8B6720A6DB8C04CC52EAB90554F8F5CE903A820F9C50F7C09265DEEB5F2BFD8FC19A0A4BB6CD52129393AE09459BE75A8FCDA7C74D1CAF6506D121AD13B3CC595F0382EF896FA7569C223D330C1B4B0B4ECF4ADE8B18D3");
privexp = tc::cli::FormatUtil::hexStringToBytes("1E397113501BA6B0740A623A96203A6E059CC65D5930BA87AEA9A20369D30BD425C0B8B36D76AFAB0223EFD7468AD83B70091CABA38D05F3101F0770721C37837731039681CD536307AD3232C753485CCA60FC243E9CB2A388A79F2E8EA6CC13B912BAD042DAE58C72516D0AFDDB73579EBF095874DF2CF33129AB7BEA0121B1");
break;
case (2048):
modulus = tc::cli::FormatUtil::hexStringToBytes("DEBE4D3F6AA0C4B0F7B90FB132FF8ABAA98DE69FBDE58AABF0494E6C7327859A5C3638DA1236B2C23D92175AD1A74C72E30AEB539CC947CBE5ECB080ED45391FFA3DB93227EFF30804660782CF56E740DA584F677D96714823C6B1C31E719A2D479314A1E7A493D72909043580058C3AE8A17187140D0A6B60054435A1CBC01BE7DF0991ABF254F37713BD93F02B9906610809C5BCB88883B91D0ED86BD2C68AF5661FAE8B9DBEB2C3D90AEE85BBA9A350D29E2E6188696ECCB465B2313DEE088F3352C4D94BA91B36F3FFF2F918292A6A1429CD22E5A317B0BE9684A8B25F55CA38F9BAE9A6EA3CB48F618A71547D832C2E9CBAC97D2769010F5CD03526352D");
privexp = tc::cli::FormatUtil::hexStringToBytes("268F784D05B70E45FAA4B178423565FD599404BC5BC20CA72662726EA8E2CB28C554E7B3ACDA8648C522F0E31A8F65573041F82A51E6B084B669AAC6AF0CC04E6E625818BC3C386D0761E863F763FA85CA26E69C2A6C2C714A2C4022E0B6D6F386C40A1ADB40AD0D5EFFBE184AF0EAED59CF751966D9B9178C986CCE0214054E1CC0801CF5AABDBE1C7DB8CE9B043AE024A18723E3AB7A68F8806993DD6B872A05BC827CC9CD33E77AEDF7B828A635EE9C99A525204FF09EB8F440825C73C072AAC2AE9882E1B29318434CD6B556A2753451173442BD65082DC4CBB9318F5A8B7FF9AF84CB9F030259CF84039F63675574C16A7027828C2C7845E1841DA63B6B");
break;
case (4096):
modulus = tc::cli::FormatUtil::hexStringToBytes("92654BE390E6DA06A826A8CA54760ED3C2D4F424318C4EA4C0741A98EF13B9DAC07F6BFB5FB8F24889B47556FCCF53492BBAC6F4E3BECEC4F906723ECBC36BAEAFB050DE30A393B7AAADAF45CE258CD1859A708DB22642AD0062EF65BCFACA3519516576A877C431417BA19EE7981BA99FE2818DEDD0A8508080AF9E79D88F1B1B90CE61F8B600D9A919B4B7B2083A88CA536CAF67C2C09A2533DEEA647BA9B5EFE8660E086DF20D6EC5AAA2B7A668B9E8E5D276AD5EE697DA95DDE79A8E53CB2B1DC28268D132B413516ACD191423AC76C7EAD37301411382FE5FB68D2096A74F0811148A87FB3C285893032736CBF5901220C7B7BE04A5E215B0F0FF8076059839EED2E492E86BDE7D6E5206A635B67A62CFF261BC686B45C7E02F485CF7BF8800027A830AE1195E3895EBBDA47612B5E42198BAB3CE3E4837FBD4DABAE199390C7BBBCB9420C2E5173AA21597CA3537B9CC5FAFBEE4D72CB232F8796B525F031B6E9F0F9CDBCDFAD839270ABF8935FED7BF53ACF841091F6B53FE545F251F4754CCE3BC0426828F7473607CBE14DACDED705B800FA93389463DAA23FE187329CB13F00849273797EE5D5936C1CB0336A571964897CAAACE540BE2910B0127F6A2B54708329A4465E4780AC0FAFD25CF6C90857ED01A18370B42CB37DEE3D07D2336F16CA537AFF2A3E312178E89ED5850551328E49294A122F2013C50E7BF");
privexp = tc::cli::FormatUtil::hexStringToBytes("2CE9E40300C13A91C143FF13F81EB244D8A8F1F02ABD63A15B2423C6D8CE81FE3181C654BC54E70C472734BAC7DC29AEB0BA6070E070794A682648A5A8691F9FDBE9E99D89699E17C2C6FF97987BDFBCA6533005E0EAA9191F9DBAD9C9455E05356BCA07C1FEE093C605D29B886D1BCB8A307953DC6AE040B67404AD47AF9F940EFC79BD080B7AAE4C9984DEB8C19A87BE1F23209B625E29CC9121EA6282A81A17ED02667AC29478F78BB062B49A5AD5F2B493C1F245C3D441ED29C3F5208667B6262EB748C629DAA2749FA225F80E4BCAB36201966E83932364BC63AADF9D28DE6FD8A1A730B9ED0669CA4CB4DAB46F75D081FB140DB9AA54F717AE908CCE684913E078F1FC71032C50CE3A4175DC1E7337C6BB1AF998EADB2713A8D6B3AA66ABBF8FDF9C2783A046C782576F60A7AAAB38E25253A9235C9C94FDAB51380A427C9B604F6373744D23D7213CF1ABC561F4914F7EBFE92EA354FDDA251ACC4F55FF55A9A1F2A0D63654F44CD3A0CD9DACE8B93F47EB542EF825590779F191628EC6FCDDCB419FD89CCBA16E1131E15E0AD730446F993AC9AF9283494A53074CC84BD856D47FA8A682A6728B9F1F60EF283DA4533501FA36C276F5E2438A7A675B871DE92E325DA4E5F0CC5795AE36743BDF209F08C47D91F85F0280988267C71FC9B05F5C34BC5BFAADB1B006C5E18DA769CDD6F88CB0AFFAB0F7CC8E027DF6B5");
break;
default:
return;
}
for (size_t i = 0; i < tests.size(); i++)
{
tests[i].key_modulus = modulus;
tests[i].key_private_exponent = privexp;
}
// digest labels if required
for (size_t i = 0; i < tests.size(); i++)
{
if (tests[i].label_is_digested)
{
if (hash_algo == SHA1)
{
auto hash = tc::ByteData(tc::crypto::Sha1Generator::kHashSize, false);
tc::crypto::GenerateSha1Hash(hash.data(), tests[i].label.data(), tests[i].label.size());
tests[i].label = hash;
}
else if (hash_algo == SHA256)
{
auto hash = tc::ByteData(tc::crypto::Sha256Generator::kHashSize, false);
tc::crypto::GenerateSha256Hash(hash.data(), tests[i].label.data(), tests[i].label.size());
tests[i].label = hash;
}
else if (hash_algo == SHA512)
{
auto hash = tc::ByteData(tc::crypto::Sha512Generator::kHashSize, false);
tc::crypto::GenerateSha512Hash(hash.data(), tests[i].label.data(), tests[i].label.size());
tests[i].label = hash;
}
else
{
// hash not supported
return;
}
}
}
// add expected results
if (key_size == 1024 && hash_algo == HashAlgo::SHA1)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("18C19B600B3A19FDD8FC3EFD89DCF02A93A38D49");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("7FBB8CA153F44DFF6E69595F1CC3E8F9092B17F44400A1375F074366269FE20601888FED57E2C7C0C441B79853F86D1364DF6A8595F3A6FC9AACEB2AF46B31FB836B9D1679FEF94981338AFEC51D409B0C9B68FB5F3316F46A99C4AF2BA200DD98AC86F8467C7227895ED160D493CAC24B692C2A82AE813F49E218E028B8C126");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("C276A7B930AD3D02B43A07288AC01DFE5BDFAAF5");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("0EE0051E3C92FE2D595E1C518B291760DF621E2DBC660833E821947017D4F878152F6752E86D266B3504172B076E0D5FB2830DE3C8B3DAA36B05ED59AF414E453D0B1B688C8BBA68F2F3E9683CA6FFEE8CC85C1886586DA02BF387B9FD646864E7FE10387781A6D3B720A7C74C3DF7290E0EDF76FB7316109587325B95676B85");
}
else if (key_size == 1024 && hash_algo == HashAlgo::SHA256)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("4379DBF9B508CBD9D2CDFD951907C09084E8C9ABEDBCEA96C60FF1AC1D9B1943");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("12120A3B1F1AB6A72B14B29CB220B9D042358865D423FB221D15698D987A387C015363DA1BCE6852F84987948B07FDBBC89AA87D70441EF8F339FAB5789EE277958445DB49148D6146D582255554C57B31CA9CBE010B54A76EAA7E9FBE67FEEE507198AD069B7B1BFFD487BAF6A66F4A2AF5FF975D55A00B98EFCBE7EF7D215D");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("6A8277479CE751792CCF0A8A462416CB0A35D4BFC0C61DB4B81E401FEF006AA1");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("6F8EBA9FFADCB912F9CE2CDD9CD8A9032736B431B631BB2E7F410250B5BFD19D3BB2EA19E89FAE8C0931DC94683FBC8349FF2EF2977550FB0029DC80D1199C3A35C7C04BF1AA8C51F70ADFB3E33FAE25B8FD808EB1D5F561A9FD06A08194A2746F5A24BEC4E925C0DDB2166E0F3E0078CFA0F5C4CE44D1BAAD3A05C85DC44411");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA1)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("8816C0CA54AAC6219B7ADB78A8CF401FA5ED00A0");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("B9C5CB733B672D15B91E182B79DE9A9790177A4CF3BB1EE02E3D983D62C0F602D9045D88F4C18232D340D93A9EFB50E8B3F5814D910D01DC61B07D722DCDD70B556A45DA3E7616E801734FB6C50123AE234AAB4C85730502A488E44724AF52C635D28A0F74374173B3F46F0622F2E1109BFCC1FC47480F1F9400D9EC6A407C3ECB581874578D1E9EAF0996D621E40606EAEE5E5EAC9ADCD2A932FA36C3B6CAA8DCFE6FDD057049927FF5FED90BA503D539085BFA6B743B4DD4E1D00FB0678859B29AD9804857C598946E3DF6DDC418620DE627B786DBDCEC704B7D658E1F1AAD42A0FD971C8F2DBE978D852179E8B687F88C3CCAB173A425757F210979EBA634");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("75D8F9CBA4B4BC0C36E203A09C16BFE02368CC89");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("877E2803AD28235772F383BC2A0B508CB6D881C8F0168160D64B435EDD9AD08D2F19E1AE8CCC10FC4112C519D4A40F22599654ABDF8FB1BE178C5C5186AF01A4611554B384B64124699800DF925D9A30331A4DD467D18F6CCF8D5E861604FA1B448894757254873B7AD48C1B5BA0B854368716923C7C67663F5A869D789B3E45ADBDB0EB01676BCC16DC5691C96B2C6BD59BA280F9BCB865E546E833248982A0BAC52ED479BFDBE2BF5F402DF900BA3E6517B00A9728F59121E509C6AF4FBD4EC3A35AA1285C45256D1A2E9433D8FA2F894542C52347A7A14A9E7EA867E0851E51D44B5C79131924F285B8FADE778C56CDBA64C21F0A78B47BEA996BA3C250D4");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA256)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("1933B327491AAD845661C64963A1E91FD820AF6E16D3B07B2965481C0FCF25FF");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("BC6678B5BC8460527C2A86A134C5E8657B4789B3FA4DAE988487907148739A5630F39A3FF8E94FF30331A02272FF2A6A59C3165DC2175BFDF9C77D4B4250FE614030B8AB7F3669CE8BB0A2F63E07CED087382E2C3A73B7A495184D407F7A3BDDF3F682BFE88B1981B0E31EDFB12556B7701BA49B8ACCCF57B48F6184F5255B5B237AD49C2863C70B07734B894A647B0683E187615C41B8AEF51FD0A21ADC4C5FB4FF087DE366C25ECCB9D0437793F4F7180AF9E8F8E9EFEC0E294E433BAD79D36511F876D657C8052DF2FC6182C30C80F9CEE1D9F160EE7850EB90D02DA4BABFEB3EAE272056D6E4424D617E41050259447D3F4A2BB559CB49BBBB509152A920");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("1F57E87A75EF286DDF3FC368C06B3022A0B1D70F852F0018E2159A68C25E20D2");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("64593270A26D75F88A13D88AF48E3DAA9342C8F3DBF030E6D3B61B0D20B30F9C33126E3EB3A13A94566897C74BFC2656617081BBFF5E358741FE6EA96A2912C4E5A0428860DF218BE77CAD183E8533226EEE8689289B89AFCCC9746F103567D050234F0060AF679292EE0E36BCF6BFA5F9F594284291991B08ED123E404F7413FD8B61393C5813A865DB1B40C010D240EFE6B0AFE7D2DF96A0DC863C0182605E93C5E6ABE2147B2D4B540B24D3E2993EA48C1D13B08B7E1CA8F14B351E9F0CD8EE31121002107649BBA9C58AD5382837640293A41AB02E835D51408C86C80CE2762054CA0CB005850025499F358E630C0D8394B089BDCFA2AA9103EF112F3C26");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA512)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("D5D1D6980B658CF66E5CC44F0C6CD59461F72D531C3DA02A2874CDEA653630412DC03CABEF601E2F2CC500D2E001539E0F355B534505AF6F1A899AB76849E6E1");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("04234CF8E2BB775CD69734F288B2EC5C3E0E77C9AF2CBEACF1A942FB8FB8703DE4462EA86C749C676646106B8D33D8443A1F3B3B60D771EAC7C28A47B633DB2D8643EA96C3D74E789AE17F52448369A7B62411312A469F22172A3E7E1F24C58D9BCABACD985EA51A9D8C44B36A221101B1FC5BBC64B1B1749912642A42582D1DFDD3C5D2821A4D4C0B9860BF5095AA01C8299CF1466FC761E6FFDD75657A37328A2DE0D6F96398EA26E6ED2853C575B1683CAD9DDA60D008D11A1CED17F7B30428953570D1B40CC3C55A3F4093CD66D5F56BCD39D39C218788AE550DAEC56990E81796608A01A6995E1B7A2FC8B3DE7970676CF2FCEF76E17852F973352A5F22");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("9A7689D4F8B664915AFF99C9B267FCC37C09368275AEA44BFA00446142180ABB65011A41D43491CFC2DA939FD211FA5A5B2FCFFE67F40AE11B8EC9836B0CE5BB");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("C0C45218128C97A70A8C966511021CE52447B236EDD0DDE17D711FD8B345E916E1F5D42BB56B86FC662BCF28C11FBA082359A81FE5C0608804E152D9F24C5FA4A86E3B0B23987B548183C16B91411F946B9C6DBB8FC5017E6D8C57F2F2CEA65CB5A71588DCAB1EBD5740A4135ADD276B74D51F57EC64C500FAF81CA22AF809D12B37AD04200BF95249809F36734B5BEC98680C3DB73DF1F4540234E035123420B3F67169B0C12286E23CFF54A7C1B90EC1C3443EA66E6ADF1E0F4BDB7D2C6F7F6668DD8E68D3F714B3ED963643DB61293C0D14C94FC57369BC50CD890EC76B72D1DA628D29CA8588F3727953D29D63BEB9A5A1CFADC24E3D9C1157F0E37CD487");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA1)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("144C65BD99D1660E0DF7F6552EC49A4F511A1324");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("0CAD479D4F74F63398F35D546A0E2D91ACD88DE5F539F5324C8ADBF9DC14FB78C79414FBC7F619277F246239DFA06C889BCD102588452B366B1724E41BE92B2DFB4635200B2C6263815894B91A912E7B7FE8B96858385B6E74F26DBC4ABF96D4A99710881706A3F4CDC9914C9646AEB005A6EC6A996424DBD853BCCF1CDFC96F8A67EC05B64F7C1C30199F9AA6519F0BF9E643153F98A1DC430EBD650FC1F435111DA7120E65F878661C96C2952A635010FC9B0EA2ED1373B0A88FE2FBDC24D28C54A2F30FEF0C064D7FC2D5F5AD1BC4B51CD82CC8B2614FFB3CFAC20176ADD9AD08AC23EF8C478C98D4A36F3715C68920D7424F8552765FFAE28AAEB043B1BB64F1F9C79E4535EB834898F9B71F6FFC3199D2FA3332B671AF8D4489019ED54AF781BA981AEC5131C8F287C8677CF7A9908CC7D56E688018E9B971310D480E32B4966C5FE53AB2AB074AE021EFA46A8D1AD6C2EB4CB50E71150BE14AC6EE61F9B846BCCB236A9B28A9F86C6D29144521D2B9129EEF424E640E7E8CD273F9782DDEEF660242D6768ED480FF17FA529C230DFAD91E272F5E4E0C6A42ACDFB65CCAFFA8E61393DE24F355296941EAE7DBF9869E2D65ED8120CF83C72AEF79CB9BB61581ED26F0029DF80557689114E963690D2AC77D0744754C93C815A81121667C4CC4E09380C0FB9184DEC8266CBDAB7C75BE5EFC2E36F9DEAFB32273438DF5D6");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("DD9D35B22A09EA34A759905960AED9F4081FE7F5");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("5308061312A823A1AEE48EF80B64C804C868416F87497D1F5607ADEB3F87F539022CC26AACF630955D11EEAC6F7D84FF46BAC539CA690A90D81162E8870946A62054FB22C25CCBE9BBED517F77C730A08C90893423169E95BCF379AC387AD96B21129FAA93DCFFE3C9915B12340DAB17F5D6FC81664F5438ED06144B93958FCC23FCAEA2F49296B3C1BDDE44FB1CC087B37DBB32323B8FBA0B6D6239743294114C894ED854929C796E48AB8B7DF4E1BD276BADC47803D9339EC12F974D4946326E11567A0A2C107D6453A071A765E179806961FE7BC611792038DC64086E6B9B23CAD997DD46F3D07B27C909F63528F58EA8327C32D7C6FE102E81581B10D5C4783F85F23645F44528A72565BEC696D8077514726321E80681885B4B774F214C98651F39CF9C410F6C0BAA2A72D822F7AA830E52BE11479948E8AFC3159ED5CD55ACB1E5E77F0BC0863BA1F0C4C15E610187265ABA88FEA24BAE1FA5A9C2A062C6CDA7E9D5E1FD05F68722C9D44AA82528AE307DEEDB613A09266E389FE8CB92080B09910DD3A0DA1F9D9846948558D209DA4AF33130BDA311347EE39A11283F1954DA00EC28238FAA8D28383843434F5178BA4CD11190175BCA1B226A2CBA373AF425E9DA594AF9475AB61AA0A7C6B7A059ED3C7DFB4588672E065BE56206B9B30D96BEE387E4CB1B3F8F3CD5AC01E9DE136C10D94BDEC912CD2788AF2EF22F");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA256)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("037DFA32F9D90A0DC9D43CDE371FF816388E3D027ABC6C33C3AD443A0F8DFDCF");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("5C478CB5E30CE0F96E9E98EDC8047A076667B18AEBFCEFA9A8CE6B69D97040DACB796BF12B01BF60E2819BEDE3C8AC6BF419C628BE5BBFE65783FE73C325B9FAF039CF58A444547A2509DC28D48B0A8E6B089614EFE5D13C77BFA74ECC4641BD432EE00A8149EDD8A7E2A3B98110C98B2D89505A4C5F93C2ACC1065DF1C55C7A907715D4CC8BBD9512BE079F82872B7DD56A3AE6D6ACFC751E8FC91986FC83C3452448DF04771293C3F15C57810310C95D6A901E2586A94CB66C9597804751D980561F88DBBA7AD20A0C34CAB9D94A666F2EBAD7B4C7F6A81415ABD5C7277A4313E0BA1CE6977D96A89E0A207E648A26AEEE208CD2000BA927982510521A59E574931431B74D43AEE19A2FD9C01E7B56FFAEB0A7063C776FC10728474827637CFDEB9639913B5CC4930649EE963A379F87818AAFFA96E6AC29B69B3057D55831DDABAFFC080C30997EE9D7F56C554F8D86214B4960DF342128D94B323118BA1F5C6BD472EFCD6A8B1921747BD0345292127B625E3001CFAB0AB000CFEB05593FEE3D59844BF172AD1E16FAD0DAD6612C40E799D32170427C7660AAB3C891A6CC32211DFA6798F372484E3F36AD41C9F000B04C3DAC3550B303B2EC2AB5C9AC4ACDABBF55B3C382F0F318BF9C09203787DBE2EE08CF1DB6F03AE07EED874F2E2BD114B4125BCCAD061A8E5BF6D3557DA6171150214633C0C832BD3C6DFC2985E9");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("B4C84542A6E383C520FA1B4E055D69CF0DB174F24011485571E684108EF9BECB");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("530B370F66765E2033AACFE6A114DAC84BD49FABFBBEC4A5202F2C1684693AFA3B145930B46514A03395CB978EA3BC81B89DDB200E223172FC1599E6E80A26C5393C0F04226D2DBB53DD1DC0B2A177C23D3BA7C5EBB0CBED10240FA4F2731BC7ED8696E1BD6CFA7B6FA7AE0BF85DE53EE98EC01805192BAAE933F234DBAF6D11E167C7781283D681CF31A9A0B202F75F06CFB3C7AAF7E8F313B890A3A83AB81FCD88AC09937EF5FBFDA50413DB347D58C425ABFECE21CCB5DB39A7022F2CE2F116B3B343FE32E70876F230F51F9A71C1200F0EA899C3968C19691E8A8322CB8BAEC66F7734AC309543951FBE80662C90D29C53217E78BFA470886570C970CDF1F43088D83DE1100DD0FB068A7C7A8E8CF39F84F11074FB89589B5F0517ED04B19E8651008F265982336D6D9A96CFD03BE909B5671659BADF447537461545156DD3058773D8F6C1840789451CC3419E3D18D199B0180F751BD6D0D9EE3687E467E7E5AB0F43437FB4F5E4CAA2E1DFFE3CEAC60DCD8229F131833CDB41E9B8BBDC517437BBBA43BCF411A69F9C6ADDD8C0386F9FA68009FC6041E8B91EEC00413FEF985CBAB6BB32A579A09170F48DB007A55BE8AACA65617DB3368421C163A9E9E78F561747D4BAAF64A68F3FE52B0061163340B9F007C46561502680874631E8AC2E39E645B732D263262F708A711E680277E0FCF5AEDAAAA25BF982DB4CC24E");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA512)
{
tests[0].enc_seed = tests[2].enc_seed = tc::cli::FormatUtil::hexStringToBytes("A8D827D8E8B0874B956D96C9647E76DF2F761C520EB28A548C651D917AB5A4FB6BC62F825C878F1C97E2FB2FF286DE827179A43302F2B573A64CD740B8AFC51F");
tests[0].enc_message = tests[2].enc_message = tc::cli::FormatUtil::hexStringToBytes("15D7A37165062CB21246F7A38A5C643C767BB1283BC07A396B6329D950B9FDDF3807590B0B9A42299D3F6C9453A0710D852E9AF99640B23D60BFEBC7DA6F091929D6B78F80DD0B8C676E69F6878501C6A9835077C17B9B3B4C9BB9D99E357B77879B2BBC5FA93F45ACC055F02A1A2777D1237E945EA166C105F3BEE662B967DBE24483CBD8FD118340197431A22682E47B4141E002A7E722B3798EA0C9B176041B9643AB1EAF312BD6946A09E6BC85E813FCF23B87A26BABDF31F88B91F62DFD4E6CDDA44E912CAD400332F24E7438472B39A3C1AF5D0A142ED80777678B3B4E21B8359AB61911075C009D23EF344F2FBFC9C6B5FEFF596F852CAF4BA5F5A28E6AF7349BB59720F7FAD094AD9132600FD761B4B6E3348D992EBB82DD7693D285F769036527152B0242D1F188B3AA1C626385AB7D24AC1667829063E7C84F6BB4E415164B2B45D3DF06F48A77E60D97B366C21C2AC8CABA5847375746AB56E44BC6F7BADCA2CC7AEA4F40F0D990563EE0F0930410E6CE07FC27EECC717141EF201BAE05DE92C8D998D8283E3A5929DCB40BF3226C560F6CD9BCE9EA3E8B45CE3A4A1C21B972335E06CFEA83728BB3D75F1FD848B520F353C992E7B4E4EDA943A24E52BB50B6F346140033ADF0C4B2E580C98380F7D78FA22BCA1FC43140B6DC67B1DC5075E21BF30B1BC9066BF091706BB49028DEBCE9C744AA15079184B97393");
tests[1].enc_seed = tests[3].enc_seed = tc::cli::FormatUtil::hexStringToBytes("BEA7FA2E2C9E9BDE240B7DDABDC083517E2CD57AFB2FEA999A3958B753D1C777DA278845ADD1EFEC446BD50417D4E9ABEBEDC28362DD7E661BF350D0E3805359");
tests[1].enc_message = tests[3].enc_message = tc::cli::FormatUtil::hexStringToBytes("558B1A6987E63E9A7FD97EB81F34E52BBB417690B417986CF75AAE18EE3A309B58FA1E4A2F8B1C1A1ED849AEA2F67CA56CD503FB4863B65267F8D6A62E440B0EA800F983D572232ED132539FA43BECF4E903A6795AC7CF462F44D19017D7393B877CA846704D0CCA1F5AF6574673E4DF374F1C572938DC69B9846E1CB066EF2D3820D26BAE89576ED7B21BE33AD27A2A30465823F70DE3D55E0A08D25E60E723C152E7A612CCF8A9A1185530A102AA43174DF3E3D900D559DBF15E4A36360C22B4550A1DE49D7105F64B02069118CB49C460AEF0EC4A063ACDB01FC83CA3F6796FA9DB0E58C435030499E70910512EBC4B73D6608B932C0DB3939476B3E0CDD7EE81B3890F9F2B0F4096A58954143773DFF1E97D27EDA1F53AE42E57FE28013AB8925E060B1B9F12D0E33706F1D3C711910A398EDA1E4D1C4A99DF96C706480C1A653A12D2F488629DDEFD1C3BA4EB6BBEA60E0CB28BCE769066DED683663D754476794A5F3F9D81E10A16CACE1BCAE7439785941868DF656830ECF62D367E46F9BF1C5C3FFC760C8FFD302D55A9E134401B4C55FE08F9509419022F05014CA30D75FCFCA33C882B803C38AC6E0A1CF07D6E9B27D1BAC7C4454D6DE03EFB0A3B46034D45AF9EDB7CC5C0914C04BAB2D252144947DCECAD197FE8BCA15CB0EF5D3A0621AD7824C65144B25759C1B9AFBFA0E808CBF0C613F1D79F3347048506ED");
}
else
{
// no case for provided key size and hash
return;
}
// copy populated tests to output
for (size_t i = 0; i < tests.size(); i++)
test_list.push_back(tests[i]);
}
@@ -1,34 +0,0 @@
#pragma once
#include <tc/types.h>
#include <tc/ByteData.h>
class RsaOaepUtil
{
public:
struct TestVector
{
std::string test_name;
tc::ByteData key_modulus;
tc::ByteData key_private_exponent;
tc::ByteData label;
bool label_is_digested;
tc::ByteData dec_message;
tc::ByteData enc_seed;
tc::ByteData enc_message;
};
enum HashAlgo
{
MD4,
MD5,
SHA1,
SHA224,
SHA256,
SHA384,
SHA512
};
/// Implementation of https://gist.github.com/jakcron/1c00ed37089743e38df46da2d9ccf6a0
static void generateRsaOaepTestVectors_Custom(std::vector<RsaOaepUtil::TestVector>& test_list, size_t key_size, RsaOaepUtil::HashAlgo hash_algo);
};
@@ -1,146 +0,0 @@
#include "RsaPkcs1Util.h"
#include <tc/cli/FormatUtil.h>
void RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(std::vector<RsaPkcs1Util::TestVector>& test_list, size_t key_size, RsaPkcs1Util::HashAlgo hash_algo)
{
std::array<RsaPkcs1Util::TestVector, 2> tests;
// test 0
tests[0].test_name = "test 1";
// test 1
tests[1].test_name = "test 2";
// set keys
auto modulus = tc::ByteData();
auto privexp = tc::ByteData();
switch (key_size)
{
case (1024):
modulus = tc::cli::FormatUtil::hexStringToBytes("A2A451678C1DFDB0A505DEC45FD34815054AEF603D8169B79743D8562D7F197021BBE7C9D07F67590E8B6720A6DB8C04CC52EAB90554F8F5CE903A820F9C50F7C09265DEEB5F2BFD8FC19A0A4BB6CD52129393AE09459BE75A8FCDA7C74D1CAF6506D121AD13B3CC595F0382EF896FA7569C223D330C1B4B0B4ECF4ADE8B18D3");
privexp = tc::cli::FormatUtil::hexStringToBytes("1E397113501BA6B0740A623A96203A6E059CC65D5930BA87AEA9A20369D30BD425C0B8B36D76AFAB0223EFD7468AD83B70091CABA38D05F3101F0770721C37837731039681CD536307AD3232C753485CCA60FC243E9CB2A388A79F2E8EA6CC13B912BAD042DAE58C72516D0AFDDB73579EBF095874DF2CF33129AB7BEA0121B1");
break;
case (2048):
modulus = tc::cli::FormatUtil::hexStringToBytes("DEBE4D3F6AA0C4B0F7B90FB132FF8ABAA98DE69FBDE58AABF0494E6C7327859A5C3638DA1236B2C23D92175AD1A74C72E30AEB539CC947CBE5ECB080ED45391FFA3DB93227EFF30804660782CF56E740DA584F677D96714823C6B1C31E719A2D479314A1E7A493D72909043580058C3AE8A17187140D0A6B60054435A1CBC01BE7DF0991ABF254F37713BD93F02B9906610809C5BCB88883B91D0ED86BD2C68AF5661FAE8B9DBEB2C3D90AEE85BBA9A350D29E2E6188696ECCB465B2313DEE088F3352C4D94BA91B36F3FFF2F918292A6A1429CD22E5A317B0BE9684A8B25F55CA38F9BAE9A6EA3CB48F618A71547D832C2E9CBAC97D2769010F5CD03526352D");
privexp = tc::cli::FormatUtil::hexStringToBytes("268F784D05B70E45FAA4B178423565FD599404BC5BC20CA72662726EA8E2CB28C554E7B3ACDA8648C522F0E31A8F65573041F82A51E6B084B669AAC6AF0CC04E6E625818BC3C386D0761E863F763FA85CA26E69C2A6C2C714A2C4022E0B6D6F386C40A1ADB40AD0D5EFFBE184AF0EAED59CF751966D9B9178C986CCE0214054E1CC0801CF5AABDBE1C7DB8CE9B043AE024A18723E3AB7A68F8806993DD6B872A05BC827CC9CD33E77AEDF7B828A635EE9C99A525204FF09EB8F440825C73C072AAC2AE9882E1B29318434CD6B556A2753451173442BD65082DC4CBB9318F5A8B7FF9AF84CB9F030259CF84039F63675574C16A7027828C2C7845E1841DA63B6B");
break;
case (4096):
modulus = tc::cli::FormatUtil::hexStringToBytes("92654BE390E6DA06A826A8CA54760ED3C2D4F424318C4EA4C0741A98EF13B9DAC07F6BFB5FB8F24889B47556FCCF53492BBAC6F4E3BECEC4F906723ECBC36BAEAFB050DE30A393B7AAADAF45CE258CD1859A708DB22642AD0062EF65BCFACA3519516576A877C431417BA19EE7981BA99FE2818DEDD0A8508080AF9E79D88F1B1B90CE61F8B600D9A919B4B7B2083A88CA536CAF67C2C09A2533DEEA647BA9B5EFE8660E086DF20D6EC5AAA2B7A668B9E8E5D276AD5EE697DA95DDE79A8E53CB2B1DC28268D132B413516ACD191423AC76C7EAD37301411382FE5FB68D2096A74F0811148A87FB3C285893032736CBF5901220C7B7BE04A5E215B0F0FF8076059839EED2E492E86BDE7D6E5206A635B67A62CFF261BC686B45C7E02F485CF7BF8800027A830AE1195E3895EBBDA47612B5E42198BAB3CE3E4837FBD4DABAE199390C7BBBCB9420C2E5173AA21597CA3537B9CC5FAFBEE4D72CB232F8796B525F031B6E9F0F9CDBCDFAD839270ABF8935FED7BF53ACF841091F6B53FE545F251F4754CCE3BC0426828F7473607CBE14DACDED705B800FA93389463DAA23FE187329CB13F00849273797EE5D5936C1CB0336A571964897CAAACE540BE2910B0127F6A2B54708329A4465E4780AC0FAFD25CF6C90857ED01A18370B42CB37DEE3D07D2336F16CA537AFF2A3E312178E89ED5850551328E49294A122F2013C50E7BF");
privexp = tc::cli::FormatUtil::hexStringToBytes("2CE9E40300C13A91C143FF13F81EB244D8A8F1F02ABD63A15B2423C6D8CE81FE3181C654BC54E70C472734BAC7DC29AEB0BA6070E070794A682648A5A8691F9FDBE9E99D89699E17C2C6FF97987BDFBCA6533005E0EAA9191F9DBAD9C9455E05356BCA07C1FEE093C605D29B886D1BCB8A307953DC6AE040B67404AD47AF9F940EFC79BD080B7AAE4C9984DEB8C19A87BE1F23209B625E29CC9121EA6282A81A17ED02667AC29478F78BB062B49A5AD5F2B493C1F245C3D441ED29C3F5208667B6262EB748C629DAA2749FA225F80E4BCAB36201966E83932364BC63AADF9D28DE6FD8A1A730B9ED0669CA4CB4DAB46F75D081FB140DB9AA54F717AE908CCE684913E078F1FC71032C50CE3A4175DC1E7337C6BB1AF998EADB2713A8D6B3AA66ABBF8FDF9C2783A046C782576F60A7AAAB38E25253A9235C9C94FDAB51380A427C9B604F6373744D23D7213CF1ABC561F4914F7EBFE92EA354FDDA251ACC4F55FF55A9A1F2A0D63654F44CD3A0CD9DACE8B93F47EB542EF825590779F191628EC6FCDDCB419FD89CCBA16E1131E15E0AD730446F993AC9AF9283494A53074CC84BD856D47FA8A682A6728B9F1F60EF283DA4533501FA36C276F5E2438A7A675B871DE92E325DA4E5F0CC5795AE36743BDF209F08C47D91F85F0280988267C71FC9B05F5C34BC5BFAADB1B006C5E18DA769CDD6F88CB0AFFAB0F7CC8E027DF6B5");
break;
default:
return;
}
for (size_t i = 0; i < tests.size(); i++)
{
tests[i].key_modulus = modulus;
tests[i].key_private_exponent = privexp;
}
if (key_size == 1024 && hash_algo == HashAlgo::MD5)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("3E4A712262786316D5ABC63F40EA0976");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("5CCE86EDF8FBA6D576993977112EDD63D06355D0EEE4B01CBC8083D0B42D0E156BE700123E608AEF90B21F5E06D22DA279E96417B237A03661484BEEAE368ABD60DC41480F6C632C16B423C131FE4299628132AAD5A521DDB8BEC714E8E47E2894A659CAB34449D97CE78B65AF5AF45C373A3EB5BEC3A96AADDE24A826712F39");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("EAB56CCFFE46084FA63084A9AC1DC4AA");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("38F9035CF80DDA81C16473EA9DB8ECDD788EB278804AFE4678ACE0B2C59BBE6E1E982D16FF2A8A168CDCF4E3B1E828EFCB72E66FC31753D9532D8B49EA79B9272FFC4F21CFBA6ECBACBA169E2E65E22E6B50E72D184949E9F32B59E2418F3FCAD6DD321D582C76B38C317CD630D5BCF42EA5CFF2505D50D9AA04A46ACC5EC19C");
}
else if (key_size == 1024 && hash_algo == HashAlgo::SHA1)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("0B54234C25D22747C81C7365B04996AB3D96EBEF");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("308D0D22C57A94A1BD04E80081B1B908FFA1ED9255F834DF2A80BB45DABDB796297A18FDE3A5E9B8EDF9A7DED6DDF59A6D038D484368CB90C0A3B96B1FE675FF725F8793C2BA4AEA559E89331924CD8A3ACC6348B0533B1B8A6566780083F9C881E9E3205D5875045FEC4C906C078899E6E31D9BB715760DF779883691583DF2");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("62737496A3A99DB6808296117E3CE796201C84B7");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("96DDC708CBEE1CB1EABFCEA4A1A5D7DE8FBE75A84C8C6CE9D11586AEB082B5B95A4FE0CF58712595ACDF45CEC5C6BF4FD680C46A3AB0B4F1B239F0D56AF97F2771490AB43FE6F46513E3EE8F396E6C8A2C32FC307C05C6827C060AE8E52F5B72B609E64B861648A328509E86AC79AD64380BFB2675183CC8CEAD5267724F2B7A");
}
else if (key_size == 1024 && hash_algo == HashAlgo::SHA256)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("A2386D7FF4EF41BAD87D62CA8BAA1563AAFDF8013ED6B3668BFC1E7616D86CAC");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("6F00ABCD0C69C4BF0020B6078EC24AF0393D8271ADCABB16B05520F687DC5FE6C56C0090C5D042296608D655471A189254731B5D5193519B5C57CB323008EC2F4158740D1FB3A95DA2EE26483487D070498BB6D1580E11FBE046D750B66C649A7D5BEC483F73249F85D7DA175CE0ECA6BFC09EF9F1C2FA16CF72E062703979D2");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("0D9E93AE17D944B14BE6F382A64341AFE7B33BE21774654ECF9FB8D3CE036E86");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("365D0E0A3286DD6FF0E6DF0EE04883D73C618A120B4A2BF6275B1B30FEDD17D7F6F1DFCF97CF3CF459FDB7344646BF81FD71EEB45958AA7FE380EE3891CFC445FBEB59AFA1D9209886DE74E2399D9F3D9E2F80369B9FF56EF88BB23AFE986C421D57413B4B74D0E85DB9D203D62D4CA15CCF50AE6A32B451E0EBDF85CA9EEA74");
}
else if (key_size == 1024 && hash_algo == HashAlgo::SHA512)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("1F5E7340F40C9B424E8112451133D56DC4EF2221083C96CDC4669BD4AE328659D4872AB16B4720EAEE67637EED14A637664AA92831DC0DE43AC659BB738FBD99");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("8F5AF83160E8B124712FFE5348853B5349BB54CE37E728A63B93A462E38147EF7EB3CA76DFEFF58C55FEC597F1A37A727705367872CA9FB92072717781052AFC8F110AA90C34049DD9A0FB51AFC6F519FA345C7B97B81A6886BF0D71187FEF049AACCA3F9C38F849C5353F6E4EA8DE84250B03AFA21569EC2A731A82A4224F48");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("709BA2DD10CFD1ED5D3B2833B6B42664ED9684DFA239520D842598A75BED777BB7D5D71E7D7A5E5658C925AB96C8ED99CFAE1E46A33C4736655CB3BC3EB23B6B");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("A01D15754D220D55B7597FD233235C425DDF5D141F38A7A4AA2A733788CB366384C3D621DC3BCADEBD85B7556D916D541672255FF634883447D49E84A73F2FE8AC67CD6E95BF3B2F4ED577F96EFBC5071ACF7E7E5F88236C553F3A74E98E2A3CA5CD0748B08E928902253E719A74FEC41A78916536F1144678DC4AC51DB19394");
}
else if (key_size == 2048 && hash_algo == HashAlgo::MD5)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("3E4A712262786316D5ABC63F40EA0976");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("1370B59A62B15002D3A0838AF12086214D7D1DC34ACB5618A46C0049D7C711AD23AC80D3A031A83C67E0787A41869850CD5BE423DD57FF549CB0E9AC168659714E57A4F07DE72E594E76810F1023D0D98A6EE1513F9208A873AE3DF484F81D47B78B6F06B5321A6ED394CEA43F8FC247A627F25A9186B6CB7BFE4030F3056CC4110E3116F17EB15650A1181EF8BEAFA00F9E943EFD173BE88A07729CA95A2EDEAA90C5D5EDB86C444F72B29F723FB955DAF507466E291BD78341455745C5B953C10DA75204FE4513AFA9B22A559A58C4433C9E375C185388ED553FC71369E1EAC86F50B0F1CC0C5917AB6A59BE9AFEB9A9A86B3EAF5E5092686EC4DEFF6B1C6B");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("EAB56CCFFE46084FA63084A9AC1DC4AA");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("559B0D9B41CC78F333671AE0F026DD3D4B001AF4A4B41508FE9F21B76C609577F5CFC243F66508BFAF688DD8AEAB45F34D0A51412F988F96347A22F5A6E20AE0455160B32EAD093070163AE148D3185D798A97C69AB3945B58079B52D11F1B96CC80DCDD82C2DE6805AE3CCE45CFCFE23F72E6F72F73D3E5BCFB44D58A9C835320C49DE66B41F1C2E528CCE6E152401AF060AAF6694206350595BF3EDAA938CFF9CBC190B4DC38DA7E7752651835A2B80839915CDDB50A740B613FB536DD8C09B08E05359F6B6C4714F1EED7784D6200B0CA66907278CB24C2ED0DFC772684CDED2B331E2B8B83DE9E774A80D7E52053DC122EDEE0F3392FFC27B60BBCC1985E");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA1)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("0B54234C25D22747C81C7365B04996AB3D96EBEF");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("2AA9197BE6C552A0AE8435BDFDA57D53AF126A77ABFE7BA3C3D746BBD1A56ABF83A95D0E827A355B3C9578006442D4A1498E7DFFAC387B8BFB2FD5C265B78F0E325B591AFC8E403EFD679B9665FA6871A8BEF0619062CC37E30BA656D8D63FF3F3BC2F7E52C565EF13017EA2AB7801E31367528714C3B421E260C8CACF7D6FB0A3E0A43BA56C1B4E21708FB2E4E5074B214B918B36BCCD9EB3E8E246CBC6DB3DF0CFBF24D29EA79FF77EB7B50700D02AD81C3D45E222B5C7189B9AC6028182C57C7519BA2BE360EBC1B5BF9654B40A0357B47F8DCA967339E0D8C426200A6F5CEADA3A484C9E9FFCDB7C3558214E3732835A4ACD40558942AA1F83F0FA62E7E7");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("62737496A3A99DB6808296117E3CE796201C84B7");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("A4D943211BCF11B6B900043D699DAB1391343C39B41369D4F103B0C5174A1CD4DD42D78CA5A9237EA8C8AAB4B26E6434F414AECFDF1FCB047B105096132F786023359FF1423C5F87AF6E2B513F3B8AF2FFE12AA5ED1999BB0C953C3EF2608A347676F76097092ADC59B3117C67D14AC7ECC5BCFA4587A6D651CE8AD0DCACF990727E32A3D2DBED1E52C6D5D96AA97276C2020B0C0928D3EE3E7D7E285BEDF781BD878B8468447E9C5DC0343837B5A1B83FBCB6747F536D41CFA6718162392F557BC5224357C170EA384502BC608F33EF9F5DFE42B12652F132C4C28142184D10B3D5CFAA43FAB01B63A761A571630BCBF8CD439AF11CD83EAE416D4892AB2640");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA256)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("A2386D7FF4EF41BAD87D62CA8BAA1563AAFDF8013ED6B3668BFC1E7616D86CAC");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("460AA83AABDCD786A4C20DFA4822E4BAAD19B1B51B51595789483A4597819A8DD7AD78F2C4587F8881A3F507A32C4E421420361FCEB617313092506C084C12153683073F637BA1517D52F157178C8CE8161C73DC017FE03282F170E5B6DABF2E5B58D7C5603786165B3889B103C9C1EB66D91EFD9B6A1708BFBE1B5D4BF5CE2EF0DDFE3AF1B52B4A120D9E89A15623E1E579A562F9FA342D41BB25F438BF4934C3DE592A79A3E0ABA0A181B6C402C10E0395CEA8D68D16CB916A66F18D16D7C3534E95D97E2501CE232A1FCAF4C09D8E7314A0E80C8D5867469F2730C894909197EDB3EA5D701160305CF6B335EDE647797C9278BB5D15CF8B48905FB469750F");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("0D9E93AE17D944B14BE6F382A64341AFE7B33BE21774654ECF9FB8D3CE036E86");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("76EAD23F3FA6392A8301B3AC8DD4DC28046BB4FD67E2FB53C00CE689202D67CE38BF64FFF9771EBEB306F4CF679FFFD6CCE39CE4252D827A965C2EAB6A44848AAB087535BD46858AF516632F8D82F8E98125BC59971369855490264226F286436746DFAEF3843710189E47AA95E478F626BF11E26A61BDAF726B5F44106EE290BA5A5551063623B276D504D5F391591303735BE8C5570E03A3A60FEE9816436AB99A3232A946E58E2CBC46887B5C382FFC00F09FC5FDEE8F27A0575F36F9146156FDF322359B06BFC5859585E978EE01508054557B2D2A213E53C813519B707A9D832A50B576D5918BA80C266A1FF4A88E6712835F9E74DF9E5B906E2B2249BE");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA512)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("1F5E7340F40C9B424E8112451133D56DC4EF2221083C96CDC4669BD4AE328659D4872AB16B4720EAEE67637EED14A637664AA92831DC0DE43AC659BB738FBD99");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("916B2800B5E76111A2F12A935789CA36B1B5313BA0C0222355F5DC328C609930B6643DD3518C74DDFB5DD9720AA1762664EA82FD393A0DCFA80AF1757D4EDA56CCC0342E6A2878EA6D5502F34719D5371D9E297529DF4770A899B11B687342C8F213B89980247A724CD14489FF1BD627DD30614CEF8F733460AFB82EB1FC6FF9B256129C79B7313D015F1F414EE56CDB566AB54B7F769EA8C90D894D773E07073243DDED1E9FA64D1965B9C4F0AF815158FA85DA19BD35CE1FAE654A669B39E99853F4F2F15DB70949055F48320AA00838CB541A27E7871FFCBE449D4E64160BE0F6C48193997F1EEFAE0E62D978965B4460135DFBEBE692FA643764CD8F8E27");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("709BA2DD10CFD1ED5D3B2833B6B42664ED9684DFA239520D842598A75BED777BB7D5D71E7D7A5E5658C925AB96C8ED99CFAE1E46A33C4736655CB3BC3EB23B6B");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("BE89EDCE1642A8470F14D27B418BB4CFD3284129A42B596DFB05DFDC5F99FF2572C40513B19019B01D34465A22DEA89952D138C99E588ABEDDC2E4211F944CBEF7A9546541DAD747AA7E0024C8AB9E620D570383145D3BFE4D622AA8D6670E51B314DAE52BCFD1A8BF1F93FE8909318C7D1491E9013AAE223243248AA4DCCFA0DDCB6A6F9AABDC74F2395B666C85398628006D2CFC737DC1D566C6155FD6F84558FC543BBFA1256F8F91E0E69C5BD7C91619DA522F9A5E14A00E42981526D09B9FBFCECED41AC7023C7AA04E7942F67E446D35FD63F5A70D9C8CF30BD01ECAF5C311EBDD1A1B06BCA3C4261E8EB06E9583E18F5359683292D2ACB018E0082F61");
}
else if (key_size == 4096 && hash_algo == HashAlgo::MD5)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("3E4A712262786316D5ABC63F40EA0976");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("53131DE9F31347199696486F46169DAFA4F3825816392183F8BB43DA51559D8AED477B3042CBD6DAE7A6931CEF96DA86EEF198AE5FA799F0BEFC18D0F12A8EAF1B7F19C348BA3857257A708CAC6536145BBEE4E19C2EC2C313F8D2CB7DC9E9CB54F7A246E8FAF9E22C2AB9EAC8CAE57BD72D6E5C852F7A0631544AEF7B98E9C7579DF79D97858B72D123557A58D1AE32C1D4AB7CBD0DA99AD05423310DAAABF33CBF6CB7689AE28E9800AFAC0860AAB72294D71F67E40D905FF99EAEA405340862E3B81FBB0746B0EB8598DED8EABF05872C98C6AE1010C4DDDB1A3510C84BA595290E808200E7956820E55E472675B8C116A63CBDD46CCB917AC29BC8A3AB227A2D9F772F0110DD4D9ED1138D60729F5C5F8ABF165537ABC467B5CE93C2C9AA80833A0557F35B5C19B422524DCD0B592C16B9ACAC5F94C0211B92989E940FF115C81FD36B77E094A00E9193081BBA2148980EF29B8D104D5019CFE32A65DBC533B6F9E6365001A8104328317F249BFBABCC10B67F1CAF08718BC8AF4EFA914DDFC76F366649AC2E633C45F0C789EF6271FD2F27E32E4BB9C5B8078FA3A2F5E5CAC5C250E86C5D63D6FAE354CEDF7E351B213D9EC7583B14D7C689B92E52C0E94CAA586B6C9DE961CB2B64A027E3E4D39F2FC94B2CE03B29E3C7611ED4CEF865E1715EB8304A0F573B7DC991EBD086C73CD321F31A5B6395F5894904CF6F553F");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("EAB56CCFFE46084FA63084A9AC1DC4AA");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("07446D0F629CDF6078ABC1949186BA6E0C788866DE37322821289D4AB50588104C02BA49A35CCC8BE8D72364BC167D4372E7AC4D4DC121F0D09CD5F21E981BBB3FE3CEC3C36DF07B2FDC57ADCF538CDE093D69B63AA0F3F95DFB69BF35C68F83B090AB00831AE0E18E79389FB5BF3B52BE69FB497EDB8921B3E57BB999FF9475B4CE7CC8CC3C7FAAB793F09DBFE0A6C617E0E6359EBD6C6B425B1906BA032FBF814BC53596AC769057B7DC9FB77B3B0427955DD77469F6F210031562A949BE6413E1F482690A2C57845655786CFD21B0E2A0035B31FE63DD559188C992CC96DE0353AD105473B2FEADF37C86628E4A3BEAB051B3BA7FAA195EBC4EA8FB17EEF7E8A668CDC62C32B58E63AC60EC1CBDDA9485EFACC5F66888B66D4F35BB162EAC3CBAC38EFA9714CE3506CF50115F4E5F15034CCEFE554B03D112B9977CC472CF10F6B1635FED82874ED34B45D2BD4DB9A244C75408DC0B8A9EEFEA5AE5E26AB355F0556DDEC255D65807FDEFE57C2818E790B796B78A5D24D7444CD8356BC0E0139D2FDC18BFB0FB278D6197E052F4BDFCC454D72BDDF6828BC92826D95FF232F4399404F1486212509372EBC7B07529B97372AEACADB3179D34FC33344208A9043846F1EA498BC17E12FC0946DE474F080D46ED393B5F725B1DD707F95E1BA2E1B70837382EFC2232B881F9938230F2B320AD3F023540356B57AFC7A56E0958");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA1)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("0B54234C25D22747C81C7365B04996AB3D96EBEF");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("226D22515C2AD353AD9B44B20807FBDC18CCB36562FB18A3EE5FB83DC6060697913A06F8A26F8B262AB41E901F69D55B9F22FAE9CF94CA0A7CCB3FC93F36E564223DE09ABA33E1B069C10F26CD866DAF333376E680AF79DF470E5C3CC5ED640EF14279F8D044418126211D1FE26D1418743B549E1E9C74114BF20112B975668FCFC65771B84D604AB0C579DA70BF22522EACD831E1C1D85435FF811478FE95D6BE21E6D0A06E52756BDFA8AEC17D101E1B6FC72FF53E99F1FDD81050509C24DF36D850AAACB555AC43064F3A2424729E90001F29D83F9718A1E4914F9E88C47C2F14038A373251F810D34E504024DD86D44733B94F76D611B921C7B40F40610D0DCAB144CC3775D8D954658530894B925F12E80A5452E6D3FEEEB6F5FD607768D31CAC837A075AB31FBE2D185C36FEBA09C25016315FE8037C7B9A9B46FCB53593C64186690E1A38F6CD7D79D998EA54D372B74DBB0D28F60A23431A4162B38120E799BA0E1D92775FDA9B086F00266F7FB21318B8F8598BF1EF15B2BC21A27C5D2964501C0EED94883CAA842CB653467978560E5F46F3EE2F66F4F0680E0D9E220E532090F100785FB83D13EEA4D8041326C853C60489BD795F2E7AD22DDB01A0E6680DCD90FB76964E9B1D98B92C26B355C194C85A29D1BEC8C31321602C4CD2D4058530C8BEDD1B3D76EADF3379F4C70BD1B48607DED271EF11C970642F7F");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("62737496A3A99DB6808296117E3CE796201C84B7");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("5C525B4BDCDDDFE7A7FB890B80A4AAEC1E633C398B1BFE468908453F53E9B81381D0F5E52BD5C17158BEAFDB43672D3DF993847947CE42F3EF8BC72F38A57139F2F8FBB905E26AE6DD848A6C3BFEC35312C64D514ABCB3E28F811ECF5BE8BE8CF62A922B4A2D6D199AA6A3063B6E7DCCE4043840EDABBED252B2DF3644C7AC25E49481F44240F2001271E80ED77682EDCACE9372D144D66A149877DEC969F5A1E02872580B6265ED3ED9F015862805486739FE0162871D4EBEAE10D61A79C1FDB3C5521CA057994CC2444934E24B2232C67B6A3DCEAD846448E14BFAEED7B8F4914F27E1334953746C0AC18EB7CC4577E07B783629C9BD3F10ED91AC895196149DBAD7558EC5229D6BE4D6C5B9B0B8FBAF7E8A57563FBB8567A4E62B212401DC2757F5AAC08EA59924E8C2D475C4B26E8359C8B520315DD31053BF11DD46B1CF3F5F803F8209880D49740A2ADA07452DA58E088D353965206B6F1E429B0E3BBEEF1DF2CA08EFDBC9A84B805F1855608B4724247E0084060A99EDB9EB5FA85471E253E5DCDF83F761AE9597FB6C47B72FBC9EB78C6306B983FE3DC02566059A3784AF222891AEDBF11E906CC6AB7F72E6E194F4393BDF3D76B8674E522844FEB750C629A3BFA2E15D92C2C58B43925C28E70199CB0745C4BEFF0D6EC18A8CB15B740A61B65EE1D8B2204D176AB1A01BCA50F535E8230B742725DA19FC59FF5EE0");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA256)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("A2386D7FF4EF41BAD87D62CA8BAA1563AAFDF8013ED6B3668BFC1E7616D86CAC");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("29E3EF33332E80A21255159572A6BC3A85037B2FD471E5A259DE625FF8286523974B3FA170D23D54EE5E28FF26FCACB95EF6BF1682EECF1D298C55561A8B7D6DD51D0492E80E0F11C7FE3874049B8DDB6969387752465EE4CF988F709CB8E29CB2DED29BEA6F47D92C15AD7C01127217E34FF252753CABD1392D0FD4C6E55109CE512BEEB459EEC4E6BB69A5CA46B1C2B5691169408EB5C9E939D458371E674FD7C57E10042E01F283C4B63B7DCEAED1E5FAB696A2E24D5D5C3FA2885CCC456A93D4018EDCD0383B0B6EA6D5E047C200EB27535439B869EF925BF0D2396DE54D32E17492A8576519878E417EC09518428049072DC623CCD9B78EEC42BEFA9CE9B18DDD6AD3E0429D791B00588DA25327942FE9AD26C83B9699B161270B0927629716087F13D15D5FE56FAF8D615BC27403D8C3C8C2C1F2751074181D55556AE1D2B38E80589541B7CFE70B1B8B8261A9FC4BF6BD7CE0CA174F972959A24FFD07A6CDF2C2D80C7C128CFF722967DAF1F2A2D29596B92F56D2C65EB1A40E5DED0A8C78ACCEFE52133D08EF8FDC9E6600D67983B29059AEEB76B3449D7A099C2DEBD161F61A80F6F3B6DA8FF569A7C3990FA037BF222480F3727345AAB268C3B0D77BEABD2A8426FAE5B630C577B79586B5CFA3FBD9C4BACA9E5B22846C9A6DBCFA21C3EEC162420DF28184C2234A930FAE41B5B2718CE9C8F89F5F02ECF2FCC61E");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("0D9E93AE17D944B14BE6F382A64341AFE7B33BE21774654ECF9FB8D3CE036E86");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("6240DA2CD63671FC6C1E78E6BB5225CC27798B456A5082FE8E8F832CF8934E90F8CA83A03159589589943B9CF2BA46CE8B5509FDB9A3FB28B884D9A5358490CF3D52765F3E398366F768EBB9178E4B99C7A8D194D98CB4E502CBF28657D0E2D1DA26B80CDDFC8B9A7CAC7D51A9A97CA4B39C5464F0A073FE06431E21B1FD496BBC62D7B18DA03284AAB56FCFBD37ECBF8B86CD2C9C1670A1682CF6A448DB02B2941E9CF212219AE0DCF325E333A2F9895B686D61E192973BFEE96CB7876CA542EFED0FE9E5F2B2680822E0E4B72243EF19615EB50D7E7C66992924F15D44B8869776B1D397A104EEDC67B8C6E4F11576430861614B4658C5A4FE853D19B79EAEE18105C6583EF4CFBCE54260F0C8FDCBD2BE71DBE1126C63433741294A844D9FE39BF2189BD7DE4000073F1B3A98FD2215A49E4768A3384CBD589B01E3AD790C6CEE4462A36AA98E5ED03807021E63A841ADF159FCC4CCDA3CD19CFA21CBE5B73A81C8F1EF70E49D3D4807F5A80847ADFD86EA0D7B4699D25CDB5300EA241C6C2AEF2B7BA4A3B02A2C21FBDA0EA8D052CC8ACE8856312B443E86E913C1474D83AB3FF6CC7AB5F8137B4B710943741715634A3E85693636570E13D8433A292281AE0B29ADFE16FA6347CB1AE7F30047B4A95C7BDBB1CB3B4A599A41162E932EEC261B3E022FAA9E554BE84156721C009C987BC6982807DFA97DD7E1E7E1F1B1FC");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA512)
{
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("1F5E7340F40C9B424E8112451133D56DC4EF2221083C96CDC4669BD4AE328659D4872AB16B4720EAEE67637EED14A637664AA92831DC0DE43AC659BB738FBD99");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("0264109A40938A354BD4D7BCB81FA4ADCCF4DFCDF575F6D30A9EA6A9361C5EBD0A5B9F51E553B9663629113A4E46A907A8A9C03C4A3AA6D4921E879F48D9E02D03AD5FA9F94411EE0BF5568317ECB008C6CCCAAF75B7C8F06A98E0F19BED2178A82263A2DB2BF37FBB8A539246B58686A27FEB0F77619A00B6C49739BFE00901D140CE49218C6B24803A9923C86971E516595901690572E29D4CC10E7D7D72A2726F41C116C3916DA41051C69315CD0EEA8A15F811762EBEE2EEC410D57BE31850C8B6C27C8ED8A768CA89A4DF44FD644D25C1789A5D339CD59397F3EA3464271737AB5E2A6BE47F4C2A8AADB298D8472566335ABD8BF093C72F059E2AFE2D5076ED0B26F935EA910C07D6B6050F0F4D19E3F9F300445EAB5BB264FF51CFF219C78F102E182D261227C280DCEAC646D55ED0C0EBFBF2F63091E3E5DE476BEB27F107B697A7EC9D01CF242A6465AEA164A073727D2BDCEA2D0889E6C547491A036AF7E095FF74E6F267E3533DE0131B391CD0985C095493CE7BC7A69D2D27201D078E0C4041D345A699E1E047B0CF3FED6AAC6CC58EA64752DF2AA6F5325CC3B5D9A9DB337E118B397EDE56EB0B922D6B980D48A63D3AD596AE9F496A3DAD44428E4CAEA3C818DBF672AE6FF72D27621C3600BA41926C9680BB1AA8A9524BCCDAEBDF90B5C094F92393590795F81AB40A5449A64B59BE3C372F738AE8795F3687");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("709BA2DD10CFD1ED5D3B2833B6B42664ED9684DFA239520D842598A75BED777BB7D5D71E7D7A5E5658C925AB96C8ED99CFAE1E46A33C4736655CB3BC3EB23B6B");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("2023C38CCB2F4C29342321ADCC88E6C2DC2E5B1BF3BB1451FCAD87D3D2891C01FB14E412DA3E9064F33321DB0CD72C222060CAF65D99E0CC9C7A2E3431896EA07C0FFEC73928E1213A8828BBAFEB50F2E89B469C17BCB021CED7DE4FB556201D00EB8A30B6E6874EDACC9239EFFB06961126B22D4E598BCC712F99A49E1EF010C1D1762C77BFE911F7CF74279A5C1DB183C82F5673C264D388322E0B3F176F65F875956560FBBD7B293638661FE2E7DF26351D55CD64BC58853D33A712B6A9AE810C24CCF5DE1F86FE2636B22AC1A035A8B129A61B5AD962F7A90D5CFDE669497EE0CA4D1762A7278BB34286982BE170E595D05DACBC18FD0CEED89FAF30EFBBAE164456E310AA0A9E4A4D46A3E6643A06F38848C3D57B31F94E8B9832783010B1845469756C8A1C84B0A05A06A2B202C94EDCA4A4B20B019D070274C286268AFD56F14E195A3A437CF6B4677853CFC16DC4CDAEBE9389ED03E55750B1547700552C8D45A0BEBA2BDC4DB1DD5D308F94EDB40E8735BF57018285D54F9CF7D3D2BF9C915CDDD5A6BE3867F82597ACE6881EEA1ED4D68C806497D622261ED5E35B2D21067A5DE25FCA8437E265B57BBB10D03C9E49B5E5CC9EA81418C92AD1395F051FD1939499DB617B21587D6976255A178B98674AC565CAA99C775B002B6361985B3A4C302EB875494F9C1C9FC4D60932B1279B31F58D78A633F69561810DAB");
}
else
{
// no case for provided key size and hash
return;
}
// copy populated tests to output
for (size_t i = 0; i < tests.size(); i++)
test_list.push_back(tests[i]);
}
@@ -1,31 +0,0 @@
#pragma once
#include <tc/types.h>
#include <tc/ByteData.h>
class RsaPkcs1Util
{
public:
struct TestVector
{
std::string test_name;
tc::ByteData key_modulus;
tc::ByteData key_private_exponent;
tc::ByteData message_digest;
tc::ByteData signature;
};
enum HashAlgo
{
MD4,
MD5,
SHA1,
SHA224,
SHA256,
SHA384,
SHA512
};
/// Implementation of https://gist.github.com/jakcron/1c00ed37089743e38df46da2d9ccf6a0
static void generateRsaPkcs1TestVectors_Custom(std::vector<RsaPkcs1Util::TestVector>& test_list, size_t key_size, RsaPkcs1Util::HashAlgo hash_algo);
};
@@ -1,143 +0,0 @@
#include "RsaPssUtil.h"
#include <tc/cli/FormatUtil.h>
void RsaPssUtil::generateRsaPssTestVectors_Custom(std::vector<RsaPssUtil::TestVector>& test_list, size_t key_size, RsaPssUtil::HashAlgo hash_algo)
{
std::array<RsaPssUtil::TestVector, 2> tests;
// test 0
tests[0].test_name = "test 1";
// test 1
tests[1].test_name = "test 2";
// set keys
auto modulus = tc::ByteData();
auto privexp = tc::ByteData();
switch (key_size)
{
case (1024):
modulus = tc::cli::FormatUtil::hexStringToBytes("A2A451678C1DFDB0A505DEC45FD34815054AEF603D8169B79743D8562D7F197021BBE7C9D07F67590E8B6720A6DB8C04CC52EAB90554F8F5CE903A820F9C50F7C09265DEEB5F2BFD8FC19A0A4BB6CD52129393AE09459BE75A8FCDA7C74D1CAF6506D121AD13B3CC595F0382EF896FA7569C223D330C1B4B0B4ECF4ADE8B18D3");
privexp = tc::cli::FormatUtil::hexStringToBytes("1E397113501BA6B0740A623A96203A6E059CC65D5930BA87AEA9A20369D30BD425C0B8B36D76AFAB0223EFD7468AD83B70091CABA38D05F3101F0770721C37837731039681CD536307AD3232C753485CCA60FC243E9CB2A388A79F2E8EA6CC13B912BAD042DAE58C72516D0AFDDB73579EBF095874DF2CF33129AB7BEA0121B1");
break;
case (2048):
modulus = tc::cli::FormatUtil::hexStringToBytes("DEBE4D3F6AA0C4B0F7B90FB132FF8ABAA98DE69FBDE58AABF0494E6C7327859A5C3638DA1236B2C23D92175AD1A74C72E30AEB539CC947CBE5ECB080ED45391FFA3DB93227EFF30804660782CF56E740DA584F677D96714823C6B1C31E719A2D479314A1E7A493D72909043580058C3AE8A17187140D0A6B60054435A1CBC01BE7DF0991ABF254F37713BD93F02B9906610809C5BCB88883B91D0ED86BD2C68AF5661FAE8B9DBEB2C3D90AEE85BBA9A350D29E2E6188696ECCB465B2313DEE088F3352C4D94BA91B36F3FFF2F918292A6A1429CD22E5A317B0BE9684A8B25F55CA38F9BAE9A6EA3CB48F618A71547D832C2E9CBAC97D2769010F5CD03526352D");
privexp = tc::cli::FormatUtil::hexStringToBytes("268F784D05B70E45FAA4B178423565FD599404BC5BC20CA72662726EA8E2CB28C554E7B3ACDA8648C522F0E31A8F65573041F82A51E6B084B669AAC6AF0CC04E6E625818BC3C386D0761E863F763FA85CA26E69C2A6C2C714A2C4022E0B6D6F386C40A1ADB40AD0D5EFFBE184AF0EAED59CF751966D9B9178C986CCE0214054E1CC0801CF5AABDBE1C7DB8CE9B043AE024A18723E3AB7A68F8806993DD6B872A05BC827CC9CD33E77AEDF7B828A635EE9C99A525204FF09EB8F440825C73C072AAC2AE9882E1B29318434CD6B556A2753451173442BD65082DC4CBB9318F5A8B7FF9AF84CB9F030259CF84039F63675574C16A7027828C2C7845E1841DA63B6B");
break;
case (4096):
modulus = tc::cli::FormatUtil::hexStringToBytes("92654BE390E6DA06A826A8CA54760ED3C2D4F424318C4EA4C0741A98EF13B9DAC07F6BFB5FB8F24889B47556FCCF53492BBAC6F4E3BECEC4F906723ECBC36BAEAFB050DE30A393B7AAADAF45CE258CD1859A708DB22642AD0062EF65BCFACA3519516576A877C431417BA19EE7981BA99FE2818DEDD0A8508080AF9E79D88F1B1B90CE61F8B600D9A919B4B7B2083A88CA536CAF67C2C09A2533DEEA647BA9B5EFE8660E086DF20D6EC5AAA2B7A668B9E8E5D276AD5EE697DA95DDE79A8E53CB2B1DC28268D132B413516ACD191423AC76C7EAD37301411382FE5FB68D2096A74F0811148A87FB3C285893032736CBF5901220C7B7BE04A5E215B0F0FF8076059839EED2E492E86BDE7D6E5206A635B67A62CFF261BC686B45C7E02F485CF7BF8800027A830AE1195E3895EBBDA47612B5E42198BAB3CE3E4837FBD4DABAE199390C7BBBCB9420C2E5173AA21597CA3537B9CC5FAFBEE4D72CB232F8796B525F031B6E9F0F9CDBCDFAD839270ABF8935FED7BF53ACF841091F6B53FE545F251F4754CCE3BC0426828F7473607CBE14DACDED705B800FA93389463DAA23FE187329CB13F00849273797EE5D5936C1CB0336A571964897CAAACE540BE2910B0127F6A2B54708329A4465E4780AC0FAFD25CF6C90857ED01A18370B42CB37DEE3D07D2336F16CA537AFF2A3E312178E89ED5850551328E49294A122F2013C50E7BF");
privexp = tc::cli::FormatUtil::hexStringToBytes("2CE9E40300C13A91C143FF13F81EB244D8A8F1F02ABD63A15B2423C6D8CE81FE3181C654BC54E70C472734BAC7DC29AEB0BA6070E070794A682648A5A8691F9FDBE9E99D89699E17C2C6FF97987BDFBCA6533005E0EAA9191F9DBAD9C9455E05356BCA07C1FEE093C605D29B886D1BCB8A307953DC6AE040B67404AD47AF9F940EFC79BD080B7AAE4C9984DEB8C19A87BE1F23209B625E29CC9121EA6282A81A17ED02667AC29478F78BB062B49A5AD5F2B493C1F245C3D441ED29C3F5208667B6262EB748C629DAA2749FA225F80E4BCAB36201966E83932364BC63AADF9D28DE6FD8A1A730B9ED0669CA4CB4DAB46F75D081FB140DB9AA54F717AE908CCE684913E078F1FC71032C50CE3A4175DC1E7337C6BB1AF998EADB2713A8D6B3AA66ABBF8FDF9C2783A046C782576F60A7AAAB38E25253A9235C9C94FDAB51380A427C9B604F6373744D23D7213CF1ABC561F4914F7EBFE92EA354FDDA251ACC4F55FF55A9A1F2A0D63654F44CD3A0CD9DACE8B93F47EB542EF825590779F191628EC6FCDDCB419FD89CCBA16E1131E15E0AD730446F993AC9AF9283494A53074CC84BD856D47FA8A682A6728B9F1F60EF283DA4533501FA36C276F5E2438A7A675B871DE92E325DA4E5F0CC5795AE36743BDF209F08C47D91F85F0280988267C71FC9B05F5C34BC5BFAADB1B006C5E18DA769CDD6F88CB0AFFAB0F7CC8E027DF6B5");
break;
default:
return;
}
for (size_t i = 0; i < tests.size(); i++)
{
tests[i].key_modulus = modulus;
tests[i].key_private_exponent = privexp;
}
if (key_size == 1024 && hash_algo == HashAlgo::SHA1)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("6AD3C408F3565C97F0BEC62FF728921967287AB8");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("0B54234C25D22747C81C7365B04996AB3D96EBEF");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("88FF134C0A0DA7FE0DEBF63C60D62DA524E98B73D8E905178258D7B645AC4CF5615165465C053A2596DD189F1DB4B7686BC36AA3F39AE67B19D3D36B64F96E7B45E0D2B4F36064D2E8EAD04CBF3F4153A0E630F4610973F625F9911A29F6C629062173810272B4B1C6C256757779C4DEE495306D798B61DACC2CB34A662368BB");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("6575F1C16518DA498E3DFD4BC06F6E30C91E5CD4");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("62737496A3A99DB6808296117E3CE796201C84B7");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("049979E126E716538234962160FCED53A9B43A2AC2404679A1C3738E111F47488F881DA8A1A9BE25C7A8322EE40E19841FB0D38915300A4CF32EBA383ECA99A47DE95F12F3495F052B2EB575DC3D78FA6FB64EB986D7DDBB9C41CA4BD558543CA249D2A34A1EABA1A6584896C6C1F5D2E717BFECD1E172E34AFF25980841996B");
}
else if (key_size == 1024 && hash_algo == HashAlgo::SHA256)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("EF01E25B33F6BF37B0BA88893672F640E200A2F6D2107AACCA4708F8891D682B");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("A2386D7FF4EF41BAD87D62CA8BAA1563AAFDF8013ED6B3668BFC1E7616D86CAC");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("8267259B830A93F5C2D711FF005F7B46C0078E4F5558BC85FB4FB2517CECAD518B8AFC387BD9050A21397E456815A46ADEAC23F636C648B65943B51DF316DEA4C30B9F255801FFBB3EEA660DF7317F5F010FA0F3437ECC1F0140BEE08A50263588523C05F248E30FF14BEAF73E74842BECFB458FE892D833D341711D24F7FB86");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("927EF7CC082FC43B7B012C38FC602369F67C8B770DDD3F4025FDC448601EE397");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("0D9E93AE17D944B14BE6F382A64341AFE7B33BE21774654ECF9FB8D3CE036E86");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("05DBFEE535D4C2613B838D70DD6E571F221C3CDF5ED6BF7B6DCD875DBB46731A14DDE3681DE4F9A90662154607EEF50E181EDB19AD08648678FC853FBF7053F237E3BD544BF6D0D1C6A87CAEEE3E00D39484BF2BA69F83300582C879F547B7B67DD6DBAE0D7D16FBC4BCC21E0DB001DC4C0395FEAFA89C1B9B36A5CD80E5F2DD");
}
else if (key_size == 1024 && hash_algo == HashAlgo::SHA512)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("1990E6A72D59A4A855B9DBA69590B6225624C1353ED09AB5669346A0F3E51F760F1FEAE0A291912C7D83A3410375CE4AF7BEEBB6C7C0EFB4CEE507317B68");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("1F5E7340F40C9B424E8112451133D56DC4EF2221083C96CDC4669BD4AE328659D4872AB16B4720EAEE67637EED14A637664AA92831DC0DE43AC659BB738FBD99");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("3C36282DDA02032D6BFD6856F950EAF0ECEA968A78CAB93E8A0DA0ED6CEF2A63EF2FB4A32CB100FD5FA4B9BEC648CE518DD6F65562E0C356C1D0E5F4B179032DD775DD18F0D598232B33E96B6E59E1453F3345EB3C9228C0CBFBD56FE4B6317C46ABF00976EE212257A39483373D5466BE9594FFD3FA8AA4421ACEA7D35CCDB2");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("7C69FE4F0010847B11BD0B309A13DD8708F731B36B5913A2982055E89A48D58BBB42645D4CEB47B9D27BA0B138D0F5ED648EF11608C33AA1F3511C4D366A");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("709BA2DD10CFD1ED5D3B2833B6B42664ED9684DFA239520D842598A75BED777BB7D5D71E7D7A5E5658C925AB96C8ED99CFAE1E46A33C4736655CB3BC3EB23B6B");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("9E2573124931E89DD9F4CCF040B46BFDAA397C9C7C7273B1E74AF978E48EC28FB5639257416A790AA07090B63887F4934A4ED646754576784A4C11EEBD9C361CDC960FB1C8C026E1C18367357ECEA2C29319CCF8B93F0B13E5AC7BE26B038031CCFF7C1938BF56D59C6FA0D7B59FA14C421BECB04146EDD9B2EC5C003137B5FF");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA1)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("01B1B2F300B75AEB4163217DACB60F64E34C4B66");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("0B54234C25D22747C81C7365B04996AB3D96EBEF");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("355C0CC734D72EEC991BE4ED7B01756180EAA2A99BAEE6658EFFA644429D2707723B5C01119751C5C6B23A42BB344D7FA14F385816C5C17369C002859BAAC2A5775D45E005F613BBFFDD0FAD4018DBCACE2811B5BAAFC5F99A4C4279FBAC661661488F0B78C2A70C1235D75DC276838561136EFE2D22C0277D718AC6F12EF7C7B79658B5EB591F5B0BDC8C4FB27A6824C50EA4FF19E570B09BB2B56865421653C678DEC6E9215B7FD3C0D8EF95745C34DA1CFFE388B56D6CDE27D106DD12C8746DD2230B37FA0C31782C37DB4ABE84132E2AE3D0F76D3EEA34AC5E160C4F61D280B73989929FDEEA78FB1809FD9EBA4B5AEF48B0F15DDF06BB0F49EF68863688");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("77133B22F3404DF71350374D6FBEF778FDCF8B7F");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("62737496A3A99DB6808296117E3CE796201C84B7");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("09D88EEF86C014F0B0E200FC6C018FA02E5DD0576DEFF828617A1F97B5B7CAA39A8B4F3E8248B13FD3FDC4638629585040E1F54D49690092FC36A857FE64DD4093CFF44F658A6183AAEFAE6D4442DD5199AF15DB791DE3B33BE7C3C3D04ECEAF7F8A085885C1EB71AB05D268354A8E9228128048B3B33A49C8BA5704DDC2CCEFB47EC553990B3978C1D06FFD741153341739E2CC39633AE9883236E5D2AAD0D3A84D345C80A2A474700A4E7831EDEBC50C055FD97698C3F489A0D679173D96C57BDC6B43B21160759DB80CA90EEE0752FD1B756796FB32E03B0CFB81B9E8014BBBA64E4A6BEB1592D734962587C7315499BB8148EE73D48C8015E52A7FFB7DA1");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA256)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("F8975F369ACC0AA1DBFC213BADBBE48DC706872349A69D361F13602868314443");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("A2386D7FF4EF41BAD87D62CA8BAA1563AAFDF8013ED6B3668BFC1E7616D86CAC");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("355C0CC734D72EEC991BE4ED7B01756180EAA2A99BAEE6658EFFA644429D2707723B5C01119751C5C6B23A42BB344D7FA14F385816C5C17369C002859BAAC2A5775D45E005F613BBFFDD0FAD4018DBCACE2811B5BAAFC5F99A4C4279FBAC661661488F0B78C2A70C1235D75DC276838561136EFE2D22C0277D718AC6F12EF7C7B79658B5EB591F5B0BDC8C4FB27A6824C50EA4FF19E570B09BB2B56865421653C678DEC6E9215B7FD3C0D8EF95745C34DA1CFFE388B56D6CDE27D106DD12C8746DD2230B37FA0C31782C37DB4ABE84132E2AE3D0F76D3EEA34AC5E160C4F61D280B73989929FDEEA78FB1809FD9EBA4B5AEF48B0F15DDF06BB0F49EF68863688");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("ACF998221C624F853BC3A3FC4EDCF25B4B5B637A18DE521443EAB1D58BBE27E8");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("0D9E93AE17D944B14BE6F382A64341AFE7B33BE21774654ECF9FB8D3CE036E86");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("09D88EEF86C014F0B0E200FC6C018FA02E5DD0576DEFF828617A1F97B5B7CAA39A8B4F3E8248B13FD3FDC4638629585040E1F54D49690092FC36A857FE64DD4093CFF44F658A6183AAEFAE6D4442DD5199AF15DB791DE3B33BE7C3C3D04ECEAF7F8A085885C1EB71AB05D268354A8E9228128048B3B33A49C8BA5704DDC2CCEFB47EC553990B3978C1D06FFD741153341739E2CC39633AE9883236E5D2AAD0D3A84D345C80A2A474700A4E7831EDEBC50C055FD97698C3F489A0D679173D96C57BDC6B43B21160759DB80CA90EEE0752FD1B756796FB32E03B0CFB81B9E8014BBBA64E4A6BEB1592D734962587C7315499BB8148EE73D48C8015E52A7FFB7DA1");
}
else if (key_size == 2048 && hash_algo == HashAlgo::SHA512)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("EA98D82C923787FDC2CF52A1F4F33921CF6DC0F5740693CAB2EA3A37FA908B377C6FFA7EAA744486836EE6757307260612E2BE53F196D6D35A7300EB4DE63171");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("1F5E7340F40C9B424E8112451133D56DC4EF2221083C96CDC4669BD4AE328659D4872AB16B4720EAEE67637EED14A637664AA92831DC0DE43AC659BB738FBD99");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("C526E0FADBE7F4DB02E45F67914C0E1C121C5EC576AA691E2DB5DE1035A092E079DF434F0F49F2FE8D9D4E0C10EB188DC5F6F555CD122AD9E0C98D2FF5FD7C0B0F6DEB93E1752ED90BFC731ACB41FF5E1274B735FB5A0DB381968B3DEA21BB22055D0D67B78CBFEF1A3ADA77F2B8CA991786CC576CD1880D5824E1C1A15AB4F8FED7F1959A6C707448F776B3E4D9E53090B15241475AF1F16A2D5279982F1C25F2C0B056BEFE8A695C4FDB105FCFC54F5F2F4D4F028DB070468A20276BBA262A6CBE4F20723CA548FB4DA07E3E008B5BF71654E59D4F24C985B5EAAF93FF72A1CDC207457AF88FC1905EB05628E88859CEE615CA01F9275423E634B27F045CB9");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("068D7064B137ECEC7F6C3580900B64E857865E3ED20F5DF7CC56189330B18F5EFBC3A713A0BF74264BAC125353A3A0F6AA38EFCE65EE5CF1638A61A1B5565BCB");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("709BA2DD10CFD1ED5D3B2833B6B42664ED9684DFA239520D842598A75BED777BB7D5D71E7D7A5E5658C925AB96C8ED99CFAE1E46A33C4736655CB3BC3EB23B6B");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("07E9A6F65AC5F39CFB3F74FC3A5F95938FDCD0AF55EF501C9592B12551B9DE394B88E18ACC48453D64E9CB3C079332F4AB150D954945AE52652EC221738A333AEEAEB480198DC2473A1113F490E72714BF2F0AE38EB45EEF2D37F005747C7B45962BF803718004D150CA497F472BA497FC79105E61C512FA77F2D54D3085583BEC9406DE5DB94AF05C426CD147FB8EF67020C6B30E06F2B1044B2362958F12933E19E8AD718422E89F0054D1868428B3493966671B951949099F70FF8B11494E385E274B75BD666F7AC64A0BC5D05AC7BF5EDDD5CC635FFC125B92FD07BF20916CB83DC09B71469C2363D80AA4A407BAA4AB38909DFDE9A5D5C12B2EC040DE5F");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA1)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("C139B807BEB43849D4ECFC284C94900AFAB597B4");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("0B54234C25D22747C81C7365B04996AB3D96EBEF");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("294BBB3537C59CD4222FCF7D3DE244F0DABCA547CBEFEFB4C6275439E48A73F48A4BD4DFCB4E176C4448C44C95AA0915488F5C286CF558BA3916E45CBD785F6798A658977418BDD9241D55EE633A95B34A876A60663CCC71ACDA935A0CA97DFB3B3545AC49AF432C26CE9765E0952BE1F9042F97441019572ADA4A17AE45A44ADFF3FD385A762886092A066DBEA60ACBCC86070C261A89FB59D6533E14B8CB2BB7F33E01DF511661066EFAD3FA1FD3DC2C3B79878B18BF863B8CC5423D4A18CF273396190B92E181B8A63B6114C09453AD9D827CF0B142D476120FED5A8705C1FAF7667D794849670832B85AEC87ACD9841E8E7224668081587602287BE31863A93BD151C0D40D949B40CAB8F748D962FE84419C7D1770B3754B313657136251DBAD597E3E6BA61764A3F22372EC6114083081916C7AD2EBC0970CBFC264509BFE3A02A07EE03BFA3CAFB7643937EEA7B247A4F9EAF47DF13BB8DDDD635B26F7AD1339008145DD5C18381813E2108D84707D8A8EA5E8D2A7F7675FB6BC28452A4AF33D4A21F53CC1C08458220026311D8E8D2AFA37D4C4E2981164F84D33B04B99D1F18C060A0C9067F854721F4DA4DB2D6C5AFB7FDA354598071D579AEBCF5F12FE1322FBAEC6CA57556C3490634D4D4BAA7908B573444AC689C21ADD10478D8A09410D2EB1892A0A74D63F617A9185321A06345655AABD0EA04E0BBDA7C579");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("34E011F064EF219D0A01EF20BE64FDC2EE945799");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("62737496A3A99DB6808296117E3CE796201C84B7");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("33C06415E53760E9812EABD077C9E50323DDC367E8361D4600030A34402CBAF53F4EAFAB0874A85EA5FE0EE6E0F56B71B87BCB838491AF04DEA64A0FFB2AEE5E49F777C364FB82C3FADEC94A396ADB67E207E6E5F2DBF9792F7036B37FD11095A293EF97BEA35AE8AB6FF5ADF631FCA1A8003BBF65F8F0B1E7756FA12792FDCC1F273C4AD0B36FBD6DFADF91C4C764AA3D9B2D374F646E7380C5919D9F245499CFB8BDE069844B9086D0E97B16E792519FFC41BBE657DC1A4888F1707C2E7A4EDFAA1F7BD0BA13E8164B75D7F180306462457AAD787396FD0DAB3CAD3BAEB974EA856B87D06E7B1B4C977DDC65A95EC483835F2F6BE0413DBDA0CCC78D5ABBE4FBD87CF5E5D7F8D5386C1E6ADCAB0EC277244C117BFC35B37CFFD41F08434A64AB862677351A6A27224D848C397EC83ED0BC6DDC4D5D2AD800311AFA5C2AB5E45AC6E6F2E99EE7538D4125AE389873EB2823AC19DCCEBA45A3D992BD5AC5ED63CAC6434D8B6EA875E7AE73BEAAF8BCD495EC12E6220604CA0617EBAA94AD9B95FAF0FFDF20925F27E928D60BCF0E59D9F5D49EC0FF854E7F7E5C580B572A9021D8EAE6D9ED3FF68D02B7BC357C2E465BCCE4B4391316C19F0050CAB8093ECA12C68365795561CC14F1448FB0AC83A365C75D3E4FA7D1FF4A73DCC5D226593EF899CA6E90C4FE42CA81CD98F7BADCB43927E83B3DF859A8C4E94CB1046A21C762");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA256)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("E01A8BDC3B51D0E4C4A92835AF2AE468D409F73233051F548EDCE0B8590FEEA2");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("A2386D7FF4EF41BAD87D62CA8BAA1563AAFDF8013ED6B3668BFC1E7616D86CAC");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("1E9F68491378B9B4E2F5BC126BC8E0781ACADD18E07F0C96B6592ED069E3F864BE208004FBE3070073192BC3651EE50C67FDB5E2466606938C0A103F7AB973D9104C5F373B37FD9D81090EE248E5ACAAEE051BB672CD58979ABC3138A11E73684F1F3EDBD96DB2B59564B0DA7175A4E222F5F13AFEBFA1013EE6FF1E6A1C19F19350EA1A7AE0CCD34054D4FD62ACD1474B1B8A276AACD60C370F3198E3711CE00DDE58D1B124262CDC198BFCB074EA897EB3B7D78E3C62BBFFEAFEC736C405030C94DA7828BE27EABD72334512DCC50F54B967C0D68BBAAA3AEE805802943493650C18D6CCAA3ECBCC0B34DEBB053BE8DACC6C1B384EF1827384B95A79BDB454C5FF692BA26119267BFB75D830E1BB363C156C15BDF9F878D511CECDE3EFBE6D09F1FADB0346D0997992735548B0F753760C19B20FBB062CA84AFC1E38E629E5ED1782FB1172996EADD2A4E9B6EEDFDB3C54CB292F233312C6AA3D8F3826D0E2236196F33CDB02B49000C473F126CACCAA3E13DC8B0274F58DE2B80DE57C05A43EE7F9EDCCB41D86337479487E5CCC4F1DCB6F9AB3166773E02380329C62CD43DED51FE4D0C7D7F4249FD3B15571DA87F51A76AA06CE716C001EA619AC611265758E7AF6290AC09AEA14F54D9141CA81A833EB925F4DFB2AF07D2E400154E160FBF062315D2B82A345A34AB45E26D36ED46AE5AB23FF74FA6E66CEF6D43B50A6");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("C89C80F4960432187ADAE12F706D0AE8AB2E2B61A8C97827451B303245C6A3D6");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("0D9E93AE17D944B14BE6F382A64341AFE7B33BE21774654ECF9FB8D3CE036E86");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("6AE6CE7C125E00AA027BF902A09F1C51D6F7328A24E537BF07781D90DA390A505508D0DA6270F00A088BD5C66FE48CF1B049EE70F53BB683187CBB7FF67CAFBD1A39AB890A6B92E245398529627DF1DFA168644F5368D6D99A7313058402DE06A33CFAB360CB78887B6D80839CFDBAC4832DAB2C2BFD929D362E1535BD27C84EB4777E1D9015AFFA50DFB5A3C96B0D96BB9A09D47AD92F9AD666758344DE984C1E2ACA0B42618EE73F0AA654D466D1481CA41FB91EE211309C51E77F63CE955C3EBDA37D75D762D5F56A433836EEB2425534958E06AD74157F7808B70F626A2E953B547DAC8E8650F06E9D2DEAA5279CB7D7DC7AC3534928DD626AB77CB97F22AC3FF4ACBDE5EC2E60EE346F26B2E598DE5F00FA884B76627CCA24010FC65AD14B2E433099A0005B4B00A350E554E959A4DD4026D9ADDAE7297AF65B852CD1DEA1A9425EB2CEBF09BC60C0EE01E48CAF730E1FFA7FE190528103DABB735D5A0B04C2CDC01DA99512FC6615090D97D0A94A1988D7178430C27D77758DCF8A77BAAA958DE834EB5BC56786DF21304D58812DB187CC4ED8836B92F6F3440D02E949E356508273CF1F91DB3C056E8BA2A78ECED059E03EF8CA2B906504CDA98CE74B2B5AD4E25BA20D8C24B34C521BC10796C9BE840ED1811FB29936F575A33776322536F7EF449B75C363DA6FF6B45B7A471176638766C1F930DBE464027E48712F");
}
else if (key_size == 4096 && hash_algo == HashAlgo::SHA512)
{
tests[0].salt = tc::cli::FormatUtil::hexStringToBytes("6B87C931844386E1EA9DD45F2AF92EC91F2E44A55FFCF80EB0FAF11A41C832FCA87939A0408C0A36A052A7D9ADC1311DB71789EA5F3E8C12F0716ADBB5FD3C67");
tests[0].message_digest = tc::cli::FormatUtil::hexStringToBytes("1F5E7340F40C9B424E8112451133D56DC4EF2221083C96CDC4669BD4AE328659D4872AB16B4720EAEE67637EED14A637664AA92831DC0DE43AC659BB738FBD99");
tests[0].signature = tc::cli::FormatUtil::hexStringToBytes("0CBF1D7BA11072DA92D6D1EB9C9484F848A70F6D3909E98BF323F342033EED440E85963B0298BA7EB003148A1FF92F2698C4EEC0B26445C5A5F1ABA10DC8C1601D9A0C6C935A5E031C8FE1EBBEFE3C81DA36EAEFB85B65033C33E1E2113B3BCB410A96A37F131C0F4E945BBC222900CE628EC2A92BD58B303DE171EAF518AF9C9460183FF1039C00F7D98308DC97272F8711A882920AB1502EA88B92BB905A58CC4059EE9BBB53006BAA0E4B04D51A6CFEE65AF3351EC4E2707CF35C57DC12351FCC95DE736B96CAC0C2DEB4275C2598E62D2A2AAAFFBF68F2CA5D247EEB818FD00BD58883F669F0D8E3877A77AA7391EFD5C69D8CB828CADEC7D60D4C589A882B9646DA32EC58BE2AE9A3C78E5821664903637F8A8837ABC55C874284B54BB3485AEE9094DBCC46822A8D6DF929D2DB01CC225CAC775AFF31DAFF0E64AE65CB57E9C24E89173C32F384553884CB0F46DEEB9233985A0B9E6F9B1B8421C20A5F29EE7AA54EF8AF134D754E98429F7EBE36B56686798964A3E2A258DC05D35097F825A630FE4695EB23CCADBC59A433F6104A7E55565D1A427E8B6E4ADE1780E8888C86EBCCE6EC950EB0BAFE3946423FE5CFF04707F245F51C7DF5C3AFC1542EC93D30D6402AF0F3CE43ECC9F0B338014F0941058AA69DECF9BFE7EA3CE3667AA434EDE1F1FC5596AC7BE326F4F4F37BD106E377093FD20B6A13D27EE50E161B");
tests[1].salt = tc::cli::FormatUtil::hexStringToBytes("0E5B60B7C6EE9DAC18ED464A4268C296C55B787D298FD23337E9D39077AC60266637D5B03338A0B752C631150EA9655AE71EC42779EF2B6119D31EAB19E33DAB");
tests[1].message_digest = tc::cli::FormatUtil::hexStringToBytes("709BA2DD10CFD1ED5D3B2833B6B42664ED9684DFA239520D842598A75BED777BB7D5D71E7D7A5E5658C925AB96C8ED99CFAE1E46A33C4736655CB3BC3EB23B6B");
tests[1].signature = tc::cli::FormatUtil::hexStringToBytes("853EF4AA99E0492D0CFD6F11653FA10164738BAD6A3CE53DA037FACEBFFD79E2350BF5F45729B262D8B5EFA769E0155D7B2AF1A837D84674269055BA69F042F6B2FDA4161D066EBDEDCDF989EE768A91C9653AD11157EAB98C0848BCE5F2052CCBFE5A67CB2621A5070219619BAE3E79333536384700AF1B9462F77904F30465107D0DAE9E40ADE7DE7167F8E29F6B8C8DF04B84781D5855BB22D3BECEDC7DEE545CEBA094028043FBD228F33D2656C4D74630DB2BF124E739AE3630BAF07855E72AF9488CD8036875805F0F6390C6C315D9A4BC65B935C0C172A0B871EAB66373D1293B37B3A95BE334A9C31B93D7F4C12397674BA59088B873973B22428C1ED8720C0488DE41FF072165B34CEE65EC78B39B141A9559395BC0EE42A81FBFADD1EAF6878F83F4BB0CA9B4AC72F900437597B32AD7878BB8921E61F0F3A522513A80BB58142EA2E16C58BC0AFF2DB30DC9475FBDE2D68BE3FD34BD9EAF35691BE40016893E86D34AD2B0423C7BE6116ECB1BB7EBF951F9E18C86626E1C106978F26DC6C8C9D4C97040634E9FEA87E6AC7F3BFB1AB585DE424F5F508BB9A01C27B5AF50768B03A37D430777A17EB34E5EFFB271034A5893213618B1022782F456A4AC006452C8D8C88B295C325C891A07D6242F2790B3E746935DCE19916594C764302A8444A859B8371B4119058BD41040B9A7032FC2B5110218A3AF78822ED1");
}
else
{
// no case for provided key size and hash
return;
}
// copy populated tests to output
for (size_t i = 0; i < tests.size(); i++)
{
test_list.push_back(tests[i]);
}
}
@@ -1,32 +0,0 @@
#pragma once
#include <tc/types.h>
#include <tc/ByteData.h>
class RsaPssUtil
{
public:
struct TestVector
{
std::string test_name;
tc::ByteData key_modulus;
tc::ByteData key_private_exponent;
tc::ByteData message_digest;
tc::ByteData salt;
tc::ByteData signature;
};
enum HashAlgo
{
MD4,
MD5,
SHA1,
SHA224,
SHA256,
SHA384,
SHA512
};
/// Implementation of https://gist.github.com/jakcron/1c00ed37089743e38df46da2d9ccf6a0
static void generateRsaPssTestVectors_Custom(std::vector<RsaPssUtil::TestVector>& test_list, size_t key_size, RsaPssUtil::HashAlgo hash_algo);
};
@@ -1,90 +0,0 @@
#include "SinkTestUtil.h"
#include <sstream>
const std::string SinkTestUtil::DummySinkBase::kClassName = "DummySinkBase";
void SinkTestUtil::testSinkLength(tc::io::ISink& sink, int64_t expected_len)
{
std::stringstream error_ss;
int64_t actual_len = sink.length();
if (actual_len != expected_len)
{
error_ss << "length() returned: " << actual_len << ", when it should have been " << expected_len << ".";
throw tc::Exception(error_ss.str());
}
}
SinkTestUtil::DummySinkBase::DummySinkBase() :
DummySinkBase(0x10000000)
{
}
SinkTestUtil::DummySinkBase::DummySinkBase(int64_t length) :
DummySinkBase(length, true)
{
}
SinkTestUtil::DummySinkBase::DummySinkBase(int64_t length, bool canSetLength)
{
init(length, canSetLength);
}
void SinkTestUtil::DummySinkBase::init(int64_t length, bool canSetLength)
{
mCanSetLength = canSetLength;
mLength = length;
}
int64_t SinkTestUtil::DummySinkBase::length()
{
return mLength;
}
void SinkTestUtil::DummySinkBase::setLength(int64_t length)
{
if (mCanSetLength == false)
throw tc::NotImplementedException(kClassName, "setLength() is not implemented");
mLength = length;
}
size_t SinkTestUtil::DummySinkBase::pushData(const tc::ByteData& data, int64_t offset)
{
throw tc::NotImplementedException(kClassName, "pushData not implemented");
}
SinkTestUtil::DummySinkTestablePushData::DummySinkTestablePushData() :
DummySinkBase(),
expected_data(std::make_shared<tc::ByteData>(0)),
expected_offset(std::make_shared<int64_t>(0))
{}
void SinkTestUtil::DummySinkTestablePushData::setExpectedPushDataCfg(const tc::ByteData& data, int64_t offset)
{
*expected_data = data;
*expected_offset = offset;
}
size_t SinkTestUtil::DummySinkTestablePushData::pushData(const tc::ByteData& data, int64_t offset)
{
std::stringstream error_ss;
if (offset != *expected_offset)
{
error_ss << "pushData() was called on base_sink with offset " << offset << ", when it should have been " << *expected_offset << ".";
throw tc::Exception(error_ss.str());
}
if (data.size() != expected_data->size())
{
error_ss << "pushData() passed a ByteData to base_sink with size " << data.size() << ", when it should have been " << expected_data->size() << ".";
throw tc::Exception(error_ss.str());
}
if (memcmp(data.data(), expected_data->data(), data.size()) != 0)
{
throw tc::Exception("ByteData pushed to base sink did not have expected data.");
}
return data.size();
}
@@ -1,42 +0,0 @@
#pragma once
#include <tc/io/ISink.h>
#include <tc/NotImplementedException.h>
#include <tc/ArgumentOutOfRangeException.h>
class SinkTestUtil
{
public:
static void testSinkLength(tc::io::ISink& source, int64_t expected_len);
class DummySinkBase : public tc::io::ISink
{
public:
DummySinkBase();
DummySinkBase(int64_t length);
DummySinkBase(int64_t length, bool canSetLength);
void init(int64_t length, bool canSetLength);
int64_t length();
void setLength(int64_t length);
virtual size_t pushData(const tc::ByteData& data, int64_t offset);
private:
static const std::string kClassName;
bool mCanSetLength;
int64_t mLength;
};
class DummySinkTestablePushData : public DummySinkBase
{
public:
DummySinkTestablePushData();
void setExpectedPushDataCfg(const tc::ByteData& data, int64_t offset);
size_t pushData(const tc::ByteData& data, int64_t offset);
private:
std::shared_ptr<tc::ByteData> expected_data;
std::shared_ptr<int64_t> expected_offset;
};
};
@@ -1,32 +0,0 @@
#include "SourceTestUtil.h"
#include <sstream>
void SourceTestUtil::testSourceLength(tc::io::ISource& source, int64_t expected_len)
{
std::stringstream error_ss;
int64_t actual_len = source.length();
if (actual_len != expected_len)
{
error_ss << "length() returned: " << actual_len << ", when it should have been " << expected_len << ".";
throw tc::Exception(error_ss.str());
}
}
void SourceTestUtil::pullTestHelper(tc::io::ISource& source, int64_t offset, size_t len, size_t expected_len, const byte_t* expected_data)
{
std::stringstream error_ss;
tc::ByteData data = source.pullData(offset, len);
if (data.size() != expected_len)
{
error_ss << "pullData(offset: " << offset << ", len:" << len << ") returned ByteData with size(): " << data.size() << ", when it should have been " << expected_len;
throw tc::Exception(error_ss.str());
}
if (expected_data != nullptr && memcmp(data.data(), expected_data, expected_len) != 0)
{
error_ss << "pullData(offset: " << offset << ", len:" << len << ") returned ByteData with incorrect layout";
throw tc::Exception(error_ss.str());
}
}
@@ -1,9 +0,0 @@
#pragma once
#include <tc/io/ISource.h>
class SourceTestUtil
{
public:
static void testSourceLength(tc::io::ISource& source, int64_t expected_len);
static void pullTestHelper(tc::io::ISource& source, int64_t offset, size_t len, size_t expected_len, const byte_t* expected_data);
};
@@ -1,125 +0,0 @@
#include "StreamTestUtil.h"
#include <fmt/core.h>
#include <tc/cli.h>
const std::string StreamTestUtil::DummyStreamBase::kClassName = "DummyStreamBase";
void StreamTestUtil::constructor_TestHelper(tc::io::IStream& stream, int64_t stream_length, int64_t exp_pos_res, bool exp_canread_res, bool exp_canwrite_res, bool exp_canseek_res)
{
int64_t length_res = stream.length();
int64_t pos_res = stream.position();
bool can_read = stream.canRead();
bool can_write = stream.canWrite();
bool can_seek = stream.canSeek();
if (length_res != stream_length)
{
throw tc::Exception(fmt::format("Stream did not have length {:d} (actual {:d})", stream_length, length_res));
}
if (pos_res != exp_pos_res)
{
throw tc::Exception(fmt::format("Stream did not have position {:d} (actual {:d})", exp_pos_res, pos_res));
}
if (can_read != exp_canread_res)
{
throw tc::Exception(fmt::format("Stream property canRead() was not {} (actual {})", exp_canread_res, can_read));
}
if (can_write != exp_canwrite_res)
{
throw tc::Exception(fmt::format("Stream property canWrite() was not {} (actual {})", exp_canwrite_res, can_write));
}
if (can_seek != exp_canseek_res)
{
throw tc::Exception(fmt::format("Stream property canSeek() was not {} (actual {})", exp_canseek_res, can_seek));
}
}
void StreamTestUtil::seek_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, int64_t exp_seek_res, int64_t exp_pos_res)
{
int64_t seek_res = stream.seek(seek_offset, seek_origin);
int64_t pos_res = stream.position();
if (seek_res != exp_seek_res)
{
throw tc::Exception(fmt::format("Stream did not return position from seek {:d} (actual {:d})", exp_seek_res, seek_res));
}
if (pos_res != exp_pos_res)
{
throw tc::Exception(fmt::format("Stream did not have position {:d} (actual {:d})", exp_pos_res, pos_res));
}
}
void StreamTestUtil::read_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, size_t dst_size, size_t read_count, size_t exp_read_res, int64_t exp_pos_res, const byte_t* expected_data)
{
tc::ByteData data(dst_size);
// offset the position
stream.seek(seek_offset, seek_origin);
// read data
size_t read_res = stream.read(data.data(), read_count);
// check position
int64_t pos_res = stream.position();
// validate read result
if (read_res != exp_read_res)
{
throw tc::Exception(fmt::format("Stream did not read expected number of bytes {:d} (actual {:d})", exp_read_res, read_res));
}
// validate read data
if (expected_data != nullptr && memcmp(data.data(), expected_data, exp_read_res) != 0)
{
throw tc::Exception(fmt::format("Stream did not read expected bytes (read: \"{}\", expected: \"{}\"", tc::cli::FormatUtil::formatBytesAsString(data.data(), data.size(), true, ""), tc::cli::FormatUtil::formatBytesAsString(expected_data, exp_read_res, true, "")));
}
// validate pos result
if (pos_res != exp_pos_res)
{
throw tc::Exception(fmt::format("Stream did not have position {:d} (actual {:d})", exp_pos_res, pos_res));
}
}
void StreamTestUtil::write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, tc::ByteData& data, int64_t exp_pos_res)
{
write_TestHelper(stream, seek_offset, seek_origin, data.data(), data.size(), exp_pos_res);
}
void StreamTestUtil::write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, tc::ByteData& data, int64_t exp_pos_res, int64_t exp_length_res)
{
write_TestHelper(stream, seek_offset, seek_origin, data.data(), data.size(), exp_pos_res, exp_length_res);
}
void StreamTestUtil::write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, const byte_t* data, size_t data_size, int64_t exp_pos_res)
{
// offset the position
stream.seek(seek_offset, seek_origin);
stream.write(data, data_size);
int64_t pos_res = stream.position();
// validate pos result
if (pos_res != exp_pos_res)
{
throw tc::Exception(fmt::format("Stream did not have position {:d} (actual {:d})", exp_pos_res, pos_res));
}
}
void StreamTestUtil::write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, const byte_t* data, size_t data_size, int64_t exp_pos_res, int64_t exp_length_res)
{
write_TestHelper(stream, seek_offset, seek_origin, data, data_size, exp_pos_res);
// validate length result
int64_t length_res = stream.length();
if (length_res != exp_length_res)
{
throw tc::Exception(fmt::format("Stream did not have length {:d} (actual {:d})", exp_length_res, length_res));
}
}
@@ -1,154 +0,0 @@
#pragma once
#include <tc/io/IStream.h>
#include <tc/ByteData.h>
#include <tc/NotImplementedException.h>
#include <tc/ArgumentOutOfRangeException.h>
class StreamTestUtil
{
public:
static void constructor_TestHelper(tc::io::IStream& stream, int64_t stream_length, int64_t exp_pos_res, bool exp_canread_res, bool exp_canwrite_res, bool exp_canseek_res);
static void seek_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, int64_t exp_seek_res, int64_t exp_pos_res);
static void read_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, size_t dst_size, size_t read_count, size_t exp_read_res, int64_t exp_pos_res, const byte_t* expected_data = nullptr);
static void write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, tc::ByteData& data, int64_t exp_pos_res);
static void write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, tc::ByteData& data, int64_t exp_pos_res, int64_t exp_length_res);
static void write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, const byte_t* data, size_t data_size, int64_t exp_pos_res);
static void write_TestHelper(tc::io::IStream& stream, int64_t seek_offset, tc::io::SeekOrigin seek_origin, const byte_t* data, size_t data_size, int64_t exp_pos_res, int64_t exp_length_res);
class DummyStreamBase : public tc::io::IStream
{
public:
DummyStreamBase() :
DummyStreamBase(0x10000000)
{
}
DummyStreamBase(int64_t length) :
DummyStreamBase(length, true, true, true, true, true)
{
}
DummyStreamBase(int64_t length, bool canRead, bool canWrite, bool canSeek, bool canSeekOnlyFromBegin, bool canSetLength)
{
init(length, canRead, canWrite, canSeek, canSeekOnlyFromBegin, canSetLength);
}
DummyStreamBase(int64_t length, int64_t position, bool canRead, bool canWrite, bool canSeek, bool canSeekOnlyFromBegin, bool canSetLength)
{
init(length, position, canRead, canWrite, canSeek, canSeekOnlyFromBegin, canSetLength);
}
void init(int64_t length, bool canRead, bool canWrite, bool canSeek, bool canSeekOnlyFromBegin, bool canSetLength)
{
init(length, 0, canRead, canWrite, canSeek, canSeekOnlyFromBegin, canSetLength);
}
void init(int64_t length, int64_t position, bool canRead, bool canWrite, bool canSeek, bool canSeekOnlyFromBegin, bool canSetLength)
{
mCanRead = canRead;
mCanWrite = canWrite;
mCanSeek = canSeek;
mCanSeekOnlyFromBegin = canSeekOnlyFromBegin;
mPosition = position;
mLength = length;
}
bool canRead() const
{
return mCanRead;
}
bool canWrite() const
{
return mCanWrite;
}
bool canSeek() const
{
return mCanSeek;
}
int64_t length()
{
return mLength;
}
int64_t position()
{
return mPosition;
}
virtual size_t read(byte_t* ptr, size_t count)
{
throw tc::NotImplementedException(kClassName, "read() not implemented");
}
virtual size_t write(const byte_t* ptr, size_t count)
{
throw tc::NotImplementedException(kClassName, "write() not implemented");
}
int64_t seek(int64_t offset, tc::io::SeekOrigin origin)
{
if (origin != tc::io::SeekOrigin::Begin && mCanSeekOnlyFromBegin)
throw tc::ArgumentOutOfRangeException(kClassName, "Should not be passing seek origin values that are not SeekOrigin::Begin to the base stream");
switch (origin)
{
case (tc::io::SeekOrigin::Begin):
mPosition = offset;
break;
case (tc::io::SeekOrigin::Current):
mPosition += offset;
break;
case (tc::io::SeekOrigin::End):
mPosition = mLength + offset;
break;
default:
throw tc::ArgumentOutOfRangeException(kClassName, "Illegal seek origin");
}
if (mPosition > mLength)
{
throw tc::ArgumentOutOfRangeException(kClassName, "Illegal seek position");
}
return mPosition;
}
void setLength(int64_t length)
{
if (mCanSetLength == false)
throw tc::NotImplementedException(kClassName, "setLength() is not implemented");
mLength = length;
}
void flush()
{
// nothing
}
void dispose()
{
flush();
mCanRead = false;
mCanWrite = false;
mCanSeek = false;
mCanSetLength = false;
mPosition = 0;
mLength = 0;
}
private:
static const std::string kClassName;
bool mCanRead;
bool mCanWrite;
bool mCanSeek;
bool mCanSeekOnlyFromBegin;
bool mCanSetLength;
int64_t mPosition;
int64_t mLength;
};
};
@@ -1,173 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include "bn_binaryutils_TestClass.h"
void bn_binaryutils_TestClass::runAllTests(void)
{
std::cout << "[tc::bn (BinaryUtils)] START" << std::endl;
test_RoundUpFunc();
test_AlignFunc();
test_MakeStructMagicU32Func();
test_MakeStructMagicU64Func();
std::cout << "[tc::bn (BinaryUtils)] END" << std::endl;
}
void bn_binaryutils_TestClass::test_RoundUpFunc()
{
std::cout << "[tc::bn (BinaryUtils)] test_RoundUpFunc : " << std::flush;
try
{
try
{
util_RoundUpFuncTestCase(0, 0x200, 0x200);
util_RoundUpFuncTestCase(1, 0x200, 0x200);
util_RoundUpFuncTestCase(0x10, 0x200, 0x200);
util_RoundUpFuncTestCase(0x1FF, 0x200, 0x200);
util_RoundUpFuncTestCase(0x200, 0x200, 0x400);
util_RoundUpFuncTestCase(0x201, 0x200, 0x400);
util_RoundUpFuncTestCase(0x3FF, 0x200, 0x400);
util_RoundUpFuncTestCase(0xDEADBE00, 0x200, 0xDEADC000);
util_RoundUpFuncTestCase(0xDEADBEEF, 0x200, 0xDEADC000);
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_binaryutils_TestClass::util_RoundUpFuncTestCase(uint32_t value, uint32_t alignment, uint32_t expected_result)
{
std::stringstream error_ss;
uint32_t result = roundup<uint32_t>(value, alignment);
if (result != expected_result)
{
error_ss << std::hex;
error_ss << "roundup(0x" << value << ", 0x" << alignment << ") returned: 0x" << result << " (expected: 0x" << expected_result << ")" << std::endl;
throw tc::Exception(error_ss.str());
}
}
void bn_binaryutils_TestClass::test_AlignFunc()
{
std::cout << "[tc::bn (BinaryUtils)] test_AlignFunc : " << std::flush;
try
{
try
{
util_AlignFuncTestCase(0, 0x200, 0x0);
util_AlignFuncTestCase(1, 0x200, 0x200);
util_AlignFuncTestCase(0x10, 0x200, 0x200);
util_AlignFuncTestCase(0x1FF, 0x200, 0x200);
util_AlignFuncTestCase(0x200, 0x200, 0x200);
util_AlignFuncTestCase(0x201, 0x200, 0x400);
util_AlignFuncTestCase(0x3FF, 0x200, 0x400);
util_AlignFuncTestCase(0xDEADBE00, 0x200, 0xDEADBE00);
util_AlignFuncTestCase(0xDEADBEEF, 0x200, 0xDEADC000);
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_binaryutils_TestClass::util_AlignFuncTestCase(uint32_t value, uint32_t alignment, uint32_t expected_result)
{
std::stringstream error_ss;
uint32_t result = align<uint32_t>(value, alignment);
if (result != expected_result)
{
error_ss << std::hex;
error_ss << "align(0x" << value << ", 0x" << alignment << ") returned: 0x" << result << " (expected: 0x" << expected_result << ")" << std::endl;
throw tc::Exception(error_ss.str());
}
}
void bn_binaryutils_TestClass::test_MakeStructMagicU32Func()
{
std::cout << "[tc::bn (BinaryUtils)] test_MakeStructMagicU32Func : " << std::flush;
try
{
try
{
util_MakeStructMagicU32FuncTestCase("BABE", 0x45424142);
util_MakeStructMagicU32FuncTestCase("NEXT", 0x5458454E);
util_MakeStructMagicU32FuncTestCase("\x7F""ELF", 0x464C457F);
util_MakeStructMagicU32FuncTestCase("BIN0", 0x304E4942);
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_binaryutils_TestClass::util_MakeStructMagicU32FuncTestCase(const char* struct_magic_str, uint32_t expected_result)
{
std::stringstream error_ss;
uint32_t result = tc::bn::make_struct_magic_uint32(struct_magic_str);
if (result != expected_result)
{
error_ss << std::hex;
error_ss << "make_struct_magic_uint32() returned: 0x" << result << " (expected: 0x" << expected_result << ")" << std::endl;
throw tc::Exception(error_ss.str());
}
}
void bn_binaryutils_TestClass::test_MakeStructMagicU64Func()
{
std::cout << "[tc::bn (BinaryUtils)] test_MakeStructMagicU64Func : " << std::flush;
try
{
try
{
util_MakeStructMagicU64FuncTestCase("HOMEBREW", 0x57455242454D4F48);
util_MakeStructMagicU64FuncTestCase("NEXTSPEC", 0x434550535458454E);
util_MakeStructMagicU64FuncTestCase("EMPTY\0\0\0", 0x0000005954504D45);
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_binaryutils_TestClass::util_MakeStructMagicU64FuncTestCase(const char* struct_magic_str, uint64_t expected_result)
{
std::stringstream error_ss;
uint64_t result = tc::bn::make_struct_magic_uint64(struct_magic_str);
if (result != expected_result)
{
error_ss << std::hex;
error_ss << "make_struct_magic_uint32() returned: 0x" << result << " (expected: 0x" << expected_result << ")" << std::endl;
throw tc::Exception(error_ss.str());
}
}
@@ -1,25 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/bn.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/NotImplementedException.h>
class bn_binaryutils_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_RoundUpFunc();
void util_RoundUpFuncTestCase(uint32_t value, uint32_t alignment, uint32_t expected_result);
void test_AlignFunc();
void util_AlignFuncTestCase(uint32_t value, uint32_t alignment, uint32_t expected_result);
void test_MakeStructMagicU32Func();
void util_MakeStructMagicU32FuncTestCase(const char* struct_magic_str, uint32_t expected_result);
void test_MakeStructMagicU64Func();
void util_MakeStructMagicU64FuncTestCase(const char* struct_magic_str, uint64_t expected_result);
};
@@ -1,238 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tc/types.h>
#include "bn_bitarrayByteBEBitBE_TestClass.h"
//---------------------------------------------------------
void bn_bitarrayByteBEBitBE_TestClass::runAllTests(void)
{
std::cout << "[tc::bn::bitarray<BE,BE>] START" << std::endl;
test_Size();
test_TestBit();
test_SetBit();
test_ResetBit();
test_FlipBit();
std::cout << "[tc::bn::bitarray<BE,BE>] END" << std::endl;
}
//---------------------------------------------------------
void bn_bitarrayByteBEBitBE_TestClass::test_Size()
{
std::cout << "[tc::bn::bitarray<BE,BE>] test_Size : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
static const size_t kBitArraySize = sizeof(uint32_t);
tc::bn::bitarray<kBitArraySize, false, false> bf;
if (sizeof(bf) != kBitArraySize)
{
ss << "sizeof(bf) had value " << std::dec << sizeof(bf) << " (expected " << kBitArraySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitBE_TestClass::test_TestBit()
{
std::cout << "[tc::bn::bitarray<BE,BE>] test_TestBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
helper_TestBit("All bits clear", bf, {});
// set all bits
*((uint32_t*)&bf) = -1;
helper_TestBit("All bits set", bf, {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31});
// test endianness calculation
byte_t* bf_raw = (byte_t*)&bf;
// set byte0 bit0
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x01;
helper_TestBit("byte0bit0 test", bf, {31});
// set byte0 bit7
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x80;
helper_TestBit("byte0bit7 test", bf, {24});
// set byte3 bit0
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x01;
helper_TestBit("byte3bit0 test", bf, {7});
// set byte3 bit7
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x80;
helper_TestBit("byte3bit7 test", bf, {0});
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitBE_TestClass::test_SetBit()
{
std::cout << "[tc::bn::bitarray<BE,BE>] test_SetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.set(i);
if (bf.test(i) != true)
{
ss << "set() failed to set bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitBE_TestClass::test_ResetBit()
{
std::cout << "[tc::bn::bitarray<BE,BE>] test_ResetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.reset(i);
if (bf.test(i) != false)
{
ss << "reset() failed to clear bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitBE_TestClass::test_FlipBit()
{
std::cout << "[tc::bn::bitarray<BE,BE>] test_FlipBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.flip(i);
if (bf.test(i) != false)
{
ss << "reset() failed to flip bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitBE_TestClass::helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits)
{
std::stringstream ss;
for (size_t i = 0; i < bitarray.bit_size(); i++)
{
bool res = bitarray.test(i);
bool expected_res = std::find(expected_set_bits.begin(), expected_set_bits.end(), i) != expected_set_bits.end();
if (res != expected_res)
{
if (test_name.empty() == false)
ss << test_name << " : ";
ss << "bitarray.test(" << std::dec << i << ") had value " << std::boolalpha << res << " (expected " << std::boolalpha << expected_res << ")";
throw tc::Exception(ss.str());
}
}
}
@@ -1,19 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/types.h>
class bn_bitarrayByteBEBitBE_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Size();
void test_TestBit();
void test_SetBit();
void test_ResetBit();
void test_FlipBit();
using testtype_t = tc::bn::bitarray<sizeof(uint32_t), false, false>;
void helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits);
};
@@ -1,238 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tc/types.h>
#include "bn_bitarrayByteBEBitLE_TestClass.h"
//---------------------------------------------------------
void bn_bitarrayByteBEBitLE_TestClass::runAllTests(void)
{
std::cout << "[tc::bn::bitarray<BE,LE>] START" << std::endl;
test_Size();
test_TestBit();
test_SetBit();
test_ResetBit();
test_FlipBit();
std::cout << "[tc::bn::bitarray<BE,LE>] END" << std::endl;
}
//---------------------------------------------------------
void bn_bitarrayByteBEBitLE_TestClass::test_Size()
{
std::cout << "[tc::bn::bitarray<BE,LE>] test_Size : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
static const size_t kBitArraySize = sizeof(uint32_t);
tc::bn::bitarray<kBitArraySize, false, true> bf;
if (sizeof(bf) != kBitArraySize)
{
ss << "sizeof(bf) had value " << std::dec << sizeof(bf) << " (expected " << kBitArraySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitLE_TestClass::test_TestBit()
{
std::cout << "[tc::bn::bitarray<BE,LE>] test_TestBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
helper_TestBit("All bits clear", bf, {});
// set all bits
*((uint32_t*)&bf) = -1;
helper_TestBit("All bits set", bf, {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31});
// test endianness calculation
byte_t* bf_raw = (byte_t*)&bf;
// set byte0 bit0
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x01;
helper_TestBit("byte0bit0 test", bf, {24});
// set byte0 bit7
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x80;
helper_TestBit("byte0bit7 test", bf, {31});
// set byte3 bit0
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x01;
helper_TestBit("byte3bit0 test", bf, {0});
// set byte3 bit7
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x80;
helper_TestBit("byte3bit7 test", bf, {7});
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitLE_TestClass::test_SetBit()
{
std::cout << "[tc::bn::bitarray<BE,LE>] test_SetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.set(i);
if (bf.test(i) != true)
{
ss << "set() failed to set bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitLE_TestClass::test_ResetBit()
{
std::cout << "[tc::bn::bitarray<BE,LE>] test_ResetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.reset(i);
if (bf.test(i) != false)
{
ss << "reset() failed to clear bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitLE_TestClass::test_FlipBit()
{
std::cout << "[tc::bn::bitarray<BE,LE>] test_FlipBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.flip(i);
if (bf.test(i) != false)
{
ss << "reset() failed to flip bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteBEBitLE_TestClass::helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits)
{
std::stringstream ss;
for (size_t i = 0; i < bitarray.bit_size(); i++)
{
bool res = bitarray.test(i);
bool expected_res = std::find(expected_set_bits.begin(), expected_set_bits.end(), i) != expected_set_bits.end();
if (res != expected_res)
{
if (test_name.empty() == false)
ss << test_name << " : ";
ss << "bitarray.test(" << std::dec << i << ") had value " << std::boolalpha << res << " (expected " << std::boolalpha << expected_res << ")";
throw tc::Exception(ss.str());
}
}
}
@@ -1,19 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/types.h>
class bn_bitarrayByteBEBitLE_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Size();
void test_TestBit();
void test_SetBit();
void test_ResetBit();
void test_FlipBit();
using testtype_t = tc::bn::bitarray<sizeof(uint32_t), false, true>;
void helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits);
};
@@ -1,238 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tc/types.h>
#include "bn_bitarrayByteLEBitBE_TestClass.h"
//---------------------------------------------------------
void bn_bitarrayByteLEBitBE_TestClass::runAllTests(void)
{
std::cout << "[tc::bn::bitarray<LE,BE>] START" << std::endl;
test_Size();
test_TestBit();
test_SetBit();
test_ResetBit();
test_FlipBit();
std::cout << "[tc::bn::bitarray<LE,BE>] END" << std::endl;
}
//---------------------------------------------------------
void bn_bitarrayByteLEBitBE_TestClass::test_Size()
{
std::cout << "[tc::bn::bitarray<LE,BE>] test_Size : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
static const size_t kBitArraySize = sizeof(uint32_t);
tc::bn::bitarray<kBitArraySize, true, false> bf;
if (sizeof(bf) != kBitArraySize)
{
ss << "sizeof(bf) had value " << std::dec << sizeof(bf) << " (expected " << kBitArraySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitBE_TestClass::test_TestBit()
{
std::cout << "[tc::bn::bitarray<LE,BE>] test_TestBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
helper_TestBit("All bits clear", bf, {});
// set all bits
*((uint32_t*)&bf) = -1;
helper_TestBit("All bits set", bf, {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31});
// test endianness calculation
byte_t* bf_raw = (byte_t*)&bf;
// set byte0 bit0
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x01;
helper_TestBit("byte0bit0 test", bf, {7});
// set byte0 bit7
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x80;
helper_TestBit("byte0bit7 test", bf, {0});
// set byte3 bit0
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x01;
helper_TestBit("byte3bit0 test", bf, {31});
// set byte3 bit7
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x80;
helper_TestBit("byte3bit7 test", bf, {24});
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitBE_TestClass::test_SetBit()
{
std::cout << "[tc::bn::bitarray<LE,BE>] test_SetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.set(i);
if (bf.test(i) != true)
{
ss << "set() failed to set bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitBE_TestClass::test_ResetBit()
{
std::cout << "[tc::bn::bitarray<LE,BE>] test_ResetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.reset(i);
if (bf.test(i) != false)
{
ss << "reset() failed to clear bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitBE_TestClass::test_FlipBit()
{
std::cout << "[tc::bn::bitarray<LE,BE>] test_FlipBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.flip(i);
if (bf.test(i) != false)
{
ss << "reset() failed to flip bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitBE_TestClass::helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits)
{
std::stringstream ss;
for (size_t i = 0; i < bitarray.bit_size(); i++)
{
bool res = bitarray.test(i);
bool expected_res = std::find(expected_set_bits.begin(), expected_set_bits.end(), i) != expected_set_bits.end();
if (res != expected_res)
{
if (test_name.empty() == false)
ss << test_name << " : ";
ss << "bitarray.test(" << std::dec << i << ") had value " << std::boolalpha << res << " (expected " << std::boolalpha << expected_res << ")";
throw tc::Exception(ss.str());
}
}
}
@@ -1,19 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/types.h>
class bn_bitarrayByteLEBitBE_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Size();
void test_TestBit();
void test_SetBit();
void test_ResetBit();
void test_FlipBit();
using testtype_t = tc::bn::bitarray<sizeof(uint32_t), true, false>;
void helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits);
};
@@ -1,238 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tc/types.h>
#include "bn_bitarrayByteLEBitLE_TestClass.h"
//---------------------------------------------------------
void bn_bitarrayByteLEBitLE_TestClass::runAllTests(void)
{
std::cout << "[tc::bn::bitarray<LE,LE>] START" << std::endl;
test_Size();
test_TestBit();
test_SetBit();
test_ResetBit();
test_FlipBit();
std::cout << "[tc::bn::bitarray<LE,LE>] END" << std::endl;
}
//---------------------------------------------------------
void bn_bitarrayByteLEBitLE_TestClass::test_Size()
{
std::cout << "[tc::bn::bitarray<LE,LE>] test_Size : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
static const size_t kBitArraySize = sizeof(uint32_t);
tc::bn::bitarray<kBitArraySize, true, true> bf;
if (sizeof(bf) != kBitArraySize)
{
ss << "sizeof(bf) had value " << std::dec << sizeof(bf) << " (expected " << kBitArraySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitLE_TestClass::test_TestBit()
{
std::cout << "[tc::bn::bitarray<LE,LE>] test_TestBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
helper_TestBit("All bits clear", bf, {});
// set all bits
*((uint32_t*)&bf) = -1;
helper_TestBit("All bits set", bf, {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31});
// test endianness calculation
byte_t* bf_raw = (byte_t*)&bf;
// set byte0 bit0
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x01;
helper_TestBit("byte0bit0 test", bf, {0});
// set byte0 bit7
*((uint32_t*)&bf) = 0;
bf_raw[0] = 0x80;
helper_TestBit("byte0bit7 test", bf, {7});
// set byte3 bit0
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x01;
helper_TestBit("byte3bit0 test", bf, {24});
// set byte3 bit7
*((uint32_t*)&bf) = 0;
bf_raw[3] = 0x80;
helper_TestBit("byte3bit7 test", bf, {31});
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitLE_TestClass::test_SetBit()
{
std::cout << "[tc::bn::bitarray<LE,LE>] test_SetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = 0;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.set(i);
if (bf.test(i) != true)
{
ss << "set() failed to set bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitLE_TestClass::test_ResetBit()
{
std::cout << "[tc::bn::bitarray<LE,LE>] test_ResetBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.reset(i);
if (bf.test(i) != false)
{
ss << "reset() failed to clear bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitLE_TestClass::test_FlipBit()
{
std::cout << "[tc::bn::bitarray<LE,LE>] test_FlipBit : " << std::flush;
try
{
try
{
std::stringstream ss;
// check size
testtype_t bf;
// clear all bits
*((uint32_t*)&bf) = -1;
for (size_t i = 0; i < bf.bit_size(); i++)
{
bf.flip(i);
if (bf.test(i) != false)
{
ss << "reset() failed to flip bit " << std::dec << i << std::endl;
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void bn_bitarrayByteLEBitLE_TestClass::helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits)
{
std::stringstream ss;
for (size_t i = 0; i < bitarray.bit_size(); i++)
{
bool res = bitarray.test(i);
bool expected_res = std::find(expected_set_bits.begin(), expected_set_bits.end(), i) != expected_set_bits.end();
if (res != expected_res)
{
if (test_name.empty() == false)
ss << test_name << " : ";
ss << "bitarray.test(" << std::dec << i << ") had value " << std::boolalpha << res << " (expected " << std::boolalpha << expected_res << ")";
throw tc::Exception(ss.str());
}
}
}
@@ -1,19 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/types.h>
class bn_bitarrayByteLEBitLE_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Size();
void test_TestBit();
void test_SetBit();
void test_ResetBit();
void test_FlipBit();
using testtype_t = tc::bn::bitarray<sizeof(uint32_t), true, true>;
void helper_TestBit(const std::string& test_name, const testtype_t& bitarray, const std::vector<size_t>& expected_set_bits);
};
@@ -1,778 +0,0 @@
#include <tc.h>
#include <fmt/core.h>
#include "bn_endian_TestClass.h"
void bn_endian_TestClass::runAllTests()
{
fmt::print("[tc::bn endian_types] START\n");
testLocalBSwap16();
testLocalBSwap32();
testLocalBSwap64();
testBeUint64Inline();
testBeUint32Inline();
testBeUint16Inline();
testLeUint64Inline();
testLeUint32Inline();
testLeUint16Inline();
testBeSwap64Inline();
testBeSwap32Inline();
testBeSwap16Inline();
testLeSwap64Inline();
testLeSwap32Inline();
testLeSwap16Inline();
testBe64TemplateClass();
testBe32TemplateClass();
testBe16TemplateClass();
testLe64TemplateClass();
testLe32TemplateClass();
testLe16TemplateClass();
fmt::print("[tc::bn endian_types] END\n");
}
void bn_endian_TestClass::testLocalBSwap16()
{
fmt::print("[tc::bn::detail::__local_bswap16] testLocalBSwap16 : ");
try
{
uint16_t x = 0xabcd;
uint16_t x_inv = 0xcdab;
uint16_t swap_ret = tc::bn::detail::__local_bswap16(x);
if (swap_ret != x_inv)
{
throw tc::Exception(fmt::format("tc::bn::detail::__local_bswap16(uint16_t) returned 0x{:x} (expected: 0x{:x}", swap_ret, x_inv));
}
uint16_t x_test = x;
tc::bn::detail::__local_bswap16(&x_test);
if (x_test != x_inv)
{
throw tc::Exception(fmt::format("tc::bn::detail::__local_bswap16(void*) transformed 0x{:x} -> 0x{:x} (expected: 0x{:x}", x, x_test, x_inv));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLocalBSwap32()
{
fmt::print("[tc::bn::detail::__local_bswap32] testLocalBSwap32 : ");
try
{
uint32_t x = 0xabcd1234;
uint32_t x_inv = 0x3412cdab;
uint32_t swap_ret = tc::bn::detail::__local_bswap32(x);
if (swap_ret != x_inv)
{
throw tc::Exception(fmt::format("tc::bn::detail::__local_bswap32(uint32_t) returned 0x{:x} (expected: 0x{:x}", swap_ret, x_inv));
}
uint32_t x_test = x;
tc::bn::detail::__local_bswap32(&x_test);
if (x_test != x_inv)
{
throw tc::Exception(fmt::format("tc::bn::detail::__local_bswap32(void*) transformed 0x{:x} -> 0x{:x} (expected: 0x{:x}", x, x_test, x_inv));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLocalBSwap64()
{
fmt::print("[tc::bn::detail::__local_bswap64] testLocalBSwap64 : ");
try
{
uint64_t x = 0x0123456789abcdef;
uint64_t x_inv = 0xefcdab8967452301;
uint64_t swap_ret = tc::bn::detail::__local_bswap64(x);
if (swap_ret != x_inv)
{
throw tc::Exception(fmt::format("tc::bn::detail::__local_bswap64(uint64_t) returned 0x{:x} (expected: 0x{:x}", swap_ret, x_inv));
}
uint64_t x_test = x;
tc::bn::detail::__local_bswap64(&x_test);
if (x_test != x_inv)
{
throw tc::Exception(fmt::format("tc::bn::detail::__local_bswap64(void*) transformed 0x{:x} -> 0x{:x} (expected: 0x{:x}", x, x_test, x_inv));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBeUint64Inline()
{
fmt::print("[tc::bn::detail::__be_uint64] testBeUint64Inline : ");
try
{
uint8_t x_raw[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint64_t* x_raw_ptr = (uint64_t*)&x_raw;
uint64_t x_expected = 0x0123456789abcdef;
uint64_t x_ret = tc::bn::detail::__be_uint64(*x_raw_ptr);
if (x_ret != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", *x_raw_ptr, x_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBeUint32Inline()
{
fmt::print("[tc::bn::detail::__be_uint32] testBeUint32Inline : ");
try
{
uint8_t x_raw[4] = { 0x01, 0x23, 0x45, 0x67 };
uint32_t* x_raw_ptr = (uint32_t*)&x_raw;
uint32_t x_expected = 0x01234567;
uint32_t x_ret = tc::bn::detail::__be_uint32(*x_raw_ptr);
if (x_ret != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", *x_raw_ptr, x_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBeUint16Inline()
{
fmt::print("[tc::bn::detail::__be_uint16] testBeUint16Inline : ");
try
{
uint8_t x_raw[2] = { 0x01, 0x23 };
uint16_t* x_raw_ptr = (uint16_t*)&x_raw;
uint16_t x_expected = 0x0123;
uint16_t x_ret = tc::bn::detail::__be_uint16(*x_raw_ptr);
if (x_ret != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", *x_raw_ptr, x_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLeUint64Inline()
{
fmt::print("[tc::bn::detail::__le_uint64] testLeUint64Inline : ");
try
{
uint8_t x_raw[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint64_t* x_raw_ptr = (uint64_t*)&x_raw;
uint64_t x_expected = 0xefcdab8967452301;
uint64_t x_ret = tc::bn::detail::__le_uint64(*x_raw_ptr);
if (x_ret != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", *x_raw_ptr, x_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLeUint32Inline()
{
fmt::print("[tc::bn::detail::__le_uint32] testLeUint32Inline : ");
try
{
uint8_t x_raw[4] = { 0x01, 0x23, 0x45, 0x67 };
uint32_t* x_raw_ptr = (uint32_t*)&x_raw;
uint32_t x_expected = 0x67452301;
uint32_t x_ret = tc::bn::detail::__le_uint32(*x_raw_ptr);
if (x_ret != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", *x_raw_ptr, x_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLeUint16Inline()
{
fmt::print("[tc::bn::detail::__le_uint16] testLeUint16Inline : ");
try
{
uint8_t x_raw[2] = { 0x01, 0x23 };
uint16_t* x_raw_ptr = (uint16_t*)&x_raw;
uint16_t x_expected = 0x2301;
uint16_t x_ret = tc::bn::detail::__le_uint16(*x_raw_ptr);
if (x_ret != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", *x_raw_ptr, x_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBeSwap64Inline()
{
fmt::print("[tc::bn::detail::__be_swap64] testBeSwap64Inline : ");
try
{
uint8_t x_raw[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint64_t* x_raw_ptr = (uint64_t*)&x_raw;
uint64_t x_expected = 0x0123456789abcdef;
uint64_t x_before = *x_raw_ptr;
tc::bn::detail::__be_swap64(x_raw);
uint64_t x_after = *x_raw_ptr;
if (x_after != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", x_before, x_after, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBeSwap32Inline()
{
fmt::print("[tc::bn::detail::__be_swap32] testBeSwap32Inline : ");
try
{
uint8_t x_raw[4] = { 0x01, 0x23, 0x45, 0x67 };
uint32_t* x_raw_ptr = (uint32_t*)&x_raw;
uint32_t x_expected = 0x01234567;
uint32_t x_before = *x_raw_ptr;
tc::bn::detail::__be_swap32(x_raw);
uint32_t x_after = *x_raw_ptr;
if (x_after != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", x_before, x_after, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBeSwap16Inline()
{
fmt::print("[tc::bn::detail::__be_swap16] testBeSwap16Inline : ");
try
{
uint8_t x_raw[2] = { 0x01, 0x23 };
uint16_t* x_raw_ptr = (uint16_t*)&x_raw;
uint16_t x_expected = 0x0123;
uint16_t x_before = *x_raw_ptr;
tc::bn::detail::__be_swap16(x_raw);
uint16_t x_after = *x_raw_ptr;
if (x_after != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", x_before, x_after, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLeSwap64Inline()
{
fmt::print("[tc::bn::detail::__le_swap64] testLeSwap64Inline : ");
try
{
uint8_t x_raw[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint64_t* x_raw_ptr = (uint64_t*)&x_raw;
uint64_t x_expected = 0xefcdab8967452301;
uint64_t x_before = *x_raw_ptr;
tc::bn::detail::__le_swap64(x_raw);
uint64_t x_after = *x_raw_ptr;
if (x_after != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", x_before, x_after, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLeSwap32Inline()
{
fmt::print("[tc::bn::detail::__le_swap32] testLeSwap32Inline : ");
try
{
uint8_t x_raw[4] = { 0x01, 0x23, 0x45, 0x67 };
uint32_t* x_raw_ptr = (uint32_t*)&x_raw;
uint32_t x_expected = 0x67452301;
uint32_t x_before = *x_raw_ptr;
tc::bn::detail::__le_swap32(x_raw);
uint32_t x_after = *x_raw_ptr;
if (x_after != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", x_before, x_after, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLeSwap16Inline()
{
fmt::print("[tc::bn::detail::__le_swap16] testLeSwap16Inline : ");
try
{
uint8_t x_raw[2] = { 0x01, 0x23 };
uint16_t* x_raw_ptr = (uint16_t*)&x_raw;
uint16_t x_expected = 0x2301;
uint16_t x_before = *x_raw_ptr;
tc::bn::detail::__le_swap16(x_raw);
uint16_t x_after = *x_raw_ptr;
if (x_after != x_expected)
{
throw tc::Exception(fmt::format("transformed 0x{:x} -> 0x{:x} (expected 0x{:x})", x_before, x_after, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBe64TemplateClass()
{
fmt::print("[tc::bn::be64<uint64_t>] testBe64TemplateClass : ");
try
{
uint8_t x_raw[sizeof(uint64_t)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint8_t x_raw_expected[sizeof(uint64_t)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
tc::bn::be64<uint64_t>* x_raw_ptr = (tc::bn::be64<uint64_t>*)&x_raw;
uint64_t x_expected = 0x0123456789abcdef;
uint64_t unwrap_ret;
// explicit unwrap/wrap
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be64<uint64_t>::unwrap() returned 0x{:x} (expected 0x{:x}", unwrap_ret, x_expected));
}
x_raw_ptr->wrap(0);
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::be64<uint64_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0, unwrap_ret, 0));
}
x_raw_ptr->wrap(x_expected);
unwrap_ret = x_raw_ptr->unwrap();
if (memcmp(x_raw, x_raw_expected, sizeof(uint64_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be64<uint64_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
// implicit unwrap/wrap
x_raw_ptr = (tc::bn::be64<uint64_t>*)&x_raw;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be64<uint64_t> implicit unwrap returned 0x{:x} (expected 0x{:x})", unwrap_ret, x_expected));
}
(*x_raw_ptr) = 0;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::be64<uint64_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0x0, unwrap_ret, 0x0));
}
(*x_raw_ptr) = x_expected;
unwrap_ret = (*x_raw_ptr);
if (memcmp(x_raw, x_raw_expected, sizeof(uint64_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be64<uint64_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBe32TemplateClass()
{
fmt::print("[tc::bn::be32<uint32_t>] testBe32TemplateClass : ");
try
{
uint8_t x_raw[sizeof(uint32_t)] = { 0x01, 0x23, 0x45, 0x67 };
uint8_t x_raw_expected[sizeof(uint32_t)] = { 0x01, 0x23, 0x45, 0x67 };
tc::bn::be32<uint32_t>* x_raw_ptr = (tc::bn::be32<uint32_t>*)&x_raw;
uint32_t x_expected = 0x01234567;
uint32_t unwrap_ret;
// explicit unwrap/wrap
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be32<uint32_t>::unwrap() returned 0x{:x} (expected 0x{:x}", unwrap_ret, x_expected));
}
x_raw_ptr->wrap(0);
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::be32<uint32_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0, unwrap_ret, 0));
}
x_raw_ptr->wrap(x_expected);
unwrap_ret = x_raw_ptr->unwrap();
if (memcmp(x_raw, x_raw_expected, sizeof(uint32_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be32<uint32_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
// implicit unwrap/wrap
x_raw_ptr = (tc::bn::be32<uint32_t>*)&x_raw;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be32<uint32_t> implicit unwrap returned 0x{:x} (expected 0x{:x})", unwrap_ret, x_expected));
}
(*x_raw_ptr) = 0;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::be32<uint32_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0x0, unwrap_ret, 0x0));
}
(*x_raw_ptr) = x_expected;
unwrap_ret = (*x_raw_ptr);
if (memcmp(x_raw, x_raw_expected, sizeof(uint32_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be32<uint32_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testBe16TemplateClass()
{
fmt::print("[tc::bn::be16<uint16_t>] testBe16TemplateClass : ");
try
{
uint8_t x_raw[sizeof(uint16_t)] = { 0x01, 0x23 };
uint8_t x_raw_expected[sizeof(uint16_t)] = { 0x01, 0x23 };
tc::bn::be16<uint16_t>* x_raw_ptr = (tc::bn::be16<uint16_t>*)&x_raw;
uint16_t x_expected = 0x0123;
uint16_t unwrap_ret;
// explicit unwrap/wrap
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be16<uint16_t>::unwrap() returned 0x{:x} (expected 0x{:x}", unwrap_ret, x_expected));
}
x_raw_ptr->wrap(0);
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::be16<uint16_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0, unwrap_ret, 0));
}
x_raw_ptr->wrap(x_expected);
unwrap_ret = x_raw_ptr->unwrap();
if (memcmp(x_raw, x_raw_expected, sizeof(uint16_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be16<uint16_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
// implicit unwrap/wrap
x_raw_ptr = (tc::bn::be16<uint16_t>*)&x_raw;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be16<uint16_t> implicit unwrap returned 0x{:x} (expected 0x{:x})", unwrap_ret, x_expected));
}
(*x_raw_ptr) = 0;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::be16<uint16_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0x0, unwrap_ret, 0x0));
}
(*x_raw_ptr) = x_expected;
unwrap_ret = (*x_raw_ptr);
if (memcmp(x_raw, x_raw_expected, sizeof(uint16_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::be16<uint16_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLe64TemplateClass()
{
fmt::print("[tc::bn::le64<uint64_t>] testLe64TemplateClass : ");
try
{
uint8_t x_raw[sizeof(uint64_t)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint8_t x_raw_expected[sizeof(uint64_t)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
tc::bn::le64<uint64_t>* x_raw_ptr = (tc::bn::le64<uint64_t>*)&x_raw;
uint64_t x_expected = 0xefcdab8967452301;
uint64_t unwrap_ret;
// explicit unwrap/wrap
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le64<uint64_t>::unwrap() returned 0x{:x} (expected 0x{:x}", unwrap_ret, x_expected));
}
x_raw_ptr->wrap(0);
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::le64<uint64_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0, unwrap_ret, 0));
}
x_raw_ptr->wrap(x_expected);
unwrap_ret = x_raw_ptr->unwrap();
if (memcmp(x_raw, x_raw_expected, sizeof(uint64_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le64<uint64_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
// implicit unwrap/wrap
x_raw_ptr = (tc::bn::le64<uint64_t>*)&x_raw;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le64<uint64_t> implicit unwrap returned 0x{:x} (expected 0x{:x})", unwrap_ret, x_expected));
}
(*x_raw_ptr) = 0;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::le64<uint64_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0x0, unwrap_ret, 0x0));
}
(*x_raw_ptr) = x_expected;
unwrap_ret = (*x_raw_ptr);
if (memcmp(x_raw, x_raw_expected, sizeof(uint64_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le64<uint64_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLe32TemplateClass()
{
fmt::print("[tc::bn::le32<uint32_t>] testLe32TemplateClass : ");
try
{
uint8_t x_raw[sizeof(uint32_t)] = { 0x01, 0x23, 0x45, 0x67 };
uint8_t x_raw_expected[sizeof(uint32_t)] = { 0x01, 0x23, 0x45, 0x67 };
tc::bn::le32<uint32_t>* x_raw_ptr = (tc::bn::le32<uint32_t>*)&x_raw;
uint32_t x_expected = 0x67452301;
uint32_t unwrap_ret;
// explicit unwrap/wrap
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le32<uint32_t>::unwrap() returned 0x{:x} (expected 0x{:x}", unwrap_ret, x_expected));
}
x_raw_ptr->wrap(0);
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::le32<uint32_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0, unwrap_ret, 0));
}
x_raw_ptr->wrap(x_expected);
unwrap_ret = x_raw_ptr->unwrap();
if (memcmp(x_raw, x_raw_expected, sizeof(uint32_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le32<uint32_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
// implicit unwrap/wrap
x_raw_ptr = (tc::bn::le32<uint32_t>*)&x_raw;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le32<uint32_t> implicit unwrap returned 0x{:x} (expected 0x{:x})", unwrap_ret, x_expected));
}
(*x_raw_ptr) = 0;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::le32<uint32_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0x0, unwrap_ret, 0x0));
}
(*x_raw_ptr) = x_expected;
unwrap_ret = (*x_raw_ptr);
if (memcmp(x_raw, x_raw_expected, sizeof(uint32_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le32<uint32_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
void bn_endian_TestClass::testLe16TemplateClass()
{
fmt::print("[tc::bn::le16<uint16_t>] testLe16TemplateClass : ");
try
{
uint8_t x_raw[sizeof(uint16_t)] = { 0x01, 0x23 };
uint8_t x_raw_expected[sizeof(uint16_t)] = { 0x01, 0x23 };
tc::bn::le16<uint16_t>* x_raw_ptr = (tc::bn::le16<uint16_t>*)&x_raw;
uint16_t x_expected = 0x2301;
uint16_t unwrap_ret;
// explicit unwrap/wrap
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le16<uint16_t>::unwrap() returned 0x{:x} (expected 0x{:x}", unwrap_ret, x_expected));
}
x_raw_ptr->wrap(0);
unwrap_ret = x_raw_ptr->unwrap();
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::le16<uint16_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0, unwrap_ret, 0));
}
x_raw_ptr->wrap(x_expected);
unwrap_ret = x_raw_ptr->unwrap();
if (memcmp(x_raw, x_raw_expected, sizeof(uint16_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le16<uint16_t>::wrap() failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
// implicit unwrap/wrap
x_raw_ptr = (tc::bn::le16<uint16_t>*)&x_raw;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le16<uint16_t> implicit unwrap returned 0x{:x} (expected 0x{:x})", unwrap_ret, x_expected));
}
(*x_raw_ptr) = 0;
unwrap_ret = (*x_raw_ptr);
if (unwrap_ret != 0)
{
throw tc::Exception(fmt::format("tc::bn::le16<uint16_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", 0x0, unwrap_ret, 0x0));
}
(*x_raw_ptr) = x_expected;
unwrap_ret = (*x_raw_ptr);
if (memcmp(x_raw, x_raw_expected, sizeof(uint16_t)) != 0 || unwrap_ret != x_expected)
{
throw tc::Exception(fmt::format("tc::bn::le16<uint16_t> implicit wrap failed to wrap 0x{:x} (unwrap returned 0x{:x}, expected 0x{:x})", x_expected, unwrap_ret, x_expected));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
@@ -1,32 +0,0 @@
#pragma once
#include "ITestClass.h"
class bn_endian_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void testLocalBSwap16();
void testLocalBSwap32();
void testLocalBSwap64();
void testBeUint64Inline();
void testBeUint32Inline();
void testBeUint16Inline();
void testLeUint64Inline();
void testLeUint32Inline();
void testLeUint16Inline();
void testBeSwap64Inline();
void testBeSwap32Inline();
void testBeSwap16Inline();
void testLeSwap64Inline();
void testLeSwap32Inline();
void testLeSwap16Inline();
void testBe64TemplateClass();
void testBe32TemplateClass();
void testBe16TemplateClass();
void testLe64TemplateClass();
void testLe32TemplateClass();
void testLe16TemplateClass();
};
@@ -1,55 +0,0 @@
#include <tc/Exception.h>
#include <iostream>
#include "bn_pad_TestClass.h"
void bn_pad_TestClass::runAllTests(void)
{
std::cout << "[tc::bn::pad] START" << std::endl;
test_CorrectSize();
std::cout << "[tc::bn::pad] END" << std::endl;
}
void bn_pad_TestClass::test_CorrectSize()
{
std::cout << "[tc::bn::pad] test_CorrectSize : " << std::flush;
try
{
try
{
tc::bn::pad<5> test_pad0;
if (sizeof(test_pad0) != 5)
{
throw tc::Exception("tc::bn::pad<5> had incorrect sizeof()");
}
if (test_pad0.size() != 5)
{
throw tc::Exception("tc::bn::pad<5> had incorrect pad::size() result");
}
tc::bn::pad<0x200> test_pad1;
if (sizeof(test_pad1) != 0x200)
{
throw tc::Exception("tc::bn::pad<0x200> had incorrect sizeof()");
}
if (test_pad1.size() != 0x200)
{
throw tc::Exception("tc::bn::pad<0x200> had incorrect pad::size() result");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,14 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/bn.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/NotImplementedException.h>
class bn_pad_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_CorrectSize();
};
@@ -1,219 +0,0 @@
#include <tc/Exception.h>
#include <fmt/core.h>
#include "bn_string_TestClass.h"
void bn_string_TestClass::runAllTests(void)
{
fmt::print("[tc::bn::string] START\n");
test_EncodedSizeVersusLogicalSize();
test_StringSizeNeverExceedsLogicalSize();
test_EncodeStringRespectsLogicalSize();
fmt::print("[tc::bn::string] END\n");
}
void bn_string_TestClass::test_EncodedSizeVersusLogicalSize()
{
fmt::print("[tc::bn::string] test_EncodedSizeVersusLogicalSize : ");
try
{
try
{
// create bn::string with encoded size of 16 bytes (and implicit logical size of 16)
{
static const size_t kEncodedSize = 16;
tc::bn::string<kEncodedSize> bn_string;
if (sizeof(bn_string) != kEncodedSize)
{
throw tc::Exception(fmt::format("sizeof(tc::bn::string<{:d}>) did not return ENCODED_SIZE ({:d})", kEncodedSize, kEncodedSize));
}
if (bn_string.max_size() != kEncodedSize)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}>::max_size() did not return LOGICAL_SIZE ({:d})", kEncodedSize, kEncodedSize));
}
}
// create bn::string with encoded size of 16 bytes and explicit logical size of 16
{
static const size_t kEncodedSize = 16;
static const size_t kLogicalSize = 16;
tc::bn::string<kEncodedSize, kLogicalSize> bn_string;
if (sizeof(bn_string) != kEncodedSize)
{
throw tc::Exception(fmt::format("sizeof(tc::bn::string<{:d}, {:d}>) did not return ENCODED_SIZE ({:d})", kEncodedSize, kLogicalSize, kEncodedSize));
}
if (bn_string.max_size() != kLogicalSize)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::max_size() did not return LOGICAL_SIZE ({:d})", kEncodedSize, kLogicalSize, kLogicalSize));
}
}
// create bn::string with encoded size of 16 bytes and explicit logical size of 8
{
static const size_t kEncodedSize = 16;
static const size_t kLogicalSize = 8;
tc::bn::string<kEncodedSize, kLogicalSize> bn_string;
if (sizeof(bn_string) != kEncodedSize)
{
throw tc::Exception(fmt::format("sizeof(tc::bn::string<{:d}, {:d}>) did not return ENCODED_SIZE ({:d})", kEncodedSize, kLogicalSize, kEncodedSize));
}
if (bn_string.max_size() != kLogicalSize)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::max_size() did not return LOGICAL_SIZE ({:d})", kEncodedSize, kLogicalSize, kLogicalSize));
}
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void bn_string_TestClass::test_StringSizeNeverExceedsLogicalSize()
{
fmt::print("[tc::bn::string] test_StringSizeNeverExceedsLogicalSize : ");
try
{
try
{
union {
std::array<char, 30> char_array;
tc::bn::string<20,20> str_2020;
tc::bn::string<20,10> str_2010;
tc::bn::string<20,05> str_2005;
} test;
// fill underlying data with different characters
for (char chr_idx = 0; chr_idx < test.char_array.size(); chr_idx++)
{
test.char_array[chr_idx] = ((chr_idx / 26) & 1) ? ('a' + (chr_idx % 26)) : ('A' + (chr_idx % 26));
}
// generate reference point string
std::string base_string = std::string(test.char_array.data(), 30);
if (test.str_2020.size() != 20)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::size() did not respect LOGICAL_SIZE ({:d})", 20, 20, 20));
}
if (test.str_2020.data() != test.char_array.data())
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::data() did not return expected pointer)", 20, 20));
}
if (test.str_2020.decode() != base_string.substr(0, 20))
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::decode() returned \"{:s}\" (expected \"{:s}\")", 20, 20, test.str_2020.decode(), base_string.substr(0, 20)));
}
if (test.str_2010.size() != 10)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::size() did not respect LOGICAL_SIZE ({:d})", 20, 10, 10));
}
if (test.str_2010.data() != test.char_array.data())
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::data() did not return expected pointer)", 20, 10));
}
if (test.str_2010.decode() != base_string.substr(0, 10))
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::decode() returned \"{:s}\" (expected \"{:s}\")", 20, 10, test.str_2010.decode(), base_string.substr(0, 10)));
}
if (test.str_2005.size() != 5)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::size() did not respect LOGICAL_SIZE ({:d})", 20, 5, 5));
}
if (test.str_2005.data() != test.char_array.data())
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::data() did not return expected pointer)", 20, 05));
}
if (test.str_2005.decode() != base_string.substr(0, 5))
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::decode() returned \"{:s}\" (expected \"{:s}\")", 20, 5, test.str_2005.decode(), base_string.substr(0, 5)));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void bn_string_TestClass::test_EncodeStringRespectsLogicalSize()
{
fmt::print("[tc::bn::string] test_EncodeStringRespectsLogicalSize : ");
try
{
try
{
union {
std::array<char, 30> char_array;
tc::bn::string<20,20> str_2020;
} test;
std::array<char, 30> empty_array;
// clear memory
memset(test.char_array.data(), 0, test.char_array.size());
memset(empty_array.data(), 0, empty_array.size());
// generate reference point string
std::string base_long_string = std::string("I am a very long string indeed");
std::string base_short_string = std::string("Smol str");
test.str_2020.encode(base_long_string);
if (test.str_2020.decode() != base_long_string.substr(0, 20))
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::encode() failed encode a string greater ({:d}) than the LOGICAL_SIZE", 20, 20, base_long_string.size()));
}
if (memcmp(test.char_array.data() + 20, empty_array.data(), 10) != 0)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::encode() touched data after the LOGICAL_SIZE boundary", 20, 20));
}
test.str_2020.encode(base_short_string);
if (test.str_2020.decode() != base_short_string)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::encode() failed encode a string smaller ({:d}) than the LOGICAL_SIZE)", 20, 20, base_short_string.size()));
}
if (memcmp(test.char_array.data() + base_short_string.size(), empty_array.data(), 20-base_short_string.size()) != 0)
{
throw tc::Exception(fmt::format("tc::bn::string<{:d}, {:d}>::encode() did not clear data after string ended", 20, 20));
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
@@ -1,16 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/bn.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/NotImplementedException.h>
class bn_string_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_EncodedSizeVersusLogicalSize();
void test_StringSizeNeverExceedsLogicalSize();
void test_EncodeStringRespectsLogicalSize();
};
@@ -1,366 +0,0 @@
#include "cli_FormatUtil_TestClass.h"
#include <iostream>
#include <iomanip>
#include <sstream>
void cli_FormatUtil_TestClass::runAllTests()
{
std::cout << "[tc::cli::FormatUtil] START" << std::endl;
testHexStringToBytes();
testFormatBytesAsString();
testFormatBytesAsStringWithLineLimit();
testFormatListWithLineLimit();
testFormatBytesAsHxdHexString();
std::cout << "[tc::cli::FormatUtil] END" << std::endl;
}
void cli_FormatUtil_TestClass::testHexStringToBytes()
{
std::cout << "[tc::cli::FormatUtil] testHexStringToBytes : " << std::flush;
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_data;
};
// create manual tests
std::vector<TestCase> tests =
{
{ "empty string", "", tc::ByteData()},
{ "unaligned string" ,"1", tc::ByteData()},
{ "unaligned larger string" ,"123456789", tc::ByteData()},
{ "multi-byte" ,"00112233445566778899aabbccddeeff010203", tc::ByteData({0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x02, 0x03})},
};
// add programatically determined tests
for (size_t i = 0; i < 0xff; i++)
{
std::string all_lower, all_upper, upper_lower, lower_upper;
size_t upper_num = (i >> 4) & 0xf;
size_t lower_num = (i) & 0xf;
if (upper_num <= 9)
{
all_lower += char('0' + upper_num);
all_upper += char('0' + upper_num);
upper_lower += char('0' + upper_num);
lower_upper += char('0' + upper_num);
}
else
{
all_lower += char('a' + upper_num - 10);
all_upper += char('A' + upper_num - 10);
upper_lower += char('A' + upper_num - 10);
lower_upper += char('a' + upper_num - 10);
}
if (lower_num <= 9)
{
all_lower += char('0' + lower_num);
all_upper += char('0' + lower_num);
upper_lower += char('0' + lower_num);
lower_upper += char('0' + lower_num);
}
else
{
all_lower += char('a' + lower_num - 10);
all_upper += char('A' + lower_num - 10);
upper_lower += char('a' + lower_num - 10);
lower_upper += char('A' + lower_num - 10);
}
tests.push_back({"all_lower_" + all_lower, all_lower, tc::ByteData({(byte_t)i})});
tests.push_back({"all_upper_" + all_lower, all_upper, tc::ByteData({(byte_t)i})});
tests.push_back({"upper_lower_" + all_lower, upper_lower, tc::ByteData({(byte_t)i})});
tests.push_back({"lower_upper_" + all_lower, lower_upper, tc::ByteData({(byte_t)i})});
}
// run tests
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData out = tc::cli::FormatUtil::hexStringToBytes(test->in_string);
if (out.size() != test->out_data.size())
{
ss << "Test(" << test->test_name << ") to convert str(" << test->in_string << ") returned ByteData with wrong size: " << out.size() << " (should be: " << test->out_data.size() << ")";
throw tc::Exception(ss.str());
}
if (out.size() != 0 && memcmp(out.data(), test->out_data.data(), out.size()) != 0)
{
ss << "Test(" << test->test_name << ") to convert str(" << test->in_string << ") returned ByteData with wrong data";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
void cli_FormatUtil_TestClass::testFormatBytesAsString()
{
std::cout << "[tc::cli::FormatUtil] testFormatBytesAsString : " << std::flush;
try
{
std::stringstream ss;
// test recipe
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
bool in_is_uppercase;
std::string in_delimiter;
std::string out_string;
};
// create tests
std::vector<TestCase> tests =
{
{"empty data", tc::ByteData(), false, "", ""},
{"8byte lowercase no delim", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), false, "", "00aa11bb22cc33dd"},
{"8byte lowercase ' ' delim", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), false, " ", "00 aa 11 bb 22 cc 33 dd"},
{"8byte lowercase ':' delim", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), false, ":", "00:aa:11:bb:22:cc:33:dd"},
{"8byte uppercase no delim", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), true, "", "00AA11BB22CC33DD"},
{"8byte uppercase ' ' delim", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), true, " ", "00 AA 11 BB 22 CC 33 DD"},
{"8byte uppercase ':' delim", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), true, ":", "00:AA:11:BB:22:CC:33:DD"},
};
// run tests
for (auto test = tests.begin(); test != tests.end(); test++)
{
std::string res = tc::cli::FormatUtil::formatBytesAsString(test->in_data.data(), test->in_data.size(), test->in_is_uppercase, test->in_delimiter);
if (res != test->out_string)
{
ss << "Test(" << test->test_name << ") Failed to format data correctly. Output: \"" << res << "\", Expected: \"" << test->out_string << "\"";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
void cli_FormatUtil_TestClass::testFormatBytesAsStringWithLineLimit()
{
std::cout << "[tc::cli::FormatUtil] testFormatBytesAsStringWithLineLimit : " << std::flush;
try
{
std::stringstream ss;
// test recipe
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
size_t in_row_len;
size_t in_indent_len;
std::string out_string;
};
// create tests
std::vector<TestCase> tests =
{
{"empty data", tc::ByteData(), 0, 0, ""},
{"size:8 row:8 indent:0", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), 8, 0, "00aa11bb22cc33dd\n"},
{"size:8 row:4 indent:0", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), 4, 0, "00aa11bb\n22cc33dd\n"},
{"size:8 row:3 indent:0", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), 3, 0, "00aa11\nbb22cc\n33dd\n"},
{"size:8 row:3 indent:2", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), 3, 2, " 00aa11\n bb22cc\n 33dd\n"},
{"size:8 row:1 indent:3", tc::ByteData({0x00, 0xaa, 0x11, 0xbb, 0x22, 0xcc, 0x33, 0xdd}), 1, 3, " 00\n aa\n 11\n bb\n 22\n cc\n 33\n dd\n"},
};
// run tests
for (auto test = tests.begin(); test != tests.end(); test++)
{
std::string res = tc::cli::FormatUtil::formatBytesAsStringWithLineLimit(test->in_data.data(), test->in_data.size(), false, "", test->in_row_len, test->in_indent_len);
if (res != test->out_string)
{
std::string& expected = test->out_string;
// replace literal new lines so they can be printed on one line for debugging
for (size_t pos = res.find('\n', 0); pos != std::string::npos; pos = res.find('\n', pos+1))
{
res.replace(pos, 1, "\\n");
}
// replace literal new lines so they can be printed on one line for debugging
for (size_t pos = expected.find('\n', 0); pos != std::string::npos; pos = expected.find('\n', pos+1))
{
expected.replace(pos, 1, "\\n");
}
ss << "Test(" << test->test_name << ") Failed to format data correctly. Output: \"" << res << "\", Expected: \"" << expected << "\"";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
void cli_FormatUtil_TestClass::testFormatListWithLineLimit()
{
std::cout << "[tc::cli::FormatUtil] testFormatListWithLineLimit : " << std::flush;
try
{
std::stringstream ss;
// test recipe
struct TestCase
{
std::string test_name;
std::vector<std::string> in_list;
size_t in_row_len;
size_t in_indent_len;
std::string out_string;
};
// create tests
std::vector<TestCase> tests =
{
{"empty", {}, 0, 0, ""},
{"empty list", {}, 40, 0, ""},
{"list of 4, row_len 20", {"Astr", "Bstr", "Cstr", "Dstr"}, 20, 0, "Astr, Bstr, Cstr, Dstr\n"},
{"list of 4, row_len 8", {"Astr", "Bstr", "Cstr", "Dstr"}, 8, 0, "Astr, Bstr, \nCstr, Dstr\n"},
{"list of 4, row_len 8, indent=2", {"Astr", "Bstr", "Cstr", "Dstr"}, 8, 2, " Astr, Bstr, \n Cstr, Dstr\n"},
{"list of 4, row_len 4", {"Astr", "Bstr", "Cstr", "Dstr"}, 4, 0, "Astr, \nBstr, \nCstr, \nDstr\n"},
{"list of 4, row_len 2", {"Astr", "Bstr", "Cstr", "Dstr"}, 2, 0, "Astr, \nBstr, \nCstr, \nDstr\n"},
{"list of 4, row_len 1", {"Astr", "Bstr", "Cstr", "Dstr"}, 1, 0, "Astr, \nBstr, \nCstr, \nDstr\n"},
{"list of 4, row_len 0", {"Astr", "Bstr", "Cstr", "Dstr"}, 0, 0, "Astr, \nBstr, \nCstr, \nDstr\n"},
};
// run tests
for (auto test = tests.begin(); test != tests.end(); test++)
{
std::string res = tc::cli::FormatUtil::formatListWithLineLimit(test->in_list, test->in_row_len, test->in_indent_len);
if (res != test->out_string)
{
std::string& expected = test->out_string;
// replace literal new lines so they can be printed on one line for debugging
for (size_t pos = res.find('\n', 0); pos != std::string::npos; pos = res.find('\n', pos+1))
{
res.replace(pos, 1, "\\n");
}
// replace literal new lines so they can be printed on one line for debugging
for (size_t pos = expected.find('\n', 0); pos != std::string::npos; pos = expected.find('\n', pos+1))
{
expected.replace(pos, 1, "\\n");
}
ss << "Test(" << test->test_name << ") Failed to format data correctly. Output: \"" << res << "\", Expected: \"" << expected << "\"";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
void cli_FormatUtil_TestClass::testFormatBytesAsHxdHexString()
{
std::cout << "[tc::cli::FormatUtil] testFormatBytesAsHxdHexString : " << std::flush;
try
{
std::stringstream ss;
// test recipe
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
size_t in_bytes_per_row;
size_t in_byte_group_size;
std::string out_string;
};
// create tests
std::vector<TestCase> tests =
{
{"empty all", {}, 0, 0, ""},
{"empty data", {}, 16, 1, ""},
{"empty bytes_per_row", {0x00, 0x01}, 0, 1, ""},
{"empty byte_group_size", {0x00, 0x01}, 16, 0, ""},
{"little data", {0x00, 0x01}, 16, 1, "00000000 | 00 01 .. \n"},
{"full row data", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 1, "00000000 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................\n"},
{"2 row ascii", {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66}, 16, 1, "00000000 | 41 42 43 44 45 46 47 48 49 4A 4B 4C 4E 4F 50 51 ABCDEFGHIJKLNOPQ\n00000010 | 51 52 53 54 55 56 57 58 59 5A 61 62 63 64 65 66 QRSTUVWXYZabcdef\n"},
{"full row data, byte_group_size=2", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 2, "00000000 | 0001 0203 0405 0607 0809 0A0B 0C0D 0E0F ................\n"},
{"full row data, byte_group_size=3", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 3, "00000000 | 000102 030405 060708 090A0B 0C0D0E 0F ................\n"},
{"full row data, byte_group_size=4", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 4, "00000000 | 00010203 04050607 08090A0B 0C0D0E0F ................\n"},
{"full row data, byte_group_size=5", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 5, "00000000 | 0001020304 0506070809 0A0B0C0D0E 0F ................\n"},
{"full row data, byte_group_size=6", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 6, "00000000 | 000102030405 060708090A0B 0C0D0E0F ................\n"},
{"full row data, byte_group_size=7", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 7, "00000000 | 00010203040506 0708090A0B0C0D 0E0F ................\n"},
{"full row data, byte_group_size=8", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 8, "00000000 | 0001020304050607 08090A0B0C0D0E0F ................\n"},
{"full row data, byte_group_size=9", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 9, "00000000 | 000102030405060708 090A0B0C0D0E0F ................\n"},
{"full row data, byte_group_size=10", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 10, "00000000 | 00010203040506070809 0A0B0C0D0E0F ................\n"},
{"full row data, byte_group_size=11", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 11, "00000000 | 000102030405060708090A 0B0C0D0E0F ................\n"},
{"full row data, byte_group_size=12", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 12, "00000000 | 000102030405060708090A0B 0C0D0E0F ................\n"},
{"full row data, byte_group_size=13", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 13, "00000000 | 000102030405060708090A0B0C 0D0E0F ................\n"},
{"full row data, byte_group_size=14", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 14, "00000000 | 000102030405060708090A0B0C0D 0E0F ................\n"},
{"full row data, byte_group_size=15", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 15, "00000000 | 000102030405060708090A0B0C0D0E 0F ................\n"},
{"full row data, byte_group_size=16", {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16, 16, "00000000 | 000102030405060708090A0B0C0D0E0F ................\n"},
};
// run tests
for (auto test = tests.begin(); test != tests.end(); test++)
{
std::string res = tc::cli::FormatUtil::formatBytesAsHxdHexString(test->in_data.data(), test->in_data.size(), test->in_bytes_per_row, test->in_byte_group_size);
if (res != test->out_string)
{
std::string& expected = test->out_string;
// replace literal new lines so they can be printed on one line for debugging
for (size_t pos = res.find('\n', 0); pos != std::string::npos; pos = res.find('\n', pos+1))
{
res.replace(pos, 1, "\\n");
}
// replace literal new lines so they can be printed on one line for debugging
for (size_t pos = expected.find('\n', 0); pos != std::string::npos; pos = expected.find('\n', pos+1))
{
expected.replace(pos, 1, "\\n");
}
ss << "Test(" << test->test_name << ") Failed to format data correctly. Output: \"" << res << "\", Expected: \"" << expected << "\"";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
@@ -1,15 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc.h>
class cli_FormatUtil_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void testHexStringToBytes();
void testFormatBytesAsString();
void testFormatBytesAsStringWithLineLimit();
void testFormatListWithLineLimit();
void testFormatBytesAsHxdHexString();
};
@@ -1,770 +0,0 @@
#include <tc/Exception.h>
#include <fmt/core.h>
#include "cli_OptionParser_TestClass.h"
//---------------------------------------------------------
void cli_OptionParser_TestClass::runAllTests(void)
{
fmt::print("[tc::cli::OptionParser] START\n");
test_Constructor_DefaultConstructor();
test_ProcessNoOptionsWithNoHandlers();
test_ProcessOptionsWithNoHandlers();
test_ProcessOptionsWithOnlyUnkHandler();
test_ProcessOptionsWithLiteralHandlers();
test_ProcessOptionsWithRegexHandlers();
test_ProcessOptionsWithLiteralAndRegexHandlers();
test_NullHandlerSupplied();
test_RegularHandlerProvidesNoOptionLiteralOrRegex();
test_ProcessMalformedOptions();
fmt::print("[tc::cli::OptionParser] END\n");
}
//---------------------------------------------------------
void cli_OptionParser_TestClass::test_Constructor_DefaultConstructor()
{
fmt::print("[tc::cli::OptionParser] test_Constructor_DefaultConstructor : ");
try
{
try
{
tc::cli::OptionParser opt;
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessNoOptionsWithNoHandlers()
{
fmt::print("[tc::cli::OptionParser] test_ProcessNoOptionsWithNoHandlers : ");
try
{
try
{
tc::cli::OptionParser opt;
std::vector<std::string> args;
opt.processOptions(args);
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessOptionsWithNoHandlers()
{
fmt::print("[tc::cli::OptionParser] test_ProcessOptionsWithNoHandlers : ");
try
{
try
{
tc::cli::OptionParser opt;
std::vector<std::string> args = {"-someopt", "someparameter"};
try
{
opt.processOptions(args);
throw tc::Exception("Did not throw an ArgumentException for unhandled option");
}
catch (const tc::ArgumentException&) { /* do nothing */ }
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessOptionsWithOnlyUnkHandler()
{
fmt::print("[tc::cli::OptionParser] test_ProcessOptionsWithOnlyUnkHandler : ");
try
{
enum class TestResult
{
Success,
DidNotUseUnkOptionHandler,
DidNotPassOptionsAndParameters
};
class UnkOptionHandler : public tc::cli::OptionParser::IOptionHandler
{
public:
UnkOptionHandler(TestResult& external_result) :
external_result(external_result)
{}
// this throws an exception as it should not be called
const std::vector<std::string>& getOptionStrings() const
{
throw tc::Exception("getOptionStrings() not defined for UnkOptionHandler.");
}
// this throws an exception as it should not be called
const std::vector<std::string>& getOptionRegexPatterns() const
{
throw tc::Exception("getOptionRegexPatterns() not defined for UnkOptionHandler.");
}
void processOption(const std::string& option, const std::vector<std::string>& params)
{
if (option == "-someopt" && params.size() == 1 && params[0] == "someparameter")
{
external_result = TestResult::Success;
}
else
{
external_result = TestResult::DidNotPassOptionsAndParameters;
}
}
private:
TestResult& external_result;
};
try
{
tc::cli::OptionParser opt;
TestResult result = TestResult::DidNotUseUnkOptionHandler;
opt.registerUnrecognisedOptionHandler(std::make_shared<UnkOptionHandler>(UnkOptionHandler(result)));
std::vector<std::string> args = {"-someopt", "someparameter"};
opt.processOptions(args);
if (result == TestResult::Success)
{
// all good
}
else if (result == TestResult::DidNotUseUnkOptionHandler)
{
throw tc::Exception("Unrecognised option handler was registered but not used.");
}
else if (result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Unrecognised option handler was registered but the option & parameter were not passed to it correctly.");
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessOptionsWithLiteralHandlers()
{
fmt::print("[tc::cli::OptionParser] test_ProcessOptionsWithLiteralHandlers : ");
try
{
enum class TestResult
{
Success,
DidNotUseOptionHandler,
DidNotPassOptionsAndParameters
};
class TestOptionHandler : public tc::cli::OptionParser::IOptionHandler
{
public:
// The constructor is where you link the object to the state you want to modify in the call-back
TestOptionHandler(TestResult& external_result, const std::string& opt_string, const std::vector<std::string>& expected_params) :
mExternalResult(external_result),
mOptStrings({opt_string}),
mOptRegex(),
mExpectedParams(expected_params)
{}
const std::vector<std::string>& getOptionStrings() const
{
return mOptStrings;
}
const std::vector<std::string>& getOptionRegexPatterns() const
{
return mOptRegex;
}
void processOption(const std::string& option, const std::vector<std::string>& params)
{
if (option == mOptStrings[0] && params == mExpectedParams)
{
mExternalResult = TestResult::Success;
}
else
{
mExternalResult = TestResult::DidNotPassOptionsAndParameters;
}
}
private:
TestResult& mExternalResult;
std::vector<std::string> mOptStrings;
std::vector<std::string> mOptRegex;
std::vector<std::string> mExpectedParams;
};
try
{
tc::cli::OptionParser opt;
TestResult flag_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(flag_result, "-flagoption", {})));
TestResult single_param_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(single_param_result, "-singleparam", {"my_param"})));
TestResult multi_param_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(multi_param_result, "-multiparam", {"my_param1", "my_param2"})));
std::vector<std::string> args = {"-flagoption", "-singleparam", "my_param", "-multiparam", "my_param1", "my_param2"};
opt.processOptions(args);
if (flag_result == TestResult::Success)
{
// all good
}
else if (flag_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for flag option) was registered but not used.");
}
else if (flag_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for flag option) was registered but the option & parameter were not passed to it correctly.");
}
if (single_param_result == TestResult::Success)
{
// all good
}
else if (single_param_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for single param option) was registered but not used.");
}
else if (single_param_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for single param option) was registered but the option & parameter were not passed to it correctly.");
}
if (multi_param_result == TestResult::Success)
{
// all good
}
else if (multi_param_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for multi param option) was registered but not used.");
}
else if (multi_param_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for multi param option) was registered but the option & parameter were not passed to it correctly.");
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessOptionsWithRegexHandlers()
{
fmt::print("[tc::cli::OptionParser] test_ProcessOptionsWithRegexHandlers : ");
try
{
enum class TestResult
{
Success,
DidNotUseOptionHandler,
DidNotPassOptionsAndParameters
};
class TestOptionHandler : public tc::cli::OptionParser::IOptionHandler
{
public:
// The constructor is where you link the object to the state you want to modify in the call-back
TestOptionHandler(TestResult& external_result, const std::string& opt_regex, const std::string& expected_option, const std::vector<std::string>& expected_params) :
mExternalResult(external_result),
mOptStrings(),
mOptRegex({opt_regex}),
mExpectedOption(expected_option),
mExpectedParams(expected_params)
{}
const std::vector<std::string>& getOptionStrings() const
{
return mOptStrings;
}
const std::vector<std::string>& getOptionRegexPatterns() const
{
return mOptRegex;
}
void processOption(const std::string& option, const std::vector<std::string>& params)
{
if (option == mExpectedOption && params == mExpectedParams)
{
mExternalResult = TestResult::Success;
}
else
{
mExternalResult = TestResult::DidNotPassOptionsAndParameters;
}
}
private:
TestResult& mExternalResult;
std::vector<std::string> mOptStrings;
std::vector<std::string> mOptRegex;
std::string mExpectedOption;
std::vector<std::string> mExpectedParams;
};
try
{
tc::cli::OptionParser opt;
TestResult flag_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(flag_result, "(-F.a.+)", "-Flag", {})));
TestResult single_param_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(single_param_result, "(-s.+)", "-singleparam", {"my_param"})));
TestResult multi_param_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(multi_param_result, "(-multiparam)", "-multiparam", {"my_param1", "my_param2"})));
std::vector<std::string> args = {"-Flag", "-singleparam", "my_param", "-multiparam", "my_param1", "my_param2"};
opt.processOptions(args);
if (flag_result == TestResult::Success)
{
// all good
}
else if (flag_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for flag option) was registered but not used.");
}
else if (flag_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for flag option) was registered but the option & parameter were not passed to it correctly.");
}
if (single_param_result == TestResult::Success)
{
// all good
}
else if (single_param_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for single param option) was registered but not used.");
}
else if (single_param_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for single param option) was registered but the option & parameter were not passed to it correctly.");
}
if (multi_param_result == TestResult::Success)
{
// all good
}
else if (multi_param_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for multi param option) was registered but not used.");
}
else if (multi_param_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for multi param option) was registered but the option & parameter were not passed to it correctly.");
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessOptionsWithLiteralAndRegexHandlers()
{
fmt::print("[tc::cli::OptionParser] test_ProcessOptionsWithLiteralAndRegexHandlers : ");
try
{
enum class TestResult
{
Success,
DidNotUseOptionHandler,
DidNotPassOptionsAndParameters
};
class TestOptionHandler : public tc::cli::OptionParser::IOptionHandler
{
public:
// The constructor is where you link the object to the state you want to modify in the call-back
TestOptionHandler(TestResult& external_result, const std::string& opt_literal, const std::string& opt_regex, const std::string& expected_option, const std::vector<std::string>& expected_params) :
mExternalResult(external_result),
mOptStrings(opt_literal.empty() ? std::vector<std::string>({}) : std::vector<std::string>({opt_literal})),
mOptRegex(opt_regex.empty() ? std::vector<std::string>({}) : std::vector<std::string>({opt_regex})),
mExpectedOption(expected_option),
mExpectedParams(expected_params)
{}
const std::vector<std::string>& getOptionStrings() const
{
return mOptStrings;
}
const std::vector<std::string>& getOptionRegexPatterns() const
{
return mOptRegex;
}
void processOption(const std::string& option, const std::vector<std::string>& params)
{
if (option == mExpectedOption && params == mExpectedParams)
{
mExternalResult = TestResult::Success;
}
else
{
mExternalResult = TestResult::DidNotPassOptionsAndParameters;
}
}
private:
TestResult& mExternalResult;
std::vector<std::string> mOptStrings;
std::vector<std::string> mOptRegex;
std::string mExpectedOption;
std::vector<std::string> mExpectedParams;
};
try
{
tc::cli::OptionParser opt;
TestResult regex_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(regex_result, "", "(-D.+)", "-DKEY", {"value"})));
TestResult flag_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(flag_result, "-flag", "", "-flag", {})));
TestResult single_param_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(single_param_result, "-singleparam", "", "-singleparam", {"my_param"})));
TestResult multi_param_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(multi_param_result, "-multiparam", "", "-multiparam", {"my_param1", "my_param2"})));
std::vector<std::string> args = {"-flag", "-DKEY=value", "-singleparam", "my_param", "-multiparam", "my_param1", "my_param2"};
opt.processOptions(args);
if (regex_result == TestResult::Success)
{
// all good
}
else if (regex_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for regex option) was registered but not used.");
}
else if (regex_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for regex option) was registered but the option & parameter were not passed to it correctly.");
}
if (flag_result == TestResult::Success)
{
// all good
}
else if (flag_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for flag option) was registered but not used.");
}
else if (flag_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for flag option) was registered but the option & parameter were not passed to it correctly.");
}
if (single_param_result == TestResult::Success)
{
// all good
}
else if (single_param_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for single param option) was registered but not used.");
}
else if (single_param_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for single param option) was registered but the option & parameter were not passed to it correctly.");
}
if (multi_param_result == TestResult::Success)
{
// all good
}
else if (multi_param_result == TestResult::DidNotUseOptionHandler)
{
throw tc::Exception("Option handler (for multi param option) was registered but not used.");
}
else if (multi_param_result == TestResult::DidNotPassOptionsAndParameters)
{
throw tc::Exception("Option handler (for multi param option) was registered but the option & parameter were not passed to it correctly.");
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_NullHandlerSupplied()
{
fmt::print("[tc::cli::OptionParser] test_NullHandlerSupplied : ");
try
{
try
{
tc::cli::OptionParser opt;
try
{
opt.registerOptionHandler(std::shared_ptr<tc::cli::OptionParser::IOptionHandler>());
throw tc::Exception(".registerOptionHandler() did not throw ArgumentNullException when passed a nullptr.");
}
catch (const tc::ArgumentNullException&)
{
// do nothing
}
try
{
opt.registerUnrecognisedOptionHandler(std::shared_ptr<tc::cli::OptionParser::IOptionHandler>());
throw tc::Exception(".registerUnrecognisedOptionHandler() did not throw ArgumentNullException when passed a nullptr.");
}
catch (const tc::ArgumentNullException&)
{
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_RegularHandlerProvidesNoOptionLiteralOrRegex()
{
fmt::print("[tc::cli::OptionParser] test_RegularHandlerProvidesNoOptionLiteralOrRegex : ");
try
{
enum class TestResult
{
Success,
DidNotUseOptionHandler,
DidNotPassOptionsAndParameters
};
class TestOptionHandler : public tc::cli::OptionParser::IOptionHandler
{
public:
// The constructor is where you link the object to the state you want to modify in the call-back
TestOptionHandler(TestResult& external_result, const std::string& opt_literal, const std::string& opt_regex, const std::string& expected_option, const std::vector<std::string>& expected_params) :
mExternalResult(external_result),
mOptStrings(opt_literal.empty() ? std::vector<std::string>({}) : std::vector<std::string>({opt_literal})),
mOptRegex(opt_regex.empty() ? std::vector<std::string>({}) : std::vector<std::string>({opt_regex})),
mExpectedOption(expected_option),
mExpectedParams(expected_params)
{}
const std::vector<std::string>& getOptionStrings() const
{
return mOptStrings;
}
const std::vector<std::string>& getOptionRegexPatterns() const
{
return mOptRegex;
}
void processOption(const std::string& option, const std::vector<std::string>& params)
{
if (option == mExpectedOption && params == mExpectedParams)
{
mExternalResult = TestResult::Success;
}
else
{
mExternalResult = TestResult::DidNotPassOptionsAndParameters;
}
}
private:
TestResult& mExternalResult;
std::vector<std::string> mOptStrings;
std::vector<std::string> mOptRegex;
std::string mExpectedOption;
std::vector<std::string> mExpectedParams;
};
try
{
tc::cli::OptionParser opt;
try
{
TestResult test_result = TestResult::DidNotUseOptionHandler;
opt.registerOptionHandler(std::make_shared<TestOptionHandler>(TestOptionHandler(test_result, "", "", "-DKEY", {"value"})));
throw tc::Exception(".registerOptionHandler() Did not throw tc::ArgumentOutOfRangeException when option handler had not option literals or option regex.");
}
catch (const tc::ArgumentOutOfRangeException&)
{
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void cli_OptionParser_TestClass::test_ProcessMalformedOptions()
{
fmt::print("[tc::cli::OptionParser] test_ProcessMalformedOptions : ");
try
{
class DummyOptionHandler : public tc::cli::OptionParser::IOptionHandler
{
public:
// The constructor is where you link the object to the state you want to modify in the call-back
DummyOptionHandler(const std::string& opt_literal, const std::string& opt_regex) :
mOptStrings(opt_literal.empty() ? std::vector<std::string>({}) : std::vector<std::string>({opt_literal})),
mOptRegex(opt_regex.empty() ? std::vector<std::string>({}) : std::vector<std::string>({opt_regex}))
{}
const std::vector<std::string>& getOptionStrings() const
{
return mOptStrings;
}
const std::vector<std::string>& getOptionRegexPatterns() const
{
return mOptRegex;
}
void processOption(const std::string& option, const std::vector<std::string>& params)
{
// do nothing
}
private:
std::vector<std::string> mOptStrings;
std::vector<std::string> mOptRegex;
};
try
{
try
{
tc::cli::OptionParser opt;
opt.processOptions({"dangling_parameter"});
throw tc::Exception(".processOptions() did not throw exception for dangling parameter");
}
catch (const tc::ArgumentException&)
{
// do nothing
}
try
{
tc::cli::OptionParser opt;
opt.registerOptionHandler(std::make_shared<DummyOptionHandler>(DummyOptionHandler("-opt","")));
opt.processOptions({"-opt=param", "dangling_parameter"});
throw tc::Exception(".processOptions() did not throw exception for dangling parameter located after compound option");
}
catch (const tc::ArgumentException&)
{
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
@@ -1,20 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/cli/OptionParser.h>
class cli_OptionParser_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constructor_DefaultConstructor();
void test_ProcessNoOptionsWithNoHandlers();
void test_ProcessOptionsWithNoHandlers();
void test_ProcessOptionsWithOnlyUnkHandler();
void test_ProcessOptionsWithLiteralHandlers();
void test_ProcessOptionsWithRegexHandlers();
void test_ProcessOptionsWithLiteralAndRegexHandlers();
void test_NullHandlerSupplied();
void test_RegularHandlerProvidesNoOptionLiteralOrRegex();
void test_ProcessMalformedOptions();
};
@@ -1,265 +0,0 @@
#include <tc/Exception.h>
#include <tc/io.h>
#include <tc/cli.h>
#include <fmt/core.h>
#include "crypto_Aes128CbcEncryptedStream_TestClass.h"
#include "StreamTestUtil.h"
void crypto_Aes128CbcEncryptedStream_TestClass::runAllTests(void)
{
fmt::print("[tc::crypto::Aes128CbcEncryptedStream] START\n");
test_CreateEmptyStream_DefaultConstructor();
test_CreateValidStream_CreateConstructor();
test_RunTestCases();
fmt::print("[tc::crypto::Aes128CbcEncryptedStream] END\n");
}
void crypto_Aes128CbcEncryptedStream_TestClass::test_CreateEmptyStream_DefaultConstructor()
{
fmt::print("[tc::crypto::Aes128CbcEncryptedStream] test_CreateEmptyStream_DefaultConstructor : ");
try
{
try
{
auto stream = tc::crypto::Aes128CbcEncryptedStream();
StreamTestUtil::constructor_TestHelper(stream, 0, 0, false, false, false);
try
{
stream.read(nullptr, 0);
throw tc::Exception(".read() did not throw tc::ObjectDisposedException for uninitialized Aes128CbcEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.write(nullptr, 0);
throw tc::Exception(".write() did not throw tc::ObjectDisposedException for uninitialized Aes128CbcEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.seek(0, tc::io::SeekOrigin::Begin);
throw tc::Exception(".seek() did not throw tc::ObjectDisposedException for uninitialized Aes128CbcEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.setLength(0);
throw tc::Exception(".setLength() did not throw tc::ObjectDisposedException for uninitialized Aes128CbcEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.flush();
throw tc::Exception(".flush() did not throw tc::ObjectDisposedException for uninitialized Aes128CbcEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void crypto_Aes128CbcEncryptedStream_TestClass::test_CreateValidStream_CreateConstructor()
{
fmt::print("[tc::crypto::Aes128CbcEncryptedStream] test_CreateValidStream_CreateConstructor : ");
try
{
try
{
tc::crypto::Aes128CbcEncryptedStream::key_t key;
tc::crypto::Aes128CbcEncryptedStream::iv_t iv;
std::shared_ptr<tc::io::IStream> base_stream;
base_stream = std::shared_ptr<tc::io::MemoryStream>(new tc::io::MemoryStream(tc::ByteData(0x100)));
auto stream = tc::crypto::Aes128CbcEncryptedStream(base_stream, key, iv);
try
{
stream.write(nullptr, 0);
throw tc::Exception(".write() did not throw tc::NotImplementedException for initialized Aes128CbcEncryptedStream");
}
catch (tc::NotImplementedException&) {
// do nothing
}
try
{
stream.setLength(0);
throw tc::Exception(".setLength() did not throw tc::NotImplementedException for initialized Aes128CbcEncryptedStream");
}
catch (tc::NotImplementedException&) {
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void crypto_Aes128CbcEncryptedStream_TestClass::test_RunTestCases()
{
fmt::print("[tc::crypto::Aes128CbcEncryptedStream] test_RunTestCases : ");
try
{
try
{
// get test cases
std::vector<crypto_Aes128CbcEncryptedStream_TestClass::TestCase> test_cases;
util_Setup_TestCases(test_cases);
for (auto itr = test_cases.begin(); itr != test_cases.end(); itr++)
{
tc::crypto::Aes128CbcEncryptedStream::key_t key;
memcpy(key.data(), itr->key.data(), itr->key.size());
tc::crypto::Aes128CbcEncryptedStream::iv_t iv;
memcpy(iv.data(), itr->iv.data(), itr->iv.size());
std::shared_ptr<tc::io::IStream> base_stream;
base_stream = std::shared_ptr<tc::io::MemoryStream>(new tc::io::MemoryStream(itr->ciphertext));
auto stream = tc::crypto::Aes128CbcEncryptedStream(base_stream, key, iv);
try
{
StreamTestUtil::constructor_TestHelper(stream, itr->ciphertext.size(), 0, true, false, true);
StreamTestUtil::read_TestHelper(stream, itr->read_offset, tc::io::SeekOrigin::Begin, itr->read_size, itr->read_size, itr->read_plaintext.size(), itr->read_offset + int64_t(itr->read_size), itr->read_plaintext.data());
}
catch (const tc::Exception& e)
{
throw tc::Exception(fmt::format("{} Failed: {}", itr->test_name, e.error()));
}
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void crypto_Aes128CbcEncryptedStream_TestClass::util_Setup_TestCases(std::vector<crypto_Aes128CbcEncryptedStream_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("2b7e151628aed2a6abf7158809cf4f3c");
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tc::ByteData plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
tc::ByteData ciphertext = tc::cli::FormatUtil::hexStringToBytes("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7");
tmp.test_name = "Test 1";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x00;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x10;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x20;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x30;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test read all blocks";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x0;
tmp.read_size = 0x40;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test un-aligned read over blocks 1-2";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x17;
tmp.read_size = 0x11;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test un-aligned read over blocks 0-3";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x07;
tmp.read_size = 0x31;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test partial block 0 read";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x2;
tmp.read_size = 0x8;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test partial block 1 read";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x12;
tmp.read_size = 0x8;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test partial block 3 read";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x32;
tmp.read_size = 0x8;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
}
@@ -1,29 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/crypto.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/NotImplementedException.h>
class crypto_Aes128CbcEncryptedStream_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_CreateEmptyStream_DefaultConstructor();
void test_CreateValidStream_CreateConstructor();
void test_RunTestCases();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
tc::ByteData ciphertext;
int64_t read_offset;
size_t read_size;
tc::ByteData read_plaintext;
};
void util_Setup_TestCases(std::vector<crypto_Aes128CbcEncryptedStream_TestClass::TestCase>& test_cases);
};
@@ -1,570 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes128CbcEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes128CbcEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes128CbcEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes128CbcEncryptor] END" << std::endl;
}
void crypto_Aes128CbcEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes128CbcEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes128CbcEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 16;
if (tc::crypto::Aes128CbcEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes128CbcEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CbcEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CbcEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes128Cbc(data.data(), test->plaintext.data(), data.size(), test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes128Cbc(data.data(), test->ciphertext.data(), data.size(), test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes128CbcEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CbcEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes128CbcEncryptor::kKeySize-1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes128CbcEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes128CbcEncryptor::kKeySize+1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes128CbcEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), nullptr, tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where iv==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes128CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes128CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CbcEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size());
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes128CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes128CbcEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes128CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes128CbcEncryptor::kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128CbcEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CbcEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size());
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes128CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes128CbcEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes128CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes128CbcEncryptor::kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CbcEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes128CbcEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("2b7e151628aed2a6abf7158809cf4f3c");
tmp.test_name = "Test 1";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("7649abac8119b246cee98e9b12e9197d");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("7649ABAC8119B246CEE98E9B12E9197D");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("5086cb9b507219ee95db113a917678b2");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("5086CB9B507219EE95DB113A917678B2");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("73bed6b8e3c1743b7116e69e22229516");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("73BED6B8E3C1743B7116E69E22229516");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("3ff1caa1681fac09120eca307586e1a7");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,33 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes128CbcEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes128CbcEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,265 +0,0 @@
#include <tc/Exception.h>
#include <tc/io.h>
#include <tc/cli.h>
#include <fmt/core.h>
#include "crypto_Aes128CtrEncryptedStream_TestClass.h"
#include "StreamTestUtil.h"
void crypto_Aes128CtrEncryptedStream_TestClass::runAllTests(void)
{
fmt::print("[tc::crypto::Aes128CtrEncryptedStream] START\n");
test_CreateEmptyStream_DefaultConstructor();
test_CreateValidStream_CreateConstructor();
test_RunTestCases();
fmt::print("[tc::crypto::Aes128CtrEncryptedStream] END\n");
}
void crypto_Aes128CtrEncryptedStream_TestClass::test_CreateEmptyStream_DefaultConstructor()
{
fmt::print("[tc::crypto::Aes128CtrEncryptedStream] test_CreateEmptyStream_DefaultConstructor : ");
try
{
try
{
auto stream = tc::crypto::Aes128CtrEncryptedStream();
StreamTestUtil::constructor_TestHelper(stream, 0, 0, false, false, false);
try
{
stream.read(nullptr, 0);
throw tc::Exception(".read() did not throw tc::ObjectDisposedException for uninitialized Aes128CtrEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.write(nullptr, 0);
throw tc::Exception(".write() did not throw tc::ObjectDisposedException for uninitialized Aes128CtrEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.seek(0, tc::io::SeekOrigin::Begin);
throw tc::Exception(".seek() did not throw tc::ObjectDisposedException for uninitialized Aes128CtrEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.setLength(0);
throw tc::Exception(".setLength() did not throw tc::ObjectDisposedException for uninitialized Aes128CtrEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
try
{
stream.flush();
throw tc::Exception(".flush() did not throw tc::ObjectDisposedException for uninitialized Aes128CtrEncryptedStream");
}
catch (tc::ObjectDisposedException&) {
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void crypto_Aes128CtrEncryptedStream_TestClass::test_CreateValidStream_CreateConstructor()
{
fmt::print("[tc::crypto::Aes128CtrEncryptedStream] test_CreateValidStream_CreateConstructor : ");
try
{
try
{
tc::crypto::Aes128CtrEncryptedStream::key_t key;
tc::crypto::Aes128CtrEncryptedStream::counter_t counter;
std::shared_ptr<tc::io::IStream> base_stream;
base_stream = std::shared_ptr<tc::io::MemoryStream>(new tc::io::MemoryStream(tc::ByteData(0x100)));
auto stream = tc::crypto::Aes128CtrEncryptedStream(base_stream, key, counter);
try
{
stream.write(nullptr, 0);
throw tc::Exception(".write() did not throw tc::NotImplementedException for initialized Aes128CtrEncryptedStream");
}
catch (tc::NotImplementedException&) {
// do nothing
}
try
{
stream.setLength(0);
throw tc::Exception(".setLength() did not throw tc::NotImplementedException for initialized Aes128CtrEncryptedStream");
}
catch (tc::NotImplementedException&) {
// do nothing
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void crypto_Aes128CtrEncryptedStream_TestClass::test_RunTestCases()
{
fmt::print("[tc::crypto::Aes128CtrEncryptedStream] test_RunTestCases : ");
try
{
try
{
// get test cases
std::vector<crypto_Aes128CtrEncryptedStream_TestClass::TestCase> test_cases;
util_Setup_TestCases(test_cases);
for (auto itr = test_cases.begin(); itr != test_cases.end(); itr++)
{
tc::crypto::Aes128CtrEncryptedStream::key_t key;
memcpy(key.data(), itr->key.data(), itr->key.size());
tc::crypto::Aes128CtrEncryptedStream::counter_t counter;
memcpy(counter.data(), itr->counter.data(), itr->counter.size());
std::shared_ptr<tc::io::IStream> base_stream;
base_stream = std::shared_ptr<tc::io::MemoryStream>(new tc::io::MemoryStream(itr->ciphertext));
auto stream = tc::crypto::Aes128CtrEncryptedStream(base_stream, key, counter);
try
{
StreamTestUtil::constructor_TestHelper(stream, itr->ciphertext.size(), 0, true, false, true);
StreamTestUtil::read_TestHelper(stream, itr->read_offset, tc::io::SeekOrigin::Begin, itr->read_size, itr->read_size, itr->read_plaintext.size(), itr->read_offset + int64_t(itr->read_size), itr->read_plaintext.data());
}
catch (const tc::Exception& e)
{
throw tc::Exception(fmt::format("{} Failed: {}", itr->test_name, e.error()));
}
}
fmt::print("PASS\n");
}
catch (const tc::Exception& e)
{
fmt::print("FAIL ({:s})\n", e.error());
}
}
catch (const std::exception& e)
{
fmt::print("UNHANDLED EXCEPTION ({:s})\n", e.what());
}
}
void crypto_Aes128CtrEncryptedStream_TestClass::util_Setup_TestCases(std::vector<crypto_Aes128CtrEncryptedStream_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("2b7e151628aed2a6abf7158809cf4f3c");
tmp.counter = tc::cli::FormatUtil::hexStringToBytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tc::ByteData plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
tc::ByteData ciphertext = tc::cli::FormatUtil::hexStringToBytes("874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee");
tmp.test_name = "Test 1";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x00;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x10;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x20;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x30;
tmp.read_size = 0x10;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test read all blocks";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x0;
tmp.read_size = 0x40;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test un-aligned read over blocks 1-2";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x17;
tmp.read_size = 0x11;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test un-aligned read over blocks 0-3";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x07;
tmp.read_size = 0x31;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test partial block 0 read";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x2;
tmp.read_size = 0x8;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test partial block 1 read";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x12;
tmp.read_size = 0x8;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
tmp.test_name = "Test partial block 3 read";
tmp.ciphertext = ciphertext;
tmp.read_offset = 0x32;
tmp.read_size = 0x8;
tmp.read_plaintext = tc::ByteData(plaintext.data() + tmp.read_offset, tmp.read_size);
test_cases.push_back(tmp);
}
@@ -1,29 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <tc/crypto.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/NotImplementedException.h>
class crypto_Aes128CtrEncryptedStream_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_CreateEmptyStream_DefaultConstructor();
void test_CreateValidStream_CreateConstructor();
void test_RunTestCases();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData counter;
tc::ByteData ciphertext;
int64_t read_offset;
size_t read_size;
tc::ByteData read_plaintext;
};
void util_Setup_TestCases(std::vector<crypto_Aes128CtrEncryptedStream_TestClass::TestCase>& test_cases);
};
@@ -1,543 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes128CtrEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes128CtrEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes128CtrEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes128CtrEncryptor] END" << std::endl;
}
void crypto_Aes128CtrEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes128CtrEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes128CtrEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 16;
if (tc::crypto::Aes128CtrEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes128CtrEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CtrEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size(), test->block_number);
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CtrEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size(), test->block_number);
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes128Ctr(data.data(), test->plaintext.data(), data.size(), test->block_number, test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes128Ctr(data.data(), test->ciphertext.data(), data.size(), test->block_number, test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes128CtrEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CtrEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes128CtrEncryptor::kKeySize-1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes128CtrEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes128CtrEncryptor::kKeySize+1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes128CtrEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), nullptr, tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where iv==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes128CtrEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes128CtrEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CtrEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size(), 0);
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128CtrEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128CtrEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size(), 0);
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128CtrEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes128CtrEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("2b7e151628aed2a6abf7158809cf4f3c");
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.test_name = "Test 1";
tmp.block_number = 0;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("874d6191b620e3261bef6864990db6ce");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.block_number = 1;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("9806f66b7970fdff8617187bb9fffdff");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.block_number = 2;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("5ae4df3edbd5d35e5b4f09020db03eab");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.block_number = 3;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("1e031dda2fbe03d1792170a0f3009cee");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.block_number = 0;
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes128CtrEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
uint64_t block_number;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes128CtrEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,523 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes128EcbEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes128EcbEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes128EcbEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes128EcbEncryptor] END" << std::endl;
}
void crypto_Aes128EcbEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes128EcbEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes128EcbEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 16;
if (tc::crypto::Aes128EcbEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes128EcbEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128EcbEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128EcbEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes128Ecb(data.data(), test->plaintext.data(), data.size(), test->key.data(), test->key.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes128Ecb(data.data(), test->ciphertext.data(), data.size(), test->key.data(), test->key.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes128EcbEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128EcbEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes128EcbEncryptor::kKeySize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes128EcbEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes128EcbEncryptor::kKeySize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes128EcbEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128EcbEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size());
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes128EcbEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes128EcbEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128EcbEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes128EcbEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size());
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes128EcbEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes128EcbEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128EcbEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes128EcbEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("2b7e151628aed2a6abf7158809cf4f3c");
tmp.test_name = "Test 1";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("3ad77bb40d7a3660a89ecaf32466ef97");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("f5d3d58503b9699de785895a96fdbaaf");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("43b1cd7f598ece23881b00e3ed030688");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("7b0c785e27e8ad3f8223207104725dd4");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,32 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes128EcbEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes128EcbEncryptor_TestClass::TestCase>& test_cases);
};
File diff suppressed because it is too large Load Diff
@@ -1,30 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes128Encryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes128Encryptor_TestClass::TestCase>& test_cases);
};
@@ -1,691 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Aes128XtsEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes128XtsEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes128XtsEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes128XtsEncryptor] END" << std::endl;
}
void crypto_Aes128XtsEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes128XtsEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes128XtsEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 16;
if (tc::crypto::Aes128XtsEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes128XtsEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes128XtsEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size(), test->data_unit_sequence_number);
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes128XtsEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size(), test->data_unit_sequence_number);
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes128Xts(data.data(), test->plaintext.data(), data.size(), test->data_unit_sequence_number, test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes128Xts(data.data(), test->ciphertext.data(), data.size(), test->data_unit_sequence_number, test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
// validate plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes128XtsEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
if (cryptor.sector_size() != 0)
{
ss << "Failed: sector_size() reported a non-zero answer when not initialized";
throw tc::Exception(ss.str());
}
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes128XtsEncryptor cryptor;
// reference initialize call
//cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
try {
cryptor.initialize(nullptr, tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentNullException where key1==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), 0, tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key1_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tc::crypto::Aes128XtsEncryptor::kKeySize-1, tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key1_size==tc::crypto::Aes128XtsEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tc::crypto::Aes128XtsEncryptor::kKeySize+1, tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key1_size==tc::crypto::Aes128XtsEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), nullptr, tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentNullException where key2==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), 0, tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key2_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tc::crypto::Aes128XtsEncryptor::kKeySize-1, tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key2_size==tc::crypto::Aes128XtsEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tc::crypto::Aes128XtsEncryptor::kKeySize+1, tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key2_size==tc::crypto::Aes128XtsEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), 0, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where sector_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tc::crypto::Aes128XtsEncryptor::kBlockSize-1, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where sector_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes128XtsEncryptor cryptor;
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size(), 0);
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), cryptor.sector_size()-1, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==cryptor.sector_size()-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes128XtsEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes128XtsEncryptor cryptor;
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size(), 0);
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].plaintext.data(), cryptor.sector_size()-1, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==cryptor.sector_size()-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes128XtsEncryptor_TestClass::util_Setup_IEEE1619_2007_TestCases(std::vector<crypto_Aes128XtsEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// XTS-AES applied for a data unit of 32 bytes, 32 bytes key material.
tmp.data_unit = 32;
tmp.test_name = "IEEE 1619-2007 Vector 1";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("00000000000000000000000000000000");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("00000000000000000000000000000000");
tmp.data_unit_sequence_number = 0x00;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("0000000000000000000000000000000000000000000000000000000000000000");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("917cf69ebd68b2ec9b9fe9a3eadda692cd43d2f59598ed858c02c2652fbf922e");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 2";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("11111111111111111111111111111111");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("22222222222222222222222222222222");
tmp.data_unit_sequence_number = 0x3333333333;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("4444444444444444444444444444444444444444444444444444444444444444");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("c454185e6a16936e39334038acef838bfb186fff7480adc4289382ecd6d394f0");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 3";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("22222222222222222222222222222222");
tmp.data_unit_sequence_number = 0x3333333333;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("4444444444444444444444444444444444444444444444444444444444444444");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("af85336b597afc1a900b2eb21ec949d292df4c047e0b21532186a5971a227a89");
test_cases.push_back(tmp);
// XTS-AES-128 applied for a data unit of 512 bytes
tmp.data_unit = 512;
tmp.test_name = "IEEE 1619-2007 Vector 4";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0x00;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("27a7479befa1d476489f308cd4cfa6e2a96e4bbe3208ff25287dd3819616e89cc78cf7f5e543445f8333d8fa7f56000005279fa5d8b5e4ad40e736ddb4d35412328063fd2aab53e5ea1e0a9f332500a5df9487d07a5c92cc512c8866c7e860ce93fdf166a24912b422976146ae20ce846bb7dc9ba94a767aaef20c0d61ad02655ea92dc4c4e41a8952c651d33174be51a10c421110e6d81588ede82103a252d8a750e8768defffed9122810aaeb99f9172af82b604dc4b8e51bcb08235a6f4341332e4ca60482a4ba1a03b3e65008fc5da76b70bf1690db4eae29c5f1badd03c5ccf2a55d705ddcd86d449511ceb7ec30bf12b1fa35b913f9f747a8afd1b130e94bff94effd01a91735ca1726acd0b197c4e5b03393697e126826fb6bbde8ecc1e08298516e2c9ed03ff3c1b7860f6de76d4cecd94c8119855ef5297ca67e9f3e7ff72b1e99785ca0a7e7720c5b36dc6d72cac9574c8cbbc2f801e23e56fd344b07f22154beba0f08ce8891e643ed995c94d9a69c9f1b5f499027a78572aeebd74d20cc39881c213ee770b1010e4bea718846977ae119f7a023ab58cca0ad752afe656bb3c17256a9f6e9bf19fdd5a38fc82bbe872c5539edb609ef4f79c203ebb140f2e583cb2ad15b4aa5b655016a8449277dbd477ef2c8d6c017db738b18deb4a427d1923ce3ff262735779a418f20a282df920147beabe421ee5319d0568");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 5";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0x01;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("27a7479befa1d476489f308cd4cfa6e2a96e4bbe3208ff25287dd3819616e89cc78cf7f5e543445f8333d8fa7f56000005279fa5d8b5e4ad40e736ddb4d35412328063fd2aab53e5ea1e0a9f332500a5df9487d07a5c92cc512c8866c7e860ce93fdf166a24912b422976146ae20ce846bb7dc9ba94a767aaef20c0d61ad02655ea92dc4c4e41a8952c651d33174be51a10c421110e6d81588ede82103a252d8a750e8768defffed9122810aaeb99f9172af82b604dc4b8e51bcb08235a6f4341332e4ca60482a4ba1a03b3e65008fc5da76b70bf1690db4eae29c5f1badd03c5ccf2a55d705ddcd86d449511ceb7ec30bf12b1fa35b913f9f747a8afd1b130e94bff94effd01a91735ca1726acd0b197c4e5b03393697e126826fb6bbde8ecc1e08298516e2c9ed03ff3c1b7860f6de76d4cecd94c8119855ef5297ca67e9f3e7ff72b1e99785ca0a7e7720c5b36dc6d72cac9574c8cbbc2f801e23e56fd344b07f22154beba0f08ce8891e643ed995c94d9a69c9f1b5f499027a78572aeebd74d20cc39881c213ee770b1010e4bea718846977ae119f7a023ab58cca0ad752afe656bb3c17256a9f6e9bf19fdd5a38fc82bbe872c5539edb609ef4f79c203ebb140f2e583cb2ad15b4aa5b655016a8449277dbd477ef2c8d6c017db738b18deb4a427d1923ce3ff262735779a418f20a282df920147beabe421ee5319d0568");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("264d3ca8512194fec312c8c9891f279fefdd608d0c027b60483a3fa811d65ee59d52d9e40ec5672d81532b38b6b089ce951f0f9c35590b8b978d175213f329bb1c2fd30f2f7f30492a61a532a79f51d36f5e31a7c9a12c286082ff7d2394d18f783e1a8e72c722caaaa52d8f065657d2631fd25bfd8e5baad6e527d763517501c68c5edc3cdd55435c532d7125c8614deed9adaa3acade5888b87bef641c4c994c8091b5bcd387f3963fb5bc37aa922fbfe3df4e5b915e6eb514717bdd2a74079a5073f5c4bfd46adf7d282e7a393a52579d11a028da4d9cd9c77124f9648ee383b1ac763930e7162a8d37f350b2f74b8472cf09902063c6b32e8c2d9290cefbd7346d1c779a0df50edcde4531da07b099c638e83a755944df2aef1aa31752fd323dcb710fb4bfbb9d22b925bc3577e1b8949e729a90bbafeacf7f7879e7b1147e28ba0bae940db795a61b15ecf4df8db07b824bb062802cc98a9545bb2aaeed77cb3fc6db15dcd7d80d7d5bc406c4970a3478ada8899b329198eb61c193fb6275aa8ca340344a75a862aebe92eee1ce032fd950b47d7704a3876923b4ad62844bf4a09c4dbe8b4397184b7471360c9564880aedddb9baa4af2e75394b08cd32ff479c57a07d3eab5d54de5f9738b8d27f27a9f0ab11799d7b7ffefb2704c95c6ad12c39f1e867a4b7b1d7818a4b753dfd2a89ccb45e001a03a867b187f225dd");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 6";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0x02;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("264d3ca8512194fec312c8c9891f279fefdd608d0c027b60483a3fa811d65ee59d52d9e40ec5672d81532b38b6b089ce951f0f9c35590b8b978d175213f329bb1c2fd30f2f7f30492a61a532a79f51d36f5e31a7c9a12c286082ff7d2394d18f783e1a8e72c722caaaa52d8f065657d2631fd25bfd8e5baad6e527d763517501c68c5edc3cdd55435c532d7125c8614deed9adaa3acade5888b87bef641c4c994c8091b5bcd387f3963fb5bc37aa922fbfe3df4e5b915e6eb514717bdd2a74079a5073f5c4bfd46adf7d282e7a393a52579d11a028da4d9cd9c77124f9648ee383b1ac763930e7162a8d37f350b2f74b8472cf09902063c6b32e8c2d9290cefbd7346d1c779a0df50edcde4531da07b099c638e83a755944df2aef1aa31752fd323dcb710fb4bfbb9d22b925bc3577e1b8949e729a90bbafeacf7f7879e7b1147e28ba0bae940db795a61b15ecf4df8db07b824bb062802cc98a9545bb2aaeed77cb3fc6db15dcd7d80d7d5bc406c4970a3478ada8899b329198eb61c193fb6275aa8ca340344a75a862aebe92eee1ce032fd950b47d7704a3876923b4ad62844bf4a09c4dbe8b4397184b7471360c9564880aedddb9baa4af2e75394b08cd32ff479c57a07d3eab5d54de5f9738b8d27f27a9f0ab11799d7b7ffefb2704c95c6ad12c39f1e867a4b7b1d7818a4b753dfd2a89ccb45e001a03a867b187f225dd");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("fa762a3680b76007928ed4a4f49a9456031b704782e65e16cecb54ed7d017b5e18abd67b338e81078f21edb7868d901ebe9c731a7c18b5e6dec1d6a72e078ac9a4262f860beefa14f4e821018272e411a951502b6e79066e84252c3346f3aa62344351a291d4bedc7a07618bdea2af63145cc7a4b8d4070691ae890cd65733e7946e9021a1dffc4c59f159425ee6d50ca9b135fa6162cea18a939838dc000fb386fad086acce5ac07cb2ece7fd580b00cfa5e98589631dc25e8e2a3daf2ffdec26531659912c9d8f7a15e5865ea8fb5816d6207052bd7128cd743c12c8118791a4736811935eb982a532349e31dd401e0b660a568cb1a4711f552f55ded59f1f15bf7196b3ca12a91e488ef59d64f3a02bf45239499ac6176ae321c4a211ec545365971c5d3f4f09d4eb139bfdf2073d33180b21002b65cc9865e76cb24cd92c874c24c18350399a936ab3637079295d76c417776b94efce3a0ef7206b15110519655c956cbd8b2489405ee2b09a6b6eebe0c53790a12a8998378b33a5b71159625f4ba49d2a2fdba59fbf0897bc7aabd8d707dc140a80f0f309f835d3da54ab584e501dfa0ee977fec543f74186a802b9a37adb3e8291eca04d66520d229e60401e7282bef486ae059aa70696e0e305d777140a7a883ecdcb69b9ff938e8a4231864c69ca2c2043bed007ff3e605e014bcf518138dc3a25c5e236171a2d01d6");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 7";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0xfd;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("8e41b78c390b5af9d758bb214a67e9f6bf7727b09ac6124084c37611398fa45daad94868600ed391fb1acd4857a95b466e62ef9f4b377244d1c152e7b30d731aad30c716d214b707aed99eb5b5e580b3e887cf7497465651d4b60e6042051da3693c3b78c14489543be8b6ad0ba629565bba202313ba7b0d0c94a3252b676f46cc02ce0f8a7d34c0ed229129673c1f61aed579d08a9203a25aac3a77e9db60267996db38df637356d9dcd1632e369939f2a29d89345c66e05066f1a3677aef18dea4113faeb629e46721a66d0a7e785d3e29af2594eb67dfa982affe0aac058f6e15864269b135418261fc3afb089472cf68c45dd7f231c6249ba0255e1e033833fc4d00a3fe02132d7bc3873614b8aee34273581ea0325c81f0270affa13641d052d36f0757d484014354d02d6883ca15c24d8c3956b1bd027bcf41f151fd8023c5340e5606f37e90fdb87c86fb4fa634b3718a30bace06a66eaf8f63c4aa3b637826a87fe8cfa44282e92cb1615af3a28e53bc74c7cba1a0977be9065d0c1a5dec6c54ae38d37f37aa35283e048e5530a85c4e7a29d7b92ec0c3169cdf2a805c7604bce60049b9fb7b8eaac10f51ae23794ceba68bb58112e293b9b692ca721b37c662f8574ed4dba6f88e170881c82cddc1034a0ca7e284bf0962b6b26292d836fa9f73c1ac770eef0f2d3a1eaf61d3e03555fd424eedd67e18a18094f888");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("d55f684f81f4426e9fde92a5ff02df2ac896af63962888a97910c1379e20b0a3b1db613fb7fe2e07004329ea5c22bfd33e3dbe4cf58cc608c2c26c19a2e2fe22f98732c2b5cb844cc6c0702d91e1d50fc4382a7eba5635cd602432a2306ac4ce82f8d70c8d9bc15f918fe71e74c622d5cf71178bf6e0b9cc9f2b41dd8dbe441c41cd0c73a6dc47a348f6702f9d0e9b1b1431e948e299b9ec2272ab2c5f0c7be86affa5dec87a0bee81d3d50007edaa2bcfccb35605155ff36ed8edd4a40dcd4b243acd11b2b987bdbfaf91a7cac27e9c5aea525ee53de7b2d3332c8644402b823e94a7db26276d2d23aa07180f76b4fd29b9c0823099c9d62c519880aee7e9697617c1497d47bf3e571950311421b6b734d38b0db91eb85331b91ea9f61530f54512a5a52a4bad589eb69781d537f23297bb459bdad2948a29e1550bf4787e0be95bb173cf5fab17dab7a13a052a63453d97ccec1a321954886b7a1299faaeecae35c6eaaca753b041b5e5f093bf83397fd21dd6b3012066fcc058cc32c3b09d7562dee29509b5839392c9ff05f51f3166aaac4ac5f238038a3045e6f72e48ef0fe8bc675e82c318a268e43970271bf119b81bf6a982746554f84e72b9f00280a320a08142923c23c883423ff949827f29bbacdc1ccdb04938ce6098c95ba6b32528f4ef78eed778b2e122ddfd1cbdd11d1c0a6783e011fc536d63d053260637");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 8";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0xfe;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("d55f684f81f4426e9fde92a5ff02df2ac896af63962888a97910c1379e20b0a3b1db613fb7fe2e07004329ea5c22bfd33e3dbe4cf58cc608c2c26c19a2e2fe22f98732c2b5cb844cc6c0702d91e1d50fc4382a7eba5635cd602432a2306ac4ce82f8d70c8d9bc15f918fe71e74c622d5cf71178bf6e0b9cc9f2b41dd8dbe441c41cd0c73a6dc47a348f6702f9d0e9b1b1431e948e299b9ec2272ab2c5f0c7be86affa5dec87a0bee81d3d50007edaa2bcfccb35605155ff36ed8edd4a40dcd4b243acd11b2b987bdbfaf91a7cac27e9c5aea525ee53de7b2d3332c8644402b823e94a7db26276d2d23aa07180f76b4fd29b9c0823099c9d62c519880aee7e9697617c1497d47bf3e571950311421b6b734d38b0db91eb85331b91ea9f61530f54512a5a52a4bad589eb69781d537f23297bb459bdad2948a29e1550bf4787e0be95bb173cf5fab17dab7a13a052a63453d97ccec1a321954886b7a1299faaeecae35c6eaaca753b041b5e5f093bf83397fd21dd6b3012066fcc058cc32c3b09d7562dee29509b5839392c9ff05f51f3166aaac4ac5f238038a3045e6f72e48ef0fe8bc675e82c318a268e43970271bf119b81bf6a982746554f84e72b9f00280a320a08142923c23c883423ff949827f29bbacdc1ccdb04938ce6098c95ba6b32528f4ef78eed778b2e122ddfd1cbdd11d1c0a6783e011fc536d63d053260637");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("72efc1ebfe1ee25975a6eb3aa8589dda2b261f1c85bdab442a9e5b2dd1d7c3957a16fc08e526d4b1223f1b1232a11af274c3d70dac57f83e0983c498f1a6f1aecb021c3e70085a1e527f1ce41ee5911a82020161529cd82773762daf5459de94a0a82adae7e1703c808543c29ed6fb32d9e004327c1355180c995a07741493a09c21ba01a387882da4f62534b87bb15d60d197201c0fd3bf30c1500a3ecfecdd66d8721f90bcc4c17ee925c61b0a03727a9c0d5f5ca462fbfa0af1c2513a9d9d4b5345bd27a5f6e653f751693e6b6a2b8ead57d511e00e58c45b7b8d005af79288f5c7c22fd4f1bf7a898b03a5634c6a1ae3f9fae5de4f296a2896b23e7ed43ed14fa5a2803f4d28f0d3ffcf24757677aebdb47bb388378708948a8d4126ed1839e0da29a537a8c198b3c66ab00712dd261674bf45a73d67f76914f830ca014b65596f27e4cf62de66125a5566df9975155628b400fbfb3a29040ed50faffdbb18aece7c5c44693260aab386c0a37b11b114f1c415aebb653be468179428d43a4d8bc3ec38813eca30a13cf1bb18d524f1992d44d8b1a42ea30b22e6c95b199d8d182f8840b09d059585c31ad691fa0619ff038aca2c39a943421157361717c49d322028a74648113bd8c9d7ec77cf3c89c1ec8718ceff8516d96b34c3c614f10699c9abc4ed0411506223bea16af35c883accdbe1104eef0cfdb54e12fb230a");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 9";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0xff;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("72efc1ebfe1ee25975a6eb3aa8589dda2b261f1c85bdab442a9e5b2dd1d7c3957a16fc08e526d4b1223f1b1232a11af274c3d70dac57f83e0983c498f1a6f1aecb021c3e70085a1e527f1ce41ee5911a82020161529cd82773762daf5459de94a0a82adae7e1703c808543c29ed6fb32d9e004327c1355180c995a07741493a09c21ba01a387882da4f62534b87bb15d60d197201c0fd3bf30c1500a3ecfecdd66d8721f90bcc4c17ee925c61b0a03727a9c0d5f5ca462fbfa0af1c2513a9d9d4b5345bd27a5f6e653f751693e6b6a2b8ead57d511e00e58c45b7b8d005af79288f5c7c22fd4f1bf7a898b03a5634c6a1ae3f9fae5de4f296a2896b23e7ed43ed14fa5a2803f4d28f0d3ffcf24757677aebdb47bb388378708948a8d4126ed1839e0da29a537a8c198b3c66ab00712dd261674bf45a73d67f76914f830ca014b65596f27e4cf62de66125a5566df9975155628b400fbfb3a29040ed50faffdbb18aece7c5c44693260aab386c0a37b11b114f1c415aebb653be468179428d43a4d8bc3ec38813eca30a13cf1bb18d524f1992d44d8b1a42ea30b22e6c95b199d8d182f8840b09d059585c31ad691fa0619ff038aca2c39a943421157361717c49d322028a74648113bd8c9d7ec77cf3c89c1ec8718ceff8516d96b34c3c614f10699c9abc4ed0411506223bea16af35c883accdbe1104eef0cfdb54e12fb230a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("3260ae8dad1f4a32c5cafe3ab0eb95549d461a67ceb9e5aa2d3afb62dece0553193ba50c75be251e08d1d08f1088576c7efdfaaf3f459559571e12511753b07af073f35da06af0ce0bbf6b8f5ccc5cea500ec1b211bd51f63b606bf6528796ca12173ba39b8935ee44ccce646f90a45bf9ccc567f0ace13dc2d53ebeedc81f58b2e41179dddf0d5a5c42f5d8506c1a5d2f8f59f3ea873cbcd0eec19acbf325423bd3dcb8c2b1bf1d1eaed0eba7f0698e4314fbeb2f1566d1b9253008cbccf45a2b0d9c5c9c21474f4076e02be26050b99dee4fd68a4cf890e496e4fcae7b70f94ea5a9062da0daeba1993d2ccd1dd3c244b8428801495a58b216547e7e847c46d1d756377b6242d2e5fb83bf752b54e0df71e889f3a2bb0f4c10805bf3c590376e3c24e22ff57f7fa965577375325cea5d920db94b9c336b455f6e894c01866fe9fbb8c8d3f70a2957285f6dfb5dcd8cbf54782f8fe7766d4723819913ac773421e3a31095866bad22c86a6036b2518b2059b4229d18c8c2ccbdf906c6cc6e82464ee57bddb0bebcb1dc645325bfb3e665ef7251082c88ebb1cf203bd779fdd38675713c8daadd17e1cabee432b09787b6ddf3304e38b731b45df5df51b78fcfb3d32466028d0ba36555e7e11ab0ee0666061d1645d962444bc47a38188930a84b4d561395c73c087021927ca638b7afc8a8679ccb84c26555440ec7f10445cd");
test_cases.push_back(tmp);
tmp.test_name = "Custom Test, Vectors 4-6";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0x00;
tmp.plaintext = tc::ByteData(tmp.data_unit * 3);
tmp.ciphertext = tc::ByteData(tmp.data_unit * 3);
for (size_t idx = 0; idx < 3; idx++)
{
memcpy(tmp.plaintext.data() + idx * tmp.data_unit, test_cases[3 + idx].plaintext.data(), tmp.data_unit);
memcpy(tmp.ciphertext.data() + idx * tmp.data_unit, test_cases[3 + idx].ciphertext.data(), tmp.data_unit);
}
test_cases.push_back(tmp);
tmp.test_name = "Custom Test, Vectors 7-9";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("27182818284590452353602874713526");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("31415926535897932384626433832795");
tmp.data_unit_sequence_number = 0xfd;
tmp.plaintext = tc::ByteData(tmp.data_unit * 3);
tmp.ciphertext = tc::ByteData(tmp.data_unit * 3);
for (size_t idx = 0; idx < 3; idx++)
{
memcpy(tmp.plaintext.data() + idx * tmp.data_unit, test_cases[6 + idx].plaintext.data(), tmp.data_unit);
memcpy(tmp.ciphertext.data() + idx * tmp.data_unit, test_cases[6 + idx].ciphertext.data(), tmp.data_unit);
}
test_cases.push_back(tmp);
// XTS-AES-128 applied for a data unit that is not a multiple of 16 bytes
// please note the ciphertext for these have been modified, as even the reference implementation cannot be validated against these test vectors
tmp.test_name = "IEEE 1619-2007 Vector 15";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("bfbebdbcbbbab9b8b7b6b5b4b3b2b1b0");
tmp.data_unit_sequence_number = 0x9a78563412;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f10");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("641610679DCBF92E505C41333FB06C2A95"); // original 6c1625db4671522d3d7599601de7ca09ed
tmp.data_unit = tmp.plaintext.size();
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 16";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("bfbebdbcbbbab9b8b7b6b5b4b3b2b1b0");
tmp.data_unit_sequence_number = 0x9a78563412;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f1011");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("223A725CBCD4DC647B9A9826D54C99C895C8"); // original d069444b7a7e0cab09e24447d24deb1fedbf
tmp.data_unit = tmp.plaintext.size();
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 17";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("bfbebdbcbbbab9b8b7b6b5b4b3b2b1b0");
tmp.data_unit_sequence_number = 0x9a78563412;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("0D39809A65C1D55501960B671D4B8B6B95C871"); // original e5df1351c0544ba1350b3363cd8ef4beedbf9d
tmp.data_unit = tmp.plaintext.size();
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 18";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("bfbebdbcbbbab9b8b7b6b5b4b3b2b1b0");
tmp.data_unit_sequence_number = 0x9a78563412;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f10111213");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("A8BA0048D75084603EB8423A09B7BF7595C871F6"); // original 9d84c813f719aa2c7be3f66171c7c5c2edbf9dac
tmp.data_unit = tmp.plaintext.size();
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 19";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("e0e1e2e3e4e5e6e7e8e9eaebecedeeef");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("c0c1c2c3c4c5c6c7c8c9cacbcccdcecf");
tmp.data_unit_sequence_number = 0x21436587a9;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("C4E60104E27AED4DEFF63B72B054AC82CC0175A5B8AEEEAA017EC12249AA641B36438DAB0589E8903BF23327127C12362E7D6864522C44538E4E979D97393BA67EAB768517E2C70B98035FC1BBEBFED48A9FF017EB3E04DC8E19AAD6BE04C23A6675726A4388C8A297FEF753BF71E9EA07DD354D42DC2393888A401188554A53315EB9A0624AE44E5B674B3607B73EE0E5FEB44F5BE7178EF54CBD460B1D6A2E93923F6B63210B06D74367BBB02884639AF3B958CACC041618A7940A983F8438123348F3D87F254377152302D821ECE64588F8BB1AF85CF934BD49C703186E48624772C9802228484249E887EBFD7440514130D8D38C2B1219241A42630BDFBD4135FAC6BEC92462D3EA9BEB95C797D23A8C04799E3EA2BA733C5E00718649E7AF0FDD6EAA5BFD5DF7F3FA9953A3B5266806709D17DDD0A0F5B7535BC9F986C109A848EF8C3A45F9033056817CE08DE8019EA103E28836B82D08C5D1FDEEC254E508BF253F6B90963FE43D7D8B8D66D30419C4733A32DA1505DE5DFD7A976E7852455DD454327EE8A1CB71D40F392A89EEE266A8F42772BB519F4044902D939E9716734F622F46E3C48F31D09E7859A14B54693F9EEB14FC021DC5B66589CA3FE16B7BC7166A3686CC869730656AE76285A518B290745E852C6AC626EB0DA25DFF404B83F001D6D23A65D91F3F38A097DA03A59B275B4F5A5480C9608E12F93B"); // original 38b45812ef43a05bd957e545907e223b954ab4aaf088303ad910eadf14b42be68b2461149d8c8ba85f992be970bc621f1b06573f63e867bf5875acafa04e42ccbd7bd3c2a0fb1fff791ec5ec36c66ae4ac1e806d81fbf709dbe29e471fad38549c8e66f5345d7c1eb94f405d1ec785cc6f6a68f6254dd8339f9d84057e01a17741990482999516b5611a38f41bb6478e6f173f320805dd71b1932fc333cb9ee39936beea9ad96fa10fb4112b901734ddad40bc1878995f8e11aee7d141a2f5d48b7a4e1e7f0b2c04830e69a4fd1378411c2f287edf48c6c4e5c247a19680f7fe41cefbd49b582106e3616cbbe4dfb2344b2ae9519391f3e0fb4922254b1d6d2d19c6d4d537b3a26f3bcc51588b32f3eca0829b6a5ac72578fb814fb43cf80d64a233e3f997a3f02683342f2b33d25b492536b93becb2f5e1a8b82f5b883342729e8ae09d16938841a21a97fb543eea3bbff59f13c1a18449e398701c1ad51648346cbc04c27bb2da3b93a1372ccae548fb53bee476f9e9c91773b1bb19828394d55d3e1a20ed69113a860b6829ffa847224604435070221b257e8dff783615d2cae4803a93aa4334ab482a0afac9c0aeda70b45a481df5dec5df8cc0f423c77a5fd46cd312021d4b438862419a791be03bb4d97c0e59578542531ba466a83baf92cefc151b5cc1611a167893819b63fb8a6b18e86de60290fa72b797b0ce59f3
tmp.data_unit = tmp.plaintext.size();
test_cases.push_back(tmp);
}
@@ -1,35 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes128XtsEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key1;
tc::ByteData key2;
uint64_t data_unit;
uint64_t data_unit_sequence_number;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_IEEE1619_2007_TestCases(std::vector<crypto_Aes128XtsEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,570 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes192CbcEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes192CbcEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes192CbcEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes192CbcEncryptor] END" << std::endl;
}
void crypto_Aes192CbcEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes192CbcEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes192CbcEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 24;
if (tc::crypto::Aes192CbcEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes192CbcEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CbcEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CbcEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes192Cbc(data.data(), test->plaintext.data(), data.size(), test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes192Cbc(data.data(), test->ciphertext.data(), data.size(), test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes192CbcEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CbcEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes192CbcEncryptor::kKeySize-1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes192CbcEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes192CbcEncryptor::kKeySize+1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes192CbcEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), nullptr, tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where iv==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes192CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes192CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CbcEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size());
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes192CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes192CbcEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes192CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes192CbcEncryptor::kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192CbcEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CbcEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size());
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes192CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes192CbcEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes192CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes192CbcEncryptor::kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CbcEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes192CbcEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
tmp.test_name = "Test 1";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("4f021db243bc633d7178183a9fa071e8");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("4F021DB243BC633D7178183A9FA071E8");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("b4d9ada9ad7dedf4e5e738763f69145a");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("B4D9ADA9AD7DEDF4E5E738763F69145A");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("571b242012fb7ae07fa9baac3df102e0");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("571B242012FB7AE07FA9BAAC3DF102E0");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("08b0e27988598881d920a9e64f5615cd");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,33 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes192CbcEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes192CbcEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,543 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes192CtrEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes192CtrEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes192CtrEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes192CtrEncryptor] END" << std::endl;
}
void crypto_Aes192CtrEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes192CtrEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes192CtrEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 24;
if (tc::crypto::Aes192CtrEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes192CtrEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CtrEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size(), test->block_number);
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CtrEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size(), test->block_number);
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes192Ctr(data.data(), test->plaintext.data(), data.size(), test->block_number, test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes192Ctr(data.data(), test->ciphertext.data(), data.size(), test->block_number, test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes192CtrEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CtrEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes192CtrEncryptor::kKeySize-1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes192CtrEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes192CtrEncryptor::kKeySize+1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes192CtrEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), nullptr, tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where iv==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes192CtrEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes192CtrEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CtrEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size(), 0);
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192CtrEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192CtrEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size(), 0);
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192CtrEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes192CtrEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.test_name = "Test 1";
tmp.block_number = 0;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("1abc932417521ca24f2b0459fe7e6e0b");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.block_number = 1;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("090339ec0aa6faefd5ccc2c6f4ce8e94");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.block_number = 2;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("1e36b26bd1ebc670d1bd1d665620abf7");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.block_number = 3;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("4f78a7f6d29809585a97daec58c6b050");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.block_number = 0;
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes192CtrEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
uint64_t block_number;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes192CtrEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,523 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes192EcbEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes192EcbEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes192EcbEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes192EcbEncryptor] END" << std::endl;
}
void crypto_Aes192EcbEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes192EcbEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes192EcbEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 24;
if (tc::crypto::Aes192EcbEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes192EcbEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192EcbEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192EcbEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes192Ecb(data.data(), test->plaintext.data(), data.size(), test->key.data(), test->key.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes192Ecb(data.data(), test->ciphertext.data(), data.size(), test->key.data(), test->key.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes192EcbEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192EcbEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes192EcbEncryptor::kKeySize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes192EcbEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes192EcbEncryptor::kKeySize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes192EcbEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192EcbEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size());
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes192EcbEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes192EcbEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes192EcbEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes192EcbEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size());
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes192EcbEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes192EcbEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes192EcbEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes192EcbEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
tmp.test_name = "Test 1";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("bd334f1d6e45f25ff712a214571fa5cc");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("974104846d0ad3ad7734ecb3ecee4eef");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("ef7afd2270e2e60adce0ba2face6444e");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("9a4b41ba738d6c72fb16691603c18e0e");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,32 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes192EcbEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes192EcbEncryptor_TestClass::TestCase>& test_cases);
};
File diff suppressed because it is too large Load Diff
@@ -1,30 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes192Encryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes192Encryptor_TestClass::TestCase>& test_cases);
};
@@ -1,570 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes256CbcEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes256CbcEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes256CbcEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes256CbcEncryptor] END" << std::endl;
}
void crypto_Aes256CbcEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes256CbcEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes256CbcEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 32;
if (tc::crypto::Aes256CbcEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes256CbcEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CbcEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CbcEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes256Cbc(data.data(), test->plaintext.data(), data.size(), test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes256Cbc(data.data(), test->ciphertext.data(), data.size(), test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes256CbcEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CbcEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes256CbcEncryptor::kKeySize-1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes256CbcEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes256CbcEncryptor::kKeySize+1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes256CbcEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), nullptr, tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where iv==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes256CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes256CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CbcEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size());
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes256CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes256CbcEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes256CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes256CbcEncryptor::kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256CbcEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CbcEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size());
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes256CbcEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes256CbcEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes256CbcEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes256CbcEncryptor::kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CbcEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes256CbcEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
tmp.test_name = "Test 1";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("f58c4c04d6e5f1ba779eabfb5f7bfbd6");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("F58C4C04D6E5F1BA779EABFB5F7BFBD6");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("9cfc4e967edb808d679f777bc6702c7d");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("9CFC4E967EDB808D679F777BC6702C7D");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("39f23369a9d9bacfa530e26304231461");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("39F23369A9D9BACFA530E26304231461");
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("b2eb05e2c39be9fcda6c19078c6a9d1b");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090A0B0C0D0E0F");
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,33 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes256CbcEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes256CbcEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,543 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes256CtrEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes256CtrEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes256CtrEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes256CtrEncryptor] END" << std::endl;
}
void crypto_Aes256CtrEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes256CtrEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes256CtrEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 32;
if (tc::crypto::Aes256CtrEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes256CtrEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CtrEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size(), test->block_number);
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CtrEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size(), test->block_number);
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes256Ctr(data.data(), test->plaintext.data(), data.size(), test->block_number, test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes256Ctr(data.data(), test->ciphertext.data(), data.size(), test->block_number, test->key.data(), test->key.size(), test->iv.data(), test->iv.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes256CtrEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CtrEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes256CtrEncryptor::kKeySize-1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes256CtrEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes256CtrEncryptor::kKeySize+1, tests[0].iv.data(), tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes256CtrEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), nullptr, tests[0].iv.size());
throw tc::Exception("Failed to throw ArgumentNullException where iv==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes256CtrEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tc::crypto::Aes256CtrEncryptor::kBlockSize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where iv_size==kBlockSize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CtrEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size(), 0);
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256CtrEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256CtrEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size(), tests[0].iv.data(), tests[0].iv.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size(), 0);
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256CtrEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes256CtrEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
tmp.iv = tc::cli::FormatUtil::hexStringToBytes("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.test_name = "Test 1";
tmp.block_number = 0;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("601ec313775789a5b7a7f504bbf3d228");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.block_number = 1;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("f443e3ca4d62b59aca84e990cacaf5c5");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.block_number = 2;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("2b0930daa23de94ce87017ba2d84988d");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.block_number = 3;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("dfc9c58db67aada613c2dd08457941a6");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.block_number = 0;
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes256CtrEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData iv;
uint64_t block_number;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes256CtrEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,523 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <mbedtls/aes.h>
#include "crypto_Aes256EcbEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes256EcbEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes256EcbEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes256EcbEncryptor] END" << std::endl;
}
void crypto_Aes256EcbEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes256EcbEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes256EcbEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 32;
if (tc::crypto::Aes256EcbEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes256EcbEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256EcbEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
cryptor.encrypt(data.data(), test->plaintext.data(), data.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256EcbEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key.data(), test->key.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// encrypt data
tc::crypto::EncryptAes256Ecb(data.data(), test->plaintext.data(), data.size(), test->key.data(), test->key.size());
// validate cipher text
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// decrypt data
tc::crypto::DecryptAes256Ecb(data.data(), test->ciphertext.data(), data.size(), test->key.data(), test->key.size());
// test plain text
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes256EcbEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size());
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256EcbEncryptor cryptor;
try {
cryptor.initialize(nullptr, tests[0].key.size());
throw tc::Exception("Failed to throw ArgumentNullException where key==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes256EcbEncryptor::kKeySize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes256EcbEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key.data(), tc::crypto::Aes256EcbEncryptor::kKeySize+1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key_size==tc::crypto::Aes256EcbEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256EcbEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size());
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), tc::crypto::Aes256EcbEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes256EcbEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256EcbEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_TestCases(tests);
tc::crypto::Aes256EcbEncryptor cryptor;
cryptor.initialize(tests[0].key.data(), tests[0].key.size());
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size());
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size());
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size());
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), tc::crypto::Aes256EcbEncryptor::kBlockSize-1);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==tc::crypto::Aes256EcbEncryptor::kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256EcbEncryptor_TestClass::util_Setup_TestCases(std::vector<crypto_Aes256EcbEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// Test vectors taken from NIST SP 800-38A
tmp.key = tc::cli::FormatUtil::hexStringToBytes("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
tmp.test_name = "Test 1";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("6bc1bee22e409f96e93d7e117393172a");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("f3eed1bdb5d2a03c064b5a7e3db181f8");
test_cases.push_back(tmp);
tmp.test_name = "Test 2";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("ae2d8a571e03ac9c9eb76fac45af8e51");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("591ccb10d410ed26dc5ba74a31362870");
test_cases.push_back(tmp);
tmp.test_name = "Test 3";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("30c81c46a35ce411e5fbc1191a0a52ef");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("b6ed21b99ca6f4f9f153e7b1beafed1d");
test_cases.push_back(tmp);
tmp.test_name = "Test 4";
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("f69f2445df4f9b17ad2b417be66c3710");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("23304b7a39f9f3ff067d8d8f9e24ecc7");
test_cases.push_back(tmp);
tmp.test_name = "Tests 1-4";
tmp.plaintext = tc::ByteData(test_cases[0].plaintext.size() * 4, false);
tmp.ciphertext = tc::ByteData(tmp.plaintext.size(), false);
for (size_t i = 0; i < 4; i++)
{
memcpy(tmp.plaintext.data() + (i * 0x10), test_cases[i].plaintext.data(), test_cases[i].plaintext.size());
memcpy(tmp.ciphertext.data() + (i * 0x10), test_cases[i].ciphertext.data(), test_cases[i].ciphertext.size());
}
test_cases.push_back(tmp);
}
@@ -1,32 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes256EcbEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes256EcbEncryptor_TestClass::TestCase>& test_cases);
};
File diff suppressed because it is too large Load Diff
@@ -1,30 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes256Encryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_TestCases(std::vector<crypto_Aes256Encryptor_TestClass::TestCase>& test_cases);
};
@@ -1,575 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Aes256XtsEncryptor_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Aes256XtsEncryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Aes256XtsEncryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] START" << std::endl;
test_Constants();
test_UseClassEnc();
test_UseClassDec();
test_UseUtilFuncEnc();
test_UseUtilFuncDec();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptThrowsExceptionOnBadInput();
test_DecryptThrowsExceptionOnBadInput();
std::cout << "[tc::crypto::Aes256XtsEncryptor] END" << std::endl;
}
void crypto_Aes256XtsEncryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 16;
if (tc::crypto::Aes256XtsEncryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Aes256XtsEncryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check key size
static const size_t kExpectedKeySize = 32;
if (tc::crypto::Aes256XtsEncryptor::kKeySize != kExpectedKeySize)
{
ss << "kKeySize had value " << std::dec << tc::crypto::Aes256XtsEncryptor::kKeySize << " (expected " << kExpectedKeySize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes256XtsEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
// clear data
memset(data.data(), 0xff, data.size());
// test encryption
cryptor.encrypt(data.data(), test->plaintext.data(), data.size(), test->data_unit_sequence_number);
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes256XtsEncryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// initialize key
cryptor.initialize(test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
// clear data
memset(data.data(), 0xff, data.size());
// test decryption
cryptor.decrypt(data.data(), test->ciphertext.data(), data.size(), test->data_unit_sequence_number);
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// test encryption
tc::crypto::EncryptAes256Xts(data.data(), test->plaintext.data(), data.size(), test->data_unit_sequence_number, test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
if (memcmp(data.data(), test->ciphertext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->ciphertext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->plaintext.size());
// clear data
memset(data.data(), 0xff, data.size());
// test decryption
tc::crypto::DecryptAes256Xts(data.data(), test->ciphertext.data(), data.size(), test->data_unit_sequence_number, test->key1.data(), test->key1.size(), test->key2.data(), test->key2.size(), test->data_unit, true);
if (memcmp(data.data(), test->plaintext.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->plaintext, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
tc::crypto::Aes256XtsEncryptor cryptor;
// create data
tc::ByteData control_data = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData data = tc::ByteData(control_data.data(), control_data.size());
if (cryptor.sector_size() != 0)
{
ss << "Failed: sector_size() reported a non-zero answer when not initialized";
throw tc::Exception(ss.str());
}
// try to decrypt without calling initialize()
cryptor.decrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: decrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
cryptor.encrypt(data.data(), data.data(), data.size(), 0);
// test plain text
if (memcmp(data.data(), control_data.data(), data.size()) != 0)
{
ss << "Failed: encrypt() operated on data when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes256XtsEncryptor cryptor;
// reference initialize call
//cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
try {
cryptor.initialize(nullptr, tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentNullException where key1==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), 0, tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key1_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tc::crypto::Aes256XtsEncryptor::kKeySize-1, tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key1_size==tc::crypto::Aes256XtsEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tc::crypto::Aes256XtsEncryptor::kKeySize+1, tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key1_size==tc::crypto::Aes256XtsEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), nullptr, tests[0].key2.size(), tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentNullException where key2==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), 0, tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key2_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tc::crypto::Aes256XtsEncryptor::kKeySize-1, tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key2_size==tc::crypto::Aes256XtsEncryptor::kKeySize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tc::crypto::Aes256XtsEncryptor::kKeySize+1, tests[0].data_unit, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where key2_size==tc::crypto::Aes256XtsEncryptor::kKeySize+1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), 0, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where sector_size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tc::crypto::Aes256XtsEncryptor::kBlockSize-1, true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where sector_size==kBlockSize-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_EncryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_EncryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes256XtsEncryptor cryptor;
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].plaintext.data(), data.size(), 0);
try {
cryptor.encrypt(nullptr, tests[0].plaintext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.encrypt(data.data(), tests[0].plaintext.data(), cryptor.sector_size()-1, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==cryptor.sector_size()-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::test_DecryptThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Aes256XtsEncryptor] test_DecryptThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_IEEE1619_2007_TestCases(tests);
tc::crypto::Aes256XtsEncryptor cryptor;
cryptor.initialize(tests[0].key1.data(), tests[0].key1.size(), tests[0].key2.data(), tests[0].key2.size(), tests[0].data_unit, true);
tc::ByteData data = tc::ByteData(tests[0].plaintext.size());
// reference decrypt call
//cryptor.decrypt(data.data(), tests[0].ciphertext.data(), data.size(), 0);
try {
cryptor.decrypt(nullptr, tests[0].ciphertext.data(), data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where dst==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), nullptr, data.size(), 0);
throw tc::Exception("Failed to throw ArgumentNullException where src==nullptr");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].ciphertext.data(), 0, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==0");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.decrypt(data.data(), tests[0].plaintext.data(), cryptor.sector_size()-1, 0);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where size==cryptor.sector_size()-1");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Aes256XtsEncryptor_TestClass::util_Setup_IEEE1619_2007_TestCases(std::vector<crypto_Aes256XtsEncryptor_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
// XTS-AES-256 applied for a data unit of 512 bytes
tmp.data_unit = 512;
tmp.test_name = "IEEE 1619-2007 Vector 10";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("2718281828459045235360287471352662497757247093699959574966967627");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("3141592653589793238462643383279502884197169399375105820974944592");
tmp.data_unit_sequence_number = 0xff;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("1c3b3a102f770386e4836c99e370cf9bea00803f5e482357a4ae12d414a3e63b5d31e276f8fe4a8d66b317f9ac683f44680a86ac35adfc3345befecb4bb188fd5776926c49a3095eb108fd1098baec70aaa66999a72a82f27d848b21d4a741b0c5cd4d5fff9dac89aeba122961d03a757123e9870f8acf1000020887891429ca2a3e7a7d7df7b10355165c8b9a6d0a7de8b062c4500dc4cd120c0f7418dae3d0b5781c34803fa75421c790dfe1de1834f280d7667b327f6c8cd7557e12ac3a0f93ec05c52e0493ef31a12d3d9260f79a289d6a379bc70c50841473d1a8cc81ec583e9645e07b8d9670655ba5bbcfecc6dc3966380ad8fecb17b6ba02469a020a84e18e8f84252070c13e9f1f289be54fbc481457778f616015e1327a02b140f1505eb309326d68378f8374595c849d84f4c333ec4423885143cb47bd71c5edae9be69a2ffeceb1bec9de244fbe15992b11b77c040f12bd8f6a975a44a0f90c29a9abc3d4d893927284c58754cce294529f8614dcd2aba991925fedc4ae74ffac6e333b93eb4aff0479da9a410e4450e0dd7ae4c6e2910900575da401fc07059f645e8b7e9bfdef33943054ff84011493c27b3429eaedb4ed5376441a77ed43851ad77f16f541dfd269d50d6a5f14fb0aab1cbb4c1550be97f7ab4066193c4caa773dad38014bd2092fa755c824bb5e54c4f36ffda9fcea70b9c6e693e148c151");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 11";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("2718281828459045235360287471352662497757247093699959574966967627");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("3141592653589793238462643383279502884197169399375105820974944592");
tmp.data_unit_sequence_number = 0xffff;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("77a31251618a15e6b92d1d66dffe7b50b50bad552305ba0217a610688eff7e11e1d0225438e093242d6db274fde801d4cae06f2092c728b2478559df58e837c2469ee4a4fa794e4bbc7f39bc026e3cb72c33b0888f25b4acf56a2a9804f1ce6d3d6e1dc6ca181d4b546179d55544aa7760c40d06741539c7e3cd9d2f6650b2013fd0eeb8c2b8e3d8d240ccae2d4c98320a7442e1c8d75a42d6e6cfa4c2eca1798d158c7aecdf82490f24bb9b38e108bcda12c3faf9a21141c3613b58367f922aaa26cd22f23d708dae699ad7cb40a8ad0b6e2784973dcb605684c08b8d6998c69aac049921871ebb65301a4619ca80ecb485a31d744223ce8ddc2394828d6a80470c092f5ba413c3378fa6054255c6f9df4495862bbb3287681f931b687c888abf844dfc8fc28331e579928cd12bd2390ae123cf03818d14dedde5c0c24c8ab018bfca75ca096f2d531f3d1619e785f1ada437cab92e980558b3dce1474afb75bfedbf8ff54cb2618e0244c9ac0d3c66fb51598cd2db11f9be39791abe447c63094f7c453b7ff87cb5bb36b7c79efb0872d17058b83b15ab0866ad8a58656c5a7e20dbdf308b2461d97c0ec0024a2715055249cf3b478ddd4740de654f75ca686e0d7345c69ed50cdc2a8b332b1f8824108ac937eb050585608ee734097fc09054fbff89eeaeea791f4a7ab1f9868294a4f9e27b42af8100cb9d59cef9645803");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 12";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("2718281828459045235360287471352662497757247093699959574966967627");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("3141592653589793238462643383279502884197169399375105820974944592");
tmp.data_unit_sequence_number = 0xffffff;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("e387aaa58ba483afa7e8eb469778317ecf4cf573aa9d4eac23f2cdf914e4e200a8b490e42ee646802dc6ee2b471b278195d60918ececb44bf79966f83faba0499298ebc699c0c8634715a320bb4f075d622e74c8c932004f25b41e361025b5a87815391f6108fc4afa6a05d9303c6ba68a128a55705d415985832fdeaae6c8e19110e84d1b1f199a2692119edc96132658f09da7c623efcec712537a3d94c0bf5d7e352ec94ae5797fdb377dc1551150721adf15bd26a8efc2fcaad56881fa9e62462c28f30ae1ceaca93c345cf243b73f542e2074a705bd2643bb9f7cc79bb6e7091ea6e232df0f9ad0d6cf502327876d82207abf2115cdacf6d5a48f6c1879a65b115f0f8b3cb3c59d15dd8c769bc014795a1837f3901b5845eb491adfefe097b1fa30a12fc1f65ba22905031539971a10f2f36c321bb51331cdefb39e3964c7ef079994f5b69b2edd83a71ef549971ee93f44eac3938fcdd61d01fa71799da3a8091c4c48aa9ed263ff0749df95d44fef6a0bb578ec69456aa5408ae32c7af08ad7ba8921287e3bbee31b767be06a0e705c864a769137df28292283ea81a2480241b44d9921cdbec1bc28dc1fda114bd8e5217ac9d8ebafa720e9da4f9ace231cc949e5b96fe76ffc21063fddc83a6b8679c00d35e09576a875305bed5f36ed242c8900dd1fa965bc950dfce09b132263a1eef52dd6888c309f5a7d712826");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 13";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("2718281828459045235360287471352662497757247093699959574966967627");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("3141592653589793238462643383279502884197169399375105820974944592");
tmp.data_unit_sequence_number = 0xffffffff;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("bf53d2dade78e822a4d949a9bc6766b01b06a8ef70d26748c6a7fc36d80ae4c5520f7c4ab0ac8544424fa405162fef5a6b7f229498063618d39f0003cb5fb8d1c86b643497da1ff945c8d3bedeca4f479702a7a735f043ddb1d6aaade3c4a0ac7ca7f3fa5279bef56f82cd7a2f38672e824814e10700300a055e1630b8f1cb0e919f5e942010a416e2bf48cb46993d3cb6a51c19bacf864785a00bc2ecff15d350875b246ed53e68be6f55bd7e05cfc2b2ed6432198a6444b6d8c247fab941f569768b5c429366f1d3f00f0345b96123d56204c01c63b22ce78baf116e525ed90fdea39fa469494d3866c31e05f295ff21fea8d4e6e13d67e47ce722e9698a1c1048d68ebcde76b86fcf976eab8aa9790268b7068e017a8b9b749409514f1053027fd16c3786ea1bac5f15cb79711ee2abe82f5cf8b13ae73030ef5b9e4457e75d1304f988d62dd6fc4b94ed38ba831da4b7634971b6cd8ec325d9c61c00f1df73627ed3745a5e8489f3a95c69639c32cd6e1d537a85f75cc844726e8a72fc0077ad22000f1d5078f6b866318c668f1ad03d5a5fced5219f2eabbd0aa5c0f460d183f04404a0d6f469558e81fab24a167905ab4c7878502ad3e38fdbe62a41556cec37325759533ce8f25f367c87bb5578d667ae93f9e2fd99bcbc5f2fbba88cf6516139420fcff3b7361d86322c4bd84c82f335abb152c4a93411373aaa8220");
test_cases.push_back(tmp);
tmp.test_name = "IEEE 1619-2007 Vector 14";
tmp.key1 = tc::cli::FormatUtil::hexStringToBytes("2718281828459045235360287471352662497757247093699959574966967627");
tmp.key2 = tc::cli::FormatUtil::hexStringToBytes("3141592653589793238462643383279502884197169399375105820974944592");
tmp.data_unit_sequence_number = 0xffffffffff;
tmp.plaintext = tc::cli::FormatUtil::hexStringToBytes("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
tmp.ciphertext = tc::cli::FormatUtil::hexStringToBytes("64497e5a831e4a932c09be3e5393376daa599548b816031d224bbf50a818ed2350eae7e96087c8a0db51ad290bd00c1ac1620857635bf246c176ab463be30b808da548081ac847b158e1264be25bb0910bbc92647108089415d45fab1b3d2604e8a8eff1ae4020cfa39936b66827b23f371b92200be90251e6d73c5f86de5fd4a950781933d79a28272b782a2ec313efdfcc0628f43d744c2dc2ff3dcb66999b50c7ca895b0c64791eeaa5f29499fb1c026f84ce5b5c72ba1083cddb5ce45434631665c333b60b11593fb253c5179a2c8db813782a004856a1653011e93fb6d876c18366dd8683f53412c0c180f9c848592d593f8609ca736317d356e13e2bff3a9f59cd9aeb19cd482593d8c46128bb32423b37a9adfb482b99453fbe25a41bf6feb4aa0bef5ed24bf73c762978025482c13115e4015aac992e5613a3b5c2f685b84795cb6e9b2656d8c88157e52c42f978d8634c43d06fea928f2822e465aa6576e9bf419384506cc3ce3c54ac1a6f67dc66f3b30191e698380bc999b05abce19dc0c6dcc2dd001ec535ba18deb2df1a101023108318c75dc98611a09dc48a0acdec676fabdf222f07e026f059b672b56e5cbc8e1d21bbd867dd927212054681d70ea737134cdfce93b6f82ae22423274e58a0821cc5502e2d0ab4585e94de6975be5e0b4efce51cd3e70c25a1fbbbd609d273ad5b0d59631c531f6a0a57b9");
test_cases.push_back(tmp);
}
@@ -1,35 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Aes256XtsEncryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassEnc();
void test_UseClassDec();
void test_UseUtilFuncEnc();
void test_UseUtilFuncDec();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptThrowsExceptionOnBadInput();
void test_DecryptThrowsExceptionOnBadInput();
struct TestCase
{
std::string test_name;
tc::ByteData key1;
tc::ByteData key2;
size_t data_unit;
uint64_t data_unit_sequence_number;
tc::ByteData plaintext;
tc::ByteData ciphertext;
};
void util_Setup_IEEE1619_2007_TestCases(std::vector<crypto_Aes256XtsEncryptor_TestClass::TestCase>& test_cases);
};
@@ -1,547 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_HmacMd5Generator_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/HmacMd5Generator.h>
#include <tc/cli/FormatUtil.h>
void crypto_HmacMd5Generator_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::HmacMd5Generator] START" << std::endl;
test_Constants();
test_SingleUpdateCall();
test_MultiUpdateCall();
test_UtilFunc();
test_NoInitNoUpdateDoMac();
test_NoInitDoUpdateDoMac();
test_DoInitNoUpdateDoMac();
test_DoInitNoKeyDoUpdateDoMac();
test_DoInitNoKeyNoUpdateDoMac();
test_CallGetMacRepeatedly();
std::cout << "[tc::crypto::HmacMd5Generator] END" << std::endl;
}
void crypto_HmacMd5Generator_TestClass::test_Constants()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check mac size
static const size_t kExpectedHashSize = 16;
if (tc::crypto::HmacMd5Generator::kMacSize != kExpectedHashSize)
{
ss << "kMacSize had value " << std::dec << tc::crypto::HmacMd5Generator::kMacSize << " (expected " << kExpectedHashSize << ")";
throw tc::Exception(ss.str());
}
// check block size
static const size_t kExpectedBlockSize = 64;
if (tc::crypto::HmacMd5Generator::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::HmacMd5Generator::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_SingleUpdateCall()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_SingleUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_MultiUpdateCall()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_MultiUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
// pick an offset to split the in_string at
size_t offset = test->in_data.size() / 2;
// update with first half
calc.update(test->in_data.data(), offset);
// update with second half
calc.update(test->in_data.data() + offset, test->in_data.size() - offset);
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_UtilFunc()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
memset(mac.data(), 0xff, mac.size());
tc::crypto::GenerateHmacMd5Mac(mac.data(), (const byte_t*)test->in_data.data(), test->in_data.size(), test->in_key.data(), test->in_key.size());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_NoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_NoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_NoInitDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_NoInitDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_DoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_DoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
// override expected MAC for when no update() is called
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("C9E99A43CD8FA24A840AA85C7CCA0061");
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("60B57DA4237ED7C91B475EDDF0E798D3");
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("68333B4B8FCBAD8D64D914430788E601");
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("24CFC1B34D4FD3388EC723F7B6214669");
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("8AAFFA8F035AF4C09CA7D1635F8CF716");
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("172C1869F3E854DC888D3B2D3ADA639F");
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("172C1869F3E854DC888D3B2D3ADA639F");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_DoInitNoKeyDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_DoInitNoKeyDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
// override expected MAC for when no key is used during initialize()
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("72C33C78CAC0B7A581AC263A344ED01D");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("AE2E4B39F3B5EE2C8B585994294201EA");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("1F265B5F8E396420867BA340A8B3AE2F");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("EC0AE3C21F1BC5DD136C488FC11E62E4");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("6F9F9B09EE74ABC55B72EA1003A5AE2B");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("647DF53417E4E001CBD1842FB13C9AE2");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("172C0788A36B21774D60D2D3B911C5D7");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_DoInitNoKeyNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_DoInitNoKeyNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
// override expected MAC for when no key is used during initialize() and update is not called
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("74E6F7298A9C2D168935F58C001BAD88");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::test_CallGetMacRepeatedly()
{
std::cout << "[tc::crypto::HmacMd5Generator] test_CallGetMacRepeatedly : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacMd5Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacMd5Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
for (size_t i = 0; i < 100; i++)
{
// by resetting the MAC here we can tell if it is updated each time
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacMd5Generator_TestClass::util_Setup_Rfc2202_TestCases(std::vector<crypto_HmacMd5Generator_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
tmp.test_name = "RFC 2202 Test 1";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("4869205468657265"); // "Hi There"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("9294727a3638bb1c13f48ef8158bfc9d");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 2";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("4a656665"); // "Jefe"
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("7768617420646f2079612077616e7420666f72206e6f7468696e673f"); // "what do ya want for nothing?"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("750c783e6ab0b503eaa86e310a5db738");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 3";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); // 50 x 0xdd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("56be34521d144c88dbb8c733f0e8b3f6");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 4";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0102030405060708090a0b0c0d0e0f10111213141516171819");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); // 50 x 0xcd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("697eaf0aca3a3aea3a75164746ffaa79");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 5";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("546573742057697468205472756e636174696f6e"); // "Test With Truncation"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("56461ef2342edc00f9bab995690efd4c");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 6";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 80 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374"); // "Test Using Larger Than Block-Size Key - Hash Key First"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 7";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 80 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d53697a652044617461"); // "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("6f630fad67cda0ee1fb1f562db3aa53e");
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_HmacMd5Generator_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_SingleUpdateCall();
void test_MultiUpdateCall();
void test_UtilFunc();
void test_NoInitNoUpdateDoMac();
void test_NoInitDoUpdateDoMac();
void test_DoInitNoUpdateDoMac();
void test_DoInitNoKeyDoUpdateDoMac();
void test_DoInitNoKeyNoUpdateDoMac();
void test_CallGetMacRepeatedly();
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
tc::ByteData in_key;
tc::ByteData out_mac;
};
void util_Setup_Rfc2202_TestCases(std::vector<crypto_HmacMd5Generator_TestClass::TestCase>& test_cases);
};
@@ -1,547 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_HmacSha1Generator_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/HmacSha1Generator.h>
#include <tc/cli/FormatUtil.h>
void crypto_HmacSha1Generator_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::HmacSha1Generator] START" << std::endl;
test_Constants();
test_SingleUpdateCall();
test_MultiUpdateCall();
test_UtilFunc();
test_NoInitNoUpdateDoMac();
test_NoInitDoUpdateDoMac();
test_DoInitNoUpdateDoMac();
test_DoInitNoKeyDoUpdateDoMac();
test_DoInitNoKeyNoUpdateDoMac();
test_CallGetMacRepeatedly();
std::cout << "[tc::crypto::HmacSha1Generator] END" << std::endl;
}
void crypto_HmacSha1Generator_TestClass::test_Constants()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check mac size
static const size_t kExpectedHashSize = 20;
if (tc::crypto::HmacSha1Generator::kMacSize != kExpectedHashSize)
{
ss << "kMacSize had value " << std::dec << tc::crypto::HmacSha1Generator::kMacSize << " (expected " << kExpectedHashSize << ")";
throw tc::Exception(ss.str());
}
// check block size
static const size_t kExpectedBlockSize = 64;
if (tc::crypto::HmacSha1Generator::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::HmacSha1Generator::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_SingleUpdateCall()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_SingleUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_MultiUpdateCall()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_MultiUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
// pick an offset to split the in_string at
size_t offset = test->in_data.size() / 2;
// update with first half
calc.update(test->in_data.data(), offset);
// update with second half
calc.update(test->in_data.data() + offset, test->in_data.size() - offset);
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_UtilFunc()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
memset(mac.data(), 0xff, mac.size());
tc::crypto::GenerateHmacSha1Mac(mac.data(), (const byte_t*)test->in_data.data(), test->in_data.size(), test->in_key.data(), test->in_key.size());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_NoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_NoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_NoInitDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_NoInitDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_DoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_DoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
// override expected MAC for when no update() is called
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("123FD78BDA0100786AE86B76F50F01BD18E477F3");
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("09D9E59D72239E62A8155C583D52743DE9B7231A");
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("4C2E4B8144C34521B2487190F0862E7E0978D42A");
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("932C2468DB83EEB23B6B8F1EDE8BE57136BA9C99");
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("3DF4B2ABD4FCFE0FF5EFA57D0E98CCA85F4B8C17");
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("6FB70A345B84A347A0CA3B58B4F8DDC6AB1EC61F");
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("6FB70A345B84A347A0CA3B58B4F8DDC6AB1EC61F");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_DoInitNoKeyDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_DoInitNoKeyDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
// override expected MAC for when no key is used during initialize()
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("69536CC84EEE5FE51C5B051AFF8485F5C9EF0B58");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("22E999C60F94D0F2D635CA4CF1B174E5CB514D38");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("3EA23ADD7131920CEDD9FA0B7ED130F9E0320B1B");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("74C78AEB607055FF54D8DFCCF1A82CA011ED7A77");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("4ED74D69F9942A1852037F4D8F4F8D0AA680AFDC");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("061B442BBD9AAC68E1AC811EDD8CA83D0586C766");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("69C4969EBA33E494509E07AE006234EEF4CD7624");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_DoInitNoKeyNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_DoInitNoKeyNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
// override expected MAC for when no key is used during initialize() and update is not called
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("FBDB1D1B18AA6C08324B7D64B71FB76370690E1D");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::test_CallGetMacRepeatedly()
{
std::cout << "[tc::crypto::HmacSha1Generator] test_CallGetMacRepeatedly : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc2202_TestCases(tests);
tc::crypto::HmacSha1Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha1Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
for (size_t i = 0; i < 100; i++)
{
// by resetting the MAC here we can tell if it is updated each time
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha1Generator_TestClass::util_Setup_Rfc2202_TestCases(std::vector<crypto_HmacSha1Generator_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
tmp.test_name = "RFC 2202 Test 1";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("4869205468657265"); // "Hi There"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("b617318655057264e28bc0b6fb378c8ef146be00");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 2";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("4a656665"); // "Jefe"
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("7768617420646f2079612077616e7420666f72206e6f7468696e673f"); // "what do ya want for nothing?"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 3";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); // 50 x 0xdd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("125d7342b9ac11cd91a39af48aa17b4f63f175d3");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 4";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0102030405060708090a0b0c0d0e0f10111213141516171819");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); // 50 x 0xcd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("4c9007f4026250c6bc8414f9bf50c86c2d7235da");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 5";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("546573742057697468205472756e636174696f6e"); // "Test With Truncation"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 6";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 80 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374"); // "Test Using Larger Than Block-Size Key - Hash Key First"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("aa4ae5e15272d00e95705637ce8a3b55ed402112");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 7";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 80 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d53697a652044617461"); // "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("e8e99d0f45237d786d6bbaa7965c7808bbff1a91");
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_HmacSha1Generator_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_SingleUpdateCall();
void test_MultiUpdateCall();
void test_UtilFunc();
void test_NoInitNoUpdateDoMac();
void test_NoInitDoUpdateDoMac();
void test_DoInitNoUpdateDoMac();
void test_DoInitNoKeyDoUpdateDoMac();
void test_DoInitNoKeyNoUpdateDoMac();
void test_CallGetMacRepeatedly();
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
tc::ByteData in_key;
tc::ByteData out_mac;
};
void util_Setup_Rfc2202_TestCases(std::vector<crypto_HmacSha1Generator_TestClass::TestCase>& test_cases);
};
@@ -1,547 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_HmacSha256Generator_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/HmacSha256Generator.h>
#include <tc/cli/FormatUtil.h>
void crypto_HmacSha256Generator_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::HmacSha256Generator] START" << std::endl;
test_Constants();
test_SingleUpdateCall();
test_MultiUpdateCall();
test_UtilFunc();
test_NoInitNoUpdateDoMac();
test_NoInitDoUpdateDoMac();
test_DoInitNoUpdateDoMac();
test_DoInitNoKeyDoUpdateDoMac();
test_DoInitNoKeyNoUpdateDoMac();
test_CallGetMacRepeatedly();
std::cout << "[tc::crypto::HmacSha256Generator] END" << std::endl;
}
void crypto_HmacSha256Generator_TestClass::test_Constants()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check mac size
static const size_t kExpectedHashSize = 32;
if (tc::crypto::HmacSha256Generator::kMacSize != kExpectedHashSize)
{
ss << "kMacSize had value " << std::dec << tc::crypto::HmacSha256Generator::kMacSize << " (expected " << kExpectedHashSize << ")";
throw tc::Exception(ss.str());
}
// check block size
static const size_t kExpectedBlockSize = 64;
if (tc::crypto::HmacSha256Generator::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::HmacSha256Generator::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_SingleUpdateCall()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_SingleUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_MultiUpdateCall()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_MultiUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
// pick an offset to split the in_string at
size_t offset = test->in_data.size() / 2;
// update with first half
calc.update(test->in_data.data(), offset);
// update with second half
calc.update(test->in_data.data() + offset, test->in_data.size() - offset);
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_UtilFunc()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
memset(mac.data(), 0xff, mac.size());
tc::crypto::GenerateHmacSha256Mac(mac.data(), (const byte_t*)test->in_data.data(), test->in_data.size(), test->in_key.data(), test->in_key.size());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_NoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_NoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_NoInitDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_NoInitDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_DoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_DoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
// override expected MAC for when no update() is called
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("999A901219F032CD497CADB5E6051E97B6A29AB297BD6AE722BD6062A2F59542");
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("923598CA6D64AF2A5DBA79DCD021A8A0FE5C5F557519ADAAF0AD532D4506DD30");
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("86E54FD448725D7E5DCFE22353C828AF48781EB48CAE8106A7E1D498949F3E46");
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("1B5713E10977DA96A5FE201005976A240544079C2724F6A9EAEAE42B9DE00F28");
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("4D36C675D91E0512D1B1BA412FE2F7D8A6595FD305BDEADF651B465D4251781F");
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("44B545DEF5B97EB719D856A15E327833E520E4770619C0E3EEFBDE24B71285A7");
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("44B545DEF5B97EB719D856A15E327833E520E4770619C0E3EEFBDE24B71285A7");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_DoInitNoKeyDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_DoInitNoKeyDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
// override expected MAC for when no key is used during initialize()
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("E48411262715C8370CD5E7BF8E82BEF53BD53712D007F3429351843B77C7BB9B");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("76D9E7194E7DBC3AA00BBE8FFB9F6FCB5A932170F971F948BB2AB61607D2B9D6");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("658C192B47D1C84BC7D5EE38CAA75864FBF43E79BD18DB6C5FA814ED0D9C4AB3");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("367352007EE6CA9FA755CE8352347D092C17A24077FD33C62F655574A8CF906D");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("53273BDCBDF6DE9AFB0CDBD1E1CF133FE7529237B5A1FBA41F34CC8792430B16");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("79E2A6536CB16B4A1E9C2AF81759764AFD026173526F79F7816E9A3BC5FBABA8");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("4DA63281EA396C9E8FBFE3D6483A602CE18760273914D9DF5FF385FB639A696F");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_DoInitNoKeyNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_DoInitNoKeyNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
// override expected MAC for when no key is used during initialize() and update is not called
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::test_CallGetMacRepeatedly()
{
std::cout << "[tc::crypto::HmacSha256Generator] test_CallGetMacRepeatedly : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha256Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha256Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
for (size_t i = 0; i < 100; i++)
{
// by resetting the MAC here we can tell if it is updated each time
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha256Generator_TestClass::util_Setup_Rfc4231_TestCases(std::vector<crypto_HmacSha256Generator_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
tmp.test_name = "RFC 2202 Test 1";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("4869205468657265"); // "Hi There"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 2";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("4a656665"); // "Jefe"
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("7768617420646f2079612077616e7420666f72206e6f7468696e673f"); // "what do ya want for nothing?"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 3";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); // 50 x 0xdd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 4";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0102030405060708090a0b0c0d0e0f10111213141516171819");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); // 50 x 0xcd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 5";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("546573742057697468205472756e636174696f6e"); // "Test With Truncation"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("A3B6167473100EE06E0C796C2955552BFA6F7C0A6A8AEF8B93F860AAB0CD20C5");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 6";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 131 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374"); // "Test Using Larger Than Block-Size Key - Hash Key First"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 7";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 131 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e"); // "This is a test using a larger than block-size key and a larger than block-size data. The key neds to be hashed before being used by the HMAC algorithm."
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2");
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_HmacSha256Generator_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_SingleUpdateCall();
void test_MultiUpdateCall();
void test_UtilFunc();
void test_NoInitNoUpdateDoMac();
void test_NoInitDoUpdateDoMac();
void test_DoInitNoUpdateDoMac();
void test_DoInitNoKeyDoUpdateDoMac();
void test_DoInitNoKeyNoUpdateDoMac();
void test_CallGetMacRepeatedly();
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
tc::ByteData in_key;
tc::ByteData out_mac;
};
void util_Setup_Rfc4231_TestCases(std::vector<crypto_HmacSha256Generator_TestClass::TestCase>& test_cases);
};
@@ -1,547 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_HmacSha512Generator_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/HmacSha512Generator.h>
#include <tc/cli/FormatUtil.h>
void crypto_HmacSha512Generator_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::HmacSha512Generator] START" << std::endl;
test_Constants();
test_SingleUpdateCall();
test_MultiUpdateCall();
test_UtilFunc();
test_NoInitNoUpdateDoMac();
test_NoInitDoUpdateDoMac();
test_DoInitNoUpdateDoMac();
test_DoInitNoKeyDoUpdateDoMac();
test_DoInitNoKeyNoUpdateDoMac();
test_CallGetMacRepeatedly();
std::cout << "[tc::crypto::HmacSha512Generator] END" << std::endl;
}
void crypto_HmacSha512Generator_TestClass::test_Constants()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check mac size
static const size_t kExpectedHashSize = 64;
if (tc::crypto::HmacSha512Generator::kMacSize != kExpectedHashSize)
{
ss << "kMacSize had value " << std::dec << tc::crypto::HmacSha512Generator::kMacSize << " (expected " << kExpectedHashSize << ")";
throw tc::Exception(ss.str());
}
// check block size
static const size_t kExpectedBlockSize = 128;
if (tc::crypto::HmacSha512Generator::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::HmacSha512Generator::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_SingleUpdateCall()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_SingleUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_MultiUpdateCall()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_MultiUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
// pick an offset to split the in_string at
size_t offset = test->in_data.size() / 2;
// update with first half
calc.update(test->in_data.data(), offset);
// update with second half
calc.update(test->in_data.data() + offset, test->in_data.size() - offset);
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_UtilFunc()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
memset(mac.data(), 0xff, mac.size());
tc::crypto::GenerateHmacSha512Mac(mac.data(), (const byte_t*)test->in_data.data(), test->in_data.size(), test->in_key.data(), test->in_key.size());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_NoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_NoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_NoInitDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_NoInitDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
tc::ByteData expected_uninitialized_mac = tc::ByteData(mac.size());
memset(expected_uninitialized_mac.data(), 0xff, expected_uninitialized_mac.size());
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memcpy(mac.data(), expected_uninitialized_mac.data(), mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), expected_uninitialized_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(expected_uninitialized_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_DoInitNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_DoInitNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
// override expected MAC for when no update() is called
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("AD8DA3D882AF6E9B872457ADCDD638E9B87AF44825425085F8CE81A4122BAB781B92F5AB92AC24948AD369F86558FD469CA3F4861CB0F0DFB33154428ED03DFB");
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("B9D14C51A6D4DD41604EB06C9C240F1F64F143B5CFDEA37129B28BB75D1371D326FC219216171261A84E6C05707CD3BE0F61E0A973A33F706D190DB9ACFFC68F");
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("6393B26D6D510B77741BEC0037A40B3A31071DB90ABF9A9E857B9CA643F7AF5F2AF35201608D2E3F14394AAAEA46DBAEC3E7A4EBAD54D310DB7659D102F4C73A");
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("10CDF916AEE81A887E7F46BF329A18715A959E5E4EAAED386E7AE8779FC56F1CA6543219476B55D767D6E34D8EF1C76F251C95719346A588FBDDEED2FC3F013E");
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("2BAD6402A42592B704DAC5BD90A559ACF444D75B7E6C9C18D44BC02FF04C94CCD180F6B71F86B0A2D853F9146F5605B8F5807006EDE0F7F35370EEF7D50FBA16");
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("264428A9B95825C9ACD9D71AAC552BD95E9BD00169C02D3921DBD53A0FDF6F0E81A19EF54E7B143F131CC5FFD5896EEF3CDCF57066A70DA3698D826A1B6366DD");
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("264428A9B95825C9ACD9D71AAC552BD95E9BD00169C02D3921DBD53A0FDF6F0E81A19EF54E7B143F131CC5FFD5896EEF3CDCF57066A70DA3698D826A1B6366DD");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.size(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_DoInitNoKeyDoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_DoInitNoKeyDoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
// override expected MAC for when no key is used during initialize()
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("F7688A104326D36C1940F6D28D746C0661D383E0D14FE8A04649444777610F5DD9565A36846AB9E9E734CF380D3A070D8EF021B5F3A50C481710A464968E3419");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("1263AC6489324BDD55FEAD956DC2BE547BECE2B699214B63B0270143D740AD638F3CC6FEEDC740F8D9AC44866DF12A0073D57214EB393F692B4AE17EDF30D861");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("364113906C7268F6C0C3F4977411CF34151DA703DBF6E0D3CD2A2179FA0446060BF5017D7F41330429AF17FAA4BCD4C3DA1E654767670EC177239C39B3EEF086");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("17FD4AD25B6D1979AB38D833053B1B44FA99E9B410D5BC35541D6AA18A5AC8964B6B86C9C01ED10212BA45EFFB161C8676FD26EF28C557A27003294325275E9D");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("6D3B39DCD123E86481D138C3237EE179329BEFB7FC82515FB6B694B6DF6E88A1827150E31366BB133A622527DAAA865E9E06BDFD0D9BDB574645292DDB7FE49E");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("7E129DE32C9CBF193A88BF5107EC58A702D646F3A1ACF1ADE5631D950E96CAEC035C05DA4953F3063917954419889EFC27BA89E0B5B7767A19D36780F646BA24");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("D9B8EE3286A49C2B649624D93E04322F0D29E9F49CE77BEF1E13C40553A3D01EAB341F67F1F208CE10FCBC696DE5BB24EC3E1F361A12485060CE561C54CC85CB");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_DoInitNoKeyNoUpdateDoMac()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_DoInitNoKeyNoUpdateDoMac : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
// override expected MAC for when no key is used during initialize() and update is not called
tests[0].in_key = tc::ByteData();
tests[0].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
tests[1].in_key = tc::ByteData();
tests[1].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
tests[2].in_key = tc::ByteData();
tests[2].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
tests[3].in_key = tc::ByteData();
tests[3].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
tests[4].in_key = tc::ByteData();
tests[4].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
tests[5].in_key = tc::ByteData();
tests[5].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
tests[6].in_key = tc::ByteData();
tests[6].out_mac = tc::cli::FormatUtil::hexStringToBytes("B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47");
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
//calc.update(test->in_data.data(), test->in_data.size());
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::test_CallGetMacRepeatedly()
{
std::cout << "[tc::crypto::HmacSha512Generator] test_CallGetMacRepeatedly : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<TestCase> tests;
util_Setup_Rfc4231_TestCases(tests);
tc::crypto::HmacSha512Generator calc;
tc::ByteData mac = tc::ByteData(tc::crypto::HmacSha512Generator::kMacSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize(test->in_key.data(), test->in_key.size());
calc.update(test->in_data.data(), test->in_data.size());
for (size_t i = 0; i < 100; i++)
{
// by resetting the MAC here we can tell if it is updated each time
memset(mac.data(), 0xff, mac.size());
calc.getMac(mac.data());
if (memcmp(mac.data(), test->out_mac.data(), mac.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong MAC: " << tc::cli::FormatUtil::formatBytesAsString(mac, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_mac, true, "");
throw tc::Exception(ss.str());
}
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_HmacSha512Generator_TestClass::util_Setup_Rfc4231_TestCases(std::vector<crypto_HmacSha512Generator_TestClass::TestCase>& test_cases)
{
TestCase tmp;
test_cases.clear();
tmp.test_name = "RFC 2202 Test 1";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("4869205468657265"); // "Hi There"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 2";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("4a656665"); // "Jefe"
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("7768617420646f2079612077616e7420666f72206e6f7468696e673f"); // "what do ya want for nothing?"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 3";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); // 50 x 0xdd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 4";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0102030405060708090a0b0c0d0e0f10111213141516171819");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); // 50 x 0xcd
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 5";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("546573742057697468205472756e636174696f6e"); // "Test With Truncation"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("415FAD6271580A531D4179BC891D87A650188707922A4FBB36663A1EB16DA008711C5B50DDD0FC235084EB9D3364A1454FB2EF67CD1D29FE6773068EA266E96B");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 6";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 131 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374"); // "Test Using Larger Than Block-Size Key - Hash Key First"
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598");
test_cases.push_back(tmp);
tmp.test_name = "RFC 2202 Test 7";
tmp.in_key = tc::cli::FormatUtil::hexStringToBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 131 x 0xaa
tmp.in_data = tc::cli::FormatUtil::hexStringToBytes("5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e"); // "This is a test using a larger than block-size key and a larger than block-size data. The key neds to be hashed before being used by the HMAC algorithm."
tmp.out_mac = tc::cli::FormatUtil::hexStringToBytes("e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58");
test_cases.push_back(tmp);
}
@@ -1,34 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_HmacSha512Generator_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_SingleUpdateCall();
void test_MultiUpdateCall();
void test_UtilFunc();
void test_NoInitNoUpdateDoMac();
void test_NoInitDoUpdateDoMac();
void test_DoInitNoUpdateDoMac();
void test_DoInitNoKeyDoUpdateDoMac();
void test_DoInitNoKeyNoUpdateDoMac();
void test_CallGetMacRepeatedly();
struct TestCase
{
std::string test_name;
tc::ByteData in_data;
tc::ByteData in_key;
tc::ByteData out_mac;
};
void util_Setup_Rfc4231_TestCases(std::vector<crypto_HmacSha512Generator_TestClass::TestCase>& test_cases);
};
@@ -1,490 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Md5Generator_TestClass.h"
#include <tc/Exception.h>
#include <tc/crypto/Md5Generator.h>
#include <tc/cli/FormatUtil.h>
#include <tc/ByteData.h>
void crypto_Md5Generator_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Md5Generator] START" << std::endl;
test_Constants();
test_SingleUpdateCall();
test_MultiUpdateCall();
test_UtilFunc();
test_NoInitNoUpdateDoHash();
test_NoInitDoUpdateDoHash();
test_DoInitNoUpdateDoHash();
test_CallGetHashRepeatedly();
std::cout << "[tc::crypto::Md5Generator] END" << std::endl;
}
void crypto_Md5Generator_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Md5Generator] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check hash size
static const size_t kExpectedHashSize = 16;
if (tc::crypto::Md5Generator::kHashSize != kExpectedHashSize)
{
ss << "kHashSize had value " << std::dec << tc::crypto::Md5Generator::kHashSize << " (expected " << kExpectedHashSize << ")";
throw tc::Exception(ss.str());
}
// check block size
static const size_t kExpectedBlockSize = 64;
if (tc::crypto::Md5Generator::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Md5Generator::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
// check ASN.1 OID data
tc::ByteData kExpectedAsn1OidData = tc::cli::FormatUtil::hexStringToBytes("3020300C06082A864886F70D020505000410");
if (tc::crypto::Md5Generator::kAsn1OidDataSize != kExpectedAsn1OidData.size())
{
ss << "kAsn1OidDataSize had value " << std::dec << tc::crypto::Md5Generator::kAsn1OidDataSize << " (expected " << kExpectedAsn1OidData.size() << ")";
throw tc::Exception(ss.str());
}
if (tc::crypto::Md5Generator::kAsn1OidData.size() != kExpectedAsn1OidData.size())
{
ss << "kAsn1OidData.size() had value " << std::dec << tc::crypto::Md5Generator::kAsn1OidData.size() << " (expected " << kExpectedAsn1OidData.size() << ")";
throw tc::Exception(ss.str());
}
if (memcmp(tc::crypto::Md5Generator::kAsn1OidData.data(), kExpectedAsn1OidData.data(), kExpectedAsn1OidData.size()) != 0)
{
ss << "kAsn1OidData.data() had data " << tc::cli::FormatUtil::formatBytesAsString(tc::crypto::Md5Generator::kAsn1OidData.data(), tc::crypto::Md5Generator::kAsn1OidData.size(), true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(kExpectedAsn1OidData, true, "");
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_SingleUpdateCall()
{
std::cout << "[tc::crypto::Md5Generator] test_SingleUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests
std::vector<TestCase> tests =
{
{ "empty string", "", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("0CC175B9C0F1B6A831C399E269772661")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("900150983CD24FB0D6963F7D28E17F72")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("F96B697D7CB7938D525A2F31AAF161D0")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("C3FCD3D76192E4007DFB496CCA67E13B")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("D174AB98D277D9F5A5611C2C9F419D9F")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("57EDF4A22BE3C955AC49DA2E2107B67A")},
};
tc::crypto::Md5Generator calc;
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize();
calc.update((const byte_t*)test->in_string.c_str(), test->in_string.size());
memset(hash.data(), 0xff, hash.size());
calc.getHash(hash.data());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_MultiUpdateCall()
{
std::cout << "[tc::crypto::Md5Generator] test_MultiUpdateCall : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests
std::vector<TestCase> tests =
{
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("0CC175B9C0F1B6A831C399E269772661")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("900150983CD24FB0D6963F7D28E17F72")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("F96B697D7CB7938D525A2F31AAF161D0")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("C3FCD3D76192E4007DFB496CCA67E13B")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("D174AB98D277D9F5A5611C2C9F419D9F")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("57EDF4A22BE3C955AC49DA2E2107B67A")},
};
tc::crypto::Md5Generator calc;
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize();
// pick an offset to split the in_string at
size_t offset = test->in_string.size() / 2;
// update with first half
calc.update((const byte_t*)test->in_string.c_str(), offset);
// update with second half
calc.update((const byte_t*)test->in_string.c_str() + offset, test->in_string.size() - offset);
memset(hash.data(), 0xff, hash.size());
calc.getHash(hash.data());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_UtilFunc()
{
std::cout << "[tc::crypto::Md5Generator] test_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests
std::vector<TestCase> tests =
{
{ "empty string", "", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("0CC175B9C0F1B6A831C399E269772661")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("900150983CD24FB0D6963F7D28E17F72")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("F96B697D7CB7938D525A2F31AAF161D0")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("C3FCD3D76192E4007DFB496CCA67E13B")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("D174AB98D277D9F5A5611C2C9F419D9F")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("57EDF4A22BE3C955AC49DA2E2107B67A")},
};
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
memset(hash.data(), 0xff, hash.size());
tc::crypto::GenerateMd5Hash(hash.data(), (const byte_t*)test->in_string.c_str(), test->in_string.size());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_NoInitNoUpdateDoHash()
{
std::cout << "[tc::crypto::Md5Generator] test_NoInitNoUpdateDoHash : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests (when not initalized getHash() should not populate the hash buffer, and so the hash buffer should remain as what we set it)
std::vector<TestCase> tests =
{
{ "empty string", "", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
};
tc::crypto::Md5Generator calc;
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize();
//calc.update((const byte_t*)test->in_string.c_str(), test->in_string.size());
memset(hash.data(), 0xff, hash.size());
calc.getHash(hash.data());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_NoInitDoUpdateDoHash()
{
std::cout << "[tc::crypto::Md5Generator] test_NoInitDoUpdateDoHash : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests (when not initalized getHash() should not populate the hash buffer, and so the hash buffer should remain as what we set it)
std::vector<TestCase> tests =
{
{ "empty string", "", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")},
};
tc::crypto::Md5Generator calc;
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
//calc.initialize();
calc.update((const byte_t*)test->in_string.c_str(), test->in_string.size());
memset(hash.data(), 0xff, hash.size());
calc.getHash(hash.data());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_DoInitNoUpdateDoHash()
{
std::cout << "[tc::crypto::Md5Generator] test_DoInitNoUpdateDoHash : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests (.getHash() should return the hash for an empty string if update is not called since they are logically the same thing)
std::vector<TestCase> tests =
{
{ "empty string", "", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
};
tc::crypto::Md5Generator calc;
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize();
//calc.update((const byte_t*)test->in_string.c_str(), test->in_string.size());
memset(hash.data(), 0xff, hash.size());
calc.getHash(hash.data());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Md5Generator_TestClass::test_CallGetHashRepeatedly()
{
std::cout << "[tc::crypto::Md5Generator] test_CallGetHashRepeatedly : " << std::flush;
try
{
try
{
std::stringstream ss;
struct TestCase
{
std::string test_name;
std::string in_string;
tc::ByteData out_hash;
};
// create tests
std::vector<TestCase> tests =
{
{ "empty string", "", tc::cli::FormatUtil::hexStringToBytes("D41D8CD98F00B204E9800998ECF8427E")},
{ "short string (\"a\")", "a", tc::cli::FormatUtil::hexStringToBytes("0CC175B9C0F1B6A831C399E269772661")},
{ "short string (\"abc\")", "abc", tc::cli::FormatUtil::hexStringToBytes("900150983CD24FB0D6963F7D28E17F72")},
{ "long string (\"message digest\")", "message digest", tc::cli::FormatUtil::hexStringToBytes("F96B697D7CB7938D525A2F31AAF161D0")},
{ "long string (alphabet)", "abcdefghijklmnopqrstuvwxyz", tc::cli::FormatUtil::hexStringToBytes("C3FCD3D76192E4007DFB496CCA67E13B")},
{ "long string (alphanum)", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", tc::cli::FormatUtil::hexStringToBytes("D174AB98D277D9F5A5611C2C9F419D9F")},
{ "long string (numerals)", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", tc::cli::FormatUtil::hexStringToBytes("57EDF4A22BE3C955AC49DA2E2107B67A")},
};
tc::crypto::Md5Generator calc;
tc::ByteData hash = tc::ByteData(tc::crypto::Md5Generator::kHashSize);
for (auto test = tests.begin(); test != tests.end(); test++)
{
calc.initialize();
calc.update((const byte_t*)test->in_string.c_str(), test->in_string.size());
for (size_t i = 0; i < 100; i++)
{
// by resetting the hash here we can tell if it is updated each time
memset(hash.data(), 0xff, hash.size());
calc.getHash(hash.data());
if (memcmp(hash.data(), test->out_hash.data(), hash.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong hash value: " << tc::cli::FormatUtil::formatBytesAsString(hash, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_hash, true, "");
throw tc::Exception(ss.str());
}
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,18 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_Md5Generator_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_SingleUpdateCall();
void test_MultiUpdateCall();
void test_UtilFunc();
void test_NoInitNoUpdateDoHash();
void test_NoInitDoUpdateDoHash();
void test_DoInitNoUpdateDoHash();
void test_CallGetHashRepeatedly();
};
@@ -1,285 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Pbkdf1Md5KeyDeriver_TestClass.h"
#include "PbkdfUtil.h"
#include <tc/crypto/Pbkdf1Md5KeyDeriver.h>
#include <tc/cli/FormatUtil.h>
void crypto_Pbkdf1Md5KeyDeriver_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] START" << std::endl;
test_Constants();
test_ConfirmTestVector_Class();
test_ConfirmTestVector_UtilFunc();
test_WillThrowExceptionOnZeroRounds();
if (std::numeric_limits<size_t>::max() < std::numeric_limits<uint64_t>::max())
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : SKIP (Cannot perform this test non 64bit systems)" << std::endl;
}
else
{
test_WillThrowExceptionOnTooLargeDkSize();
}
test_GetBytesWithoutInitDoesNothing();
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] END" << std::endl;
}
void crypto_Pbkdf1Md5KeyDeriver_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check max derivable size
static const uint64_t kExpectedMaxDerivableSize = tc::crypto::Md5Generator::kHashSize;
if (tc::crypto::Pbkdf1Md5KeyDeriver::kMaxDerivableSize != kExpectedMaxDerivableSize)
{
ss << "kMaxDerivableSize had value " << std::dec << tc::crypto::Pbkdf1Md5KeyDeriver::kMaxDerivableSize << " (expected " << kExpectedMaxDerivableSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Md5KeyDeriver_TestClass::test_ConfirmTestVector_Class()
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_ConfirmTestVector_Class : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::MD5);
tc::crypto::Pbkdf1Md5KeyDeriver keydev;
for (auto test = tests.begin(); test != tests.end(); test++)
{
keydev.initialize((const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
auto dk = tc::ByteData(test->in_dk_len);
keydev.getBytes(dk.data(), dk.size());
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Md5KeyDeriver_TestClass::test_ConfirmTestVector_UtilFunc()
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_ConfirmTestVector_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::MD5);
for (auto test = tests.begin(); test != tests.end(); test++)
{
auto dk = tc::ByteData(test->in_dk_len);
tc::crypto::DeriveKeyPbkdf1Md5(dk.data(), dk.size(), (const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Md5KeyDeriver_TestClass::test_WillThrowExceptionOnZeroRounds()
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_WillThrowExceptionOnZeroRounds : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::MD5);
auto dk = tc::ByteData(tests[0].in_dk_len);
try
{
tc::crypto::DeriveKeyPbkdf1Md5(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("DeriveKeyPbkdf1Md5() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf1Md5KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("Pbkdf1Md5KeyDeriver::initialize() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Md5KeyDeriver_TestClass::test_WillThrowExceptionOnTooLargeDkSize()
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::MD5);
// derive a key larger than the maximum derivable size
auto dk = tc::ByteData(tc::crypto::Pbkdf1Md5KeyDeriver::kMaxDerivableSize + 1);
try
{
tc::crypto::DeriveKeyPbkdf1Md5(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
throw tc::Exception("DeriveKeyPbkdf1Md5() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf1Md5KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
keydev.getBytes(dk.data(), dk.size());
throw tc::Exception("Pbkdf1Md5KeyDeriver::getBytes() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Md5KeyDeriver_TestClass::test_GetBytesWithoutInitDoesNothing()
{
std::cout << "[tc::crypto::Pbkdf1Md5KeyDeriver] test_GetBytesWithoutInitDoesNothing : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::MD5);
auto dk = tc::ByteData(tests[0].in_dk_len);
memset(dk.data(), 0xab, dk.size());
tc::crypto::Pbkdf1Md5KeyDeriver keydev;
keydev.getBytes(dk.data(), dk.size());
byte_t cmp = 1;
for (size_t i = 0; i < dk.size(); i++)
{
cmp &= dk[i] == 0xab;
}
if (cmp != 1)
{
throw tc::Exception("getBytes() operated inspite of not being initialized.");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,15 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_Pbkdf1Md5KeyDeriver_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_ConfirmTestVector_Class();
void test_ConfirmTestVector_UtilFunc();
void test_WillThrowExceptionOnZeroRounds();
void test_WillThrowExceptionOnTooLargeDkSize();
void test_GetBytesWithoutInitDoesNothing();
};
@@ -1,285 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Pbkdf1Sha1KeyDeriver_TestClass.h"
#include "PbkdfUtil.h"
#include <tc/crypto/Pbkdf1Sha1KeyDeriver.h>
#include <tc/cli/FormatUtil.h>
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] START" << std::endl;
test_Constants();
test_ConfirmTestVector_Class();
test_ConfirmTestVector_UtilFunc();
test_WillThrowExceptionOnZeroRounds();
if (std::numeric_limits<size_t>::max() < std::numeric_limits<uint64_t>::max())
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : SKIP (Cannot perform this test non 64bit systems)" << std::endl;
}
else
{
test_WillThrowExceptionOnTooLargeDkSize();
}
test_GetBytesWithoutInitDoesNothing();
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] END" << std::endl;
}
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check max derivable size
static const uint64_t kExpectedMaxDerivableSize = tc::crypto::Sha1Generator::kHashSize;
if (tc::crypto::Pbkdf1Sha1KeyDeriver::kMaxDerivableSize != kExpectedMaxDerivableSize)
{
ss << "kMaxDerivableSize had value " << std::dec << tc::crypto::Pbkdf1Sha1KeyDeriver::kMaxDerivableSize << " (expected " << kExpectedMaxDerivableSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::test_ConfirmTestVector_Class()
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_ConfirmTestVector_Class : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::SHA1);
tc::crypto::Pbkdf1Sha1KeyDeriver keydev;
for (auto test = tests.begin(); test != tests.end(); test++)
{
keydev.initialize((const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
auto dk = tc::ByteData(test->in_dk_len);
keydev.getBytes(dk.data(), dk.size());
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::test_ConfirmTestVector_UtilFunc()
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_ConfirmTestVector_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::SHA1);
for (auto test = tests.begin(); test != tests.end(); test++)
{
auto dk = tc::ByteData(test->in_dk_len);
tc::crypto::DeriveKeyPbkdf1Sha1(dk.data(), dk.size(), (const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::test_WillThrowExceptionOnZeroRounds()
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_WillThrowExceptionOnZeroRounds : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::SHA1);
auto dk = tc::ByteData(tests[0].in_dk_len);
try
{
tc::crypto::DeriveKeyPbkdf1Sha1(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("DeriveKeyPbkdf1Sha1() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf1Sha1KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("Pbkdf1Sha1KeyDeriver::initialize() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::test_WillThrowExceptionOnTooLargeDkSize()
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::SHA1);
// derive a key larger than the maximum derivable size
auto dk = tc::ByteData(tc::crypto::Pbkdf1Sha1KeyDeriver::kMaxDerivableSize + 1);
try
{
tc::crypto::DeriveKeyPbkdf1Sha1(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
throw tc::Exception("DeriveKeyPbkdf1Sha1() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf1Sha1KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
keydev.getBytes(dk.data(), dk.size());
throw tc::Exception("Pbkdf1Sha1KeyDeriver::getBytes() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf1Sha1KeyDeriver_TestClass::test_GetBytesWithoutInitDoesNothing()
{
std::cout << "[tc::crypto::Pbkdf1Sha1KeyDeriver] test_GetBytesWithoutInitDoesNothing : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf1TestVectors_Custom(tests, PbkdfUtil::SHA1);
auto dk = tc::ByteData(tests[0].in_dk_len);
memset(dk.data(), 0xab, dk.size());
tc::crypto::Pbkdf1Sha1KeyDeriver keydev;
keydev.getBytes(dk.data(), dk.size());
byte_t cmp = 1;
for (size_t i = 0; i < dk.size(); i++)
{
cmp &= dk[i] == 0xab;
}
if (cmp != 1)
{
throw tc::Exception("getBytes() operated inspite of not being initialized.");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,15 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_Pbkdf1Sha1KeyDeriver_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_ConfirmTestVector_Class();
void test_ConfirmTestVector_UtilFunc();
void test_WillThrowExceptionOnZeroRounds();
void test_WillThrowExceptionOnTooLargeDkSize();
void test_GetBytesWithoutInitDoesNothing();
};
@@ -1,286 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Pbkdf2Sha1KeyDeriver_TestClass.h"
#include "PbkdfUtil.h"
#include <tc/crypto/Pbkdf2Sha1KeyDeriver.h>
#include <tc/cli/FormatUtil.h>
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] START" << std::endl;
test_Constants();
test_ConfirmTestVector_Class();
test_ConfirmTestVector_UtilFunc();
test_WillThrowExceptionOnZeroRounds();
if (std::numeric_limits<size_t>::max() < std::numeric_limits<uint64_t>::max())
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : SKIP (Cannot perform this test non 64bit systems)" << std::endl;
}
else
{
test_WillThrowExceptionOnTooLargeDkSize();
}
test_GetBytesWithoutInitDoesNothing();
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] END" << std::endl;
}
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check max derivable size
static const uint64_t kExpectedMaxDerivableSize = uint64_t(0xffffffff) * uint64_t(tc::crypto::Sha1Generator::kHashSize);
if (tc::crypto::Pbkdf2Sha1KeyDeriver::kMaxDerivableSize != kExpectedMaxDerivableSize)
{
ss << "kMaxDerivableSize had value " << std::dec << tc::crypto::Pbkdf2Sha1KeyDeriver::kMaxDerivableSize << " (expected " << kExpectedMaxDerivableSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::test_ConfirmTestVector_Class()
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_ConfirmTestVector_Class : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA1);
tc::crypto::Pbkdf2Sha1KeyDeriver keydev;
for (auto test = tests.begin(); test != tests.end(); test++)
{
keydev.initialize((const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
auto dk = tc::ByteData(test->in_dk_len);
keydev.getBytes(dk.data(), dk.size());
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::test_ConfirmTestVector_UtilFunc()
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_ConfirmTestVector_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA1);
for (auto test = tests.begin(); test != tests.end(); test++)
{
auto dk = tc::ByteData(test->in_dk_len);
tc::crypto::DeriveKeyPbkdf2Sha1(dk.data(), dk.size(), (const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::test_WillThrowExceptionOnZeroRounds()
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_WillThrowExceptionOnZeroRounds : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA1);
auto dk = tc::ByteData(tests[0].in_dk_len);
try
{
tc::crypto::DeriveKeyPbkdf2Sha1(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("DeriveKeyPbkdf2Sha1() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf2Sha1KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("Pbkdf2Sha1KeyDeriver::initialize() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::test_WillThrowExceptionOnTooLargeDkSize()
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA1);
try
{
// nullptr because we expect this to fail outright and allocating kMaxDerivableSize+1 is too large
tc::crypto::DeriveKeyPbkdf2Sha1(nullptr, tc::crypto::Pbkdf2Sha1KeyDeriver::kMaxDerivableSize + 1, (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
throw tc::Exception("DeriveKeyPbkdf2Sha1() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf2Sha1KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
// nullptr because we expect this to fail outright and allocating kMaxDerivableSize+1 is too large
keydev.getBytes(nullptr, tc::crypto::Pbkdf2Sha1KeyDeriver::kMaxDerivableSize + 1);
throw tc::Exception("Pbkdf2Sha1KeyDeriver::getBytes() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha1KeyDeriver_TestClass::test_GetBytesWithoutInitDoesNothing()
{
std::cout << "[tc::crypto::Pbkdf2Sha1KeyDeriver] test_GetBytesWithoutInitDoesNothing : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA1);
auto dk = tc::ByteData(tests[0].in_dk_len);
memset(dk.data(), 0xab, dk.size());
tc::crypto::Pbkdf2Sha1KeyDeriver keydev;
keydev.getBytes(dk.data(), dk.size());
byte_t cmp = 1;
for (size_t i = 0; i < dk.size(); i++)
{
cmp &= dk[i] == 0xab;
}
if (cmp != 1)
{
throw tc::Exception("getBytes() operated inspite of not being initialized.");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,15 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_Pbkdf2Sha1KeyDeriver_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_ConfirmTestVector_Class();
void test_ConfirmTestVector_UtilFunc();
void test_WillThrowExceptionOnZeroRounds();
void test_WillThrowExceptionOnTooLargeDkSize();
void test_GetBytesWithoutInitDoesNothing();
};
@@ -1,287 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Pbkdf2Sha256KeyDeriver_TestClass.h"
#include "PbkdfUtil.h"
#include <tc/crypto/Pbkdf2Sha256KeyDeriver.h>
#include <tc/cli/FormatUtil.h>
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] START" << std::endl;
test_Constants();
test_ConfirmTestVector_Class();
test_ConfirmTestVector_UtilFunc();
test_WillThrowExceptionOnZeroRounds();
if (std::numeric_limits<size_t>::max() < std::numeric_limits<uint64_t>::max())
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : SKIP (Cannot perform this test non 64bit systems)" << std::endl;
}
else
{
test_WillThrowExceptionOnTooLargeDkSize();
}
test_GetBytesWithoutInitDoesNothing();
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] END" << std::endl;
}
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check max derivable size
static const uint64_t kExpectedMaxDerivableSize = uint64_t(0xffffffff) * uint64_t(tc::crypto::Sha256Generator::kHashSize);
if (tc::crypto::Pbkdf2Sha256KeyDeriver::kMaxDerivableSize != kExpectedMaxDerivableSize)
{
ss << "kMaxDerivableSize had value " << std::dec << tc::crypto::Pbkdf2Sha256KeyDeriver::kMaxDerivableSize << " (expected " << kExpectedMaxDerivableSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::test_ConfirmTestVector_Class()
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_ConfirmTestVector_Class : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA256);
tc::crypto::Pbkdf2Sha256KeyDeriver keydev;
for (auto test = tests.begin(); test != tests.end(); test++)
{
keydev.initialize((const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
auto dk = tc::ByteData(test->in_dk_len);
keydev.getBytes(dk.data(), dk.size());
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::test_ConfirmTestVector_UtilFunc()
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_ConfirmTestVector_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA256);
for (auto test = tests.begin(); test != tests.end(); test++)
{
auto dk = tc::ByteData(test->in_dk_len);
tc::crypto::DeriveKeyPbkdf2Sha256(dk.data(), dk.size(), (const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::test_WillThrowExceptionOnZeroRounds()
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_WillThrowExceptionOnZeroRounds : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA256);
auto dk = tc::ByteData(tests[0].in_dk_len);
try
{
tc::crypto::DeriveKeyPbkdf2Sha256(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("DeriveKeyPbkdf2Sha256() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf2Sha256KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("Pbkdf2Sha256KeyDeriver::initialize() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::test_WillThrowExceptionOnTooLargeDkSize()
{
// this test relies on size_t being 64bit, since the maximum kMaxDerivableSize is uint64_t, manually generating kMaxDerivableSize bytes would take very long. The point of this test is to refuse it outright which can't be done on 32bit systems.
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA256);
try
{
// nullptr because we expect this to fail outright and allocating kMaxDerivableSize+1 is too large
tc::crypto::DeriveKeyPbkdf2Sha256(nullptr, tc::crypto::Pbkdf2Sha256KeyDeriver::kMaxDerivableSize + 1, (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
throw tc::Exception("DeriveKeyPbkdf2Sha256() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf2Sha256KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
// nullptr because we expect this to fail outright and allocating kMaxDerivableSize+1 is too large
keydev.getBytes(nullptr, tc::crypto::Pbkdf2Sha256KeyDeriver::kMaxDerivableSize + 1);
throw tc::Exception("Pbkdf2Sha256KeyDeriver::getBytes() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha256KeyDeriver_TestClass::test_GetBytesWithoutInitDoesNothing()
{
std::cout << "[tc::crypto::Pbkdf2Sha256KeyDeriver] test_GetBytesWithoutInitDoesNothing : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA256);
auto dk = tc::ByteData(tests[0].in_dk_len);
memset(dk.data(), 0xab, dk.size());
tc::crypto::Pbkdf2Sha256KeyDeriver keydev;
keydev.getBytes(dk.data(), dk.size());
byte_t cmp = 1;
for (size_t i = 0; i < dk.size(); i++)
{
cmp &= dk[i] == 0xab;
}
if (cmp != 1)
{
throw tc::Exception("getBytes() operated in spite of not being initialized.");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,15 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_Pbkdf2Sha256KeyDeriver_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_ConfirmTestVector_Class();
void test_ConfirmTestVector_UtilFunc();
void test_WillThrowExceptionOnZeroRounds();
void test_WillThrowExceptionOnTooLargeDkSize();
void test_GetBytesWithoutInitDoesNothing();
};
@@ -1,286 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Pbkdf2Sha512KeyDeriver_TestClass.h"
#include "PbkdfUtil.h"
#include <tc/crypto/Pbkdf2Sha512KeyDeriver.h>
#include <tc/cli/FormatUtil.h>
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] START" << std::endl;
test_Constants();
test_ConfirmTestVector_Class();
test_ConfirmTestVector_UtilFunc();
test_WillThrowExceptionOnZeroRounds();
if (std::numeric_limits<size_t>::max() < std::numeric_limits<uint64_t>::max())
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : SKIP (Cannot perform this test non 64bit systems)" << std::endl;
}
else
{
test_WillThrowExceptionOnTooLargeDkSize();
}
test_GetBytesWithoutInitDoesNothing();
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] END" << std::endl;
}
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check max derivable size
static const uint64_t kExpectedMaxDerivableSize = uint64_t(0xffffffff) * uint64_t(tc::crypto::Sha512Generator::kHashSize);
if (tc::crypto::Pbkdf2Sha512KeyDeriver::kMaxDerivableSize != kExpectedMaxDerivableSize)
{
ss << "kMaxDerivableSize had value " << std::dec << tc::crypto::Pbkdf2Sha512KeyDeriver::kMaxDerivableSize << " (expected " << kExpectedMaxDerivableSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::test_ConfirmTestVector_Class()
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_ConfirmTestVector_Class : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA512);
tc::crypto::Pbkdf2Sha512KeyDeriver keydev;
for (auto test = tests.begin(); test != tests.end(); test++)
{
keydev.initialize((const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
auto dk = tc::ByteData(test->in_dk_len);
keydev.getBytes(dk.data(), dk.size());
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::test_ConfirmTestVector_UtilFunc()
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_ConfirmTestVector_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA512);
for (auto test = tests.begin(); test != tests.end(); test++)
{
auto dk = tc::ByteData(test->in_dk_len);
tc::crypto::DeriveKeyPbkdf2Sha512(dk.data(), dk.size(), (const byte_t*)test->in_password.c_str(), test->in_password.size(), (const byte_t*)test->in_salt.c_str(), test->in_salt.size(), test->in_rounds);
if (memcmp(dk.data(), test->out_dk.data(), dk.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed. Had wrong DK: " << tc::cli::FormatUtil::formatBytesAsString(dk, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->out_dk, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::test_WillThrowExceptionOnZeroRounds()
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_WillThrowExceptionOnZeroRounds : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA512);
auto dk = tc::ByteData(tests[0].in_dk_len);
try
{
tc::crypto::DeriveKeyPbkdf2Sha512(dk.data(), dk.size(), (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("DeriveKeyPbkdf2Sha512() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf2Sha512KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), 0);
throw tc::Exception("Pbkdf2Sha512KeyDeriver::initialize() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::test_WillThrowExceptionOnTooLargeDkSize()
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_WillThrowExceptionOnTooLargeDkSize : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA512);
try
{
// nullptr because we expect this to fail outright and allocating kMaxDerivableSize+1 is too large
tc::crypto::DeriveKeyPbkdf2Sha512(nullptr, tc::crypto::Pbkdf2Sha512KeyDeriver::kMaxDerivableSize + 1, (const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
throw tc::Exception("DeriveKeyPbkdf2Sha512() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
try
{
tc::crypto::Pbkdf2Sha512KeyDeriver keydev;
keydev.initialize((const byte_t*)tests[0].in_password.c_str(), tests[0].in_password.size(), (const byte_t*)tests[0].in_salt.c_str(), tests[0].in_salt.size(), tests[0].in_rounds);
// nullptr because we expect this to fail outright and allocating kMaxDerivableSize+1 is too large
keydev.getBytes(nullptr, tc::crypto::Pbkdf2Sha512KeyDeriver::kMaxDerivableSize + 1);
throw tc::Exception("Pbkdf2Sha512KeyDeriver::getBytes() Did not throw exception");
} catch (const tc::crypto::CryptoException&)
{
// do nothing
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Pbkdf2Sha512KeyDeriver_TestClass::test_GetBytesWithoutInitDoesNothing()
{
std::cout << "[tc::crypto::Pbkdf2Sha512KeyDeriver] test_GetBytesWithoutInitDoesNothing : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<PbkdfUtil::TestVector> tests;
PbkdfUtil::generatePbkdf2TestVectors_RFC6070(tests, PbkdfUtil::SHA512);
auto dk = tc::ByteData(tests[0].in_dk_len);
memset(dk.data(), 0xab, dk.size());
tc::crypto::Pbkdf2Sha512KeyDeriver keydev;
keydev.getBytes(dk.data(), dk.size());
byte_t cmp = 1;
for (size_t i = 0; i < dk.size(); i++)
{
cmp &= dk[i] == 0xab;
}
if (cmp != 1)
{
throw tc::Exception("getBytes() operated inspite of not being initialized.");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,15 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_Pbkdf2Sha512KeyDeriver_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_ConfirmTestVector_Class();
void test_ConfirmTestVector_UtilFunc();
void test_WillThrowExceptionOnZeroRounds();
void test_WillThrowExceptionOnTooLargeDkSize();
void test_GetBytesWithoutInitDoesNothing();
};
@@ -1,226 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_PseudoRandomByteGenerator_TestClass.h"
#include <tc/crypto/PseudoRandomByteGenerator.h>
#include <tc/io/PaddingSource.h>
#include <tc/cli/FormatUtil.h>
void crypto_PseudoRandomByteGenerator_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::PseudoRandomByteGenerator] START" << std::endl;
test_Class();
test_UtilFunc();
test_MultipleObjectsCreateDifferentData();
test_RepeatedCallsCreateDifferentData();
std::cout << "[tc::crypto::PseudoRandomByteGenerator] END" << std::endl;
}
void crypto_PseudoRandomByteGenerator_TestClass::test_Class()
{
std::cout << "[tc::crypto::PseudoRandomByteGenerator] test_Class : " << std::flush;
try
{
try
{
std::stringstream ss;
// create buffer to hold random data
auto random_data = tc::ByteData(0x20);
// generate control_data to compare to random_data to ensure that it is populated with random bytes
auto control_data = tc::io::PaddingSource(0xbe, random_data.size()).pullData(0, random_data.size());
// copy control data
memcpy(random_data.data(), control_data.data(), random_data.size());
// generate random bytes
tc::crypto::PseudoRandomByteGenerator prbg;
prbg.getBytes(random_data.data(), random_data.size());
// compare with control data to see if the data changed
if (memcmp(random_data.data(), control_data.data(), random_data.size()) == 0)
{
throw tc::Exception(".getBytes() did not populate array");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_PseudoRandomByteGenerator_TestClass::test_UtilFunc()
{
std::cout << "[tc::crypto::PseudoRandomByteGenerator] test_UtilFunc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create buffer to hold random data
auto random_data = tc::ByteData(0x20);
// generate control_data to compare to random_data to ensure that it is populated with random bytes
auto control_data = tc::io::PaddingSource(0xbe, random_data.size()).pullData(0, random_data.size());
// copy control data
memcpy(random_data.data(), control_data.data(), random_data.size());
// generate random bytes
tc::crypto::GeneratePseudoRandomBytes(random_data.data(), random_data.size());
// compare with control data to see if the data changed
if (memcmp(random_data.data(), control_data.data(), random_data.size()) == 0)
{
throw tc::Exception("GeneratePseudoRandomBytes() did not populate array");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_PseudoRandomByteGenerator_TestClass::test_MultipleObjectsCreateDifferentData()
{
std::cout << "[tc::crypto::PseudoRandomByteGenerator] test_MultipleObjectsCreateDifferentData : " << std::flush;
try
{
try
{
std::stringstream ss;
// generate allocate storage for each test
static const size_t kRandomDataSize = 0x20;
auto random_data1 = tc::ByteData(kRandomDataSize);
auto random_data2 = tc::ByteData(kRandomDataSize);
auto random_data3 = tc::ByteData(kRandomDataSize);
// create PRBG objects
tc::crypto::PseudoRandomByteGenerator prbg1, prbg2, prbg3;
// generate random data
prbg1.getBytes(random_data1.data(), random_data1.size());
prbg2.getBytes(random_data2.data(), random_data2.size());
prbg3.getBytes(random_data3.data(), random_data3.size());
size_t cmp12 = 0, cmp13 = 0, cmp23 = 0;
for (size_t i = 0; i < kRandomDataSize; i++)
{
cmp12 += random_data1[i] == random_data2[i];
cmp13 += random_data1[i] == random_data3[i];
cmp23 += random_data2[i] == random_data3[i];
}
// check to see if any of the tests were similar
static const size_t kSimilarityThreshold = 2;
if (cmp12 > kSimilarityThreshold)
{
ss << "case 1 & case 2 has " << std::dec << cmp12 << " similar bytes" ;
throw tc::Exception(ss.str());
}
if (cmp13 > kSimilarityThreshold)
{
ss << "case 1 & case 3 has " << std::dec << cmp13 << " similar bytes" ;
throw tc::Exception(ss.str());
}
if (cmp23 > kSimilarityThreshold)
{
ss << "case 2 & case 3 has " << std::dec << cmp23 << " similar bytes" ;
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_PseudoRandomByteGenerator_TestClass::test_RepeatedCallsCreateDifferentData()
{
std::cout << "[tc::crypto::PseudoRandomByteGenerator] test_RepeatedCallsCreateDifferentData : " << std::flush;
try
{
try
{
std::stringstream ss;
// generate allocate storage for each test
static const size_t kRandomDataSize = 0x20;
auto random_data1 = tc::ByteData(kRandomDataSize);
auto random_data2 = tc::ByteData(kRandomDataSize);
auto random_data3 = tc::ByteData(kRandomDataSize);
// create PRBG object
tc::crypto::PseudoRandomByteGenerator prbg;
// generate random data
prbg.getBytes(random_data1.data(), random_data1.size());
prbg.getBytes(random_data2.data(), random_data2.size());
prbg.getBytes(random_data3.data(), random_data3.size());
size_t cmp12 = 0, cmp13 = 0, cmp23 = 0;
for (size_t i = 0; i < kRandomDataSize; i++)
{
cmp12 += random_data1[i] == random_data2[i];
cmp13 += random_data1[i] == random_data3[i];
cmp23 += random_data2[i] == random_data3[i];
}
// check to see if any of the tests were similar
static const size_t kSimilarityThreshold = 2;
if (cmp12 > kSimilarityThreshold)
{
ss << "case 1 & case 2 has " << std::dec << cmp12 << " similar bytes" ;
throw tc::Exception(ss.str());
}
if (cmp13 > kSimilarityThreshold)
{
ss << "case 1 & case 3 has " << std::dec << cmp13 << " similar bytes" ;
throw tc::Exception(ss.str());
}
if (cmp23 > kSimilarityThreshold)
{
ss << "case 2 & case 3 has " << std::dec << cmp23 << " similar bytes" ;
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,14 +0,0 @@
#pragma once
#include "ITestClass.h"
class crypto_PseudoRandomByteGenerator_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Class();
void test_UtilFunc();
void test_MultipleObjectsCreateDifferentData();
void test_RepeatedCallsCreateDifferentData();
};
@@ -1,673 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Rsa1024OaepSha256Encryptor_TestClass.h"
#include "RsaOaepUtil.h"
#include <tc/Exception.h>
#include <tc/crypto/RsaOaepSha256Encryptor.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Rsa1024OaepSha256Encryptor_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] START" << std::endl;
test_Constants();
test_UseClassDec();
test_UseClassEnc();
test_UseUtilFuncDec();
test_UseUtilFuncEnc();
test_UnspecifiedSeedProducesDifferentBlock();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_EncryptReturnsFalseOnBadInput();
test_DecryptReturnsFalseOnBadInput();
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] END" << std::endl;
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check block size
static const size_t kExpectedBlockSize = 1024 >> 3;
if (tc::crypto::Rsa1024OaepSha256Encryptor::kBlockSize != kExpectedBlockSize)
{
ss << "kBlockSize had value " << std::dec << tc::crypto::Rsa1024OaepSha256Encryptor::kBlockSize << " (expected " << kExpectedBlockSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_UseClassDec()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_UseClassDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->dec_message.size());
// initialize
cryptor.initialize(tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()), test->label.data(), test->label.size(), test->label_is_digested);
// clear data
memset(data.data(), 0xff, data.size());
// test decryption
size_t message_size = 0;
cryptor.decrypt(data.data(), message_size, data.size(), test->enc_message.data());
if (message_size != test->dec_message.size())
{
ss << "Test \"" << test->test_name << "\" Failed: message_size = " << message_size << " (expected " << test->dec_message.size() << ")";
throw tc::Exception(ss.str());
}
if (memcmp(data.data(), test->dec_message.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->dec_message, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_UseClassEnc()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_UseClassEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->enc_message.size());
// initialize
cryptor.initialize(tc::crypto::RsaPublicKey(test->key_modulus.data(), test->key_modulus.size()), test->label.data(), test->label.size(), test->label_is_digested);
// clear data
memset(data.data(), 0xff, data.size());
// test encryption
cryptor.encrypt(data.data(), test->dec_message.data(), test->dec_message.size(), test->enc_seed.data(), test->enc_seed.size());
if (memcmp(data.data(), test->enc_message.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->enc_message, true, "");
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_UseUtilFuncDec()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_UseUtilFuncDec : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData data = tc::ByteData(test->dec_message.size());
// clear data
memset(data.data(), 0xff, data.size());
// test decryption
size_t message_size = 0;
bool res = tc::crypto::DecryptRsa1024OaepSha256(data.data(), message_size, data.size(), test->enc_message.data(), tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()), test->label.data(), test->label.size(), test->label_is_digested);
if (res != true)
{
ss << "Test \"" << test->test_name << "\" Failed: DecryptRsa1024OaepSha256 returned " << std::boolalpha << res << " (expected " << std::boolalpha << true << ")";
throw tc::Exception(ss.str());
}
if (message_size != test->dec_message.size())
{
ss << "Test \"" << test->test_name << "\" Failed: message_size = " << message_size << " (expected " << test->dec_message.size() << ")";
throw tc::Exception(ss.str());
}
if (memcmp(data.data(), test->dec_message.data(), data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->dec_message, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_UseUtilFuncEnc()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_UseUtilFuncEnc : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
// the utility function doesn't support specifying a seed, so we'll have to do some `lite` validation using the already validated decryption utility.
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData dec_data = tc::ByteData(test->dec_message.size());
tc::ByteData enc_data = tc::ByteData(test->enc_message.size());
// clear data
memset(dec_data.data(), 0xff, dec_data.size());
memset(enc_data.data(), 0xff, enc_data.size());
// test encryption
bool res = tc::crypto::EncryptRsa1024OaepSha256(enc_data.data(), test->dec_message.data(), test->dec_message.size(), tc::crypto::RsaPublicKey(test->key_modulus.data(), test->key_modulus.size()), test->label.data(), test->label.size(), test->label_is_digested);
if (res != true)
{
ss << "Test \"" << test->test_name << "\" Failed: EncryptRsa1024OaepSha256 returned " << std::boolalpha << res << " (expected " << std::boolalpha << true << ")";
throw tc::Exception(ss.str());
}
// try to decrypt message
size_t message_size = 0;
res = tc::crypto::DecryptRsa1024OaepSha256(dec_data.data(), message_size, dec_data.size(), enc_data.data(), tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()), test->label.data(), test->label.size(), test->label_is_digested);
if (res != true)
{
ss << "Test \"" << test->test_name << "\" Failed: encrypted message could not be decrypted";
throw tc::Exception(ss.str());
}
if (message_size != test->dec_message.size())
{
ss << "Test \"" << test->test_name << "\" Failed: message_size = " << message_size << " (expected " << test->dec_message.size() << ")";
throw tc::Exception(ss.str());
}
if (memcmp(dec_data.data(), test->dec_message.data(), dec_data.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: decrypted message was not expected " << tc::cli::FormatUtil::formatBytesAsString(dec_data, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->dec_message, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_UnspecifiedSeedProducesDifferentBlock()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_UnspecifiedSeedProducesDifferentBlock : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
// initialize
cryptor.initialize(tc::crypto::RsaPublicKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()), tests[0].label.data(), tests[0].label.size(), tests[0].label_is_digested);
static const size_t kTestNum = 20;
std::vector<tc::ByteData> enc_messages;
int difference_score = 0;
for (size_t i = 0; i < kTestNum; i++)
{
// create message buffer
tc::ByteData msg = tc::ByteData(tests[0].enc_message.size());
// encrypt using auto seed generator
cryptor.encrypt(msg.data(), tests[0].dec_message.data(), tests[0].dec_message.size());
// add message to vector
enc_messages.push_back(msg);
// compare this message to the previous messages
for (size_t j = 0; j < i; j++)
{
difference_score += memcmp(enc_messages[i].data(), enc_messages[j].data(), tests[0].enc_message.size()) == 0;
}
}
if (difference_score != 0)
{
throw tc::Exception("Failed to generate unique encrypted messages.");
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
bool res;
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
// create data
tc::ByteData control_message = tc::io::PaddingSource(0xee, 0x20).pullData(0, 0x20);
tc::ByteData message = tc::ByteData(control_message.data(), control_message.size());
tc::ByteData control_block = tc::io::PaddingSource(0xdd, tc::crypto::Rsa1024OaepSha256Encryptor::kBlockSize).pullData(0, tc::crypto::Rsa1024OaepSha256Encryptor::kBlockSize);
tc::ByteData block = tc::ByteData(control_block);
size_t control_message_size = 0x1337;
size_t message_size = control_message_size;
// try to decrypt without calling initialize()
res = cryptor.decrypt(message.data(), message_size, message.size(), block.data());
if (res != false)
{
ss << "Failed: decrypt() returned true when not initialized";
throw tc::Exception(ss.str());
}
if (message_size != control_message_size)
{
ss << "Failed: decrypt() modified message_size when not initialized";
throw tc::Exception(ss.str());
}
if (memcmp(message.data(), control_message.data(), message.size()) != 0)
{
ss << "Failed: decrypt() operated on message when not initialized";
throw tc::Exception(ss.str());
}
// try to encrypt without calling initialize()
res = cryptor.encrypt(block.data(), message.data(), message.size());
if (res != false)
{
ss << "Failed: encrypt() returned true when not initialized";
throw tc::Exception(ss.str());
}
if (memcmp(block.data(), control_block.data(), block.size()) != 0)
{
ss << "Failed: encrypt() operated on block when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
// reference initialize call
//cryptor.initialize(tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()), tests[0].label.data(), tests[0].label.size(), false);
auto label = tc::io::PaddingSource(0xab, tc::crypto::Sha256Generator::kHashSize).pullData(0, tc::crypto::Sha256Generator::kHashSize-2);
auto empty_label = tc::ByteData();
auto key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size());
auto empty_key = tc::crypto::RsaKey();
auto bad_modulus_size_key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()-2, tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size());
auto no_modulus_key = key;
no_modulus_key.n = tc::ByteData();
auto bad_privexp_size_key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()-2);
auto bad_pubexp_size_key = key;
bad_pubexp_size_key.e = tc::ByteData(5);
auto no_exponent_key = key;
no_exponent_key.d = tc::ByteData();
no_exponent_key.e = tc::ByteData();
try {
cryptor.initialize(empty_key, label.data(), label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey empty");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(bad_modulus_size_key, label.data(), label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad modulus size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(no_modulus_key, label.data(), label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a no modulus");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(bad_privexp_size_key, label.data(), label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad private exponent size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(bad_pubexp_size_key, label.data(), label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad public exponent size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(no_exponent_key, label.data(), label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a neither public nor private exponents");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(key, label.data(), label.size(), true);
throw tc::Exception("Failed to throw ArgumentOutOfRangeException where isLabelDigested==true but the label isn't the correct size");
} catch(const tc::ArgumentOutOfRangeException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(key, label.data(), 0, false);
throw tc::Exception("Failed to throw ArgumentNullException where label != nullptr but label_size == 0");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
cryptor.initialize(key, nullptr, label.size(), false);
throw tc::Exception("Failed to throw ArgumentNullException where label == nullptr but label_size != 0");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_EncryptReturnsFalseOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_EncryptReturnsFalseOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
cryptor.initialize(tc::crypto::RsaPublicKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()), tests[0].label.data(), tests[0].label.size(), false);
tc::ByteData data = tc::ByteData(tests[0].enc_message.size());
// reference encrypt call
//cryptor.encrypt(data.data(), tests[0].dec_message.data(), tests[0].dec_message.size(), tests[0].enc_seed.data(), tests[0].enc_seed.size());
bool result = false;
result = cryptor.encrypt(nullptr, tests[0].dec_message.data(), tests[0].dec_message.size(), tests[0].enc_seed.data(), tests[0].enc_seed.size());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where block==nullptr"); // ArgumentNullException
}
result = cryptor.encrypt(data.data(), nullptr, tests[0].dec_message.size(), tests[0].enc_seed.data(), tests[0].enc_seed.size());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where message==nullptr"); // ArgumentNullException
}
result = cryptor.encrypt(data.data(), tests[0].dec_message.data(), 0, tests[0].enc_seed.data(), tests[0].enc_seed.size());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where message_size==0"); // ArgumentOutOfRangeException
}
size_t max_message_size = tc::crypto::Rsa1024OaepSha256Encryptor::kBlockSize - (2 * tc::crypto::Sha256Generator::kHashSize) - 2;
result = cryptor.encrypt(data.data(), tests[0].dec_message.data(), max_message_size+1, tests[0].enc_seed.data(), tests[0].enc_seed.size());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where message_size was too large to be encrypted in one RSA-OAEP block"); // ArgumentOutOfRangeException
}
result = cryptor.encrypt(data.data(), tests[0].dec_message.data(), tests[0].dec_message.size(), tests[0].enc_seed.data(), 0);
if (result != false)
{
throw tc::Exception("encrypt() did not return false where seed_size!=HashCalculator::kHashSize"); // ArgumentOutOfRangeException
}
result = cryptor.encrypt(data.data(), tests[0].dec_message.data(), tests[0].dec_message.size(), nullptr, tests[0].enc_seed.size());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where seed==nullptr"); // ArgumentNullException
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024OaepSha256Encryptor_TestClass::test_DecryptReturnsFalseOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024OaepSha256Encryptor] test_DecryptReturnsFalseOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaOaepUtil::TestVector> tests;
RsaOaepUtil::generateRsaOaepTestVectors_Custom(tests, 1024, RsaOaepUtil::SHA256);
tc::crypto::Rsa1024OaepSha256Encryptor cryptor;
cryptor.initialize(tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()), tests[0].label.data(), tests[0].label.size(), false);
tc::ByteData data = tc::ByteData(tests[0].dec_message.size());
size_t message_size = 0;
// reference encrypt call
//cryptor.decrypt(data.data(), message_size, data.size(), tests[0].enc_message.data());
bool result = false;
result = cryptor.decrypt(nullptr, message_size, data.size(), tests[0].enc_message.data());
if (result != false)
{
throw tc::Exception("decrypt() did not return false where message==nullptr"); // ArgumentNullException
}
result = cryptor.decrypt(data.data(), message_size, 0, tests[0].enc_message.data());
if (result != false)
{
throw tc::Exception("decrypt() did not return false where message_capacity==0"); // ArgumentOutOfRangeException
}
result = cryptor.decrypt(data.data(), message_size, data.size(), nullptr);
if (result != false)
{
throw tc::Exception("decrypt() did not return false where block==nullptr"); // ArgumentNullException
}
result = cryptor.decrypt(data.data(), message_size, tests[0].dec_message.size()-1, tests[0].enc_message.data());
if (result != false)
{
throw tc::Exception("decrypt() did not return false where message_capacity was not large enough"); // ArgumentOutOfRangeException
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,23 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Rsa1024OaepSha256Encryptor_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassDec();
void test_UseClassEnc();
void test_UseUtilFuncDec();
void test_UseUtilFuncEnc();
void test_UnspecifiedSeedProducesDifferentBlock();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_EncryptReturnsFalseOnBadInput();
void test_DecryptReturnsFalseOnBadInput();
};
@@ -1,498 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Rsa1024Pkcs1Md5Signer_TestClass.h"
#include "RsaPkcs1Util.h"
#include <tc/Exception.h>
#include <tc/crypto/RsaPkcs1Md5Signer.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] START" << std::endl;
test_Constants();
test_UseClassSign();
test_UseClassVerify();
test_UseUtilFuncSign();
test_UseUtilFuncVerify();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_SignReturnsFalseOnBadInput();
test_VerifyReturnsFalseOnBadInput();
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] END" << std::endl;
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check signature size
static const size_t kExpectedSignatureSize = 1024 >> 3;
if (tc::crypto::Rsa1024Pkcs1Md5Signer::kSignatureSize != kExpectedSignatureSize)
{
ss << "kSignatureSize had value " << std::dec << tc::crypto::Rsa1024Pkcs1Md5Signer::kSignatureSize << " (expected " << kExpectedSignatureSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_UseClassSign()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_UseClassSign : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024Pkcs1Md5Signer signer;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData signature = tc::ByteData(test->signature.size());
// initialize
signer.initialize(tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()));
// clear data
memset(signature.data(), 0xff, signature.size());
// test sign
bool result = signer.sign(signature.data(), test->message_digest.data());
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: sign() returned false";
throw tc::Exception(ss.str());
}
if (memcmp(signature.data(), test->signature.data(), signature.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(signature, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->signature, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_UseClassVerify()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_UseClassVerify : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024Pkcs1Md5Signer signer;
for (auto test = tests.begin(); test != tests.end(); test++)
{
// initialize
signer.initialize(tc::crypto::RsaPublicKey(test->key_modulus.data(), test->key_modulus.size()));
// test verify
bool result = signer.verify(test->signature.data(), test->message_digest.data());
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: verify() returned false";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_UseUtilFuncSign()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_UseUtilFuncSign : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData signature = tc::ByteData(test->signature.size());
// clear data
memset(signature.data(), 0xff, signature.size());
// test sign
bool result = tc::crypto::SignRsa1024Pkcs1Md5(signature.data(), test->message_digest.data(), tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()));
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: sign() returned false";
throw tc::Exception(ss.str());
}
if (memcmp(signature.data(), test->signature.data(), signature.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(signature, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->signature, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_UseUtilFuncVerify()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_UseUtilFuncVerify : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
for (auto test = tests.begin(); test != tests.end(); test++)
{
// test verify
bool result = tc::crypto::VerifyRsa1024Pkcs1Md5(test->signature.data(), test->message_digest.data(), tc::crypto::RsaPublicKey(test->key_modulus.data(), test->key_modulus.size()));
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: verify() returned false";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
bool res;
tc::crypto::Rsa1024Pkcs1Md5Signer signer;
// create data
tc::ByteData hash = tc::ByteData(tests[0].message_digest.size());
tc::ByteData control_signature = tc::io::PaddingSource(0xee, tests[0].signature.size()).pullData(0, 0x20);
tc::ByteData signature = tc::ByteData(control_signature.data(), control_signature.size());
// try to sign without calling initialize()
res = signer.sign(signature.data(), hash.data());
if (res != false)
{
ss << "Failed: sign() returned true when not initialized";
throw tc::Exception(ss.str());
}
if (memcmp(signature.data(), control_signature.data(), signature.size()) != 0)
{
ss << "Failed: sign() operated on message when not initialized";
throw tc::Exception(ss.str());
}
// try to verify without calling initialize()
res = signer.verify(signature.data(), hash.data());
if (res != false)
{
ss << "Failed: verify() returned true when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
tc::crypto::Rsa1024Pkcs1Md5Signer signer;
// reference initialize call
//signer.initialize(tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()));
auto key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size());
auto empty_key = tc::crypto::RsaKey();
auto bad_modulus_size_key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()-2, tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size());
auto no_modulus_key = key;
no_modulus_key.n = tc::ByteData();
auto bad_privexp_size_key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()-2);
auto bad_pubexp_size_key = key;
bad_pubexp_size_key.e = tc::ByteData(5);
auto no_exponent_key = key;
no_exponent_key.d = tc::ByteData();
no_exponent_key.e = tc::ByteData();
try {
signer.initialize(empty_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey empty");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(bad_modulus_size_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad modulus size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(no_modulus_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a no modulus");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(bad_privexp_size_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad private exponent size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(bad_pubexp_size_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad public exponent size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(no_exponent_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a neither public nor private exponents");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_SignReturnsFalseOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_SignReturnsFalseOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
tc::crypto::Rsa1024Pkcs1Md5Signer signer;
signer.initialize(tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()));
tc::ByteData signature = tc::ByteData(tests[0].signature.size());
// reference sign call
//signer.sign(signature.data(), tests[0].message_digest.data());
bool result = false;
result = signer.sign(nullptr, tests[0].message_digest.data());
if (result != false)
{
throw tc::Exception("sign() did not return false where signature==nullptr"); // ArgumentNullException
}
result = signer.sign(signature.data(), nullptr);
if (result != false)
{
throw tc::Exception("sign() did not return false where message_digest==nullptr"); // ArgumentNullException
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Md5Signer_TestClass::test_VerifyReturnsFalseOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Md5Signer] test_VerifyReturnsFalseOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::MD5);
tc::crypto::Rsa1024Pkcs1Md5Signer signer;
signer.initialize(tc::crypto::RsaPublicKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()));
// reference verify call
//signer.verify(tests[0].signature.data(), tests[0].message_digest.data());
bool result = false;
result = signer.verify(nullptr, tests[0].message_digest.data());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where signature==nullptr"); // ArgumentNullException
}
result = signer.verify(tests[0].signature.data(), nullptr);
if (result != false)
{
throw tc::Exception("encrypt() did not return false where message_digest==nullptr"); // ArgumentNullException
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
@@ -1,22 +0,0 @@
#pragma once
#include "ITestClass.h"
#include <vector>
#include <tc/ByteData.h>
class crypto_Rsa1024Pkcs1Md5Signer_TestClass : public ITestClass
{
public:
void runAllTests();
private:
void test_Constants();
void test_UseClassSign();
void test_UseClassVerify();
void test_UseUtilFuncSign();
void test_UseUtilFuncVerify();
void test_DoesNothingWhenNotInit();
void test_InitializeThrowsExceptionOnBadInput();
void test_SignReturnsFalseOnBadInput();
void test_VerifyReturnsFalseOnBadInput();
};
@@ -1,498 +0,0 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include "crypto_Rsa1024Pkcs1Sha1Signer_TestClass.h"
#include "RsaPkcs1Util.h"
#include <tc/Exception.h>
#include <tc/crypto/RsaPkcs1Sha1Signer.h>
#include <tc/cli/FormatUtil.h>
#include <tc/io/PaddingSource.h>
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::runAllTests(void)
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] START" << std::endl;
test_Constants();
test_UseClassSign();
test_UseClassVerify();
test_UseUtilFuncSign();
test_UseUtilFuncVerify();
test_DoesNothingWhenNotInit();
test_InitializeThrowsExceptionOnBadInput();
test_SignReturnsFalseOnBadInput();
test_VerifyReturnsFalseOnBadInput();
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] END" << std::endl;
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_Constants()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_Constants : " << std::flush;
try
{
try
{
std::stringstream ss;
// check signature size
static const size_t kExpectedSignatureSize = 1024 >> 3;
if (tc::crypto::Rsa1024Pkcs1Sha1Signer::kSignatureSize != kExpectedSignatureSize)
{
ss << "kSignatureSize had value " << std::dec << tc::crypto::Rsa1024Pkcs1Sha1Signer::kSignatureSize << " (expected " << kExpectedSignatureSize << ")";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_UseClassSign()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_UseClassSign : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024Pkcs1Sha1Signer signer;
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData signature = tc::ByteData(test->signature.size());
// initialize
signer.initialize(tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()));
// clear data
memset(signature.data(), 0xff, signature.size());
// test sign
bool result = signer.sign(signature.data(), test->message_digest.data());
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: sign() returned false";
throw tc::Exception(ss.str());
}
if (memcmp(signature.data(), test->signature.data(), signature.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(signature, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->signature, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_UseClassVerify()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_UseClassVerify : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
tc::crypto::Rsa1024Pkcs1Sha1Signer signer;
for (auto test = tests.begin(); test != tests.end(); test++)
{
// initialize
signer.initialize(tc::crypto::RsaPublicKey(test->key_modulus.data(), test->key_modulus.size()));
// test verify
bool result = signer.verify(test->signature.data(), test->message_digest.data());
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: verify() returned false";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_UseUtilFuncSign()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_UseUtilFuncSign : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
for (auto test = tests.begin(); test != tests.end(); test++)
{
tc::ByteData signature = tc::ByteData(test->signature.size());
// clear data
memset(signature.data(), 0xff, signature.size());
// test sign
bool result = tc::crypto::SignRsa1024Pkcs1Sha1(signature.data(), test->message_digest.data(), tc::crypto::RsaPrivateKey(test->key_modulus.data(), test->key_modulus.size(), test->key_private_exponent.data(), test->key_private_exponent.size()));
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: sign() returned false";
throw tc::Exception(ss.str());
}
if (memcmp(signature.data(), test->signature.data(), signature.size()) != 0)
{
ss << "Test \"" << test->test_name << "\" Failed: " << tc::cli::FormatUtil::formatBytesAsString(signature, true, "") << " (expected " << tc::cli::FormatUtil::formatBytesAsString(test->signature, true, "") << ")";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_UseUtilFuncVerify()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_UseUtilFuncVerify : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
for (auto test = tests.begin(); test != tests.end(); test++)
{
// test verify
bool result = tc::crypto::VerifyRsa1024Pkcs1Sha1(test->signature.data(), test->message_digest.data(), tc::crypto::RsaPublicKey(test->key_modulus.data(), test->key_modulus.size()));
if (result == false)
{
ss << "Test \"" << test->test_name << "\" Failed: verify() returned false";
throw tc::Exception(ss.str());
}
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_DoesNothingWhenNotInit()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_DoesNothingWhenNotInit : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
if (tests.begin() == tests.end())
{
throw tc::Exception("No tests");
}
bool res;
tc::crypto::Rsa1024Pkcs1Sha1Signer signer;
// create data
tc::ByteData hash = tc::ByteData(tests[0].message_digest.size());
tc::ByteData control_signature = tc::io::PaddingSource(0xee, tests[0].signature.size()).pullData(0, 0x20);
tc::ByteData signature = tc::ByteData(control_signature.data(), control_signature.size());
// try to sign without calling initialize()
res = signer.sign(signature.data(), hash.data());
if (res != false)
{
ss << "Failed: sign() returned true when not initialized";
throw tc::Exception(ss.str());
}
if (memcmp(signature.data(), control_signature.data(), signature.size()) != 0)
{
ss << "Failed: sign() operated on message when not initialized";
throw tc::Exception(ss.str());
}
// try to verify without calling initialize()
res = signer.verify(signature.data(), hash.data());
if (res != false)
{
ss << "Failed: verify() returned true when not initialized";
throw tc::Exception(ss.str());
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_InitializeThrowsExceptionOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_InitializeThrowsExceptionOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
tc::crypto::Rsa1024Pkcs1Sha1Signer signer;
// reference initialize call
//signer.initialize(tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()));
auto key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size());
auto empty_key = tc::crypto::RsaKey();
auto bad_modulus_size_key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()-2, tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size());
auto no_modulus_key = key;
no_modulus_key.n = tc::ByteData();
auto bad_privexp_size_key = tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()-2);
auto bad_pubexp_size_key = key;
bad_pubexp_size_key.e = tc::ByteData(5);
auto no_exponent_key = key;
no_exponent_key.d = tc::ByteData();
no_exponent_key.e = tc::ByteData();
try {
signer.initialize(empty_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey empty");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(bad_modulus_size_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad modulus size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(no_modulus_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a no modulus");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(bad_privexp_size_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad private exponent size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(bad_pubexp_size_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a bad public exponent size");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
try {
signer.initialize(no_exponent_key);
throw tc::Exception("Failed to throw ArgumentNullException where RsaKey had a neither public nor private exponents");
} catch(const tc::ArgumentNullException&) {
// all good if this was thrown.
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_SignReturnsFalseOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_SignReturnsFalseOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
tc::crypto::Rsa1024Pkcs1Sha1Signer signer;
signer.initialize(tc::crypto::RsaPrivateKey(tests[0].key_modulus.data(), tests[0].key_modulus.size(), tests[0].key_private_exponent.data(), tests[0].key_private_exponent.size()));
tc::ByteData signature = tc::ByteData(tests[0].signature.size());
// reference sign call
//signer.sign(signature.data(), tests[0].message_digest.data());
bool result = false;
result = signer.sign(nullptr, tests[0].message_digest.data());
if (result != false)
{
throw tc::Exception("sign() did not return false where signature==nullptr"); // ArgumentNullException
}
result = signer.sign(signature.data(), nullptr);
if (result != false)
{
throw tc::Exception("sign() did not return false where message_digest==nullptr"); // ArgumentNullException
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}
void crypto_Rsa1024Pkcs1Sha1Signer_TestClass::test_VerifyReturnsFalseOnBadInput()
{
std::cout << "[tc::crypto::Rsa1024Pkcs1Sha1Signer] test_VerifyReturnsFalseOnBadInput : " << std::flush;
try
{
try
{
std::stringstream ss;
// create tests
std::vector<RsaPkcs1Util::TestVector> tests;
RsaPkcs1Util::generateRsaPkcs1TestVectors_Custom(tests, 1024, RsaPkcs1Util::SHA1);
tc::crypto::Rsa1024Pkcs1Sha1Signer signer;
signer.initialize(tc::crypto::RsaPublicKey(tests[0].key_modulus.data(), tests[0].key_modulus.size()));
// reference verify call
//signer.verify(tests[0].signature.data(), tests[0].message_digest.data());
bool result = false;
result = signer.verify(nullptr, tests[0].message_digest.data());
if (result != false)
{
throw tc::Exception("encrypt() did not return false where signature==nullptr"); // ArgumentNullException
}
result = signer.verify(tests[0].signature.data(), nullptr);
if (result != false)
{
throw tc::Exception("encrypt() did not return false where message_digest==nullptr"); // ArgumentNullException
}
std::cout << "PASS" << std::endl;
}
catch (const tc::Exception& e)
{
std::cout << "FAIL (" << e.error() << ")" << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "UNHANDLED EXCEPTION (" << e.what() << ")" << std::endl;
}
}

Some files were not shown because too many files have changed in this diff Show More