Cleanup HC128 and HC256 OperateKeystream

pull/730/head
Jeffrey Walton 2018-10-25 08:24:13 -04:00
parent ba5ca6b8cd
commit 352083b1d0
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
2 changed files with 26 additions and 16 deletions

View File

@ -200,8 +200,7 @@ void HC128Policy::CipherSetKey(const NameValuePairs &params, const byte *userKey
void HC128Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) void HC128Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
{ {
size_t msglen = GetBytesPerIteration() * iterationCount; while (iterationCount--)
while (msglen >= 64)
{ {
word32 keystream[16]; word32 keystream[16];
GenerateKeystream(keystream); GenerateKeystream(keystream);
@ -231,9 +230,12 @@ void HC128Policy::OperateKeystream(KeystreamOperation operation, byte *output, c
// keystream is written to the output buffer. The optional part is // keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream. // adding the input buffer and keystream.
if ((operation & INPUT_NULL) != INPUT_NULL) if ((operation & INPUT_NULL) != INPUT_NULL)
xorbuf(output, input, 64); {
xorbuf(output, input, BYTES_PER_ITERATION);
input += BYTES_PER_ITERATION;
}
msglen -= 64; input += 64; output += 64; output += BYTES_PER_ITERATION;
} }
} }

View File

@ -94,19 +94,27 @@ void HC256Policy::CipherSetKey(const NameValuePairs &params, const byte *userKey
void HC256Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) void HC256Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
{ {
size_t msglen = GetBytesPerIteration() * iterationCount; while (iterationCount--)
byte* out = output; {
for (size_t i = 0; i < (msglen >> 2); i++, out += 4) PutWord(false, LITTLE_ENDIAN_ORDER, output + 0, Generate());
PutWord(false, LITTLE_ENDIAN_ORDER, out, Generate()); PutWord(false, LITTLE_ENDIAN_ORDER, output + 4, Generate());
PutWord(false, LITTLE_ENDIAN_ORDER, output + 8, Generate());
PutWord(false, LITTLE_ENDIAN_ORDER, output + 12, Generate());
// If AdditiveCipherTemplate does not have an accumulated keystream // If AdditiveCipherTemplate does not have an accumulated keystream
// then it will ask OperateKeystream to generate one. Optionally it // then it will ask OperateKeystream to generate one. Optionally it
// will ask for an XOR of the input with the keystream while // will ask for an XOR of the input with the keystream while
// writing the result to the output buffer. In all cases the // writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is // keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream. // adding the input buffer and keystream.
if ((operation & INPUT_NULL) != INPUT_NULL) if ((operation & INPUT_NULL) != INPUT_NULL)
xorbuf(output, input, msglen); {
xorbuf(output, input, BYTES_PER_ITERATION);
input += BYTES_PER_ITERATION;
}
output += BYTES_PER_ITERATION;
}
} }
void HC256Policy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length) void HC256Policy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)