diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-04-10 08:32:23 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-04-10 08:32:23 +0000 |
commit | d49fc3aeac4daa65416751f94943b6611ad247d3 (patch) | |
tree | 2d843ebe59284d06fea389744a3a08aad423868d /src/experimental/ciphermodecbc.h | |
parent | 1e9d8f7a92e43f61e4d9f478f6e8dd5ae206616e (diff) | |
download | libbu++-d49fc3aeac4daa65416751f94943b6611ad247d3.tar.gz libbu++-d49fc3aeac4daa65416751f94943b6611ad247d3.tar.bz2 libbu++-d49fc3aeac4daa65416751f94943b6611ad247d3.tar.xz libbu++-d49fc3aeac4daa65416751f94943b6611ad247d3.zip |
Rearranged the Cipher system, and added four modes of operation. It's pretty
slick, really, and we actually support four of the most common modes.
Blowfish is still a template, but it doesn't really need to be anymore...
Diffstat (limited to 'src/experimental/ciphermodecbc.h')
-rw-r--r-- | src/experimental/ciphermodecbc.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/experimental/ciphermodecbc.h b/src/experimental/ciphermodecbc.h new file mode 100644 index 0000000..8fbf9f6 --- /dev/null +++ b/src/experimental/ciphermodecbc.h | |||
@@ -0,0 +1,54 @@ | |||
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 | template<int iBlockSize, typename CipherType> | ||
10 | class CipherModeCbc : public CipherType | ||
11 | { | ||
12 | public: | ||
13 | CipherModeCbc(class Stream &rNext ) : | ||
14 | CipherType( rNext ), | ||
15 | bStart( true ) | ||
16 | { | ||
17 | memset( aVector, 0, iBlockSize ); | ||
18 | } | ||
19 | |||
20 | virtual ~CipherModeCbc() | ||
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::decipher( pBuf ); | ||
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 | for( int j = 0; j < iBlockSize; j++ ) | ||
43 | ((uint8_t *)pBuf)[j] ^= aVector[j]; | ||
44 | CipherType::encipher( pBuf ); | ||
45 | memcpy( aVector, pBuf, iBlockSize ); | ||
46 | } | ||
47 | |||
48 | private: | ||
49 | bool bStart; | ||
50 | uint8_t aVector[iBlockSize]; | ||
51 | }; | ||
52 | }; | ||
53 | |||
54 | #endif | ||