aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/ciphermodeofb.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/ciphermodeofb.h')
-rw-r--r--src/unstable/ciphermodeofb.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/unstable/ciphermodeofb.h b/src/unstable/ciphermodeofb.h
new file mode 100644
index 0000000..19d0f83
--- /dev/null
+++ b/src/unstable/ciphermodeofb.h
@@ -0,0 +1,65 @@
1#ifndef BU_MODE_OFB_H
2#define BU_MODE_OFB_H
3
4#include "bu/filter.h"
5#include "bu/string.h"
6
7namespace Bu
8{
9 /**
10 * Output Feedback Mode. This cipher mode is one of the most resiliant.
11 * Instead of encrypting your data directly it encrypts a "key stream" using
12 * the initialization vector, and then XORs those blocks with your stream
13 * blocks. This means that an error in your stream will still produce an
14 * error in the output, but it will not propegate. Also, with most
15 * encryption schemes error correction codes on the source data will still
16 * work on the encrypted data or decrypted output.
17 */
18 template<int iBlockSize, typename CipherType>
19 class CipherModeOfb : public CipherType
20 {
21 public:
22 CipherModeOfb(class Stream &rNext ) :
23 CipherType( rNext ),
24 bStart( true )
25 {
26 memset( aVector, 0, iBlockSize );
27 }
28
29 virtual ~CipherModeOfb()
30 {
31 }
32
33 void setIv( const Bu::String &sIv )
34 {
35 memcpy( aVector, sIv.getStr(), iBlockSize );
36 }
37
38 protected:
39 void decipher( void *pBuf )
40 {
41 CipherType::encipher( aVector );
42 uint8_t aTmp[iBlockSize];
43 memcpy( aTmp, aVector, iBlockSize );
44 for( int j = 0; j < iBlockSize; j++ )
45 ((uint8_t *)pBuf)[j] ^= aVector[j];
46 memcpy( aVector, aTmp, iBlockSize );
47 }
48
49 void encipher( void *pBuf )
50 {
51 CipherType::encipher( aVector );
52 uint8_t aTmp[iBlockSize];
53 memcpy( aTmp, aVector, iBlockSize );
54 for( int j = 0; j < iBlockSize; j++ )
55 ((uint8_t *)pBuf)[j] ^= aVector[j];
56 memcpy( aVector, aTmp, iBlockSize );
57 }
58
59 private:
60 bool bStart;
61 uint8_t aVector[iBlockSize];
62 };
63};
64
65#endif