2015-07-31 14:01:49 +03:00
|
|
|
#define BOOST_TEST_DYN_LINK
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include "crypto/aes.h"
|
2015-08-30 00:21:32 +03:00
|
|
|
#include "crypto/EdDSA25519.h"
|
2015-07-31 14:01:49 +03:00
|
|
|
|
|
|
|
using namespace i2p::crypto;
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(CryptographyTests)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(XorZeroCipherBlocks)
|
|
|
|
{
|
|
|
|
CipherBlock block = {};
|
|
|
|
block ^= block;
|
|
|
|
const CipherBlock result = {};
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.buf, result.buf + 16, block.buf, block.buf + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(XorSelfCipherBlocks)
|
|
|
|
{
|
|
|
|
CipherBlock block = {
|
|
|
|
0xc9, 0x4c, 0xaf, 0x5, 0x9c, 0x1c, 0x10, 0x1e, 0x20, 0xb3, 0x7e,
|
|
|
|
0xcf, 0xf5, 0xbf, 0xf0, 0xd6
|
|
|
|
};
|
|
|
|
block ^= block;
|
|
|
|
const CipherBlock result = {};
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.buf, result.buf + 16, block.buf, block.buf + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(XorCipherBlocks)
|
|
|
|
{
|
|
|
|
const CipherBlock block1 = {
|
|
|
|
0xc9, 0x4c, 0xaf, 0x5, 0x9c, 0x1c, 0x10, 0x1e, 0x20, 0xb3, 0x7e,
|
|
|
|
0xcf, 0xf5, 0xbf, 0xf0, 0xd6
|
|
|
|
};
|
|
|
|
CipherBlock block2 = {
|
|
|
|
0x2e, 0xfb, 0x26, 0xa9, 0x90, 0x3b, 0xf7, 0xc8, 0x5c, 0xfe, 0x20,
|
|
|
|
0x23, 0x1d, 0xaf, 0x67, 0xac
|
|
|
|
};
|
|
|
|
block2 ^= block1;
|
|
|
|
const CipherBlock result = {
|
|
|
|
0xe7, 0xb7, 0x89, 0xac, 0xc, 0x27, 0xe7, 0xd6, 0x7c, 0x4d, 0x5e,
|
|
|
|
0xec, 0xe8, 0x10, 0x97, 0x7a
|
|
|
|
};
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(block2.buf, block2.buf + 16, result.buf, result.buf + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NIST test parameters
|
|
|
|
// see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
|
|
|
struct AesCbcFixture {
|
|
|
|
|
|
|
|
AesCbcFixture()
|
|
|
|
: cbc_encrypt(AESKey(key), iv), cbc_decrypt(AESKey(key), iv) {}
|
|
|
|
|
|
|
|
uint8_t key[32] = {
|
|
|
|
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73,
|
|
|
|
0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07,
|
|
|
|
0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14,
|
|
|
|
0xdf, 0xf4
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t iv[16] = {
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
|
|
|
|
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
|
|
|
};
|
|
|
|
|
|
|
|
CBCEncryption cbc_encrypt;
|
|
|
|
CBCDecryption cbc_decrypt;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AesCbcSingleBlockEncrypt, AesCbcFixture)
|
|
|
|
{
|
|
|
|
uint8_t output[16] = {};
|
|
|
|
|
|
|
|
const uint8_t input[] = {
|
|
|
|
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d,
|
|
|
|
0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
|
|
|
|
};
|
|
|
|
const uint8_t result[] = {
|
|
|
|
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e,
|
|
|
|
0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6
|
|
|
|
};
|
|
|
|
cbc_encrypt.Encrypt(input, output);
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(output, output + 16, result, result + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AesCbcSingleBlockDecrypt, AesCbcFixture)
|
|
|
|
{
|
|
|
|
uint8_t output[16] = {};
|
|
|
|
|
|
|
|
const uint8_t input[] = {
|
|
|
|
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e,
|
|
|
|
0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6
|
|
|
|
};
|
|
|
|
const uint8_t result[] = {
|
|
|
|
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d,
|
|
|
|
0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
|
|
|
|
};
|
|
|
|
cbc_decrypt.Decrypt(input, output);
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(output, output + 16, result, result + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AesCbcEncrypt, AesCbcFixture)
|
|
|
|
{
|
|
|
|
CipherBlock output[4] = {};
|
|
|
|
|
|
|
|
CipherBlock input[4] = {};
|
|
|
|
input[0] = {
|
|
|
|
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d,
|
|
|
|
0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
|
|
|
|
};
|
|
|
|
input[1] = {
|
|
|
|
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7,
|
|
|
|
0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
|
|
|
|
};
|
|
|
|
input[2] = {
|
|
|
|
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb,
|
|
|
|
0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef
|
|
|
|
};
|
|
|
|
input[3] = {
|
|
|
|
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b,
|
|
|
|
0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
|
|
|
|
};
|
|
|
|
|
|
|
|
CipherBlock result[4] = {};
|
|
|
|
result[0] = {
|
|
|
|
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e,
|
|
|
|
0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6
|
|
|
|
};
|
|
|
|
result[1] = {
|
|
|
|
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f,
|
|
|
|
0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d
|
|
|
|
};
|
|
|
|
result[2] = {
|
|
|
|
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30,
|
|
|
|
0xe2, 0x63, 0x04, 0x23, 0x14, 0x61
|
|
|
|
};
|
|
|
|
result[3] = {
|
|
|
|
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c,
|
|
|
|
0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b
|
|
|
|
};
|
|
|
|
cbc_encrypt.Encrypt(4, input, output);
|
|
|
|
|
|
|
|
for(int i = 0; i < 3; ++i) {
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(
|
|
|
|
output[i].buf, output[i].buf + 16,
|
|
|
|
result[i].buf, result[i].buf + 16
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(AesCbcDecrypt, AesCbcFixture)
|
|
|
|
{
|
|
|
|
CipherBlock output[4] = {};
|
|
|
|
|
|
|
|
CipherBlock input[4] = {};
|
|
|
|
input[0] = {
|
|
|
|
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e,
|
|
|
|
0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6
|
|
|
|
};
|
|
|
|
input[1] = {
|
|
|
|
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f,
|
|
|
|
0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d
|
|
|
|
};
|
|
|
|
input[2] = {
|
|
|
|
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30,
|
|
|
|
0xe2, 0x63, 0x04, 0x23, 0x14, 0x61
|
|
|
|
};
|
|
|
|
input[3] = {
|
|
|
|
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c,
|
|
|
|
0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b
|
|
|
|
};
|
|
|
|
|
|
|
|
CipherBlock result[4] = {};
|
|
|
|
result[0] = {
|
|
|
|
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d,
|
|
|
|
0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
|
|
|
|
};
|
|
|
|
result[1] = {
|
|
|
|
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7,
|
|
|
|
0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
|
|
|
|
};
|
|
|
|
result[2] = {
|
|
|
|
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb,
|
|
|
|
0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef
|
|
|
|
};
|
|
|
|
result[3] = {
|
|
|
|
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b,
|
|
|
|
0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
|
|
|
|
};
|
|
|
|
cbc_decrypt.Decrypt(4, input, output);
|
|
|
|
|
|
|
|
for(int i = 0; i < 3; ++i) {
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(
|
|
|
|
output[i].buf, output[i].buf + 16,
|
|
|
|
result[i].buf, result[i].buf + 16
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 00:21:32 +03:00
|
|
|
struct EDDSAFixture {
|
|
|
|
|
|
|
|
EDDSAFixture()
|
|
|
|
: verifier(public_key), signer(private_key), dummy_rng() {}
|
|
|
|
|
|
|
|
uint8_t private_key[32] = {
|
|
|
|
0xe1, 0xec, 0xff, 0xa6, 0xcd, 0x4e, 0xc7, 0x09, 0x2f, 0x87,
|
|
|
|
0x44, 0xaf, 0x48, 0xb3, 0x7f, 0x63, 0x71, 0x63, 0x1e, 0x01,
|
|
|
|
0xf7, 0x20, 0xe9, 0x0a, 0xfa, 0x3c, 0x90, 0xec, 0x97, 0x4c,
|
|
|
|
0x16, 0x27
|
|
|
|
};
|
|
|
|
uint8_t public_key[32] = {
|
|
|
|
0x0f, 0x90, 0x8b, 0xaf, 0xef, 0x40, 0x79, 0xb5, 0x94, 0xb5,
|
|
|
|
0x13, 0xf9, 0xf6, 0x02, 0x65, 0xef, 0x4d, 0x95, 0xa4, 0x84,
|
|
|
|
0x2d, 0xc7, 0x23, 0x1b, 0x93, 0xe4, 0x2e, 0x9d, 0x45, 0x52,
|
|
|
|
0xed, 0x62
|
|
|
|
};
|
|
|
|
|
|
|
|
EDDSA25519Verifier verifier;
|
|
|
|
EDDSA25519Signer signer;
|
|
|
|
CryptoPP::RandomNumberGenerator dummy_rng;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(EdDSA25519KeyLength, EDDSAFixture)
|
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL(verifier.GetPublicKeyLen(), EDDSA25519_PUBLIC_KEY_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(EdDSA25519SignatureLength, EDDSAFixture)
|
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL(verifier.GetSignatureLen(), EDDSA25519_SIGNATURE_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(EdDSA25519Sign, EDDSAFixture)
|
|
|
|
{
|
|
|
|
const uint8_t message[33] = {
|
|
|
|
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
|
|
|
|
0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61,
|
|
|
|
0x67, 0x65, 0x21, 0x20, 0x2d, 0x45, 0x69, 0x6e, 0x4d, 0x42,
|
|
|
|
0x79, 0x74, 0x65
|
|
|
|
};
|
|
|
|
const uint8_t signature[64] = {
|
|
|
|
0xfa, 0x69, 0x31, 0x22, 0x61, 0xb5, 0x4f, 0xf5, 0x7e, 0x20,
|
|
|
|
0xa6, 0x05, 0x91, 0xe7, 0xab, 0x41, 0x43, 0x48, 0x85, 0x02,
|
|
|
|
0xac, 0xcf, 0x3f, 0x17, 0x13, 0x09, 0x3e, 0x25, 0x3c, 0x15,
|
|
|
|
0x6d, 0xc3, 0x55, 0xcd, 0x8a, 0x30, 0x07, 0xe2, 0x41, 0xa1,
|
|
|
|
0x98, 0x24, 0xe5, 0xc0, 0x9e, 0x90, 0xbb, 0x9e, 0x6b, 0xe8,
|
|
|
|
0x41, 0x84, 0x2d, 0x13, 0x2e, 0x1f, 0x2a, 0x46, 0x31, 0x2e,
|
|
|
|
0x5c, 0x94, 0x7e, 0x0d
|
|
|
|
};
|
|
|
|
uint8_t output[64] = {};
|
|
|
|
signer.Sign(dummy_rng, message, 33, output);
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(output, output + 64, signature, signature + 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(EdDSA25519Verify, EDDSAFixture)
|
|
|
|
{
|
|
|
|
const uint8_t message[33] = {
|
|
|
|
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
|
|
|
|
0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61,
|
|
|
|
0x67, 0x65, 0x21, 0x20, 0x2d, 0x45, 0x69, 0x6e, 0x4d, 0x42,
|
|
|
|
0x79, 0x74, 0x65
|
|
|
|
};
|
|
|
|
const uint8_t signature[64] = {
|
|
|
|
0xfa, 0x69, 0x31, 0x22, 0x61, 0xb5, 0x4f, 0xf5, 0x7e, 0x20,
|
|
|
|
0xa6, 0x05, 0x91, 0xe7, 0xab, 0x41, 0x43, 0x48, 0x85, 0x02,
|
|
|
|
0xac, 0xcf, 0x3f, 0x17, 0x13, 0x09, 0x3e, 0x25, 0x3c, 0x15,
|
|
|
|
0x6d, 0xc3, 0x55, 0xcd, 0x8a, 0x30, 0x07, 0xe2, 0x41, 0xa1,
|
|
|
|
0x98, 0x24, 0xe5, 0xc0, 0x9e, 0x90, 0xbb, 0x9e, 0x6b, 0xe8,
|
|
|
|
0x41, 0x84, 0x2d, 0x13, 0x2e, 0x1f, 0x2a, 0x46, 0x31, 0x2e,
|
|
|
|
0x5c, 0x94, 0x7e, 0x0d
|
|
|
|
};
|
|
|
|
BOOST_CHECK(verifier.Verify(message, 33, signature));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(EdDSA25519VerifyBadMsg, EDDSAFixture)
|
|
|
|
{
|
|
|
|
const uint8_t message[10] = {};
|
|
|
|
const uint8_t signature[64] = {
|
|
|
|
0xfa, 0x69, 0x31, 0x22, 0x61, 0xb5, 0x4f, 0xf5, 0x7e, 0x20,
|
|
|
|
0xa6, 0x05, 0x91, 0xe7, 0xab, 0x41, 0x43, 0x48, 0x85, 0x02,
|
|
|
|
0xac, 0xcf, 0x3f, 0x17, 0x13, 0x09, 0x3e, 0x25, 0x3c, 0x15,
|
|
|
|
0x6d, 0xc3, 0x55, 0xcd, 0x8a, 0x30, 0x07, 0xe2, 0x41, 0xa1,
|
|
|
|
0x98, 0x24, 0xe5, 0xc0, 0x9e, 0x90, 0xbb, 0x9e, 0x6b, 0xe8,
|
|
|
|
0x41, 0x84, 0x2d, 0x13, 0x2e, 0x1f, 0x2a, 0x46, 0x31, 0x2e,
|
|
|
|
0x5c, 0x94, 0x7e, 0x0d
|
|
|
|
};
|
|
|
|
BOOST_CHECK(!verifier.Verify(message, 10, signature));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(EdDSA25519VerifyBadSignature, EDDSAFixture)
|
|
|
|
{
|
|
|
|
const uint8_t message[33] = {
|
|
|
|
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
|
|
|
|
0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61,
|
|
|
|
0x67, 0x65, 0x21, 0x20, 0x2d, 0x45, 0x69, 0x6e, 0x4d, 0x42,
|
|
|
|
0x79, 0x74, 0x65
|
|
|
|
};
|
|
|
|
const uint8_t signature[64] = {};
|
|
|
|
BOOST_CHECK(!verifier.Verify(message, 33, signature));
|
|
|
|
}
|
2015-07-31 14:01:49 +03:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|