diff options
author | Igor Babaev <igor@askmonty.org> | 2012-04-11 20:44:52 -0700 |
---|---|---|
committer | Igor Babaev <igor@askmonty.org> | 2012-04-11 20:44:52 -0700 |
commit | 9e74665a2d91e299d1b9c5c2695855426aad78eb (patch) | |
tree | 9c865c2e18fcb814ef80e96965debf53a0923e6a /extra/yassl/taocrypt | |
parent | 1c0a89afcc1581187e8ee84abbd445da2bfa45d9 (diff) | |
parent | 85a025f30c5196c22c5b1d7960912fe9b3f0d6c0 (diff) | |
download | mariadb-git-9e74665a2d91e299d1b9c5c2695855426aad78eb.tar.gz |
Merge 5.5 -> 5.5-mwl248.
Diffstat (limited to 'extra/yassl/taocrypt')
56 files changed, 1228 insertions, 382 deletions
diff --git a/extra/yassl/taocrypt/CMakeLists.txt b/extra/yassl/taocrypt/CMakeLists.txt index 750731a0449..10ed614445e 100644 --- a/extra/yassl/taocrypt/CMakeLists.txt +++ b/extra/yassl/taocrypt/CMakeLists.txt @@ -20,12 +20,14 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) ADD_DEFINITIONS(${SSL_DEFINES}) SET(TAOCRYPT_SOURCES src/aes.cpp src/aestables.cpp src/algebra.cpp src/arc4.cpp src/asn.cpp src/coding.cpp src/des.cpp src/dh.cpp src/dsa.cpp src/file.cpp src/hash.cpp src/integer.cpp src/md2.cpp - src/md4.cpp src/md5.cpp src/misc.cpp src/random.cpp src/ripemd.cpp src/rsa.cpp src/sha.cpp + src/md4.cpp src/md5.cpp src/misc.cpp src/random.cpp src/ripemd.cpp src/rsa.cpp src/sha.cpp + src/rabbit.cpp src/hc128.cpp include/aes.hpp include/algebra.hpp include/arc4.hpp include/asn.hpp include/block.hpp include/coding.hpp include/des.hpp include/dh.hpp include/dsa.hpp include/dsa.hpp include/error.hpp include/file.hpp include/hash.hpp include/hmac.hpp include/integer.hpp include/md2.hpp include/md5.hpp include/misc.hpp include/modarith.hpp include/modes.hpp - include/random.hpp include/ripemd.hpp include/rsa.hpp include/sha.hpp) + include/random.hpp include/ripemd.hpp include/rsa.hpp include/sha.hpp + include/rabbit.hpp include/hc128.hpp) IF(HAVE_EXPLICIT_TEMPLATE_INSTANTIATION) SET(TAOCRYPT_SOURCES ${TAOCRYPT_SOURCES} src/template_instnt.cpp) diff --git a/extra/yassl/taocrypt/README b/extra/yassl/taocrypt/README index 0a7ff301786..bd786b7ce54 100644 --- a/extra/yassl/taocrypt/README +++ b/extra/yassl/taocrypt/README @@ -21,7 +21,7 @@ Stream Ciphers: ARC4 Public Key Crypto: RSA, DSA, Diffie-Hellman Password based key derivation: PBKDF2 from PKCS #5 Pseudo Random Number Generators -Lare Integer Support +Large Integer Support Base 16/64 encoding/decoding DER encoding/decoding X.509 processing diff --git a/extra/yassl/taocrypt/benchmark/benchmark.cpp b/extra/yassl/taocrypt/benchmark/benchmark.cpp index 1d38b080d0d..6ed7351a56b 100644 --- a/extra/yassl/taocrypt/benchmark/benchmark.cpp +++ b/extra/yassl/taocrypt/benchmark/benchmark.cpp @@ -103,7 +103,7 @@ void bench_des() double persec = 1 / total * megs; - printf("3DES %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("3DES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -123,7 +123,7 @@ void bench_aes(bool show) double persec = 1 / total * megs; if (show) - printf("AES %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("AES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -142,7 +142,7 @@ void bench_twofish() double persec = 1 / total * megs; - printf("Twofish %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("Twofish %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -162,7 +162,7 @@ void bench_blowfish() double persec = 1 / total * megs; - printf("Blowfish %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("Blowfish %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -181,7 +181,7 @@ void bench_arc4() double persec = 1 / total * megs; - printf("ARC4 %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("ARC4 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -203,7 +203,7 @@ void bench_md5() double persec = 1 / total * megs; - printf("MD5 %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("MD5 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -231,7 +231,7 @@ void bench_sha() double persec = 1 / total * megs; - printf("SHA %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("SHA %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -253,7 +253,7 @@ void bench_ripemd() double persec = 1 / total * megs; - printf("RIPEMD %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, + printf("RIPEMD %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total, persec); } @@ -276,7 +276,7 @@ void bench_rsa() byte message[] = "Everyone gets Friday off."; byte cipher[128]; // for 1024 bit byte plain[128]; // for 1024 bit - const int len = strlen((char*)message); + const int len = (word32)strlen((char*)message); int i; double start = current_time(); diff --git a/extra/yassl/taocrypt/benchmark/benchmark.dsp b/extra/yassl/taocrypt/benchmark/benchmark.dsp index 878dc2b2783..449299a1c8d 100644 --- a/extra/yassl/taocrypt/benchmark/benchmark.dsp +++ b/extra/yassl/taocrypt/benchmark/benchmark.dsp @@ -49,8 +49,8 @@ BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 !ELSEIF "$(CFG)" == "benchmark - Win32 Debug" @@ -64,7 +64,7 @@ LINK32=link.exe # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # PROP Target_Dir "" -# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "..\include" /I "..\mySTL" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG" @@ -72,8 +72,8 @@ BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept !ENDIF diff --git a/extra/yassl/taocrypt/benchmark/dh1024.der b/extra/yassl/taocrypt/benchmark/dh1024.der Binary files differindex 1a85d90f3f7..09f81ee14c1 100644 --- a/extra/yassl/taocrypt/benchmark/dh1024.der +++ b/extra/yassl/taocrypt/benchmark/dh1024.der diff --git a/extra/yassl/taocrypt/benchmark/dsa1024.der b/extra/yassl/taocrypt/benchmark/dsa1024.der Binary files differindex 1fcb37fad6a..3a6dfca4b8f 100644 --- a/extra/yassl/taocrypt/benchmark/dsa1024.der +++ b/extra/yassl/taocrypt/benchmark/dsa1024.der diff --git a/extra/yassl/taocrypt/benchmark/make.bat b/extra/yassl/taocrypt/benchmark/make.bat index 1457521bce9..1457521bce9 100644..100755 --- a/extra/yassl/taocrypt/benchmark/make.bat +++ b/extra/yassl/taocrypt/benchmark/make.bat diff --git a/extra/yassl/taocrypt/benchmark/rsa1024.der b/extra/yassl/taocrypt/benchmark/rsa1024.der Binary files differindex 8fc91814472..5ba3fbe6c9c 100644 --- a/extra/yassl/taocrypt/benchmark/rsa1024.der +++ b/extra/yassl/taocrypt/benchmark/rsa1024.der diff --git a/extra/yassl/taocrypt/certs/client-cert.der b/extra/yassl/taocrypt/certs/client-cert.der Binary files differnew file mode 100644 index 00000000000..9c2ef138bf6 --- /dev/null +++ b/extra/yassl/taocrypt/certs/client-cert.der diff --git a/extra/yassl/taocrypt/certs/client-key.der b/extra/yassl/taocrypt/certs/client-key.der Binary files differnew file mode 100644 index 00000000000..649406c4417 --- /dev/null +++ b/extra/yassl/taocrypt/certs/client-key.der diff --git a/extra/yassl/taocrypt/certs/dh1024.dat b/extra/yassl/taocrypt/certs/dh1024.dat new file mode 100644 index 00000000000..86a95518278 --- /dev/null +++ b/extra/yassl/taocrypt/certs/dh1024.dat @@ -0,0 +1 @@ +30818702818100DA9A18547FF03B385CC16508C173A7EF4EB61CB40EF8FEF3B31F145051676166BCDC3FE6B799FC394D08C26385F9413F896E09117E46209D6923602683CEA100924A6EE695281775C619DAA94EA8CB3691B4275B0183F1D39639EBC92995FE645D6C1BC28D409E585549BBD2C5DCDD6C208B04EADD8B7A6D997F72CBAD88390F020102
\ No newline at end of file diff --git a/extra/yassl/taocrypt/certs/dsa512.der b/extra/yassl/taocrypt/certs/dsa512.der Binary files differnew file mode 100644 index 00000000000..027bedeffb1 --- /dev/null +++ b/extra/yassl/taocrypt/certs/dsa512.der diff --git a/extra/yassl/taocrypt/include/asn.hpp b/extra/yassl/taocrypt/include/asn.hpp index ece7140741a..c20387d86c7 100644 --- a/extra/yassl/taocrypt/include/asn.hpp +++ b/extra/yassl/taocrypt/include/asn.hpp @@ -167,6 +167,7 @@ public: void Decode(RSA_PublicKey&); private: void ReadHeader(); + void ReadHeaderOpenSSL(); }; diff --git a/extra/yassl/taocrypt/include/block.hpp b/extra/yassl/taocrypt/include/block.hpp index 9885c44d2c9..77017e9d5e4 100644 --- a/extra/yassl/taocrypt/include/block.hpp +++ b/extra/yassl/taocrypt/include/block.hpp @@ -61,10 +61,6 @@ public: void destroy(pointer p) {p->~T();} size_type max_size() const {return ~size_type(0)/sizeof(T);} protected: - static void CheckSize(size_t n) - { - assert(n <= ~size_t(0) / sizeof(T)); - } }; @@ -101,7 +97,8 @@ public: pointer allocate(size_type n, const void* = 0) { - this->CheckSize(n); + if (n > this->max_size()) + return 0; if (n == 0) return 0; return NEW_TC T[n]; @@ -144,9 +141,8 @@ public: return *this; } - T& operator[] (word32 i) { assert(i < sz_); return buffer_[i]; } - const T& operator[] (word32 i) const - { assert(i < sz_); return buffer_[i]; } + T& operator[] (word32 i) { return buffer_[i]; } + const T& operator[] (word32 i) const { return buffer_[i]; } T* operator+ (word32 i) { return buffer_ + i; } const T* operator+ (word32 i) const { return buffer_ + i; } diff --git a/extra/yassl/taocrypt/include/blowfish.hpp b/extra/yassl/taocrypt/include/blowfish.hpp index e03ed41aab8..e868872d3cf 100644 --- a/extra/yassl/taocrypt/include/blowfish.hpp +++ b/extra/yassl/taocrypt/include/blowfish.hpp @@ -59,11 +59,11 @@ public: void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION); void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); } private: - static const word32 p_init_[ROUNDS + 2]; - static const word32 s_init_[4 * 256]; + static const word32 p_init_[ROUNDS + 2]; + static const word32 s_init_[4 * 256]; - word32 pbox_[ROUNDS + 2 + 4 * 256]; - word32* sbox_; + word32 pbox_[ROUNDS + 2 + 4 * 256]; + word32* sbox_; void crypt_block(const word32 in[2], word32 out[2]) const; void AsmProcess(const byte* in, byte* out) const; diff --git a/extra/yassl/taocrypt/include/des.hpp b/extra/yassl/taocrypt/include/des.hpp index f99a289392f..2bc41fc2628 100644 --- a/extra/yassl/taocrypt/include/des.hpp +++ b/extra/yassl/taocrypt/include/des.hpp @@ -84,7 +84,7 @@ private: // DES_EDE3 class DES_EDE3 : public Mode_BASE { public: - DES_EDE3(CipherDir DIR, Mode MODE) + DES_EDE3(CipherDir DIR, Mode MODE) : Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {} void SetKey(const byte*, word32, CipherDir dir); diff --git a/extra/yassl/taocrypt/include/file.hpp b/extra/yassl/taocrypt/include/file.hpp index e22040f60f0..c340c4fa3ea 100644 --- a/extra/yassl/taocrypt/include/file.hpp +++ b/extra/yassl/taocrypt/include/file.hpp @@ -1,5 +1,5 @@ /* - Copyright (C) 2000-2007 MySQL AB + Copyright (C) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 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 @@ -39,25 +39,32 @@ public: explicit Source(word32 sz = 0) : buffer_(sz), current_(0) {} Source(const byte* b, word32 sz) : buffer_(b, sz), current_(0) {} + word32 remaining() { if (GetError().What()) return 0; + else return buffer_.size() - current_; } word32 size() const { return buffer_.size(); } void grow(word32 sz) { buffer_.CleanGrow(sz); } + + bool IsLeft(word32 sz) { if (remaining() >= sz) return true; + else { SetError(CONTENT_E); return false; } } const byte* get_buffer() const { return buffer_.get_buffer(); } const byte* get_current() const { return &buffer_[current_]; } word32 get_index() const { return current_; } - void set_index(word32 i) { current_ = i; } + void set_index(word32 i) { if (i < size()) current_ = i; } byte operator[] (word32 i) { current_ = i; return next(); } - byte next() { return buffer_[current_++]; } - byte prev() { return buffer_[--current_]; } + byte next() { if (IsLeft(1)) return buffer_[current_++]; else return 0; } + byte prev() { if (current_) return buffer_[--current_]; else return 0; } void add(const byte* data, word32 len) { - memcpy(buffer_.get_buffer() + current_, data, len); - current_ += len; + if (IsLeft(len)) { + memcpy(buffer_.get_buffer() + current_, data, len); + current_ += len; + } } - void advance(word32 i) { current_ += i; } + void advance(word32 i) { if (IsLeft(i)) current_ += i; } void reset(ByteBlock&); Error GetError() { return error_; } diff --git a/extra/yassl/taocrypt/include/hash.hpp b/extra/yassl/taocrypt/include/hash.hpp index fa5f6c04720..4ebc82e073d 100644 --- a/extra/yassl/taocrypt/include/hash.hpp +++ b/extra/yassl/taocrypt/include/hash.hpp @@ -63,8 +63,8 @@ protected: word32 buffLen_; // in bytes HashLengthType loLen_; // length in bytes HashLengthType hiLen_; // length in bytes - word32 digest_[MaxDigestSz]; - word32 buffer_[MaxBufferSz / sizeof(word32)]; + word32 digest_[MaxDigestSz]; + word32 buffer_[MaxBufferSz / sizeof(word32)]; virtual void Transform() = 0; diff --git a/extra/yassl/taocrypt/include/hc128.hpp b/extra/yassl/taocrypt/include/hc128.hpp new file mode 100644 index 00000000000..d1cf5f075f2 --- /dev/null +++ b/extra/yassl/taocrypt/include/hc128.hpp @@ -0,0 +1,63 @@ +/* + Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + + 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + 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. +*/ + +/* hc128.hpp defines HC128 +*/ + + +#ifndef TAO_CRYPT_HC128_HPP +#define TAO_CRYPT_HC128_HPP + +#include "misc.hpp" + +namespace TaoCrypt { + + +// HC128 encryption and decryption +class HC128 { +public: + + typedef HC128 Encryption; + typedef HC128 Decryption; + + + HC128() {} + + void Process(byte*, const byte*, word32); + void SetKey(const byte*, const byte*); +private: + word32 T_[1024]; /* P[i] = T[i]; Q[i] = T[1024 + i ]; */ + word32 X_[16]; + word32 Y_[16]; + word32 counter1024_; /* counter1024 = i mod 1024 at the ith step */ + word32 key_[8]; + word32 iv_[8]; + + void SetIV(const byte*); + void GenerateKeystream(word32*); + void SetupUpdate(); + + HC128(const HC128&); // hide copy + const HC128 operator=(const HC128&); // and assign +}; + +} // namespace + + +#endif // TAO_CRYPT_HC128_HPP + diff --git a/extra/yassl/taocrypt/include/integer.hpp b/extra/yassl/taocrypt/include/integer.hpp index 186d62bb866..256573cf622 100644 --- a/extra/yassl/taocrypt/include/integer.hpp +++ b/extra/yassl/taocrypt/include/integer.hpp @@ -110,13 +110,6 @@ namespace TaoCrypt { #endif -// general MIN -template<typename T> inline -const T& min(const T& a, const T& b) -{ - return a < b ? a : b; -} - // general MAX template<typename T> inline diff --git a/extra/yassl/taocrypt/include/kernelc.hpp b/extra/yassl/taocrypt/include/kernelc.hpp index daa3762d5dd..5bdf1cffa3f 100644 --- a/extra/yassl/taocrypt/include/kernelc.hpp +++ b/extra/yassl/taocrypt/include/kernelc.hpp @@ -30,17 +30,5 @@ extern "C" void* memcpy(void*, const void*, size_t); extern "C" void* memset(void*, int, size_t); extern "C" void printk(char *fmt, ...); -#define KERN_ERR "<3>" /* error conditions */ - -#if defined(NDEBUG) - #define assert(p) ((void)0) -#else - #define assert(expr) \ - if (!(expr)) { \ - printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \ - #expr,__FILE__,__FUNCTION__,__LINE__); } -#endif - - #endif // TAOCRYPT_KERNELC_HPP diff --git a/extra/yassl/taocrypt/include/misc.hpp b/extra/yassl/taocrypt/include/misc.hpp index a375b17eb1e..b6925f916f8 100644 --- a/extra/yassl/taocrypt/include/misc.hpp +++ b/extra/yassl/taocrypt/include/misc.hpp @@ -24,7 +24,6 @@ #if !defined(DO_TAOCRYPT_KERNEL_MODE) #include <stdlib.h> - #include <assert.h> #include <string.h> #else #include "kernelc.hpp" @@ -62,30 +61,30 @@ void CleanUp(); template<typename T> void tcDelete(T* ptr) { - if (ptr) ptr->~T(); - ::operator delete(ptr, TaoCrypt::tc); + if (ptr) ptr->~T(); + ::operator delete(ptr, TaoCrypt::tc); } template<typename T> void tcArrayDelete(T* ptr) { - // can't do array placement destruction since not tracking size in - // allocation, only allow builtins to use array placement since they - // don't need destructors called - typedef char builtin[IsFundamentalType<T>::Yes ? 1 : -1]; - (void)sizeof(builtin); + // can't do array placement destruction since not tracking size in + // allocation, only allow builtins to use array placement since they + // don't need destructors called + typedef char builtin[IsFundamentalType<T>::Yes ? 1 : -1]; + (void)sizeof(builtin); - ::operator delete[](ptr, TaoCrypt::tc); + ::operator delete[](ptr, TaoCrypt::tc); } #define NEW_TC new (TaoCrypt::tc) // to resolve compiler generated operator delete on base classes with - // virtual destructors (when on stack), make sure doesn't get called + // virtual destructors (when on stack) class virtual_base { public: - static void operator delete(void*) { assert(0); } + static void operator delete(void*) { } }; #else // YASSL_PURE_C @@ -366,7 +365,6 @@ inline bool IsPowerOf2(T n) template <class T1, class T2> inline T2 ModPowerOf2(T1 a, T2 b) { - assert(IsPowerOf2(b)); return T2(a) & (b-1); } @@ -409,14 +407,12 @@ inline bool IsAligned(const void* p, T* dummy = 0) // VC60 workaround template <class T> inline T rotlFixed(T x, unsigned int y) { - assert(y < sizeof(T)*8); - return (x<<y) | (x>>(sizeof(T)*8-y)); + return (x<<y) | (x>>(sizeof(T)*8-y)); } template <class T> inline T rotrFixed(T x, unsigned int y) { - assert(y < sizeof(T)*8); - return (x>>y) | (x<<(sizeof(T)*8-y)); + return (x>>y) | (x<<(sizeof(T)*8-y)); } #ifdef INTEL_INTRINSICS @@ -425,13 +421,11 @@ template <class T> inline T rotrFixed(T x, unsigned int y) template<> inline word32 rotlFixed(word32 x, word32 y) { - assert(y < 32); return y ? _lrotl(x, y) : x; } template<> inline word32 rotrFixed(word32 x, word32 y) { - assert(y < 32); return y ? _lrotr(x, y) : x; } @@ -441,7 +435,9 @@ template<> inline word32 rotrFixed(word32 x, word32 y) #undef min #endif -inline word32 min(word32 a, word32 b) + +template <class T> +inline const T& min(const T& a, const T& b) { return a < b ? a : b; } @@ -486,7 +482,6 @@ inline word64 ByteReverse(word64 value) template <typename T> inline void ByteReverse(T* out, const T* in, word32 byteCount) { - assert(byteCount % sizeof(T) == 0); word32 count = byteCount/sizeof(T); for (word32 i=0; i<count; i++) out[i] = ByteReverse(in[i]); @@ -574,7 +569,6 @@ inline void GetUserKey(ByteOrder order, T* out, word32 outlen, const byte* in, word32 inlen) { const unsigned int U = sizeof(T); - assert(inlen <= outlen*U); memcpy(out, in, inlen); memset((byte *)out+inlen, 0, outlen*U-inlen); ByteReverseIf(out, out, RoundUpToMultipleOf(inlen, U), order); @@ -583,7 +577,8 @@ inline void GetUserKey(ByteOrder order, T* out, word32 outlen, const byte* in, #ifdef _MSC_VER // disable conversion warning - #pragma warning(disable:4244) + // 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy + #pragma warning(disable:4244 4996) #endif @@ -678,10 +673,7 @@ template <class T> inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block) { if (assumeAligned) - { - assert(IsAligned<T>(block)); return ByteReverseIf(*reinterpret_cast<const T *>(block), order); - } else return UnalignedGetWord<T>(order, block); } @@ -699,7 +691,6 @@ inline void PutWord(bool assumeAligned, ByteOrder order, byte* block, T value, { if (assumeAligned) { - assert(IsAligned<T>(block)); if (xorBlock) *reinterpret_cast<T *>(block) = ByteReverseIf(value, order) ^ *reinterpret_cast<const T *>(xorBlock); @@ -812,7 +803,6 @@ inline T SafeLeftShift(T value) inline word ShiftWordsLeftByBits(word* r, unsigned int n, unsigned int shiftBits) { - assert (shiftBits<WORD_BITS); word u, carry=0; if (shiftBits) for (unsigned int i=0; i<n; i++) @@ -828,7 +818,6 @@ word ShiftWordsLeftByBits(word* r, unsigned int n, unsigned int shiftBits) inline word ShiftWordsRightByBits(word* r, unsigned int n, unsigned int shiftBits) { - assert (shiftBits<WORD_BITS); word u, carry=0; if (shiftBits) for (int i=n-1; i>=0; i--) diff --git a/extra/yassl/taocrypt/include/modes.hpp b/extra/yassl/taocrypt/include/modes.hpp index 2285bd0e845..5fd36b80d3e 100644 --- a/extra/yassl/taocrypt/include/modes.hpp +++ b/extra/yassl/taocrypt/include/modes.hpp @@ -61,9 +61,7 @@ public: explicit Mode_BASE(int sz, CipherDir dir, Mode mode) : blockSz_(sz), reg_(reinterpret_cast<byte*>(r_)), tmp_(reinterpret_cast<byte*>(t_)), dir_(dir), mode_(mode) - { - assert(sz <= MaxBlockSz); - } + {} virtual ~Mode_BASE() {} virtual void Process(byte*, const byte*, word32); @@ -96,8 +94,7 @@ inline void Mode_BASE::Process(byte* out, const byte* in, word32 sz) { if (mode_ == ECB) ECB_Process(out, in, sz); - else if (mode_ == CBC) - { + else if (mode_ == CBC) { if (dir_ == ENCRYPTION) CBC_Encrypt(out, in, sz); else diff --git a/extra/yassl/taocrypt/include/pwdbased.hpp b/extra/yassl/taocrypt/include/pwdbased.hpp index 5ece1a8f43b..f40a336e2c3 100644 --- a/extra/yassl/taocrypt/include/pwdbased.hpp +++ b/extra/yassl/taocrypt/include/pwdbased.hpp @@ -48,8 +48,9 @@ word32 PBKDF2_HMAC<T>::DeriveKey(byte* derived, word32 dLen, const byte* pwd, word32 pLen, const byte* salt, word32 sLen, word32 iterations) const { - assert(dLen <= MaxDerivedKeyLength()); - assert(iterations > 0); + if (dLen > MaxDerivedKeyLength()) + return 0; + ByteBlock buffer(T::DIGEST_SIZE); HMAC<T> hmac; diff --git a/extra/yassl/taocrypt/include/rabbit.hpp b/extra/yassl/taocrypt/include/rabbit.hpp new file mode 100644 index 00000000000..1e7276dd8ec --- /dev/null +++ b/extra/yassl/taocrypt/include/rabbit.hpp @@ -0,0 +1,65 @@ +/* + Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + + 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + 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. +*/ + +/* rabbit.hpp defines Rabbit +*/ + + +#ifndef TAO_CRYPT_RABBIT_HPP +#define TAO_CRYPT_RABBIT_HPP + +#include "misc.hpp" + +namespace TaoCrypt { + + +// Rabbit encryption and decryption +class Rabbit { +public: + + typedef Rabbit Encryption; + typedef Rabbit Decryption; + + enum RabbitCtx { Master = 0, Work = 1 }; + + Rabbit() {} + + void Process(byte*, const byte*, word32); + void SetKey(const byte*, const byte*); +private: + struct Ctx { + word32 x[8]; + word32 c[8]; + word32 carry; + }; + + Ctx masterCtx_; + Ctx workCtx_; + + void NextState(RabbitCtx); + void SetIV(const byte*); + + Rabbit(const Rabbit&); // hide copy + const Rabbit operator=(const Rabbit&); // and assign +}; + +} // namespace + + +#endif // TAO_CRYPT_RABBIT_HPP + diff --git a/extra/yassl/taocrypt/include/rsa.hpp b/extra/yassl/taocrypt/include/rsa.hpp index c895ab6fd34..11a4ccfd039 100644 --- a/extra/yassl/taocrypt/include/rsa.hpp +++ b/extra/yassl/taocrypt/include/rsa.hpp @@ -178,7 +178,8 @@ void RSA_Encryptor<Pad>::Encrypt(const byte* plain, word32 sz, byte* cipher, RandomNumberGenerator& rng) { PK_Lengths lengths(key_.GetModulus()); - assert(sz <= lengths.FixedMaxPlaintextLength()); + if (sz > lengths.FixedMaxPlaintextLength()) + return; ByteBlock paddedBlock(lengths.PaddedBlockByteLength()); padding_.Pad(plain, sz, paddedBlock.get_buffer(), @@ -195,7 +196,6 @@ word32 RSA_Decryptor<Pad>::Decrypt(const byte* cipher, word32 sz, byte* plain, RandomNumberGenerator& rng) { PK_Lengths lengths(key_.GetModulus()); - assert(sz == lengths.FixedCiphertextLength()); if (sz != lengths.FixedCiphertextLength()) return 0; diff --git a/extra/yassl/taocrypt/include/runtime.hpp b/extra/yassl/taocrypt/include/runtime.hpp index 3b9d0d9b095..328c8e9e978 100644 --- a/extra/yassl/taocrypt/include/runtime.hpp +++ b/extra/yassl/taocrypt/include/runtime.hpp @@ -31,7 +31,6 @@ #ifdef __sun -#include <assert.h> // Handler for pure virtual functions namespace __Crun { @@ -46,9 +45,7 @@ namespace __Crun { #if __GNUC__ > 2 extern "C" { -#if !defined(DO_TAOCRYPT_KERNEL_MODE) - #include <assert.h> -#else +#if defined(DO_TAOCRYPT_KERNEL_MODE) #include "kernelc.hpp" #endif int __cxa_pure_virtual () __attribute__ ((weak)); diff --git a/extra/yassl/taocrypt/include/types.hpp b/extra/yassl/taocrypt/include/types.hpp index 3efdcdfbccb..5d8bc9f0683 100644 --- a/extra/yassl/taocrypt/include/types.hpp +++ b/extra/yassl/taocrypt/include/types.hpp @@ -62,7 +62,7 @@ typedef unsigned int word32; // compilers we've found 64-bit multiply insructions for #if defined(__GNUC__) || defined(_MSC_VER) || defined(__DECCXX) #if !(defined(__ICC) || defined(__INTEL_COMPILER)) - #define HAVE_64_MULTIPLY + #define HAVE_64_MULTIPLY #endif #endif diff --git a/extra/yassl/taocrypt/mySTL/stdexcept.hpp b/extra/yassl/taocrypt/mySTL/stdexcept.hpp index 9696995248d..15533eebc02 100644 --- a/extra/yassl/taocrypt/mySTL/stdexcept.hpp +++ b/extra/yassl/taocrypt/mySTL/stdexcept.hpp @@ -26,7 +26,6 @@ #include <string.h> // strncpy -#include <assert.h> // assert #include <stdlib.h> // size_t @@ -41,7 +40,7 @@ public: virtual const char* what() const { return ""; } // for compiler generated call, never used - static void operator delete(void*) { assert(0); } + static void operator delete(void*) { } private: // don't allow dynamic creation of exceptions static void* operator new(size_t); diff --git a/extra/yassl/taocrypt/mySTL/vector.hpp b/extra/yassl/taocrypt/mySTL/vector.hpp index 8ba8813ca70..35b92610942 100644 --- a/extra/yassl/taocrypt/mySTL/vector.hpp +++ b/extra/yassl/taocrypt/mySTL/vector.hpp @@ -26,7 +26,6 @@ #include "helpers.hpp" // construct, destory, fill, etc. #include "algorithm.hpp" // swap -#include <assert.h> // assert namespace mySTL { @@ -141,9 +140,9 @@ private: // for growing, n must be bigger than other size vector(size_t n, const vector& other) : vec_(n) { - assert(n > other.size()); - vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_, - vec_.start_); + if (n > other.size()) + vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_, + vec_.start_); } }; diff --git a/extra/yassl/taocrypt/src/aes.cpp b/extra/yassl/taocrypt/src/aes.cpp index bf40f414eaa..3b09c5280bf 100644 --- a/extra/yassl/taocrypt/src/aes.cpp +++ b/extra/yassl/taocrypt/src/aes.cpp @@ -79,7 +79,7 @@ void AES::Process(byte* out, const byte* in, word32 sz) out += BLOCK_SIZE; in += BLOCK_SIZE; } - } + } } #endif // DO_AES_ASM @@ -87,8 +87,13 @@ void AES::Process(byte* out, const byte* in, word32 sz) void AES::SetKey(const byte* userKey, word32 keylen, CipherDir /*dummy*/) { - assert( (keylen == 16) || (keylen == 24) || (keylen == 32) ); - + if (keylen <= 16) + keylen = 16; + else if (keylen >= 32) + keylen = 32; + else if (keylen != 24) + keylen = 24; + rounds_ = keylen/4 + 6; word32 temp, *rk = key_; @@ -246,34 +251,34 @@ void AES::encrypt(const byte* inBlock, const byte* xorBlock, for (;;) { t0 = Te0[GETBYTE(s0, 3)] ^ - Te1[GETBYTE(s1, 2)] ^ - Te2[GETBYTE(s2, 1)] ^ - Te3[GETBYTE(s3, 0)] ^ + Te1[GETBYTE(s1, 2)] ^ + Te2[GETBYTE(s2, 1)] ^ + Te3[GETBYTE(s3, 0)] ^ rk[4]; t1 = Te0[GETBYTE(s1, 3)] ^ - Te1[GETBYTE(s2, 2)] ^ - Te2[GETBYTE(s3, 1)] ^ - Te3[GETBYTE(s0, 0)] ^ + Te1[GETBYTE(s2, 2)] ^ + Te2[GETBYTE(s3, 1)] ^ + Te3[GETBYTE(s0, 0)] ^ rk[5]; t2 = Te0[GETBYTE(s2, 3)] ^ - Te1[GETBYTE(s3, 2)] ^ - Te2[GETBYTE(s0, 1)] ^ - Te3[GETBYTE(s1, 0)] ^ + Te1[GETBYTE(s3, 2)] ^ + Te2[GETBYTE(s0, 1)] ^ + Te3[GETBYTE(s1, 0)] ^ rk[6]; t3 = Te0[GETBYTE(s3, 3)] ^ - Te1[GETBYTE(s0, 2)] ^ - Te2[GETBYTE(s1, 1)] ^ - Te3[GETBYTE(s2, 0)] ^ + Te1[GETBYTE(s0, 2)] ^ + Te2[GETBYTE(s1, 1)] ^ + Te3[GETBYTE(s2, 0)] ^ rk[7]; rk += 8; if (--r == 0) { break; } - + s0 = Te0[GETBYTE(t0, 3)] ^ Te1[GETBYTE(t1, 2)] ^ @@ -422,7 +427,7 @@ void AES::decrypt(const byte* inBlock, const byte* xorBlock, (Td4[GETBYTE(t3, 2)] & 0x00ff0000) ^ (Td4[GETBYTE(t2, 1)] & 0x0000ff00) ^ (Td4[GETBYTE(t1, 0)] & 0x000000ff) ^ - rk[0]; + rk[0]; s1 = (Td4[GETBYTE(t1, 3)] & 0xff000000) ^ (Td4[GETBYTE(t0, 2)] & 0x00ff0000) ^ diff --git a/extra/yassl/taocrypt/src/aestables.cpp b/extra/yassl/taocrypt/src/aestables.cpp index e9d6b7a80cb..05c2c3b8e62 100644 --- a/extra/yassl/taocrypt/src/aestables.cpp +++ b/extra/yassl/taocrypt/src/aestables.cpp @@ -33,6 +33,4 @@ const word32 AES::rcon_[] = { }; - - } // namespace diff --git a/extra/yassl/taocrypt/src/algebra.cpp b/extra/yassl/taocrypt/src/algebra.cpp index b4afda44535..29754b27b5e 100644 --- a/extra/yassl/taocrypt/src/algebra.cpp +++ b/extra/yassl/taocrypt/src/algebra.cpp @@ -246,7 +246,6 @@ void AbstractGroup::SimultaneousMultiply(Integer *results, const Integer &base, for (i=0; i<expCount; i++) { - assert(expBegin->NotNegative()); exponents.push_back(WindowSlider(*expBegin++, InversionIsFast(), 0)); exponents[i].FindNextWindow(); buckets[i].resize(1<<(exponents[i].windowSize-1), Identity()); @@ -287,7 +286,7 @@ void AbstractGroup::SimultaneousMultiply(Integer *results, const Integer &base, r = buckets[i][buckets[i].size()-1]; if (buckets[i].size() > 1) { - for (int j= (unsigned int) (buckets[i].size()) - 2; j >= 1; j--) + for (size_t j = buckets[i].size()-2; j >= 1; j--) { Accumulate(buckets[i][j], buckets[i][j+1]); Accumulate(r, buckets[i][j]); diff --git a/extra/yassl/taocrypt/src/arc4.cpp b/extra/yassl/taocrypt/src/arc4.cpp index 0944cc31837..4630adcb11a 100644 --- a/extra/yassl/taocrypt/src/arc4.cpp +++ b/extra/yassl/taocrypt/src/arc4.cpp @@ -104,7 +104,7 @@ void ARC4::Process(byte* out, const byte* in, word32 length) #ifdef DO_ARC4_ASM #ifdef _MSC_VER - __declspec(naked) + __declspec(naked) #else __attribute__ ((noinline)) #endif diff --git a/extra/yassl/taocrypt/src/asn.cpp b/extra/yassl/taocrypt/src/asn.cpp index 1b81db4f0a4..a502666d15b 100644 --- a/extra/yassl/taocrypt/src/asn.cpp +++ b/extra/yassl/taocrypt/src/asn.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2005-2007 MySQL AB, 2009, 2010 Sun Microsystems, Inc. + Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. This program is free software; you can redistribute it and/or modify @@ -111,7 +111,8 @@ bool ValidateDate(const byte* date, byte format, CertDecoder::DateType dt) GetTime(certTime.tm_min, date, i); GetTime(certTime.tm_sec, date, i); - assert(date[i] == 'Z'); // only Zulu supported for this profile + if (date[i] != 'Z') // only Zulu supported for this profile + return false; time_t ltime = time(0); tm* localTime = gmtime(<ime); @@ -143,6 +144,8 @@ word32 GetLength(Source& source) if (b >= LONG_LENGTH) { word32 bytes = b & 0x7F; + if (source.IsLeft(bytes) == false) return 0; + while (bytes--) { b = source.next(); length = (length << 8) | b; @@ -214,9 +217,9 @@ void PublicKey::AddToEnd(const byte* data, word32 len) Signer::Signer(const byte* k, word32 kSz, const char* n, const byte* h) : key_(k, kSz) { - size_t sz = strlen(n); - memcpy(name_, n, sz); - name_[sz] = 0; + size_t sz = strlen(n); + memcpy(name_, n, sz); + name_[sz] = 0; memcpy(hash_, h, SHA::DIGEST_SIZE); } @@ -364,12 +367,59 @@ void RSA_Public_Decoder::Decode(RSA_PublicKey& key) ReadHeader(); if (source_.GetError().What()) return; + ReadHeaderOpenSSL(); // may or may not be + if (source_.GetError().What()) return; + // public key key.SetModulus(GetInteger(Integer().Ref())); key.SetPublicExponent(GetInteger(Integer().Ref())); } +// Read OpenSSL format public header +void RSA_Public_Decoder::ReadHeaderOpenSSL() +{ + byte b = source_.next(); // peek + source_.prev(); + + if (b != INTEGER) { // have OpenSSL public format + GetSequence(); + b = source_.next(); + if (b != OBJECT_IDENTIFIER) { + source_.SetError(OBJECT_ID_E); + return; + } + + word32 len = GetLength(source_); + source_.advance(len); + + b = source_.next(); + if (b == TAG_NULL) { // could have NULL tag and 0 terminator, may not + b = source_.next(); + if (b != 0) { + source_.SetError(EXPECT_0_E); + return; + } + } + else + source_.prev(); // put back + + b = source_.next(); + if (b != BIT_STRING) { + source_.SetError(BIT_STR_E); + return; + } + + len = GetLength(source_); + b = source_.next(); + if (b != 0) // could have 0 + source_.prev(); // put back + + GetSequence(); + } +} + + void RSA_Public_Decoder::ReadHeader() { GetSequence(); @@ -420,12 +470,12 @@ CertDecoder::CertDecoder(Source& s, bool decode, SignerList* signers, bool noVerify, CertType ct) : BER_Decoder(s), certBegin_(0), sigIndex_(0), sigLength_(0), signature_(0), verify_(!noVerify) -{ +{ issuer_[0] = 0; subject_[0] = 0; if (decode) - Decode(signers, ct); + Decode(signers, ct); } @@ -470,9 +520,9 @@ void CertDecoder::Decode(SignerList* signers, CertType ct) source_.SetError(SIG_OID_E); return; } - + if (ct != CA && verify_ && !ValidateSignature(signers)) - source_.SetError(SIG_OTHER_E); + source_.SetError(SIG_OTHER_E); } @@ -530,8 +580,10 @@ void CertDecoder::StoreKey() read = source_.get_index() - read; length += read; + if (source_.GetError().What()) return; while (read--) source_.prev(); + if (source_.IsLeft(length) == false) return; key_.SetSize(length); key_.SetKey(source_.get_current()); source_.advance(length); @@ -563,6 +615,8 @@ void CertDecoder::AddDSA() word32 length = GetLength(source_); length += source_.get_index() - idx; + if (source_.IsLeft(length) == false) return; + key_.AddToEnd(source_.get_buffer() + idx, length); } @@ -572,6 +626,8 @@ word32 CertDecoder::GetAlgoId() { if (source_.GetError().What()) return 0; word32 length = GetSequence(); + + if (source_.GetError().What()) return 0; byte b = source_.next(); if (b != OBJECT_IDENTIFIER) { @@ -580,26 +636,25 @@ word32 CertDecoder::GetAlgoId() } length = GetLength(source_); + if (source_.IsLeft(length) == false) return 0; + word32 oid = 0; - while(length--) oid += source_.next(); // just sum it up for now - if (oid != SHAwDSA && oid != DSAk) { - b = source_.next(); // should have NULL tag and 0 - - if (b != TAG_NULL) { - source_.SetError(TAG_NULL_E); - return 0; - } - + // could have NULL tag and 0 terminator, but may not + b = source_.next(); + if (b == TAG_NULL) { b = source_.next(); if (b != 0) { source_.SetError(EXPECT_0_E); return 0; } } - + else + // go back, didn't have it + b = source_.prev(); + return oid; } @@ -616,6 +671,10 @@ word32 CertDecoder::GetSignature() } sigLength_ = GetLength(source_); + if (sigLength_ == 0 || source_.IsLeft(sigLength_) == false) { + source_.SetError(CONTENT_E); + return 0; + } b = source_.next(); if (b != 0) { @@ -653,20 +712,22 @@ word32 CertDecoder::GetDigest() } -char *CertDecoder::AddTag(char *ptr, const char *buf_end, - const char *tag_name, word32 tag_name_length, - word32 tag_value_length) +// memory length checked add tag to buffer +char* CertDecoder::AddTag(char* ptr, const char* buf_end, const char* tag_name, + word32 tag_name_length, word32 tag_value_length) { - if (ptr + tag_name_length + tag_value_length > buf_end) - return 0; - - memcpy(ptr, tag_name, tag_name_length); - ptr+= tag_name_length; - - memcpy(ptr, source_.get_current(), tag_value_length); - ptr+= tag_value_length; - - return ptr; + if (ptr + tag_name_length + tag_value_length > buf_end) { + source_.SetError(CONTENT_E); + return 0; + } + + memcpy(ptr, tag_name, tag_name_length); + ptr += tag_name_length; + + memcpy(ptr, source_.get_current(), tag_value_length); + ptr += tag_value_length; + + return ptr; } @@ -679,18 +740,20 @@ void CertDecoder::GetName(NameType nt) word32 length = GetSequence(); // length of all distinguished names if (length >= ASN_NAME_MAX) - goto err; + return; + if (source_.IsLeft(length) == false) return; length += source_.get_index(); - - char *ptr, *buf_end; + + char* ptr; + char* buf_end; if (nt == ISSUER) { - ptr= issuer_; - buf_end= ptr + sizeof(issuer_) - 1; // 1 byte for trailing 0 + ptr = issuer_; + buf_end = ptr + sizeof(issuer_) - 1; // 1 byte for trailing 0 } else { - ptr= subject_; - buf_end= ptr + sizeof(subject_) - 1; // 1 byte for trailing 0 + ptr = subject_; + buf_end = ptr + sizeof(subject_) - 1; // 1 byte for trailing 0 } while (source_.get_index() < length) { @@ -704,7 +767,10 @@ void CertDecoder::GetName(NameType nt) } word32 oidSz = GetLength(source_); + if (source_.IsLeft(oidSz) == false) return; + byte joint[2]; + if (source_.IsLeft(sizeof(joint)) == false) return; memcpy(joint, source_.get_current(), sizeof(joint)); // v1 name types @@ -714,62 +780,68 @@ void CertDecoder::GetName(NameType nt) b = source_.next(); // strType word32 strLen = GetLength(source_); + if (source_.IsLeft(strLen) == false) return; + switch (id) { case COMMON_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/CN=", 4, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/CN=", 4, strLen))) + return; break; case SUR_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/SN=", 4, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/SN=", 4, strLen))) + return; break; case COUNTRY_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/C=", 3, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/C=", 3, strLen))) + return; break; case LOCALITY_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/L=", 3, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/L=", 3, strLen))) + return; break; case STATE_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/ST=", 4, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/ST=", 4, strLen))) + return; break; case ORG_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/O=", 3, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/O=", 3, strLen))) + return; break; case ORGUNIT_NAME: - if (!(ptr= AddTag(ptr, buf_end, "/OU=", 4, strLen))) - goto err; + if (!(ptr = AddTag(ptr, buf_end, "/OU=", 4, strLen))) + return; break; } sha.Update(source_.get_current(), strLen); source_.advance(strLen); } - else { + else { bool email = false; if (joint[0] == 0x2a && joint[1] == 0x86) // email id hdr email = true; source_.advance(oidSz + 1); word32 length = GetLength(source_); + if (source_.IsLeft(length) == false) return; - if (email && !(ptr= AddTag(ptr, buf_end, "/emailAddress=", 14, length))) - goto err; + if (email) { + if (!(ptr = AddTag(ptr, buf_end, "/emailAddress=", 14, length))) { + source_.SetError(CONTENT_E); + return; + } + } source_.advance(length); } } - *ptr= 0; - sha.Final(nt == ISSUER ? issuerHash_ : subjectHash_); - - return; - -err: - source_.SetError(CONTENT_E); + *ptr = 0; + + if (nt == ISSUER) + sha.Final(issuerHash_); + else + sha.Final(subjectHash_); } @@ -785,6 +857,8 @@ void CertDecoder::GetDate(DateType dt) } word32 length = GetLength(source_); + if (source_.IsLeft(length) == false) return; + byte date[MAX_DATE_SZ]; if (length > MAX_DATE_SZ || length < MIN_DATE_SZ) { source_.SetError(DATE_SZ_E); @@ -794,8 +868,7 @@ void CertDecoder::GetDate(DateType dt) memcpy(date, source_.get_current(), length); source_.advance(length); - if (!ValidateDate(date, b, dt) && verify_) - { + if (!ValidateDate(date, b, dt) && verify_) { if (dt == BEFORE) source_.SetError(BEFORE_DATE_E); else @@ -856,7 +929,8 @@ void CertDecoder::GetCompareHash(const byte* plain, word32 sz, byte* digest, // validate signature signed by someone else bool CertDecoder::ValidateSignature(SignerList* signers) { - assert(signers); + if (!signers) + return false; SignerList::iterator first = signers->begin(); SignerList::iterator last = signers->end(); @@ -1077,8 +1151,7 @@ word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz) return 0; } word32 rLen = GetLength(source); - if (rLen != 20) - { + if (rLen != 20) { if (rLen == 21) { // zero at front, eat source.next(); --rLen; @@ -1101,8 +1174,7 @@ word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz) return 0; } word32 sLen = GetLength(source); - if (sLen != 20) - { + if (sLen != 20) { if (sLen == 21) { source.next(); // zero at front, eat --sLen; @@ -1123,6 +1195,7 @@ word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz) } +/* // Get Cert in PEM format from BEGIN to END int GetCert(Source& source) { @@ -1174,12 +1247,10 @@ void PKCS12_Decoder::Decode() // Get MacData optional - /* - mac digestInfo like certdecoder::getdigest? - macsalt octet string - iter integer + // mac digestInfo like certdecoder::getdigest? + // macsalt octet string + // iter integer - */ } @@ -1199,6 +1270,7 @@ int GetPKCS_Cert(const char* password, Source& source) return 0; } +*/ diff --git a/extra/yassl/taocrypt/src/blowfish.cpp b/extra/yassl/taocrypt/src/blowfish.cpp index 2e68f7ffb5c..e1c7c852d19 100644 --- a/extra/yassl/taocrypt/src/blowfish.cpp +++ b/extra/yassl/taocrypt/src/blowfish.cpp @@ -87,7 +87,10 @@ void Blowfish::Process(byte* out, const byte* in, word32 sz) void Blowfish::SetKey(const byte* key_string, word32 keylength, CipherDir dir) { - assert(keylength >= 4 && keylength <= 56); + if (keylength < 4) + keylength = 4; + else if (keylength > 56) + keylength = 56; unsigned i, j=0, k; word32 data, dspace[2] = {0, 0}; @@ -166,16 +169,21 @@ void Blowfish::crypt_block(const word32 in[2], word32 out[2]) const word32 left = in[0]; word32 right = in[1]; + const word32 *const s = sbox_; const word32* p = pbox_; - word32 tmp; left ^= p[0]; - BF_ROUNDS + // roll back up and use s and p index instead of just p + for (unsigned i = 0; i < ROUNDS / 2; i++) { + right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)]) + ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)]) + ^ p[2*i+1]; -#if ROUNDS == 20 - BF_EXTRA_ROUNDS -#endif + left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)]) + ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)]) + ^ p[2*i+2]; + } right ^= p[ROUNDS + 1]; @@ -189,17 +197,23 @@ typedef BlockGetAndPut<word32, BigEndian> gpBlock; void Blowfish::ProcessAndXorBlock(const byte* in, const byte* xOr, byte* out) const { - word32 tmp, left, right; + word32 left, right; + const word32 *const s = sbox_; const word32* p = pbox_; gpBlock::Get(in)(left)(right); left ^= p[0]; - BF_ROUNDS + // roll back up and use s and p index instead of just p + for (unsigned i = 0; i < ROUNDS / 2; i++) { + right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)]) + ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)]) + ^ p[2*i+1]; -#if ROUNDS == 20 - BF_EXTRA_ROUNDS -#endif + left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)]) + ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)]) + ^ p[2*i+2]; + } right ^= p[ROUNDS + 1]; diff --git a/extra/yassl/taocrypt/src/coding.cpp b/extra/yassl/taocrypt/src/coding.cpp index 75c8f44f636..97c62ea12a7 100644 --- a/extra/yassl/taocrypt/src/coding.cpp +++ b/extra/yassl/taocrypt/src/coding.cpp @@ -95,7 +95,6 @@ void HexEncoder::Encode() void HexDecoder::Decode() { word32 bytes = coded_.size(); - assert((bytes % 2) == 0); decoded_.New(bytes / 2); word32 i(0); @@ -104,15 +103,9 @@ void HexDecoder::Decode() byte b = coded_.next() - 0x30; // 0 starts at 0x30 byte b2 = coded_.next() - 0x30; - // sanity checks - assert( b < sizeof(hexDecode)/sizeof(hexDecode[0]) ); - assert( b2 < sizeof(hexDecode)/sizeof(hexDecode[0]) ); - b = hexDecode[b]; b2 = hexDecode[b2]; - - assert( b != bad && b2 != bad ); - + decoded_[i++] = (b << 4) | b2; bytes -= 2; } @@ -174,9 +167,9 @@ void Base64Encoder::Encode() } encoded_[i++] = '\n'; - assert(i == outSz); - - plain_.reset(encoded_); + + if (i == outSz) + plain_.reset(encoded_); } @@ -197,7 +190,6 @@ void Base64Decoder::Decode() byte e3 = coded_.next(); byte e4 = coded_.next(); - // do asserts first if (e1 == 0) // end file 0's break; diff --git a/extra/yassl/taocrypt/src/dsa.cpp b/extra/yassl/taocrypt/src/dsa.cpp index 79ee91e76f9..a4e9c9503e7 100644 --- a/extra/yassl/taocrypt/src/dsa.cpp +++ b/extra/yassl/taocrypt/src/dsa.cpp @@ -183,7 +183,8 @@ word32 DSA_Signer::Sign(const byte* sha_digest, byte* sig, Integer kInv = k.InverseMod(q); s_ = (kInv * (H + x*r_)) % q; - assert(!!r_ && !!s_); + if (!(!!r_ && !!s_)) + return -1; int rSz = r_.ByteCount(); diff --git a/extra/yassl/taocrypt/src/hash.cpp b/extra/yassl/taocrypt/src/hash.cpp index c51dc42a909..b36a486368c 100644 --- a/extra/yassl/taocrypt/src/hash.cpp +++ b/extra/yassl/taocrypt/src/hash.cpp @@ -21,7 +21,6 @@ #include "runtime.hpp" #include <string.h> -#include <assert.h> #include "hash.hpp" @@ -31,8 +30,6 @@ namespace TaoCrypt { HASHwithTransform::HASHwithTransform(word32 digSz, word32 buffSz) { - assert(digSz <= MaxDigestSz); - assert(buffSz <= MaxBufferSz); } @@ -73,15 +70,15 @@ void HASHwithTransform::Update(const byte* data, word32 len) // Final process, place digest in hash void HASHwithTransform::Final(byte* hash) { - word32 blockSz = getBlockSize(); - word32 digestSz = getDigestSize(); - word32 padSz = getPadSize(); - ByteOrder order = getByteOrder(); + word32 blockSz = getBlockSize(); + word32 digestSz = getDigestSize(); + word32 padSz = getPadSize(); + ByteOrder order = getByteOrder(); AddLength(buffLen_); // before adding pads HashLengthType preLoLen = GetBitCountLo(); HashLengthType preHiLen = GetBitCountHi(); - byte* local = reinterpret_cast<byte*>(buffer_); + byte* local = reinterpret_cast<byte*>(buffer_); local[buffLen_++] = 0x80; // add 1 @@ -95,7 +92,7 @@ void HASHwithTransform::Final(byte* hash) buffLen_ = 0; } memset(&local[buffLen_], 0, padSz - buffLen_); - + ByteReverseIf(local, local, blockSz, order); memcpy(&local[padSz], order ? &preHiLen : &preLoLen, sizeof(preLoLen)); @@ -113,8 +110,6 @@ void HASHwithTransform::Final(byte* hash) HASH64withTransform::HASH64withTransform(word32 digSz, word32 buffSz) { - assert(digSz <= MaxDigestSz); - assert(buffSz <= MaxBufferSz); } diff --git a/extra/yassl/taocrypt/src/hc128.cpp b/extra/yassl/taocrypt/src/hc128.cpp new file mode 100644 index 00000000000..aac92f0abb4 --- /dev/null +++ b/extra/yassl/taocrypt/src/hc128.cpp @@ -0,0 +1,317 @@ +/* + Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + + 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + 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. +*/ + + +#include "runtime.hpp" +#include "hc128.hpp" + + + +namespace TaoCrypt { + + + + +#ifdef BIG_ENDIAN_ORDER + #define LITTLE32(x) ByteReverse((word32)x) +#else + #define LITTLE32(x) (x) +#endif + + +/*h1 function*/ +#define h1(x, y) { \ + byte a,c; \ + a = (byte) (x); \ + c = (byte) ((x) >> 16); \ + y = (T_[512+a])+(T_[512+256+c]); \ +} + +/*h2 function*/ +#define h2(x, y) { \ + byte a,c; \ + a = (byte) (x); \ + c = (byte) ((x) >> 16); \ + y = (T_[a])+(T_[256+c]); \ +} + +/*one step of HC-128, update P and generate 32 bits keystream*/ +#define step_P(u,v,a,b,c,d,n){ \ + word32 tem0,tem1,tem2,tem3; \ + h1((X_[(d)]),tem3); \ + tem0 = rotrFixed((T_[(v)]),23); \ + tem1 = rotrFixed((X_[(c)]),10); \ + tem2 = rotrFixed((X_[(b)]),8); \ + (T_[(u)]) += tem2+(tem0 ^ tem1); \ + (X_[(a)]) = (T_[(u)]); \ + (n) = tem3 ^ (T_[(u)]) ; \ +} + +/*one step of HC-128, update Q and generate 32 bits keystream*/ +#define step_Q(u,v,a,b,c,d,n){ \ + word32 tem0,tem1,tem2,tem3; \ + h2((Y_[(d)]),tem3); \ + tem0 = rotrFixed((T_[(v)]),(32-23)); \ + tem1 = rotrFixed((Y_[(c)]),(32-10)); \ + tem2 = rotrFixed((Y_[(b)]),(32-8)); \ + (T_[(u)]) += tem2 + (tem0 ^ tem1); \ + (Y_[(a)]) = (T_[(u)]); \ + (n) = tem3 ^ (T_[(u)]) ; \ +} + + +/*16 steps of HC-128, generate 512 bits keystream*/ +void HC128::GenerateKeystream(word32* keystream) +{ + word32 cc,dd; + cc = counter1024_ & 0x1ff; + dd = (cc+16)&0x1ff; + + if (counter1024_ < 512) + { + counter1024_ = (counter1024_ + 16) & 0x3ff; + step_P(cc+0, cc+1, 0, 6, 13,4, keystream[0]); + step_P(cc+1, cc+2, 1, 7, 14,5, keystream[1]); + step_P(cc+2, cc+3, 2, 8, 15,6, keystream[2]); + step_P(cc+3, cc+4, 3, 9, 0, 7, keystream[3]); + step_P(cc+4, cc+5, 4, 10,1, 8, keystream[4]); + step_P(cc+5, cc+6, 5, 11,2, 9, keystream[5]); + step_P(cc+6, cc+7, 6, 12,3, 10,keystream[6]); + step_P(cc+7, cc+8, 7, 13,4, 11,keystream[7]); + step_P(cc+8, cc+9, 8, 14,5, 12,keystream[8]); + step_P(cc+9, cc+10,9, 15,6, 13,keystream[9]); + step_P(cc+10,cc+11,10,0, 7, 14,keystream[10]); + step_P(cc+11,cc+12,11,1, 8, 15,keystream[11]); + step_P(cc+12,cc+13,12,2, 9, 0, keystream[12]); + step_P(cc+13,cc+14,13,3, 10,1, keystream[13]); + step_P(cc+14,cc+15,14,4, 11,2, keystream[14]); + step_P(cc+15,dd+0, 15,5, 12,3, keystream[15]); + } + else + { + counter1024_ = (counter1024_ + 16) & 0x3ff; + step_Q(512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]); + step_Q(512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]); + step_Q(512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]); + step_Q(512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]); + step_Q(512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]); + step_Q(512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]); + step_Q(512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]); + step_Q(512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]); + step_Q(512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]); + step_Q(512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]); + step_Q(512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]); + step_Q(512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]); + step_Q(512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]); + step_Q(512+cc+13,512+cc+14,13,3, 10,1, keystream[13]); + step_Q(512+cc+14,512+cc+15,14,4, 11,2, keystream[14]); + step_Q(512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]); + } +} + + +/* The following defines the initialization functions */ +#define f1(x) (rotrFixed((x),7) ^ rotrFixed((x),18) ^ ((x) >> 3)) +#define f2(x) (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10)) + +/*update table P*/ +#define update_P(u,v,a,b,c,d){ \ + word32 tem0,tem1,tem2,tem3; \ + tem0 = rotrFixed((T_[(v)]),23); \ + tem1 = rotrFixed((X_[(c)]),10); \ + tem2 = rotrFixed((X_[(b)]),8); \ + h1((X_[(d)]),tem3); \ + (T_[(u)]) = ((T_[(u)]) + tem2+(tem0^tem1)) ^ tem3; \ + (X_[(a)]) = (T_[(u)]); \ +} + +/*update table Q*/ +#define update_Q(u,v,a,b,c,d){ \ + word32 tem0,tem1,tem2,tem3; \ + tem0 = rotrFixed((T_[(v)]),(32-23)); \ + tem1 = rotrFixed((Y_[(c)]),(32-10)); \ + tem2 = rotrFixed((Y_[(b)]),(32-8)); \ + h2((Y_[(d)]),tem3); \ + (T_[(u)]) = ((T_[(u)]) + tem2+(tem0^tem1)) ^ tem3; \ + (Y_[(a)]) = (T_[(u)]); \ +} + +/*16 steps of HC-128, without generating keystream, */ +/*but use the outputs to update P and Q*/ +void HC128::SetupUpdate() /*each time 16 steps*/ +{ + word32 cc,dd; + cc = counter1024_ & 0x1ff; + dd = (cc+16)&0x1ff; + + if (counter1024_ < 512) + { + counter1024_ = (counter1024_ + 16) & 0x3ff; + update_P(cc+0, cc+1, 0, 6, 13, 4); + update_P(cc+1, cc+2, 1, 7, 14, 5); + update_P(cc+2, cc+3, 2, 8, 15, 6); + update_P(cc+3, cc+4, 3, 9, 0, 7); + update_P(cc+4, cc+5, 4, 10,1, 8); + update_P(cc+5, cc+6, 5, 11,2, 9); + update_P(cc+6, cc+7, 6, 12,3, 10); + update_P(cc+7, cc+8, 7, 13,4, 11); + update_P(cc+8, cc+9, 8, 14,5, 12); + update_P(cc+9, cc+10,9, 15,6, 13); + update_P(cc+10,cc+11,10,0, 7, 14); + update_P(cc+11,cc+12,11,1, 8, 15); + update_P(cc+12,cc+13,12,2, 9, 0); + update_P(cc+13,cc+14,13,3, 10, 1); + update_P(cc+14,cc+15,14,4, 11, 2); + update_P(cc+15,dd+0, 15,5, 12, 3); + } + else + { + counter1024_ = (counter1024_ + 16) & 0x3ff; + update_Q(512+cc+0, 512+cc+1, 0, 6, 13, 4); + update_Q(512+cc+1, 512+cc+2, 1, 7, 14, 5); + update_Q(512+cc+2, 512+cc+3, 2, 8, 15, 6); + update_Q(512+cc+3, 512+cc+4, 3, 9, 0, 7); + update_Q(512+cc+4, 512+cc+5, 4, 10,1, 8); + update_Q(512+cc+5, 512+cc+6, 5, 11,2, 9); + update_Q(512+cc+6, 512+cc+7, 6, 12,3, 10); + update_Q(512+cc+7, 512+cc+8, 7, 13,4, 11); + update_Q(512+cc+8, 512+cc+9, 8, 14,5, 12); + update_Q(512+cc+9, 512+cc+10,9, 15,6, 13); + update_Q(512+cc+10,512+cc+11,10,0, 7, 14); + update_Q(512+cc+11,512+cc+12,11,1, 8, 15); + update_Q(512+cc+12,512+cc+13,12,2, 9, 0); + update_Q(512+cc+13,512+cc+14,13,3, 10, 1); + update_Q(512+cc+14,512+cc+15,14,4, 11, 2); + update_Q(512+cc+15,512+dd+0, 15,5, 12, 3); + } +} + + +/* for the 128-bit key: key[0]...key[15] +* key[0] is the least significant byte of ctx->key[0] (K_0); +* key[3] is the most significant byte of ctx->key[0] (K_0); +* ... +* key[12] is the least significant byte of ctx->key[3] (K_3) +* key[15] is the most significant byte of ctx->key[3] (K_3) +* +* for the 128-bit iv: iv[0]...iv[15] +* iv[0] is the least significant byte of ctx->iv[0] (IV_0); +* iv[3] is the most significant byte of ctx->iv[0] (IV_0); +* ... +* iv[12] is the least significant byte of ctx->iv[3] (IV_3) +* iv[15] is the most significant byte of ctx->iv[3] (IV_3) +*/ + + + +void HC128::SetIV(const byte* iv) +{ + word32 i; + + for (i = 0; i < (128 >> 5); i++) + iv_[i] = LITTLE32(((word32*)iv)[i]); + + for (; i < 8; i++) iv_[i] = iv_[i-4]; + + /* expand the key and IV into the table T */ + /* (expand the key and IV into the table P and Q) */ + + for (i = 0; i < 8; i++) T_[i] = key_[i]; + for (i = 8; i < 16; i++) T_[i] = iv_[i-8]; + + for (i = 16; i < (256+16); i++) + T_[i] = f2(T_[i-2]) + T_[i-7] + f1(T_[i-15]) + T_[i-16]+i; + + for (i = 0; i < 16; i++) T_[i] = T_[256+i]; + + for (i = 16; i < 1024; i++) + T_[i] = f2(T_[i-2]) + T_[i-7] + f1(T_[i-15]) + T_[i-16]+256+i; + + /* initialize counter1024, X and Y */ + counter1024_ = 0; + for (i = 0; i < 16; i++) X_[i] = T_[512-16+i]; + for (i = 0; i < 16; i++) Y_[i] = T_[512+512-16+i]; + + /* run the cipher 1024 steps before generating the output */ + for (i = 0; i < 64; i++) SetupUpdate(); +} + + +void HC128::SetKey(const byte* key, const byte* iv) +{ + word32 i; + + /* Key size in bits 128 */ + for (i = 0; i < (128 >> 5); i++) + key_[i] = LITTLE32(((word32*)key)[i]); + + for ( ; i < 8 ; i++) key_[i] = key_[i-4]; + + SetIV(iv); +} + + +/* The following defines the encryption of data stream */ +void HC128::Process(byte* output, const byte* input, word32 msglen) +{ + word32 i, keystream[16]; + + for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64) + { + GenerateKeystream(keystream); + + /* unroll loop */ + ((word32*)output)[0] = ((word32*)input)[0] ^ LITTLE32(keystream[0]); + ((word32*)output)[1] = ((word32*)input)[1] ^ LITTLE32(keystream[1]); + ((word32*)output)[2] = ((word32*)input)[2] ^ LITTLE32(keystream[2]); + ((word32*)output)[3] = ((word32*)input)[3] ^ LITTLE32(keystream[3]); + ((word32*)output)[4] = ((word32*)input)[4] ^ LITTLE32(keystream[4]); + ((word32*)output)[5] = ((word32*)input)[5] ^ LITTLE32(keystream[5]); + ((word32*)output)[6] = ((word32*)input)[6] ^ LITTLE32(keystream[6]); + ((word32*)output)[7] = ((word32*)input)[7] ^ LITTLE32(keystream[7]); + ((word32*)output)[8] = ((word32*)input)[8] ^ LITTLE32(keystream[8]); + ((word32*)output)[9] = ((word32*)input)[9] ^ LITTLE32(keystream[9]); + ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]); + ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]); + ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]); + ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]); + ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]); + ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]); + } + + if (msglen > 0) + { + GenerateKeystream(keystream); + +#ifdef BIG_ENDIAN_ORDER + { + word32 wordsLeft = msglen / sizeof(word32); + if (msglen % sizeof(word32)) wordsLeft++; + + ByteReverse(keystream, keystream, wordsLeft * sizeof(word32)); + } +#endif + + for (i = 0; i < msglen; i++) + output[i] = input[i] ^ ((byte*)keystream)[i]; + } + +} + + +} // namespace diff --git a/extra/yassl/taocrypt/src/integer.cpp b/extra/yassl/taocrypt/src/integer.cpp index 04c4cefff98..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 @@ -73,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) @@ -91,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]; @@ -116,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 @@ -284,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 @@ -365,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) @@ -393,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; @@ -491,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) @@ -504,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) @@ -538,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; } @@ -576,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) { @@ -597,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) { @@ -1013,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 @@ -2130,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) @@ -2188,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); } } @@ -2196,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) @@ -2227,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) @@ -2250,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); @@ -2315,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); @@ -2364,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]) @@ -2433,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; @@ -2460,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]; @@ -2489,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) @@ -2508,7 +2474,6 @@ unsigned int AlmostInverse(word *R, word *T, const word *A, unsigned int NA, { b[bcLen] = 1; bcLen+=2; - assert(bcLen <= N); } } } @@ -2622,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); @@ -2741,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(); @@ -2881,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(); } @@ -2998,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 @@ -3007,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; } } @@ -3067,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; @@ -3127,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; @@ -3227,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 } @@ -3241,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; @@ -3260,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 } } @@ -3280,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; @@ -3294,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 @@ -3314,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]; @@ -3340,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; @@ -3429,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); @@ -3470,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 @@ -3517,7 +3461,6 @@ Integer Integer::SquareRoot() const // overestimate square root Integer x, y = Power2((BitCount()+1)/2); - assert(y*y >= *this); do { @@ -3562,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); @@ -3806,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); } @@ -3862,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()); } @@ -3873,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()); @@ -3888,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()); @@ -3902,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()); @@ -3919,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()); diff --git a/extra/yassl/taocrypt/src/make.bat b/extra/yassl/taocrypt/src/make.bat index ecd3023f5ab..ecd3023f5ab 100644..100755 --- a/extra/yassl/taocrypt/src/make.bat +++ b/extra/yassl/taocrypt/src/make.bat diff --git a/extra/yassl/taocrypt/src/md4.cpp b/extra/yassl/taocrypt/src/md4.cpp index cf17c218809..54820f4c944 100644 --- a/extra/yassl/taocrypt/src/md4.cpp +++ b/extra/yassl/taocrypt/src/md4.cpp @@ -27,7 +27,7 @@ #include "algorithm.hpp" #endif - + namespace STL = STL_NAMESPACE; diff --git a/extra/yassl/taocrypt/src/md5.cpp b/extra/yassl/taocrypt/src/md5.cpp index f18e0290c90..4d0a8bd03be 100644 --- a/extra/yassl/taocrypt/src/md5.cpp +++ b/extra/yassl/taocrypt/src/md5.cpp @@ -30,7 +30,7 @@ namespace STL = STL_NAMESPACE; - + namespace TaoCrypt { @@ -108,14 +108,14 @@ void MD5::Update(const byte* data, word32 len) // at once for asm if (buffLen_ == 0) { - word32 times = len / BLOCK_SIZE; - if (times) { - AsmTransform(data, times); - const word32 add = BLOCK_SIZE * times; - AddLength(add); - len -= add; - data += add; - } + word32 times = len / BLOCK_SIZE; + if (times) { + AsmTransform(data, times); + const word32 add = BLOCK_SIZE * times; + AddLength(add); + len -= add; + data += add; + } } // cache any data left diff --git a/extra/yassl/taocrypt/src/misc.cpp b/extra/yassl/taocrypt/src/misc.cpp index 07ef25ad773..1a4dd4a52d3 100644 --- a/extra/yassl/taocrypt/src/misc.cpp +++ b/extra/yassl/taocrypt/src/misc.cpp @@ -41,28 +41,28 @@ namespace STL = STL_NAMESPACE; void* operator new(size_t sz, TaoCrypt::new_t) { - void* ptr = malloc(sz ? sz : 1); - if (!ptr) abort(); + void* ptr = malloc(sz ? sz : 1); + if (!ptr) abort(); - return ptr; + return ptr; } void operator delete(void* ptr, TaoCrypt::new_t) { - if (ptr) free(ptr); + if (ptr) free(ptr); } void* operator new[](size_t sz, TaoCrypt::new_t nt) { - return ::operator new(sz, nt); + return ::operator new(sz, nt); } void operator delete[](void* ptr, TaoCrypt::new_t nt) { - ::operator delete(ptr, nt); + ::operator delete(ptr, nt); } @@ -89,7 +89,6 @@ namespace STL = STL_NAMESPACE; // Handler for pure virtual functions namespace __Crun { void pure_error() { - assert(!"Aborted: pure virtual method called."); } } @@ -100,7 +99,6 @@ namespace __Crun { extern "C" { int __cxa_pure_virtual() { - assert(!"Aborted: pure virtual method called."); return 0; } @@ -205,8 +203,8 @@ bool HaveCpuId() return true; #else word32 eax, ebx; - __asm__ __volatile - ( + __asm__ __volatile + ( /* Put EFLAGS in eax and ebx */ "pushf;" "pushf;" @@ -223,9 +221,9 @@ bool HaveCpuId() "pop %0;" "popf" : "=r" (eax), "=r" (ebx) - : + : : "cc" - ); + ); if (eax == ebx) return false; diff --git a/extra/yassl/taocrypt/src/rabbit.cpp b/extra/yassl/taocrypt/src/rabbit.cpp new file mode 100644 index 00000000000..0ea4146618a --- /dev/null +++ b/extra/yassl/taocrypt/src/rabbit.cpp @@ -0,0 +1,250 @@ +/* + Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + + 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 + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + 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. +*/ + + +#include "runtime.hpp" +#include "rabbit.hpp" + + + +namespace TaoCrypt { + + +#define U32V(x) (word32)(x) + + +#ifdef BIG_ENDIAN_ORDER + #define LITTLE32(x) ByteReverse((word32)x) +#else + #define LITTLE32(x) (x) +#endif + + +// local +namespace { + + +/* Square a 32-bit unsigned integer to obtain the 64-bit result and return */ +/* the upper 32 bits XOR the lower 32 bits */ +word32 RABBIT_g_func(word32 x) +{ + /* Temporary variables */ + word32 a, b, h, l; + + /* Construct high and low argument for squaring */ + a = x&0xFFFF; + b = x>>16; + + /* Calculate high and low result of squaring */ + h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b; + l = x*x; + + /* Return high XOR low */ + return U32V(h^l); +} + + +} // namespace local + + +/* Calculate the next internal state */ +void Rabbit::NextState(RabbitCtx which) +{ + /* Temporary variables */ + word32 g[8], c_old[8], i; + + Ctx* ctx; + + if (which == Master) + ctx = &masterCtx_; + else + ctx = &workCtx_; + + /* Save old counter values */ + for (i=0; i<8; i++) + c_old[i] = ctx->c[i]; + + /* Calculate new counter values */ + ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry); + ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0])); + ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1])); + ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2])); + ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3])); + ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4])); + ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5])); + ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6])); + ctx->carry = (ctx->c[7] < c_old[7]); + + /* Calculate the g-values */ + for (i=0;i<8;i++) + g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i])); + + /* Calculate new state values */ + ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16)); + ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]); + ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16)); + ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]); + ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16)); + ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]); + ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16)); + ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]); +} + + +/* IV setup */ +void Rabbit::SetIV(const byte* iv) +{ + /* Temporary variables */ + word32 i0, i1, i2, i3, i; + + /* Generate four subvectors */ + i0 = LITTLE32(*(word32*)(iv+0)); + i2 = LITTLE32(*(word32*)(iv+4)); + i1 = (i0>>16) | (i2&0xFFFF0000); + i3 = (i2<<16) | (i0&0x0000FFFF); + + /* Modify counter values */ + workCtx_.c[0] = masterCtx_.c[0] ^ i0; + workCtx_.c[1] = masterCtx_.c[1] ^ i1; + workCtx_.c[2] = masterCtx_.c[2] ^ i2; + workCtx_.c[3] = masterCtx_.c[3] ^ i3; + workCtx_.c[4] = masterCtx_.c[4] ^ i0; + workCtx_.c[5] = masterCtx_.c[5] ^ i1; + workCtx_.c[6] = masterCtx_.c[6] ^ i2; + workCtx_.c[7] = masterCtx_.c[7] ^ i3; + + /* Copy state variables */ + for (i=0; i<8; i++) + workCtx_.x[i] = masterCtx_.x[i]; + workCtx_.carry = masterCtx_.carry; + + /* Iterate the system four times */ + for (i=0; i<4; i++) + NextState(Work); +} + + +/* Key setup */ +void Rabbit::SetKey(const byte* key, const byte* iv) +{ + /* Temporary variables */ + word32 k0, k1, k2, k3, i; + + /* Generate four subkeys */ + k0 = LITTLE32(*(word32*)(key+ 0)); + k1 = LITTLE32(*(word32*)(key+ 4)); + k2 = LITTLE32(*(word32*)(key+ 8)); + k3 = LITTLE32(*(word32*)(key+12)); + + /* Generate initial state variables */ + masterCtx_.x[0] = k0; + masterCtx_.x[2] = k1; + masterCtx_.x[4] = k2; + masterCtx_.x[6] = k3; + masterCtx_.x[1] = U32V(k3<<16) | (k2>>16); + masterCtx_.x[3] = U32V(k0<<16) | (k3>>16); + masterCtx_.x[5] = U32V(k1<<16) | (k0>>16); + masterCtx_.x[7] = U32V(k2<<16) | (k1>>16); + + /* Generate initial counter values */ + masterCtx_.c[0] = rotlFixed(k2, 16); + masterCtx_.c[2] = rotlFixed(k3, 16); + masterCtx_.c[4] = rotlFixed(k0, 16); + masterCtx_.c[6] = rotlFixed(k1, 16); + masterCtx_.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF); + masterCtx_.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF); + masterCtx_.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF); + masterCtx_.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF); + + /* Clear carry bit */ + masterCtx_.carry = 0; + + /* Iterate the system four times */ + for (i=0; i<4; i++) + NextState(Master); + + /* Modify the counters */ + for (i=0; i<8; i++) + masterCtx_.c[i] ^= masterCtx_.x[(i+4)&0x7]; + + /* Copy master instance to work instance */ + for (i=0; i<8; i++) { + workCtx_.x[i] = masterCtx_.x[i]; + workCtx_.c[i] = masterCtx_.c[i]; + } + workCtx_.carry = masterCtx_.carry; + + if (iv) SetIV(iv); +} + + +/* Encrypt/decrypt a message of any size */ +void Rabbit::Process(byte* output, const byte* input, word32 msglen) +{ + /* Temporary variables */ + word32 i; + byte buffer[16]; + + /* Encrypt/decrypt all full blocks */ + while (msglen >= 16) { + /* Iterate the system */ + NextState(Work); + + /* Encrypt/decrypt 16 bytes of data */ + *(word32*)(output+ 0) = *(word32*)(input+ 0) ^ + LITTLE32(workCtx_.x[0] ^ (workCtx_.x[5]>>16) ^ + U32V(workCtx_.x[3]<<16)); + *(word32*)(output+ 4) = *(word32*)(input+ 4) ^ + LITTLE32(workCtx_.x[2] ^ (workCtx_.x[7]>>16) ^ + U32V(workCtx_.x[5]<<16)); + *(word32*)(output+ 8) = *(word32*)(input+ 8) ^ + LITTLE32(workCtx_.x[4] ^ (workCtx_.x[1]>>16) ^ + U32V(workCtx_.x[7]<<16)); + *(word32*)(output+12) = *(word32*)(input+12) ^ + LITTLE32(workCtx_.x[6] ^ (workCtx_.x[3]>>16) ^ + U32V(workCtx_.x[1]<<16)); + + /* Increment pointers and decrement length */ + input += 16; + output += 16; + msglen -= 16; + } + + /* Encrypt/decrypt remaining data */ + if (msglen) { + /* Iterate the system */ + NextState(Work); + + /* Generate 16 bytes of pseudo-random data */ + *(word32*)(buffer+ 0) = LITTLE32(workCtx_.x[0] ^ + (workCtx_.x[5]>>16) ^ U32V(workCtx_.x[3]<<16)); + *(word32*)(buffer+ 4) = LITTLE32(workCtx_.x[2] ^ + (workCtx_.x[7]>>16) ^ U32V(workCtx_.x[5]<<16)); + *(word32*)(buffer+ 8) = LITTLE32(workCtx_.x[4] ^ + (workCtx_.x[1]>>16) ^ U32V(workCtx_.x[7]<<16)); + *(word32*)(buffer+12) = LITTLE32(workCtx_.x[6] ^ + (workCtx_.x[3]>>16) ^ U32V(workCtx_.x[1]<<16)); + + /* Encrypt/decrypt the data */ + for (i=0; i<msglen; i++) + output[i] = input[i] ^ buffer[i]; + } +} + + +} // namespace diff --git a/extra/yassl/taocrypt/src/random.cpp b/extra/yassl/taocrypt/src/random.cpp index 1be0fed612f..aa9be7f1985 100644 --- a/extra/yassl/taocrypt/src/random.cpp +++ b/extra/yassl/taocrypt/src/random.cpp @@ -96,13 +96,13 @@ void OS_Seed::GenerateSeed(byte* output, word32 sz) /* The default OS_Seed implementation */ -OS_Seed::OS_Seed() +OS_Seed::OS_Seed() { fd_ = open("/dev/urandom",O_RDONLY); if (fd_ == -1) { fd_ = open("/dev/random",O_RDONLY); - if (fd_ == -1) - error_.SetError(OPEN_RAN_E); + if (fd_ == -1) + error_.SetError(OPEN_RAN_E); } } diff --git a/extra/yassl/taocrypt/src/ripemd.cpp b/extra/yassl/taocrypt/src/ripemd.cpp index a738c197bf2..e012db43de5 100644 --- a/extra/yassl/taocrypt/src/ripemd.cpp +++ b/extra/yassl/taocrypt/src/ripemd.cpp @@ -110,14 +110,14 @@ void RIPEMD160::Update(const byte* data, word32 len) // all at once for asm if (buffLen_ == 0) { - word32 times = len / BLOCK_SIZE; - if (times) { - AsmTransform(data, times); - const word32 add = BLOCK_SIZE * times; - AddLength(add); - len -= add; - data += add; - } + word32 times = len / BLOCK_SIZE; + if (times) { + AsmTransform(data, times); + const word32 add = BLOCK_SIZE * times; + AddLength(add); + len -= add; + data += add; + } } // cache any data left diff --git a/extra/yassl/taocrypt/src/rsa.cpp b/extra/yassl/taocrypt/src/rsa.cpp index 57d19a59c76..576146377a3 100644 --- a/extra/yassl/taocrypt/src/rsa.cpp +++ b/extra/yassl/taocrypt/src/rsa.cpp @@ -61,7 +61,6 @@ Integer RSA_PrivateKey::CalculateInverse(RandomNumberGenerator& rng, Integer y = ModularRoot(re, dq_, dp_, q_, p_, u_); y = modn.Divide(y, r); // unblind - assert(modn.Exponentiate(y, e_) == x); // check return y; } @@ -124,7 +123,8 @@ word32 RSA_BlockType2::UnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen, unsigned i=1; while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body } - assert(i==pkcsBlockLen || pkcsBlock[i-1]==0); + if (!(i==pkcsBlockLen || pkcsBlock[i-1]==0)) + return 0; unsigned int outputLen = pkcsBlockLen - i; invalid = (outputLen > maxOutputLen) || invalid; @@ -179,7 +179,8 @@ word32 RSA_BlockType1::UnPad(const byte* pkcsBlock, word32 pkcsBlockLen, unsigned i=1; while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body } - assert(i==pkcsBlockLen || pkcsBlock[i-1]==0); + if (!(i==pkcsBlockLen || pkcsBlock[i-1]==0)) + return 0; unsigned int outputLen = pkcsBlockLen - i; invalid = (outputLen > maxOutputLen) || invalid; diff --git a/extra/yassl/taocrypt/src/sha.cpp b/extra/yassl/taocrypt/src/sha.cpp index ef165a342ad..1ae42d94e4c 100644 --- a/extra/yassl/taocrypt/src/sha.cpp +++ b/extra/yassl/taocrypt/src/sha.cpp @@ -344,14 +344,14 @@ void SHA::Update(const byte* data, word32 len) // all at once for asm if (buffLen_ == 0) { - word32 times = len / BLOCK_SIZE; - if (times) { - AsmTransform(data, times); - const word32 add = BLOCK_SIZE * times; - AddLength(add); - len -= add; - data += add; - } + word32 times = len / BLOCK_SIZE; + if (times) { + AsmTransform(data, times); + const word32 add = BLOCK_SIZE * times; + AddLength(add); + len -= add; + data += add; + } } // cache any data left diff --git a/extra/yassl/taocrypt/src/template_instnt.cpp b/extra/yassl/taocrypt/src/template_instnt.cpp index 390da58e191..b472d18236f 100644 --- a/extra/yassl/taocrypt/src/template_instnt.cpp +++ b/extra/yassl/taocrypt/src/template_instnt.cpp @@ -61,7 +61,6 @@ template class PBKDF2_HMAC<SHA>; template class HMAC<MD5>; template class HMAC<SHA>; template class HMAC<RIPEMD160>; - } namespace mySTL { diff --git a/extra/yassl/taocrypt/src/twofish.cpp b/extra/yassl/taocrypt/src/twofish.cpp index 15bb778f444..4ae493624dc 100644 --- a/extra/yassl/taocrypt/src/twofish.cpp +++ b/extra/yassl/taocrypt/src/twofish.cpp @@ -141,8 +141,6 @@ inline word32 Twofish::h(word32 x, const word32* key, unsigned int kLen) void Twofish::SetKey(const byte* userKey, word32 keylen, CipherDir /*dummy*/) { - assert(keylen >= 16 && keylen <= 32); - unsigned int len = (keylen <= 16 ? 2 : (keylen <= 24 ? 3 : 4)); word32 key[8]; GetUserKey(LittleEndianOrder, key, len*2, userKey, keylen); diff --git a/extra/yassl/taocrypt/taocrypt.dsp b/extra/yassl/taocrypt/taocrypt.dsp index 3f1b47990ad..9c8e74da8c8 100644 --- a/extra/yassl/taocrypt/taocrypt.dsp +++ b/extra/yassl/taocrypt/taocrypt.dsp @@ -138,6 +138,10 @@ SOURCE=.\src\hash.cpp # End Source File # Begin Source File +SOURCE=.\src\hc128.cpp +# End Source File +# Begin Source File + SOURCE=.\src\integer.cpp # End Source File # Begin Source File @@ -158,6 +162,10 @@ SOURCE=.\src\misc.cpp # End Source File # Begin Source File +SOURCE=.\src\rabbit.cpp +# End Source File +# Begin Source File + SOURCE=.\src\random.cpp # End Source File # Begin Source File @@ -238,6 +246,10 @@ SOURCE=.\include\hash.hpp # End Source File # Begin Source File +SOURCE=.\include\hc128.hpp +# End Source File +# Begin Source File + SOURCE=.\include\hmac.hpp # End Source File # Begin Source File @@ -274,6 +286,10 @@ SOURCE=.\include\pwdbased.hpp # End Source File # Begin Source File +SOURCE=.\include\rabbit.hpp +# End Source File +# Begin Source File + SOURCE=.\include\random.hpp # End Source File # Begin Source File diff --git a/extra/yassl/taocrypt/test/make.bat b/extra/yassl/taocrypt/test/make.bat index d6d62b984a8..d6d62b984a8 100644..100755 --- a/extra/yassl/taocrypt/test/make.bat +++ b/extra/yassl/taocrypt/test/make.bat diff --git a/extra/yassl/taocrypt/test/memory.cpp b/extra/yassl/taocrypt/test/memory.cpp index bac8f9c2e97..ec398a64c45 100644 --- a/extra/yassl/taocrypt/test/memory.cpp +++ b/extra/yassl/taocrypt/test/memory.cpp @@ -328,3 +328,32 @@ void operator delete[](void* ptr) { ::operator delete(ptr); } + + +extern "C" { + +void* XMALLOC(size_t sz, void* head) +{ + return ::operator new(sz); +} + +void* XREALLOC(void* ptr, size_t sz, void* heap) +{ + void* ret = ::operator new(sz); + + if (ret && ptr) + memcpy(ret, ptr, sz); + + if (ret) + ::operator delete(ptr); + return ret; +} + + +void XFREE(void* ptr, void* heap) +{ + ::operator delete(ptr); +} + +} // extern "C" + diff --git a/extra/yassl/taocrypt/test/test.cpp b/extra/yassl/taocrypt/test/test.cpp index 09836a2ef56..807f91f32ae 100644 --- a/extra/yassl/taocrypt/test/test.cpp +++ b/extra/yassl/taocrypt/test/test.cpp @@ -27,6 +27,8 @@ #include "coding.hpp" #include "random.hpp" #include "pwdbased.hpp" +#include "rabbit.hpp" +#include "hc128.hpp" @@ -86,16 +88,18 @@ using TaoCrypt::PBKDF2_HMAC; using TaoCrypt::tcArrayDelete; using TaoCrypt::GetCert; using TaoCrypt::GetPKCS_Cert; - +using TaoCrypt::Rabbit; +using TaoCrypt::HC128; struct testVector { byte* input_; byte* output_; - size_t inLen_; - size_t outLen_; + word32 inLen_; + word32 outLen_; testVector(const char* in, const char* out) : input_((byte*)in), - output_((byte*)out), inLen_(strlen(in)), outLen_(strlen(out)) {} + output_((byte*)out), inLen_((word32)strlen(in)), + outLen_((word32)strlen(out)) {} }; int sha_test(); @@ -120,13 +124,15 @@ int dsa_test(); int dh_test(); int pwdbased_test(); int pkcs12_test(); +int rabbit_test(); +int hc128_test(); TaoCrypt::RandomNumberGenerator rng; void err_sys(const char* msg, int es) { - printf("%s", msg); + printf("%s\n", msg); exit(es); } @@ -162,7 +168,7 @@ byte* cipher = 0; // block output void taocrypt_test(void* args) { ((func_args*)args)->return_code = -1; // error state - + msg = NEW_TC byte[24]; plain = NEW_TC byte[24]; cipher = NEW_TC byte[24]; @@ -229,6 +235,16 @@ void taocrypt_test(void* args) else printf( "ARC4 test passed!\n"); + if ( (ret = rabbit_test()) ) + err_sys("Rabbit test failed!\n", ret); + else + printf( "Rabbit test passed!\n"); + + if ( (ret = hc128_test()) ) + err_sys("HC128 test failed!\n", ret); + else + printf( "HC128 test passed!\n"); + if ( (ret = des_test()) ) err_sys("DES test failed!\n", ret); else @@ -306,16 +322,16 @@ void taocrypt_test(void* args) void file_test(const char* file, byte* check) { FILE* f; - int i(0); - MD5 md5; - byte buf[1024]; - byte md5sum[MD5::DIGEST_SIZE]; + int i = 0; + MD5 md5; + byte buf[1024]; + byte md5sum[MD5::DIGEST_SIZE]; if( !( f = fopen( file, "rb" ) )) { printf("Can't open %s\n", file); return; } - while( ( i = fread(buf, 1, sizeof(buf), f )) > 0 ) + while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 ) md5.Update(buf, i); md5.Final(md5sum); @@ -704,7 +720,7 @@ int hmac_test() int times( sizeof(test_hmacMD5) / sizeof(testVector) ); for (int i = 0; i < times; ++i) { - hmacMD5.SetKey((byte*)keys[i], strlen(keys[i])); + hmacMD5.SetKey((byte*)keys[i], (word32)strlen(keys[i])); hmacMD5.Update(test_hmacMD5[i].input_, test_hmacMD5[i].inLen_); hmacMD5.Final(hash); @@ -747,8 +763,8 @@ int arc4_test() ARC4::Encryption enc; ARC4::Decryption dec; - enc.SetKey((byte*)keys[i], strlen(keys[i])); - dec.SetKey((byte*)keys[i], strlen(keys[i])); + enc.SetKey((byte*)keys[i], (word32)strlen(keys[i])); + dec.SetKey((byte*)keys[i], (word32)strlen(keys[i])); enc.Process(cipher, test_arc4[i].input_, test_arc4[i].outLen_); dec.Process(plain, cipher, test_arc4[i].outLen_); @@ -764,6 +780,114 @@ int arc4_test() } +int rabbit_test() +{ + byte cipher[16]; + byte plain[16]; + + const char* keys[] = + { + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91" + }; + + const char* ivs[] = + { + "\x00\x00\x00\x00\x00\x00\x00\x00", + "\x59\x7E\x26\xC1\x75\xF5\x73\xC3", + 0 + }; + + + testVector test_rabbit[] = + { + testVector("\x00\x00\x00\x00\x00\x00\x00\x00", + "\xED\xB7\x05\x67\x37\x5D\xCD\x7C"), + testVector("\x00\x00\x00\x00\x00\x00\x00\x00", + "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0"), + testVector("\x00\x00\x00\x00\x00\x00\x00\x00", + "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9") + }; + + + int times( sizeof(test_rabbit) / sizeof(testVector) ); + for (int i = 0; i < times; ++i) { + Rabbit::Encryption enc; + Rabbit::Decryption dec; + + enc.SetKey((byte*)keys[i], (byte*)ivs[i]); + dec.SetKey((byte*)keys[i], (byte*)ivs[i]); + + enc.Process(cipher, test_rabbit[i].input_, test_rabbit[i].outLen_); + dec.Process(plain, cipher, test_rabbit[i].outLen_); + + if (memcmp(plain, test_rabbit[i].input_, test_rabbit[i].outLen_)) + return -230 - i; + + if (memcmp(cipher, test_rabbit[i].output_, test_rabbit[i].outLen_)) + return -240 - i; + } + + return 0; +} + + +int hc128_test() +{ + byte cipher[16]; + byte plain[16]; + + const char* keys[] = + { + "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD", + "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC" + }; + + const char* ivs[] = + { + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16", + "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31" + }; + + testVector test_hc128[] = + { + testVector("\x00\x00\x00\x00\x00\x00\x00\x00", + "\x37\x86\x02\xB9\x8F\x32\xA7\x48"), + testVector("\x00\x00\x00\x00\x00\x00\x00\x00", + "\x33\x7F\x86\x11\xC6\xED\x61\x5F"), + testVector("\x00\x00\x00\x00\x00\x00\x00\x00", + "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A"), + testVector("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29") + }; + + int times( sizeof(test_hc128) / sizeof(testVector) ); + for (int i = 0; i < times; ++i) { + HC128::Encryption enc; + HC128::Decryption dec; + + enc.SetKey((byte*)keys[i], (byte*)ivs[i]); + dec.SetKey((byte*)keys[i], (byte*)ivs[i]); + + enc.Process(cipher, test_hc128[i].input_, test_hc128[i].outLen_); + dec.Process(plain, cipher, test_hc128[i].outLen_); + + if (memcmp(plain, test_hc128[i].input_, test_hc128[i].outLen_)) + return -330 - i; + + if (memcmp(cipher, test_hc128[i].output_, test_hc128[i].outLen_)) + return -340 - i; + } + + return 0; +} + + int des_test() { //ECB mode @@ -1026,7 +1150,7 @@ int rsa_test() RSAES_Encryptor enc(priv); byte message[] = "Everyone gets Friday off."; - const int len(strlen((char*)message)); + const word32 len = (word32)strlen((char*)message); byte cipher[64]; enc.Encrypt(message, len, cipher, rng); @@ -1054,6 +1178,8 @@ int rsa_test() } } CertDecoder cd(source2, true, 0, false, CertDecoder::CA); + if (cd.GetError().What()) + err_sys("cert error", -80); Source source3(cd.GetPublicKey().GetKey(), cd.GetPublicKey().size()); RSA_PublicKey pub(source3); @@ -1174,6 +1300,7 @@ int pwdbased_test() } +/* int pkcs12_test() { Source cert; @@ -1206,4 +1333,5 @@ int pkcs12_test() return 0; } +*/ |