Rework OperateKeystream (GH #680)
This improves performance on a Core i5 6400 from 5.4 cpb to 2.9 cpbpull/681/head^2
parent
989bf6dc5e
commit
23c8bfde7e
49
hc256.cpp
49
hc256.cpp
|
|
@ -18,43 +18,43 @@ using CryptoPP::rotrConstant;
|
||||||
|
|
||||||
inline word32 f1(word32 x)
|
inline word32 f1(word32 x)
|
||||||
{
|
{
|
||||||
return rotrConstant<7>(x) ^ rotrConstant<18>(x) ^ ((x) >> 3);
|
return rotrConstant<7>(x) ^ rotrConstant<18>(x) ^ (x >> 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline word32 f2(word32 x)
|
inline word32 f2(word32 x)
|
||||||
{
|
{
|
||||||
return rotrConstant<17>(x) ^ rotrConstant<19>(x) ^ ((x) >> 10);
|
return rotrConstant<17>(x) ^ rotrConstant<19>(x) ^ (x >> 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
ANONYMOUS_NAMESPACE_END
|
ANONYMOUS_NAMESPACE_END
|
||||||
|
|
||||||
NAMESPACE_BEGIN(CryptoPP)
|
NAMESPACE_BEGIN(CryptoPP)
|
||||||
|
|
||||||
word32 HC256Policy::H1(word32 u)
|
inline word32 HC256Policy::H1(word32 u)
|
||||||
{
|
{
|
||||||
word32 tem;
|
word32 tem;
|
||||||
unsigned char a, b, c, d;
|
byte a, b, c, d;
|
||||||
a = (unsigned char)((u));
|
a = (byte)((u));
|
||||||
b = (unsigned char)((u) >> 8);
|
b = (byte)((u) >> 8);
|
||||||
c = (unsigned char)((u) >> 16);
|
c = (byte)((u) >> 16);
|
||||||
d = (unsigned char)((u) >> 24);
|
d = (byte)((u) >> 24);
|
||||||
tem = m_Q[a] + m_Q[256 + b] + m_Q[512 + c] + m_Q[768 + d];
|
tem = m_Q[a] + m_Q[256 + b] + m_Q[512 + c] + m_Q[768 + d];
|
||||||
return (tem);
|
return (tem);
|
||||||
}
|
}
|
||||||
|
|
||||||
word32 HC256Policy::H2(word32 u)
|
inline word32 HC256Policy::H2(word32 u)
|
||||||
{
|
{
|
||||||
word32 tem;
|
word32 tem;
|
||||||
unsigned char a, b, c, d;
|
byte a, b, c, d;
|
||||||
a = (unsigned char)((u));
|
a = (byte)((u));
|
||||||
b = (unsigned char)((u) >> 8);
|
b = (byte)((u) >> 8);
|
||||||
c = (unsigned char)((u) >> 16);
|
c = (byte)((u) >> 16);
|
||||||
d = (unsigned char)((u) >> 24);
|
d = (byte)((u) >> 24);
|
||||||
tem = m_P[a] + m_P[256 + b] + m_P[512 + c] + m_P[768 + d];
|
tem = m_P[a] + m_P[256 + b] + m_P[512 + c] + m_P[768 + d];
|
||||||
return (tem);
|
return (tem);
|
||||||
}
|
}
|
||||||
|
|
||||||
word32 HC256Policy::Generate() /*one step of the cipher*/
|
inline word32 HC256Policy::Generate() /*one step of the cipher*/
|
||||||
{
|
{
|
||||||
word32 i, i3, i10, i12, i1023;
|
word32 i, i3, i10, i12, i1023;
|
||||||
word32 output;
|
word32 output;
|
||||||
|
|
@ -94,17 +94,16 @@ void HC256Policy::CipherSetKey(const NameValuePairs ¶ms, 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) >> 2;
|
size_t msglen = GetBytesPerIteration() * iterationCount;
|
||||||
for (unsigned int i = 0; i < msglen; i++, input += 4, output += 4)
|
const byte* in = input; byte* out = output;
|
||||||
{
|
for (unsigned int i = 0; i < (msglen >> 2); i++, in += 4, out += 4)
|
||||||
PutWord(false, LITTLE_ENDIAN_ORDER, output, Generate());
|
PutWord(false, LITTLE_ENDIAN_ORDER, out, Generate());
|
||||||
|
|
||||||
// If AdditiveCipherTemplate does not have an acculated keystream
|
// If AdditiveCipherTemplate does not have an acculated keystream
|
||||||
// then it will ask OperateKeystream to XOR the plaintext with
|
// then it will ask OperateKeystream to XOR the plaintext with
|
||||||
// the keystream and write it to the ciphertext buffer.
|
// the keystream and write it to the ciphertext buffer.
|
||||||
if ((operation & INPUT_NULL) != INPUT_NULL)
|
if ((operation & INPUT_NULL) != INPUT_NULL)
|
||||||
xorbuf(output, input, 4);
|
xorbuf(output, input, msglen);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HC256Policy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
|
void HC256Policy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue