7 #ifndef CRYPTOPP_MISC_H
8 #define CRYPTOPP_MISC_H
12 #if !CRYPTOPP_DOXYGEN_PROCESSING
14 #if CRYPTOPP_MSC_VERSION
15 # pragma warning(push)
16 # pragma warning(disable: 4146 4514)
17 # if (CRYPTOPP_MSC_VERSION >= 1400)
18 # pragma warning(disable: 6326)
29 #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
30 #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
31 #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3
32 #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4
34 #undef _interlockedbittestandset
35 #undef _interlockedbittestandreset
36 #undef _interlockedbittestandset64
37 #undef _interlockedbittestandreset64
38 #define CRYPTOPP_FAST_ROTATE(x) 1
39 #elif _MSC_VER >= 1300
40 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
42 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
44 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
45 (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
46 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
47 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions
48 #define CRYPTOPP_FAST_ROTATE(x) 1
50 #define CRYPTOPP_FAST_ROTATE(x) 0
58 #if defined(__GNUC__) && defined(__linux__)
59 #define CRYPTOPP_BYTESWAP_AVAILABLE
63 #if defined(__GNUC__) && defined(__BMI__)
64 # include <immintrin.h>
65 # if defined(__clang__)
67 # define _tzcnt_u32(x) __tzcnt_u32(x)
70 # define _blsr_u32(x) __blsr_u32(x)
74 # define _tzcnt_u64(x) __tzcnt_u64(x)
77 # define _blsr_u64(x) __blsr_u64(x)
81 #endif // GNUC and BMI
83 #endif // CRYPTOPP_DOXYGEN_PROCESSING
85 #if CRYPTOPP_DOXYGEN_PROCESSING
101 # if defined(__SIZE_MAX__) && (__SIZE_MAX__ > 0)
102 # define SIZE_MAX __SIZE_MAX__
103 # elif defined(SIZE_T_MAX) && (SIZE_T_MAX > 0)
104 # define SIZE_MAX SIZE_T_MAX
106 # define SIZE_MAX ((std::numeric_limits<size_t>::max)())
110 #endif // CRYPTOPP_DOXYGEN_PROCESSING
119 #if CRYPTOPP_DOXYGEN_PROCESSING
123 #define CRYPTOPP_COMPILE_ASSERT(expr) ...
124 #else // CRYPTOPP_DOXYGEN_PROCESSING
128 static char dummy[2*b-1];
132 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
133 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
134 #define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
136 # if defined(__GNUC__)
137 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
138 static CompileAssert<(assertion)> \
139 CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance) __attribute__ ((unused))
141 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
142 static CompileAssert<(assertion)> \
143 CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance)
146 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
147 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
149 #endif // CRYPTOPP_DOXYGEN_PROCESSING
153 #if CRYPTOPP_DOXYGEN_PROCESSING
161 # define COUNTOF(arr)
165 # if defined(_MSC_VER) && (_MSC_VER >= 1400)
166 # define COUNTOF(x) _countof(x)
168 # define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
171 #endif // CRYPTOPP_DOXYGEN_PROCESSING
181 #if !CRYPTOPP_DOXYGEN_PROCESSING
182 template <
class BASE1,
class BASE2>
183 class CRYPTOPP_NO_VTABLE TwoBases :
public BASE1,
public BASE2
187 template <
class BASE1,
class BASE2,
class BASE3>
188 class CRYPTOPP_NO_VTABLE ThreeBases :
public BASE1,
public BASE2,
public BASE3
191 #endif // CRYPTOPP_DOXYGEN_PROCESSING
225 T* operator()()
const {
return new T;}
228 #if CRYPTOPP_DOXYGEN_PROCESSING
237 #define MEMORY_BARRIER ...
239 #if defined(CRYPTOPP_CXX11_ATOMICS)
240 # define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
241 #elif (_MSC_VER >= 1400)
242 # pragma intrinsic(_ReadWriteBarrier)
243 # define MEMORY_BARRIER() _ReadWriteBarrier()
244 #elif defined(__INTEL_COMPILER)
245 # define MEMORY_BARRIER() __memory_barrier()
246 #elif defined(__GNUC__) || defined(__clang__)
247 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
249 # define MEMORY_BARRIER()
251 #endif // CRYPTOPP_DOXYGEN_PROCESSING
263 template <
class T,
class F = NewObject<T>,
int instance=0>
267 Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
270 CRYPTOPP_NOINLINE
const T &
Ref(CRYPTOPP_NOINLINE_DOTDOTDOT)
const;
280 #if defined(CRYPTOPP_CXX11_ATOMICS) && defined(CRYPTOPP_CXX11_SYNCHRONIZATION)
281 template <
class T,
class F,
int instance>
284 static std::mutex s_mutex;
285 static std::atomic<T*> s_pObject;
287 T *p = s_pObject.load(std::memory_order_relaxed);
288 std::atomic_thread_fence(std::memory_order_acquire);
293 std::lock_guard<std::mutex> lock(s_mutex);
294 p = s_pObject.load(std::memory_order_relaxed);
295 std::atomic_thread_fence(std::memory_order_acquire);
300 T *newObject = m_objectFactory();
301 s_pObject.store(newObject, std::memory_order_relaxed);
302 std::atomic_thread_fence(std::memory_order_release);
307 template <
class T,
class F,
int instance>
311 T *p = s_pObject.m_p;
317 T *newObject = m_objectFactory();
327 s_pObject.m_p = newObject;
336 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
356 inline void memcpy_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
361 assert(dest != NULL); assert(src != NULL);
363 assert(sizeInBytes >= count);
364 if (count > sizeInBytes)
367 #if CRYPTOPP_MSC_VERSION
368 # pragma warning(push)
369 # pragma warning(disable: 4996)
370 # if (CRYPTOPP_MSC_VERSION >= 1400)
371 # pragma warning(disable: 6386)
374 memcpy(dest, src, count);
375 #if CRYPTOPP_MSC_VERSION
376 # pragma warning(pop)
398 inline void memmove_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
403 assert(dest != NULL); assert(src != NULL);
405 assert(sizeInBytes >= count);
406 if (count > sizeInBytes)
409 #if CRYPTOPP_MSC_VERSION
410 # pragma warning(push)
411 # pragma warning(disable: 4996)
412 # if (CRYPTOPP_MSC_VERSION >= 1400)
413 # pragma warning(disable: 6386)
416 memmove(dest, src, count);
417 #if CRYPTOPP_MSC_VERSION
418 # pragma warning(pop)
422 #if __BORLANDC__ >= 0x620
424 # define memcpy_s CryptoPP::memcpy_s
425 # define memmove_s CryptoPP::memmove_s
428 #endif // __STDC_WANT_SECURE_LIB__
451 inline void *
memset_z(
void *ptr,
int value,
size_t num)
454 #if CRYPTOPP_GCC_VERSION >= 30001
455 if (__builtin_constant_p(num) && num==0)
458 volatile void* x = memset(ptr, value, num);
459 return const_cast<void*
>(x);
467 template <
class T>
inline const T&
STDMIN(
const T& a,
const T& b)
469 return b < a ? b : a;
477 template <
class T>
inline const T&
STDMAX(
const T& a,
const T& b)
480 return a < b ? b : a;
483 #if CRYPTOPP_MSC_VERSION
484 # pragma warning(push)
485 # pragma warning(disable: 4389)
488 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
489 # pragma GCC diagnostic push
490 # pragma GCC diagnostic ignored "-Wsign-compare"
491 # if (CRYPTOPP_LLVM_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000)
492 # pragma GCC diagnostic ignored "-Wtautological-compare"
493 # elif (CRYPTOPP_GCC_VERSION >= 40300)
494 # pragma GCC diagnostic ignored "-Wtype-limits"
503 template <
class T1,
class T2>
inline const T1
UnsignedMin(
const T1& a,
const T2& b)
506 if (
sizeof(T1)<=
sizeof(T2))
507 return b < (T2)a ? (T1)b : a;
509 return (T1)b < a ? (T1)b : a;
516 template <
class T1,
class T2>
520 if (from != to || (from > 0) != (to > 0))
533 static const unsigned int HIGH_BIT = (1U << 31);
534 const char CH = !!(base & HIGH_BIT) ?
'A' :
'a';
550 T digit = value % base;
551 result = char((digit < 10 ?
'0' : (CH - 10)) + digit) + result;
555 result =
"-" + result;
565 template <> CRYPTOPP_DLL
587 template <> CRYPTOPP_DLL
590 #if CRYPTOPP_MSC_VERSION
591 # pragma warning(pop)
594 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
595 # pragma GCC diagnostic pop
598 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
601 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
606 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
614 for (
unsigned int i=8*
sizeof(value)/2; i>0; i/=2)
616 return (
unsigned int)value&1;
628 unsigned int l=0, h=8*
sizeof(value);
631 unsigned int t = (l+h)/2;
650 unsigned int l=0, h=8*
sizeof(value);
654 unsigned int t = (l+h)/2;
676 #if defined(__GNUC__) && defined(__BMI__)
677 return (
unsigned int)_tzcnt_u32(v);
678 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
679 return (
unsigned int)__builtin_ctz(v);
680 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
681 unsigned long result;
682 _BitScanForward(&result, v);
683 return (
unsigned int)result;
686 static const int MultiplyDeBruijnBitPosition[32] =
688 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
689 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
691 return MultiplyDeBruijnBitPosition[((word32)((v & -v) * 0x077CB531U)) >> 27];
707 #if defined(__GNUC__) && defined(__BMI__) && defined(__x86_64__)
708 return (
unsigned int)_tzcnt_u64(v);
709 #elif defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 30400)
710 return (
unsigned int)__builtin_ctzll(v);
711 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && (defined(_M_X64) || defined(_M_IA64))
712 unsigned long result;
713 _BitScanForward64(&result, v);
714 return (
unsigned int)result;
728 inline T
Crop(T value,
size_t bits)
730 if (bits < 8*
sizeof(value))
731 return T(value & ((T(1) << bits) - 1));
742 return ((bitCount+7)/(8));
752 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
762 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
772 return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
781 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(
byte *buf,
const byte *mask,
size_t count);
790 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(
byte *output,
const byte *input,
const byte *mask,
size_t count);
800 CRYPTOPP_DLL
bool CRYPTOPP_API
VerifyBufsEqual(
const byte *buf1,
const byte *buf2,
size_t count);
810 return value > 0 && (value & (value-1)) == 0;
813 #if defined(__GNUC__) && defined(__BMI__)
815 inline bool IsPowerOf2<word32>(
const word32 &value)
817 return value > 0 && _blsr_u32(value) == 0;
820 # if defined(__x86_64__)
822 inline bool IsPowerOf2<word64>(
const word64 &value)
824 return value > 0 && _blsr_u64(value) == 0;
835 template <
class T1,
class T2>
839 return T2(a) & (b-1);
848 template <
class T1,
class T2>
864 template <
class T1,
class T2>
883 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS)
887 CRYPTOPP_UNUSED(dummy);
888 #if defined(CRYPTOPP_CXX11_ALIGNOF)
890 #elif (_MSC_VER >= 1300)
892 #elif defined(__GNUC__)
893 return __alignof__(T);
894 #elif CRYPTOPP_BOOL_SLOW_WORD64
909 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2((
size_t)ptr, alignment) == 0 : (size_t)ptr % alignment == 0);
920 CRYPTOPP_UNUSED(dummy);
924 #if defined(IS_LITTLE_ENDIAN)
926 #elif defined(IS_BIG_ENDIAN)
929 # error "Unable to determine endian-ness"
942 return NativeByteOrder::ToEnum();
960 template <
class T1,
class T2>
964 return T1((a > b) ? (a - b) : 0);
975 template <
class T1,
class T2>
979 return T1((a > b) ? (a - b) : 1);
1015 assert(inout != NULL); assert(size < INT_MAX);
1016 for (
int i=
int(size-1), carry=1; i>=0 && carry; i--)
1017 carry = !++inout[i];
1029 assert(output != NULL); assert(input != NULL); assert(size < INT_MAX);
1032 for (i=
int(size-1), carry=1; i>=0 && carry; i--)
1033 carry = ((output[i] = input[i]+1) == 0);
1034 memcpy_s(output, size, input,
size_t(i)+1);
1056 ptrdiff_t t = size_t(c) * (a - b);
1072 volatile T *p = buf+n;
1074 *((
volatile T*)(--p)) = 0;
1077 #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1085 volatile byte *p = buf;
1087 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1089 __stosb((
byte *)(
size_t)p, 0, n);
1099 volatile word16 *p = buf;
1101 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1103 __stosw((word16 *)(
size_t)p, 0, n);
1113 volatile word32 *p = buf;
1115 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1117 __stosd((
unsigned long *)(
size_t)p, 0, n);
1127 #if CRYPTOPP_BOOL_X64
1128 volatile word64 *p = buf;
1130 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1132 __stosq((word64 *)(
size_t)p, 0, n);
1139 #endif // #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
1141 #if (_MSC_VER >= 1700) && defined(_M_ARM)
1144 char *p =
reinterpret_cast<char*
>(buf+n);
1146 __iso_volatile_store8(--p, 0);
1151 short *p =
reinterpret_cast<short*
>(buf+n);
1153 __iso_volatile_store16(--p, 0);
1158 int *p =
reinterpret_cast<int*
>(buf+n);
1160 __iso_volatile_store32(--p, 0);
1165 __int64 *p =
reinterpret_cast<__int64*
>(buf+n);
1167 __iso_volatile_store64(--p, 0);
1178 if (
sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1180 else if (
sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1182 else if (
sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1200 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
1201 std::string StringNarrow(
const wchar_t *str,
bool throwOnError =
true);
1203 static std::string StringNarrow(
const wchar_t *str,
bool throwOnError =
true)
1209 #if (CRYPTOPP_MSC_VERSION >= 1400)
1210 size_t len=0, size=0;
1215 len = wcslen(str)+1;
1217 err = wcstombs_s(&size, NULL, 0, str, len*
sizeof(
wchar_t));
1219 if (err != 0) {
goto CONVERSION_ERROR;}
1221 result.resize(size);
1222 err = wcstombs_s(&size, &result[0], size, str, len*
sizeof(
wchar_t));
1231 return std::string();
1235 if (!result.empty() && result[size - 1] ==
'\0')
1236 result.erase(size - 1);
1238 size_t size = wcstombs(NULL, str, 0);
1239 assert(size != (
size_t)-1);
1240 if (size == (
size_t)-1) {
goto CONVERSION_ERROR;}
1242 result.resize(size);
1243 size = wcstombs(&result[0], str, size);
1244 assert(size != (
size_t)-1);
1246 if (size == (
size_t)-1)
1252 return std::string();
1258 #endif // StringNarrow and CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
1260 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
1278 #endif // CRYPTOPP_DOXYGEN_PROCESSING
1280 #if CRYPTOPP_BOOL_ALIGN16
1283 #endif // CRYPTOPP_BOOL_ALIGN16
1311 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1312 static const unsigned int MASK = THIS_SIZE-1;
1313 assert(y < THIS_SIZE);
1314 return T((x<<y)|(x>>(-y&MASK)));
1333 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1334 static const unsigned int MASK = THIS_SIZE-1;
1335 assert(y < THIS_SIZE);
1336 return T((x >> y)|(x<<(-y&MASK)));
1351 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1352 static const unsigned int MASK = THIS_SIZE-1;
1353 assert(y < THIS_SIZE);
1354 return T((x<<y)|(x>>(-y&MASK)));
1369 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1370 static const unsigned int MASK = THIS_SIZE-1;
1371 assert(y < THIS_SIZE);
1372 return T((x>>y)|(x<<(-y&MASK)));
1382 template <
class T>
inline T
rotlMod(T x,
unsigned int y)
1384 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1385 static const unsigned int MASK = THIS_SIZE-1;
1386 return T((x<<(y&MASK))|(x>>(-y&MASK)));
1396 template <
class T>
inline T
rotrMod(T x,
unsigned int y)
1398 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1399 static const unsigned int MASK = THIS_SIZE-1;
1400 return T((x>>(y&MASK))|(x<<(-y&MASK)));
1413 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1416 assert(y < 8*
sizeof(x));
1417 return y ? _lrotl(x,
static_cast<byte>(y)) : x;
1428 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1431 assert(y < 8*
sizeof(x));
1432 return y ? _lrotr(x,
static_cast<byte>(y)) : x;
1443 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1445 assert(y < 8*
sizeof(x));
1446 return _lrotl(x,
static_cast<byte>(y));
1457 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1459 assert(y < 8*
sizeof(x));
1460 return _lrotr(x,
static_cast<byte>(y));
1470 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1473 return _lrotl(x,
static_cast<byte>(y));
1483 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1486 return _lrotr(x,
static_cast<byte>(y));
1489 #endif // #ifdef _MSC_VER
1491 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
1502 template<>
inline word64 rotlFixed<word64>(word64 x,
unsigned int y)
1505 assert(y < 8*
sizeof(x));
1506 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1517 template<>
inline word64 rotrFixed<word64>(word64 x,
unsigned int y)
1520 assert(y < 8*
sizeof(x));
1521 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1532 template<>
inline word64 rotlVariable<word64>(word64 x,
unsigned int y)
1534 assert(y < 8*
sizeof(x));
1535 return _rotl64(x,
static_cast<byte>(y));
1546 template<>
inline word64 rotrVariable<word64>(word64 x,
unsigned int y)
1548 assert(y < 8*
sizeof(x));
1549 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1559 template<>
inline word64 rotlMod<word64>(word64 x,
unsigned int y)
1561 assert(y < 8*
sizeof(x));
1562 return y ? _rotl64(x,
static_cast<byte>(y)) : x;
1572 template<>
inline word64 rotrMod<word64>(word64 x,
unsigned int y)
1574 assert(y < 8*
sizeof(x));
1575 return y ? _rotr64(x,
static_cast<byte>(y)) : x;
1578 #endif // #if _MSC_VER >= 1310
1580 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
1583 template<>
inline word16 rotlFixed<word16>(word16 x,
unsigned int y)
1586 return _rotl16(x,
static_cast<byte>(y));
1589 template<>
inline word16 rotrFixed<word16>(word16 x,
unsigned int y)
1592 return _rotr16(x,
static_cast<byte>(y));
1595 template<>
inline word16 rotlVariable<word16>(word16 x,
unsigned int y)
1597 return _rotl16(x,
static_cast<byte>(y));
1600 template<>
inline word16 rotrVariable<word16>(word16 x,
unsigned int y)
1602 return _rotr16(x,
static_cast<byte>(y));
1605 template<>
inline word16 rotlMod<word16>(word16 x,
unsigned int y)
1607 return _rotl16(x,
static_cast<byte>(y));
1610 template<>
inline word16 rotrMod<word16>(word16 x,
unsigned int y)
1612 return _rotr16(x,
static_cast<byte>(y));
1615 template<>
inline byte rotlFixed<byte>(
byte x,
unsigned int y)
1618 return _rotl8(x,
static_cast<byte>(y));
1621 template<>
inline byte rotrFixed<byte>(
byte x,
unsigned int y)
1624 return _rotr8(x,
static_cast<byte>(y));
1627 template<>
inline byte rotlVariable<byte>(
byte x,
unsigned int y)
1629 return _rotl8(x,
static_cast<byte>(y));
1632 template<>
inline byte rotrVariable<byte>(
byte x,
unsigned int y)
1634 return _rotr8(x,
static_cast<byte>(y));
1637 template<>
inline byte rotlMod<byte>(
byte x,
unsigned int y)
1639 return _rotl8(x,
static_cast<byte>(y));
1642 template<>
inline byte rotrMod<byte>(
byte x,
unsigned int y)
1644 return _rotr8(x,
static_cast<byte>(y));
1647 #endif // #if _MSC_VER >= 1400
1649 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1651 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1654 return y ? __rlwinm(x,y,0,31) : x;
1657 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1660 return y ? __rlwinm(x,32-y,0,31) : x;
1663 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1666 return (__rlwnm(x,y,0,31));
1669 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1672 return (__rlwnm(x,32-y,0,31));
1675 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1677 return (__rlwnm(x,y,0,31));
1680 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1682 return (__rlwnm(x,32-y,0,31));
1685 #endif // #if (defined(__MWERKS__) && TARGET_CPU_PPC)
1697 return GETBYTE(value, index);
1699 return GETBYTE(value,
sizeof(T)-index-1);
1716 #ifdef CRYPTOPP_BYTESWAP_AVAILABLE
1717 return bswap_16(value);
1718 #elif defined(_MSC_VER) && _MSC_VER >= 1300
1719 return _byteswap_ushort(value);
1731 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
1732 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1734 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
1735 return bswap_32(value);
1736 #elif defined(__MWERKS__) && TARGET_CPU_PPC
1737 return (word32)__lwbrx(&value,0);
1738 #elif _MSC_VER >= 1400 || (_MSC_VER >= 1300 && !defined(_DLL))
1739 return _byteswap_ulong(value);
1740 #elif CRYPTOPP_FAST_ROTATE(32)
1745 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
1756 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
1757 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1759 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
1760 return bswap_64(value);
1761 #elif defined(_MSC_VER) && _MSC_VER >= 1300
1762 return _byteswap_uint64(value);
1763 #elif CRYPTOPP_BOOL_SLOW_WORD64
1766 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
1767 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
1777 value = byte((value & 0xAA) >> 1) | byte((value & 0x55) << 1);
1778 value = byte((value & 0xCC) >> 2) | byte((value & 0x33) << 2);
1787 value = word16((value & 0xAAAA) >> 1) | word16((value & 0x5555) << 1);
1788 value = word16((value & 0xCCCC) >> 2) | word16((value & 0x3333) << 2);
1789 value = word16((value & 0xF0F0) >> 4) | word16((value & 0x0F0F) << 4);
1798 value = word32((value & 0xAAAAAAAA) >> 1) | word32((value & 0x55555555) << 1);
1799 value = word32((value & 0xCCCCCCCC) >> 2) | word32((value & 0x33333333) << 2);
1800 value = word32((value & 0xF0F0F0F0) >> 4) | word32((value & 0x0F0F0F0F) << 4);
1809 #if CRYPTOPP_BOOL_SLOW_WORD64
1812 value = word64((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | word64((value & W64LIT(0x5555555555555555)) << 1);
1813 value = word64((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | word64((value & W64LIT(0x3333333333333333)) << 2);
1814 value = word64((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | word64((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
1830 else if (
sizeof(T) == 2)
1832 else if (
sizeof(T) == 4)
1836 assert(
sizeof(T) == 8);
1892 assert(byteCount %
sizeof(T) == 0);
1893 size_t count = byteCount/
sizeof(T);
1894 for (
size_t i=0; i<count; i++)
1917 memcpy_s(out, byteCount, in, byteCount);
1921 inline void GetUserKey(
ByteOrder order, T *out,
size_t outlen,
const byte *in,
size_t inlen)
1923 const size_t U =
sizeof(T);
1924 assert(inlen <= outlen*U);
1925 memcpy_s(out, outlen*U, in, inlen);
1926 memset_z((
byte *)out+inlen, 0, outlen*U-inlen);
1930 #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
1931 inline byte UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const byte *)
1933 CRYPTOPP_UNUSED(order);
1937 inline word16 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word16 *)
1940 ? block[1] | (block[0] << 8)
1941 : block[0] | (block[1] << 8);
1944 inline word32 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word32 *)
1947 ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
1948 : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
1951 inline word64 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word64 *)
1956 (word64(block[6]) << 8) |
1957 (word64(block[5]) << 16) |
1958 (word64(block[4]) << 24) |
1959 (word64(block[3]) << 32) |
1960 (word64(block[2]) << 40) |
1961 (word64(block[1]) << 48) |
1962 (word64(block[0]) << 56))
1965 (word64(block[1]) << 8) |
1966 (word64(block[2]) << 16) |
1967 (word64(block[3]) << 24) |
1968 (word64(block[4]) << 32) |
1969 (word64(block[5]) << 40) |
1970 (word64(block[6]) << 48) |
1971 (word64(block[7]) << 56));
1974 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block,
byte value,
const byte *xorBlock)
1976 CRYPTOPP_UNUSED(order);
1977 block[0] = (byte)(xorBlock ? (value ^ xorBlock[0]) : value);
1980 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word16 value,
const byte *xorBlock)
1986 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1987 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1991 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1992 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1999 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2000 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2004 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2005 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2010 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word32 value,
const byte *xorBlock)
2016 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2017 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2018 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2019 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2023 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2024 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2025 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2026 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2033 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2034 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2035 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2036 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2040 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2041 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2042 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2043 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2048 inline void UnalignedbyteNonTemplate(
ByteOrder order,
byte *block, word64 value,
const byte *xorBlock)
2054 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2055 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2056 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2057 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2058 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2059 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2060 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2061 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2065 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2066 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2067 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2068 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2069 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2070 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2071 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2072 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2079 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2080 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2081 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2082 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2083 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2084 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2085 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2086 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2090 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
2091 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
2092 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
2093 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
2094 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
2095 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
2096 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
2097 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
2101 #endif // #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
2104 inline T GetWord(
bool assumeAligned,
ByteOrder order,
const byte *block)
2112 CRYPTOPP_UNUSED(assumeAligned);
2113 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
2117 memcpy(&temp, block,
sizeof(T));
2123 inline void GetWord(
bool assumeAligned,
ByteOrder order, T &result,
const byte *block)
2125 result = GetWord<T>(assumeAligned, order, block);
2129 inline void PutWord(
bool assumeAligned,
ByteOrder order,
byte *block, T value,
const byte *xorBlock = NULL)
2138 CRYPTOPP_UNUSED(assumeAligned);
2139 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
2140 *
reinterpret_cast<T *
>((
void *)block) =
ConditionalByteReverse(order, value) ^ (xorBlock ? *
reinterpret_cast<const T *
>((
const void *)xorBlock) : 0);
2144 if (xorBlock) memcpy(&t2, xorBlock,
sizeof(T));
2145 memmove(block, &(t1 ^= t2),
sizeof(T));
2165 template <
class T,
class B,
bool A=false>
2172 : m_block((const byte *)block) {}
2182 x = GetWord<T>(A, B::ToEnum(), m_block);
2183 m_block +=
sizeof(T);
2188 const byte *m_block;
2207 template <
class T,
class B,
bool A=false>
2215 : m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
2224 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2225 m_block +=
sizeof(T);
2227 m_xorBlock +=
sizeof(T);
2232 const byte *m_xorBlock;
2245 template <
class T,
class B,
bool GA=false,
bool PA=false>
2259 return std::string((
char *)&value,
sizeof(value));
2295 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2307 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2326 return value >> bits;
2337 return value << bits;
2350 template <
unsigned int bits,
class T>
2353 return SafeShifter<(bits>=(8*
sizeof(T)))>::RightShift(value, bits);
2365 template <
unsigned int bits,
class T>
2368 return SafeShifter<(bits>=(8*
sizeof(T)))>::LeftShift(value, bits);
2373 #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);}
2374 #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);}
2375 #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);}
2376 #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);}
2377 #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);}
2378 #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);}
2379 #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);}
2380 #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);}
2381 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
2385 #if CRYPTOPP_MSC_VERSION
2386 # pragma warning(pop)