aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/ciphermodecbc.h
blob: 8fbf9f66af00c077e70dc016173b353c74905b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef BU_MODE_CBC_H
#define BU_MODE_CBC_H

#include "bu/filter.h"
#include "bu/string.h"

namespace Bu
{
	template<int iBlockSize, typename CipherType>
	class CipherModeCbc : public CipherType
	{
	public:
		CipherModeCbc(class Stream &rNext ) :
			CipherType( rNext ),
			bStart( true )
		{
			memset( aVector, 0, iBlockSize );
		}

		virtual ~CipherModeCbc()
		{
		}

		void setIv( const Bu::String &sIv )
		{
			memcpy( aVector, sIv.getStr(), iBlockSize );
		}

	protected:
		void decipher( void *pBuf )
		{
			uint8_t aTmp[iBlockSize];
			memcpy( aTmp, pBuf, iBlockSize );
			CipherType::decipher( pBuf );
			for( int j = 0; j < iBlockSize; j++ )
				((uint8_t *)pBuf)[j] ^= aVector[j];
			memcpy( aVector, aTmp, iBlockSize );
		}

		void encipher( void *pBuf )
		{
			for( int j = 0; j < iBlockSize; j++ )
				((uint8_t *)pBuf)[j] ^= aVector[j];
			CipherType::encipher( pBuf );
			memcpy( aVector, pBuf, iBlockSize );
		}

	private:
		bool bStart;
		uint8_t aVector[iBlockSize];
	};
};

#endif