6 #ifndef CRYPTOPP_MISC_H
7 #define CRYPTOPP_MISC_H
17 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
19 #if (CRYPTOPP_MSC_VERSION)
20 # pragma warning(push)
21 # pragma warning(disable: 4146 4514)
22 # if (CRYPTOPP_MSC_VERSION >= 1400)
23 # pragma warning(disable: 6326)
28 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
29 # pragma GCC diagnostic push
30 # pragma GCC diagnostic ignored "-Wconversion"
31 # pragma GCC diagnostic ignored "-Wsign-conversion"
32 # pragma GCC diagnostic ignored "-Wunused-function"
38 #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
39 #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
40 #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3
41 #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4
43 #undef _interlockedbittestandset
44 #undef _interlockedbittestandreset
45 #undef _interlockedbittestandset64
46 #undef _interlockedbittestandreset64
47 #define CRYPTOPP_FAST_ROTATE(x) 1
48 #elif _MSC_VER >= 1300
49 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
51 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
53 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
54 (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
55 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
56 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions
57 #define CRYPTOPP_FAST_ROTATE(x) 1
59 #define CRYPTOPP_FAST_ROTATE(x) 0
67 #if (defined(__GNUC__) || defined(__clang__)) && defined(__linux__)
68 #define CRYPTOPP_BYTESWAP_AVAILABLE 1
73 #if defined(__arm__) && (defined(__GNUC__) || defined(__clang__)) && (__ARM_ARCH >= 6)
74 #define CRYPTOPP_ARM_BYTEREV_AVAILABLE 1
78 #if defined(__arm__) && (defined(__GNUC__) || defined(__clang__)) && (__ARM_ARCH >= 7)
79 #define CRYPTOPP_ARM_BITREV_AVAILABLE 1
83 # include <x86intrin.h>
84 # include <immintrin.h>
89 #if defined(__clang__)
91 # define _blsr_u32 __blsr_u32
94 # define _blsr_u64 __blsr_u64
97 # define _tzcnt_u32 __tzcnt_u32
100 # define _tzcnt_u64 __tzcnt_u64
104 #endif // CRYPTOPP_DOXYGEN_PROCESSING
106 #if CRYPTOPP_DOXYGEN_PROCESSING
118 # define SIZE_MAX ...
124 # if defined(__SIZE_MAX__)
125 # define SIZE_MAX __SIZE_MAX__
126 # elif defined(SIZE_T_MAX)
127 # define SIZE_MAX SIZE_T_MAX
128 # elif defined(__SIZE_TYPE__)
129 # define SIZE_MAX (~(__SIZE_TYPE__)0)
131 # define SIZE_MAX ((std::numeric_limits<size_t>::max)())
135 #endif // CRYPTOPP_DOXYGEN_PROCESSING
144 #if CRYPTOPP_DOXYGEN_PROCESSING
151 # define CRYPTOPP_COMPILE_ASSERT(expr) { ... }
152 #elif defined(CRYPTOPP_CXX17_STATIC_ASSERT)
153 # define CRYPTOPP_COMPILE_ASSERT(expr) static_assert(expr)
154 #else // CRYPTOPP_DOXYGEN_PROCESSING
158 static char dummy[2*b-1];
161 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
162 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
163 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
165 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
166 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
168 # if defined(__GNUC__) || defined(__clang__)
169 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
170 static CompileAssert<(assertion)> \
171 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) __attribute__ ((unused))
173 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
174 static CompileAssert<(assertion)> \
175 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance)
176 # endif // GCC or Clang
179 #endif // CRYPTOPP_DOXYGEN_PROCESSING
183 #if CRYPTOPP_DOXYGEN_PROCESSING
191 # define COUNTOF(arr)
195 # if defined(_MSC_VER) && (_MSC_VER >= 1400)
196 # define COUNTOF(x) _countof(x)
198 # define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
201 #endif // CRYPTOPP_DOXYGEN_PROCESSING
211 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
212 template <
class BASE1,
class BASE2>
213 class CRYPTOPP_NO_VTABLE TwoBases :
public BASE1,
public BASE2
217 template <
class BASE1,
class BASE2,
class BASE3>
218 class CRYPTOPP_NO_VTABLE ThreeBases :
public BASE1,
public BASE2,
public BASE3
221 #endif // CRYPTOPP_DOXYGEN_PROCESSING
242 #if CRYPTOPP_CXX11_DELETED_FUNCTIONS
258 T* operator()()
const {
return new T;}
261 #if CRYPTOPP_DOXYGEN_PROCESSING
270 #define MEMORY_BARRIER ...
272 #if defined(CRYPTOPP_CXX11_ATOMIC)
273 # define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
274 #elif (_MSC_VER >= 1400)
275 # pragma intrinsic(_ReadWriteBarrier)
276 # define MEMORY_BARRIER() _ReadWriteBarrier()
277 #elif defined(__INTEL_COMPILER)
278 # define MEMORY_BARRIER() __memory_barrier()
279 #elif defined(__GNUC__) || defined(__clang__)
280 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
282 # define MEMORY_BARRIER()
284 #endif // CRYPTOPP_DOXYGEN_PROCESSING
305 template <
class T,
class F = NewObject<T>,
int instance=0>
309 Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
312 CRYPTOPP_NOINLINE
const T &
Ref(CRYPTOPP_NOINLINE_DOTDOTDOT)
const;
326 template <
class T,
class F,
int instance>
329 #if defined(CRYPTOPP_CXX11_ATOMIC) && defined(CRYPTOPP_CXX11_SYNCHRONIZATION) && defined(CRYPTOPP_CXX11_STATIC_INIT)
330 static std::mutex s_mutex;
331 static std::atomic<T*> s_pObject;
333 T *p = s_pObject.load(std::memory_order_relaxed);
334 std::atomic_thread_fence(std::memory_order_acquire);
339 std::lock_guard<std::mutex> lock(s_mutex);
340 p = s_pObject.load(std::memory_order_relaxed);
341 std::atomic_thread_fence(std::memory_order_acquire);
346 T *newObject = m_objectFactory();
347 s_pObject.store(newObject, std::memory_order_relaxed);
348 std::atomic_thread_fence(std::memory_order_release);
353 T *p = s_pObject.m_p;
359 T *newObject = m_objectFactory();
369 s_pObject.m_p = newObject;
385 template <
typename PTR,
typename OFF>
386 inline PTR
PtrAdd(PTR pointer, OFF offset)
388 return pointer+
static_cast<ptrdiff_t
>(offset);
398 template <
typename PTR,
typename OFF>
399 inline PTR
PtrSub(PTR pointer, OFF offset)
401 return pointer-
static_cast<ptrdiff_t
>(offset);
413 template <
typename PTR>
414 inline ptrdiff_t
PtrDiff(
const PTR pointer1,
const PTR pointer2)
416 return pointer1 - pointer2;
428 template <
typename PTR>
431 return (
size_t)(
reinterpret_cast<uintptr_t
>(pointer1) -
reinterpret_cast<uintptr_t
>(pointer2));
446 return reinterpret_cast<byte*
>(&str[0]);
465 return reinterpret_cast<const byte*
>(&str[0]);
496 #if (CRYPTOPP_CXX11_CONSTEXPR)
497 template <
typename T>
499 return static_cast<int>(v);
502 # define EnumToInt(v) static_cast<int>(v)
505 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
525 inline void memcpy_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
536 if (count > sizeInBytes)
539 #if CRYPTOPP_MSC_VERSION
540 # pragma warning(push)
541 # pragma warning(disable: 4996)
542 # if (CRYPTOPP_MSC_VERSION >= 1400)
543 # pragma warning(disable: 6386)
546 if (src != NULLPTR && dest != NULLPTR)
547 std::memcpy(dest, src, count);
548 #if CRYPTOPP_MSC_VERSION
549 # pragma warning(pop)
571 inline void memmove_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
580 if (count > sizeInBytes)
583 #if CRYPTOPP_MSC_VERSION
584 # pragma warning(push)
585 # pragma warning(disable: 4996)
586 # if (CRYPTOPP_MSC_VERSION >= 1400)
587 # pragma warning(disable: 6386)
590 if (src != NULLPTR && dest != NULLPTR)
591 std::memmove(dest, src, count);
592 #if CRYPTOPP_MSC_VERSION
593 # pragma warning(pop)
597 #if __BORLANDC__ >= 0x620
600 # define memcpy_s CryptoPP::memcpy_s
601 # define memmove_s CryptoPP::memmove_s
604 #endif // __STDC_WANT_SECURE_LIB__
621 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
638 inline void *
memset_z(
void *ptr,
int val,
size_t num)
641 #if CRYPTOPP_GCC_VERSION >= 30001 || CRYPTOPP_LLVM_CLANG_VERSION >= 20800 || \
642 CRYPTOPP_APPLE_CLANG_VERSION >= 30000
643 if (__builtin_constant_p(num) && num==0)
646 return std::memset(ptr, val, num);
655 template <
class T>
inline const T&
STDMIN(
const T& a,
const T& b)
657 return b < a ? b : a;
666 template <
class T>
inline const T&
STDMAX(
const T& a,
const T& b)
668 return a < b ? b : a;
671 #if CRYPTOPP_MSC_VERSION
672 # pragma warning(push)
673 # pragma warning(disable: 4389)
676 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
677 # pragma GCC diagnostic push
678 # pragma GCC diagnostic ignored "-Wsign-compare"
679 # pragma GCC diagnostic ignored "-Wstrict-overflow"
680 # if (CRYPTOPP_LLVM_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000)
681 # pragma GCC diagnostic ignored "-Wtautological-compare"
682 # elif (CRYPTOPP_GCC_VERSION >= 40300)
683 # pragma GCC diagnostic ignored "-Wtype-limits"
694 template <
class T1,
class T2>
inline const T1
UnsignedMin(
const T1& a,
const T2& b)
697 if (
sizeof(T1)<=
sizeof(T2))
698 return b < (T2)a ? (T1)b : a;
700 return (T1)b < a ? (T1)b : a;
709 template <
class T1,
class T2>
712 to =
static_cast<T2
>(from);
713 if (from != to || (from > 0) != (to > 0))
727 const unsigned int HIGH_BIT = (1U << 31);
728 const char CH = !!(base & HIGH_BIT) ?
'A' :
'a';
744 T digit = value % base;
745 result = char((digit < 10 ?
'0' : (CH - 10)) + digit) + result;
749 result =
"-" + result;
759 template <> CRYPTOPP_DLL
781 template <> CRYPTOPP_DLL
784 #if CRYPTOPP_MSC_VERSION
785 # pragma warning(pop)
788 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
789 # pragma GCC diagnostic pop
792 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
795 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
800 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
809 for (
unsigned int i=8*
sizeof(value)/2; i>0; i/=2)
811 return (
unsigned int)value&1;
824 unsigned int l=0, h=8*
sizeof(value);
827 unsigned int t = (l+h)/2;
847 unsigned int l=0, h=8*
sizeof(value);
851 unsigned int t = (l+h)/2;
874 return (
unsigned int)_tzcnt_u32(v);
875 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
876 return (
unsigned int)__builtin_ctz(v);
877 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
878 unsigned long result;
879 _BitScanForward(&result, v);
880 return static_cast<unsigned int>(result);
883 static const int MultiplyDeBruijnBitPosition[32] =
885 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
886 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
888 return MultiplyDeBruijnBitPosition[((
word32)((v & -v) * 0x077CB531U)) >> 27];
904 #if defined(__BMI__) && defined(__x86_64__)
905 return (
unsigned int)_tzcnt_u64(v);
906 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
907 return (
unsigned int)__builtin_ctzll(v);
908 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && (defined(_M_X64) || defined(_M_IA64))
909 unsigned long result;
910 _BitScanForward64(&result, v);
911 return static_cast<unsigned int>(result);
926 inline T
Crop(T value,
size_t bits)
928 if (bits < 8*
sizeof(value))
929 return T(value & ((T(1) << bits) - 1));
940 return ((bitCount+7)/(8));
988 CRYPTOPP_DLL
void CRYPTOPP_API xorbuf(
byte *output,
const byte *input,
const byte *mask,
size_t count);
1012 return value > 0 && (value & (value-1)) == 0;
1015 #if defined(__BMI__)
1017 inline bool IsPowerOf2<word32>(
const word32 &value)
1019 return value > 0 && _blsr_u32(value) == 0;
1022 # if defined(__x86_64__)
1024 inline bool IsPowerOf2<word64>(
const word64 &value)
1026 return value > 0 && _blsr_u64(value) == 0;
1028 # endif // __x86_64__
1046 return (std::numeric_limits<T>::min)();
1064 return (std::numeric_limits<T>::max)();
1069 #if defined(CRYPTOPP_WORD128_AVAILABLE)
1092 template <
class T1,
class T2>
1096 return T1((a > b) ? (a - b) : 0);
1109 template <
class T1,
class T2>
1113 return T1((a > b) ? (a - b) : 1);
1124 template <
class T1,
class T2>
1143 template <
class T1,
class T2>
1147 #if !defined(CRYPTOPP_APPLE_CLANG_VERSION) || (CRYPTOPP_APPLE_CLANG_VERSION >= 80000)
1173 template <
class T1,
class T2>
1177 #if !defined(CRYPTOPP_APPLE_CLANG_VERSION) || (CRYPTOPP_APPLE_CLANG_VERSION >= 80000)
1185 if (NumericLimitsMax<T1>() - m + 1 < n)
1201 #if defined(CRYPTOPP_CXX11_ALIGNOF)
1203 #elif (_MSC_VER >= 1300)
1204 return __alignof(T);
1205 #elif defined(__GNUC__)
1206 return __alignof__(T);
1207 #elif defined(__SUNPRO_CC)
1208 return __alignof__(T);
1209 #elif defined(__IBM_ALIGNOF__)
1210 return __alignof__(T);
1211 #elif CRYPTOPP_BOOL_SLOW_WORD64
1228 const uintptr_t x =
reinterpret_cast<uintptr_t
>(ptr);
1229 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2(x, alignment) == 0 : x % alignment == 0);
1245 #if (CRYPTOPP_LITTLE_ENDIAN)
1247 #elif (CRYPTOPP_BIG_ENDIAN)
1250 # error "Unable to determine endianness"
1265 return NativeByteOrder::ToEnum();
1302 unsigned int carry=1;
1303 while (carry && size != 0)
1306 carry = ! ++inout[size-1];
1323 unsigned int carry=1;
1324 while (carry && size != 0)
1327 carry = ! (output[size-1] = input[size-1] + 1);
1333 output[size-1] = input[size-1];
1359 ptrdiff_t t = size_t(c) * (a - b);
1378 volatile T *p = buf+n;
1383 #if !defined(CRYPTOPP_DISABLE_ASM) && \
1384 (_MSC_VER >= 1400 || defined(__GNUC__)) && \
1385 (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1394 volatile byte *p = buf;
1396 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1398 __stosb(
reinterpret_cast<byte *
>(
reinterpret_cast<size_t>(p)), 0, n);
1409 volatile word16 *p = buf;
1411 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1413 __stosw(
reinterpret_cast<word16 *
>(
reinterpret_cast<size_t>(p)), 0, n);
1424 volatile word32 *p = buf;
1426 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1428 __stosd(
reinterpret_cast<unsigned long *
>(
reinterpret_cast<size_t>(p)), 0, n);
1439 #if CRYPTOPP_BOOL_X64
1440 volatile word64 *p = buf;
1442 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1444 __stosq(
const_cast<word64 *
>(p), 0, n);
1451 #endif // CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86
1453 #if !defined(CRYPTOPP_DISABLE_ASM) && (_MSC_VER >= 1700) && defined(_M_ARM)
1456 char *p =
reinterpret_cast<char*
>(buf+n);
1458 __iso_volatile_store8(--p, 0);
1463 short *p =
reinterpret_cast<short*
>(buf+n);
1465 __iso_volatile_store16(--p, 0);
1470 int *p =
reinterpret_cast<int*
>(buf+n);
1472 __iso_volatile_store32(--p, 0);
1477 __int64 *p =
reinterpret_cast<__int64*
>(buf+n);
1479 __iso_volatile_store64(--p, 0);
1492 if (
sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1494 else if (
sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1496 else if (
sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1499 SecureWipeBuffer(
reinterpret_cast<byte *
>(
static_cast<void *
>(buf)), n *
sizeof(T));
1553 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1554 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1556 return T((x<<R)|(x>>(-R&MASK)));
1579 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1580 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1582 return T((x >> R)|(x<<(-R&MASK)));
1604 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1605 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1607 return T((x<<y)|(x>>(-y&MASK)));
1629 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1630 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1632 return T((x >> y)|(x<<(-y&MASK)));
1649 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1650 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1652 return T((x<<y)|(x>>(-y&MASK)));
1669 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1670 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1672 return T((x>>y)|(x<<(-y&MASK)));
1684 template <
class T>
inline T
rotlMod(T x,
unsigned int y)
1686 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1687 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1688 return T((x<<(y&MASK))|(x>>(-y&MASK)));
1700 template <
class T>
inline T
rotrMod(T x,
unsigned int y)
1702 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1703 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1704 return T((x>>(y&MASK))|(x<<(-y&MASK)));
1718 template<>
inline word32 rotlFixed<word32>(
word32 x,
unsigned int y)
1722 return y ? _lrotl(x,
static_cast<byte>(y)) : x;
1734 template<>
inline word32 rotrFixed<word32>(
word32 x,
unsigned int y)
1738 return y ? _lrotr(x,
static_cast<byte>(y)) : x;
1750 template<>
inline word32 rotlVariable<word32>(
word32 x,
unsigned int y)
1753 return _lrotl(x,
static_cast<byte>(y));
1765 template<>
inline word32 rotrVariable<word32>(
word32 x,
unsigned int y)
1768 return _lrotr(x,
static_cast<byte>(y));
1779 template<>
inline word32 rotlMod<word32>(
word32 x,
unsigned int y)
1782 return _lrotl(x,
static_cast<byte>(y));
1793 template<>
inline word32 rotrMod<word32>(
word32 x,
unsigned int y)
1796 return _lrotr(x,
static_cast<byte>(y));
1799 #endif // #ifdef _MSC_VER
1801 #if (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1813 template<>
inline word64 rotlFixed<word64>(
word64 x,
unsigned int y)
1817 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1829 template<>
inline word64 rotrFixed<word64>(
word64 x,
unsigned int y)
1833 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1845 template<>
inline word64 rotlVariable<word64>(
word64 x,
unsigned int y)
1848 return _rotl64(x,
static_cast<byte>(y));
1860 template<>
inline word64 rotrVariable<word64>(
word64 x,
unsigned int y)
1863 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1874 template<>
inline word64 rotlMod<word64>(
word64 x,
unsigned int y)
1877 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1888 template<>
inline word64 rotrMod<word64>(
word64 x,
unsigned int y)
1891 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1894 #endif // #if _MSC_VER >= 1310
1896 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
1898 template<>
inline word16 rotlFixed<word16>(
word16 x,
unsigned int y)
1901 return _rotl16(x,
static_cast<byte>(y));
1904 template<>
inline word16 rotrFixed<word16>(
word16 x,
unsigned int y)
1907 return _rotr16(x,
static_cast<byte>(y));
1910 template<>
inline word16 rotlVariable<word16>(
word16 x,
unsigned int y)
1912 return _rotl16(x,
static_cast<byte>(y));
1915 template<>
inline word16 rotrVariable<word16>(
word16 x,
unsigned int y)
1917 return _rotr16(x,
static_cast<byte>(y));
1920 template<>
inline word16 rotlMod<word16>(
word16 x,
unsigned int y)
1922 return _rotl16(x,
static_cast<byte>(y));
1925 template<>
inline word16 rotrMod<word16>(
word16 x,
unsigned int y)
1927 return _rotr16(x,
static_cast<byte>(y));
1930 template<>
inline byte rotlFixed<byte>(
byte x,
unsigned int y)
1933 return _rotl8(x,
static_cast<byte>(y));
1936 template<>
inline byte rotrFixed<byte>(
byte x,
unsigned int y)
1939 return _rotr8(x,
static_cast<byte>(y));
1942 template<>
inline byte rotlVariable<byte>(
byte x,
unsigned int y)
1944 return _rotl8(x,
static_cast<byte>(y));
1947 template<>
inline byte rotrVariable<byte>(
byte x,
unsigned int y)
1949 return _rotr8(x,
static_cast<byte>(y));
1952 template<>
inline byte rotlMod<byte>(
byte x,
unsigned int y)
1954 return _rotl8(x,
static_cast<byte>(y));
1957 template<>
inline byte rotrMod<byte>(
byte x,
unsigned int y)
1959 return _rotr8(x,
static_cast<byte>(y));
1962 #endif // #if _MSC_VER >= 1400
1964 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1966 template<>
inline word32 rotlFixed<word32>(
word32 x,
unsigned int y)
1969 return y ? __rlwinm(x,y,0,31) : x;
1972 template<>
inline word32 rotrFixed<word32>(
word32 x,
unsigned int y)
1975 return y ? __rlwinm(x,32-y,0,31) : x;
1978 template<>
inline word32 rotlVariable<word32>(
word32 x,
unsigned int y)
1981 return (__rlwnm(x,y,0,31));
1984 template<>
inline word32 rotrVariable<word32>(
word32 x,
unsigned int y)
1987 return (__rlwnm(x,32-y,0,31));
1990 template<>
inline word32 rotlMod<word32>(
word32 x,
unsigned int y)
1992 return (__rlwnm(x,y,0,31));
1995 template<>
inline word32 rotrMod<word32>(
word32 x,
unsigned int y)
1997 return (__rlwnm(x,32-y,0,31));
2000 #endif // __MWERKS__ && TARGET_CPU_PPC
2012 return GETBYTE(value, index);
2014 return GETBYTE(value,
sizeof(T)-index-1);
2032 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2033 return bswap_16(value);
2034 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2035 return _byteswap_ushort(value);
2047 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2048 return bswap_32(value);
2049 #elif defined(CRYPTOPP_ARM_BYTEREV_AVAILABLE)
2051 __asm__ (
"rev %0, %1" :
"=r" (rvalue) :
"r" (value));
2053 #elif defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
2054 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2056 #elif defined(__MWERKS__) && TARGET_CPU_PPC
2057 return (
word32)__lwbrx(&value,0);
2058 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2059 return _byteswap_ulong(value);
2060 #elif CRYPTOPP_FAST_ROTATE(32) && !defined(__xlC__)
2065 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
2076 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2077 return bswap_64(value);
2078 #elif defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
2079 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2081 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2082 return _byteswap_uint64(value);
2083 #elif CRYPTOPP_BOOL_SLOW_WORD64
2086 value = ((value &
W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value &
W64LIT(0x00FF00FF00FF00FF)) << 8);
2087 value = ((value &
W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value &
W64LIT(0x0000FFFF0000FFFF)) << 16);
2097 value =
byte((value & 0xAA) >> 1) |
byte((value & 0x55) << 1);
2098 value =
byte((value & 0xCC) >> 2) |
byte((value & 0x33) << 2);
2107 #if defined(CRYPTOPP_ARM_BITREV_AVAILABLE)
2110 __asm__ (
"rbit %0, %1" :
"=r" (rvalue) :
"r" (value));
2111 return word16(rvalue >> 16);
2114 value =
word16((value & 0xAAAA) >> 1) |
word16((value & 0x5555) << 1);
2115 value =
word16((value & 0xCCCC) >> 2) |
word16((value & 0x3333) << 2);
2116 value =
word16((value & 0xF0F0) >> 4) |
word16((value & 0x0F0F) << 4);
2126 #if defined(CRYPTOPP_ARM_BITREV_AVAILABLE)
2129 __asm__ (
"rbit %0, %1" :
"=r" (rvalue) :
"r" (value));
2133 value =
word32((value & 0xAAAAAAAA) >> 1) |
word32((value & 0x55555555) << 1);
2134 value =
word32((value & 0xCCCCCCCC) >> 2) |
word32((value & 0x33333333) << 2);
2135 value =
word32((value & 0xF0F0F0F0) >> 4) |
word32((value & 0x0F0F0F0F) << 4);
2145 #if CRYPTOPP_BOOL_SLOW_WORD64
2166 else if (
sizeof(T) == 2)
2168 else if (
sizeof(T) == 4)
2170 else if (
sizeof(T) == 8)
2235 size_t count = byteCount/
sizeof(T);
2236 for (
size_t i=0; i<count; i++)
2259 memcpy_s(out, byteCount, in, byteCount);
2263 inline void GetUserKey(
ByteOrder order, T *out,
size_t outlen,
const byte *in,
size_t inlen)
2265 const size_t U =
sizeof(T);
2267 memcpy_s(out, outlen*U, in, inlen);
2268 memset_z((
byte *)out+inlen, 0, outlen*U-inlen);
2272 inline byte UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const byte *)
2274 CRYPTOPP_UNUSED(order);
2281 ? block[1] | (block[0] << 8)
2282 : block[0] | (block[1] << 8);
2297 (
word64(block[6]) << 8) |
2298 (
word64(block[5]) << 16) |
2299 (
word64(block[4]) << 24) |
2300 (
word64(block[3]) << 32) |
2301 (
word64(block[2]) << 40) |
2302 (
word64(block[1]) << 48) |
2303 (
word64(block[0]) << 56))
2306 (
word64(block[1]) << 8) |
2307 (
word64(block[2]) << 16) |
2308 (
word64(block[3]) << 24) |
2309 (
word64(block[4]) << 32) |
2310 (
word64(block[5]) << 40) |
2311 (
word64(block[6]) << 48) |
2312 (
word64(block[7]) << 56));
2315 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
byte value,
const byte *xorBlock)
2317 CRYPTOPP_UNUSED(order);
2318 block[0] =
static_cast<byte>(xorBlock ? (value ^ xorBlock[0]) : value);
2321 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
word16 value,
const byte *xorBlock)
2327 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2328 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2332 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2333 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2340 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2341 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2345 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2346 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2351 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
word32 value,
const byte *xorBlock)
2357 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2358 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2359 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2360 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2364 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2365 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2366 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2367 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2374 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2375 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2376 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2377 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2381 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2382 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2383 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2384 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2389 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
word64 value,
const byte *xorBlock)
2395 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2396 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2397 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2398 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2399 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2400 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2401 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2402 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2406 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2407 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2408 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2409 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2410 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2411 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2412 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2413 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2420 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2421 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2422 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2423 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2424 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2425 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2426 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2427 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2431 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2432 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2433 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2434 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2435 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2436 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2437 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2438 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2462 CRYPTOPP_UNUSED(assumeAligned);
2465 if (block != NULLPTR) {std::memcpy(&temp, block,
sizeof(T));}
2488 result = GetWord<T>(assumeAligned, order, block);
2502 inline void PutWord(
bool assumeAligned,
ByteOrder order,
byte *block, T value,
const byte *xorBlock = NULLPTR)
2504 CRYPTOPP_UNUSED(assumeAligned);
2508 if (xorBlock != NULLPTR) {std::memcpy(&t2, xorBlock,
sizeof(T)); t1 ^= t2;}
2509 if (block != NULLPTR) {std::memcpy(block, &t1,
sizeof(T));}
2527 template <
class T,
class B,
bool A=false>
2534 : m_block((const
byte *)block) {}
2544 x = GetWord<T>(A, B::ToEnum(), m_block);
2545 m_block +=
sizeof(T);
2550 const byte *m_block;
2568 template <
class T,
class B,
bool A=false>
2576 : m_xorBlock((const
byte *)xorBlock), m_block((
byte *)block) {}
2585 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2586 m_block +=
sizeof(T);
2588 m_xorBlock +=
sizeof(T);
2593 const byte *m_xorBlock;
2605 template <
class T,
class B,
bool GA=false,
bool PA=false>
2624 return std::string((
char *)&value,
sizeof(value));
2663 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2675 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2693 return value >> bits;
2704 return value << bits;
2716 template <
unsigned int bits,
class T>
2719 return SafeShifter<(bits>=(8*
sizeof(T)))>::RightShift(value, bits);
2730 template <
unsigned int bits,
class T>
2733 return SafeShifter<(bits>=(8*
sizeof(T)))>::LeftShift(value, bits);
2743 template<
typename InputIt,
typename T>
2744 inline InputIt
FindIfNot(InputIt first, InputIt last,
const T &value) {
2745 #ifdef CRYPTOPP_CXX11_LAMBDA
2746 return std::find_if(first, last, [&value](
const T &o) {
2750 return std::find_if(first, last, std::bind2nd(std::not_equal_to<T>(), value));
2756 #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);}
2757 #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);}
2758 #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);}
2759 #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);}
2760 #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);}
2761 #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);}
2762 #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);}
2763 #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);}
2764 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
2768 #if (CRYPTOPP_MSC_VERSION)
2769 # pragma warning(pop)
2772 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
2773 # pragma GCC diagnostic pop