From 0bf879883573b9d716e57dee34a050913710a116 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Thu, 28 Sep 2023 00:48:18 -0400 Subject: [PATCH] Switch HC128 and HC256 to use CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH (GH #1231) --- hc128.cpp | 57 ++++++++++++++++++++++++------------------------------- hc256.cpp | 36 +++++++++++++++++------------------ 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/hc128.cpp b/hc128.cpp index 74f67c171..d83ec8784 100644 --- a/hc128.cpp +++ b/hc128.cpp @@ -9,6 +9,7 @@ #include "hc128.h" #include "secblock.h" +#include "strciphr.h" #include "misc.h" /*h1 function*/ @@ -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; @@ -202,40 +225,10 @@ void HC128Policy::OperateKeystream(KeystreamOperation operation, byte *output, c { while (iterationCount--) { - word32 keystream[16]; + FixedSizeSecBlock 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); } } diff --git a/hc256.cpp b/hc256.cpp index 9259b87a7..b0567615d 100644 --- a/hc256.cpp +++ b/hc256.cpp @@ -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; @@ -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 keystream; + keystream[0] = Generate(); + keystream[1] = Generate(); + keystream[2] = Generate(); + keystream[3] = Generate(); + + CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(HC256_OUTPUT, BYTES_PER_ITERATION); } }