#include "CryCipher.h" #include #include using std::vector; using std::string; using cpputils::unique_ref; using cpputils::make_unique_ref; using blockstore::BlockStore; using std::shared_ptr; using std::make_shared; using boost::optional; using boost::none; using blockstore::encrypted::EncryptedBlockStore; using namespace cryfs; using namespace cpputils; template class CryCipherInstance: public CryCipher { public: BOOST_CONCEPT_ASSERT((CipherConcept)); CryCipherInstance(const std::string &cipherName, const optional warning = none): _cipherName(cipherName), _warning(warning) { } const string &cipherName() const override { return _cipherName; } const optional &warning() const override { return _warning; } unique_ref createEncryptedBlockstore(unique_ref baseBlockStore, const string &encKey) const override { return make_unique_ref>(std::move(baseBlockStore), Cipher::EncryptionKey::FromString(encKey)); } string createKey(cpputils::RandomGenerator &randomGenerator) const override { return Cipher::CreateKey(randomGenerator).ToString(); } private: string _cipherName; optional _warning; }; const string CryCiphers::INTEGRITY_WARNING = "This cipher does not ensure integrity."; //We have to use shared_ptr instead of unique_ref, because c++ initializer_list needs copyable values const vector> CryCiphers::SUPPORTED_CIPHERS = { make_shared>("aes-256-gcm"), make_shared>("aes-256-cfb", INTEGRITY_WARNING), make_shared>("aes-128-gcm"), make_shared>("aes-128-cfb", INTEGRITY_WARNING), make_shared>("twofish-256-gcm"), make_shared>("twofish-256-cfb", INTEGRITY_WARNING), make_shared>("twofish-128-gcm"), make_shared>("twofish-128-cfb", INTEGRITY_WARNING), make_shared>("serpent-256-gcm"), make_shared>("serpent-256-cfb", INTEGRITY_WARNING), make_shared>("serpent-128-gcm"), make_shared>("serpent-128-cfb", INTEGRITY_WARNING), make_shared>("cast-256-gcm"), make_shared>("cast-256-cfb", INTEGRITY_WARNING), make_shared>("mars-448-gcm"), make_shared>("mars-448-cfb", INTEGRITY_WARNING), make_shared>("mars-256-gcm"), make_shared>("mars-256-cfb", INTEGRITY_WARNING), make_shared>("mars-128-gcm"), make_shared>("mars-128-cfb", INTEGRITY_WARNING) }; const CryCipher& CryCiphers::find(const string &cipherName) { auto found = std::find_if(CryCiphers::SUPPORTED_CIPHERS.begin(), CryCiphers::SUPPORTED_CIPHERS.end(), [cipherName] (const auto& element) { return element->cipherName() == cipherName; }); ASSERT(found != CryCiphers::SUPPORTED_CIPHERS.end(), "Unknown Cipher"); return **found; } vector CryCiphers::supportedCipherNames() { vector result; for (const auto& cipher : CryCiphers::SUPPORTED_CIPHERS) { result.push_back(cipher->cipherName()); } return result; }