6 #ifndef CRYPTOPP_MISC_H 7 #define CRYPTOPP_MISC_H 11 #if !CRYPTOPP_DOXYGEN_PROCESSING 13 #if CRYPTOPP_MSC_VERSION 14 # pragma warning(push) 15 # pragma warning(disable: 4146) 16 # if (CRYPTOPP_MSC_VERSION >= 1400) 17 # pragma warning(disable: 6326) 28 #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1 29 #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2 30 #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3 31 #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4 33 #undef _interlockedbittestandset 34 #undef _interlockedbittestandreset 35 #undef _interlockedbittestandset64 36 #undef _interlockedbittestandreset64 37 #define CRYPTOPP_FAST_ROTATE(x) 1 38 #elif _MSC_VER >= 1300 39 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64) 41 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) 43 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \ 44 (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM))) 45 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32) 46 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions 47 #define CRYPTOPP_FAST_ROTATE(x) 1 49 #define CRYPTOPP_FAST_ROTATE(x) 0 56 #if defined(__GNUC__) && defined(__linux__) 57 #define CRYPTOPP_BYTESWAP_AVAILABLE 61 #endif // CRYPTOPP_DOXYGEN_PROCESSING 63 #if CRYPTOPP_DOXYGEN_PROCESSING 79 # if defined(__SIZE_MAX__) 80 # define SIZE_MAX __SIZE_MAX__ 81 # elif defined(SIZE_T_MAX) 82 # define SIZE_MAX SIZE_T_MAX 84 # define SIZE_MAX ((std::numeric_limits<size_t>::max)()) 88 #endif // CRYPTOPP_DOXYGEN_PROCESSING 97 #if CRYPTOPP_DOXYGEN_PROCESSING 101 #define CRYPTOPP_COMPILE_ASSERT(expr) ... 102 #else // CRYPTOPP_DOXYGEN_PROCESSING 106 static char dummy[2*b-1];
110 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__) 111 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS) 112 #define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) 114 # if defined(__GNUC__) 115 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \ 116 static CompileAssert<(assertion)> \ 117 CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance) __attribute__ ((unused)) 119 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \ 120 static CompileAssert<(assertion)> \ 121 CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance) 124 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y) 125 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y 127 #endif // CRYPTOPP_DOXYGEN_PROCESSING 131 #if CRYPTOPP_DOXYGEN_PROCESSING 140 # define COUNTOF(arr) 144 # if defined(_MSC_VER) && (_MSC_VER >= 1400) 145 # define COUNTOF(x) _countof(x) 147 # define COUNTOF(x) (sizeof(x)/sizeof(x[0])) 150 #endif // CRYPTOPP_DOXYGEN_PROCESSING 154 #if !CRYPTOPP_DOXYGEN_PROCESSING 155 class CRYPTOPP_DLL Empty
159 template <
class BASE1,
class BASE2>
160 class CRYPTOPP_NO_VTABLE TwoBases :
public BASE1,
public BASE2
164 template <
class BASE1,
class BASE2,
class BASE3>
165 class CRYPTOPP_NO_VTABLE ThreeBases :
public BASE1,
public BASE2,
public BASE3
168 #endif // CRYPTOPP_DOXYGEN_PROCESSING 202 T* operator()()
const {
return new T;}
205 #if CRYPTOPP_DOXYGEN_PROCESSING 213 #define MEMORY_BARRIER ... 215 #if (_MSC_VER >= 1400) 216 # pragma intrinsic(_ReadWriteBarrier) 217 # define MEMORY_BARRIER() _ReadWriteBarrier() 218 #elif defined(__INTEL_COMPILER) 219 # define MEMORY_BARRIER() __memory_barrier() 220 #elif defined(__GNUC__) || defined(__clang__) 221 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory") 223 # define MEMORY_BARRIER() 225 #endif // CRYPTOPP_DOXYGEN_PROCESSING 235 template <
class T,
class F = NewObject<T>,
int instance=0>
239 Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
242 CRYPTOPP_NOINLINE
const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT)
const;
251 template <
class T,
class F,
int instance>
255 T *p = s_pObject.m_p;
261 T *newObject = m_objectFactory();
271 s_pObject.m_p = newObject;
279 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB) 299 inline void memcpy_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
304 assert(dest != NULL); assert(src != NULL);
306 assert(sizeInBytes >= count);
307 if (count > sizeInBytes)
310 #if CRYPTOPP_MSC_VERSION 311 # pragma warning(push) 312 # pragma warning(disable: 4996) 313 # if (CRYPTOPP_MSC_VERSION >= 1400) 314 # pragma warning(disable: 6386) 317 memcpy(dest, src, count);
318 #if CRYPTOPP_MSC_VERSION 319 # pragma warning(pop) 341 inline void memmove_s(
void *dest,
size_t sizeInBytes,
const void *src,
size_t count)
346 assert(dest != NULL); assert(src != NULL);
348 assert(sizeInBytes >= count);
349 if (count > sizeInBytes)
352 #if CRYPTOPP_MSC_VERSION 353 # pragma warning(push) 354 # pragma warning(disable: 4996) 355 # if (CRYPTOPP_MSC_VERSION >= 1400) 356 # pragma warning(disable: 6386) 359 memmove(dest, src, count);
360 #if CRYPTOPP_MSC_VERSION 361 # pragma warning(pop) 365 #if __BORLANDC__ >= 0x620 367 # define memcpy_s CryptoPP::memcpy_s 368 # define memmove_s CryptoPP::memmove_s 371 #endif // __STDC_WANT_SECURE_LIB__ 379 inline void *
memset_z(
void *ptr,
int value,
size_t num)
382 #if CRYPTOPP_GCC_VERSION >= 30001 383 if (__builtin_constant_p(num) && num==0)
386 volatile void* x = memset(ptr, value, num);
387 return const_cast<void*
>(x);
395 template <
class T>
inline const T&
STDMIN(
const T& a,
const T& b)
397 return b < a ? b : a;
405 template <
class T>
inline const T&
STDMAX(
const T& a,
const T& b)
408 return a < b ? b : a;
411 #if CRYPTOPP_MSC_VERSION 412 # pragma warning(push) 413 # pragma warning(disable: 4389) 416 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 417 # pragma GCC diagnostic push 418 # pragma GCC diagnostic ignored "-Wsign-compare" 419 # if (CRYPTOPP_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000) 420 # pragma GCC diagnostic ignored "-Wtautological-compare" 421 # elif (CRYPTOPP_GCC_VERSION >= 40300) 422 # pragma GCC diagnostic ignored "-Wtype-limits" 431 template <
class T1,
class T2>
inline const T1
UnsignedMin(
const T1& a,
const T2& b)
434 if (
sizeof(T1)<=
sizeof(T2))
435 return b < (T2)a ? (T1)b : a;
437 return (T1)b < a ? (T1)b : a;
444 template <
class T1,
class T2>
448 if (from != to || (from > 0) != (to > 0))
461 static const unsigned int HIGH_BIT = (1U << 31);
462 const char CH = !!(base & HIGH_BIT) ?
'A' :
'a';
478 T digit = value % base;
479 result = char((digit < 10 ?
'0' : (CH - 10)) + digit) + result;
483 result =
"-" + result;
493 template <> CRYPTOPP_DLL
516 template <> CRYPTOPP_DLL
519 #if CRYPTOPP_MSC_VERSION 520 # pragma warning(pop) 523 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 524 # pragma GCC diagnostic pop 527 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue 530 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y))) 535 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y))) 543 for (
unsigned int i=8*
sizeof(value)/2; i>0; i/=2)
545 return (
unsigned int)value&1;
557 unsigned int l=0, h=8*
sizeof(value);
560 unsigned int t = (l+h)/2;
579 unsigned int l=0, h=8*
sizeof(value);
583 unsigned int t = (l+h)/2;
602 #if defined(__GNUC__) && CRYPTOPP_GCC_VERSION >= 30400 603 return __builtin_ctz(v);
604 #elif defined(_MSC_VER) && _MSC_VER >= 1400 605 unsigned long result;
606 _BitScanForward(&result, v);
610 static const int MultiplyDeBruijnBitPosition[32] =
612 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
613 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
615 return MultiplyDeBruijnBitPosition[((word32)((v & -v) * 0x077CB531U)) >> 27];
628 #if defined(__GNUC__) && CRYPTOPP_GCC_VERSION >= 30400 629 return __builtin_ctzll(v);
630 #elif defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(_M_X64) || defined(_M_IA64)) 631 unsigned long result;
632 _BitScanForward64(&result, v);
647 inline T
Crop(T value,
size_t bits)
649 if (bits < 8*
sizeof(value))
650 return T(value & ((T(1) << bits) - 1));
661 return ((bitCount+7)/(8));
671 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
681 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
691 return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
700 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(byte *buf,
const byte *mask,
size_t count);
709 CRYPTOPP_DLL
void CRYPTOPP_API
xorbuf(byte *output,
const byte *input,
const byte *mask,
size_t count);
719 CRYPTOPP_DLL
bool CRYPTOPP_API
VerifyBufsEqual(
const byte *buf1,
const byte *buf2,
size_t count);
729 return value > 0 && (value & (value-1)) == 0;
738 template <
class T1,
class T2>
742 return T2(a) & (b-1);
751 template <
class T1,
class T2>
767 template <
class T1,
class T2>
786 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) 790 CRYPTOPP_UNUSED(dummy);
791 #if defined(CRYPTOPP_CXX11_ALIGNOF) 793 #elif (_MSC_VER >= 1300) 795 #elif defined(__GNUC__) 796 return __alignof__(T);
797 #elif CRYPTOPP_BOOL_SLOW_WORD64 812 return alignment==1 || (
IsPowerOf2(alignment) ?
ModPowerOf2((
size_t)ptr, alignment) == 0 : (size_t)ptr % alignment == 0);
823 CRYPTOPP_UNUSED(dummy);
827 #if defined(IS_LITTLE_ENDIAN) 829 #elif defined(IS_BIG_ENDIAN) 832 # error "Unable to determine endian-ness" 845 return NativeByteOrder::ToEnum();
863 template <
class T1,
class T2>
867 return T1((a > b) ? (a - b) : 0);
877 template <
class T1,
class T2>
881 return T1((a > b) ? (a - b) : 1);
895 return obj.IsForwardTransformation() ? ENCRYPTION : DECRYPTION;
916 assert(inout != NULL); assert(size < INT_MAX);
917 for (
int i=
int(size-1), carry=1; i>=0 && carry; i--)
930 assert(output != NULL); assert(input != NULL); assert(size < INT_MAX);
933 for (i=
int(size-1), carry=1; i>=0 && carry; i--)
934 carry = ((output[i] = input[i]+1) == 0);
957 ptrdiff_t t = size_t(c) * (a - b);
973 volatile T *p = buf+n;
975 *((
volatile T*)(--p)) = 0;
978 #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) 986 volatile byte *p = buf;
988 asm volatile(
"rep stosb" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
990 __stosb((byte *)(
size_t)p, 0, n);
1000 volatile word16 *p = buf;
1002 asm volatile(
"rep stosw" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1004 __stosw((word16 *)(
size_t)p, 0, n);
1014 volatile word32 *p = buf;
1016 asm volatile(
"rep stosl" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1018 __stosd((
unsigned long *)(
size_t)p, 0, n);
1028 #if CRYPTOPP_BOOL_X64 1029 volatile word64 *p = buf;
1031 asm volatile(
"rep stosq" :
"+c"(n),
"+D"(p) :
"a"(0) :
"memory");
1033 __stosq((word64 *)(
size_t)p, 0, n);
1040 #endif // #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) 1049 if (
sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
1051 else if (
sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
1053 else if (
sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
1071 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 1072 static inline std::string StringNarrow(
const wchar_t *str,
bool throwOnError =
true)
1074 static std::string StringNarrow(
const wchar_t *str,
bool throwOnError =
true)
1081 #if (CRYPTOPP_MSC_VERSION >= 1400) 1082 size_t len=0, size = 0;
1087 len = wcslen(str)+1;
1089 err = wcstombs_s(&size, NULL, 0, str, len*
sizeof(
wchar_t));
1091 if (err != 0) {
goto CONVERSION_ERROR;}
1093 result.resize(size);
1094 err = wcstombs_s(&size, &result[0], size, str, len*
sizeof(
wchar_t));
1103 return std::string();
1107 if (!result.empty() && result[size - 1] ==
'\0')
1108 result.erase(size - 1);
1110 size_t size = wcstombs(NULL, str, 0);
1111 assert(size != (
size_t)-1);
1112 if (size == (
size_t)-1) {
goto CONVERSION_ERROR;}
1114 result.resize(size);
1115 size = wcstombs(&result[0], str, size);
1116 assert(size != (
size_t)-1);
1118 if (size == (
size_t)-1)
1124 return std::string();
1131 #ifdef CRYPTOPP_DOXYGEN_PROCESSING 1149 #endif // CRYPTOPP_DOXYGEN_PROCESSING 1151 #if CRYPTOPP_BOOL_ALIGN16 1154 #endif // CRYPTOPP_BOOL_ALIGN16 1182 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1183 static const unsigned int MASK = THIS_SIZE-1;
1185 assert(y < THIS_SIZE);
1186 return T((x<<y)|(x>>(-y&MASK)));
1204 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1205 static const unsigned int MASK = THIS_SIZE-1;
1206 assert(y < THIS_SIZE);
1207 return T((x >> y)|(x<<(-y&MASK)));
1221 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1222 static const unsigned int MASK = THIS_SIZE-1;
1223 assert(y < THIS_SIZE);
1224 return T((x<<y)|(x>>(-y&MASK)));
1238 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1239 static const unsigned int MASK = THIS_SIZE-1;
1240 assert(y < THIS_SIZE);
1241 return T((x>>y)|(x<<(-y&MASK)));
1250 template <
class T>
inline T
rotlMod(T x,
unsigned int y)
1252 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1253 static const unsigned int MASK = THIS_SIZE-1;
1254 return T((x<<(y&MASK))|(x>>(-y&MASK)));
1263 template <
class T>
inline T
rotrMod(T x,
unsigned int y)
1265 static const unsigned int THIS_SIZE =
sizeof(T)*8;
1266 static const unsigned int MASK = THIS_SIZE-1;
1267 return T((x>>(y&MASK))|(x<<(-y&MASK)));
1279 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1282 assert(y < 8*
sizeof(x));
1283 return y ? _lrotl(x, static_cast<byte>(y)) : x;
1293 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1296 assert(y < 8*
sizeof(x));
1297 return y ? _lrotr(x, static_cast<byte>(y)) : x;
1307 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1309 assert(y < 8*
sizeof(x));
1310 return _lrotl(x, static_cast<byte>(y));
1320 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1322 assert(y < 8*
sizeof(x));
1323 return _lrotr(x, static_cast<byte>(y));
1332 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1335 return _lrotl(x, static_cast<byte>(y));
1344 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1347 return _lrotr(x, static_cast<byte>(y));
1350 #endif // #ifdef _MSC_VER 1352 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) 1362 template<>
inline word64 rotlFixed<word64>(word64 x,
unsigned int y)
1365 assert(y < 8*
sizeof(x));
1366 return y ? _rotl64(x, static_cast<byte>(y)) : x;
1376 template<>
inline word64 rotrFixed<word64>(word64 x,
unsigned int y)
1379 assert(y < 8*
sizeof(x));
1380 return y ? _rotr64(x, static_cast<byte>(y)) : x;
1390 template<>
inline word64 rotlVariable<word64>(word64 x,
unsigned int y)
1392 assert(y < 8*
sizeof(x));
1393 return _rotl64(x, static_cast<byte>(y));
1403 template<>
inline word64 rotrVariable<word64>(word64 x,
unsigned int y)
1405 assert(y < 8*
sizeof(x));
1406 return y ? _rotr64(x, static_cast<byte>(y)) : x;
1415 template<>
inline word64 rotlMod<word64>(word64 x,
unsigned int y)
1417 assert(y < 8*
sizeof(x));
1418 return y ? _rotl64(x, static_cast<byte>(y)) : x;
1427 template<>
inline word64 rotrMod<word64>(word64 x,
unsigned int y)
1429 assert(y < 8*
sizeof(x));
1430 return y ? _rotr64(x, static_cast<byte>(y)) : x;
1433 #endif // #if _MSC_VER >= 1310 1435 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER) 1438 template<>
inline word16 rotlFixed<word16>(word16 x,
unsigned int y)
1441 return _rotl16(x, static_cast<byte>(y));
1444 template<>
inline word16 rotrFixed<word16>(word16 x,
unsigned int y)
1447 return _rotr16(x, static_cast<byte>(y));
1450 template<>
inline word16 rotlVariable<word16>(word16 x,
unsigned int y)
1452 return _rotl16(x, static_cast<byte>(y));
1455 template<>
inline word16 rotrVariable<word16>(word16 x,
unsigned int y)
1457 return _rotr16(x, static_cast<byte>(y));
1460 template<>
inline word16 rotlMod<word16>(word16 x,
unsigned int y)
1462 return _rotl16(x, static_cast<byte>(y));
1465 template<>
inline word16 rotrMod<word16>(word16 x,
unsigned int y)
1467 return _rotr16(x, static_cast<byte>(y));
1470 template<>
inline byte rotlFixed<byte>(byte x,
unsigned int y)
1473 return _rotl8(x, static_cast<byte>(y));
1476 template<>
inline byte rotrFixed<byte>(byte x,
unsigned int y)
1479 return _rotr8(x, static_cast<byte>(y));
1482 template<>
inline byte rotlVariable<byte>(byte x,
unsigned int y)
1484 return _rotl8(x, static_cast<byte>(y));
1487 template<>
inline byte rotrVariable<byte>(byte x,
unsigned int y)
1489 return _rotr8(x, static_cast<byte>(y));
1492 template<>
inline byte rotlMod<byte>(byte x,
unsigned int y)
1494 return _rotl8(x, static_cast<byte>(y));
1497 template<>
inline byte rotrMod<byte>(byte x,
unsigned int y)
1499 return _rotr8(x, static_cast<byte>(y));
1502 #endif // #if _MSC_VER >= 1400 1504 #if (defined(__MWERKS__) && TARGET_CPU_PPC) 1506 template<>
inline word32 rotlFixed<word32>(word32 x,
unsigned int y)
1509 return y ? __rlwinm(x,y,0,31) : x;
1512 template<>
inline word32 rotrFixed<word32>(word32 x,
unsigned int y)
1515 return y ? __rlwinm(x,32-y,0,31) : x;
1518 template<>
inline word32 rotlVariable<word32>(word32 x,
unsigned int y)
1521 return (__rlwnm(x,y,0,31));
1524 template<>
inline word32 rotrVariable<word32>(word32 x,
unsigned int y)
1527 return (__rlwnm(x,32-y,0,31));
1530 template<>
inline word32 rotlMod<word32>(word32 x,
unsigned int y)
1532 return (__rlwnm(x,y,0,31));
1535 template<>
inline word32 rotrMod<word32>(word32 x,
unsigned int y)
1537 return (__rlwnm(x,32-y,0,31));
1540 #endif // #if (defined(__MWERKS__) && TARGET_CPU_PPC) 1551 if (order == LITTLE_ENDIAN_ORDER)
1552 return GETBYTE(value, index);
1554 return GETBYTE(value,
sizeof(T)-index-1);
1571 #ifdef CRYPTOPP_BYTESWAP_AVAILABLE 1572 return bswap_16(value);
1573 #elif defined(_MSC_VER) && _MSC_VER >= 1300 1574 return _byteswap_ushort(value);
1586 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) 1587 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1589 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) 1590 return bswap_32(value);
1591 #elif defined(__MWERKS__) && TARGET_CPU_PPC 1592 return (word32)__lwbrx(&value,0);
1593 #elif _MSC_VER >= 1400 || (_MSC_VER >= 1300 && !defined(_DLL)) 1594 return _byteswap_ulong(value);
1595 #elif CRYPTOPP_FAST_ROTATE(32) 1600 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
1611 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__) 1612 __asm__ (
"bswap %0" :
"=r" (value) :
"0" (value));
1614 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE) 1615 return bswap_64(value);
1616 #elif defined(_MSC_VER) && _MSC_VER >= 1300 1617 return _byteswap_uint64(value);
1618 #elif CRYPTOPP_BOOL_SLOW_WORD64 1621 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
1622 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
1632 value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
1633 value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
1642 value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1);
1643 value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2);
1644 value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4);
1653 value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1);
1654 value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2);
1655 value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4);
1664 #if CRYPTOPP_BOOL_SLOW_WORD64 1667 value = ((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | ((value & W64LIT(0x5555555555555555)) << 1);
1668 value = ((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | ((value & W64LIT(0x3333333333333333)) << 2);
1669 value = ((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | ((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
1685 else if (
sizeof(T) == 2)
1687 else if (
sizeof(T) == 4)
1691 assert(
sizeof(T) == 8);
1724 assert(byteCount %
sizeof(T) == 0);
1725 size_t count = byteCount/
sizeof(T);
1726 for (
size_t i=0; i<count; i++)
1749 memcpy_s(out, byteCount, in, byteCount);
1753 inline void GetUserKey(
ByteOrder order, T *out,
size_t outlen,
const byte *in,
size_t inlen)
1755 const size_t U =
sizeof(T);
1756 assert(inlen <= outlen*U);
1757 memcpy_s(out, outlen*U, in, inlen);
1758 memset_z((byte *)out+inlen, 0, outlen*U-inlen);
1762 #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 1763 inline byte UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const byte *)
1765 CRYPTOPP_UNUSED(order);
1769 inline word16 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word16 *)
1771 return (order == BIG_ENDIAN_ORDER)
1772 ? block[1] | (block[0] << 8)
1773 : block[0] | (block[1] << 8);
1776 inline word32 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word32 *)
1778 return (order == BIG_ENDIAN_ORDER)
1779 ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
1780 : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
1783 inline word64 UnalignedGetWordNonTemplate(
ByteOrder order,
const byte *block,
const word64 *)
1785 return (order == BIG_ENDIAN_ORDER)
1788 (word64(block[6]) << 8) |
1789 (word64(block[5]) << 16) |
1790 (word64(block[4]) << 24) |
1791 (word64(block[3]) << 32) |
1792 (word64(block[2]) << 40) |
1793 (word64(block[1]) << 48) |
1794 (word64(block[0]) << 56))
1797 (word64(block[1]) << 8) |
1798 (word64(block[2]) << 16) |
1799 (word64(block[3]) << 24) |
1800 (word64(block[4]) << 32) |
1801 (word64(block[5]) << 40) |
1802 (word64(block[6]) << 48) |
1803 (word64(block[7]) << 56));
1806 inline void UnalignedbyteNonTemplate(
ByteOrder order, byte *block, byte value,
const byte *xorBlock)
1808 CRYPTOPP_UNUSED(order);
1809 block[0] = xorBlock ? (value ^ xorBlock[0]) : value;
1812 inline void UnalignedbyteNonTemplate(
ByteOrder order, byte *block, word16 value,
const byte *xorBlock)
1814 if (order == BIG_ENDIAN_ORDER)
1818 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1819 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1823 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1824 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1831 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1832 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1836 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1837 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1842 inline void UnalignedbyteNonTemplate(
ByteOrder order, byte *block, word32 value,
const byte *xorBlock)
1844 if (order == BIG_ENDIAN_ORDER)
1848 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1849 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1850 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1851 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1855 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1856 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1857 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1858 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1865 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1866 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1867 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1868 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1872 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1873 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1874 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1875 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1880 inline void UnalignedbyteNonTemplate(
ByteOrder order, byte *block, word64 value,
const byte *xorBlock)
1882 if (order == BIG_ENDIAN_ORDER)
1886 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
1887 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
1888 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
1889 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
1890 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1891 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1892 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1893 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1897 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
1898 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
1899 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
1900 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
1901 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1902 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1903 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1904 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1911 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1912 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1913 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1914 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1915 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
1916 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
1917 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
1918 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
1922 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
1923 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
1924 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
1925 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
1926 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
1927 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
1928 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
1929 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
1933 #endif // #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 1936 inline T GetWord(
bool assumeAligned,
ByteOrder order,
const byte *block)
1944 CRYPTOPP_UNUSED(assumeAligned);
1945 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 1949 memcpy(&temp, block,
sizeof(T));
1955 inline void GetWord(
bool assumeAligned,
ByteOrder order, T &result,
const byte *block)
1957 result = GetWord<T>(assumeAligned, order, block);
1961 inline void PutWord(
bool assumeAligned,
ByteOrder order, byte *block, T value,
const byte *xorBlock = NULL)
1970 CRYPTOPP_UNUSED(assumeAligned);
1971 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS 1972 *
reinterpret_cast<T *
>(block) =
ConditionalByteReverse(order, value) ^ (xorBlock ? *
reinterpret_cast<const T *
>(xorBlock) : 0);
1976 if (xorBlock) memcpy(&t2, xorBlock,
sizeof(T));
1977 memmove(block, &(t1 ^= t2),
sizeof(T));
1981 template <
class T,
class B,
bool A=false>
1986 : m_block((
const byte *)block) {}
1992 x = GetWord<T>(A, B::ToEnum(), m_block);
1993 m_block +=
sizeof(T);
1998 const byte *m_block;
2001 template <
class T,
class B,
bool A=false>
2005 PutBlock(
const void *xorBlock,
void *block)
2006 : m_xorBlock((
const byte *)xorBlock), m_block((byte *)block) {}
2011 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
2012 m_block +=
sizeof(T);
2014 m_xorBlock +=
sizeof(T);
2019 const byte *m_xorBlock;
2023 template <
class T,
class B,
bool GA=false,
bool PA=false>
2032 std::string WordToString(T value,
ByteOrder order = BIG_ENDIAN_ORDER)
2037 return std::string((
char *)&value,
sizeof(value));
2041 T StringToWord(
const std::string &str,
ByteOrder order = BIG_ENDIAN_ORDER)
2055 static inline T RightShift(T value,
unsigned int bits)
2057 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2062 static inline T LeftShift(T value,
unsigned int bits)
2064 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
2072 static inline T RightShift(T value,
unsigned int bits)
2074 return value >> bits;
2078 static inline T LeftShift(T value,
unsigned int bits)
2080 return value << bits;
2084 template <
unsigned int bits,
class T>
2085 inline T SafeRightShift(T value)
2090 template <
unsigned int bits,
class T>
2091 inline T SafeLeftShift(T value)
2098 #define CRYPTOPP_BLOCK_1(n, t, s) t* m_##n() {return (t *)(m_aggregate+0);} size_t SS1() {return sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2099 #define CRYPTOPP_BLOCK_2(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS1());} size_t SS2() {return SS1()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2100 #define CRYPTOPP_BLOCK_3(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS2());} size_t SS3() {return SS2()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2101 #define CRYPTOPP_BLOCK_4(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS3());} size_t SS4() {return SS3()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2102 #define CRYPTOPP_BLOCK_5(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS4());} size_t SS5() {return SS4()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2103 #define CRYPTOPP_BLOCK_6(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS5());} size_t SS6() {return SS5()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2104 #define CRYPTOPP_BLOCK_7(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS6());} size_t SS7() {return SS6()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2105 #define CRYPTOPP_BLOCK_8(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS7());} size_t SS8() {return SS7()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} 2106 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate; 2110 #if CRYPTOPP_MSC_VERSION 2111 # pragma warning(pop) void SecureWipeBuffer(T *buf, size_t n)
Sets each element of an array to 0.
An invalid argument was detected.
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memmove()
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from → to is safe to perform.
unsigned int GetAlignmentOf(T *dummy=NULL)
Returns the minimum alignment requirements of a type.
ByteOrder
Provides the byte ordering.
void AlignedDeallocate(void *ptr)
Frees a buffer allocated with AlignedAllocate.
Restricts the instantiation of a class to one static object without locks.
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
T2 ModPowerOf2(const T1 &a, const T2 &b)
Tests whether the residue of a value is a power of 2.
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
T rotlFixed(T x, unsigned int y)
Performs a left rotate.
size_t BitsToWords(size_t bitCount)
Returns the number of words required for the specified number of bits.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
Converts a typename to an enumerated value.
CipherDir
Specifies a direction for a cipher to operate.
Abstract base classes that provide a uniform interface to this library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
An object factory function.
Classes for automatic resource management.
Library configuration file.
bool IsAligned(const void *ptr, T *dummy=NULL)
Determines whether ptr is minimally aligned.
size_t BytesToWords(size_t byteCount)
Returns the number of words required for the specified number of bytes.
T rotlVariable(T x, unsigned int y)
Performs a left rotate.
byte BitReverse(byte value)
Reverses bits in a 8-bit value.
void SecureWipeArray(T *buf, size_t n)
Sets each element of an array to 0.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Uses encapsulation to hide an object in derived classes.
void * UnalignedAllocate(size_t size)
Allocates a buffer.
Manages resources for a single object.
unsigned int TrailingZeros(word32 v)
Determines the number of trailing 0-bits in a value.
void CallNewHandler()
Attempts to reclaim unused memory.
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianess.
void ConditionalSwapPointers(bool c, T &a, T &b)
Performs a branchless swap of pointers a and b if condition c is true.
Multiple precision integer with arithmetic operations.
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
bool IsPowerOf2(const T &value)
Tests whether a value is a power of 2.
#define MEMORY_BARRIER
A memory barrier.
void * memset_z(void *ptr, int value, size_t num)
Memory block initializer and eraser that attempts to survive optimizations.
unsigned int Parity(T value)
Returns the parity of a value.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
std::string IntToString< unsigned long long >(unsigned long long value, unsigned int base)
Converts an unsigned value to a string.
void ConditionalSwap(bool c, T &a, T &b)
Performs a branchless swap of values a and b if condition c is true.
T1 SaturatingSubtract1(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 1.
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
bool VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
ByteOrder GetNativeByteOrder()
Returns NativeByteOrder as an enumerated ByteOrder value.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
std::string IntToString< Integer >(Integer value, unsigned int base)
Converts an Integer to a string.
Crypto++ library namespace.
T rotrVariable(T x, unsigned int y)
Performs a right rotate.
T rotlMod(T x, unsigned int y)
Performs a left rotate.
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
T rotrMod(T x, unsigned int y)
Performs a right rotate.
T rotrFixed(T x, unsigned int y)
Performs a right rotate.
Ensures an object is not copyable.
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
void UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
const T & Ref(...) const
Return a reference to the inner Singleton object.
size_t BitsToDwords(size_t bitCount)
Returns the number of double words required for the specified number of bits.
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
void * AlignedAllocate(size_t size)
Allocates a buffer on 16-byte boundary.
#define SIZE_MAX
The maximum value of a machine word.