Add Cipher concept
This commit is contained in:
parent
5adcf4aca1
commit
008c6f7ab7
@ -9,6 +9,7 @@
|
|||||||
#include "messmer/cpp-utils/macros.h"
|
#include "messmer/cpp-utils/macros.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <boost/optional.hpp>
|
#include <boost/optional.hpp>
|
||||||
|
#include "ciphers/Cipher.h"
|
||||||
|
|
||||||
namespace blockstore {
|
namespace blockstore {
|
||||||
namespace encrypted {
|
namespace encrypted {
|
||||||
@ -19,6 +20,7 @@ template<class Cipher> class EncryptedBlockStore;
|
|||||||
template<class Cipher>
|
template<class Cipher>
|
||||||
class EncryptedBlock: public Block {
|
class EncryptedBlock: public Block {
|
||||||
public:
|
public:
|
||||||
|
BOOST_CONCEPT_ASSERT((CipherConcept<Cipher>));
|
||||||
static std::unique_ptr<EncryptedBlock> TryCreateNew(BlockStore *baseBlockStore, const Key &key, Data data, const typename Cipher::EncryptionKey &encKey);
|
static std::unique_ptr<EncryptedBlock> TryCreateNew(BlockStore *baseBlockStore, const Key &key, Data data, const typename Cipher::EncryptionKey &encKey);
|
||||||
static std::unique_ptr<EncryptedBlock> TryDecrypt(std::unique_ptr<Block> baseBlock, const typename Cipher::EncryptionKey &key);
|
static std::unique_ptr<EncryptedBlock> TryDecrypt(std::unique_ptr<Block> baseBlock, const typename Cipher::EncryptionKey &key);
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "../../../utils/Data.h"
|
#include "../../../utils/Data.h"
|
||||||
#include <cryptopp/cryptopp/aes.h>
|
#include <cryptopp/cryptopp/aes.h>
|
||||||
#include <boost/optional.hpp>
|
#include <boost/optional.hpp>
|
||||||
|
#include "Cipher.h"
|
||||||
|
|
||||||
namespace blockstore {
|
namespace blockstore {
|
||||||
namespace encrypted {
|
namespace encrypted {
|
||||||
@ -13,11 +14,11 @@ namespace encrypted {
|
|||||||
//TODO Add contract/interface for ciphers
|
//TODO Add contract/interface for ciphers
|
||||||
class AES256_CFB {
|
class AES256_CFB {
|
||||||
public:
|
public:
|
||||||
|
BOOST_CONCEPT_ASSERT((CipherConcept<AES256_CFB>));
|
||||||
|
|
||||||
using EncryptionKey = FixedSizeData<32>;
|
using EncryptionKey = FixedSizeData<32>;
|
||||||
static_assert(32 == CryptoPP::AES::MAX_KEYLENGTH, "If AES offered larger keys, we should offer a variant with it");
|
static_assert(32 == CryptoPP::AES::MAX_KEYLENGTH, "If AES offered larger keys, we should offer a variant with it");
|
||||||
|
|
||||||
AES256_CFB(const EncryptionKey &key);
|
|
||||||
|
|
||||||
static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) {
|
static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) {
|
||||||
return plaintextBlockSize + IV_SIZE;
|
return plaintextBlockSize + IV_SIZE;
|
||||||
}
|
}
|
||||||
|
32
implementations/encrypted/ciphers/Cipher.h
Normal file
32
implementations/encrypted/ciphers/Cipher.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_CIPHERS_CIPHER_H_
|
||||||
|
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_CIPHERS_CIPHER_H_
|
||||||
|
|
||||||
|
#include <boost/concept_check.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace blockstore {
|
||||||
|
namespace encrypted {
|
||||||
|
|
||||||
|
template<class X>
|
||||||
|
struct CipherConcept {
|
||||||
|
public:
|
||||||
|
BOOST_CONCEPT_USAGE(CipherConcept) {
|
||||||
|
same_type(UINT32_C(0), X::ciphertextSize(UINT32_C(5)));
|
||||||
|
same_type(UINT32_C(0), X::plaintextSize(UINT32_C(5)));
|
||||||
|
typename X::EncryptionKey key = X::EncryptionKey::CreateRandom();
|
||||||
|
same_type(Data(0), X::encrypt((byte*)nullptr, UINT32_C(0), key));
|
||||||
|
same_type(boost::optional<Data>(Data(0)), X::decrypt((byte*)nullptr, UINT32_C(0), key));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Type deduction will fail unless the arguments have the same type.
|
||||||
|
template <typename T> void same_type(T const&, T const&);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -1,5 +1,6 @@
|
|||||||
#include <google/gtest/gtest.h>
|
#include <google/gtest/gtest.h>
|
||||||
#include "../../../implementations/encrypted/ciphers/AES256_CFB.h"
|
#include "../../../implementations/encrypted/ciphers/AES256_CFB.h"
|
||||||
|
#include "../../../implementations/encrypted/ciphers/Cipher.h"
|
||||||
|
|
||||||
#include "../../testutils/DataBlockFixture.h"
|
#include "../../testutils/DataBlockFixture.h"
|
||||||
#include "../../../utils/Data.h"
|
#include "../../../utils/Data.h"
|
||||||
@ -10,6 +11,7 @@ using blockstore::Data;
|
|||||||
template<class Cipher>
|
template<class Cipher>
|
||||||
class CipherTest: public ::testing::Test {
|
class CipherTest: public ::testing::Test {
|
||||||
public:
|
public:
|
||||||
|
BOOST_CONCEPT_ASSERT((CipherConcept<Cipher>));
|
||||||
typename Cipher::EncryptionKey encKey = createRandomKey();
|
typename Cipher::EncryptionKey encKey = createRandomKey();
|
||||||
|
|
||||||
static typename Cipher::EncryptionKey createRandomKey(int seed = 0) {
|
static typename Cipher::EncryptionKey createRandomKey(int seed = 0) {
|
||||||
@ -82,7 +84,7 @@ TYPED_TEST_P(CipherTest, Size_1048576) {
|
|||||||
EXPECT_EQ(1048576, TypeParam::plaintextSize(TypeParam::ciphertextSize(1048576)));
|
EXPECT_EQ(1048576, TypeParam::plaintextSize(TypeParam::ciphertextSize(1048576)));
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr std::initializer_list<unsigned int> SIZES = {0, 1, 100, 1024, 5000, 1048576, 52428800};
|
constexpr std::initializer_list<unsigned int> SIZES = {0, 1, 100, 1024, 5000, 1048576, 20971520};
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes) {
|
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes) {
|
||||||
for (auto size: SIZES) {
|
for (auto size: SIZES) {
|
||||||
|
Loading…
Reference in New Issue
Block a user