diff options
Diffstat (limited to 'src/experimental/ciphermodecfb.h')
| -rw-r--r-- | src/experimental/ciphermodecfb.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/experimental/ciphermodecfb.h b/src/experimental/ciphermodecfb.h new file mode 100644 index 0000000..dab6c2e --- /dev/null +++ b/src/experimental/ciphermodecfb.h | |||
| @@ -0,0 +1,53 @@ | |||
| 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 | template<int iBlockSize, typename CipherType> | ||
| 10 | class CipherModeCfb : public CipherType | ||
| 11 | { | ||
| 12 | public: | ||
| 13 | CipherModeCfb(class Stream &rNext ) : | ||
| 14 | CipherType( rNext ), | ||
| 15 | bStart( true ) | ||
| 16 | { | ||
| 17 | memset( aVector, 0, iBlockSize ); | ||
| 18 | } | ||
| 19 | |||
| 20 | virtual ~CipherModeCfb() | ||
| 21 | { | ||
| 22 | } | ||
| 23 | |||
| 24 | void setIv( const Bu::String &sIv ) | ||
| 25 | { | ||
| 26 | memcpy( aVector, sIv.getStr(), iBlockSize ); | ||
| 27 | } | ||
| 28 | |||
| 29 | protected: | ||
| 30 | void decipher( void *pBuf ) | ||
| 31 | { | ||
| 32 | uint8_t aTmp[iBlockSize]; | ||
| 33 | memcpy( aTmp, pBuf, iBlockSize ); | ||
| 34 | CipherType::encipher( aVector ); | ||
| 35 | for( int j = 0; j < iBlockSize; j++ ) | ||
| 36 | ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
| 37 | memcpy( aVector, aTmp, iBlockSize ); | ||
| 38 | } | ||
| 39 | |||
| 40 | void encipher( void *pBuf ) | ||
| 41 | { | ||
| 42 | CipherType::encipher( aVector ); | ||
| 43 | for( int j = 0; j < iBlockSize; j++ ) | ||
| 44 | aVector[j] = ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
| 45 | } | ||
| 46 | |||
| 47 | private: | ||
| 48 | bool bStart; | ||
| 49 | uint8_t aVector[iBlockSize]; | ||
| 50 | }; | ||
| 51 | }; | ||
| 52 | |||
| 53 | #endif | ||
