6 #ifndef CRYPTOPP_MISC_H
7 #define CRYPTOPP_MISC_H
15 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
17 #if (CRYPTOPP_MSC_VERSION)
18 # pragma warning(push)
19 # pragma warning(disable: 4146 4514)
20 # if (CRYPTOPP_MSC_VERSION >= 1400)
21 # pragma warning(disable: 6326)
26 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
27 # pragma GCC diagnostic push
28 # pragma GCC diagnostic ignored "-Wconversion"
29 # pragma GCC diagnostic ignored "-Wsign-conversion"
30 # 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(__clang__)) && defined(__linux__)
66 #define CRYPTOPP_BYTESWAP_AVAILABLE 1
71 #if defined(__arm__) && (defined(__GNUC__) || defined(__clang__)) && (__ARM_ARCH >= 6)
72 #define CRYPTOPP_ARM_BYTEREV_AVAILABLE 1
76 #if defined(__arm__) && (defined(__GNUC__) || defined(__clang__)) && (__ARM_ARCH >= 7)
77 #define CRYPTOPP_ARM_BITREV_AVAILABLE 1
81 # include <x86intrin.h>
82 # include <immintrin.h>
87 #if defined(__clang__)
89 # define _blsr_u32 __blsr_u32
92 # define _blsr_u64 __blsr_u64
95 # define _tzcnt_u32 __tzcnt_u32
98 # define _tzcnt_u64 __tzcnt_u64
102 #endif // CRYPTOPP_DOXYGEN_PROCESSING
104 #if CRYPTOPP_DOXYGEN_PROCESSING
116 # define SIZE_MAX ...
122 # if defined(__SIZE_MAX__)
123 # define SIZE_MAX __SIZE_MAX__
124 # elif defined(SIZE_T_MAX)
125 # define SIZE_MAX SIZE_T_MAX
126 # elif defined(__SIZE_TYPE__)
127 # define SIZE_MAX (~(__SIZE_TYPE__)0)
129 # define SIZE_MAX ((std::numeric_limits<size_t>::max)())
133 #endif // CRYPTOPP_DOXYGEN_PROCESSING
142 #if CRYPTOPP_DOXYGEN_PROCESSING
149 # define CRYPTOPP_COMPILE_ASSERT(expr) { ... }
150 #elif defined(CRYPTOPP_CXX17_STATIC_ASSERT)
151 # define CRYPTOPP_COMPILE_ASSERT(expr) static_assert(expr)
152 #else // CRYPTOPP_DOXYGEN_PROCESSING
156 static char dummy[2*b-1];
159 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
160 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
161 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
163 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
164 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
166 # if defined(__GNUC__) || defined(__clang__)
167 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
168 static CompileAssert<(assertion)> \
169 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) __attribute__ ((unused))
171 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
172 static CompileAssert<(assertion)> \
173 CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance)
174 # endif // GCC or Clang
177 #endif // CRYPTOPP_DOXYGEN_PROCESSING
181 #if CRYPTOPP_DOXYGEN_PROCESSING
189 # define COUNTOF(arr)
193 # if defined(_MSC_VER) && (_MSC_VER >= 1400)
194 # define COUNTOF(x) _countof(x)
196 # define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
199 #endif // CRYPTOPP_DOXYGEN_PROCESSING
209 #if !defined(CRYPTOPP_DOXYGEN_PROCESSING)
210 template <
class BASE1,
class BASE2>
211 class CRYPTOPP_NO_VTABLE TwoBases :
public BASE1,
public BASE2
215 template <
class BASE1,
class BASE2,
class BASE3>
216 class CRYPTOPP_NO_VTABLE ThreeBases :
public BASE1,
public BASE2,
public BASE3
219 #endif // CRYPTOPP_DOXYGEN_PROCESSING
240 #if CRYPTOPP_CXX11_DELETED_FUNCTIONS
256 T* operator()()
const {
return new T;}
259 #if CRYPTOPP_DOXYGEN_PROCESSING
268 #define MEMORY_BARRIER ...
270 #if defined(CRYPTOPP_CXX11_ATOMIC)
271 # define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
272 #elif (_MSC_VER >= 1400)
273 # pragma intrinsic(_ReadWriteBarrier)
274 # define MEMORY_BARRIER() _ReadWriteBarrier()
275 #elif defined(__INTEL_COMPILER)
276 # define MEMORY_BARRIER() __memory_barrier()
277 #elif defined(__GNUC__) || defined(__clang__)
278 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
280 # define MEMORY_BARRIER()
282 #endif // CRYPTOPP_DOXYGEN_PROCESSING
303 template <
class T,
class F = NewObject<T>,
int instance=0>
307 Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
310 CRYPTOPP_NOINLINE
const T &
Ref(CRYPTOPP_NOINLINE_DOTDOTDOT)
const;
324 template <
class T,
class F,
int instance>
327 #if defined(CRYPTOPP_CXX11_ATOMIC) && defined(CRYPTOPP_CXX11_SYNCHRONIZATION) && defined(CRYPTOPP_CXX11_STATIC_INIT)
328 static std::mutex s_mutex;
329 static std::atomic<T*> s_pObject;
331 T *p = s_pObject.load(std::memory_order_relaxed);
332 std::atomic_thread_fence(std::memory_order_acquire);
337 std::lock_guard<std::mutex> lock(s_mutex);
338 p = s_pObject.load(std::memory_order_relaxed);
339 std::atomic_thread_fence(std::memory_order_acquire);
344 T *newObject = m_objectFactory();
345 s_pObject.store(newObject, std::memory_order_relaxed);
346 std::atomic_thread_fence(std::memory_order_release);
351 T *p = s_pObject.m_p;
357 T *newObject = m_objectFactory();
367 s_pObject.m_p = newObject;
383 template <
typename PTR,
typename OFF>
384 inline PTR
PtrAdd(PTR pointer, OFF offset)
386 return pointer+
static_cast<ptrdiff_t
>(offset);
396 template <
typename PTR,
typename OFF>
397 inline PTR
PtrSub(PTR pointer, OFF offset)
399 return pointer-
static_cast<ptrdiff_t
>(offset);
411 template <
typename PTR>
412 inline ptrdiff_t
PtrDiff(
const PTR pointer1,
const PTR pointer2)
414 return pointer1 - pointer2;
426 template <
typename PTR>
429 return (
size_t)(
reinterpret_cast<uintptr_t
>(pointer1) -
reinterpret_cast<uintptr_t
>(pointer2));
444 return reinterpret_cast<byte*
>(&str[0]);
463 return reinterpret_cast<const byte*
>(&str[0]);
486 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
506 inline void memcpy_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
517 if (count > sizeInBytes)
520 #if CRYPTOPP_MSC_VERSION
521 # pragma warning(push)
522 # pragma warning(disable: 4996)
523 # if (CRYPTOPP_MSC_VERSION >= 1400)
524 # pragma warning(disable: 6386)
527 if (src != NULLPTR && dest != NULLPTR)
528 std::memcpy(dest, src, count);
529 #if CRYPTOPP_MSC_VERSION
530 # pragma warning(pop)
552 inline void memmove_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
561 if (count > sizeInBytes)
564 #if CRYPTOPP_MSC_VERSION
565 # pragma warning(push)
566 # pragma warning(disable: 4996)
567 # if (CRYPTOPP_MSC_VERSION >= 1400)
568 # pragma warning(disable: 6386)
571 if (src != NULLPTR && dest != NULLPTR)
572 std::memmove(dest, src, count);
573 #if CRYPTOPP_MSC_VERSION
574 # pragma warning(pop)
578 #if __BORLANDC__ >= 0x620
580 # define memcpy_s CryptoPP::memcpy_s
581 # define memmove_s CryptoPP::memmove_s
584 #endif // __STDC_WANT_SECURE_LIB__
601 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
618 inline void *
memset_z(
void *ptr,
int val,
size_t num)
621 #if CRYPTOPP_GCC_VERSION >= 30001 || CRYPTOPP_LLVM_CLANG_VERSION >= 20800 || \
622 CRYPTOPP_APPLE_CLANG_VERSION >= 30000
623 if (__builtin_constant_p(num) && num==0)
626 return std::memset(ptr, val, num);
635 template <
class T>
inline const T&
STDMIN(
const T& a,
const T& b)
637 return b < a ? b : a;
646 template <
class T>
inline const T&
STDMAX(
const T& a,
const T& b)
648 return a < b ? b : a;
651 #if CRYPTOPP_MSC_VERSION
652 # pragma warning(push)
653 # pragma warning(disable: 4389)
656 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
657 # pragma GCC diagnostic push
658 # pragma GCC diagnostic ignored "-Wsign-compare"
659 # pragma GCC diagnostic ignored "-Wstrict-overflow"
660 # if (CRYPTOPP_LLVM_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000)
661 # pragma GCC diagnostic ignored "-Wtautological-compare"
662 # elif (CRYPTOPP_GCC_VERSION >= 40300)
663 # pragma GCC diagnostic ignored "-Wtype-limits"
674 template <
class T1,
class T2>
inline const T1
UnsignedMin(
const T1& a,
const T2& b)
677 if (
sizeof(T1)<=
sizeof(T2))
678 return b < (T2)a ? (T1)b : a;
680 return (T1)b < a ? (T1)b : a;
689 template <
class T1,
class T2>
692 to =
static_cast<T2
>(from);
693 if (from != to || (from > 0) != (to > 0))
707 const unsigned int HIGH_BIT = (1U << 31);
708 const char CH = !!(base & HIGH_BIT) ?
'A' :
'a';
724 T digit = value % base;
725 result = char((digit < 10 ?
'0' : (CH - 10)) + digit) + result;
729 result =
"-" + result;
739 template <> CRYPTOPP_DLL
761 template <> CRYPTOPP_DLL
764 #if CRYPTOPP_MSC_VERSION
765 # pragma warning(pop)
768 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
769 # pragma GCC diagnostic pop
772 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
775 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
780 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
789 for (
unsigned int i=8*
sizeof(value)/2; i>0; i/=2)
791 return (
unsigned int)value&1;
804 unsigned int l=0, h=8*
sizeof(value);
807 unsigned int t = (l+h)/2;
827 unsigned int l=0, h=8*
sizeof(value);
831 unsigned int t = (l+h)/2;
854 return (
unsigned int)_tzcnt_u32(v);
855 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
856 return (
unsigned int)__builtin_ctz(v);
857 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
858 unsigned long result;
859 _BitScanForward(&result, v);
860 return static_cast<unsigned int>(result);
863 static const int MultiplyDeBruijnBitPosition[32] =
865 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
866 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
868 return MultiplyDeBruijnBitPosition[((
word32)((v & -v) * 0x077CB531U)) >> 27];
884 #if defined(__BMI__) && defined(__x86_64__)
885 return (
unsigned int)_tzcnt_u64(v);
886 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
887 return (
unsigned int)__builtin_ctzll(v);
888 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && (defined(_M_X64) || defined(_M_IA64))
889 unsigned long result;
890 _BitScanForward64(&result, v);
891 return static_cast<unsigned int>(result);
906 inline T
Crop(T value,
size_t bits)
908 if (bits < 8*
sizeof(value))
909 return T(value & ((T(1) << bits) - 1));
920 return ((bitCount+7)/(8));
968 CRYPTOPP_DLL
void CRYPTOPP_API xorbuf(
byte *output,
const byte *input,
const byte *mask,
size_t count);
992 return value > 0 && (value & (value-1)) == 0;
997 inline bool IsPowerOf2<word32>(
const word32 &value)
999 return value > 0 && _blsr_u32(value) == 0;
1002 # if defined(__x86_64__)
1004 inline bool IsPowerOf2<word64>(
const word64 &value)
1006 return value > 0 && _blsr_u64(value) == 0;
1008 # endif // __x86_64__
1026 return (std::numeric_limits<T>::min)();
1044 return (std::numeric_limits<T>::max)();
1049 #if defined(CRYPTOPP_WORD128_AVAILABLE)
1072 template <
class T1,
class T2>
1076 return T1((a > b) ? (a - b) : 0);
1089 template <
class T1,
class T2>
1093 return T1((a > b) ? (a - b) : 1);
1104 template <
class T1,
class T2>
1123 template <
class T1,
class T2>
1127 #if !defined(CRYPTOPP_APPLE_CLANG_VERSION) || (CRYPTOPP_APPLE_CLANG_VERSION >= 80000)
1153 template <
class T1,
class T2>
1157 #if !defined(CRYPTOPP_APPLE_CLANG_VERSION) || (CRYPTOPP_APPLE_CLANG_VERSION >= 80000)
1165 if (NumericLimitsMax<T1>() - m + 1 < n)
1181 #if defined(CRYPTOPP_CXX11_ALIGNOF)
1183 #elif (_MSC_VER >= 1300)
1184 return __alignof(T);
1185 #elif defined(__GNUC__)
1186 return __alignof__(T);
1187 #elif defined(__SUNPRO_CC)
1188 return __alignof__(T);
1189 #elif defined(__IBM_ALIGNOF__)
1190 return __alignof__(T);
1191 #elif CRYPTOPP_BOOL_SLOW_WORD64
1208 const uintptr_t x =
reinterpret_cast<uintptr_t
>(ptr);
1209 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2(x, alignment) == 0 : x % alignment == 0);
1225 #if (CRYPTOPP_LITTLE_ENDIAN)
1227 #elif (CRYPTOPP_BIG_ENDIAN)
1230 # error "Unable to determine endianness"
1245 return NativeByteOrder::ToEnum();
1282 unsigned int carry=1;
1283 while (carry && size != 0)
1286 carry = ! ++inout[size-1];
1303 unsigned int carry=1;
1304 while (carry && size != 0)
1307 carry = ! (output[size-1] = input[size-1] + 1);
1313 output[size-1] = input[size-1];
1339 ptrdiff_t t = size_t(c) * (a - b);
1358 volatile T *p = buf+n;
1363 #if !defined(CRYPTOPP_DISABLE_ASM) && \
1364 (_MSC_VER >= 1400 || defined(__GNUC__)) && \
1365 (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1374 volatile byte *p = buf;
1376 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1378 __stosb(
reinterpret_cast<byte *
>(
reinterpret_cast<size_t>(p)), 0, n);
1389 volatile word16 *p = buf;
1391 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1393 __stosw(
reinterpret_cast<word16 *
>(
reinterpret_cast<size_t>(p)), 0, n);
1404 volatile word32 *p = buf;
1406 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1408 __stosd(
reinterpret_cast<unsigned long *
>(
reinterpret_cast<size_t>(p)), 0, n);
1419 #if CRYPTOPP_BOOL_X64
1420 volatile word64 *p = buf;
1422 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1424 __stosq(
const_cast<word64 *
>(p), 0, n);
1431 #endif // CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86
1433 #if !defined(CRYPTOPP_DISABLE_ASM) && (_MSC_VER >= 1700) && defined(_M_ARM)
1436 char *p =
reinterpret_cast<char*
>(buf+n);
1438 __iso_volatile_store8(--p, 0);
1443 short *p =
reinterpret_cast<short*
>(buf+n);
1445 __iso_volatile_store16(--p, 0);
1450 int *p =
reinterpret_cast<int*
>(buf+n);
1452 __iso_volatile_store32(--p, 0);
1457 __int64 *p =
reinterpret_cast<__int64*
>(buf+n);
1459 __iso_volatile_store64(--p, 0);
1472 if (
sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1474 else if (
sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1476 else if (
sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1479 SecureWipeBuffer(
reinterpret_cast<byte *
>(
static_cast<void *
>(buf)), n *
sizeof(T));
1533 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1534 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1536 return T((x<<R)|(x>>(-R&MASK)));
1559 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1560 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1562 return T((x >> R)|(x<<(-R&MASK)));
1584 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1585 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1587 return T((x<<y)|(x>>(-y&MASK)));
1609 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1610 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1612 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)));
1664 template <
class T>
inline T
rotlMod(T x,
unsigned int y)
1666 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1667 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1668 return T((x<<(y&MASK))|(x>>(-y&MASK)));
1680 template <
class T>
inline T
rotrMod(T x,
unsigned int y)
1682 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1683 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1684 return T((x>>(y&MASK))|(x<<(-y&MASK)));
1698 template<>
inline word32 rotlFixed<word32>(
word32 x,
unsigned int y)
1702 return y ? _lrotl(x,
static_cast<byte>(y)) : x;
1714 template<>
inline word32 rotrFixed<word32>(
word32 x,
unsigned int y)
1718 return y ? _lrotr(x,
static_cast<byte>(y)) : x;
1730 template<>
inline word32 rotlVariable<word32>(
word32 x,
unsigned int y)
1733 return _lrotl(x,
static_cast<byte>(y));
1745 template<>
inline word32 rotrVariable<word32>(
word32 x,
unsigned int y)
1748 return _lrotr(x,
static_cast<byte>(y));
1759 template<>
inline word32 rotlMod<word32>(
word32 x,
unsigned int y)
1762 return _lrotl(x,
static_cast<byte>(y));
1773 template<>
inline word32 rotrMod<word32>(
word32 x,
unsigned int y)
1776 return _lrotr(x,
static_cast<byte>(y));
1779 #endif // #ifdef _MSC_VER
1781 #if (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1793 template<>
inline word64 rotlFixed<word64>(
word64 x,
unsigned int y)
1797 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1809 template<>
inline word64 rotrFixed<word64>(
word64 x,
unsigned int y)
1813 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1825 template<>
inline word64 rotlVariable<word64>(
word64 x,
unsigned int y)
1828 return _rotl64(x,
static_cast<byte>(y));
1840 template<>
inline word64 rotrVariable<word64>(
word64 x,
unsigned int y)
1843 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1854 template<>
inline word64 rotlMod<word64>(
word64 x,
unsigned int y)
1857 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1868 template<>
inline word64 rotrMod<word64>(
word64 x,
unsigned int y)
1871 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1874 #endif // #if _MSC_VER >= 1310
1876 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
1878 template<>
inline word16 rotlFixed<word16>(
word16 x,
unsigned int y)
1881 return _rotl16(x,
static_cast<byte>(y));
1884 template<>
inline word16 rotrFixed<word16>(
word16 x,
unsigned int y)
1887 return _rotr16(x,
static_cast<byte>(y));
1890 template<>
inline word16 rotlVariable<word16>(
word16 x,
unsigned int y)
1892 return _rotl16(x,
static_cast<byte>(y));
1895 template<>
inline word16 rotrVariable<word16>(
word16 x,
unsigned int y)
1897 return _rotr16(x,
static_cast<byte>(y));
1900 template<>
inline word16 rotlMod<word16>(
word16 x,
unsigned int y)
1902 return _rotl16(x,
static_cast<byte>(y));
1905 template<>
inline word16 rotrMod<word16>(
word16 x,
unsigned int y)
1907 return _rotr16(x,
static_cast<byte>(y));
1910 template<>
inline byte rotlFixed<byte>(
byte x,
unsigned int y)
1913 return _rotl8(x,
static_cast<byte>(y));
1916 template<>
inline byte rotrFixed<byte>(
byte x,
unsigned int y)
1919 return _rotr8(x,
static_cast<byte>(y));
1922 template<>
inline byte rotlVariable<byte>(
byte x,
unsigned int y)
1924 return _rotl8(x,
static_cast<byte>(y));
1927 template<>
inline byte rotrVariable<byte>(
byte x,
unsigned int y)
1929 return _rotr8(x,
static_cast<byte>(y));
1932 template<>
inline byte rotlMod<byte>(
byte x,
unsigned int y)
1934 return _rotl8(x,
static_cast<byte>(y));
1937 template<>
inline byte rotrMod<byte>(
byte x,
unsigned int y)
1939 return _rotr8(x,
static_cast<byte>(y));
1942 #endif // #if _MSC_VER >= 1400
1944 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1946 template<>
inline word32 rotlFixed<word32>(
word32 x,
unsigned int y)
1949 return y ? __rlwinm(x,y,0,31) : x;
1952 template<>
inline word32 rotrFixed<word32>(
word32 x,
unsigned int y)
1955 return y ? __rlwinm(x,32-y,0,31) : x;
1958 template<>
inline word32 rotlVariable<word32>(
word32 x,
unsigned int y)
1961 return (__rlwnm(x,y,0,31));
1964 template<>
inline word32 rotrVariable<word32>(
word32 x,
unsigned int y)
1967 return (__rlwnm(x,32-y,0,31));
1970 template<>
inline word32 rotlMod<word32>(
word32 x,
unsigned int y)
1972 return (__rlwnm(x,y,0,31));
1975 template<>
inline word32 rotrMod<word32>(
word32 x,
unsigned int y)
1977 return (__rlwnm(x,32-y,0,31));
1980 #endif // __MWERKS__ && TARGET_CPU_PPC
1992 return GETBYTE(value, index);
1994 return GETBYTE(value,
sizeof(T)-index-1);
2012 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2013 return bswap_16(value);
2014 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2015 return _byteswap_ushort(value);
2027 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2028 return bswap_32(value);
2029 #elif defined(CRYPTOPP_ARM_BYTEREV_AVAILABLE)
2031 __asm__ (
"rev %0, %1" :
"=r" (rvalue) :
"r" (value));
2033 #elif defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
2034 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2036 #elif defined(__MWERKS__) && TARGET_CPU_PPC
2037 return (
word32)__lwbrx(&value,0);
2038 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2039 return _byteswap_ulong(value);
2040 #elif CRYPTOPP_FAST_ROTATE(32) && !defined(__xlC__)
2045 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
2056 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2057 return bswap_64(value);
2058 #elif defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
2059 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2061 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2062 return _byteswap_uint64(value);
2063 #elif CRYPTOPP_BOOL_SLOW_WORD64
2066 value = ((value &
W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value &
W64LIT(0x00FF00FF00FF00FF)) << 8);
2067 value = ((value &
W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value &
W64LIT(0x0000FFFF0000FFFF)) << 16);
2077 value =
byte((value & 0xAA) >> 1) |
byte((value & 0x55) << 1);
2078 value =
byte((value & 0xCC) >> 2) |
byte((value & 0x33) << 2);
2087 #if defined(CRYPTOPP_ARM_BITREV_AVAILABLE)
2090 __asm__ (
"rbit %0, %1" :
"=r" (rvalue) :
"r" (value));
2091 return word16(rvalue >> 16);
2094 value =
word16((value & 0xAAAA) >> 1) |
word16((value & 0x5555) << 1);
2095 value =
word16((value & 0xCCCC) >> 2) |
word16((value & 0x3333) << 2);
2096 value =
word16((value & 0xF0F0) >> 4) |
word16((value & 0x0F0F) << 4);
2106 #if defined(CRYPTOPP_ARM_BITREV_AVAILABLE)
2109 __asm__ (
"rbit %0, %1" :
"=r" (rvalue) :
"r" (value));
2113 value =
word32((value & 0xAAAAAAAA) >> 1) |
word32((value & 0x55555555) << 1);
2114 value =
word32((value & 0xCCCCCCCC) >> 2) |
word32((value & 0x33333333) << 2);
2115 value =
word32((value & 0xF0F0F0F0) >> 4) |
word32((value & 0x0F0F0F0F) << 4);
2125 #if CRYPTOPP_BOOL_SLOW_WORD64
2146 else if (
sizeof(T) == 2)
2148 else if (
sizeof(T) == 4)
2150 else if (
sizeof(T) == 8)
2215 size_t count = byteCount/
sizeof(T);
2216 for (
size_t i=0; i<count; i++)
2239 memcpy_s(out, byteCount, in, byteCount);
2243 inline void GetUserKey(
ByteOrder order, T *out,
size_t outlen,
const byte *in,
size_t inlen)
2245 const size_t U =
sizeof(T);
2247 memcpy_s(out, outlen*U, in, inlen);
2248 memset_z((
byte *)out+inlen, 0, outlen*U-inlen);
2252 inline byte UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const byte *)
2254 CRYPTOPP_UNUSED(order);
2261 ? block[1] | (block[0] << 8)
2262 : block[0] | (block[1] << 8);
2277 (
word64(block[6]) << 8) |
2278 (
word64(block[5]) << 16) |
2279 (
word64(block[4]) << 24) |
2280 (
word64(block[3]) << 32) |
2281 (
word64(block[2]) << 40) |
2282 (
word64(block[1]) << 48) |
2283 (
word64(block[0]) << 56))
2286 (
word64(block[1]) << 8) |
2287 (
word64(block[2]) << 16) |
2288 (
word64(block[3]) << 24) |
2289 (
word64(block[4]) << 32) |
2290 (
word64(block[5]) << 40) |
2291 (
word64(block[6]) << 48) |
2292 (
word64(block[7]) << 56));
2295 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
byte value,
const byte *xorBlock)
2297 CRYPTOPP_UNUSED(order);
2298 block[0] =
static_cast<byte>(xorBlock ? (value ^ xorBlock[0]) : value);
2301 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
word16 value,
const byte *xorBlock)
2307 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2308 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2312 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2313 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2320 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2321 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2325 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2326 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2331 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
word32 value,
const byte *xorBlock)
2337 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2338 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2339 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2340 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2344 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2345 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2346 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2347 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2354 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2355 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2356 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2357 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2361 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2362 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2363 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2364 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2369 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
word64 value,
const byte *xorBlock)
2375 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2376 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2377 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2378 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2379 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2380 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2381 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2382 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2386 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2387 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2388 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2389 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2390 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2391 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2392 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2393 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2400 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2401 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2402 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2403 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2404 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2405 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2406 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2407 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2411 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2412 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2413 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2414 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2415 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2416 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2417 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2418 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2442 CRYPTOPP_UNUSED(assumeAligned);
2445 if (block != NULLPTR) {std::memcpy(&temp, block,
sizeof(T));}
2468 result = GetWord<T>(assumeAligned, order, block);
2482 inline void PutWord(
bool assumeAligned,
ByteOrder order,
byte *block, T value,
const byte *xorBlock = NULLPTR)
2484 CRYPTOPP_UNUSED(assumeAligned);
2488 if (xorBlock != NULLPTR) {std::memcpy(&t2, xorBlock,
sizeof(T)); t1 ^= t2;}
2489 if (block != NULLPTR) {std::memcpy(block, &t1,
sizeof(T));}
2507 template <
class T,
class B,
bool A=false>
2514 : m_block((const
byte *)block) {}
2524 x = GetWord<T>(A, B::ToEnum(), m_block);
2525 m_block +=
sizeof(T);
2530 const byte *m_block;
2548 template <
class T,
class B,
bool A=false>
2556 : m_xorBlock((const
byte *)xorBlock), m_block((
byte *)block) {}
2565 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2566 m_block +=
sizeof(T);
2568 m_xorBlock +=
sizeof(T);
2573 const byte *m_xorBlock;
2585 template <
class T,
class B,
bool GA=false,
bool PA=false>
2604 return std::string((
char *)&value,
sizeof(value));
2643 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2655 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2673 return value >> bits;
2684 return value << bits;
2696 template <
unsigned int bits,
class T>
2699 return SafeShifter<(bits>=(8*
sizeof(T)))>::RightShift(value, bits);
2710 template <
unsigned int bits,
class T>
2713 return SafeShifter<(bits>=(8*
sizeof(T)))>::LeftShift(value, bits);
2723 template<
typename InputIt,
typename T>
2724 inline InputIt
FindIfNot(InputIt first, InputIt last,
const T &value) {
2725 #ifdef CRYPTOPP_CXX11_LAMBDA
2726 return std::find_if(first, last, [&value](
const T &o) {
2730 return std::find_if(first, last, std::bind2nd(std::not_equal_to<T>(), value));
2736 #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);}
2737 #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);}
2738 #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);}
2739 #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);}
2740 #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);}
2741 #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);}
2742 #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);}
2743 #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);}
2744 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
2748 #if (CRYPTOPP_MSC_VERSION)
2749 # pragma warning(pop)
2752 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
2753 # pragma GCC diagnostic pop