crypto module: make it testable

master
mogria 1 month ago
parent 2f3a065e42
commit 7ab0a0bfb4

@ -15,7 +15,6 @@ namespace libpox {
using buffer = ::std::string;
buffer symmetric_encrypt(const buffer &buffer_to_encrypt, const CombinedKey &key, const Nonce &nonce) {
// StringSource s1(buffer_to_encrypt.as_bytes(), buffer_to_encrypt.size());
// buffer encrypted;
@ -26,8 +25,16 @@ namespace libpox {
}
buffer symmetric_decrypt(const buffer &buffer_to_decrypt, const CombinedKey &key, const Nonce &nonce) {
// CryptoPP Salsa decryption example: https://www.cryptopp.com/wiki/Salsa20
// Salsa20::Decryption dec;
// dec.SetKeyWithIV(key, key.size(), iv, iv.size());
//
// // Perform the decryption
// recover.resize(cipher.size());
// dec.ProcessData((byte*)&recover[0], (const byte*)cipher.data(), cipher.size());
}
// void encrypt(uint8_t *bytes, size_t num_bytes, CombinedKey &key, Nonce &nonce) {
//
// crypto_box_afternm(ciphertext, message, num_bytes, key.get_bytes(), nonce.get_bytes())

@ -51,13 +51,11 @@ namespace libpox {
::CryptoPP::OS_GenerateRandomBlock(false, this->nonce_data.data(), NONCE_SIZE_BYTES);
}
Nonce::Nonce(const Nonce &copy_from)
Nonce::Nonce(const NonceData &copy_from)
: nonce_data(copy_from)
{
this->nonce_data = copy_from.nonce_data;
}
Nonce::~Nonce() {}
void Nonce::increment()
{
// big-endian increment
@ -102,6 +100,13 @@ namespace libpox {
private_key.get_data().data());
}
PublicKey::PublicKey(const KeyData &_key_data)
: key_data(_key_data)
{
}
const KeyData &PublicKey::get_data() const
{
return this->key_data;

@ -47,7 +47,7 @@ namespace libpox {
public:
Nonce();
Nonce(const Nonce &copy_from);
Nonce(const NonceData &copy_from);
~Nonce();
void increment();
@ -73,6 +73,7 @@ namespace libpox {
public:
PublicKey(const PrivateKey &private_key);
PublicKey(const KeyData &key_data);
const KeyData &get_data() const;
KeyDistance distance(const PublicKey &other_key) const;

@ -13,13 +13,13 @@ int main()
std::cout << "Test Nonce" << std::endl;
{
std::cout << "* Generates Random Nonce" << std::endl;
Nonce nonce;
Nonce nonce{};
std::cout << " - generated nonce: " << nonce.to_hex_string() << std::endl;
std::cout << "* Copy Nonce" << std::endl;
Nonce nonce_copy(nonce);
Nonce nonce_copy{nonce};
std::cout << "* Increment Nonce" << std::endl;
nonce.increment();
@ -31,8 +31,85 @@ int main()
}
{
std::cout << "Test specialization for int" << std::endl;
// assert(ret == __cplusplus);
std::cout << "* Create self-initialized Nonce" << std::endl;
NonceData nd0{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
NonceData ndFF{
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFE,
};
Nonce nonce0{nd00};
Nonce nonce{ndFF};
Nonce nonce_copy{ndFF};
std::cout << "* Copied nonce data are equal"
assert(nonce.nonce_data == nonce_copy.nonce_data);
std::cout << "* Different nonce data are not equal"
assert(nonce0.nonce_data != nonce.nonce_data);
assert(nonce0.nonce_data != nonce_copy.nonce_data);
nonce.increment();
std::cout << "* Self-initialized nonce increases idependently"
assert(nonce.nonce_data[31] == 0xFF);
assert(nonce.nonce_data != nonce_copy.nonce_data);
nonce.increment();
std::cout << "* Nonce rolls over properly on overflow"
assert(nonce.nonce_data == nonce0.nonce_data);
assert(nonce.nonce_data[30] == 0x00);
assert(nonce.nonce_data[15] == 0x00);
assert(nonce.nonce_data[31] == 0x00);
}
std::cout << "Test PublicKey"
{
std::cout << "* Self-Initialize PublicKey" << std::endl;
KeyData kd1 = {
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
0x00. 0x00. 0x00. 0x01,
};
PublicKey pubkey{kd1};
std::cout << " - test public key: " << pubkey.to_hex_string() << std::endl;
std::cout << "* Copy public key" << std::endl;
PublicKey pubkey_copy{pubkey};
std::cout << "* Copied public key data are equal" << std::endl;
assert(public_key.get_data() == public_key.get_data());
}
std::cout << "Test KeyPair"
{
std::cout << "Test generate_keypair" << std::endl;
KeyPair kp = generate_keypair();
PublicKey pubkey = kp.first();
PrivateKey privkey = kp.second();
// keys are not equal
assert(pubkey.get_data() != privkey.get_data());
}
std::cout << "All tests passed" << std::endl;

Loading…
Cancel
Save