Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
add elg tests and fix up dsa tests
Browse files Browse the repository at this point in the history
  • Loading branch information
majestrate committed Feb 4, 2016
1 parent 748ec5b commit d4ffd85
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/tests/unit_tests/CMakeLists.txt
Expand Up @@ -4,7 +4,8 @@ set(TESTS_SRC
"Utility.cpp"
"crypto/aes.cpp"
"crypto/dsa.cpp"
"crypto/ed25519.cpp")
"crypto/ed25519.cpp"
"crypto/elg.cpp")

include_directories(
"../../"
Expand Down
13 changes: 5 additions & 8 deletions src/tests/unit_tests/crypto/dsa.cpp
Expand Up @@ -57,6 +57,7 @@ struct DSAFixture {
uint8_t publicKey[128];
DSAVerifier* verifier;
DSASigner* signer;
static constexpr size_t messageLen = 1024;
};

BOOST_FIXTURE_TEST_CASE(DSASHA1KeyLength, DSAFixture) {
Expand All @@ -69,7 +70,6 @@ BOOST_FIXTURE_TEST_CASE(DSASHA1SignatureLength, DSAFixture) {
}

BOOST_FIXTURE_TEST_CASE(DSASHA1SignVerifyValid, DSAFixture) {
constexpr size_t messageLen = 1024;
uint8_t signature[40];
uint8_t message[messageLen];
RandBytes(message, messageLen);
Expand All @@ -78,42 +78,39 @@ BOOST_FIXTURE_TEST_CASE(DSASHA1SignVerifyValid, DSAFixture) {
BOOST_CHECK_EQUAL(verifier->Verify(message, messageLen, signature), true);
}
BOOST_FIXTURE_TEST_CASE(DSASHA1SignVerifyBadSignature, DSAFixture) {
constexpr size_t messageLen = 1024;
uint8_t signature[40];
uint8_t message[messageLen];
RandBytes(message, messageLen);
signer->Sign(message, messageLen, signature);

// now we fugg up the signature a bit :-DDDD
signature[5] = Rand<uint8_t>();
signature[5] ^= RandInRange<uint8_t>(1, 128);
// it should fail verification
BOOST_CHECK_EQUAL(verifier->Verify(message, messageLen, signature), false);

}

BOOST_FIXTURE_TEST_CASE(DSASHA1SignVerifyBadMessage, DSAFixture) {
constexpr size_t messageLen = 1024;
uint8_t signature[40];
uint8_t message[messageLen];
RandBytes(message, messageLen);
signer->Sign(message, messageLen, signature);
// fugg up the message
message[5] = Rand<uint8_t>();
message[5] ^= RandInRange<uint8_t>(1, 128);
// this should also fail verification
BOOST_CHECK_EQUAL(verifier->Verify(message, messageLen, signature), false);
}

BOOST_FIXTURE_TEST_CASE(DSASHA1SignVerifyBadSignatureAndMessage, DSAFixture) {
constexpr size_t messageLen = 1024;
uint8_t signature[40];
uint8_t message[messageLen];
RandBytes(message, messageLen);

signer->Sign(message, messageLen, signature);

// now we fug up both message and signature
message[6] = Rand<uint8_t>();
signature[2] = Rand<uint8_t>();
message[6] ^= RandInRange<uint8_t>(1, 128);
signature[2] ^= RandInRange<uint8_t>(1, 128);
// this should fail verification as well
BOOST_CHECK_EQUAL(verifier->Verify(message, messageLen, signature), false);
}
Expand Down
87 changes: 87 additions & 0 deletions src/tests/unit_tests/crypto/elg.cpp
@@ -0,0 +1,87 @@

#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include "crypto/ElGamal.h"
#include "crypto/Rand.h"

using namespace i2p::crypto;

BOOST_AUTO_TEST_SUITE(ElgamalTests)

struct ElgamalFixture {

uint8_t privateKey[256];
uint8_t publicKey[256];
ElGamalEncryption* enc;
static constexpr size_t messageLen = 222;

ElgamalFixture() {
// TODO(psi): use static keys
GenerateElGamalKeyPair(privateKey, publicKey);
enc = new ElGamalEncryption(publicKey);
}

~ElgamalFixture() {
delete enc;
}


};


BOOST_FIXTURE_TEST_CASE(ElgamalEncryptDecryptSuccess, ElgamalFixture) {
uint8_t plaintext[messageLen];
uint8_t ciphertext[512];
uint8_t result[messageLen];
RandBytes(plaintext, messageLen);
enc->Encrypt(plaintext, messageLen, ciphertext, false);
BOOST_CHECK(ElGamalDecrypt(privateKey, ciphertext, result, false));

BOOST_CHECK_EQUAL_COLLECTIONS(
plaintext, plaintext + messageLen,
result, result + messageLen);
}

BOOST_FIXTURE_TEST_CASE(ElgamalEncryptDecryptFail, ElgamalFixture) {
uint8_t plaintext[messageLen];
uint8_t ciphertext[512];
uint8_t result[messageLen];
RandBytes(plaintext, messageLen);
enc->Encrypt(plaintext, messageLen, ciphertext, false);
// fug up the ciphertext
ciphertext[4] ^= RandInRange<uint8_t>(1, 128);

BOOST_CHECK(!ElGamalDecrypt(privateKey, ciphertext, result, false));
}

BOOST_FIXTURE_TEST_CASE(ElgamalEncryptDecryptZeroPaddBadPad, ElgamalFixture) {
uint8_t plaintext[messageLen];
uint8_t ciphertext[514];
uint8_t result[messageLen];
RandBytes(plaintext, messageLen);
enc->Encrypt(plaintext, messageLen, ciphertext, true);
// fug up the ciphertext zeropadding
ciphertext[0] = RandInRange<uint8_t>(1, 128);
BOOST_CHECK(!ElGamalDecrypt(privateKey, ciphertext, result, true));
}


BOOST_FIXTURE_TEST_CASE(ElgamalEncryptDecryptZeroPadSuccess, ElgamalFixture) {
uint8_t plaintext[messageLen];
uint8_t ciphertext[514];
uint8_t result[messageLen];
RandBytes(plaintext, messageLen);
enc->Encrypt(plaintext, messageLen, ciphertext, true);

bool res = ElGamalDecrypt(privateKey, ciphertext, result, true);

BOOST_CHECK(res);
if (res) {
BOOST_CHECK_EQUAL_COLLECTIONS(
plaintext, plaintext + messageLen,
result, result + messageLen);
}
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit d4ffd85

Please sign in to comment.