aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/ciphermodeofb.h
blob: e1b51087c616b1d3ef64c366d0d7ed9dc67f095f (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
55
56
#ifndef BU_MODE_OFB_H
#define BU_MODE_OFB_H

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

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

        virtual ~CipherModeOfb()
        {
        }

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

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

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

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

#endif