#include "bu/blowfish.h" #include "bu/sio.h" using Bu::sio; #define F(x) \ (((SB[0][x.byte.zero] + SB[1][x.byte.one]) ^ SB[2][x.byte.two]) + \ SB[3][x.byte.three]) Bu::Blowfish::Blowfish( Bu::Stream &rNext ) : Bu::Cipher<8>( rNext ) { } Bu::Blowfish::~Blowfish() { reset(); } #define revBytes( x ) x = (((x&0xff)<<24)|((x&0xff00)<<8)|((x&0xff0000)>>8)|((x&0xff000000)>>24)) void Bu::Blowfish::setPassword( const Bu::String &sPass ) { reset(); uint32_t i,j,len=sPass.getSize(); Word Work,null0,null1; if (len > 0) { j = 0; 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]; w2.word ^= F(w1)^PA[5]; w1.word ^= F(w2)^PA[6]; w2.word ^= F(w1)^PA[7]; w1.word ^= F(w2)^PA[8]; w2.word ^= F(w1)^PA[9]; w1.word ^= F(w2)^PA[10]; w2.word ^= F(w1)^PA[11]; w1.word ^= F(w2)^PA[12]; w2.word ^= F(w1)^PA[13]; w1.word ^= F(w2)^PA[14]; w2.word ^= F(w1)^PA[15]; w1.word ^= F(w2)^PA[16]; w2.word ^= PA[17]; Bu::swap( w1, w2 ); } void Bu::Blowfish::decipher( void *pData ) { 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]; w2.word ^= F(w1)^PA[12]; w1.word ^= F(w2)^PA[11]; w2.word ^= F(w1)^PA[10]; w1.word ^= F(w2)^PA[9]; w2.word ^= F(w1)^PA[8]; w1.word ^= F(w2)^PA[7]; w2.word ^= F(w1)^PA[6]; w1.word ^= F(w2)^PA[5]; 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 ); w1.word = htobe32( w1.word ); w2.word = htobe32( w2.word ); }