From 959494871fab813780b3f8a0e36bf2fdbbffb766 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 2 Jun 2019 05:29:08 -0400 Subject: Guard use of volatile cast in TEA and XTEA --- tea.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'tea.cpp') diff --git a/tea.cpp b/tea.cpp index 30e75832..32708c93 100644 --- a/tea.cpp +++ b/tea.cpp @@ -4,6 +4,13 @@ #include "tea.h" #include "misc.h" +// http://github.com/weidai11/cryptopp/issues/503 +#if defined(__xlC__) || defined(__SUNPRO_CC) +# define MAYBE_VOLATILE(x) (*const_cast(&x)) +#else +# define MAYBE_VOLATILE(x) (x) +#endif + NAMESPACE_BEGIN(CryptoPP) static const word32 DELTA = 0x9e3779b9; @@ -26,7 +33,7 @@ void TEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byt Block::Get(inBlock)(y)(z); // http://github.com/weidai11/cryptopp/issues/503 - while (*const_cast(&sum) != m_limit) + while (MAYBE_VOLATILE(sum) != m_limit) { sum += DELTA; y += ((z << 4) + m_k[0]) ^ (z + sum) ^ ((z >> 5) + m_k[1]); @@ -42,7 +49,7 @@ void TEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byt Block::Get(inBlock)(y)(z); // http://github.com/weidai11/cryptopp/issues/503 - while (*const_cast(&sum) != 0) + while (MAYBE_VOLATILE(sum) != 0) { z -= ((y << 4) + m_k[2]) ^ (y + sum) ^ ((y >> 5) + m_k[3]); y -= ((z << 4) + m_k[0]) ^ (z + sum) ^ ((z >> 5) + m_k[1]); @@ -66,7 +73,7 @@ void XTEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by Block::Get(inBlock)(y)(z); // http://github.com/weidai11/cryptopp/issues/503 - while (*const_cast(&sum) != m_limit) + while (MAYBE_VOLATILE(sum) != m_limit) { y += ((z<<4 ^ z>>5) + z) ^ (sum + m_k[sum&3]); sum += DELTA; @@ -82,7 +89,7 @@ void XTEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by Block::Get(inBlock)(y)(z); // http://github.com/weidai11/cryptopp/issues/503 - while (*const_cast(&sum) != 0) + while (MAYBE_VOLATILE(sum) != 0) { z -= ((y<<4 ^ y>>5) + y) ^ (sum + m_k[sum>>11 & 3]); sum -= DELTA; -- cgit v1.2.1