summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2019-10-13 04:31:39 -0400
committerJeffrey Walton <noloader@gmail.com>2019-10-13 04:31:39 -0400
commit9c711221c68c0366ed7549bb6c5e0ed38b25d352 (patch)
tree6e1789c33d05aecf44e9890afefedddfe4f78f8d
parent97aa173507c125ff2cc48c4ef30f776e6193ea47 (diff)
downloadcryptopp-git-9c711221c68c0366ed7549bb6c5e0ed38b25d352.tar.gz
Fix XTS GetValidKeyLength (GH #891)
-rw-r--r--xts.cpp7
-rw-r--r--xts.h7
2 files changed, 11 insertions, 3 deletions
diff --git a/xts.cpp b/xts.cpp
index fb6d2e2f..c4eeca18 100644
--- a/xts.cpp
+++ b/xts.cpp
@@ -206,9 +206,16 @@ ANONYMOUS_NAMESPACE_END
NAMESPACE_BEGIN(CryptoPP)
+void XTS_ModeBase::ThrowIfInvalidKeyLength(size_t length)
+{
+ if (!AccessBlockCipher().IsValidKeyLength((length+1)/2))
+ throw InvalidKeyLength(AlgorithmName(), length);
+}
+
void XTS_ModeBase::SetKey(const byte *key, size_t length, const NameValuePairs &params)
{
CRYPTOPP_ASSERT(length % 2 == 0);
+ ThrowIfInvalidKeyLength(length);
#if (CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS == 0)
CRYPTOPP_ASSERT(BlockSize() == 16);
diff --git a/xts.h b/xts.h
index 76c7b9ce..6559dcc7 100644
--- a/xts.h
+++ b/xts.h
@@ -61,9 +61,10 @@ public:
size_t DefaultKeyLength() const
{return GetBlockCipher().DefaultKeyLength()*2;}
size_t GetValidKeyLength(size_t n) const
- {return GetBlockCipher().GetValidKeyLength((n+1)/2);}
+ {return 2*GetBlockCipher().GetValidKeyLength((n+1)/2);}
bool IsValidKeyLength(size_t keylength) const
{return keylength == GetValidKeyLength(keylength);}
+ void ThrowIfInvalidKeyLength(size_t length);
/// Provides the block size of the cipher
/// \return the block size of the cipher, in bytes
@@ -111,8 +112,8 @@ template <class CIPHER>
class CRYPTOPP_NO_VTABLE XTS_Final : public XTS_ModeBase
{
public:
- static std::string CRYPTOPP_API StaticAlgorithmName()
- {return std::string(CIPHER::StaticAlgorithmName()) + "/XTS";}
+ static const char* CRYPTOPP_API StaticAlgorithmName()
+ {return "XTS";}
protected:
BlockCipher& AccessBlockCipher()