summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.h4
-rw-r--r--misc.h31
-rw-r--r--rijndael.cpp31
-rw-r--r--validat1.cpp15
4 files changed, 28 insertions, 53 deletions
diff --git a/config.h b/config.h
index 3d721e87..d2635e7a 100644
--- a/config.h
+++ b/config.h
@@ -75,8 +75,8 @@
// this file. At the moment it should only affect std::uncaught_exceptions.
// #define CRYPTOPP_NO_CXX17 1
-// Define this to allow unaligned data access. If you experience a break with
-// GCC at -O3, you should immediately suspect unaligned data accesses.
+// CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is no longer honored. It
+// was removed at https://github.com/weidai11/cryptopp/issues/682
// #define CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 1
// ***************** Less Important Settings ***************
diff --git a/misc.h b/misc.h
index 12529781..a80b3801 100644
--- a/misc.h
+++ b/misc.h
@@ -1043,22 +1043,15 @@ inline T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
/// \brief Returns the minimum alignment requirements of a type
/// \tparam T class or type
/// \returns the minimum alignment requirements of <tt>T</tt>, in bytes
-/// \details Internally the function calls C++11's <tt>alignof</tt> if available. If not available,
-/// then the function uses compiler specific extensions such as <tt>__alignof</tt> and
-/// <tt>_alignof_</tt>. If an extension is not available, then the function uses
-/// <tt>__BIGGEST_ALIGNMENT__</tt> if <tt>__BIGGEST_ALIGNMENT__</tt> is smaller than <tt>sizeof(T)</tt>.
-/// <tt>sizeof(T)</tt> is used if all others are not available.
-/// In <em>all</em> cases, if <tt>CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS</tt> is defined, then the
-/// function returns 1.
+/// \details Internally the function calls C++11's <tt>alignof</tt> if available. If not
+/// available, then the function uses compiler specific extensions such as
+/// <tt>__alignof</tt> and <tt>_alignof_</tt>. If an extension is not available, then
+/// the function uses <tt>__BIGGEST_ALIGNMENT__</tt> if <tt>__BIGGEST_ALIGNMENT__</tt>
+/// is smaller than <tt>sizeof(T)</tt>. <tt>sizeof(T)</tt> is used if all others are
+/// not available.
template <class T>
inline unsigned int GetAlignmentOf()
{
-// GCC 4.6 (circa 2008) and above aggressively uses vectorization.
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS)
- if (sizeof(T) < 16)
- return 1;
-#endif
-
#if defined(CRYPTOPP_CXX11_ALIGNOF)
return alignof(T);
#elif (_MSC_VER >= 1300)
@@ -2113,7 +2106,6 @@ inline void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, s
ConditionalByteReverse(order, out, out, RoundUpToMultipleOf(inlen, U));
}
-#ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
inline byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const byte *)
{
CRYPTOPP_UNUSED(order);
@@ -2284,7 +2276,6 @@ inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word64 value,
}
}
}
-#endif // #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
/// \brief Access a block of memory
/// \tparam T class or type
@@ -2306,13 +2297,10 @@ template <class T>
inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
{
CRYPTOPP_UNUSED(assumeAligned);
-#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
- return ConditionalByteReverse(order, *reinterpret_cast<const T *>((const void *)block));
-#else
+
T temp;
memcpy(&temp, block, sizeof(T));
return ConditionalByteReverse(order, temp);
-#endif
}
/// \brief Access a block of memory
@@ -2351,14 +2339,11 @@ template <class T>
inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock = NULLPTR)
{
CRYPTOPP_UNUSED(assumeAligned);
-#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
- *reinterpret_cast<T *>((void *)block) = ConditionalByteReverse(order, value) ^ (xorBlock ? *reinterpret_cast<const T *>((const void *)xorBlock) : 0);
-#else
+
T t1, t2;
t1 = ConditionalByteReverse(order, value);
if (xorBlock) {memcpy(&t2, xorBlock, sizeof(T)); t1 ^= t2;}
memcpy(block, &t1, sizeof(T));
-#endif
}
/// \brief Access a block of memory
diff --git a/rijndael.cpp b/rijndael.cpp
index 100d2a6c..1553e02e 100644
--- a/rijndael.cpp
+++ b/rijndael.cpp
@@ -30,8 +30,9 @@ x86 assembly code, doing an 8-bit register move to minimize the number of
register spills. Also switched to compressed tables and copying round keys to
the stack.
-The C++ implementation now uses compressed tables if
-CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is defined.
+The C++ implementation uses compressed tables if
+CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS is defined.
+It is defined on x86 platforms by default but no others.
*/
/*
@@ -95,7 +96,7 @@ NAMESPACE_BEGIN(CryptoPP)
#endif
// Hack for http://github.com/weidai11/cryptopp/issues/42 and http://github.com/weidai11/cryptopp/issues/132
-#if (CRYPTOPP_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)) && !defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS)
+#if (CRYPTOPP_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE))
# define CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS 1
#endif
@@ -103,7 +104,7 @@ NAMESPACE_BEGIN(CryptoPP)
#define M128I_CAST(x) ((__m128i *)(void *)(x))
#define CONST_M128I_CAST(x) ((const __m128i *)(const void *)(x))
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+#if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
# if (CRYPTOPP_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)) && !defined(CRYPTOPP_DISABLE_RIJNDAEL_ASM)
namespace rdtable {CRYPTOPP_ALIGN_DATA(16) word64 Te[256+2];}
using namespace rdtable;
@@ -111,14 +112,14 @@ using namespace rdtable;
static word64 Te[256];
# endif
static word64 Td[256];
-#else // Not CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
+#else // Not CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS
# if defined(CRYPTOPP_X64_MASM_AVAILABLE)
// Unused; avoids linker error on Microsoft X64 non-AESNI platforms
namespace rdtable {CRYPTOPP_ALIGN_DATA(16) word64 Te[256+2];}
# endif
CRYPTOPP_ALIGN_DATA(16) static word32 Te[256*4];
CRYPTOPP_ALIGN_DATA(16) static word32 Td[256*4];
-#endif // CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
+#endif // CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS
static volatile bool s_TeFilled = false, s_TdFilled = false;
@@ -199,7 +200,7 @@ ANONYMOUS_NAMESPACE_END
tempBlock[c] = ((byte *)(Te+byte(t)))[1]; t >>= 8;\
tempBlock[d] = ((byte *)(Te+t))[1];
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+#if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
#define QUARTER_ROUND_LD(t, a, b, c, d) \
tempBlock[a] = ((byte *)(Td+byte(t)))[GetNativeByteOrder()*7]; t >>= 8;\
tempBlock[b] = ((byte *)(Td+byte(t)))[GetNativeByteOrder()*7]; t >>= 8;\
@@ -219,7 +220,7 @@ ANONYMOUS_NAMESPACE_END
#ifdef CRYPTOPP_LITTLE_ENDIAN
#define QUARTER_ROUND_FE(t, a, b, c, d) QUARTER_ROUND(TL_F, Te, t, d, c, b, a)
#define QUARTER_ROUND_FD(t, a, b, c, d) QUARTER_ROUND(TL_F, Td, t, d, c, b, a)
- #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+ #if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
#define TL_F(T, i, x) (*(word32 *)(void *)((byte *)T + x*8 + (6-i)%4+1))
#define TL_M(T, i, x) (*(word32 *)(void *)((byte *)T + x*8 + (i+3)%4+1))
#else
@@ -229,7 +230,7 @@ ANONYMOUS_NAMESPACE_END
#else
#define QUARTER_ROUND_FE(t, a, b, c, d) QUARTER_ROUND(TL_F, Te, t, a, b, c, d)
#define QUARTER_ROUND_FD(t, a, b, c, d) QUARTER_ROUND(TL_F, Td, t, a, b, c, d)
- #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+ #if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
#define TL_F(T, i, x) (*(word32 *)(void *)((byte *)T + x*8 + (4-i)%4))
#define TL_M TL_F
#else
@@ -272,7 +273,7 @@ void Rijndael::Base::FillEncTable()
for (int i=0; i<256; i++)
{
byte x = Se[i];
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+#if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
word32 y = word32(x)<<8 | word32(x)<<16 | word32(f2(x))<<24;
Te[i] = word64(y | f3(x))<<32 | y;
#else
@@ -295,7 +296,7 @@ void Rijndael::Base::FillDecTable()
for (int i=0; i<256; i++)
{
byte x = Sd[i];
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+#if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
word32 y = word32(fd(x))<<8 | word32(f9(x))<<16 | word32(fe(x))<<24;
Td[i] = word64(y | fb(x))<<32 | y | x;
#else
@@ -591,7 +592,7 @@ void Rijndael::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock
unsigned int i;
volatile word32 _u = 0;
word32 u = _u;
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+#if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
for (i=0; i<2048; i+=cacheLineSize)
#else
for (i=0; i<1024; i+=cacheLineSize)
@@ -693,7 +694,7 @@ void Rijndael::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock
unsigned int i;
volatile word32 _u = 0;
word32 u = _u;
-#if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
+#if defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS)
for (i=0; i<2048; i+=cacheLineSize)
#else
for (i=0; i<1024; i+=cacheLineSize)
@@ -728,9 +729,9 @@ void Rijndael::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock
rk += 8;
} while (--r);
-#if !(defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) || defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS))
+#if !(defined(CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS))
// timing attack countermeasure. see comments at top for more details
- // If CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is defined,
+ // If CRYPTOPP_ALLOW_RIJNDAEL_UNALIGNED_DATA_ACCESS is defined,
// QUARTER_ROUND_LD will use Td, which is already preloaded.
u = _u;
for (i=0; i<256; i+=cacheLineSize)
diff --git a/validat1.cpp b/validat1.cpp
index 16b5fa92..77dba93f 100644
--- a/validat1.cpp
+++ b/validat1.cpp
@@ -281,19 +281,8 @@ bool TestSettings()
std::cout << "Library version (library): " << v1 << ", header version (app): " << v2 << "\n";
#endif
-#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
- // Don't assert the alignment of testvals. That's what this test is for.
- byte testvals[10] = {1,2,2,3,3,3,3,2,2,1};
- if (*(word32 *)(void *)(testvals+3) == 0x03030303 && *(word64 *)(void *)(testvals+1) == W64LIT(0x0202030303030202))
- std::cout << "passed: Unaligned data access (CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS).\n";
- else
- {
- std::cout << "FAILED: Unaligned data access gave incorrect results.\n";
- pass = false;
- }
-#else
- std::cout << "passed: Aligned data access (no CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS).\n";
-#endif
+ // CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS removed at Issue 682.
+ std::cout << "passed: Aligned data access.\n";
if (sizeof(byte) == 1)
std::cout << "passed: ";