diff --git a/algebra.cpp b/algebra.cpp index 59ff2de8..599ea329 100644 --- a/algebra.cpp +++ b/algebra.cpp @@ -236,12 +236,12 @@ struct WindowSlider exp >>= skipCount; windowBegin += skipCount; - expWindow = exp % (1 << windowSize); + expWindow = word32(exp % (word(1) << windowSize)); if (fastNegate && exp.GetBit(windowSize)) { negateNext = true; - expWindow = (1 << windowSize) - expWindow; + expWindow = (word32(1) << windowSize) - expWindow; exp += windowModulus; } else @@ -249,7 +249,8 @@ struct WindowSlider } Integer exp, windowModulus; - unsigned int windowSize, windowBegin, expWindow; + unsigned int windowSize, windowBegin; + word32 expWindow; bool fastNegate, negateNext, firstTime, finished; }; diff --git a/bench.cpp b/bench.cpp index 4e69c20a..6ba54314 100644 --- a/bench.cpp +++ b/bench.cpp @@ -1,5 +1,7 @@ // bench.cpp - written and placed in the public domain by Wei Dai +#define _CRT_SECURE_NO_DEPRECATE + #include "bench.h" #include "crc.h" #include "adler32.h" diff --git a/blumshub.h b/blumshub.h index 0fd0371f..6aab3261 100644 --- a/blumshub.h +++ b/blumshub.h @@ -28,10 +28,9 @@ public: bool IsForwardTransformation() const {return true;} protected: - const ModularArithmetic modn; - const word maxBits; + ModularArithmetic modn; + word maxBits, bitsLeft; Integer current; - int bitsLeft; friend class BlumGoldwasserPublicKey; friend class BlumGoldwasserPrivateKey; diff --git a/cryptlib.vcproj b/cryptlib.vcproj index bf0fb975..f622f95e 100755 --- a/cryptlib.vcproj +++ b/cryptlib.vcproj @@ -90,6 +90,79 @@ Name="VCPostBuildEventTool" /> + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -599,6 +608,15 @@ PreprocessorDefinitions="" /> + + + @@ -609,7 +627,7 @@ /> - - - - - - - - - - - - - - - @@ -702,6 +675,15 @@ Outputs="adhoc.cpp.copied" /> + + + @@ -711,6 +693,15 @@ Outputs="adhoc.cpp.copied" /> + + + @@ -720,6 +711,15 @@ Outputs="adhoc.cpp.copied" /> + + + @@ -742,6 +742,15 @@ PreprocessorDefinitions="" /> + + + @@ -751,6 +760,15 @@ PreprocessorDefinitions="" /> + + + @@ -761,7 +779,7 @@ /> - - - - - - + + + @@ -827,6 +836,15 @@ PreprocessorDefinitions="" /> + + + @@ -837,7 +855,7 @@ /> - - - - - - + + + @@ -903,6 +912,15 @@ PreprocessorDefinitions="" /> + + + @@ -913,7 +931,7 @@ /> - - - - - - + + + @@ -979,6 +988,15 @@ PreprocessorDefinitions="" /> + + + @@ -989,7 +1007,7 @@ /> - - - - - - + + + @@ -1055,6 +1064,15 @@ PreprocessorDefinitions="" /> + + + @@ -1065,7 +1083,7 @@ /> - - - - - - + + + @@ -1131,6 +1140,15 @@ PreprocessorDefinitions="" /> + + + @@ -1141,7 +1159,7 @@ /> - - - - - - + + + @@ -1207,6 +1216,15 @@ PreprocessorDefinitions="" /> + + + @@ -1217,7 +1235,7 @@ /> - - - - - - + + + @@ -1283,6 +1292,15 @@ PreprocessorDefinitions="" /> + + + @@ -1293,7 +1311,7 @@ /> - - - - - - + + + @@ -1359,6 +1368,15 @@ PreprocessorDefinitions="" /> + + + @@ -1369,7 +1387,7 @@ /> - - - - - - + + + @@ -1435,6 +1444,15 @@ PreprocessorDefinitions="" /> + + + @@ -1445,7 +1463,7 @@ /> - - - - - - + + + @@ -1511,6 +1520,15 @@ PreprocessorDefinitions="" /> + + + @@ -1521,7 +1539,7 @@ /> - - - - - - + + + @@ -1587,6 +1596,15 @@ PreprocessorDefinitions="" /> + + + @@ -1597,7 +1615,7 @@ /> - - - - - - + + + @@ -1663,6 +1672,15 @@ PreprocessorDefinitions="" /> + + + @@ -1673,7 +1691,7 @@ /> - - - - - - + + + @@ -1739,6 +1748,15 @@ PreprocessorDefinitions="" /> + + + @@ -1749,7 +1767,7 @@ /> - - - - - - + + + @@ -1815,6 +1824,15 @@ PreprocessorDefinitions="" /> + + + @@ -1825,7 +1843,7 @@ /> - - - - - - + + + @@ -1891,6 +1900,15 @@ PreprocessorDefinitions="" /> + + + @@ -1901,7 +1919,7 @@ /> - - - - - - + + + @@ -1967,6 +1976,15 @@ PreprocessorDefinitions="" /> + + + @@ -1977,7 +1995,7 @@ /> - - - - - - + + + @@ -2043,6 +2052,15 @@ PreprocessorDefinitions="" /> + + + @@ -2053,7 +2071,7 @@ /> - - - - - - + + + @@ -2119,6 +2128,15 @@ PreprocessorDefinitions="" /> + + + @@ -2129,7 +2147,7 @@ /> - - - - - - + + + @@ -2195,6 +2204,15 @@ PreprocessorDefinitions="" /> + + + @@ -2205,7 +2223,7 @@ /> - - - - - - + + + @@ -2271,6 +2280,15 @@ PreprocessorDefinitions="" /> + + + @@ -2281,7 +2299,7 @@ /> - - - - - - + + + @@ -2347,6 +2356,15 @@ PreprocessorDefinitions="" /> + + + @@ -2357,7 +2375,7 @@ /> - - - - - - + + + @@ -2423,6 +2432,15 @@ PreprocessorDefinitions="" /> + + + @@ -2433,7 +2451,7 @@ /> - - - - - - + + + @@ -2501,6 +2511,16 @@ UsePrecompiledHeader="0" /> + + + @@ -2512,7 +2532,7 @@ /> - - - - - - + + + @@ -2583,6 +2592,15 @@ PreprocessorDefinitions="" /> + + + @@ -2593,7 +2611,7 @@ /> - - - - - - + + + @@ -2659,6 +2668,15 @@ PreprocessorDefinitions="" /> + + + @@ -2669,7 +2687,7 @@ /> - - - - - - + + + + + + + + + - - - - - - - - - + + + @@ -2811,6 +2828,15 @@ PreprocessorDefinitions="" /> + + + @@ -2821,7 +2847,7 @@ /> - - - - - - + + + @@ -2887,6 +2904,15 @@ PreprocessorDefinitions="" /> + + + @@ -2897,7 +2923,7 @@ /> - - - - - - + + + @@ -2967,6 +2984,15 @@ PreprocessorDefinitions="" /> + + + @@ -2977,7 +3003,7 @@ /> - - - - - - + + + @@ -3043,6 +3060,15 @@ PreprocessorDefinitions="" /> + + + @@ -3053,7 +3079,7 @@ /> - - - - - - + + + @@ -3119,6 +3136,15 @@ PreprocessorDefinitions="" /> + + + @@ -3129,7 +3155,7 @@ /> - - - - - - + + + @@ -3195,6 +3212,15 @@ PreprocessorDefinitions="" /> + + + @@ -3205,7 +3231,7 @@ /> - - - - - - + + + @@ -3271,6 +3288,15 @@ PreprocessorDefinitions="" /> + + + @@ -3281,7 +3307,7 @@ /> - - - - - - + + + @@ -3347,6 +3364,15 @@ PreprocessorDefinitions="" /> + + + @@ -3357,7 +3383,7 @@ /> - - - - - - + + + @@ -3423,6 +3440,15 @@ PreprocessorDefinitions="" /> + + + @@ -3433,7 +3459,7 @@ /> - - - - - - + + + @@ -3499,6 +3516,15 @@ PreprocessorDefinitions="" /> + + + @@ -3509,7 +3535,7 @@ /> - - - - - - + + + @@ -3575,6 +3592,15 @@ PreprocessorDefinitions="" /> + + + @@ -3585,7 +3611,7 @@ /> - - - - - - + + + @@ -3651,6 +3668,15 @@ PreprocessorDefinitions="" /> + + + @@ -3661,7 +3687,7 @@ /> - - - - - - + + + @@ -3727,6 +3744,15 @@ PreprocessorDefinitions="" /> + + + @@ -3737,7 +3763,7 @@ /> - - - - - - + + + @@ -3803,6 +3820,15 @@ PreprocessorDefinitions="" /> + + + @@ -3813,7 +3839,7 @@ /> - - - - - - + + + @@ -3879,6 +3896,15 @@ PreprocessorDefinitions="" /> + + + @@ -3889,7 +3915,7 @@ /> - - - - - - + + + @@ -3955,6 +3972,15 @@ PreprocessorDefinitions="" /> + + + @@ -3965,7 +3991,7 @@ /> - - - - - - + + + @@ -4031,6 +4048,15 @@ PreprocessorDefinitions="" /> + + + @@ -4041,7 +4067,7 @@ /> - - - - - - + + + @@ -4107,6 +4124,15 @@ PreprocessorDefinitions="" /> + + + @@ -4117,7 +4143,7 @@ /> - - - - - - + + + @@ -4183,6 +4200,15 @@ PreprocessorDefinitions="" /> + + + @@ -4193,7 +4219,7 @@ /> - - - - - - + + + @@ -4259,6 +4276,15 @@ PreprocessorDefinitions="" /> + + + @@ -4269,7 +4295,7 @@ /> - - - - - - + + + @@ -4335,6 +4352,15 @@ PreprocessorDefinitions="" /> + + + @@ -4345,7 +4371,7 @@ /> - - - - - - + + + @@ -4411,6 +4428,15 @@ PreprocessorDefinitions="" /> + + + @@ -4421,7 +4447,7 @@ /> - - - - - - + + + @@ -4487,6 +4504,15 @@ PreprocessorDefinitions="" /> + + + @@ -4497,7 +4523,7 @@ /> - - - - - - + + + @@ -4563,6 +4580,15 @@ PreprocessorDefinitions="" /> + + + @@ -4573,7 +4599,7 @@ /> - - - - - - + + + @@ -4639,6 +4656,15 @@ PreprocessorDefinitions="" /> + + + @@ -4649,7 +4675,7 @@ /> - - - - - - + + + @@ -4715,6 +4732,15 @@ PreprocessorDefinitions="" /> + + + @@ -4725,7 +4751,7 @@ /> - - - - - - + + + @@ -4791,6 +4808,15 @@ PreprocessorDefinitions="" /> + + + @@ -4801,7 +4827,7 @@ /> - - - - - - + + + @@ -4867,6 +4884,15 @@ PreprocessorDefinitions="" /> + + + @@ -4877,7 +4903,7 @@ /> - - - - - - + + + @@ -4943,6 +4960,15 @@ PreprocessorDefinitions="" /> + + + @@ -4953,7 +4979,7 @@ /> - - - - - - + + + @@ -5019,6 +5036,15 @@ PreprocessorDefinitions="" /> + + + @@ -5029,7 +5055,7 @@ /> - - - - - - + + + @@ -5095,6 +5112,15 @@ PreprocessorDefinitions="" /> + + + @@ -5105,7 +5131,7 @@ /> - - - - - - + + + @@ -5171,6 +5188,15 @@ PreprocessorDefinitions="" /> + + + @@ -5181,7 +5207,7 @@ /> - - - - - - + + + @@ -5247,6 +5264,15 @@ PreprocessorDefinitions="" /> + + + @@ -5257,7 +5283,7 @@ /> - - - - - - + + + @@ -5323,6 +5340,15 @@ PreprocessorDefinitions="" /> + + + @@ -5333,7 +5359,7 @@ /> - - - - - - + + + @@ -5399,6 +5416,15 @@ PreprocessorDefinitions="" /> + + + @@ -5409,7 +5435,7 @@ /> - - - - - - + + + @@ -5475,6 +5492,15 @@ PreprocessorDefinitions="" /> + + + @@ -5485,7 +5511,7 @@ /> - - - - - - + + + @@ -5551,6 +5568,15 @@ PreprocessorDefinitions="" /> + + + @@ -5561,7 +5587,7 @@ /> - - - - - - + + + @@ -5627,6 +5644,15 @@ PreprocessorDefinitions="" /> + + + @@ -5637,7 +5663,7 @@ /> - - - - - - + + + @@ -5703,6 +5720,15 @@ PreprocessorDefinitions="" /> + + + @@ -5713,7 +5739,7 @@ /> - - - - - - + + + @@ -5781,6 +5799,16 @@ UsePrecompiledHeader="1" /> + + + @@ -5792,7 +5820,7 @@ /> - - - - - - + + + @@ -5863,6 +5880,15 @@ PreprocessorDefinitions="" /> + + + @@ -5873,7 +5899,7 @@ /> - - - - - - + + + @@ -5939,6 +5956,15 @@ PreprocessorDefinitions="" /> + + + @@ -5949,7 +5975,7 @@ /> - - - - - - + + + @@ -6015,6 +6032,15 @@ PreprocessorDefinitions="" /> + + + @@ -6025,7 +6051,7 @@ /> - - - - - - + + + @@ -6091,6 +6108,15 @@ PreprocessorDefinitions="" /> + + + @@ -6101,7 +6127,7 @@ /> - - - - - - + + + @@ -6167,6 +6184,15 @@ PreprocessorDefinitions="" /> + + + @@ -6177,7 +6203,7 @@ /> - - - - - - + + + @@ -6243,6 +6260,15 @@ PreprocessorDefinitions="" /> + + + @@ -6253,7 +6279,7 @@ /> - - - - - - + + + @@ -6319,6 +6336,15 @@ PreprocessorDefinitions="" /> + + + @@ -6329,7 +6355,7 @@ /> - - - - - - + + + @@ -6395,6 +6412,15 @@ PreprocessorDefinitions="" /> + + + @@ -6405,7 +6431,7 @@ /> - - - - - - + + + @@ -6471,6 +6488,15 @@ PreprocessorDefinitions="" /> + + + @@ -6481,7 +6507,7 @@ /> - - - - - - + + + @@ -6547,6 +6564,15 @@ PreprocessorDefinitions="" /> + + + @@ -6557,7 +6583,7 @@ /> - - - - - - + + + @@ -6623,6 +6640,15 @@ PreprocessorDefinitions="" /> + + + @@ -6633,7 +6659,7 @@ /> - - - - - - + + + @@ -6699,6 +6716,15 @@ PreprocessorDefinitions="" /> + + + @@ -6709,7 +6735,7 @@ /> - - - - - - + + + @@ -6775,6 +6792,15 @@ PreprocessorDefinitions="" /> + + + @@ -6785,7 +6811,7 @@ /> - - - - - - + + + @@ -6851,6 +6868,15 @@ PreprocessorDefinitions="" /> + + + @@ -6861,7 +6887,7 @@ /> - - - - - - + + + @@ -6927,6 +6944,15 @@ PreprocessorDefinitions="" /> + + + @@ -6937,7 +6963,7 @@ /> - - - - - - + + + @@ -7003,6 +7020,15 @@ PreprocessorDefinitions="" /> + + + @@ -7013,7 +7039,7 @@ /> - - - - - - + + + @@ -7079,6 +7096,15 @@ PreprocessorDefinitions="" /> + + + @@ -7089,7 +7115,7 @@ /> - - - - - - + + + @@ -7155,6 +7172,15 @@ PreprocessorDefinitions="" /> + + + @@ -7165,7 +7191,7 @@ /> - - - - - - + + + @@ -7231,6 +7248,15 @@ PreprocessorDefinitions="" /> + + + @@ -7241,7 +7267,7 @@ /> - - - - - - + + + @@ -7307,6 +7324,15 @@ PreprocessorDefinitions="" /> + + + @@ -7317,7 +7343,7 @@ /> - - - - - - + + + @@ -7383,6 +7400,15 @@ PreprocessorDefinitions="" /> + + + @@ -7393,7 +7419,7 @@ /> - - - - - - + + + @@ -7459,6 +7476,15 @@ PreprocessorDefinitions="" /> + + + @@ -7469,7 +7495,7 @@ /> - - - - - - + + + @@ -7535,6 +7552,15 @@ PreprocessorDefinitions="" /> + + + @@ -7545,7 +7571,7 @@ /> - - - - - - + + + @@ -7611,6 +7628,15 @@ PreprocessorDefinitions="" /> + + + @@ -7621,7 +7647,7 @@ /> - - - - - - + + + @@ -7687,6 +7704,15 @@ PreprocessorDefinitions="" /> + + + @@ -7697,7 +7723,7 @@ /> - - - - - - + + + @@ -7763,6 +7780,15 @@ PreprocessorDefinitions="" /> + + + @@ -7773,7 +7799,7 @@ /> - - - - - - + + + @@ -7839,6 +7856,15 @@ PreprocessorDefinitions="" /> + + + @@ -7849,7 +7875,7 @@ /> - - - - - - + + + @@ -7915,6 +7932,15 @@ PreprocessorDefinitions="" /> + + + @@ -7925,7 +7951,7 @@ /> - - - - - - + + + @@ -7991,6 +8008,15 @@ PreprocessorDefinitions="" /> + + + @@ -8001,7 +8027,7 @@ /> - - - - - - + + + @@ -8067,6 +8084,15 @@ PreprocessorDefinitions="" /> + + + @@ -8077,7 +8103,7 @@ /> - - - - - - + + + @@ -8143,6 +8160,15 @@ PreprocessorDefinitions="" /> + + + @@ -8153,7 +8179,7 @@ /> - - - - - - + + + @@ -8219,6 +8236,15 @@ PreprocessorDefinitions="" /> + + + @@ -8229,7 +8255,7 @@ /> - - - - - - + + + @@ -8295,6 +8312,15 @@ PreprocessorDefinitions="" /> + + + @@ -8305,7 +8331,7 @@ /> - - - - - - + + + @@ -8371,6 +8388,15 @@ PreprocessorDefinitions="" /> + + + @@ -8381,7 +8407,7 @@ /> - - - - - - + + + @@ -8447,6 +8464,15 @@ PreprocessorDefinitions="" /> + + + @@ -8457,7 +8483,7 @@ /> - - - - - - + + + @@ -8523,6 +8540,15 @@ PreprocessorDefinitions="" /> + + + @@ -8533,7 +8559,7 @@ /> - - - - - - + + + @@ -8599,6 +8616,15 @@ PreprocessorDefinitions="" /> + + + @@ -8609,7 +8635,7 @@ /> - - - - - - + + + @@ -8675,6 +8692,15 @@ PreprocessorDefinitions="" /> + + + @@ -8685,7 +8711,7 @@ /> - - - - - - + + + @@ -8751,6 +8768,15 @@ PreprocessorDefinitions="" /> + + + @@ -8761,7 +8787,7 @@ /> - - - - - - + + + @@ -8827,6 +8844,15 @@ PreprocessorDefinitions="" /> + + + @@ -8837,7 +8863,7 @@ /> - - - - - - + + + @@ -8903,6 +8920,15 @@ PreprocessorDefinitions="" /> + + + @@ -8913,7 +8939,7 @@ /> - - - - - - + + + @@ -8979,6 +8996,15 @@ PreprocessorDefinitions="" /> + + + @@ -8989,7 +9015,7 @@ /> - - - - - - + + + @@ -9055,6 +9072,15 @@ PreprocessorDefinitions="" /> + + + @@ -9065,7 +9091,7 @@ /> - - - - - - + + + @@ -9131,6 +9148,15 @@ PreprocessorDefinitions="" /> + + + @@ -9141,7 +9167,7 @@ /> - - - - - - + + + @@ -9207,6 +9224,15 @@ PreprocessorDefinitions="" /> + + + @@ -9217,7 +9243,7 @@ /> - - - - - - bases; std::vector exponents; exponents.reserve(expCount); - std::vector > baseIndices(expCount); + std::vector > baseIndices(expCount); std::vector > negateBase(expCount); - std::vector > exponentWindows(expCount); + std::vector > exponentWindows(expCount); unsigned int i; for (i=0; i > finalCascade; + std::vector > finalCascade; for (i=0; i>16) +CRYPTOPP_COMPILE_ASSERT(sizeof(IDEA::Word) >= 2); + // should use an inline function but macros are still faster in MSVC 4.0 #define DirectMUL(a,b) \ { \ @@ -21,7 +23,7 @@ static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4); // key schedule length in # of if (p) \ { \ p = low16(p) - high16(p); \ - a = (word)p - (word)high16(p); \ + a = (IDEA::Word)p - (IDEA::Word)high16(p); \ } \ else \ a = 1-a-b; \ @@ -40,7 +42,7 @@ void IDEA::Base::BuildLogTables() { tablesBuilt = true; - word x=1; + IDEA::Word x=1; word32 i; for (i=0; i<0x10000; i++) @@ -56,7 +58,7 @@ void IDEA::Base::BuildLogTables() void IDEA::Base::LookupKeyLogs() { - word* Z=key; + IDEA::Word* Z=key; int r=ROUNDS; do { @@ -70,7 +72,7 @@ void IDEA::Base::LookupKeyLogs() Z[3] = log[Z[3]]; } -inline void IDEA::Base::LookupMUL(word &a, word b) +inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b) { a = antilog[low16(log[low16(a)]+b)]; } @@ -99,7 +101,7 @@ void IDEA::Base::EnKey (const byte *userKey) unsigned int i; for (i=0; i<8; i++) - m_key[i] = ((word)userKey[2*i]<<8) | userKey[2*i+1]; + m_key[i] = ((IDEA::Word)userKey[2*i]<<8) | userKey[2*i+1]; for (; i tempkey; + FixedSizeSecBlock tempkey; unsigned int i; for (i=0; i Block; - const word *key = m_key; - word x0,x1,x2,x3,t0,t1; + const IDEA::Word *key = m_key; + IDEA::Word x0,x1,x2,x3,t0,t1; Block::Get(inBlock)(x0)(x1)(x2)(x3); for (unsigned int i=0; i, public FixedKeyLength<16>, public F /// IDEA class IDEA : public IDEA_Info, public BlockCipherDocumentation { +public: // made public for internal purposes +#ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE + typedef word Word; +#else + typedef hword Word; +#endif + +private: class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -29,7 +37,7 @@ class IDEA : public IDEA_Info, public BlockCipherDocumentation private: void EnKey(const byte *); void DeKey(); - FixedSizeSecBlock m_key; + FixedSizeSecBlock m_key; #ifdef IDEA_LARGECACHE static inline void LookupMUL(word &a, word b); diff --git a/integer.cpp b/integer.cpp index 7f22fecd..ca741905 100644 --- a/integer.cpp +++ b/integer.cpp @@ -123,7 +123,7 @@ static int Compare(const word *A, const word *B, size_t N) return 0; } -static word Increment(word *A, size_t N, word B=1) +static int Increment(word *A, size_t N, word B=1) { assert(N); word t = A[0]; @@ -136,7 +136,7 @@ static word Increment(word *A, size_t N, word B=1) return 1; } -static word Decrement(word *A, size_t N, word B=1) +static int Decrement(word *A, size_t N, word B=1) { assert(N); word t = A[0]; @@ -462,8 +462,8 @@ inline word DWord::operator%(word a) class Portable { public: - static word Add(word *C, const word *A, const word *B, size_t N); - static word Subtract(word *C, const word *A, const word *B, size_t N); + static int Add(word *C, const word *A, const word *B, size_t N); + static int Subtract(word *C, const word *A, const word *B, size_t N); static inline void Multiply2(word *C, const word *A, const word *B); static inline word Multiply2Add(word *C, const word *A, const word *B); @@ -482,7 +482,7 @@ public: static inline unsigned int SquareRecursionLimit() {return 4;} }; -word Portable::Add(word *C, const word *A, const word *B, size_t N) +int Portable::Add(word *C, const word *A, const word *B, size_t N) { assert (N%2 == 0); @@ -494,10 +494,10 @@ word Portable::Add(word *C, const word *A, const word *B, size_t N) u = DWord(A[i+1]) + B[i+1] + u.GetHighHalf(); C[i+1] = u.GetLowHalf(); } - return u.GetHighHalf(); + return int(u.GetHighHalf()); } -word Portable::Subtract(word *C, const word *A, const word *B, size_t N) +int Portable::Subtract(word *C, const word *A, const word *B, size_t N) { assert (N%2 == 0); @@ -509,7 +509,7 @@ word Portable::Subtract(word *C, const word *A, const word *B, size_t N) u = (DWord) A[i+1] - B[i+1] - u.GetHighHalfAsBorrow(); C[i+1] = u.GetLowHalf(); } - return 0-u.GetHighHalf(); + return int(0-u.GetHighHalf()); } void Portable::Multiply2(word *C, const word *A, const word *B) @@ -991,8 +991,8 @@ static bool IsP4() class PentiumOptimized : public Portable { public: - static word Add(word *C, const word *A, const word *B, size_t N); - static word Subtract(word *C, const word *A, const word *B, size_t N); + static int Add(word *C, const word *A, const word *B, size_t N); + static int Subtract(word *C, const word *A, const word *B, size_t N); static void Multiply4(word *C, const word *A, const word *B); static void Multiply8(word *C, const word *A, const word *B); static void Multiply8Bottom(word *C, const word *A, const word *B); @@ -1001,8 +1001,8 @@ public: class P4Optimized { public: - static word Add(word *C, const word *A, const word *B, size_t N); - static word Subtract(word *C, const word *A, const word *B, size_t N); + static int Add(word *C, const word *A, const word *B, size_t N); + static int Subtract(word *C, const word *A, const word *B, size_t N); #ifdef SSE2_INTRINSICS_AVAILABLE static void Multiply4(word *C, const word *A, const word *B); static void Multiply8(word *C, const word *A, const word *B); @@ -1010,7 +1010,7 @@ public: #endif }; -typedef word (* PAddSub)(word *C, const word *A, const word *B, size_t N); +typedef int (* PAddSub)(word *C, const word *A, const word *B, size_t N); typedef void (* PMul)(word *C, const word *A, const word *B); static PAddSub s_pAdd, s_pSub; @@ -1058,9 +1058,9 @@ void DisableSSE2() class LowLevel : public PentiumOptimized { public: - inline static word Add(word *C, const word *A, const word *B, size_t N) + inline static int Add(word *C, const word *A, const word *B, size_t N) {return s_pAdd(C, A, B, N);} - inline static word Subtract(word *C, const word *A, const word *B, size_t N) + inline static int Subtract(word *C, const word *A, const word *B, size_t N) {return s_pSub(C, A, B, N);} inline static void Square4(word *R, const word *A) {Multiply4(R, A, A);} @@ -1146,7 +1146,7 @@ public: ); #endif -CRYPTOPP_NAKED word PentiumOptimized::Add(word *C, const word *A, const word *B, size_t N) +CRYPTOPP_NAKED int PentiumOptimized::Add(word *C, const word *A, const word *B, size_t N) { AddPrologue @@ -1184,7 +1184,7 @@ CRYPTOPP_NAKED word PentiumOptimized::Add(word *C, const word *A, const word *B, AddEpilogue } -CRYPTOPP_NAKED word PentiumOptimized::Subtract(word *C, const word *A, const word *B, size_t N) +CRYPTOPP_NAKED int PentiumOptimized::Subtract(word *C, const word *A, const word *B, size_t N) { AddPrologue @@ -1224,7 +1224,7 @@ CRYPTOPP_NAKED word PentiumOptimized::Subtract(word *C, const word *A, const wor // On Pentium 4, the adc and sbb instructions are very expensive, so avoid them. -CRYPTOPP_NAKED word P4Optimized::Add(word *C, const word *A, const word *B, size_t N) +CRYPTOPP_NAKED int P4Optimized::Add(word *C, const word *A, const word *B, size_t N) { AddPrologue @@ -1271,7 +1271,7 @@ CRYPTOPP_NAKED word P4Optimized::Add(word *C, const word *A, const word *B, size AddEpilogue } -CRYPTOPP_NAKED word P4Optimized::Subtract(word *C, const word *A, const word *B, size_t N) +CRYPTOPP_NAKED int P4Optimized::Subtract(word *C, const word *A, const word *B, size_t N) { AddPrologue @@ -2090,7 +2090,7 @@ void RecursiveSquare(word *R, word *T, const word *A, size_t N) RecursiveSquare(R2, T2, A1, N2); RecursiveMultiply(T0, T2, A0, A1, N2); - word carry = LowLevel::Add(R1, R1, T0, N); + int carry = LowLevel::Add(R1, R1, T0, N); carry += LowLevel::Add(R1, R1, T0, N); Increment(R3, N2, carry); } @@ -2187,9 +2187,9 @@ void RecursiveMultiplyTop(word *R, word *T, const word *L, const word *A, const // now T[01] holds (A1-A0)*(B0-B1), T[23] holds A1*B1 - word c2 = LowLevel::Subtract(R0, L+N2, L, N2); + int c2 = LowLevel::Subtract(R0, L+N2, L, N2); c2 += LowLevel::Subtract(R0, R0, T0, N2); - word t = (Compare(R0, T2, N2) == -1); + int t = (Compare(R0, T2, N2) == -1); carry += t; carry += Increment(R0, N2, c2+t); @@ -2202,12 +2202,12 @@ void RecursiveMultiplyTop(word *R, word *T, const word *L, const word *A, const } } -inline word Add(word *C, const word *A, const word *B, size_t N) +inline int Add(word *C, const word *A, const word *B, size_t N) { return LowLevel::Add(C, A, B, N); } -inline word Subtract(word *C, const word *A, const word *B, size_t N) +inline int Subtract(word *C, const word *A, const word *B, size_t N) { return LowLevel::Subtract(C, A, B, N); } @@ -2738,7 +2738,7 @@ static inline size_t RoundupSize(size_t n) return 32; else if (n<=64) return 64; - else return 1U << BitPrecision(n-1); + else return size_t(1) << BitPrecision(n-1); } Integer::Integer() @@ -2786,8 +2786,8 @@ bool Integer::IsConvertableToLong() const if (ByteCount() > sizeof(long)) return false; - unsigned long value = reg[0]; - value += SafeLeftShift(reg[1]); + unsigned long value = (unsigned long)reg[0]; + value += SafeLeftShift((unsigned long)reg[1]); if (sign==POSITIVE) return (signed long)value >= 0; @@ -2799,8 +2799,8 @@ signed long Integer::ConvertToLong() const { assert(IsConvertableToLong()); - unsigned long value = reg[0]; - value += SafeLeftShift(reg[1]); + unsigned long value = (unsigned long)reg[0]; + value += SafeLeftShift((unsigned long)reg[1]); return sign==POSITIVE ? value : -(signed long)value; } @@ -2953,7 +2953,7 @@ Integer::Integer(word value, size_t length) template static Integer StringToInteger(const T *str) { - word radix; + int radix; // GCC workaround // std::char_traits::length() not defined in GCC 3.2 and STLport 4.5.3 unsigned int length; @@ -2987,7 +2987,7 @@ static Integer StringToInteger(const T *str) for (unsigned i=0; i= '0' && str[i] <= '9') digit = str[i] - '0'; @@ -3456,7 +3456,7 @@ Integer& Integer::operator--() void PositiveAdd(Integer &sum, const Integer &a, const Integer& b) { - word carry; + int carry; if (a.reg.size() == b.reg.size()) carry = Add(sum.reg, a.reg, b.reg, a.reg.size()); else if (a.reg.size() > b.reg.size()) @@ -3750,7 +3750,7 @@ void Integer::DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, unsigne CopyWords(r.reg, a.reg, wordCount); SetWords(r.reg+wordCount, 0, r.reg.size()-wordCount); if (n % WORD_BITS != 0) - r.reg[wordCount-1] %= (1 << (n % WORD_BITS)); + r.reg[wordCount-1] %= (word(1) << (n % WORD_BITS)); } else { diff --git a/integer.h b/integer.h index 0c123c63..7bb051a5 100644 --- a/integer.h +++ b/integer.h @@ -278,7 +278,7 @@ public: //! Integer& operator/=(word t) {return *this = DividedBy(t);} //! - Integer& operator%=(word t) {return *this = Modulo(t);} + Integer& operator%=(word t) {return *this = Integer(POSITIVE, 0, Modulo(t));} //! Integer& operator<<=(size_t); diff --git a/lubyrack.h b/lubyrack.h index c04fd914..424152aa 100644 --- a/lubyrack.h +++ b/lubyrack.h @@ -84,7 +84,7 @@ class LR : public LR_Info, public BlockCipherDocumentation if (xorBlock) xorbuf(outBlock, xorBlock, this->buffer, 2*this->S); else - memcpy(outBlock, this->buffer, 2*this->S); + memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S); } }; diff --git a/mdc.h b/mdc.h index a9bb430c..7091fe8b 100644 --- a/mdc.h +++ b/mdc.h @@ -32,7 +32,7 @@ class MDC : public MDC_Info { assert(direction == ENCRYPTION); this->AssertValidKeyLength(length); - memcpy(Key(), userKey, this->KEYLENGTH); + memcpy_s(m_key, m_key.size(), userKey, this->KEYLENGTH); T::CorrectEndianess(Key(), Key(), this->KEYLENGTH); } diff --git a/misc.h b/misc.h index ba77ac4a..3b25ee0c 100644 --- a/misc.h +++ b/misc.h @@ -355,7 +355,7 @@ inline void IncrementCounterByOne(byte *output, const byte *input, unsigned int int i, carry; for (i=s-1, carry=1; i>=0 && carry; i--) carry = !(output[i] = input[i]+1); - memcpy(output, input, i+1); + memcpy_s(output, s, input, i+1); } // ************** rotate functions *************** @@ -602,7 +602,7 @@ inline void ConditionalByteReverse(ByteOrder order, T *out, const T *in, size_t if (!NativeByteOrderIs(order)) ByteReverse(out, in, byteCount); else if (in != out) - memcpy(out, in, byteCount); + memcpy_s(out, byteCount, in, byteCount); } template @@ -852,7 +852,7 @@ template T StringToWord(const std::string &str, ByteOrder order = BIG_ENDIAN_ORDER) { T value = 0; - memcpy(&value, str.data(), UnsignedMin(str.size(), sizeof(value))); + memcpy_s(&value, sizeof(value), str.data(), UnsignedMin(str.size(), sizeof(value))); return NativeByteOrderIs(order) ? value : ByteReverse(value); } diff --git a/modes.h b/modes.h index bc401e68..533a7501 100644 --- a/modes.h +++ b/modes.h @@ -90,12 +90,13 @@ protected: void TransformRegister() { m_cipher->ProcessBlock(m_register, m_temp); - memmove(m_register, m_register+m_feedbackSize, BlockSize()-m_feedbackSize); - memcpy(m_register+BlockSize()-m_feedbackSize, m_temp, m_feedbackSize); + unsigned int updateSize = BlockSize()-m_feedbackSize; + memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize); + memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize); } void CipherResynchronize(const byte *iv) { - memcpy(m_register, iv, BlockSize()); + memcpy_s(m_register, m_register.size(), iv, BlockSize()); TransformRegister(); } void SetFeedbackSize(unsigned int feedbackSize) @@ -117,7 +118,7 @@ protected: inline void CopyOrZero(void *dest, const void *src, size_t s) { if (src) - memcpy(dest, src, s); + memcpy_s(dest, s, src, s); else memset(dest, 0, s); } @@ -136,7 +137,7 @@ private: { assert(iterationCount == 1); m_cipher->ProcessBlock(keystreamBuffer); - memcpy(m_register, keystreamBuffer, BlockSize()); + memcpy_s(m_register, m_register.size(), keystreamBuffer, BlockSize()); } void CipherResynchronize(byte *keystreamBuffer, const byte *iv) { @@ -175,7 +176,7 @@ public: bool IsRandomAccess() const {return false;} bool IsSelfInverting() const {return false;} bool IsForwardTransformation() const {return m_cipher->IsForwardTransformation();} - void Resynchronize(const byte *iv) {memcpy(m_register, iv, BlockSize());} + void Resynchronize(const byte *iv) {memcpy_s(m_register, m_register.size(), iv, BlockSize());} void ProcessData(byte *outString, const byte *inString, size_t length); protected: diff --git a/nbtheory.cpp b/nbtheory.cpp index 97689bde..75edc148 100644 --- a/nbtheory.cpp +++ b/nbtheory.cpp @@ -319,7 +319,7 @@ bool PrimeSieve::NextCandidate(Integer &c) } else { - c = m_first + m_next*m_step; + c = m_first + long(m_next)*m_step; ++m_next; return true; } @@ -330,9 +330,9 @@ void PrimeSieve::SieveSingle(std::vector &sieve, word16 p, const Integer & if (stepInv) { size_t sieveSize = sieve.size(); - word j = word((word32(p-(first%p))*stepInv) % p); + size_t j = (word32(p-(first%p))*stepInv) % p; // if the first multiple of p is p, skip it - if (first.WordCount() <= 1 && first + step*j == p) + if (first.WordCount() <= 1 && first + step*long(j) == p) j += p; for (; j < sieveSize; j += p) sieve[j] = true; @@ -353,7 +353,7 @@ void PrimeSieve::DoSieve() if (m_delta == 0) { for (unsigned int i = 0; i < primeTableSize; ++i) - SieveSingle(m_sieve, primeTable[i], m_first, m_step, m_step.InverseMod(primeTable[i])); + SieveSingle(m_sieve, primeTable[i], m_first, m_step, (word16)m_step.InverseMod(primeTable[i])); } else { @@ -363,7 +363,7 @@ void PrimeSieve::DoSieve() for (unsigned int i = 0; i < primeTableSize; ++i) { word16 p = primeTable[i]; - word16 stepInv = m_step.InverseMod(p); + word16 stepInv = (word16)m_step.InverseMod(p); SieveSingle(m_sieve, p, m_first, m_step, stepInv); word16 halfStepInv = 2*stepInv < p ? 2*stepInv : 2*stepInv-p; diff --git a/secblock.h b/secblock.h index a828ba23..12122081 100644 --- a/secblock.h +++ b/secblock.h @@ -61,7 +61,7 @@ typename A::pointer StandardReallocate(A& a, T *p, typename A::size_type oldSize { A b; typename A::pointer newPointer = b.allocate(newSize, NULL); - memcpy(newPointer, p, sizeof(T)*STDMIN(oldSize, newSize)); + memcpy_s(newPointer, sizeof(T)*newSize, p, sizeof(T)*STDMIN(oldSize, newSize)); a.deallocate(p, oldSize); std::swap(a, b); return newPointer; @@ -211,7 +211,7 @@ public: explicit SecBlock(size_type size=0) : m_size(size) {m_ptr = m_alloc.allocate(size, NULL);} SecBlock(const SecBlock &t) - : m_size(t.m_size) {m_ptr = m_alloc.allocate(m_size, NULL); memcpy(m_ptr, t.m_ptr, m_size*sizeof(T));} + : m_size(t.m_size) {m_ptr = m_alloc.allocate(m_size, NULL); memcpy_s(m_ptr, m_size*sizeof(T), t.m_ptr, m_size*sizeof(T));} SecBlock(const T *t, size_type len) : m_size(len) { @@ -270,16 +270,18 @@ public: size_type size() const {return m_size;} bool empty() const {return m_size == 0;} + size_type SizeInBytes() const {return m_size*sizeof(T);} + void Assign(const T *t, size_type len) { New(len); - memcpy(m_ptr, t, len*sizeof(T)); + memcpy_s(m_ptr, m_size*sizeof(T), t, len*sizeof(T)); } void Assign(const SecBlock &t) { New(t.m_size); - memcpy(m_ptr, t.m_ptr, m_size*sizeof(T)); + memcpy_s(m_ptr, m_size*sizeof(T), t.m_ptr, m_size*sizeof(T)); } SecBlock& operator=(const SecBlock &t) @@ -292,15 +294,15 @@ public: { size_type oldSize = m_size; Grow(m_size+t.m_size); - memcpy(m_ptr+oldSize, t.m_ptr, t.m_size*sizeof(T)); + memcpy_s(m_ptr+oldSize, m_size*sizeof(T), t.m_ptr, t.m_size*sizeof(T)); return *this; } SecBlock operator+(const SecBlock &t) { SecBlock result(m_size+t.m_size); - memcpy(result.m_ptr, m_ptr, m_size*sizeof(T)); - memcpy(result.m_ptr+m_size, t.m_ptr, t.m_size*sizeof(T)); + memcpy_s(result.m_ptr, result.m_size*sizeof(T), m_ptr, m_size*sizeof(T)); + memcpy_s(result.m_ptr+m_size, t.m_size*sizeof(T), t.m_ptr, t.m_size*sizeof(T)); return result; } diff --git a/test.cpp b/test.cpp index 66827dbd..e5ef22c7 100644 --- a/test.cpp +++ b/test.cpp @@ -1,6 +1,8 @@ // test.cpp - written and placed in the public domain by Wei Dai +#define _CRT_SECURE_NO_DEPRECATE #define CRYPTOPP_DEFAULT_NO_DLL + #include "dll.h" #include "md5.h" #include "ripemd.h" @@ -190,7 +192,7 @@ int __cdecl main(int argc, char *argv[]) return 1; } - unsigned int macPos = found-buf.begin(); + unsigned int macPos = unsigned int(found-buf.begin()); member_ptr pMac(NewIntegrityCheckingMAC()); pMac->Update(buf.begin(), macPos); pMac->Update(buf.begin() + macPos + sizeof(dummyMac), fileSize - sizeof(dummyMac) - macPos); diff --git a/xormac.h b/xormac.h index 8ab0e187..18a2ae8a 100644 --- a/xormac.h +++ b/xormac.h @@ -79,7 +79,7 @@ template void XMACC_Base::CheckedSetKey(void *, Empty empty, const GetWord(false, BIG_ENDIAN_ORDER, m_counter, iv); else params.GetValue(Name::XMACC_Counter(), m_counter); - memcpy(m_key, key, this->KEYLENGTH); + memcpy_s(m_key, m_key.SizeInBytes(), key, this->KEYLENGTH); Init(); } @@ -105,7 +105,7 @@ template inline void XMACC_Base::XorDigest(HashWordType *digest, co template void XMACC_Base::HashEndianCorrectedBlock(const HashWordType *input) { - memcpy(m_buffer, m_key, this->KEYLENGTH); + memcpy_s(m_buffer, m_buffer.SizeInBytes(), m_key, this->KEYLENGTH); WriteWord32((byte *)m_buffer.begin()+this->KEYLENGTH, ++m_index); T::CorrectEndianess(m_buffer, m_buffer, T::DIGESTSIZE); T::Transform(m_buffer, input); @@ -126,7 +126,7 @@ template void XMACC_Base::TruncatedFinal(byte *mac, size_t size) this->m_data[this->m_data.size()-1] = ByteReverse(this->GetBitCountLo()); HashEndianCorrectedBlock(this->m_data); - memcpy(m_buffer, m_key, this->KEYLENGTH); + memcpy_s(m_buffer, m_buffer.SizeInBytes(), m_key, this->KEYLENGTH); WriteWord32((byte *)m_buffer.begin()+this->KEYLENGTH, 0); memset(this->m_data, 0, this->BLOCKSIZE-4); WriteWord32((byte *)this->m_data.begin()+this->BLOCKSIZE-4, ++m_counter); @@ -137,7 +137,7 @@ template void XMACC_Base::TruncatedFinal(byte *mac, size_t size) WriteWord32(mac, m_counter); T::CorrectEndianess(this->m_digest, this->m_digest, T::DIGESTSIZE); - memcpy(mac+4, this->m_digest, size-4); + memcpy_s(mac+4, size-4, this->m_digest, size-4); this->Restart(); // reinit for next use } @@ -152,10 +152,10 @@ template bool XMACC_Base::TruncatedVerify(const byte *mac, size_t s this->m_data[this->m_data.size()-1] = ByteReverse(this->GetBitCountLo()); HashEndianCorrectedBlock(this->m_data); - memcpy(m_buffer, m_key, this->KEYLENGTH); + memcpy_s(m_buffer, m_buffer.SizeInBytes(), m_key, this->KEYLENGTH); WriteWord32((byte *)m_buffer.begin()+this->KEYLENGTH, 0); memset(this->m_data, 0, this->BLOCKSIZE-4); - memcpy((byte *)this->m_data.begin()+this->BLOCKSIZE-4, mac, 4); + memcpy_s((byte *)this->m_data.begin()+this->BLOCKSIZE-4, 4, mac, 4); T::CorrectEndianess(m_buffer, m_buffer, T::DIGESTSIZE); T::CorrectEndianess(this->m_data, this->m_data, this->BLOCKSIZE); T::Transform(m_buffer, this->m_data); diff --git a/zdeflate.cpp b/zdeflate.cpp index 2c6fd887..b83243cd 100644 --- a/zdeflate.cpp +++ b/zdeflate.cpp @@ -383,7 +383,7 @@ unsigned int Deflator::LongestMatch(unsigned int &bestMatch) const if (scan[bestLength-1] == match[bestLength-1] && scan[bestLength] == match[bestLength] && scan[0] == match[0] && scan[1] == match[1]) { assert(scan[2] == match[2]); - unsigned int len = (unsigned int)(std::mismatch(scan+3, scanEnd, match+3).first - scan); + unsigned int len = (unsigned int)(stdext::unchecked_mismatch(scan+3, scanEnd, match+3).first - scan); assert(len != bestLength); if (len > bestLength) { diff --git a/zinflate.cpp b/zinflate.cpp index e0be8ba7..4a43420f 100644 --- a/zinflate.cpp +++ b/zinflate.cpp @@ -138,7 +138,7 @@ void HuffmanDecoder::Initialize(const unsigned int *codeBits, unsigned int nCode m_normalizedCacheMask = NormalizeCode(m_cacheMask, m_cacheBits); assert(m_normalizedCacheMask == BitReverse(m_cacheMask)); - if (m_cache.size() != 1 << m_cacheBits) + if (m_cache.size() != size_t(1) << m_cacheBits) m_cache.resize(1 << m_cacheBits); for (i=0; i