From 03e8c5ad314252cde58c53688c70b9f836a1d5b4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 28 Nov 2012 17:39:09 +0000 Subject: More comments; moved the encryption system to unstable. --- src/unstable/ciphermodecfb.h | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/unstable/ciphermodecfb.h (limited to 'src/unstable/ciphermodecfb.h') diff --git a/src/unstable/ciphermodecfb.h b/src/unstable/ciphermodecfb.h new file mode 100644 index 0000000..1c9c5e9 --- /dev/null +++ b/src/unstable/ciphermodecfb.h @@ -0,0 +1,59 @@ +#ifndef BU_MODE_CFB_H +#define BU_MODE_CFB_H + +#include "bu/filter.h" +#include "bu/string.h" + +namespace Bu +{ + /** + * Cipher Feedback mode. This is very similar to the Cipher-block chaining + * mode, with a slight tweak (Bu::CipherModeCbc). Each block is still + * dependant on all previous blocks. Any corruption and the entire stream + * will be corrupt. + */ + template + class CipherModeCfb : public CipherType + { + public: + CipherModeCfb(class Stream &rNext ) : + CipherType( rNext ), + bStart( true ) + { + memset( aVector, 0, iBlockSize ); + } + + virtual ~CipherModeCfb() + { + } + + void setIv( const Bu::String &sIv ) + { + memcpy( aVector, sIv.getStr(), iBlockSize ); + } + + protected: + void decipher( void *pBuf ) + { + uint8_t aTmp[iBlockSize]; + memcpy( aTmp, pBuf, iBlockSize ); + CipherType::encipher( aVector ); + for( int j = 0; j < iBlockSize; j++ ) + ((uint8_t *)pBuf)[j] ^= aVector[j]; + memcpy( aVector, aTmp, iBlockSize ); + } + + void encipher( void *pBuf ) + { + CipherType::encipher( aVector ); + for( int j = 0; j < iBlockSize; j++ ) + aVector[j] = ((uint8_t *)pBuf)[j] ^= aVector[j]; + } + + private: + bool bStart; + uint8_t aVector[iBlockSize]; + }; +}; + +#endif -- cgit v1.2.3