Skip to content

Commit

Permalink
Switch HC128 and HC256 to use CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH (GH #1231
Browse files Browse the repository at this point in the history
)
  • Loading branch information
noloader committed Sep 28, 2023
1 parent b157b4d commit 0bf8798
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
57 changes: 25 additions & 32 deletions hc128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "hc128.h"
#include "secblock.h"
#include "strciphr.h"
#include "misc.h"

/*h1 function*/
Expand Down Expand Up @@ -73,6 +74,28 @@
(m_Y[(a)]) = (m_T[(u)]); \
}

#define BYTES_PER_ITERATION 64

#define WordType word32

#define HC128_OUTPUT(x){\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 0, keystream[ 0]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 1, keystream[ 1]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 2, keystream[ 2]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 3, keystream[ 3]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 4, keystream[ 4]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 5, keystream[ 5]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 6, keystream[ 6]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 7, keystream[ 7]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 8, keystream[ 8]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 9, keystream[ 9]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 10, keystream[10]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 11, keystream[11]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 12, keystream[12]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 13, keystream[13]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 14, keystream[14]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 15, keystream[15]);}

ANONYMOUS_NAMESPACE_BEGIN

using CryptoPP::word32;
Expand Down Expand Up @@ -202,40 +225,10 @@ void HC128Policy::OperateKeystream(KeystreamOperation operation, byte *output, c
{
while (iterationCount--)
{
word32 keystream[16];
FixedSizeSecBlock<word32, 16> keystream;
GenerateKeystream(keystream);

PutWord(false, LITTLE_ENDIAN_ORDER, output + 0, keystream[0]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 4, keystream[1]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 8, keystream[2]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 12, keystream[3]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 16, keystream[4]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 20, keystream[5]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 24, keystream[6]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 28, keystream[7]);

PutWord(false, LITTLE_ENDIAN_ORDER, output + 32, keystream[8]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 36, keystream[9]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 40, keystream[10]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 44, keystream[11]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 48, keystream[12]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 52, keystream[13]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 56, keystream[14]);
PutWord(false, LITTLE_ENDIAN_ORDER, output + 60, keystream[15]);

// If AdditiveCipherTemplate does not have an accumulated keystream
// then it will ask OperateKeystream to generate one. Optionally it
// will ask for an XOR of the input with the keystream while
// writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream.
if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL))
{
xorbuf(output, input, BYTES_PER_ITERATION);
input += BYTES_PER_ITERATION;
}

output += BYTES_PER_ITERATION;
CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(HC128_OUTPUT, BYTES_PER_ITERATION);
}
}

Expand Down
36 changes: 18 additions & 18 deletions hc256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@

#include "hc256.h"
#include "secblock.h"
#include "strciphr.h"
#include "misc.h"

#define BYTES_PER_ITERATION 16

#define WordType word32

#define HC256_OUTPUT(x){\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 0, keystream[0]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 1, keystream[1]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 2, keystream[2]);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 3, keystream[3]);}

ANONYMOUS_NAMESPACE_BEGIN

using CryptoPP::word32;
Expand Down Expand Up @@ -96,24 +107,13 @@ void HC256Policy::OperateKeystream(KeystreamOperation operation, byte *output, c
{
while (iterationCount--)
{
PutWord(false, LITTLE_ENDIAN_ORDER, output + 0, 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
// then it will ask OperateKeystream to generate one. Optionally it
// will ask for an XOR of the input with the keystream while
// writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream.
if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL))
{
xorbuf(output, input, BYTES_PER_ITERATION);
input += BYTES_PER_ITERATION;
}

output += BYTES_PER_ITERATION;
FixedSizeSecBlock<word32, 4> keystream;
keystream[0] = Generate();
keystream[1] = Generate();
keystream[2] = Generate();
keystream[3] = Generate();

CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(HC256_OUTPUT, BYTES_PER_ITERATION);
}
}

Expand Down

0 comments on commit 0bf8798

Please sign in to comment.