From 228b885b41652a015a91770dfd993456d76ad102 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 9 Apr 2012 16:29:18 +0000 Subject: Blowfish works in it's new split form, which will make it much easier to add other types of ciphers down the road, should we choose to. --- src/experimental/blowfish.cpp | 102 ++++++++++++------------------------------ src/experimental/blowfish.h | 17 +++---- src/experimental/cipher.cpp | 65 +++++++++++++++++++++++++++ src/experimental/cipher.h | 29 ++++++++++++ src/tests/blowfish.cpp | 93 ++++++++++++++++++++++++++++++++++++++ src/tests/threadid.cpp | 72 +++++++++++++++++++++++++++++ 6 files changed, 292 insertions(+), 86 deletions(-) create mode 100644 src/experimental/cipher.cpp create mode 100644 src/experimental/cipher.h create mode 100644 src/tests/blowfish.cpp create mode 100644 src/tests/threadid.cpp (limited to 'src') diff --git a/src/experimental/blowfish.cpp b/src/experimental/blowfish.cpp index 3dda87a..797ec73 100644 --- a/src/experimental/blowfish.cpp +++ b/src/experimental/blowfish.cpp @@ -9,7 +9,7 @@ using Bu::sio; SB[3][x.byte.three]) Bu::Blowfish::Blowfish( Bu::Stream &rNext ) : - Bu::Filter( rNext ) + Bu::Cipher( rNext ) { } @@ -43,7 +43,7 @@ void Bu::Blowfish::setPassword( const Bu::String &sPass ) for (i=0;iword0, &w2 = dwWork->word1; + + w1.word = be32toh( w1.word ); + w2.word = be32toh( w2.word ); + keyEncipher( w1, w2 ); + + revBytes( w1.word ); + revBytes( w2.word ); +} + +void Bu::Blowfish::keyEncipher( Word &w1, Word &w2 ) +{ w1.word ^= PA[0]; w2.word ^= F(w1)^PA[1]; w1.word ^= F(w2)^PA[2]; w2.word ^= F(w1)^PA[3]; w1.word ^= F(w2)^PA[4]; @@ -428,14 +377,17 @@ void Bu::Blowfish::BF_En( Word *x1, Word *x2 ) w2.word ^= F(w1)^PA[15]; w1.word ^= F(w2)^PA[16]; w2.word ^= PA[17]; - *x1 = w2; - *x2 = w1; + Bu::swap( w1, w2 ); } -void Bu::Blowfish::BF_De( Word *x1, Word *x2 ) +void Bu::Blowfish::decipher( void *pData ) { - Word w1=*x1,w2=*x2; + DWord *dwWork = (DWord *)pData; + Word &w1 = dwWork->word0, &w2 = dwWork->word1; + revBytes( w1.word ); + revBytes( w2.word ); + w1.word ^= PA[17]; w2.word ^= F(w1)^PA[16]; w1.word ^= F(w2)^PA[15]; w2.word ^= F(w1)^PA[14]; w1.word ^= F(w2)^PA[13]; @@ -446,8 +398,10 @@ void Bu::Blowfish::BF_De( Word *x1, Word *x2 ) w2.word ^= F(w1)^PA[4]; w1.word ^= F(w2)^PA[3]; w2.word ^= F(w1)^PA[2]; w1.word ^= F(w2)^PA[1]; w2.word ^= PA[0]; + + Bu::swap( w1, w2 ); - *x1 = w2; - *x2 = w1; + w1.word = htobe32( w1.word ); + w2.word = htobe32( w2.word ); } diff --git a/src/experimental/blowfish.h b/src/experimental/blowfish.h index 054fc82..4dbd637 100644 --- a/src/experimental/blowfish.h +++ b/src/experimental/blowfish.h @@ -1,7 +1,7 @@ #ifndef BU_BLOWFISH_H #define BU_BLOWFISH_H -#include "bu/filter.h" +#include "bu/cipher.h" #define NUM_SUBKEYS 18 #define NUM_S_BOXES 4 @@ -12,7 +12,7 @@ namespace Bu { - class Blowfish : public Bu::Filter + class Blowfish : public Bu::Cipher { public: Blowfish( Bu::Stream &rNext ); @@ -20,14 +20,6 @@ namespace Bu void setPassword( const Bu::String &sPass ); - virtual void start(); - virtual Bu::size stop(); - - virtual Bu::size read( void *pBuf, Bu::size iBytes ); - virtual Bu::size write( const void *pBuf, Bu::size iBytes ); - using Bu::Stream::read; - using Bu::Stream::write; - private: uint32_t PA[NUM_SUBKEYS]; uint32_t SB[NUM_S_BOXES][NUM_ENTRIES]; @@ -65,8 +57,9 @@ namespace Bu }; void reset(); - inline void BF_En( Word *, Word * ); - inline void BF_De( Word *, Word * ); + virtual void encipher( void *pData ); + virtual void decipher( void *pData ); + inline void keyEncipher( Word &w1, Word &w2 ); }; }; diff --git a/src/experimental/cipher.cpp b/src/experimental/cipher.cpp new file mode 100644 index 0000000..3430c08 --- /dev/null +++ b/src/experimental/cipher.cpp @@ -0,0 +1,65 @@ +#include "bu/cipher.h" + +Bu::Cipher::Cipher( Bu::Stream &rNext ) : + Bu::Filter( rNext ) +{ +} + +Bu::Cipher::~Cipher() +{ +} + +void Bu::Cipher::start() +{ +} + +Bu::size Bu::Cipher::stop() +{ + return 0; +} + +Bu::size Bu::Cipher::read( void *pBuf, Bu::size iBytes ) +{ + uint32_t i; + + if (iBytes%8) + { + return 0; + } + + iBytes /= 8; + + for (i=0;i +#include +#include +#include +#include +#include + +using namespace Bu; + +static const char *testdat[34][3] ={ +{"0000000000000000", "0000000000000000", "4EF997456198DD78"}, +{"FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A"}, +{"3000000000000000", "1000000000000001", "7D856F9A613063F2"}, +{"1111111111111111", "1111111111111111", "2466DD878B963C9D"}, +{"0123456789ABCDEF", "1111111111111111", "61F9C3802281B096"}, +{"1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7"}, +{"0000000000000000", "0000000000000000", "4EF997456198DD78"}, +{"FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D"}, +{"7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B"}, +{"0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0"}, +{"07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4"}, +{"3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB"}, +{"04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A"}, +{"0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918"}, +{"0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98"}, +{"43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5"}, +{"07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79"}, +{"04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3"}, +{"37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969"}, +{"1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B"}, +{"584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E"}, +{"025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD"}, +{"49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019"}, +{"4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3"}, +{"49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5"}, +{"018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778"}, +{"1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201"}, +{"0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2"}, +{"1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE"}, +{"E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D"}, +{"0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4"}, +{"FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC"}, +{"0123456789ABCDEF", "0000000000000000", "245946885754369A"}, +{"FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A"}}; + + +int main( int argc, char *argv[] ) +{ + + for( int j = 0; j < 34; j++ ) + { + MemBuf mb; + Blowfish bf( mb ); + bf.setPassword( decodeStr( testdat[j][0] ) ); + bf.write( decodeStr( testdat[j][1] ) ); + sio << "Test " << j << ": " << (mb.getString() == decodeStr( testdat[j][2] )) << " (" << encodeStr( mb.getString(), true ) << " == " << testdat[j][2] << ")" << sio.nl; + + mb.setPos( 0 ); + Blowfish bf2( mb ); + bf2.setPassword( decodeStr( testdat[j][0] ) ); + char buf[8]; + bf2.read( buf, 8 ); + + sio << " - Back: " << (Bu::String(testdat[j][1]) == encodeStr(String(buf,8),true)) << sio.nl; + } + + /* + { + File fIn("data.plain", File::Read ); + File fOut("data.crypt", File::WriteNew ); + + Blowfish bOut( fOut ); + bOut.setPassword("abcdefghijklmnop"); + bOut.write( fIn.readAll() ); + } + */ + /* + { + File fIn("data.java", File::Read ); + File fOut("data.stuff", File::WriteNew ); + + Blowfish bIn( fIn ); + bIn.setPassword("abcdefghijklmnop"); + char buf[64]; + bIn.read( buf, 64 ); + fOut.write( buf, 64 ); + sio << sio.nl << "All done." << sio.nl; + } + */ + + return 0; +} + diff --git a/src/tests/threadid.cpp b/src/tests/threadid.cpp new file mode 100644 index 0000000..9ff99df --- /dev/null +++ b/src/tests/threadid.cpp @@ -0,0 +1,72 @@ +#include +#include + +#define BU_TRACE +#include + +using namespace Bu; + +class CopyThing +{ +public: + CopyThing() + { + TRACE(); + tidHome = Thread::currentThread(); + } + + CopyThing( const CopyThing &rSrc ) + { + TRACE(); + tidHome = Thread::currentThread(); + sio << "Same thread? " << (tidHome == rSrc.tidHome) << sio.nl; + } + + void doThings() + { + TRACE(); + if( tidHome != Thread::currentThread() ) + sio << "Different threads, hard copy here." << sio.nl; + else + sio << "Same thread, everything is cool." << sio.nl; + } + +private: + ThreadId tidHome; +}; + +class SubThread : public Thread +{ +public: + SubThread( CopyThing &src ) : + src( src ) + { + src.doThings(); + } + +protected: + void run() + { + src.doThings(); + sio << "run-Child is me? " << (getId() == Thread::currentThread()) << sio.nl; + } + +private: + CopyThing src; +}; + +int main( int argc, char *argv[] ) +{ + CopyThing a; + + SubThread st( a ); + st.start(); + + sio << "Child is me? " << (st.getId() == Thread::currentThread()) << sio.nl; + + st.join(); + + + return 0; +} + -- cgit v1.2.3