27
void CFB_ModePolicy::Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
29
assert(m_cipher->IsForwardTransformation()); // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
30
assert(m_feedbackSize == BlockSize());
32
unsigned int s = BlockSize();
33
if (dir == ENCRYPTION)
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);
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);
48
void CFB_ModePolicy::TransformRegister()
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);
57
void CFB_ModePolicy::CipherResynchronize(const byte *iv, size_t length)
59
memcpy_s(m_register, m_register.size(), iv, BlockSize());
63
void CFB_ModePolicy::SetFeedbackSize(unsigned int feedbackSize)
65
if (feedbackSize > BlockSize())
66
throw InvalidArgument("CFB_Mode: invalid feedback size");
67
m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
70
void CFB_ModePolicy::ResizeBuffers()
72
CipherModeBase::ResizeBuffers();
73
m_temp.New(BlockSize());
76
void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
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);
86
void OFB_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
88
CopyOrZero(m_register, iv, length);
27
91
void CTR_ModePolicy::SeekToIteration(lword iterationCount)
39
inline void CTR_ModePolicy::ProcessMultipleBlocks(byte *output, const byte *input, size_t n)
103
void CTR_ModePolicy::IncrementCounterBy256()
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);
48
108
void CTR_ModePolicy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
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();
54
unsigned int sizeIncrement = BlockSize();
55
while (iterationCount)
57
m_cipher->ProcessAndXorBlock(m_counterArray, input, output);
58
IncrementCounterByOne(m_counterArray, sizeIncrement);
59
output += sizeIncrement;
60
input += sizeIncrement;
66
unsigned int sizeIncrement = maxBlocks * BlockSize();
67
while (iterationCount >= maxBlocks)
69
ProcessMultipleBlocks(output, input, maxBlocks);
70
output += sizeIncrement;
71
input += sizeIncrement;
72
iterationCount -= maxBlocks;
74
if (iterationCount > 0)
75
ProcessMultipleBlocks(output, input, iterationCount);
111
unsigned int s = BlockSize();
112
unsigned int inputIncrement = input ? s : 0;
114
while (iterationCount)
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();
123
input += blocks*inputIncrement;
124
iterationCount -= blocks;
79
void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv)
128
void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
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;
87
135
void BlockOrientedCipherModeBase::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
89
137
m_cipher->SetKey(key, length, params);
91
139
if (IsResynchronizable())
92
Resynchronize(GetIVAndThrowIfInvalid(params));
95
void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, size_t length)
142
const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
143
Resynchronize(iv, (int)ivLength);
147
void ECB_OneWay::ProcessData(byte *outString, const byte *inString, size_t length)
149
assert(length%BlockSize()==0);
150
m_cipher->AdvancedProcessBlocks(inString, NULL, outString, length, 0);
153
void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t length)
100
unsigned int s = BlockSize();
101
assert(length % s == 0);
103
if (!RequireAlignedInput() || IsAlignedOn(inString, m_cipher->BlockAlignment()))
104
ProcessBlocks(outString, inString, length / s);
109
memcpy(m_buffer, inString, s);
110
ProcessBlocks(outString, m_buffer, 1);
114
} while (length > 0);
118
void CBC_Encryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
157
assert(length%BlockSize()==0);
120
159
unsigned int blockSize = BlockSize();
121
xorbuf(m_register, inString, blockSize);
122
while (--numberOfBlocks)
124
m_cipher->ProcessBlock(m_register, outString);
125
inString += blockSize;
126
xorbuf(m_register, inString, outString, blockSize);
127
outString += blockSize;
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);
133
166
void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
157
190
memcpy(outString, m_register, BlockSize());
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)
197
assert(length%BlockSize()==0);
162
199
unsigned int blockSize = BlockSize();
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);
173
207
void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)