~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to modes.cpp

  • Committer: weidai
  • Date: 2009-03-13 11:15:21 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:450
fix compile on OpenSolaris 8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
}
25
25
#endif
26
26
 
 
27
void CFB_ModePolicy::Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
 
28
{
 
29
        assert(m_cipher->IsForwardTransformation());    // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
 
30
        assert(m_feedbackSize == BlockSize());
 
31
 
 
32
        unsigned int s = BlockSize();
 
33
        if (dir == ENCRYPTION)
 
34
        {
 
35
                m_cipher->ProcessAndXorBlock(m_register, input, output);
 
36
                m_cipher->AdvancedProcessBlocks(output, input+s, output+s, (iterationCount-1)*s, 0);
 
37
                memcpy(m_register, output+(iterationCount-1)*s, s);
 
38
        }
 
39
        else
 
40
        {
 
41
                memcpy(m_temp, input+(iterationCount-1)*s, s);  // make copy first in case of in-place decryption
 
42
                m_cipher->AdvancedProcessBlocks(input, input+s, output+s, (iterationCount-1)*s, BlockTransformation::BT_ReverseDirection);
 
43
                m_cipher->ProcessAndXorBlock(m_register, input, output);
 
44
                memcpy(m_register, m_temp, s);
 
45
        }
 
46
}
 
47
 
 
48
void CFB_ModePolicy::TransformRegister()
 
49
{
 
50
        assert(m_cipher->IsForwardTransformation());    // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
 
51
        m_cipher->ProcessBlock(m_register, m_temp);
 
52
        unsigned int updateSize = BlockSize()-m_feedbackSize;
 
53
        memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
 
54
        memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize);
 
55
}
 
56
 
 
57
void CFB_ModePolicy::CipherResynchronize(const byte *iv, size_t length)
 
58
{
 
59
        memcpy_s(m_register, m_register.size(), iv, BlockSize());
 
60
        TransformRegister();
 
61
}
 
62
 
 
63
void CFB_ModePolicy::SetFeedbackSize(unsigned int feedbackSize)
 
64
{
 
65
        if (feedbackSize > BlockSize())
 
66
                throw InvalidArgument("CFB_Mode: invalid feedback size");
 
67
        m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
 
68
}
 
69
 
 
70
void CFB_ModePolicy::ResizeBuffers()
 
71
{
 
72
        CipherModeBase::ResizeBuffers();
 
73
        m_temp.New(BlockSize());
 
74
}
 
75
 
 
76
void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
 
77
{
 
78
        assert(m_cipher->IsForwardTransformation());    // OFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
 
79
        unsigned int s = BlockSize();
 
80
        m_cipher->ProcessBlock(m_register, keystreamBuffer);
 
81
        if (iterationCount > 1)
 
82
                m_cipher->AdvancedProcessBlocks(keystreamBuffer, NULL, keystreamBuffer+s, s*(iterationCount-1), 0);
 
83
        memcpy(m_register, keystreamBuffer+s*(iterationCount-1), s);
 
84
}
 
85
 
 
86
void OFB_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
 
87
{
 
88
        CopyOrZero(m_register, iv, length);
 
89
}
 
90
 
27
91
void CTR_ModePolicy::SeekToIteration(lword iterationCount)
28
92
{
29
93
        int carry=0;
36
100
        }
37
101
}
38
102
 
39
 
inline void CTR_ModePolicy::ProcessMultipleBlocks(byte *output, const byte *input, size_t n)
 
103
void CTR_ModePolicy::IncrementCounterBy256()
40
104
{
41
 
        unsigned int s = BlockSize(), j = 0;
42
 
        for (unsigned int i=1; i<n; i++, j+=s)
43
 
                IncrementCounterByOne(m_counterArray + j + s, m_counterArray + j, s);
44
 
        m_cipher->ProcessAndXorMultipleBlocks(m_counterArray, input, output, n);
45
 
        IncrementCounterByOne(m_counterArray, m_counterArray + s*(n-1), s);
 
105
        IncrementCounterByOne(m_counterArray, BlockSize()-1);
46
106
}
47
107
 
48
108
void CTR_ModePolicy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
49
109
{
50
110
        assert(m_cipher->IsForwardTransformation());    // CTR mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
51
 
        unsigned int maxBlocks = m_cipher->OptimalNumberOfParallelBlocks();
52
 
        if (maxBlocks == 1)
53
 
        {
54
 
                unsigned int sizeIncrement = BlockSize();
55
 
                while (iterationCount)
56
 
                {
57
 
                        m_cipher->ProcessAndXorBlock(m_counterArray, input, output);
58
 
                        IncrementCounterByOne(m_counterArray, sizeIncrement);
59
 
                        output += sizeIncrement;
60
 
                        input += sizeIncrement;
61
 
                        iterationCount -= 1;
62
 
                }
63
 
        }
64
 
        else
65
 
        {
66
 
                unsigned int sizeIncrement = maxBlocks * BlockSize();
67
 
                while (iterationCount >= maxBlocks)
68
 
                {
69
 
                        ProcessMultipleBlocks(output, input, maxBlocks);
70
 
                        output += sizeIncrement;
71
 
                        input += sizeIncrement;
72
 
                        iterationCount -= maxBlocks;
73
 
                }
74
 
                if (iterationCount > 0)
75
 
                        ProcessMultipleBlocks(output, input, iterationCount);
 
111
        unsigned int s = BlockSize();
 
112
        unsigned int inputIncrement = input ? s : 0;
 
113
 
 
114
        while (iterationCount)
 
115
        {
 
116
                byte lsb = m_counterArray[s-1];
 
117
                size_t blocks = UnsignedMin(iterationCount, 256U-lsb);
 
118
                m_cipher->AdvancedProcessBlocks(m_counterArray, input, output, blocks*s, BlockTransformation::BT_InBlockIsCounter);
 
119
                if ((m_counterArray[s-1] = lsb + (byte)blocks) == 0)
 
120
                        IncrementCounterBy256();
 
121
 
 
122
                output += blocks*s;
 
123
                input += blocks*inputIncrement;
 
124
                iterationCount -= blocks;
76
125
        }
77
126
}
78
127
 
79
 
void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv)
 
