From 926d1ce7a4a6a50314db1325393cf13bcd70eead Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 28 Oct 2015 01:41:38 +0100 Subject: [PATCH] Allow Cipher::NAME (e.g. AES256_GCM::NAME) --- crypto/symmetric/ciphers.h | 47 ++++++++++++++++------------ test/crypto/symmetric/CipherTest.cpp | 32 ++++++++++++++++++- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/crypto/symmetric/ciphers.h b/crypto/symmetric/ciphers.h index 8fb9e5a3..f57bca84 100644 --- a/crypto/symmetric/ciphers.h +++ b/crypto/symmetric/ciphers.h @@ -10,37 +10,44 @@ #include "GCM_Cipher.h" #include "CFB_Cipher.h" +#define DEFINE_CIPHER(InstanceName, StringName, Mode, Base, Keysize) \ + class InstanceName final: public Mode { \ + public: \ + static constexpr const char *NAME = StringName; \ + } \ + + namespace cpputils { static_assert(32 == CryptoPP::AES::MAX_KEYLENGTH, "If AES offered larger keys, we should offer a variant with it"); -using AES256_GCM = GCM_Cipher; -using AES256_CFB = CFB_Cipher; -using AES128_GCM = GCM_Cipher; -using AES128_CFB = CFB_Cipher; +DEFINE_CIPHER(AES256_GCM, "aes-256-gcm", GCM_Cipher, CryptoPP::AES, 32); +DEFINE_CIPHER(AES256_CFB, "aes-256-cfb", CFB_Cipher, CryptoPP::AES, 32); +DEFINE_CIPHER(AES128_GCM, "aes-128-gcm", GCM_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"); -using Twofish256_GCM = GCM_Cipher; -using Twofish256_CFB = CFB_Cipher; -using Twofish128_GCM = GCM_Cipher; -using Twofish128_CFB = CFB_Cipher; +DEFINE_CIPHER(Twofish256_GCM, "twofish-256-gcm", GCM_Cipher, CryptoPP::Twofish, 32); +DEFINE_CIPHER(Twofish256_CFB, "twofish-256-cfb", CFB_Cipher, CryptoPP::Twofish, 32); +DEFINE_CIPHER(Twofish128_GCM, "twofish-128-gcm", GCM_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"); -using Serpent256_GCM = GCM_Cipher; -using Serpent256_CFB = CFB_Cipher; -using Serpent128_GCM = GCM_Cipher; -using Serpent128_CFB = CFB_Cipher; +DEFINE_CIPHER(Serpent256_GCM, "serpent-256-gcm", GCM_Cipher, CryptoPP::Serpent, 32); +DEFINE_CIPHER(Serpent256_CFB, "serpent-256-cfb", CFB_Cipher, CryptoPP::Serpent, 32); +DEFINE_CIPHER(Serpent128_GCM, "serpent-128-gcm", GCM_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"); -using Cast256_GCM = GCM_Cipher; -using Cast256_CFB = CFB_Cipher; +DEFINE_CIPHER(Cast256_GCM, "cast-256-gcm", GCM_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"); -using Mars448_GCM = GCM_Cipher; -using Mars448_CFB = CFB_Cipher; -using Mars256_GCM = GCM_Cipher; -using Mars256_CFB = CFB_Cipher; -using Mars128_GCM = GCM_Cipher; -using Mars128_CFB = CFB_Cipher; +DEFINE_CIPHER(Mars448_GCM, "mars-448-gcm", GCM_Cipher, CryptoPP::MARS, 56); +DEFINE_CIPHER(Mars448_CFB, "mars-448-cfb", CFB_Cipher, CryptoPP::MARS, 56); +DEFINE_CIPHER(Mars256_GCM, "mars-256-gcm", GCM_Cipher, CryptoPP::MARS, 32); +DEFINE_CIPHER(Mars256_CFB, "mars-256-cfb", CFB_Cipher, CryptoPP::MARS, 32); +DEFINE_CIPHER(Mars128_GCM, "mars-128-gcm", GCM_Cipher, CryptoPP::MARS, 16); +DEFINE_CIPHER(Mars128_CFB, "mars-128-cfb", CFB_Cipher, CryptoPP::MARS, 16); } diff --git a/test/crypto/symmetric/CipherTest.cpp b/test/crypto/symmetric/CipherTest.cpp index d51fb4c6..18d60d01 100644 --- a/test/crypto/symmetric/CipherTest.cpp +++ b/test/crypto/symmetric/CipherTest.cpp @@ -8,6 +8,7 @@ #include using namespace cpputils; +using std::string; template class CipherTest: public ::testing::Test { @@ -251,4 +252,33 @@ INSTANTIATE_TYPED_TEST_CASE_P(Mars256_GCM, CipherTest, Mars256_GCM); 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_GCM, CipherTest, Mars128_GCM); -INSTANTIATE_TYPED_TEST_CASE_P(Mars128_GCM, AuthenticatedCipherTest, Mars128_GCM); \ No newline at end of file +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)); +}