2015-10-27 23:19:20 +01:00
|
|
|
#pragma once
|
|
|
|
#ifndef MESSMER_CPPUTILS_TEST_CRYPTO_SYMMETRIC_TESTUTILS_FAKEAUTHENTICATEDCIPHER_H_
|
|
|
|
#define MESSMER_CPPUTILS_TEST_CRYPTO_SYMMETRIC_TESTUTILS_FAKEAUTHENTICATEDCIPHER_H_
|
|
|
|
|
2017-08-16 04:00:46 +02:00
|
|
|
#include "cpp-utils/crypto/cryptopp_byte.h"
|
2016-02-11 12:53:42 +01:00
|
|
|
#include "cpp-utils/crypto/symmetric/Cipher.h"
|
|
|
|
#include "cpp-utils/data/FixedSizeData.h"
|
|
|
|
#include "cpp-utils/data/Data.h"
|
|
|
|
#include "cpp-utils/random/RandomGenerator.h"
|
2015-10-27 23:19:20 +01:00
|
|
|
|
|
|
|
namespace cpputils {
|
|
|
|
|
|
|
|
struct FakeKey {
|
|
|
|
static FakeKey FromBinary(const void *data) {
|
|
|
|
return FakeKey{*(uint8_t *) data};
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr unsigned int BINARY_LENGTH = 1;
|
|
|
|
|
2017-09-05 01:43:43 +02:00
|
|
|
static FakeKey CreateKey(RandomGenerator &randomGenerator) {
|
|
|
|
auto data = randomGenerator.getFixedSize<1>();
|
|
|
|
return FakeKey{*((uint8_t *) data.data())};
|
|
|
|
}
|
|
|
|
|
2015-10-27 23:19:20 +01:00
|
|
|
uint8_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is a fake cipher that uses an indeterministic caesar chiffre and a 4-byte parity for a simple authentication mechanism
|
|
|
|
class FakeAuthenticatedCipher {
|
|
|
|
public:
|
|
|
|
BOOST_CONCEPT_ASSERT((CipherConcept<FakeAuthenticatedCipher>));
|
|
|
|
|
|
|
|
using EncryptionKey = FakeKey;
|
|
|
|
|
|
|
|
static EncryptionKey Key1() {
|
|
|
|
return FakeKey{5};
|
|
|
|
}
|
|
|
|
|
|
|
|
static EncryptionKey Key2() {
|
|
|
|
return FakeKey{63};
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) {
|
|
|
|
return plaintextBlockSize + 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr unsigned int plaintextSize(unsigned int ciphertextBlockSize) {
|
|
|
|
return ciphertextBlockSize - 5;
|
|
|
|
}
|
|
|
|
|
2017-08-16 04:00:46 +02:00
|
|
|
static Data encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) {
|
2015-10-27 23:19:20 +01:00
|
|
|
Data result(ciphertextSize(plaintextSize));
|
|
|
|
|
|
|
|
//Add a random IV
|
|
|
|
uint8_t iv = rand();
|
|
|
|
std::memcpy(result.data(), &iv, 1);
|
|
|
|
|
|
|
|
//Use caesar chiffre on plaintext
|
2017-08-16 04:00:46 +02:00
|
|
|
_caesar((CryptoPP::byte *) result.data() + 1, plaintext, plaintextSize, encKey.value + iv);
|
2015-10-27 23:19:20 +01:00
|
|
|
|
|
|
|
//Add parity information
|
2017-08-16 04:00:46 +02:00
|
|
|
int32_t parity = _parity((CryptoPP::byte *) result.data(), plaintextSize + 1);
|
|
|
|
std::memcpy((CryptoPP::byte *) result.data() + plaintextSize + 1, &parity, 4);
|
2015-10-27 23:19:20 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-16 04:00:46 +02:00
|
|
|
static boost::optional <Data> decrypt(const CryptoPP::byte *ciphertext, unsigned int ciphertextSize,
|
2015-10-27 23:19:20 +01:00
|
|
|
const EncryptionKey &encKey) {
|
|
|
|
//We need at least 5 bytes (iv + parity)
|
|
|
|
if (ciphertextSize < 5) {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check parity
|
|
|
|
int32_t expectedParity = _parity(ciphertext, plaintextSize(ciphertextSize) + 1);
|
|
|
|
int32_t actualParity = *(int32_t * )(ciphertext + plaintextSize(ciphertextSize) + 1);
|
|
|
|
if (expectedParity != actualParity) {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Decrypt caesar chiffre from ciphertext
|
|
|
|
int32_t iv = *(int32_t *) ciphertext;
|
|
|
|
Data result(plaintextSize(ciphertextSize));
|
2017-08-16 04:00:46 +02:00
|
|
|
_caesar((CryptoPP::byte *) result.data(), ciphertext + 1, plaintextSize(ciphertextSize), -(encKey.value + iv));
|
2015-10-27 23:19:20 +01:00
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2015-10-28 01:49:12 +01:00
|
|
|
static constexpr const char *NAME = "FakeAuthenticatedCipher";
|
|
|
|
|
2015-10-27 23:19:20 +01:00
|
|
|
private:
|
2017-08-16 04:00:46 +02:00
|
|
|
static int32_t _parity(const CryptoPP::byte *data, unsigned int size) {
|
2015-10-27 23:19:20 +01:00
|
|
|
int32_t parity = 34343435; // some init value
|
2016-02-15 05:00:30 +01:00
|
|
|
const int32_t *intData = reinterpret_cast<const int32_t *>(data);
|
2015-10-27 23:19:20 +01:00
|
|
|
unsigned int intSize = size / sizeof(int32_t);
|
|
|
|
for (unsigned int i = 0; i < intSize; ++i) {
|
2016-02-15 05:00:30 +01:00
|
|
|
parity = ((int64_t)parity) + intData[i];
|
2015-10-27 23:19:20 +01:00
|
|
|
}
|
|
|
|
unsigned int remainingBytes = size - 4 * intSize;
|
|
|
|
for (unsigned int i = 0; i < remainingBytes; ++i) {
|
2016-02-15 05:00:30 +01:00
|
|
|
parity = ((int64_t)parity) + (data[4 * intSize + i] << (24 - 8 * i));
|
2015-10-27 23:19:20 +01:00
|
|
|
}
|
|
|
|
return parity;
|
|
|
|
}
|
|
|
|
|
2017-08-16 04:00:46 +02:00
|
|
|
static void _caesar(CryptoPP::byte *dst, const CryptoPP::byte *src, unsigned int size, uint8_t key) {
|
2015-10-27 23:19:20 +01:00
|
|
|
for (unsigned int i = 0; i < size; ++i) {
|
|
|
|
dst[i] = src[i] + key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|