diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2012-11-28 17:39:09 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2012-11-28 17:39:09 +0000 |
| commit | 03e8c5ad314252cde58c53688c70b9f836a1d5b4 (patch) | |
| tree | 6d26558aaae5e3758ca8b23c4086116e6d6b2636 /src/unstable/ciphermodecbc.h | |
| parent | 223e2986ad7752d38ce24d1cbeff47db98df1ae3 (diff) | |
| download | libbu++-03e8c5ad314252cde58c53688c70b9f836a1d5b4.tar.gz libbu++-03e8c5ad314252cde58c53688c70b9f836a1d5b4.tar.bz2 libbu++-03e8c5ad314252cde58c53688c70b9f836a1d5b4.tar.xz libbu++-03e8c5ad314252cde58c53688c70b9f836a1d5b4.zip | |
More comments; moved the encryption system to unstable.
Diffstat (limited to 'src/unstable/ciphermodecbc.h')
| -rw-r--r-- | src/unstable/ciphermodecbc.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/unstable/ciphermodecbc.h b/src/unstable/ciphermodecbc.h new file mode 100644 index 0000000..19fdd7d --- /dev/null +++ b/src/unstable/ciphermodecbc.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #ifndef BU_MODE_CBC_H | ||
| 2 | #define BU_MODE_CBC_H | ||
| 3 | |||
| 4 | #include "bu/filter.h" | ||
| 5 | #include "bu/string.h" | ||
| 6 | |||
| 7 | namespace Bu | ||
| 8 | { | ||
| 9 | /** | ||
| 10 | * Cipher-block chaining mode. The Initialization Vector (IV) is fed into | ||
| 11 | * the first block, then each subsequent block is fed into the next making | ||
| 12 | * each block dependant on all previous blocks. | ||
| 13 | */ | ||
| 14 | template<int iBlockSize, typename CipherType> | ||
| 15 | class CipherModeCbc : public CipherType | ||
| 16 | { | ||
| 17 | public: | ||
| 18 | CipherModeCbc(class Stream &rNext ) : | ||
| 19 | CipherType( rNext ), | ||
| 20 | bStart( true ) | ||
| 21 | { | ||
| 22 | memset( aVector, 0, iBlockSize ); | ||
| 23 | } | ||
| 24 | |||
| 25 | virtual ~CipherModeCbc() | ||
| 26 | { | ||
| 27 | } | ||
| 28 | |||
| 29 | void setIv( const Bu::String &sIv ) | ||
| 30 | { | ||
| 31 | memcpy( aVector, sIv.getStr(), iBlockSize ); | ||
| 32 | } | ||
| 33 | |||
| 34 | protected: | ||
| 35 | void decipher( void *pBuf ) | ||
| 36 | { | ||
| 37 | uint8_t aTmp[iBlockSize]; | ||
| 38 | memcpy( aTmp, pBuf, iBlockSize ); | ||
| 39 | CipherType::decipher( pBuf ); | ||
| 40 | for( int j = 0; j < iBlockSize; j++ ) | ||
| 41 | ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
| 42 | memcpy( aVector, aTmp, iBlockSize ); | ||
| 43 | } | ||
| 44 | |||
| 45 | void encipher( void *pBuf ) | ||
| 46 | { | ||
| 47 | for( int j = 0; j < iBlockSize; j++ ) | ||
| 48 | ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
| 49 | CipherType::encipher( pBuf ); | ||
| 50 | memcpy( aVector, pBuf, iBlockSize ); | ||
| 51 | } | ||
| 52 | |||
| 53 | private: | ||
| 54 | bool bStart; | ||
| 55 | uint8_t aVector[iBlockSize]; | ||
| 56 | }; | ||
| 57 | }; | ||
| 58 | |||
| 59 | #endif | ||
