summaryrefslogtreecommitdiff
path: root/src/unstable/ciphermodecbc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/ciphermodecbc.h')
-rw-r--r--src/unstable/ciphermodecbc.h59
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
7namespace 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