Allow Cipher::NAME (e.g. AES256_GCM::NAME)

This commit is contained in:
Sebastian Messmer 2015-10-28 01:41:38 +01:00
parent c50371d583
commit 926d1ce7a4
2 changed files with 58 additions and 21 deletions

View File

@ -10,37 +10,44 @@
#include "GCM_Cipher.h" #include "GCM_Cipher.h"
#include "CFB_Cipher.h" #include "CFB_Cipher.h"
#define DEFINE_CIPHER(InstanceName, StringName, Mode, Base, Keysize) \
class InstanceName final: public Mode<Base, Keysize> { \
public: \
static constexpr const char *NAME = StringName; \
} \
namespace cpputils { namespace cpputils {
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");
using AES256_GCM = GCM_Cipher<CryptoPP::AES, 32>; DEFINE_CIPHER(AES256_GCM, "aes-256-gcm", GCM_Cipher, CryptoPP::AES, 32);
using AES256_CFB = CFB_Cipher<CryptoPP::AES, 32>; DEFINE_CIPHER(AES256_CFB, "aes-256-cfb", CFB_Cipher, CryptoPP::AES, 32);
using AES128_GCM = GCM_Cipher<CryptoPP::AES, 16>; DEFINE_CIPHER(AES128_GCM, "aes-128-gcm", GCM_Cipher, CryptoPP::AES, 16);
using AES128_CFB = CFB_Cipher<CryptoPP::AES, 16>; DEFINE_CIPHER(AES128_CFB, "aes-128-cfb", CFB_Cipher, CryptoPP::AES, 16);
static_assert(32 == CryptoPP::Twofish::MAX_KEYLENGTH, "If Twofish offered larger keys, we should offer a variant with it"); static_assert(32 == CryptoPP::Twofish::MAX_KEYLENGTH, "If Twofish offered larger keys, we should offer a variant with it");
using Twofish256_GCM = GCM_Cipher<CryptoPP::Twofish, 32>; DEFINE_CIPHER(Twofish256_GCM, "twofish-256-gcm", GCM_Cipher, CryptoPP::Twofish, 32);
using Twofish256_CFB = CFB_Cipher<CryptoPP::Twofish, 32>; DEFINE_CIPHER(Twofish256_CFB, "twofish-256-cfb", CFB_Cipher, CryptoPP::Twofish, 32);
using Twofish128_GCM = GCM_Cipher<CryptoPP::Twofish, 16>; DEFINE_CIPHER(Twofish128_GCM, "twofish-128-gcm", GCM_Cipher, CryptoPP::Twofish, 16);
using Twofish128_CFB = CFB_Cipher<CryptoPP::Twofish, 16>; DEFINE_CIPHER(Twofish128_CFB, "twofish-128-cfb", CFB_Cipher, CryptoPP::Twofish, 16);
static_assert(32 == CryptoPP::Serpent::MAX_KEYLENGTH, "If Serpent offered larger keys, we should offer a variant with it"); static_assert(32 == CryptoPP::Serpent::MAX_KEYLENGTH, "If Serpent offered larger keys, we should offer a variant with it");
using Serpent256_GCM = GCM_Cipher<CryptoPP::Serpent, 32>; DEFINE_CIPHER(Serpent256_GCM, "serpent-256-gcm", GCM_Cipher, CryptoPP::Serpent, 32);
using Serpent256_CFB = CFB_Cipher<CryptoPP::Serpent, 32>; DEFINE_CIPHER(Serpent256_CFB, "serpent-256-cfb", CFB_Cipher, CryptoPP::Serpent, 32);
using Serpent128_GCM = GCM_Cipher<CryptoPP::Serpent, 16>; DEFINE_CIPHER(Serpent128_GCM, "serpent-128-gcm", GCM_Cipher, CryptoPP::Serpent, 16);
using Serpent128_CFB = CFB_Cipher<CryptoPP::Serpent, 16>; DEFINE_CIPHER(Serpent128_CFB, "serpent-128-cfb", CFB_Cipher, CryptoPP::Serpent, 16);
static_assert(32 == CryptoPP::CAST256::MAX_KEYLENGTH, "If Cast offered larger keys, we should offer a variant with it"); static_assert(32 == CryptoPP::CAST256::MAX_KEYLENGTH, "If Cast offered larger keys, we should offer a variant with it");
using Cast256_GCM = GCM_Cipher<CryptoPP::CAST256, 32>; DEFINE_CIPHER(Cast256_GCM, "cast-256-gcm", GCM_Cipher, CryptoPP::CAST256, 32);
using Cast256_CFB = CFB_Cipher<CryptoPP::CAST256, 32>; DEFINE_CIPHER(Cast256_CFB, "cast-256-cfb", CFB_Cipher, CryptoPP::CAST256, 32);
static_assert(56 == CryptoPP::MARS::MAX_KEYLENGTH, "If Mars offered larger keys, we should offer a variant with it"); static_assert(56 == CryptoPP::MARS::MAX_KEYLENGTH, "If Mars offered larger keys, we should offer a variant with it");
using Mars448_GCM = GCM_Cipher<CryptoPP::MARS, 56>; DEFINE_CIPHER(Mars448_GCM, "mars-448-gcm", GCM_Cipher, CryptoPP::MARS, 56);
using Mars448_CFB = CFB_Cipher<CryptoPP::MARS, 56>; DEFINE_CIPHER(Mars448_CFB, "mars-448-cfb", CFB_Cipher, CryptoPP::MARS, 56);
using Mars256_GCM = GCM_Cipher<CryptoPP::MARS, 32>; DEFINE_CIPHER(Mars256_GCM, "mars-256-gcm", GCM_Cipher, CryptoPP::MARS, 32);
using Mars256_CFB = CFB_Cipher<CryptoPP::MARS, 32>; DEFINE_CIPHER(Mars256_CFB, "mars-256-cfb", CFB_Cipher, CryptoPP::MARS, 32);
using Mars128_GCM = GCM_Cipher<CryptoPP::MARS, 16>; DEFINE_CIPHER(Mars128_GCM, "mars-128-gcm", GCM_Cipher, CryptoPP::MARS, 16);
using Mars128_CFB = CFB_Cipher<CryptoPP::MARS, 16>; DEFINE_CIPHER(Mars128_CFB, "mars-128-cfb", CFB_Cipher, CryptoPP::MARS, 16);
} }

