diff options
Diffstat (limited to 'extra/yassl/taocrypt/src/integer.cpp')
-rw-r--r-- | extra/yassl/taocrypt/src/integer.cpp | 105 |
1 files changed, 21 insertions, 84 deletions
diff --git a/extra/yassl/taocrypt/src/integer.cpp b/extra/yassl/taocrypt/src/integer.cpp index 6d7e52e8243..15deb59d4f3 100644 --- a/extra/yassl/taocrypt/src/integer.cpp +++ b/extra/yassl/taocrypt/src/integer.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2005, 2010, Oracle and/or its affiliates + Copyright (c) 2005, 2012, Oracle and/or its affiliates This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -11,8 +11,9 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + along with this program; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, + MA 02110-1301 USA. */ @@ -72,7 +73,8 @@ template <class T> CPP_TYPENAME AlignedAllocator<T>::pointer AlignedAllocator<T>::allocate( size_type n, const void *) { - CheckSize(n); + if (n > max_size()) + return 0; if (n == 0) return 0; if (n >= 4) @@ -90,16 +92,13 @@ CPP_TYPENAME AlignedAllocator<T>::pointer AlignedAllocator<T>::allocate( #endif #ifdef TAOCRYPT_NO_ALIGNED_ALLOC - assert(m_pBlock == 0); m_pBlock = p; if (!IsAlignedOn(p, 16)) { - assert(IsAlignedOn(p, 8)); p = (byte *)p + 8; } #endif - assert(IsAlignedOn(p, 16)); return (T*)p; } return NEW_TC T[n]; @@ -115,7 +114,6 @@ void AlignedAllocator<T>::deallocate(void* p, size_type n) #ifdef TAOCRYPT_MM_MALLOC_AVAILABLE _mm_free(p); #elif defined(TAOCRYPT_NO_ALIGNED_ALLOC) - assert(m_pBlock == p || (byte*)m_pBlock+8 == p); free(m_pBlock); m_pBlock = 0; #else @@ -283,14 +281,14 @@ DWord() {} private: struct dword_struct - { - #ifdef LITTLE_ENDIAN_ORDER - word low; - word high; - #else - word high; - word low; - #endif + { + #ifdef LITTLE_ENDIAN_ORDER + word low; + word high; + #else + word high; + word low; + #endif }; union @@ -364,9 +362,6 @@ private: template <class S, class D> S DivideThreeWordsByTwo(S* A, S B0, S B1, D* dummy_VC6_WorkAround = 0) { - // assert {A[2],A[1]} < {B1,B0}, so quotient can fit in a S - assert(A[2] < B1 || (A[2]==B1 && A[1] < B0)); - // estimate the quotient: do a 2 S by 1 S divide S Q; if (S(B1+1) == 0) @@ -392,7 +387,6 @@ S DivideThreeWordsByTwo(S* A, S B0, S B1, D* dummy_VC6_WorkAround = 0) A[1] = u.GetLowHalf(); A[2] += u.GetHighHalf(); Q++; - assert(Q); // shouldn't overflow } return Q; @@ -490,7 +484,6 @@ static int Compare(const word *A, const word *B, unsigned int N) static word Increment(word *A, unsigned int N, word B=1) { - assert(N); word t = A[0]; A[0] = t+B; if (A[0] >= t) @@ -503,7 +496,6 @@ static word Increment(word *A, unsigned int N, word B=1) static word Decrement(word *A, unsigned int N, word B=1) { - assert(N); word t = A[0]; A[0] = t-B; if (A[0] <= t) @@ -537,14 +529,11 @@ static word LinearMultiply(word *C, const word *A, word B, unsigned int N) static word AtomicInverseModPower2(word A) { - assert(A%2==1); - word R=A%8; for (unsigned i=3; i<WORD_BITS; i*=2) R = R*(2-R*A); - assert(word(R*A)==1); return R; } @@ -575,14 +564,11 @@ public: static void TAOCRYPT_CDECL Square2(word *R, const word *A); static void TAOCRYPT_CDECL Square4(word *R, const word *A); - static void TAOCRYPT_CDECL Square8(word *R, const word *A) {assert(false);} static unsigned int TAOCRYPT_CDECL SquareRecursionLimit() {return 4;} }; word Portable::Add(word *C, const word *A, const word *B, unsigned int N) { - assert (N%2 == 0); - DWord u(0, 0); for (unsigned int i = 0; i < N; i+=2) { @@ -596,8 +582,6 @@ word Portable::Add(word *C, const word *A, const word *B, unsigned int N) word Portable::Subtract(word *C, const word *A, const word *B, unsigned int N) { - assert (N%2 == 0); - DWord u(0, 0); for (unsigned int i = 0; i < N; i+=2) { @@ -1012,7 +996,7 @@ void Portable::Multiply8Bottom(word *R, const word *A, const word *B) static jmp_buf s_env; static void SigIllHandler(int) { - longjmp(s_env, 1); + longjmp(s_env, 1); } #endif @@ -2129,8 +2113,6 @@ void P4Optimized::Multiply8Bottom(word *C, const word *A, const word *B) void RecursiveMultiply(word *R, word *T, const word *A, const word *B, unsigned int N) { - assert(N>=2 && N%2==0); - if (LowLevel::MultiplyRecursionLimit() >= 8 && N==8) LowLevel::Multiply8(R, A, B); else if (LowLevel::MultiplyRecursionLimit() >= 4 && N==4) @@ -2187,7 +2169,6 @@ void RecursiveMultiply(word *R, word *T, const word *A, const word *B, carry += LowLevel::Add(T0, T0, R2, N); carry += LowLevel::Add(R1, R1, T0, N); - assert (carry >= 0 && carry <= 2); Increment(R3, N2, carry); } } @@ -2195,9 +2176,6 @@ void RecursiveMultiply(word *R, word *T, const word *A, const word *B, void RecursiveSquare(word *R, word *T, const word *A, unsigned int N) { - assert(N && N%2==0); - if (LowLevel::SquareRecursionLimit() >= 8 && N==8) - LowLevel::Square8(R, A); if (LowLevel::SquareRecursionLimit() >= 4 && N==4) LowLevel::Square4(R, A); else if (N==2) @@ -2226,7 +2204,6 @@ void RecursiveSquare(word *R, word *T, const word *A, unsigned int N) void RecursiveMultiplyBottom(word *R, word *T, const word *A, const word *B, unsigned int N) { - assert(N>=2 && N%2==0); if (LowLevel::MultiplyBottomRecursionLimit() >= 8 && N==8) LowLevel::Multiply8Bottom(R, A, B); else if (LowLevel::MultiplyBottomRecursionLimit() >= 4 && N==4) @@ -2249,8 +2226,6 @@ void RecursiveMultiplyBottom(word *R, word *T, const word *A, const word *B, void RecursiveMultiplyTop(word *R, word *T, const word *L, const word *A, const word *B, unsigned int N) { - assert(N>=2 && N%2==0); - if (N==4) { LowLevel::Multiply4(T, A, B); @@ -2314,7 +2289,6 @@ void RecursiveMultiplyTop(word *R, word *T, const word *L, const word *A, carry += Increment(R0, N2, c2+t); carry += LowLevel::Add(R0, R0, T1, N2); carry += LowLevel::Add(R0, R0, T3, N2); - assert (carry >= 0 && carry <= 2); CopyWords(R1, T3, N2); Increment(R1, N2, carry); @@ -2363,9 +2337,6 @@ void AsymmetricMultiply(word *R, word *T, const word *A, unsigned int NA, STL::swap(NA, NB); } - assert(NB % NA == 0); - assert((NB/NA)%2 == 0); // NB is an even multiple of NA - if (NA==2 && !A[1]) { switch (A[0]) @@ -2432,8 +2403,6 @@ static inline unsigned int EvenWordCount(const word *X, unsigned int N) unsigned int AlmostInverse(word *R, word *T, const word *A, unsigned int NA, const word *M, unsigned int N) { - assert(NA<=N && N && N%2==0); - word *b = T; word *c = T+N; word *f = T+2*N; @@ -2459,7 +2428,6 @@ unsigned int AlmostInverse(word *R, word *T, const word *A, unsigned int NA, ShiftWordsRightByWords(f, fgLen, 1); if (c[bcLen-1]) bcLen+=2; - assert(bcLen <= N); ShiftWordsLeftByWords(c, bcLen, 1); k+=WORD_BITS; t=f[0]; @@ -2488,7 +2456,6 @@ unsigned int AlmostInverse(word *R, word *T, const word *A, unsigned int NA, { c[bcLen] = t; bcLen+=2; - assert(bcLen <= N); } if (f[fgLen-2]==0 && g[fgLen-2]==0 && f[fgLen-1]==0 && g[fgLen-1]==0) @@ -2507,7 +2474,6 @@ unsigned int AlmostInverse(word *R, word *T, const word *A, unsigned int NA, { b[bcLen] = 1; bcLen+=2; - assert(bcLen <= N); } } } @@ -2621,11 +2587,14 @@ void Integer::Decode(Source& source) } word32 length = GetLength(source); + if (length == 0 || source.GetError().What()) return; if ( (b = source.next()) == 0x00) length--; else source.prev(); + + if (source.IsLeft(length) == false) return; unsigned int words = (length + WORD_SIZE - 1) / WORD_SIZE; words = RoundupSize(words); @@ -2740,8 +2709,6 @@ void Integer::Randomize(RandomNumberGenerator& rng, unsigned int nbits) void Integer::Randomize(RandomNumberGenerator& rng, const Integer& min, const Integer& max) { - assert(min <= max); - Integer range = max - min; const unsigned int nbits = range.BitCount(); @@ -2880,7 +2847,7 @@ Integer& Integer::operator++() else { word borrow = Decrement(reg_.get_buffer(), reg_.size()); - assert(!borrow); + (void)borrow; // shut up compiler if (WordCount()==0) *this = Zero(); } @@ -2997,7 +2964,6 @@ void PositiveSubtract(Integer &diff, const Integer &a, const Integer& b) b.reg_.get_buffer(), bSize); CopyWords(diff.reg_+bSize, a.reg_+bSize, aSize-bSize); borrow = Decrement(diff.reg_+bSize, aSize-bSize, borrow); - assert(!borrow); diff.sign_ = Integer::POSITIVE; } else @@ -3006,7 +2972,6 @@ void PositiveSubtract(Integer &diff, const Integer &a, const Integer& b) a.reg_.get_buffer(), aSize); CopyWords(diff.reg_+aSize, b.reg_+aSize, bSize-aSize); borrow = Decrement(diff.reg_+aSize, bSize-aSize, borrow); - assert(!borrow); diff.sign_ = Integer::NEGATIVE; } } @@ -3066,7 +3031,6 @@ bool Integer::GetBit(unsigned int n) const unsigned long Integer::GetBits(unsigned int i, unsigned int n) const { - assert(n <= sizeof(unsigned long)*8); unsigned long v = 0; for (unsigned int j=0; j<n; j++) v |= GetBit(i+j) << j; @@ -3126,8 +3090,6 @@ bool Integer::IsConvertableToLong() const signed long Integer::ConvertToLong() const { - assert(IsConvertableToLong()); - unsigned long value = reg_[0]; value += SafeLeftShift<WORD_BITS, unsigned long>(reg_[1]); return sign_ == POSITIVE ? value : -(signed long)value; @@ -3226,11 +3188,9 @@ static inline void AtomicDivide(word *Q, const word *A, const word *B) { // multiply quotient and divisor and add remainder, make sure it // equals dividend - assert(!T[2] && !T[3] && (T[1] < B[1] || (T[1]==B[1] && T[0]<B[0]))); word P[4]; Portable::Multiply2(P, Q, B); Add(P, P, T, 4); - assert(memcmp(P, A, 4*WORD_SIZE)==0); } #endif } @@ -3240,8 +3200,6 @@ static inline void AtomicDivide(word *Q, const word *A, const word *B) static void CorrectQuotientEstimate(word *R, word *T, word *Q, const word *B, unsigned int N) { - assert(N && N%2==0); - if (Q[1]) { T[N] = T[N+1] = 0; @@ -3259,13 +3217,12 @@ static void CorrectQuotientEstimate(word *R, word *T, word *Q, const word *B, } word borrow = Subtract(R, R, T, N+2); - assert(!borrow && !R[N+1]); + (void)borrow; // shut up compiler while (R[N] || Compare(R, B, N) >= 0) { R[N] -= Subtract(R, R, B, N); Q[1] += (++Q[0]==0); - assert(Q[0] || Q[1]); // no overflow } } @@ -3279,10 +3236,6 @@ static void CorrectQuotientEstimate(word *R, word *T, word *Q, const word *B, void Divide(word* R, word* Q, word* T, const word* A, unsigned int NA, const word* B, unsigned int NB) { - assert(NA && NB && NA%2==0 && NB%2==0); - assert(B[NB-1] || B[NB-2]); - assert(NB <= NA); - // set up temporary work space word *const TA=T; word *const TB=T+NA+2; @@ -3293,7 +3246,6 @@ void Divide(word* R, word* Q, word* T, const word* A, unsigned int NA, TB[0] = TB[NB-1] = 0; CopyWords(TB+shiftWords, B, NB-shiftWords); unsigned shiftBits = WORD_BITS - BitPrecision(TB[NB-1]); - assert(shiftBits < WORD_BITS); ShiftWordsLeftByBits(TB, NB, shiftBits); // copy A into TA and normalize it @@ -3313,7 +3265,6 @@ void Divide(word* R, word* Q, word* T, const word* A, unsigned int NA, else { NA+=2; - assert(Compare(TA+NA-NB, TB, NB) < 0); } word BT[2]; @@ -3339,8 +3290,6 @@ void PositiveDivide(Integer& remainder, Integer& quotient, unsigned aSize = a.WordCount(); unsigned bSize = b.WordCount(); - assert(bSize); - if (a.PositiveCompare(b) == -1) { remainder = a; @@ -3428,8 +3377,6 @@ Integer Integer::Modulo(const Integer &b) const void Integer::Divide(word &remainder, Integer "ient, const Integer ÷nd, word divisor) { - assert(divisor); - if ((divisor & (divisor-1)) == 0) // divisor is a power of 2 { quotient = dividend >> (BitPrecision(divisor)-1); @@ -3469,8 +3416,6 @@ Integer Integer::DividedBy(word b) const word Integer::Modulo(word divisor) const { - assert(divisor); - word remainder; if ((divisor & (divisor-1)) == 0) // divisor is a power of 2 @@ -3516,7 +3461,6 @@ Integer Integer::SquareRoot() const // overestimate square root Integer x, y = Power2((BitCount()+1)/2); - assert(y*y >= *this); do { @@ -3561,8 +3505,6 @@ Integer Integer::Gcd(const Integer &a, const Integer &b) Integer Integer::InverseMod(const Integer &m) const { - assert(m.NotNegative()); - if (IsNegative() || *this>=m) return (*this%m).InverseMod(m); @@ -3805,7 +3747,7 @@ void MontgomeryReduce(word *R, word *T, const word *X, const word *M, word borrow = Subtract(T, X+N, T, N); // defend against timing attack by doing this Add even when not needed word carry = Add(T+N, T, M, N); - assert(carry || !borrow); + (void)carry; // shut up compiler CopyWords(R, T + (borrow ? N : 0), N); } @@ -3861,7 +3803,6 @@ MontgomeryRepresentation::MontgomeryRepresentation(const Integer &m) u((word)0, modulus.reg_.size()), workspace(5*modulus.reg_.size()) { - assert(modulus.IsOdd()); RecursiveInverseModPower2(u.reg_.get_buffer(), workspace.get_buffer(), modulus.reg_.get_buffer(), modulus.reg_.size()); } @@ -3872,7 +3813,6 @@ const Integer& MontgomeryRepresentation::Multiply(const Integer &a, word *const T = workspace.begin(); word *const R = result.reg_.begin(); const unsigned int N = modulus.reg_.size(); - assert(a.reg_.size()<=N && b.reg_.size()<=N); AsymmetricMultiply(T, T+2*N, a.reg_.get_buffer(), a.reg_.size(), b.reg_.get_buffer(), b.reg_.size()); @@ -3887,7 +3827,6 @@ const Integer& MontgomeryRepresentation::Square(const Integer &a) const word *const T = workspace.begin(); word *const R = result.reg_.begin(); const unsigned int N = modulus.reg_.size(); - assert(a.reg_.size()<=N); TaoCrypt::Square(T, T+2*N, a.reg_.get_buffer(), a.reg_.size()); SetWords(T+2*a.reg_.size(), 0, 2*N-2*a.reg_.size()); @@ -3901,7 +3840,6 @@ Integer MontgomeryRepresentation::ConvertOut(const Integer &a) const word *const T = workspace.begin(); word *const R = result.reg_.begin(); const unsigned int N = modulus.reg_.size(); - assert(a.reg_.size()<=N); CopyWords(T, a.reg_.get_buffer(), a.reg_.size()); SetWords(T+a.reg_.size(), 0, 2*N-a.reg_.size()); @@ -3918,7 +3856,6 @@ const Integer& MontgomeryRepresentation::MultiplicativeInverse( word *const T = workspace.begin(); word *const R = result.reg_.begin(); const unsigned int N = modulus.reg_.size(); - assert(a.reg_.size()<=N); CopyWords(T, a.reg_.get_buffer(), a.reg_.size()); SetWords(T+a.reg_.size(), 0, 2*N-a.reg_.size()); |