summaryrefslogtreecommitdiff
path: root/src/unstable/cipher.h
blob: eed73af3d073b19d22224a2e61c131629e8c66ec (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_CIPHER_H
#define BU_CIPHER_H

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

namespace Bu
{
    /**
     * Represents a nice base class for a stream filter block encryption scheme.
     * This class handles efficient caching during reading and writing,
     * encrypting and decrypting for block ciphers.  For each individual cipher
     * you only have to worry about the block encryption and decryption.  Cipher
     * modes are handled with seperate classes, see Bu::CipherModeCbc,
     * Bu::CipherModeCfb, Bu::CipherModeEcb, and Bu::CipherModeOfb.
     *
     *
     */
    template<int iBlockSize>
    class Cipher : public Bu::Filter
    {
    public:
        Cipher( Bu::Stream &rNext ) :
            Bu::Filter( rNext ),
            iReadBufFill( 0 ),
            iReadBufPos( 0 ),
            iWriteBufFill( 0 )
        {
        }

        virtual ~Cipher()
        {
        }

        virtual void start()
        {
        }

        virtual Bu::size stop()
        {
            flush();
            return 0;
        }

        virtual Bu::size read( void *pBuf, Bu::size iBytes )
        {
            Bu::size iRead = 0;
            while( iRead < iBytes )
            {
                if( iReadBufFill < iBlockSize )
                {
                    int iR = rNext.read(
                        aReadBuf+iReadBufFill,
                        iBlockSize-iReadBufFill
                        );
                    if( iR == 0 )
                        return iRead;

                    iReadBufFill += iR;

                    if( iReadBufFill == iBlockSize )
                        decipher( aReadBuf );
                }

                if( iReadBufFill == iBlockSize )
                {
                    int iCpy = Bu::buMin( (int)(iBytes-iRead), iBlockSize-iReadBufPos );
                    memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy );
                    iRead += iCpy;
                    iReadBufPos += iCpy;
                    if( iReadBufPos == iBlockSize )
                    {
                        iReadBufPos = iReadBufFill = 0;
                    }
                }
            }

            return iRead;
        }

        virtual Bu::size write( const void *pBuf, Bu::size iBytes )
        {
            Bu::size iPos = 0;
            
            while( iPos < iBytes )
            {
                int iLeft = Bu::buMin((int)(iBytes-iPos),iBlockSize-iWriteBufFill);
                memcpy( aWriteBuf+iWriteBufFill, (char *)pBuf+iPos, iLeft );
                iPos += iLeft;
                iWriteBufFill += iLeft;
                if( iWriteBufFill == iBlockSize )
                {
                    encipher( aWriteBuf );
                    rNext.write( aWriteBuf, iBlockSize );
                    iWriteBufFill = 0;
                }
            }

            return iPos;
        }

        virtual void flush()
        {
            if( iWriteBufFill > 0 && iWriteBufFill < iBlockSize )
            {
                memset( aWriteBuf+iWriteBufFill, 0, iBlockSize-iWriteBufFill );
                encipher( aWriteBuf );
                rNext.write( aWriteBuf, iBlockSize );
                iWriteBufFill = 0;
            }
            rNext.flush();
        }

        using Bu::Stream::read;
        using Bu::Stream::write;

    protected:
        virtual void encipher( void *pData )=0;
        virtual void decipher( void *pData )=0;

    private:
        char aReadBuf[iBlockSize];
        char aWriteBuf[iBlockSize];
        int iReadBufFill;
        int iReadBufPos;
        int iWriteBufFill;
    };
};

#endif