128
void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
80
129
{
81
 
        unsigned int s = BlockSize();
82
 
        CopyOrZero(m_register, iv, s);
83
 
        m_counterArray.New(s * m_cipher->OptimalNumberOfParallelBlocks());
84
 
        CopyOrZero(m_counterArray, iv, s);
 
130
        assert(length == BlockSize());
 
131
        CopyOrZero(m_register, iv, length);
 
132
        m_counterArray = m_register;
85
133
}
86
134
 
87
135
void BlockOrientedCipherModeBase::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
89
137
        m_cipher->SetKey(key, length, params);
90
138
        ResizeBuffers();
91
139
        if (IsResynchronizable())
92
 
                Resynchronize(GetIVAndThrowIfInvalid(params));
93
 
}
94
 
 
95
 
void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, size_t length)
 
140
        {
 
141
                size_t ivLength;
 
142
                const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
 
143
                Resynchronize(iv, (int)ivLength);
 
144
        }
 
145
}
 
146
 
 
147
void ECB_OneWay::ProcessData(byte *outString, const byte *inString, size_t length)
 
148
{
 
149
        assert(length%BlockSize()==0);
 
150
        m_cipher->AdvancedProcessBlocks(inString, NULL, outString, length, 0);
 
151
}
 
152
 
 
153
void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t length)
96
154
{
97
155
        if (!length)
98
156
                return;
99
 
 
100
 
        unsigned int s = BlockSize();
101
 
        assert(length % s == 0);
102
 
 
103
 
        if (!RequireAlignedInput() || IsAlignedOn(inString, m_cipher->BlockAlignment()))
104
 
                ProcessBlocks(outString, inString, length / s);
105
 
        else
106
 
        {
107
 
                do
108
 
                {
109
 
                        memcpy(m_buffer, inString, s);
110
 
                        ProcessBlocks(outString, m_buffer, 1);
111
 
                        inString += s;
112
 
                        outString += s;
113
 
                        length -= s;
114
 
                } while (length > 0);
115
 
        }
116
 
}
117
 
 
118
 
void CBC_Encryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
119
 
{
 
157
        assert(length%BlockSize()==0);
 
158
 
120
159
        unsigned int blockSize = BlockSize();
121
 
        xorbuf(m_register, inString, blockSize);
122
 
        while (--numberOfBlocks)
123
 
        {
124
 
                m_cipher->ProcessBlock(m_register, outString);
125
 
                inString += blockSize;
126
 
                xorbuf(m_register, inString, outString, blockSize);
127
 
                outString += blockSize;
128
 
        }
129
 
        m_cipher->ProcessBlock(m_register);
130
 
        memcpy(outString, m_register, blockSize);
 
160
        m_cipher->AdvancedProcessBlocks(inString, m_register, outString, blockSize, BlockTransformation::BT_XorInput);
 
161
        if (length > blockSize)
 
162
                m_cipher->AdvancedProcessBlocks(inString+blockSize, outString, outString+blockSize, length-blockSize, BlockTransformation::BT_XorInput);
 
163
        memcpy(m_register, outString + length - blockSize, blockSize);
131
164
}
132
165
 
133
166
void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
157
190
        memcpy(outString, m_register, BlockSize());
158
191
}
159
192
 
160
 
void CBC_Decryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
 
193
void CBC_Decryption::ProcessData(byte *outString, const byte *inString, size_t length)
161
194
{
 
195
        if (!length)
 
196
                return;
 
197
        assert(length%BlockSize()==0);
 
198
 
162
199
        unsigned int blockSize = BlockSize();
163
 
        do
164
 
        {
165
 
                memcpy(m_temp, inString, blockSize);    // make copy in case we're doing in place decryption
166
 
                m_cipher->ProcessAndXorBlock(m_temp, m_register, outString);
167
 
                m_register.swap(m_temp);
168
 
                inString += blockSize;
169
 
                outString += blockSize;
170
 
        } while (--numberOfBlocks);
 
200
        memcpy(m_temp, inString+length-blockSize, blockSize);   // save copy now in case of in-place decryption
 
201
        if (length > blockSize)
 
202
                m_cipher->AdvancedProcessBlocks(inString+blockSize, inString, outString+blockSize, length-blockSize, BlockTransformation::BT_ReverseDirection);
 
203
        m_cipher->ProcessAndXorBlock(inString, m_register, outString);
 
204
        m_register.swap(m_temp);
171
205
}
172
206
 
173
207
void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)