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 ++++++++++++ 4 files changed, 127 insertions(+), 86 deletions(-) create mode 100644 src/experimental/cipher.cpp create mode 100644 src/experimental/cipher.h (limited to 'src/experimental') 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