From e19f33351ddd630351f391a37ad98a5d03519bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Tue, 6 Mar 2018 19:46:24 +0100 Subject: [PATCH] add additional cipher modes --- Crypto/include/Poco/Crypto/CipherKeyImpl.h | 19 ++++++++++-------- Crypto/src/CipherKeyImpl.cpp | 23 +++++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Crypto/include/Poco/Crypto/CipherKeyImpl.h b/Crypto/include/Poco/Crypto/CipherKeyImpl.h index e1febd9fd..e35d6b343 100644 --- a/Crypto/include/Poco/Crypto/CipherKeyImpl.h +++ b/Crypto/include/Poco/Crypto/CipherKeyImpl.h @@ -48,11 +48,14 @@ public: MODE_ECB, /// Electronic codebook (plain concatenation) MODE_CBC, /// Cipher block chaining (default) MODE_CFB, /// Cipher feedback - MODE_OFB /// Output feedback + MODE_OFB, /// Output feedback + MODE_CTR, /// Counter mode + MODE_GCM, /// Galois/Counter mode + MODE_CCM /// Counter with CBC-MAC }; - CipherKeyImpl(const std::string& name, - const std::string& passphrase, + CipherKeyImpl(const std::string& name, + const std::string& passphrase, const std::string& salt, int iterationCount, const std::string& digest); @@ -60,10 +63,10 @@ public: /// the given cipher name, passphrase, salt value /// and iteration count. - CipherKeyImpl(const std::string& name, - const ByteVec& key, + CipherKeyImpl(const std::string& name, + const ByteVec& key, const ByteVec& iv); - /// Creates a new CipherKeyImpl object, using the + /// Creates a new CipherKeyImpl object, using the /// given cipher name, key and initialization vector. CipherKeyImpl(const std::string& name); @@ -87,7 +90,7 @@ public: Mode mode() const; /// Returns the Cipher's mode of operation. - + const ByteVec& getKey() const; /// Returns the key for the Cipher. @@ -102,7 +105,7 @@ public: const EVP_CIPHER* cipher(); /// Returns the cipher object - + private: void generateKey(const std::string& passphrase, const std::string& salt, diff --git a/Crypto/src/CipherKeyImpl.cpp b/Crypto/src/CipherKeyImpl.cpp index d2bf1e1c5..b30cbd5e7 100644 --- a/Crypto/src/CipherKeyImpl.cpp +++ b/Crypto/src/CipherKeyImpl.cpp @@ -25,8 +25,8 @@ namespace Poco { namespace Crypto { -CipherKeyImpl::CipherKeyImpl(const std::string& name, - const std::string& passphrase, +CipherKeyImpl::CipherKeyImpl(const std::string& name, + const std::string& passphrase, const std::string& salt, int iterationCount, const std::string& digest): @@ -54,8 +54,8 @@ CipherKeyImpl::CipherKeyImpl(const std::string& name, } -CipherKeyImpl::CipherKeyImpl(const std::string& name, - const ByteVec& key, +CipherKeyImpl::CipherKeyImpl(const std::string& name, + const ByteVec& key, const ByteVec& iv): _pCipher(0), _pDigest(0), @@ -71,7 +71,7 @@ CipherKeyImpl::CipherKeyImpl(const std::string& name, throw Poco::NotFoundException("Cipher " + name + " was not found"); } - + CipherKeyImpl::CipherKeyImpl(const std::string& name): _pCipher(0), _pDigest(0), @@ -114,6 +114,15 @@ CipherKeyImpl::Mode CipherKeyImpl::mode() const case EVP_CIPH_OFB_MODE: return MODE_OFB; + + case EVP_CIPH_CTR_MODE: + return MODE_CTR; + + case EVP_CIPH_GCM_MODE: + return MODE_GCM; + + case EVP_CIPH_CCM_MODE: + return MODE_CCM; } throw Poco::IllegalStateException("Unexpected value of EVP_CIPHER_mode()"); } @@ -125,7 +134,7 @@ void CipherKeyImpl::generateKey() getRandomBytes(vec, keySize()); setKey(vec); - + getRandomBytes(vec, ivSize()); setIV(vec); } @@ -134,7 +143,7 @@ void CipherKeyImpl::generateKey() void CipherKeyImpl::getRandomBytes(ByteVec& vec, std::size_t count) { Poco::RandomInputStream random; - + vec.clear(); vec.reserve(count);