View File

@ -8,6 +8,7 @@
#include <boost/optional/optional_io.hpp> #include <boost/optional/optional_io.hpp>
using namespace cpputils; using namespace cpputils;
using std::string;
template<class Cipher> template<class Cipher>
class CipherTest: public ::testing::Test { class CipherTest: public ::testing::Test {
@ -252,3 +253,32 @@ INSTANTIATE_TYPED_TEST_CASE_P(Mars256_GCM, AuthenticatedCipherTest, Mars256_GCM)
INSTANTIATE_TYPED_TEST_CASE_P(Mars128_CFB, CipherTest, Mars128_CFB); //CFB mode is not authenticated INSTANTIATE_TYPED_TEST_CASE_P(Mars128_CFB, CipherTest, Mars128_CFB); //CFB mode is not authenticated
INSTANTIATE_TYPED_TEST_CASE_P(Mars128_GCM, CipherTest, Mars128_GCM); INSTANTIATE_TYPED_TEST_CASE_P(Mars128_GCM, CipherTest, Mars128_GCM);
INSTANTIATE_TYPED_TEST_CASE_P(Mars128_GCM, AuthenticatedCipherTest, Mars128_GCM); INSTANTIATE_TYPED_TEST_CASE_P(Mars128_GCM, AuthenticatedCipherTest, Mars128_GCM);
// Test cipher names
TEST(CipherNameTest, TestCipherNames) {
EXPECT_EQ("aes-256-gcm", string(AES256_GCM::NAME));
EXPECT_EQ("aes-256-cfb", string(AES256_CFB::NAME));
EXPECT_EQ("aes-128-gcm", string(AES128_GCM::NAME));
EXPECT_EQ("aes-128-cfb", string(AES128_CFB::NAME));
EXPECT_EQ("twofish-256-gcm", string(Twofish256_GCM::NAME));
EXPECT_EQ("twofish-256-cfb", string(Twofish256_CFB::NAME));
EXPECT_EQ("twofish-128-gcm", string(Twofish128_GCM::NAME));
EXPECT_EQ("twofish-128-cfb", string(Twofish128_CFB::NAME));
EXPECT_EQ("serpent-256-gcm", string(Serpent256_GCM::NAME));
EXPECT_EQ("serpent-256-cfb", string(Serpent256_CFB::NAME));
EXPECT_EQ("serpent-128-gcm", string(Serpent128_GCM::NAME));
EXPECT_EQ("serpent-128-cfb", string(Serpent128_CFB::NAME));
EXPECT_EQ("cast-256-gcm", string(Cast256_GCM::NAME));
EXPECT_EQ("cast-256-cfb", string(Cast256_CFB::NAME));
EXPECT_EQ("mars-448-gcm", string(Mars448_GCM::NAME));
EXPECT_EQ("mars-448-cfb", string(Mars448_CFB::NAME));
EXPECT_EQ("mars-256-gcm", string(Mars256_GCM::NAME));
EXPECT_EQ("mars-256-cfb", string(Mars256_CFB::NAME));
EXPECT_EQ("mars-128-gcm", string(Mars128_GCM::NAME));
EXPECT_EQ("mars-128-cfb", string(Mars128_CFB::NAME));
}