diff options
Diffstat (limited to 'src/unstable/ciphermodecfb.h')
| -rw-r--r-- | src/unstable/ciphermodecfb.h | 59 |
1 files changed, 59 insertions, 0 deletions
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 @@ | |||
| 1 | #ifndef BU_MODE_CFB_H | ||
| 2 | #define BU_MODE_CFB_H | ||
| 3 | |||
| 4 | #include "bu/filter.h" | ||
| 5 | #include "bu/string.h" | ||
| 6 | |||
| 7 | namespace Bu | ||
| 8 | { | ||
| 9 | /** | ||
| 10 | * Cipher Feedback mode. This is very similar to the Cipher-block chaining | ||
| 11 | * mode, with a slight tweak (Bu::CipherModeCbc). Each block is still | ||
| 12 | * dependant on all previous blocks. Any corruption and the entire stream | ||
| 13 | * will be corrupt. | ||
| 14 | */ | ||
| 15 | template<int iBlockSize, typename CipherType> | ||
| 16 | class CipherModeCfb : public CipherType | ||
| 17 | { | ||
| 18 | public: | ||
| 19 | CipherModeCfb(class Stream &rNext ) : | ||
| 20 | CipherType( rNext ), | ||
| 21 | bStart( true ) | ||
| 22 | { | ||
| 23 | memset( aVector, 0, iBlockSize ); | ||
| 24 | } | ||
| 25 | |||
| 26 | virtual ~CipherModeCfb() | ||
| 27 | { | ||
| 28 | } | ||
| 29 | |||
| 30 | void setIv( const Bu::String &sIv ) | ||
| 31 | { | ||
| 32 | memcpy( aVector, sIv.getStr(), iBlockSize ); | ||
| 33 | } | ||
| 34 | |||
| 35 | protected: | ||
| 36 | void decipher( void *pBuf ) | ||
| 37 | { | ||
| 38 | uint8_t aTmp[iBlockSize]; | ||
| 39 | memcpy( aTmp, pBuf, iBlockSize ); | ||
| 40 | CipherType::encipher( aVector ); | ||
| 41 | for( int j = 0; j < iBlockSize; j++ ) | ||
| 42 | ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
| 43 | memcpy( aVector, aTmp, iBlockSize ); | ||
| 44 | } | ||
| 45 | |||
| 46 | void encipher( void *pBuf ) | ||
| 47 | { | ||
| 48 | CipherType::encipher( aVector ); | ||
| 49 | for( int j = 0; j < iBlockSize; j++ ) | ||
| 50 | aVector[j] = ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
| 51 | } | ||
| 52 | |||
| 53 | private: | ||
| 54 | bool bStart; | ||
| 55 | uint8_t aVector[iBlockSize]; | ||
| 56 | }; | ||
| 57 | }; | ||
| 58 | |||
| 59 | #endif | ||
