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));
930 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
940 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
950 return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
959 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(
byte *buf,
const byte *mask,
size_t count);
968 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(
byte *output,
const byte *input,
const byte *mask,
size_t count);
981 CRYPTOPP_DLL
bool CRYPTOPP_API
VerifyBufsEqual(
const byte *buf1,
const byte *buf2,
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)
1058 return (
static_cast<word128
>(LWORD_MAX) << 64U) | LWORD_MAX;
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)
1182 #if defined(CRYPTOPP_CXX11_ALIGNOF)
1184 #elif (_MSC_VER >= 1300)
1185 return __alignof(T);
1186 #elif defined(__GNUC__)
1187 return __alignof__(T);
1188 #elif defined(__SUNPRO_CC)
1189 return __alignof__(T);
1190 #elif defined(__IBM_ALIGNOF__)
1191 return __alignof__(T);
1192 #elif CRYPTOPP_BOOL_SLOW_WORD64
1209 const uintptr_t x =
reinterpret_cast<uintptr_t
>(ptr);
1210 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2(x, alignment) == 0 : x % alignment == 0);
1226 #if (CRYPTOPP_LITTLE_ENDIAN)
1228 #elif (CRYPTOPP_BIG_ENDIAN)
1231 # error "Unable to determine endianness"
1246 return NativeByteOrder::ToEnum();
1283 unsigned int carry=1;
1284 while (carry && size != 0)
1287 carry = ! ++inout[size-1];
1304 unsigned int carry=1;
1305 while (carry && size != 0)
1308 carry = ! (output[size-1] = input[size-1] + 1);
1314 output[size-1] = input[size-1];
1340 ptrdiff_t t = size_t(c) * (a - b);
1359 volatile T *p = buf+n;
1364 #if !defined(CRYPTOPP_DISABLE_ASM) && \
1365 (_MSC_VER >= 1400 || defined(__GNUC__)) && \
1366 (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1375 volatile byte *p = buf;
1377 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1379 __stosb(
reinterpret_cast<byte *
>(
reinterpret_cast<size_t>(p)), 0, n);
1390 volatile word16 *p = buf;
1392 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1394 __stosw(
reinterpret_cast<word16 *
>(
reinterpret_cast<size_t>(p)), 0, n);
1405 volatile word32 *p = buf;
1407 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1409 __stosd(
reinterpret_cast<unsigned long *
>(
reinterpret_cast<size_t>(p)), 0, n);
1420 #if CRYPTOPP_BOOL_X64
1421 volatile word64 *p = buf;
1423 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1425 __stosq(
const_cast<word64 *
>(p), 0, n);
1432 #endif // CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86
1434 #if !defined(CRYPTOPP_DISABLE_ASM) && (_MSC_VER >= 1700) && defined(_M_ARM)
1437 char *p =
reinterpret_cast<char*
>(buf+n);
1439 __iso_volatile_store8(--p, 0);
1444 short *p =
reinterpret_cast<short*
>(buf+n);
1446 __iso_volatile_store16(--p, 0);
1451 int *p =
reinterpret_cast<int*
>(buf+n);
1453 __iso_volatile_store32(--p, 0);
1458 __int64 *p =
reinterpret_cast<__int64*
>(buf+n);
1460 __iso_volatile_store64(--p, 0);
1473 if (
sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1474 SecureWipeBuffer(
reinterpret_cast<word64 *
>(
static_cast<void *
>(buf)), n * (
sizeof(T)/8));
1475 else if (
sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1476 SecureWipeBuffer(
reinterpret_cast<word32 *
>(
static_cast<void *
>(buf)), n * (
sizeof(T)/4));
1477 else if (
sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1478 SecureWipeBuffer(
reinterpret_cast<word16 *
>(
static_cast<void *
>(buf)), n * (
sizeof(T)/2));
1480 SecureWipeBuffer(
reinterpret_cast<byte *
>(
static_cast<void *
>(buf)), n *
sizeof(T));
1495 std::string
StringNarrow(
const wchar_t *str,
bool throwOnError =
true);
1509 std::wstring
StringWiden(
const char *str,
bool throwOnError =
true);
1534 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1535 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1537 return T((x<<R)|(x>>(-R&MASK)));
1560 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1561 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1563 return T((x >> R)|(x<<(-R&MASK)));
1585 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1586 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1588 return T((x<<y)|(x>>(-y&MASK)));
1610 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1611 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1613 return T((x >> y)|(x<<(-y&MASK)));
1630 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1631 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1633 return T((x<<y)|(x>>(-y&MASK)));
1650 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1651 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1653 return T((x>>y)|(x<<(-y&MASK)));
1665 template <
class T>
inline T
rotlMod(T x,
unsigned int y)
1667 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1668 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1669 return T((x<<(y&MASK))|(x>>(-y&MASK)));
1681 template <
class T>
inline T
rotrMod(T x,
unsigned int y)
1683 CRYPTOPP_CONSTANT(THIS_SIZE =
sizeof(T)*8);
1684 CRYPTOPP_CONSTANT(MASK = THIS_SIZE-1);
1685 return T((x>>(y&MASK))|(x<<(-y&MASK)));
1699 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1703 return y ? _lrotl(x,
static_cast<byte>(y)) : x;
1715 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1719 return y ? _lrotr(x,
static_cast<byte>(y)) : x;
1731 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1734 return _lrotl(x,
static_cast<byte>(y));
1746 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1749 return _lrotr(x,
static_cast<byte>(y));
1760 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1763 return _lrotl(x,
static_cast<byte>(y));
1774 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1777 return _lrotr(x,
static_cast<byte>(y));
1780 #endif // #ifdef _MSC_VER
1782 #if (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
1794 template<>
inline word64 rotlFixed<word64>(word64 x,
unsigned int y)
1798 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1810 template<>
inline word64 rotrFixed<word64>(word64 x,
unsigned int y)
1814 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1826 template<>
inline word64 rotlVariable<word64>(word64 x,
unsigned int y)
1829 return _rotl64(x,
static_cast<byte>(y));
1841 template<>
inline word64 rotrVariable<word64>(word64 x,
unsigned int y)
1844 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1855 template<>
inline word64 rotlMod<word64>(word64 x,
unsigned int y)
1858 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1869 template<>
inline word64 rotrMod<word64>(word64 x,
unsigned int y)
1872 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1875 #endif // #if _MSC_VER >= 1310
1877 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
1879 template<>
inline word16 rotlFixed<word16>(word16 x,
unsigned int y)
1882 return _rotl16(x,
static_cast<byte>(y));
1885 template<>
inline word16 rotrFixed<word16>(word16 x,
unsigned int y)
1888 return _rotr16(x,
static_cast<byte>(y));
1891 template<>
inline word16 rotlVariable<word16>(word16 x,
unsigned int y)
1893 return _rotl16(x,
static_cast<byte>(y));
1896 template<>
inline word16 rotrVariable<word16>(word16 x,
unsigned int y)
1898 return _rotr16(x,
static_cast<byte>(y));
1901 template<>
inline word16 rotlMod<word16>(word16 x,
unsigned int y)
1903 return _rotl16(x,
static_cast<byte>(y));
1906 template<>
inline word16 rotrMod<word16>(word16 x,
unsigned int y)
1908 return _rotr16(x,
static_cast<byte>(y));
1911 template<>
inline byte rotlFixed<byte>(
byte x,
unsigned int y)
1914 return _rotl8(x,
static_cast<byte>(y));
1917 template<>
inline byte rotrFixed<byte>(
byte x,
unsigned int y)
1920 return _rotr8(x,
static_cast<byte>(y));
1923 template<>
inline byte rotlVariable<byte>(
byte x,
unsigned int y)
1925 return _rotl8(x,
static_cast<byte>(y));
1928 template<>
inline byte rotrVariable<byte>(
byte x,
unsigned int y)
1930 return _rotr8(x,
static_cast<byte>(y));
1933 template<>
inline byte rotlMod<byte>(
byte x,
unsigned int y)
1935 return _rotl8(x,
static_cast<byte>(y));
1938 template<>
inline byte rotrMod<byte>(
byte x,
unsigned int y)
1940 return _rotr8(x,
static_cast<byte>(y));
1943 #endif // #if _MSC_VER >= 1400
1945 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1947 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1950 return y ? __rlwinm(x,y,0,31) : x;
1953 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1956 return y ? __rlwinm(x,32-y,0,31) : x;
1959 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1962 return (__rlwnm(x,y,0,31));
1965 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1968 return (__rlwnm(x,32-y,0,31));
1971 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1973 return (__rlwnm(x,y,0,31));
1976 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1978 return (__rlwnm(x,32-y,0,31));
1981 #endif // __MWERKS__ && TARGET_CPU_PPC
1993 return GETBYTE(value, index);
1995 return GETBYTE(value,
sizeof(T)-index-1);
2013 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2014 return bswap_16(value);
2015 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2016 return _byteswap_ushort(value);
2028 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2029 return bswap_32(value);
2030 #elif defined(CRYPTOPP_ARM_BYTEREV_AVAILABLE)
2032 __asm__ (
"rev %0, %1" :
"=r" (rvalue) :
"r" (value));
2034 #elif defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
2035 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2037 #elif defined(__MWERKS__) && TARGET_CPU_PPC
2038 return (word32)__lwbrx(&value,0);
2039 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2040 return _byteswap_ulong(value);
2041 #elif CRYPTOPP_FAST_ROTATE(32) && !defined(__xlC__)
2046 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
2057 #if defined(CRYPTOPP_BYTESWAP_AVAILABLE)
2058 return bswap_64(value);
2059 #elif defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
2060 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
2062 #elif (_MSC_VER >= 1400) || (defined(_MSC_VER) && !defined(_DLL))
2063 return _byteswap_uint64(value);
2064 #elif CRYPTOPP_BOOL_SLOW_WORD64
2067 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
2068 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
2078 value = byte((value & 0xAA) >> 1) | byte((value & 0x55) << 1);
2079 value = byte((value & 0xCC) >> 2) | byte((value & 0x33) << 2);
2088 #if defined(CRYPTOPP_ARM_BITREV_AVAILABLE)
2091 __asm__ (
"rbit %0, %1" :
"=r" (rvalue) :
"r" (value));
2092 return word16(rvalue >> 16);
2095 value = word16((value & 0xAAAA) >> 1) | word16((value & 0x5555) << 1);
2096 value = word16((value & 0xCCCC) >> 2) | word16((value & 0x3333) << 2);
2097 value = word16((value & 0xF0F0) >> 4) | word16((value & 0x0F0F) << 4);
2107 #if defined(CRYPTOPP_ARM_BITREV_AVAILABLE)
2110 __asm__ (
"rbit %0, %1" :
"=r" (rvalue) :
"r" (value));
2114 value = word32((value & 0xAAAAAAAA) >> 1) | word32((value & 0x55555555) << 1);
2115 value = word32((value & 0xCCCCCCCC) >> 2) | word32((value & 0x33333333) << 2);
2116 value = word32((value & 0xF0F0F0F0) >> 4) | word32((value & 0x0F0F0F0F) << 4);
2126 #if CRYPTOPP_BOOL_SLOW_WORD64
2129 value = word64((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | word64((value & W64LIT(0x5555555555555555)) << 1);
2130 value = word64((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | word64((value & W64LIT(0x3333333333333333)) << 2);
2131 value = word64((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | word64((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
2147 else if (
sizeof(T) == 2)
2149 else if (
sizeof(T) == 4)
2151 else if (
sizeof(T) == 8)
2216 size_t count = byteCount/
sizeof(T);
2217 for (
size_t i=0; i<count; i++)
2240 memcpy_s(out, byteCount, in, byteCount);
2244 inline void GetUserKey(
ByteOrder order, T *out,
size_t outlen,
const byte *in,
size_t inlen)
2246 const size_t U =
sizeof(T);
2248 memcpy_s(out, outlen*U, in, inlen);
2249 memset_z((
byte *)out+inlen, 0, outlen*U-inlen);
2253 inline byte UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const byte *)
2255 CRYPTOPP_UNUSED(order);
2259 inline word16 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word16 *)
2262 ? block[1] | (block[0] << 8)
2263 : block[0] | (block[1] << 8);
2266 inline word32 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word32 *)
2269 ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
2270 : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
2273 inline word64 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word64 *)
2278 (word64(block[6]) << 8) |
2279 (word64(block[5]) << 16) |
2280 (word64(block[4]) << 24) |
2281 (word64(block[3]) << 32) |
2282 (word64(block[2]) << 40) |
2283 (word64(block[1]) << 48) |
2284 (word64(block[0]) << 56))
2287 (word64(block[1]) << 8) |
2288 (word64(block[2]) << 16) |
2289 (word64(block[3]) << 24) |
2290 (word64(block[4]) << 32) |
2291 (word64(block[5]) << 40) |
2292 (word64(block[6]) << 48) |
2293 (word64(block[7]) << 56));
2296 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
byte value,
const byte *xorBlock)
2298 CRYPTOPP_UNUSED(order);
2299 block[0] =
static_cast<byte>(xorBlock ? (value ^ xorBlock[0]) : value);
2302 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word16 value,
const byte *xorBlock)
2308 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2309 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2313 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2314 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2321 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2322 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2326 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2327 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2332 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word32 value,
const byte *xorBlock)
2338 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2339 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2340 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2341 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2345 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2346 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2347 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2348 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2355 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2356 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2357 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2358 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2362 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2363 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2364 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2365 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2370 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word64 value,
const byte *xorBlock)
2376 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2377 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2378 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2379 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2380 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2381 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2382 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2383 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2387 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2388 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2389 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2390 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2391 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2392 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2393 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2394 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2401 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2402 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2403 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2404 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2405 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2406 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2407 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2408 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2412 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2413 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2414 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2415 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2416 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2417 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2418 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2419 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2443 CRYPTOPP_UNUSED(assumeAligned);
2446 if (block != NULLPTR) {std::memcpy(&temp, block,
sizeof(T));}
2469 result = GetWord<T>(assumeAligned, order, block);
2483 inline void PutWord(
bool assumeAligned,
ByteOrder order,
byte *block, T value,
const byte *xorBlock = NULLPTR)
2485 CRYPTOPP_UNUSED(assumeAligned);
2489 if (xorBlock != NULLPTR) {std::memcpy(&t2, xorBlock,
sizeof(T)); t1 ^= t2;}
2490 if (block != NULLPTR) {std::memcpy(block, &t1,
sizeof(T));}
2508 template <
class T,
class B,
bool A=false>
2515 : m_block((const byte *)block) {}
2525 x = GetWord<T>(A, B::ToEnum(), m_block);
2526 m_block +=
sizeof(T);
2531 const byte *m_block;
2549 template <
class T,
class B,
bool A=false>
2557 : m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
2566 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2567 m_block +=
sizeof(T);
2569 m_xorBlock +=
sizeof(T);
2574 const byte *m_xorBlock;
2586 template <
class T,
class B,
bool GA=false,
bool PA=false>
2605 return std::string((
char *)&value,
sizeof(value));
2644 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2656 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2674 return value >> bits;
2685 return value << bits;
2697 template <
unsigned int bits,
class T>
2700 return SafeShifter<(bits>=(8*
sizeof(T)))>::RightShift(value, bits);
2711 template <
unsigned int bits,
class T>
2714 return SafeShifter<(bits>=(8*
sizeof(T)))>::LeftShift(value, bits);
2724 template<
typename InputIt,
typename T>
2725 inline InputIt
FindIfNot(InputIt first, InputIt last,
const T &value) {
2726 #ifdef CRYPTOPP_CXX11_LAMBDA
2727 return std::find_if(first, last, [&value](
const T &o) {
2731 return std::find_if(first, last, std::bind2nd(std::not_equal_to<T>(), value));
2737 #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);}
2738 #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);}
2739 #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);}
2740 #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);}
2741 #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);}
2742 #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);}
2743 #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);}
2744 #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);}
2745 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
2749 #if (CRYPTOPP_MSC_VERSION)
2750 # pragma warning(pop)
2753 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
2754 # pragma GCC diagnostic pop