00001
00002
00003
00004
00005
00006 #ifndef CRYPTOPP_MISC_H
00007 #define CRYPTOPP_MISC_H
00008
00009 #include "config.h"
00010
00011 #if !CRYPTOPP_DOXYGEN_PROCESSING
00012
00013 #if CRYPTOPP_MSC_VERSION
00014 # pragma warning(push)
00015 # pragma warning(disable: 4146)
00016 # if (CRYPTOPP_MSC_VERSION >= 1400)
00017 # pragma warning(disable: 6326)
00018 # endif
00019 #endif
00020
00021 #include "cryptlib.h"
00022 #include "stdcpp.h"
00023 #include "smartptr.h"
00024
00025 #ifdef _MSC_VER
00026 #if _MSC_VER >= 1400
00027
00028 #define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
00029 #define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
00030 #define _interlockedbittestandset64 CRYPTOPP_DISABLED_INTRINSIC_3
00031 #define _interlockedbittestandreset64 CRYPTOPP_DISABLED_INTRINSIC_4
00032 #include <intrin.h>
00033 #undef _interlockedbittestandset
00034 #undef _interlockedbittestandreset
00035 #undef _interlockedbittestandset64
00036 #undef _interlockedbittestandreset64
00037 #define CRYPTOPP_FAST_ROTATE(x) 1
00038 #elif _MSC_VER >= 1300
00039 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
00040 #else
00041 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
00042 #endif
00043 #elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
00044 (defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
00045 #define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
00046 #elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions
00047 #define CRYPTOPP_FAST_ROTATE(x) 1
00048 #else
00049 #define CRYPTOPP_FAST_ROTATE(x) 0
00050 #endif
00051
00052 #ifdef __BORLANDC__
00053 #include <mem.h>
00054 #endif
00055
00056 #if defined(__GNUC__) && defined(__linux__)
00057 #define CRYPTOPP_BYTESWAP_AVAILABLE
00058 #include <byteswap.h>
00059 #endif
00060
00061 #endif // CRYPTOPP_DOXYGEN_PROCESSING
00062
00063 #if CRYPTOPP_DOXYGEN_PROCESSING
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 # define SIZE_MAX ...
00074 #else
00075
00076
00077
00078 #ifndef SIZE_MAX
00079 # if defined(__SIZE_MAX__)
00080 # define SIZE_MAX __SIZE_MAX__
00081 # elif defined(SIZE_T_MAX)
00082 # define SIZE_MAX SIZE_T_MAX
00083 # else
00084 # define SIZE_MAX ((std::numeric_limits<size_t>::max)())
00085 # endif
00086 #endif
00087
00088 #endif // CRYPTOPP_DOXYGEN_PROCESSING
00089
00090 NAMESPACE_BEGIN(CryptoPP)
00091
00092
00093 class Integer;
00094
00095
00096
00097 #if CRYPTOPP_DOXYGEN_PROCESSING
00098
00099
00100
00101 #define CRYPTOPP_COMPILE_ASSERT(expr) ...
00102 #else // CRYPTOPP_DOXYGEN_PROCESSING
00103 template <bool b>
00104 struct CompileAssert
00105 {
00106 static char dummy[2*b-1];
00107 };
00108
00109
00110 #define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
00111 #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
00112 #define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
00113 #else
00114 # if defined(__GNUC__)
00115 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
00116 static CompileAssert<(assertion)> \
00117 CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance) __attribute__ ((unused))
00118 # else
00119 # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \
00120 static CompileAssert<(assertion)> \
00121 CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance)
00122 # endif // __GNUC__
00123 #endif
00124 #define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
00125 #define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
00126
00127 #endif // CRYPTOPP_DOXYGEN_PROCESSING
00128
00129
00130
00131 #if CRYPTOPP_DOXYGEN_PROCESSING
00132
00133
00134
00135
00136
00137
00138
00139
00140 # define COUNTOF(arr)
00141 #else
00142
00143 #ifndef COUNTOF
00144 # if defined(_MSC_VER) && (_MSC_VER >= 1400)
00145 # define COUNTOF(x) _countof(x)
00146 # else
00147 # define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
00148 # endif
00149 #endif // COUNTOF
00150 #endif // CRYPTOPP_DOXYGEN_PROCESSING
00151
00152
00153
00154 #if !CRYPTOPP_DOXYGEN_PROCESSING
00155 class CRYPTOPP_DLL Empty
00156 {
00157 };
00158
00159 template <class BASE1, class BASE2>
00160 class CRYPTOPP_NO_VTABLE TwoBases : public BASE1, public BASE2
00161 {
00162 };
00163
00164 template <class BASE1, class BASE2, class BASE3>
00165 class CRYPTOPP_NO_VTABLE ThreeBases : public BASE1, public BASE2, public BASE3
00166 {
00167 };
00168 #endif // CRYPTOPP_DOXYGEN_PROCESSING
00169
00170
00171
00172
00173
00174 template <class T>
00175 class ObjectHolder
00176 {
00177 protected:
00178 T m_object;
00179 };
00180
00181
00182
00183
00184
00185
00186
00187 class NotCopyable
00188 {
00189 public:
00190 NotCopyable() {}
00191 private:
00192 NotCopyable(const NotCopyable &);
00193 void operator=(const NotCopyable &);
00194 };
00195
00196
00197
00198
00199 template <class T>
00200 struct NewObject
00201 {
00202 T* operator()() const {return new T;}
00203 };
00204
00205 #if CRYPTOPP_DOXYGEN_PROCESSING
00206
00207
00208
00209
00210
00211
00212
00213 #define MEMORY_BARRIER ...
00214 #else
00215 #if (_MSC_VER >= 1400)
00216 # pragma intrinsic(_ReadWriteBarrier)
00217 # define MEMORY_BARRIER() _ReadWriteBarrier()
00218 #elif defined(__INTEL_COMPILER)
00219 # define MEMORY_BARRIER() __memory_barrier()
00220 #elif defined(__GNUC__) || defined(__clang__)
00221 # define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
00222 #else
00223 # define MEMORY_BARRIER()
00224 #endif
00225 #endif // CRYPTOPP_DOXYGEN_PROCESSING
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 template <class T, class F = NewObject<T>, int instance=0>
00236 class Singleton
00237 {
00238 public:
00239 Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
00240
00241
00242 CRYPTOPP_NOINLINE const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const;
00243
00244 private:
00245 F m_objectFactory;
00246 };
00247
00248
00249
00250
00251 template <class T, class F, int instance>
00252 const T & Singleton<T, F, instance>::Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
00253 {
00254 static volatile simple_ptr<T> s_pObject;
00255 T *p = s_pObject.m_p;
00256 MEMORY_BARRIER();
00257
00258 if (p)
00259 return *p;
00260
00261 T *newObject = m_objectFactory();
00262 p = s_pObject.m_p;
00263 MEMORY_BARRIER();
00264
00265 if (p)
00266 {
00267 delete newObject;
00268 return *p;
00269 }
00270
00271 s_pObject.m_p = newObject;
00272 MEMORY_BARRIER();
00273
00274 return *newObject;
00275 }
00276
00277
00278
00279 #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 inline void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
00300 {
00301
00302
00303
00304 assert(dest != NULL); assert(src != NULL);
00305
00306 assert(sizeInBytes >= count);
00307 if (count > sizeInBytes)
00308 throw InvalidArgument("memcpy_s: buffer overflow");
00309
00310 #if CRYPTOPP_MSC_VERSION
00311 # pragma warning(push)
00312 # pragma warning(disable: 4996)
00313 # if (CRYPTOPP_MSC_VERSION >= 1400)
00314 # pragma warning(disable: 6386)
00315 # endif
00316 #endif
00317 memcpy(dest, src, count);
00318 #if CRYPTOPP_MSC_VERSION
00319 # pragma warning(pop)
00320 #endif
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 inline void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
00342 {
00343
00344
00345
00346 assert(dest != NULL); assert(src != NULL);
00347
00348 assert(sizeInBytes >= count);
00349 if (count > sizeInBytes)
00350 throw InvalidArgument("memmove_s: buffer overflow");
00351
00352 #if CRYPTOPP_MSC_VERSION
00353 # pragma warning(push)
00354 # pragma warning(disable: 4996)
00355 # if (CRYPTOPP_MSC_VERSION >= 1400)
00356 # pragma warning(disable: 6386)
00357 # endif
00358 #endif
00359 memmove(dest, src, count);
00360 #if CRYPTOPP_MSC_VERSION
00361 # pragma warning(pop)
00362 #endif
00363 }
00364
00365 #if __BORLANDC__ >= 0x620
00366
00367 # define memcpy_s CryptoPP::memcpy_s
00368 # define memmove_s CryptoPP::memmove_s
00369 #endif
00370
00371 #endif // __STDC_WANT_SECURE_LIB__
00372
00373
00374
00375
00376
00377
00378
00379 inline void * memset_z(void *ptr, int value, size_t num)
00380 {
00381
00382 #if CRYPTOPP_GCC_VERSION >= 30001
00383 if (__builtin_constant_p(num) && num==0)
00384 return ptr;
00385 #endif
00386 volatile void* x = memset(ptr, value, num);
00387 return const_cast<void*>(x);
00388 }
00389
00390
00391
00392
00393
00394
00395 template <class T> inline const T& STDMIN(const T& a, const T& b)
00396 {
00397 return b < a ? b : a;
00398 }
00399
00400
00401
00402
00403
00404
00405 template <class T> inline const T& STDMAX(const T& a, const T& b)
00406 {
00407
00408 return a < b ? b : a;
00409 }
00410
00411 #if CRYPTOPP_MSC_VERSION
00412 # pragma warning(push)
00413 # pragma warning(disable: 4389)
00414 #endif
00415
00416 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
00417 # pragma GCC diagnostic push
00418 # pragma GCC diagnostic ignored "-Wsign-compare"
00419 # if (CRYPTOPP_CLANG_VERSION >= 20800) || (CRYPTOPP_APPLE_CLANG_VERSION >= 30000)
00420 # pragma GCC diagnostic ignored "-Wtautological-compare"
00421 # elif (CRYPTOPP_GCC_VERSION >= 40300)
00422 # pragma GCC diagnostic ignored "-Wtype-limits"
00423 # endif
00424 #endif
00425
00426
00427
00428
00429
00430
00431 template <class T1, class T2> inline const T1 UnsignedMin(const T1& a, const T2& b)
00432 {
00433 CRYPTOPP_COMPILE_ASSERT((sizeof(T1)<=sizeof(T2) && T2(-1)>0) || (sizeof(T1)>sizeof(T2) && T1(-1)>0));
00434 if (sizeof(T1)<=sizeof(T2))
00435 return b < (T2)a ? (T1)b : a;
00436 else
00437 return (T1)b < a ? (T1)b : a;
00438 }
00439
00440
00441
00442
00443
00444 template <class T1, class T2>
00445 inline bool SafeConvert(T1 from, T2 &to)
00446 {
00447 to = (T2)from;
00448 if (from != to || (from > 0) != (to > 0))
00449 return false;
00450 return true;
00451 }
00452
00453
00454
00455
00456
00457 template <class T>
00458 std::string IntToString(T value, unsigned int base = 10)
00459 {
00460
00461 static const unsigned int HIGH_BIT = (1U << 31);
00462 const char CH = !!(base & HIGH_BIT) ? 'A' : 'a';
00463 base &= ~HIGH_BIT;
00464
00465 assert(base >= 2);
00466 if (value == 0)
00467 return "0";
00468
00469 bool negate = false;
00470 if (value < 0)
00471 {
00472 negate = true;
00473 value = 0-value;
00474 }
00475 std::string result;
00476 while (value > 0)
00477 {
00478 T digit = value % base;
00479 result = char((digit < 10 ? '0' : (CH - 10)) + digit) + result;
00480 value /= base;
00481 }
00482 if (negate)
00483 result = "-" + result;
00484 return result;
00485 }
00486
00487
00488
00489
00490
00491
00492
00493 template <> CRYPTOPP_DLL
00494 std::string IntToString<unsigned long long>(unsigned long long value, unsigned int base);
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 template <> CRYPTOPP_DLL
00517 std::string IntToString<Integer>(Integer value, unsigned int base);
00518
00519 #if CRYPTOPP_MSC_VERSION
00520 # pragma warning(pop)
00521 #endif
00522
00523 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
00524 # pragma GCC diagnostic pop
00525 #endif
00526
00527 #define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
00528
00529
00530 #define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
00531
00532
00533
00534
00535 #define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
00536
00537
00538
00539
00540 template <class T>
00541 unsigned int Parity(T value)
00542 {
00543 for (unsigned int i=8*sizeof(value)/2; i>0; i/=2)
00544 value ^= value >> i;
00545 return (unsigned int)value&1;
00546 }
00547
00548
00549
00550
00551 template <class T>
00552 unsigned int BytePrecision(const T &value)
00553 {
00554 if (!value)
00555 return 0;
00556
00557 unsigned int l=0, h=8*sizeof(value);
00558 while (h-l > 8)
00559 {
00560 unsigned int t = (l+h)/2;
00561 if (value >> t)
00562 l = t;
00563 else
00564 h = t;
00565 }
00566
00567 return h/8;
00568 }
00569
00570
00571
00572
00573 template <class T>
00574 unsigned int BitPrecision(const T &value)
00575 {
00576 if (!value)
00577 return 0;
00578
00579 unsigned int l=0, h=8*sizeof(value);
00580
00581 while (h-l > 1)
00582 {
00583 unsigned int t = (l+h)/2;
00584 if (value >> t)
00585 l = t;
00586 else
00587 h = t;
00588 }
00589
00590 return h;
00591 }
00592
00593
00594
00595
00596
00597
00598
00599 inline unsigned int TrailingZeros(word32 v)
00600 {
00601 assert(v != 0);
00602 #if defined(__GNUC__) && CRYPTOPP_GCC_VERSION >= 30400
00603 return __builtin_ctz(v);
00604 #elif defined(_MSC_VER) && _MSC_VER >= 1400
00605 unsigned long result;
00606 _BitScanForward(&result, v);
00607 return result;
00608 #else
00609
00610 static const int MultiplyDeBruijnBitPosition[32] =
00611 {
00612 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
00613 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
00614 };
00615 return MultiplyDeBruijnBitPosition[((word32)((v & -v) * 0x077CB531U)) >> 27];
00616 #endif
00617 }
00618
00619
00620
00621
00622
00623
00624
00625 inline unsigned int TrailingZeros(word64 v)
00626 {
00627 assert(v != 0);
00628 #if defined(__GNUC__) && CRYPTOPP_GCC_VERSION >= 30400
00629 return __builtin_ctzll(v);
00630 #elif defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(_M_X64) || defined(_M_IA64))
00631 unsigned long result;
00632 _BitScanForward64(&result, v);
00633 return result;
00634 #else
00635 return word32(v) ? TrailingZeros(word32(v)) : 32 + TrailingZeros(word32(v>>32));
00636 #endif
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646 template <class T>
00647 inline T Crop(T value, size_t bits)
00648 {
00649 if (bits < 8*sizeof(value))
00650 return T(value & ((T(1) << bits) - 1));
00651 else
00652 return value;
00653 }
00654
00655
00656
00657
00658
00659 inline size_t BitsToBytes(size_t bitCount)
00660 {
00661 return ((bitCount+7)/(8));
00662 }
00663
00664
00665
00666
00667
00668
00669 inline size_t BytesToWords(size_t byteCount)
00670 {
00671 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
00672 }
00673
00674
00675
00676
00677
00678
00679 inline size_t BitsToWords(size_t bitCount)
00680 {
00681 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
00682 }
00683
00684
00685
00686
00687
00688
00689 inline size_t BitsToDwords(size_t bitCount)
00690 {
00691 return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
00692 }
00693
00694
00695
00696
00697
00698
00699
00700 CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *buf, const byte *mask, size_t count);
00701
00702
00703
00704
00705
00706
00707
00708
00709 CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *output, const byte *input, const byte *mask, size_t count);
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 CRYPTOPP_DLL bool CRYPTOPP_API VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count);
00720
00721
00722
00723
00724
00725
00726 template <class T>
00727 inline bool IsPowerOf2(const T &value)
00728 {
00729 return value > 0 && (value & (value-1)) == 0;
00730 }
00731
00732
00733
00734
00735
00736
00737
00738 template <class T1, class T2>
00739 inline T2 ModPowerOf2(const T1 &a, const T2 &b)
00740 {
00741 assert(IsPowerOf2(b));
00742 return T2(a) & (b-1);
00743 }
00744
00745
00746
00747
00748
00749
00750
00751 template <class T1, class T2>
00752 inline T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
00753 {
00754 if (IsPowerOf2(m))
00755 return n - ModPowerOf2(n, m);
00756 else
00757 return n - n%m;
00758 }
00759
00760
00761
00762
00763
00764
00765
00766
00767 template <class T1, class T2>
00768 inline T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
00769 {
00770 if (n > (SIZE_MAX/sizeof(T1))-m-1)
00771 throw InvalidArgument("RoundUpToMultipleOf: integer overflow");
00772 return RoundDownToMultipleOf(T1(n+m-1), m);
00773 }
00774
00775
00776
00777
00778
00779
00780
00781
00782 template <class T>
00783 inline unsigned int GetAlignmentOf(T *dummy=NULL)
00784 {
00785
00786 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS)
00787 if (sizeof(T) < 16)
00788 return 1;
00789 #endif
00790 CRYPTOPP_UNUSED(dummy);
00791 #if defined(CRYPTOPP_CXX11_ALIGNOF)
00792 return alignof(T);
00793 #elif (_MSC_VER >= 1300)
00794 return __alignof(T);
00795 #elif defined(__GNUC__)
00796 return __alignof__(T);
00797 #elif CRYPTOPP_BOOL_SLOW_WORD64
00798 return UnsignedMin(4U, sizeof(T));
00799 #else
00800 return sizeof(T);
00801 #endif
00802 }
00803
00804
00805
00806
00807
00808
00809
00810 inline bool IsAlignedOn(const void *ptr, unsigned int alignment)
00811 {
00812 return alignment==1 || (IsPowerOf2(alignment) ? ModPowerOf2((size_t)ptr, alignment) == 0 : (size_t)ptr % alignment == 0);
00813 }
00814
00815
00816
00817
00818
00819
00820 template <class T>
00821 inline bool IsAligned(const void *ptr, T *dummy=NULL)
00822 {
00823 CRYPTOPP_UNUSED(dummy);
00824 return IsAlignedOn(ptr, GetAlignmentOf<T>());
00825 }
00826
00827 #if defined(IS_LITTLE_ENDIAN)
00828 typedef LittleEndian NativeByteOrder;
00829 #elif defined(IS_BIG_ENDIAN)
00830 typedef BigEndian NativeByteOrder;
00831 #else
00832 # error "Unable to determine endian-ness"
00833 #endif
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843 inline ByteOrder GetNativeByteOrder()
00844 {
00845 return NativeByteOrder::ToEnum();
00846 }
00847
00848
00849
00850
00851 inline bool NativeByteOrderIs(ByteOrder order)
00852 {
00853 return order == GetNativeByteOrder();
00854 }
00855
00856
00857
00858
00859
00860
00861
00862
00863 template <class T1, class T2>
00864 inline T1 SaturatingSubtract(const T1 &a, const T2 &b)
00865 {
00866
00867 return T1((a > b) ? (a - b) : 0);
00868 }
00869
00870
00871
00872
00873
00874
00875
00876
00877 template <class T1, class T2>
00878 inline T1 SaturatingSubtract1(const T1 &a, const T2 &b)
00879 {
00880
00881 return T1((a > b) ? (a - b) : 1);
00882 }
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892 template <class T>
00893 inline CipherDir GetCipherDir(const T &obj)
00894 {
00895 return obj.IsForwardTransformation() ? ENCRYPTION : DECRYPTION;
00896 }
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906 CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler();
00907
00908
00909
00910
00911
00912
00913
00914 inline void IncrementCounterByOne(byte *inout, unsigned int size)
00915 {
00916 assert(inout != NULL); assert(size < INT_MAX);
00917 for (int i=int(size-1), carry=1; i>=0 && carry; i--)
00918 carry = !++inout[i];
00919 }
00920
00921
00922
00923
00924
00925
00926
00927
00928 inline void IncrementCounterByOne(byte *output, const byte *input, unsigned int size)
00929 {
00930 assert(output != NULL); assert(input != NULL); assert(size < INT_MAX);
00931
00932 int i, carry;
00933 for (i=int(size-1), carry=1; i>=0 && carry; i--)
00934 carry = ((output[i] = input[i]+1) == 0);
00935 memcpy_s(output, size, input, i+1);
00936 }
00937
00938
00939
00940
00941
00942 template <class T>
00943 inline void ConditionalSwap(bool c, T &a, T &b)
00944 {
00945 T t = c * (a ^ b);
00946 a ^= t;
00947 b ^= t;
00948 }
00949
00950
00951
00952
00953
00954 template <class T>
00955 inline void ConditionalSwapPointers(bool c, T &a, T &b)
00956 {
00957 ptrdiff_t t = size_t(c) * (a - b);
00958 a -= t;
00959 b += t;
00960 }
00961
00962
00963
00964
00965
00966
00967
00968
00969 template <class T>
00970 void SecureWipeBuffer(T *buf, size_t n)
00971 {
00972
00973 volatile T *p = buf+n;
00974 while (n--)
00975 *((volatile T*)(--p)) = 0;
00976 }
00977
00978 #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
00979
00980
00981
00982
00983
00984 template<> inline void SecureWipeBuffer(byte *buf, size_t n)
00985 {
00986 volatile byte *p = buf;
00987 #ifdef __GNUC__
00988 asm volatile("rep stosb" : "+c"(n), "+D"(p) : "a"(0) : "memory");
00989 #else
00990 __stosb((byte *)(size_t)p, 0, n);
00991 #endif
00992 }
00993
00994
00995
00996
00997
00998 template<> inline void SecureWipeBuffer(word16 *buf, size_t n)
00999 {
01000 volatile word16 *p = buf;
01001 #ifdef __GNUC__
01002 asm volatile("rep stosw" : "+c"(n), "+D"(p) : "a"(0) : "memory");
01003 #else
01004 __stosw((word16 *)(size_t)p, 0, n);
01005 #endif
01006 }
01007
01008
01009
01010
01011
01012 template<> inline void SecureWipeBuffer(word32 *buf, size_t n)
01013 {
01014 volatile word32 *p = buf;
01015 #ifdef __GNUC__
01016 asm volatile("rep stosl" : "+c"(n), "+D"(p) : "a"(0) : "memory");
01017 #else
01018 __stosd((unsigned long *)(size_t)p, 0, n);
01019 #endif
01020 }
01021
01022
01023
01024
01025
01026 template<> inline void SecureWipeBuffer(word64 *buf, size_t n)
01027 {
01028 #if CRYPTOPP_BOOL_X64
01029 volatile word64 *p = buf;
01030 #ifdef __GNUC__
01031 asm volatile("rep stosq" : "+c"(n), "+D"(p) : "a"(0) : "memory");
01032 #else
01033 __stosq((word64 *)(size_t)p, 0, n);
01034 #endif
01035 #else
01036 SecureWipeBuffer((word32 *)buf, 2*n);
01037 #endif
01038 }
01039
01040 #endif // #if (_MSC_VER >= 1400 || defined(__GNUC__)) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86)
01041
01042
01043
01044
01045
01046 template <class T>
01047 inline void SecureWipeArray(T *buf, size_t n)
01048 {
01049 if (sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
01050 SecureWipeBuffer((word64 *)buf, n * (sizeof(T)/8));
01051 else if (sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
01052 SecureWipeBuffer((word32 *)buf, n * (sizeof(T)/4));
01053 else if (sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
01054 SecureWipeBuffer((word16 *)buf, n * (sizeof(T)/2));
01055 else
01056 SecureWipeBuffer((byte *)buf, n * sizeof(T));
01057 }
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
01072 static inline std::string StringNarrow(const wchar_t *str, bool throwOnError = true)
01073 #else
01074 static std::string StringNarrow(const wchar_t *str, bool throwOnError = true)
01075 #endif
01076 {
01077 assert(str);
01078 std::string result;
01079
01080
01081 #if (CRYPTOPP_MSC_VERSION >= 1400)
01082 size_t len=0, size = 0;
01083 errno_t err = 0;
01084
01085
01086
01087 len = wcslen(str)+1;
01088
01089 err = wcstombs_s(&size, NULL, 0, str, len*sizeof(wchar_t));
01090 assert(err == 0);
01091 if (err != 0) {goto CONVERSION_ERROR;}
01092
01093 result.resize(size);
01094 err = wcstombs_s(&size, &result[0], size, str, len*sizeof(wchar_t));
01095 assert(err == 0);
01096
01097 if (err != 0)
01098 {
01099 CONVERSION_ERROR:
01100 if (throwOnError)
01101 throw InvalidArgument("StringNarrow: wcstombs_s() call failed with error " + IntToString(err));
01102 else
01103 return std::string();
01104 }
01105
01106
01107 if (!result.empty() && result[size - 1] == '\0')
01108 result.erase(size - 1);
01109 #else
01110 size_t size = wcstombs(NULL, str, 0);
01111 assert(size != (size_t)-1);
01112 if (size == (size_t)-1) {goto CONVERSION_ERROR;}
01113
01114 result.resize(size);
01115 size = wcstombs(&result[0], str, size);
01116 assert(size != (size_t)-1);
01117
01118 if (size == (size_t)-1)
01119 {
01120 CONVERSION_ERROR:
01121 if (throwOnError)
01122 throw InvalidArgument("StringNarrow: wcstombs() call failed");
01123 else
01124 return std::string();
01125 }
01126 #endif
01127
01128 return result;
01129 }
01130
01131 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141 CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size);
01142
01143
01144
01145
01146
01147 CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr);
01148
01149 #endif // CRYPTOPP_DOXYGEN_PROCESSING
01150
01151 #if CRYPTOPP_BOOL_ALIGN16
01152 CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size);
01153 CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr);
01154 #endif // CRYPTOPP_BOOL_ALIGN16
01155
01156
01157
01158 CRYPTOPP_DLL void * CRYPTOPP_API UnalignedAllocate(size_t size);
01159
01160
01161
01162 CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr);
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175 template <class T> inline T rotlFixed(T x, unsigned int y)
01176 {
01177
01178
01179
01180
01181
01182 static const unsigned int THIS_SIZE = sizeof(T)*8;
01183 static const unsigned int MASK = THIS_SIZE-1;
01184
01185 assert(y < THIS_SIZE);
01186 return T((x<<y)|(x>>(-y&MASK)));
01187 }
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198 template <class T> inline T rotrFixed(T x, unsigned int y)
01199 {
01200
01201
01202
01203
01204 static const unsigned int THIS_SIZE = sizeof(T)*8;
01205 static const unsigned int MASK = THIS_SIZE-1;
01206 assert(y < THIS_SIZE);
01207 return T((x >> y)|(x<<(-y&MASK)));
01208 }
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219 template <class T> inline T rotlVariable(T x, unsigned int y)
01220 {
01221 static const unsigned int THIS_SIZE = sizeof(T)*8;
01222 static const unsigned int MASK = THIS_SIZE-1;
01223 assert(y < THIS_SIZE);
01224 return T((x<<y)|(x>>(-y&MASK)));
01225 }
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236 template <class T> inline T rotrVariable(T x, unsigned int y)
01237 {
01238 static const unsigned int THIS_SIZE = sizeof(T)*8;
01239 static const unsigned int MASK = THIS_SIZE-1;
01240 assert(y < THIS_SIZE);
01241 return T((x>>y)|(x<<(-y&MASK)));
01242 }
01243
01244
01245
01246
01247
01248
01249
01250 template <class T> inline T rotlMod(T x, unsigned int y)
01251 {
01252 static const unsigned int THIS_SIZE = sizeof(T)*8;
01253 static const unsigned int MASK = THIS_SIZE-1;
01254 return T((x<<(y&MASK))|(x>>(-y&MASK)));
01255 }
01256
01257
01258
01259
01260
01261
01262
01263 template <class T> inline T rotrMod(T x, unsigned int y)
01264 {
01265 static const unsigned int THIS_SIZE = sizeof(T)*8;
01266 static const unsigned int MASK = THIS_SIZE-1;
01267 return T((x>>(y&MASK))|(x<<(-y&MASK)));
01268 }
01269
01270 #ifdef _MSC_VER
01271
01272
01273
01274
01275
01276
01277
01278
01279 template<> inline word32 rotlFixed<word32>(word32 x, unsigned int y)
01280 {
01281
01282 assert(y < 8*sizeof(x));
01283 return y ? _lrotl(x, static_cast<byte>(y)) : x;
01284 }
01285
01286
01287
01288
01289
01290
01291
01292
01293 template<> inline word32 rotrFixed<word32>(word32 x, unsigned int y)
01294 {
01295
01296 assert(y < 8*sizeof(x));
01297 return y ? _lrotr(x, static_cast<byte>(y)) : x;
01298 }
01299
01300
01301
01302
01303
01304
01305
01306
01307 template<> inline word32 rotlVariable<word32>(word32 x, unsigned int y)
01308 {
01309 assert(y < 8*sizeof(x));
01310 return _lrotl(x, static_cast<byte>(y));
01311 }
01312
01313
01314
01315
01316
01317
01318
01319
01320 template<> inline word32 rotrVariable<word32>(word32 x, unsigned int y)
01321 {
01322 assert(y < 8*sizeof(x));
01323 return _lrotr(x, static_cast<byte>(y));
01324 }
01325
01326
01327
01328
01329
01330
01331
01332 template<> inline word32 rotlMod<word32>(word32 x, unsigned int y)
01333 {
01334 y %= 8*sizeof(x);
01335 return _lrotl(x, static_cast<byte>(y));
01336 }
01337
01338
01339
01340
01341
01342
01343
01344 template<> inline word32 rotrMod<word32>(word32 x, unsigned int y)
01345 {
01346 y %= 8*sizeof(x);
01347 return _lrotr(x, static_cast<byte>(y));
01348 }
01349
01350 #endif // #ifdef _MSC_VER
01351
01352 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362 template<> inline word64 rotlFixed<word64>(word64 x, unsigned int y)
01363 {
01364
01365 assert(y < 8*sizeof(x));
01366 return y ? _rotl64(x, static_cast<byte>(y)) : x;
01367 }
01368
01369
01370
01371
01372
01373
01374
01375
01376 template<> inline word64 rotrFixed<word64>(word64 x, unsigned int y)
01377 {
01378
01379 assert(y < 8*sizeof(x));
01380 return y ? _rotr64(x, static_cast<byte>(y)) : x;
01381 }
01382
01383
01384
01385
01386
01387
01388
01389
01390 template<> inline word64 rotlVariable<word64>(word64 x, unsigned int y)
01391 {
01392 assert(y < 8*sizeof(x));
01393 return _rotl64(x, static_cast<byte>(y));
01394 }
01395
01396
01397
01398
01399
01400
01401
01402
01403 template<> inline word64 rotrVariable<word64>(word64 x, unsigned int y)
01404 {
01405 assert(y < 8*sizeof(x));
01406 return y ? _rotr64(x, static_cast<byte>(y)) : x;
01407 }
01408
01409
01410
01411
01412
01413
01414
01415 template<> inline word64 rotlMod<word64>(word64 x, unsigned int y)
01416 {
01417 assert(y < 8*sizeof(x));
01418 return y ? _rotl64(x, static_cast<byte>(y)) : x;
01419 }
01420
01421
01422
01423
01424
01425
01426
01427 template<> inline word64 rotrMod<word64>(word64 x, unsigned int y)
01428 {
01429 assert(y < 8*sizeof(x));
01430 return y ? _rotr64(x, static_cast<byte>(y)) : x;
01431 }
01432
01433 #endif // #if _MSC_VER >= 1310
01434
01435 #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
01436
01437
01438 template<> inline word16 rotlFixed<word16>(word16 x, unsigned int y)
01439 {
01440
01441 return _rotl16(x, static_cast<byte>(y));
01442 }
01443
01444 template<> inline word16 rotrFixed<word16>(word16 x, unsigned int y)
01445 {
01446
01447 return _rotr16(x, static_cast<byte>(y));
01448 }
01449
01450 template<> inline word16 rotlVariable<word16>(word16 x, unsigned int y)
01451 {
01452 return _rotl16(x, static_cast<byte>(y));
01453 }
01454
01455 template<> inline word16 rotrVariable<word16>(word16 x, unsigned int y)
01456 {
01457 return _rotr16(x, static_cast<byte>(y));
01458 }
01459
01460 template<> inline word16 rotlMod<word16>(word16 x, unsigned int y)
01461 {
01462 return _rotl16(x, static_cast<byte>(y));
01463 }
01464
01465 template<> inline word16 rotrMod<word16>(word16 x, unsigned int y)
01466 {
01467 return _rotr16(x, static_cast<byte>(y));
01468 }
01469
01470 template<> inline byte rotlFixed<byte>(byte x, unsigned int y)
01471 {
01472
01473 return _rotl8(x, static_cast<byte>(y));
01474 }
01475
01476 template<> inline byte rotrFixed<byte>(byte x, unsigned int y)
01477 {
01478
01479 return _rotr8(x, static_cast<byte>(y));
01480 }
01481
01482 template<> inline byte rotlVariable<byte>(byte x, unsigned int y)
01483 {
01484 return _rotl8(x, static_cast<byte>(y));
01485 }
01486
01487 template<> inline byte rotrVariable<byte>(byte x, unsigned int y)
01488 {
01489 return _rotr8(x, static_cast<byte>(y));
01490 }
01491
01492 template<> inline byte rotlMod<byte>(byte x, unsigned int y)
01493 {
01494 return _rotl8(x, static_cast<byte>(y));
01495 }
01496
01497 template<> inline byte rotrMod<byte>(byte x, unsigned int y)
01498 {
01499 return _rotr8(x, static_cast<byte>(y));
01500 }
01501
01502 #endif // #if _MSC_VER >= 1400
01503
01504 #if (defined(__MWERKS__) && TARGET_CPU_PPC)
01505
01506 template<> inline word32 rotlFixed<word32>(word32 x, unsigned int y)
01507 {
01508 assert(y < 32);
01509 return y ? __rlwinm(x,y,0,31) : x;
01510 }
01511
01512 template<> inline word32 rotrFixed<word32>(word32 x, unsigned int y)
01513 {
01514 assert(y < 32);
01515 return y ? __rlwinm(x,32-y,0,31) : x;
01516 }
01517
01518 template<> inline word32 rotlVariable<word32>(word32 x, unsigned int y)
01519 {
01520 assert(y < 32);
01521 return (__rlwnm(x,y,0,31));
01522 }
01523
01524 template<> inline word32 rotrVariable<word32>(word32 x, unsigned int y)
01525 {
01526 assert(y < 32);
01527 return (__rlwnm(x,32-y,0,31));
01528 }
01529
01530 template<> inline word32 rotlMod<word32>(word32 x, unsigned int y)
01531 {
01532 return (__rlwnm(x,y,0,31));
01533 }
01534
01535 template<> inline word32 rotrMod<word32>(word32 x, unsigned int y)
01536 {
01537 return (__rlwnm(x,32-y,0,31));
01538 }
01539
01540 #endif // #if (defined(__MWERKS__) && TARGET_CPU_PPC)
01541
01542
01543
01544
01545
01546
01547
01548 template <class T>
01549 inline unsigned int GetByte(ByteOrder order, T value, unsigned int index)
01550 {
01551 if (order == LITTLE_ENDIAN_ORDER)
01552 return GETBYTE(value, index);
01553 else
01554 return GETBYTE(value, sizeof(T)-index-1);
01555 }
01556
01557
01558
01559
01560 inline byte ByteReverse(byte value)
01561 {
01562 return value;
01563 }
01564
01565
01566
01567
01568
01569 inline word16 ByteReverse(word16 value)
01570 {
01571 #ifdef CRYPTOPP_BYTESWAP_AVAILABLE
01572 return bswap_16(value);
01573 #elif defined(_MSC_VER) && _MSC_VER >= 1300
01574 return _byteswap_ushort(value);
01575 #else
01576 return rotlFixed(value, 8U);
01577 #endif
01578 }
01579
01580
01581
01582
01583
01584 inline word32 ByteReverse(word32 value)
01585 {
01586 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE)
01587 __asm__ ("bswap %0" : "=r" (value) : "0" (value));
01588 return value;
01589 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
01590 return bswap_32(value);
01591 #elif defined(__MWERKS__) && TARGET_CPU_PPC
01592 return (word32)__lwbrx(&value,0);
01593 #elif _MSC_VER >= 1400 || (_MSC_VER >= 1300 && !defined(_DLL))
01594 return _byteswap_ulong(value);
01595 #elif CRYPTOPP_FAST_ROTATE(32)
01596
01597 return (rotrFixed(value, 8U) & 0xff00ff00) | (rotlFixed(value, 8U) & 0x00ff00ff);
01598 #else
01599
01600 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
01601 return rotlFixed(value, 16U);
01602 #endif
01603 }
01604
01605
01606
01607
01608
01609 inline word64 ByteReverse(word64 value)
01610 {
01611 #if defined(__GNUC__) && defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__x86_64__)
01612 __asm__ ("bswap %0" : "=r" (value) : "0" (value));
01613 return value;
01614 #elif defined(CRYPTOPP_BYTESWAP_AVAILABLE)
01615 return bswap_64(value);
01616 #elif defined(_MSC_VER) && _MSC_VER >= 1300
01617 return _byteswap_uint64(value);
01618 #elif CRYPTOPP_BOOL_SLOW_WORD64
01619 return (word64(ByteReverse(word32(value))) << 32) | ByteReverse(word32(value>>32));
01620 #else
01621 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
01622 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
01623 return rotlFixed(value, 32U);
01624 #endif
01625 }
01626
01627
01628
01629
01630 inline byte BitReverse(byte value)
01631 {
01632 value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
01633 value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
01634 return rotlFixed(value, 4U);
01635 }
01636
01637
01638
01639
01640 inline word16 BitReverse(word16 value)
01641 {
01642 value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1);
01643 value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2);
01644 value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4);
01645 return ByteReverse(value);
01646 }
01647
01648
01649
01650
01651 inline word32 BitReverse(word32 value)
01652 {
01653 value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1);
01654 value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2);
01655 value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4);
01656 return ByteReverse(value);
01657 }
01658
01659
01660
01661
01662 inline word64 BitReverse(word64 value)
01663 {
01664 #if CRYPTOPP_BOOL_SLOW_WORD64
01665 return (word64(BitReverse(word32(value))) << 32) | BitReverse(word32(value>>32));
01666 #else
01667 value = ((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | ((value & W64LIT(0x5555555555555555)) << 1);
01668 value = ((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | ((value & W64LIT(0x3333333333333333)) << 2);
01669 value = ((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | ((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
01670 return ByteReverse(value);
01671 #endif
01672 }
01673
01674
01675
01676
01677
01678
01679
01680 template <class T>
01681 inline T BitReverse(T value)
01682 {
01683 if (sizeof(T) == 1)
01684 return (T)BitReverse((byte)value);
01685 else if (sizeof(T) == 2)
01686 return (T)BitReverse((word16)value);
01687 else if (sizeof(T) == 4)
01688 return (T)BitReverse((word32)value);
01689 else
01690 {
01691 assert(sizeof(T) == 8);
01692 return (T)BitReverse((word64)value);
01693 }
01694 }
01695
01696
01697
01698
01699
01700
01701
01702
01703 template <class T>
01704 inline T ConditionalByteReverse(ByteOrder order, T value)
01705 {
01706 return NativeByteOrderIs(order) ? value : ByteReverse(value);
01707 }
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721 template <class T>
01722 void ByteReverse(T *out, const T *in, size_t byteCount)
01723 {
01724 assert(byteCount % sizeof(T) == 0);
01725 size_t count = byteCount/sizeof(T);
01726 for (size_t i=0; i<count; i++)
01727 out[i] = ByteReverse(in[i]);
01728 }
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743 template <class T>
01744 inline void ConditionalByteReverse(ByteOrder order, T *out, const T *in, size_t byteCount)
01745 {
01746 if (!NativeByteOrderIs(order))
01747 ByteReverse(out, in, byteCount);
01748 else if (in != out)
01749 memcpy_s(out, byteCount, in, byteCount);
01750 }
01751
01752 template <class T>
01753 inline void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
01754 {
01755 const size_t U = sizeof(T);
01756 assert(inlen <= outlen*U);
01757 memcpy_s(out, outlen*U, in, inlen);
01758 memset_z((byte *)out+inlen, 0, outlen*U-inlen);
01759 ConditionalByteReverse(order, out, out, RoundUpToMultipleOf(inlen, U));
01760 }
01761
01762 #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
01763 inline byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const byte *)
01764 {
01765 CRYPTOPP_UNUSED(order);
01766 return block[0];
01767 }
01768
01769 inline word16 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const word16 *)
01770 {
01771 return (order == BIG_ENDIAN_ORDER)
01772 ? block[1] | (block[0] << 8)
01773 : block[0] | (block[1] << 8);
01774 }
01775
01776 inline word32 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const word32 *)
01777 {
01778 return (order == BIG_ENDIAN_ORDER)
01779 ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
01780 : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
01781 }
01782
01783 inline word64 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, const word64 *)
01784 {
01785 return (order == BIG_ENDIAN_ORDER)
01786 ?
01787 (word64(block[7]) |
01788 (word64(block[6]) << 8) |
01789 (word64(block[5]) << 16) |
01790 (word64(block[4]) << 24) |
01791 (word64(block[3]) << 32) |
01792 (word64(block[2]) << 40) |
01793 (word64(block[1]) << 48) |
01794 (word64(block[0]) << 56))
01795 :
01796 (word64(block[0]) |
01797 (word64(block[1]) << 8) |
01798 (word64(block[2]) << 16) |
01799 (word64(block[3]) << 24) |
01800 (word64(block[4]) << 32) |
01801 (word64(block[5]) << 40) |
01802 (word64(block[6]) << 48) |
01803 (word64(block[7]) << 56));
01804 }
01805
01806 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, byte value, const byte *xorBlock)
01807 {
01808 CRYPTOPP_UNUSED(order);
01809 block[0] = xorBlock ? (value ^ xorBlock[0]) : value;
01810 }
01811
01812 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word16 value, const byte *xorBlock)
01813 {
01814 if (order == BIG_ENDIAN_ORDER)
01815 {
01816 if (xorBlock)
01817 {
01818 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01819 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01820 }
01821 else
01822 {
01823 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01824 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01825 }
01826 }
01827 else
01828 {
01829 if (xorBlock)
01830 {
01831 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01832 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01833 }
01834 else
01835 {
01836 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01837 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01838 }
01839 }
01840 }
01841
01842 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word32 value, const byte *xorBlock)
01843 {
01844 if (order == BIG_ENDIAN_ORDER)
01845 {
01846 if (xorBlock)
01847 {
01848 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01849 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01850 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01851 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01852 }
01853 else
01854 {
01855 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01856 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01857 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01858 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01859 }
01860 }
01861 else
01862 {
01863 if (xorBlock)
01864 {
01865 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01866 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01867 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01868 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01869 }
01870 else
01871 {
01872 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01873 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01874 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01875 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01876 }
01877 }
01878 }
01879
01880 inline void UnalignedbyteNonTemplate(ByteOrder order, byte *block, word64 value, const byte *xorBlock)
01881 {
01882 if (order == BIG_ENDIAN_ORDER)
01883 {
01884 if (xorBlock)
01885 {
01886 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
01887 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
01888 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
01889 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
01890 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01891 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01892 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01893 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01894 }
01895 else
01896 {
01897 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
01898 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
01899 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
01900 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
01901 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01902 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01903 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01904 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01905 }
01906 }
01907 else
01908 {
01909 if (xorBlock)
01910 {
01911 block[0] = xorBlock[0] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01912 block[1] = xorBlock[1] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01913 block[2] = xorBlock[2] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01914 block[3] = xorBlock[3] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01915 block[4] = xorBlock[4] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
01916 block[5] = xorBlock[5] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
01917 block[6] = xorBlock[6] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
01918 block[7] = xorBlock[7] ^ CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
01919 }
01920 else
01921 {
01922 block[0] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 0);
01923 block[1] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 1);
01924 block[2] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 2);
01925 block[3] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 3);
01926 block[4] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 4);
01927 block[5] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 5);
01928 block[6] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 6);
01929 block[7] = CRYPTOPP_GET_BYTE_AS_BYTE(value, 7);
01930 }
01931 }
01932 }
01933 #endif // #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
01934
01935 template <class T>
01936 inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
01937 {
01938
01939
01940
01941
01942
01943
01944 CRYPTOPP_UNUSED(assumeAligned);
01945 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
01946 return ConditionalByteReverse(order, *reinterpret_cast<const T *>(block));
01947 #else
01948 T temp;
01949 memcpy(&temp, block, sizeof(T));
01950 return ConditionalByteReverse(order, temp);
01951 #endif
01952 }
01953
01954 template <class T>
01955 inline void GetWord(bool assumeAligned, ByteOrder order, T &result, const byte *block)
01956 {
01957 result = GetWord<T>(assumeAligned, order, block);
01958 }
01959
01960 template <class T>
01961 inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock = NULL)
01962 {
01963
01964
01965
01966
01967
01968
01969
01970 CRYPTOPP_UNUSED(assumeAligned);
01971 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
01972 *reinterpret_cast<T *>(block) = ConditionalByteReverse(order, value) ^ (xorBlock ? *reinterpret_cast<const T *>(xorBlock) : 0);
01973 #else
01974 T t1, t2 = 0;
01975 t1 = ConditionalByteReverse(order, value);
01976 if (xorBlock) memcpy(&t2, xorBlock, sizeof(T));
01977 memmove(block, &(t1 ^= t2), sizeof(T));
01978 #endif
01979 }
01980
01981 template <class T, class B, bool A=false>
01982 class GetBlock
01983 {
01984 public:
01985 GetBlock(const void *block)
01986 : m_block((const byte *)block) {}
01987
01988 template <class U>
01989 inline GetBlock<T, B, A> & operator()(U &x)
01990 {
01991 CRYPTOPP_COMPILE_ASSERT(sizeof(U) >= sizeof(T));
01992 x = GetWord<T>(A, B::ToEnum(), m_block);
01993 m_block += sizeof(T);
01994 return *this;
01995 }
01996
01997 private:
01998 const byte *m_block;
01999 };
02000
02001 template <class T, class B, bool A=false>
02002 class PutBlock
02003 {
02004 public:
02005 PutBlock(const void *xorBlock, void *block)
02006 : m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
02007
02008 template <class U>
02009 inline PutBlock<T, B, A> & operator()(U x)
02010 {
02011 PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
02012 m_block += sizeof(T);
02013 if (m_xorBlock)
02014 m_xorBlock += sizeof(T);
02015 return *this;
02016 }
02017
02018 private:
02019 const byte *m_xorBlock;
02020 byte *m_block;
02021 };
02022
02023 template <class T, class B, bool GA=false, bool PA=false>
02024 struct BlockGetAndPut
02025 {
02026
02027 static inline GetBlock<T, B, GA> Get(const void *block) {return GetBlock<T, B, GA>(block);}
02028 typedef PutBlock<T, B, PA> Put;
02029 };
02030
02031 template <class T>
02032 std::string WordToString(T value, ByteOrder order = BIG_ENDIAN_ORDER)
02033 {
02034 if (!NativeByteOrderIs(order))
02035 value = ByteReverse(value);
02036
02037 return std::string((char *)&value, sizeof(value));
02038 }
02039
02040 template <class T>
02041 T StringToWord(const std::string &str, ByteOrder order = BIG_ENDIAN_ORDER)
02042 {
02043 T value = 0;
02044 memcpy_s(&value, sizeof(value), str.data(), UnsignedMin(str.size(), sizeof(value)));
02045 return NativeByteOrderIs(order) ? value : ByteReverse(value);
02046 }
02047
02048
02049
02050 template <bool overflow> struct SafeShifter;
02051
02052 template<> struct SafeShifter<true>
02053 {
02054 template <class T>
02055 static inline T RightShift(T value, unsigned int bits)
02056 {
02057 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
02058 return 0;
02059 }
02060
02061 template <class T>
02062 static inline T LeftShift(T value, unsigned int bits)
02063 {
02064 CRYPTOPP_UNUSED(value); CRYPTOPP_UNUSED(bits);
02065 return 0;
02066 }
02067 };
02068
02069 template<> struct SafeShifter<false>
02070 {
02071 template <class T>
02072 static inline T RightShift(T value, unsigned int bits)
02073 {
02074 return value >> bits;
02075 }
02076
02077 template <class T>
02078 static inline T LeftShift(T value, unsigned int bits)
02079 {
02080 return value << bits;
02081 }
02082 };
02083
02084 template <unsigned int bits, class T>
02085 inline T SafeRightShift(T value)
02086 {
02087 return SafeShifter<(bits>=(8*sizeof(T)))>::RightShift(value, bits);
02088 }
02089
02090 template <unsigned int bits, class T>
02091 inline T SafeLeftShift(T value)
02092 {
02093 return SafeShifter<(bits>=(8*sizeof(T)))>::LeftShift(value, bits);
02094 }
02095
02096
02097
02098 #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);}
02099 #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);}
02100 #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);}
02101 #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);}
02102 #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);}
02103 #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);}
02104 #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);}
02105 #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);}
02106 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
02107
02108 NAMESPACE_END
02109
02110 #if CRYPTOPP_MSC_VERSION
02111 # pragma warning(pop)
02112 #endif
02113
02114 #endif