6 #ifndef CRYPTOPP_MISC_H
7 #define CRYPTOPP_MISC_H
11 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
13 #if (CRYPTOPP_MSC_VERSION)
14 # pragma warning(push)
15 # pragma warning(disable: 4146 4514)
16 # if (CRYPTOPP_MSC_VERSION >= 1400)
17 # pragma warning(disable: 6326)
22 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
23 # pragma GCC diagnostic push
24 # pragma GCC diagnostic ignored "-Wconversion"
25 # pragma GCC diagnostic ignored "-Wsign-conversion"
26 # pragma GCC diagnostic ignored "-Wunused-function"
36 #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
37 #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
38 #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3
39 #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4
41 #undef _interlockedbittestandset
42 #undef _interlockedbittestandreset
43 #undef _interlockedbittestandset64
44 #undef _interlockedbittestandreset64
45 #define CRYPTOPP_FAST_ROTATE(x) 1
46 #elif _MSC_VER >= 1300
47 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
49 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
51 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
52 (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
53 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
54 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions
55 #define CRYPTOPP_FAST_ROTATE(x) 1
57 #define CRYPTOPP_FAST_ROTATE(x) 0
65 #if defined(__GNUC__) && defined(__linux__)
66 #define CRYPTOPP_BYTESWAP_AVAILABLE
71 # include <x86intrin.h>
74 #endif // CRYPTOPP_DOXYGEN_PROCESSING
76 #if CRYPTOPP_DOXYGEN_PROCESSING
92 # if defined(__SIZE_MAX__) && (__SIZE_MAX__ > 0)
93 # define SIZE_MAX __SIZE_MAX__
94 # elif defined(SIZE_T_MAX) && (SIZE_T_MAX > 0)
95 # define SIZE_MAX SIZE_T_MAX
96 # elif defined(__SIZE_TYPE__)
97 # define SIZE_MAX (~(__SIZE_TYPE__)0)
99 # define SIZE_MAX ((std::numeric_limits<size_t>::max)())
103 #endif // CRYPTOPP_DOXYGEN_PROCESSING
112 #if CRYPTOPP_DOXYGEN_PROCESSING
116 #define CRYPTOPP_COMPILE_ASSERT(expr) { ... }
117 #else // CRYPTOPP_DOXYGEN_PROCESSING
121 static char dummy[2*b-1];
124 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
125 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
126 #define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
128 # if defined(__GNUC__)
129 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
130 static CompileAssert<(assertion)> \
131 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) __attribute__ ((unused))
133 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
134 static CompileAssert<(assertion)> \
135 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance)
138 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
139 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
141 #endif // CRYPTOPP_DOXYGEN_PROCESSING
145 #if CRYPTOPP_DOXYGEN_PROCESSING
153 # define COUNTOF(arr)
157 # if defined(_MSC_VER) && (_MSC_VER >= 1400)
158 # define COUNTOF(x) _countof(x)
160 # define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
163 #endif // CRYPTOPP_DOXYGEN_PROCESSING
173 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
174 template <
class BASE1,
class BASE2>
175 class CRYPTOPP_NO_VTABLE TwoBases :
public BASE1,
public BASE2
179 template <
class BASE1,
class BASE2,
class BASE3>
180 class CRYPTOPP_NO_VTABLE ThreeBases :
public BASE1,
public BASE2,
public BASE3
183 #endif // CRYPTOPP_DOXYGEN_PROCESSING
215 T* operator()()
const {
return new T;}
218 #if CRYPTOPP_DOXYGEN_PROCESSING
227 #define MEMORY_BARRIER ...
229 #if defined(CRYPTOPP_CXX11_ATOMICS)
230 # define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
231 #elif (_MSC_VER >= 1400)
232 # pragma intrinsic(_ReadWriteBarrier)
233 # define MEMORY_BARRIER() _ReadWriteBarrier()
234 #elif defined(__INTEL_COMPILER)
235 # define MEMORY_BARRIER() __memory_barrier()
236 #elif defined(__GNUC__) || defined(__clang__)
237 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
239 # define MEMORY_BARRIER()
241 #endif // CRYPTOPP_DOXYGEN_PROCESSING
262 template <
class T,
class F = NewObject<T>,
int instance=0>
266 Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
269 CRYPTOPP_NOINLINE
const T &
Ref(CRYPTOPP_NOINLINE_DOTDOTDOT)
const;
283 template <
class T,
class F,
int instance>
286 #if defined(CRYPTOPP_CXX11_ATOMICS) && defined(CRYPTOPP_CXX11_SYNCHRONIZATION) && defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
287 static std::mutex s_mutex;
288 static std::atomic<T*> s_pObject;
290 T *p = s_pObject.load(std::memory_order_relaxed);
291 std::atomic_thread_fence(std::memory_order_acquire);
296 std::lock_guard<std::mutex> lock(s_mutex);
297 p = s_pObject.load(std::memory_order_relaxed);
298 std::atomic_thread_fence(std::memory_order_acquire);
303 T *newObject = m_objectFactory();
304 s_pObject.store(newObject, std::memory_order_relaxed);
305 std::atomic_thread_fence(std::memory_order_release);
310 T *p = s_pObject.m_p;
316 T *newObject = m_objectFactory();
326 s_pObject.m_p = newObject;
342 template <
typename PTR,
typename OFF>
343 inline PTR
PtrAdd(PTR pointer, OFF offset)
345 return pointer+
static_cast<ptrdiff_t
>(offset);
355 template <
typename PTR,
typename OFF>
356 inline PTR
PtrSub(PTR pointer, OFF offset)
358 return pointer-
static_cast<ptrdiff_t
>(offset);
370 template <
typename PTR>
371 inline ptrdiff_t
PtrDiff(
const PTR pointer1,
const PTR pointer2)
373 return pointer1 - pointer2;
385 template <
typename PTR>
388 return (
size_t)(
reinterpret_cast<uintptr_t
>(pointer1) -
reinterpret_cast<uintptr_t
>(pointer2));
402 return reinterpret_cast<byte*
>(&str[0]);
412 return reinterpret_cast<const byte*
>(str.c_str());
423 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
443 inline void memcpy_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
454 if (count > sizeInBytes)
457 #if CRYPTOPP_MSC_VERSION
458 # pragma warning(push)
459 # pragma warning(disable: 4996)
460 # if (CRYPTOPP_MSC_VERSION >= 1400)
461 # pragma warning(disable: 6386)
464 memcpy(dest, src, count);
465 #if CRYPTOPP_MSC_VERSION
466 # pragma warning(pop)
488 inline void memmove_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
497 if (count > sizeInBytes)
500 #if CRYPTOPP_MSC_VERSION
501 # pragma warning(push)
502 # pragma warning(disable: 4996)
503 # if (CRYPTOPP_MSC_VERSION >= 1400)
504 # pragma warning(disable: 6386)
507 memmove(dest, src, count);
508 #if CRYPTOPP_MSC_VERSION
509 # pragma warning(pop)
513 #if __BORLANDC__ >= 0x620
515 # define memcpy_s CryptoPP::memcpy_s
516 # define memmove_s CryptoPP::memmove_s
519 #endif // __STDC_WANT_SECURE_LIB__
536 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
550 inline void *
memset_z(
void *ptr,
int value,
size_t num)
553 #if CRYPTOPP_GCC_VERSION >= 30001
554 if (__builtin_constant_p(num) && num==0)
557 volatile void* x = memset(ptr, value, num);
558 return const_cast<void*
>(x);
567 template <
class T>
inline const T&
STDMIN(
const T& a,
const T& b)
569 return b < a ? b : a;
578 template <
class T>
inline const T&
STDMAX(
const T& a,
const T& b)
580 return a < b ? b : a;
583 #if CRYPTOPP_MSC_VERSION
584 # pragma warning(push)
585 # pragma warning(disable: 4389)
588 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
589 # pragma GCC diagnostic push
590 # pragma GCC diagnostic ignored "-Wsign-compare"
591 # pragma GCC diagnostic ignored "-Wstrict-overflow"
592 # if (CRYPTOPP_LLVM_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000)
593 # pragma GCC diagnostic ignored "-Wtautological-compare"
594 # elif (CRYPTOPP_GCC_VERSION >= 40300)
595 # pragma GCC diagnostic ignored "-Wtype-limits"
606 template <
class T1,
class T2>
inline const T1
UnsignedMin(
const T1& a,
const T2& b)
609 if (
sizeof(T1)<=
sizeof(T2))
610 return b < (T2)a ? (T1)b : a;
612 return (T1)b < a ? (T1)b : a;
621 template <
class T1,
class T2>
624 to =
static_cast<T2
>(from);
625 if (from != to || (from > 0) != (to > 0))
639 const unsigned int HIGH_BIT = (1U << 31);
640 const char CH = !!(base & HIGH_BIT) ?
'A' :
'a';
656 T digit = value % base;
657 result = char((digit < 10 ?
'0' : (CH - 10)) + digit) + result;
661 result =
"-" + result;
671 template <> CRYPTOPP_DLL
693 template <> CRYPTOPP_DLL
696 #if CRYPTOPP_MSC_VERSION
697 # pragma warning(pop)
700 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
701 # pragma GCC diagnostic pop
704 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
707 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
712 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
721 for (
unsigned int i=8*
sizeof(value)/2; i>0; i/=2)
723 return (
unsigned int)value&1;
736 unsigned int l=0, h=8*
sizeof(value);
739 unsigned int t = (l+h)/2;
759 unsigned int l=0, h=8*
sizeof(value);
763 unsigned int t = (l+h)/2;
786 return (
unsigned int)_tzcnt_u32(v);
787 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
788 return (
unsigned int)__builtin_ctz(v);
789 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
790 unsigned long result;
791 _BitScanForward(&result, v);
792 return static_cast<unsigned int>(result);
795 static const int MultiplyDeBruijnBitPosition[32] =
797 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
798 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
800 return MultiplyDeBruijnBitPosition[((word32)((v & -v) * 0x077CB531U)) >> 27];
816 #if defined(__BMI__) && defined(__x86_64__)
817 return (
unsigned int)_tzcnt_u64(v);
818 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
819 return (
unsigned int)__builtin_ctzll(v);
820 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && (defined(_M_X64) || defined(_M_IA64))
821 unsigned long result;
822 _BitScanForward64(&result, v);
823 return static_cast<unsigned int>(result);
838 inline T
Crop(T value,
size_t bits)
840 if (bits < 8*
sizeof(value))
841 return T(value & ((T(1) << bits) - 1));
852 return ((bitCount+7)/(8));
862 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
872 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
882 return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
891 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(
byte *buf,
const byte *mask,
size_t count);
900 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(
byte *output,
const byte *input,
const byte *mask,
size_t count);
912 CRYPTOPP_DLL
bool CRYPTOPP_API
VerifyBufsEqual(
const byte *buf1,
const byte *buf2,
size_t count);
923 return value > 0 && (value & (value-1)) == 0;
928 inline bool IsPowerOf2<word32>(
const word32 &value)
930 return value > 0 && _blsr_u32(value) == 0;
933 # if defined(__x86_64__)
935 inline bool IsPowerOf2<word64>(
const word64 &value)
937 return value > 0 && _blsr_u64(value) == 0;
939 # endif // __x86_64__
957 return (std::numeric_limits<T>::min)();
975 return (std::numeric_limits<T>::max)();
980 #if defined(CRYPTOPP_WORD128_AVAILABLE)
989 return (
static_cast<word128
>(LWORD_MAX) << 64U) | LWORD_MAX;
1003 template <
class T1,
class T2>
1007 return T1((a > b) ? (a - b) : 0);
1020 template <
class T1,
class T2>
1024 return T1((a > b) ? (a - b) : 1);
1035 template <
class T1,
class T2>
1054 template <
class T1,
class T2>
1058 #if !defined(CRYPTOPP_APPLE_CLANG_VERSION) || (CRYPTOPP_APPLE_CLANG_VERSION >= 80000)
1084 template <
class T1,
class T2>
1088 #if !defined(CRYPTOPP_APPLE_CLANG_VERSION) || (CRYPTOPP_APPLE_CLANG_VERSION >= 80000)
1096 if (NumericLimitsMax<T1>() - m + 1 < n)
1113 #if defined(CRYPTOPP_CXX11_ALIGNOF)
1115 #elif (_MSC_VER >= 1300)
1116 return __alignof(T);
1117 #elif defined(__GNUC__)
1118 return __alignof__(T);
1119 #elif defined(__SUNPRO_CC)
1120 return __alignof__(T);
1121 #elif defined(__IBM_ALIGNOF__)
1122 return __alignof__(T);
1123 #elif CRYPTOPP_BOOL_SLOW_WORD64
1126 # if __BIGGEST_ALIGNMENT__
1127 if (__BIGGEST_ALIGNMENT__ <
sizeof(T))
1128 return __BIGGEST_ALIGNMENT__;
1145 const uintptr_t x =
reinterpret_cast<uintptr_t
>(ptr);
1146 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2(x, alignment) == 0 : x % alignment == 0);
1162 #if (CRYPTOPP_LITTLE_ENDIAN)
1164 #elif (CRYPTOPP_BIG_ENDIAN)
1167 # error "Unable to determine endian-ness"
1182 return NativeByteOrder::ToEnum();
1229 for (
int i=
int(size-1), carry=1; i>=0 && carry; i--)
1230 carry = !++inout[i];
1245 for (i=
int(size-1), carry=1; i>=0 && carry; i--)
1246 carry = ((output[i] = input[i]+1) == 0);
1247 memcpy_s(output, size, input,
size_t(i)+1);
1271 ptrdiff_t t = size_t(c) * (a - b);
1290 volatile T *p = buf+n;
1295 #if !defined(CRYPTOPP_DISABLE_ASM) && \
1296 (_MSC_VER >= 1400 || defined(__GNUC__)) && \
1297 (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1306 volatile byte *p = buf;
1308 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1310 __stosb(
reinterpret_cast<byte *
>(
reinterpret_cast<size_t>(p)), 0, n);
1321 volatile word16 *p = buf;
1323 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1325 __stosw(
reinterpret_cast<word16 *
>(
reinterpret_cast<size_t>(p)), 0, n);
1336 volatile word32 *p = buf;
1338 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1340 __stosd(
reinterpret_cast<unsigned long *
>(
reinterpret_cast<size_t>(p)), 0, n);
1351 #if CRYPTOPP_BOOL_X64
1352 volatile word64 *p = buf;
1354 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1356 __stosq(
const_cast<word64 *
>(p), 0, n);
1363 #endif // CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86
1365 #if !defined(CRYPTOPP_DISABLE_ASM) && (_MSC_VER >= 1700) && defined(_M_ARM)
1368 char *p =
reinterpret_cast<char*
>(buf+n);
1370 __iso_volatile_store8(--p, 0);
1375 short *p =
reinterpret_cast<short*
>(buf+n);
1377 __iso_volatile_store16(--p, 0);
1382 int *p =
reinterpret_cast<int*
>(buf+n);
1384 __iso_volatile_store32(--p, 0);
1389 __int64 *p =
reinterpret_cast<__int64*
>(buf+n);
1391 __iso_volatile_store64(--p, 0);
1404 if (
sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1405 SecureWipeBuffer(
reinterpret_cast<word64 *
>(
static_cast<void *
>(buf)), n * (
sizeof(T)/8));
1406 else if (
sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1407 SecureWipeBuffer(
reinterpret_cast<word32 *
>(
static_cast<void *
>(buf)), n * (
sizeof(T)/4));
1408 else if (
sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1409 SecureWipeBuffer(
reinterpret_cast<word16 *
>(
static_cast<void *
>(buf)), n * (
sizeof(T)/2));
1411 SecureWipeBuffer(
reinterpret_cast<byte *
>(
static_cast<void *
>(buf)), n *
sizeof(T));
1426 std::string
StringNarrow(
const wchar_t *str,
bool throwOnError =
true);
1440 std::wstring
StringWiden(
const char *str,
bool throwOnError =
true);
1505 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1506 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1508 return T((x<<R)|(x>>(-R&MASK)));
1531 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1532 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1534 return T((x >> R)|(x<<(-R&MASK)));
1556 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1557 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1559 return T((x<<y)|(x>>(-y&MASK)));
1581 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1582 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1584 return T((x >> y)|(x<<(-y&MASK)));
1601 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1602 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1604 return T((x<<y)|(x>>(-y&MASK)));
1621 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1622 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1624 return T((x>>y)|(x<<(-y&MASK)));
1636 template <
class T>
inline T
rotlMod(T x,
unsigned int y)
1638 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1639 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1640 return T((x<<(y&MASK))|(x>>(-y&MASK)));
1652 template <
class T>
inline T
rotrMod(T x,
unsigned int y)
1654 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8)
1655 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1)
1656 return T((x>>(y&MASK))|(x<<(-y&MASK)));
1670 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1674 return y ? _lrotl(x,
static_cast<byte>(y)) : x;
1686 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1690 return y ? _lrotr(x,
static_cast<byte>(y)) : x;
1702 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1705 return _lrotl(x,
static_cast<byte>(y));
1717 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1720 return _lrotr(x,
static_cast<byte>(y));
1731 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1734 return _lrotl(x,
static_cast<byte>(y));
1745 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1748 return _lrotr(x,
static_cast<byte>(y));
1751 #endif // #ifdef _MSC_VER
1753 #if (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1765 template<>
inline word64 rotlFixed<word64>(word64 x,
unsigned int y)
1769 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1781 template<>
inline word64 rotrFixed<word64>(word64 x,
unsigned int y)
1785 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1797 template<>
inline word64 rotlVariable<word64>(word64 x,
unsigned int y)
1800 return _rotl64(x,
static_cast<byte>(y));
1812 template<>
inline word64 rotrVariable<word64>(word64 x,
unsigned int y)
1815 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1826 template<>
inline word64 rotlMod<word64>(word64 x,
unsigned int y)
1829 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1840 template<>
inline word64 rotrMod<word64>(word64 x,
unsigned int y)
1843 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1846 #endif // #if _MSC_VER >= 1310
1848 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
1850 template<>
inline word16 rotlFixed<word16>(word16 x,
unsigned int y)
1853 return _rotl16(x,
static_cast<byte>(y));
1856 template<>
inline word16 rotrFixed<word16>(word16 x,
unsigned int y)
1859 return _rotr16(x,
static_cast<byte>(y));
1862 template<>
inline word16 rotlVariable<word16>(word16 x,
unsigned int y)
1864 return _rotl16(x,
static_cast<byte>(y));
1867 template<>
inline word16 rotrVariable<word16>(word16 x,
unsigned int y)
1869 return _rotr16(x,
static_cast<byte>(y));
1872 template<>
inline word16 rotlMod<word16>(word16 x,
unsigned int y)
1874 return _rotl16(x,
static_cast<byte>(y));
1877 template<>
inline word16 rotrMod<word16>(word16 x,
unsigned int y)
1879 return _rotr16(x,
static_cast<byte>(y));
1882 template<>
inline byte rotlFixed<byte>(
byte x,
unsigned int y)
1885 return _rotl8(x,
static_cast<byte>(y));
1888 template<>
inline byte rotrFixed<byte>(
byte x,
unsigned int y)
1891 return _rotr8(x,
static_cast<byte>(y));
1894 template<>
inline byte rotlVariable<byte>(
byte x,
unsigned int y)
1896 return _rotl8(x,
static_cast<byte>(y));
1899 template<>
inline byte rotrVariable<byte>(
byte x,
unsigned int y)
1901 return _rotr8(x,
static_cast<byte>(y));
1904 template<>
inline byte rotlMod<byte>(
byte x,
unsigned int y)
1906 return _rotl8(x,
static_cast<byte>(y));
1909 template<>
inline byte rotrMod<byte>(
byte x,
unsigned int y)
1911 return _rotr8(x,
static_cast<byte>(y));
1914 #endif // #if _MSC_VER >= 1400
1916 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1918 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1921 return y ? __rlwinm(x,y,0,31) : x;
1924 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1927 return y ? __rlwinm(x,32-y,0,31) : x;
1930 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1933 return (__rlwnm(x,y,0,31));
1936 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1939 return (__rlwnm(x,32-y,0,31));
1942 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1944 return (__rlwnm(x,y,0,31));
1947 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1949 return (__rlwnm(x,32-y,0,31));
1952 #endif // __MWERKS__ && TARGET_CPU_PPC
1964 return GETBYTE(value, index);
1966 return GETBYTE(value,
sizeof(T)-index-1);
1982 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
1983 return bswap_16(value);
1984 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1985 return _byteswap_ushort(value);
1996 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
1997 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1999 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2000 return bswap_32(value);
2001 #elif defined(__MWERKS__) && TARGET_CPU_PPC
2002 return (word32)__lwbrx(&value,0);
2003 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2004 return _byteswap_ulong(value);
2005 #elif CRYPTOPP_FAST_ROTATE(32) && !defined(__xlC__)
2010 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
2020 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
2021 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2023 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2024 return bswap_64(value);
2025 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2026 return _byteswap_uint64(value);
2027 #elif CRYPTOPP_BOOL_SLOW_WORD64
2030 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
2031 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
2041 value = byte((value & 0xAA) >> 1) | byte((value & 0x55) << 1);
2042 value = byte((value & 0xCC) >> 2) | byte((value & 0x33) << 2);
2051 value = word16((value & 0xAAAA) >> 1) | word16((value & 0x5555) << 1);
2052 value = word16((value & 0xCCCC) >> 2) | word16((value & 0x3333) << 2);
2053 value = word16((value & 0xF0F0) >> 4) | word16((value & 0x0F0F) << 4);
2062 value = word32((value & 0xAAAAAAAA) >> 1) | word32((value & 0x55555555) << 1);
2063 value = word32((value & 0xCCCCCCCC) >> 2) | word32((value & 0x33333333) << 2);
2064 value = word32((value & 0xF0F0F0F0) >> 4) | word32((value & 0x0F0F0F0F) << 4);
2073 #if CRYPTOPP_BOOL_SLOW_WORD64
2076 value = word64((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | word64((value & W64LIT(0x5555555555555555)) << 1);
2077 value = word64((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | word64((value & W64LIT(0x3333333333333333)) << 2);
2078 value = word64((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | word64((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
2094 else if (
sizeof(T) == 2)
2096 else if (
sizeof(T) == 4)
2161 size_t count = byteCount/
sizeof(T);
2162 for (
size_t i=0; i<count; i++)
2185 memcpy_s(out, byteCount, in, byteCount);
2189 inline void GetUserKey(
ByteOrder order, T *out,
size_t outlen,
const byte *in,
size_t inlen)
2191 const size_t U =
sizeof(T);
2193 memcpy_s(out, outlen*U, in, inlen);
2194 memset_z((
byte *)out+inlen, 0, outlen*U-inlen);
2198 inline byte UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const byte *)
2200 CRYPTOPP_UNUSED(order);
2204 inline word16 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word16 *)
2207 ? block[1] | (block[0] << 8)
2208 : block[0] | (block[1] << 8);
2211 inline word32 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word32 *)
2214 ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
2215 : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
2218 inline word64 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word64 *)
2223 (word64(block[6]) << 8) |
2224 (word64(block[5]) << 16) |
2225 (word64(block[4]) << 24) |
2226 (word64(block[3]) << 32) |
2227 (word64(block[2]) << 40) |
2228 (word64(block[1]) << 48) |
2229 (word64(block[0]) << 56))
2232 (word64(block[1]) << 8) |
2233 (word64(block[2]) << 16) |
2234 (word64(block[3]) << 24) |
2235 (word64(block[4]) << 32) |
2236 (word64(block[5]) << 40) |
2237 (word64(block[6]) << 48) |
2238 (word64(block[7]) << 56));
2241 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
byte value,
const byte *xorBlock)
2243 CRYPTOPP_UNUSED(order);
2244 block[0] =
static_cast<byte>(xorBlock ? (value ^ xorBlock[0]) : value);
2247 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word16 value,
const byte *xorBlock)
2253 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2254 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2258 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2259 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2266 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2267 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2271 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2272 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2277 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word32 value,
const byte *xorBlock)
2283 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2284 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2285 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2286 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2290 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2291 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2292 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2293 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2300 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2301 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2302 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2303 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2307 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2308 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2309 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2310 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2315 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word64 value,
const byte *xorBlock)
2321 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2322 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2323 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2324 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2325 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2326 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2327 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2328 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2332 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2333 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2334 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2335 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2336 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2337 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2338 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2339 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2346 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2347 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2348 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2349 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2350 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2351 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2352 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2353 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2357 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2358 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2359 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2360 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2361 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2362 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2363 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2364 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2388 CRYPTOPP_UNUSED(assumeAligned);
2391 memcpy(&temp, block,
sizeof(T));
2414 result = GetWord<T>(assumeAligned, order, block);
2428 inline void PutWord(
bool assumeAligned,
ByteOrder order,
byte *block, T value,
const byte *xorBlock = NULLPTR)
2430 CRYPTOPP_UNUSED(assumeAligned);
2434 if (xorBlock) {memcpy(&t2, xorBlock,
sizeof(T)); t1 ^= t2;}
2435 memcpy(block, &t1,
sizeof(T));
2453 template <
class T,
class B,
bool A=false>
2460 : m_block((const byte *)block) {}
2470 x = GetWord<T>(A, B::ToEnum(), m_block);
2471 m_block +=
sizeof(T);
2476 const byte *m_block;
2494 template <
class T,
class B,
bool A=false>
2502 : m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
2511 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2512 m_block +=
sizeof(T);
2514 m_xorBlock +=
sizeof(T);
2519 const byte *m_xorBlock;
2531 template <
class T,
class B,
bool GA=false,
bool PA=false>
2550 return std::string((
char *)&value,
sizeof(value));
2589 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2601 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2619 return value >> bits;
2630 return value << bits;
2642 template <
unsigned int bits,
class T>
2645 return SafeShifter<(bits>=(8*
sizeof(T)))>::RightShift(value, bits);
2656 template <
unsigned int bits,
class T>
2659 return SafeShifter<(bits>=(8*
sizeof(T)))>::LeftShift(value, bits);
2669 template<
typename InputIt,
typename T>
2670 inline InputIt
FindIfNot(InputIt first, InputIt last,
const T &value) {
2671 #ifdef CRYPTOPP_CXX11_LAMBDA
2672 return std::find_if(first, last, [&value](
const T &o) {
2676 return std::find_if(first, last, std::bind2nd(std::not_equal_to<T>(), value));
2682 #define CRYPTOPP_BLOCK_1(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+0);} size_t SS1() {return sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2683 #define CRYPTOPP_BLOCK_2(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS1());} size_t SS2() {return SS1()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2684 #define CRYPTOPP_BLOCK_3(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS2());} size_t SS3() {return SS2()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2685 #define CRYPTOPP_BLOCK_4(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS3());} size_t SS4() {return SS3()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2686 #define CRYPTOPP_BLOCK_5(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS4());} size_t SS5() {return SS4()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2687 #define CRYPTOPP_BLOCK_6(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS5());} size_t SS6() {return SS5()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2688 #define CRYPTOPP_BLOCK_7(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS6());} size_t SS7() {return SS6()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2689 #define CRYPTOPP_BLOCK_8(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS7());} size_t SS8() {return SS7()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
2690 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
2694 #if (CRYPTOPP_MSC_VERSION)
2695 # pragma warning(pop)
2698 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
2699 # pragma GCC diagnostic pop