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/ciphermodecfb.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/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 | ||