summaryrefslogtreecommitdiff
path: root/chacha.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2018-10-25 12:15:33 -0400
committerJeffrey Walton <noloader@gmail.com>2018-10-25 12:15:33 -0400
commitb4b36239385a65e0c4a3fa4b196ad60cc61d80ac (patch)
tree515d098ddbc33db7abf8cff8641bc4e43a3aa849 /chacha.cpp
parentb1050636a6148a937c61d19d100f4f808bde04aa (diff)
downloadcryptopp-git-b4b36239385a65e0c4a3fa4b196ad60cc61d80ac.tar.gz
Whitespace check-in
Diffstat (limited to 'chacha.cpp')
-rw-r--r--chacha.cpp222
1 files changed, 111 insertions, 111 deletions
diff --git a/chacha.cpp b/chacha.cpp
index aa2e9268..9a5aedfc 100644
--- a/chacha.cpp
+++ b/chacha.cpp
@@ -28,84 +28,84 @@ extern void ChaCha_OperateKeystream_SSE2(const word32 *state, const byte* input,
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
void ChaCha_TestInstantiations()
{
- ChaCha::Encryption x;
+ ChaCha::Encryption x;
}
#endif
std::string ChaCha_Policy::AlgorithmProvider() const
{
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE)
- if (HasSSE2())
- return "SSE2";
+ if (HasSSE2())
+ return "SSE2";
#endif
#if (CRYPTOPP_ARM_NEON_AVAILABLE)
- if (HasNEON())
- return "NEON";
+ if (HasNEON())
+ return "NEON";
#endif
- return "C++";
+ return "C++";
}
void ChaCha_Policy::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
{
- CRYPTOPP_UNUSED(params);
- CRYPTOPP_ASSERT(length == 16 || length == 32);
+ CRYPTOPP_UNUSED(params);
+ CRYPTOPP_ASSERT(length == 16 || length == 32);
- m_rounds = params.GetIntValueWithDefault(Name::Rounds(), 20);
- if (!(m_rounds == 8 || m_rounds == 12 || m_rounds == 20))
- throw InvalidRounds(ChaCha::StaticAlgorithmName(), m_rounds);
+ m_rounds = params.GetIntValueWithDefault(Name::Rounds(), 20);
+ if (!(m_rounds == 8 || m_rounds == 12 || m_rounds == 20))
+ throw InvalidRounds(ChaCha::StaticAlgorithmName(), m_rounds);
- // "expand 16-byte k" or "expand 32-byte k"
- m_state[0] = 0x61707865;
- m_state[1] = (length == 16) ? 0x3120646e : 0x3320646e;
- m_state[2] = (length == 16) ? 0x79622d36 : 0x79622d32;
- m_state[3] = 0x6b206574;
+ // "expand 16-byte k" or "expand 32-byte k"
+ m_state[0] = 0x61707865;
+ m_state[1] = (length == 16) ? 0x3120646e : 0x3320646e;
+ m_state[2] = (length == 16) ? 0x79622d36 : 0x79622d32;
+ m_state[3] = 0x6b206574;
- GetBlock<word32, LittleEndian> get1(key);
- get1(m_state[4])(m_state[5])(m_state[6])(m_state[7]);
+ GetBlock<word32, LittleEndian> get1(key);
+ get1(m_state[4])(m_state[5])(m_state[6])(m_state[7]);
- GetBlock<word32, LittleEndian> get2(key + ((length == 32) ? 16 : 0));
- get2(m_state[8])(m_state[9])(m_state[10])(m_state[11]);
+ GetBlock<word32, LittleEndian> get2(key + ((length == 32) ? 16 : 0));
+ get2(m_state[8])(m_state[9])(m_state[10])(m_state[11]);
}
void ChaCha_Policy::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
{
- CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
- CRYPTOPP_ASSERT(length==8);
+ CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
+ CRYPTOPP_ASSERT(length==8);
- GetBlock<word32, LittleEndian> get(IV);
- m_state[12] = m_state[13] = 0;
- get(m_state[14])(m_state[15]);
+ GetBlock<word32, LittleEndian> get(IV);
+ m_state[12] = m_state[13] = 0;
+ get(m_state[14])(m_state[15]);
}
void ChaCha_Policy::SeekToIteration(lword iterationCount)
{
- m_state[13] = (word32)iterationCount;
- m_state[12] = (word32)SafeRightShift<32>(iterationCount);
+ m_state[13] = (word32)iterationCount;
+ m_state[12] = (word32)SafeRightShift<32>(iterationCount);
}
unsigned int ChaCha_Policy::GetAlignment() const
{
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE)
- if (HasSSE2())
- return 16;
- else
+ if (HasSSE2())
+ return 16;
+ else
#endif
- return GetAlignmentOf<word32>();
+ return GetAlignmentOf<word32>();
}
unsigned int ChaCha_Policy::GetOptimalBlockSize() const
{
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE)
- if (HasSSE2())
- return 4*BYTES_PER_ITERATION;
- else
+ if (HasSSE2())
+ return 4*BYTES_PER_ITERATION;
+ else
#endif
#if (CRYPTOPP_ARM_NEON_AVAILABLE)
- if (HasNEON())
- return 4*BYTES_PER_ITERATION;
- else
+ if (HasNEON())
+ return 4*BYTES_PER_ITERATION;
+ else
#endif
- return BYTES_PER_ITERATION;
+ return BYTES_PER_ITERATION;
}
// OperateKeystream always produces a key stream. The key stream is written
@@ -115,91 +115,91 @@ void ChaCha_Policy::OperateKeystream(KeystreamOperation operation,
byte *output, const byte *input, size_t iterationCount)
{
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE)
- if (HasSSE2())
- {
- while (iterationCount >= 4)
- {
- bool xorInput = (operation & INPUT_NULL) != INPUT_NULL;
- ChaCha_OperateKeystream_SSE2(m_state, input, output, m_rounds, xorInput);
-
- m_state[12] += 4;
- if (m_state[12] < 4)
- m_state[13]++;
-
- input += (!!xorInput)*4*BYTES_PER_ITERATION;
- output += 4*BYTES_PER_ITERATION;
- iterationCount -= 4;
- }
- }
+ if (HasSSE2())
+ {
+ while (iterationCount >= 4)
+ {
+ bool xorInput = (operation & INPUT_NULL) != INPUT_NULL;
+ ChaCha_OperateKeystream_SSE2(m_state, input, output, m_rounds, xorInput);
+
+ m_state[12] += 4;
+ if (m_state[12] < 4)
+ m_state[13]++;
+
+ input += (!!xorInput)*4*BYTES_PER_ITERATION;
+ output += 4*BYTES_PER_ITERATION;
+ iterationCount -= 4;
+ }
+ }
#endif
#if (CRYPTOPP_ARM_NEON_AVAILABLE)
- if (HasNEON())
- {
- while (iterationCount >= 4)
- {
- bool xorInput = (operation & INPUT_NULL) != INPUT_NULL;
- ChaCha_OperateKeystream_NEON(m_state, input, output, m_rounds, xorInput);
-
- m_state[12] += 4;
- if (m_state[12] < 4)
- m_state[13]++;
-
- input += (!!xorInput)*4*BYTES_PER_ITERATION;
- output += 4*BYTES_PER_ITERATION;
- iterationCount -= 4;
- }
- }
+ if (HasNEON())
+ {
+ while (iterationCount >= 4)
+ {
+ bool xorInput = (operation & INPUT_NULL) != INPUT_NULL;
+ ChaCha_OperateKeystream_NEON(m_state, input, output, m_rounds, xorInput);
+
+ m_state[12] += 4;
+ if (m_state[12] < 4)
+ m_state[13]++;
+
+ input += (!!xorInput)*4*BYTES_PER_ITERATION;
+ output += 4*BYTES_PER_ITERATION;
+ iterationCount -= 4;
+ }
+ }
#endif
- while (iterationCount--)
- {
- word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
+ while (iterationCount--)
+ {
+ word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
- x0 = m_state[0]; x1 = m_state[1]; x2 = m_state[2]; x3 = m_state[3];
- x4 = m_state[4]; x5 = m_state[5]; x6 = m_state[6]; x7 = m_state[7];
- x8 = m_state[8]; x9 = m_state[9]; x10 = m_state[10]; x11 = m_state[11];
- x12 = m_state[12]; x13 = m_state[13]; x14 = m_state[14]; x15 = m_state[15];
+ x0 = m_state[0]; x1 = m_state[1]; x2 = m_state[2]; x3 = m_state[3];
+ x4 = m_state[4]; x5 = m_state[5]; x6 = m_state[6]; x7 = m_state[7];
+ x8 = m_state[8]; x9 = m_state[9]; x10 = m_state[10]; x11 = m_state[11];
+ x12 = m_state[12]; x13 = m_state[13]; x14 = m_state[14]; x15 = m_state[15];
- for (int i = static_cast<int>(m_rounds); i > 0; i -= 2)
- {
- CHACHA_QUARTER_ROUND(x0, x4, x8, x12);
- CHACHA_QUARTER_ROUND(x1, x5, x9, x13);
- CHACHA_QUARTER_ROUND(x2, x6, x10, x14);
- CHACHA_QUARTER_ROUND(x3, x7, x11, x15);
+ for (int i = static_cast<int>(m_rounds); i > 0; i -= 2)
+ {
+ CHACHA_QUARTER_ROUND(x0, x4, x8, x12);
+ CHACHA_QUARTER_ROUND(x1, x5, x9, x13);
+ CHACHA_QUARTER_ROUND(x2, x6, x10, x14);
+ CHACHA_QUARTER_ROUND(x3, x7, x11, x15);
- CHACHA_QUARTER_ROUND(x0, x5, x10, x15);
- CHACHA_QUARTER_ROUND(x1, x6, x11, x12);
- CHACHA_QUARTER_ROUND(x2, x7, x8, x13);
- CHACHA_QUARTER_ROUND(x3, x4, x9, x14);
- }
+ CHACHA_QUARTER_ROUND(x0, x5, x10, x15);
+ CHACHA_QUARTER_ROUND(x1, x6, x11, x12);
+ CHACHA_QUARTER_ROUND(x2, x7, x8, x13);
+ CHACHA_QUARTER_ROUND(x3, x4, x9, x14);
+ }
#ifndef CRYPTOPP_DOXYGEN_PROCESSING
- #define CHACHA_OUTPUT(x){\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 0, x0 + m_state[0]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 1, x1 + m_state[1]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 2, x2 + m_state[2]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 3, x3 + m_state[3]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 4, x4 + m_state[4]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 5, x5 + m_state[5]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 6, x6 + m_state[6]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 7, x7 + m_state[7]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 8, x8 + m_state[8]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 9, x9 + m_state[9]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 10, x10 + m_state[10]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 11, x11 + m_state[11]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 12, x12 + m_state[12]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 13, x13 + m_state[13]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 14, x14 + m_state[14]);\
- CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 15, x15 + m_state[15]);}
-
- CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(CHACHA_OUTPUT, BYTES_PER_ITERATION);
- #undef CHACHA_OUTPUT
+ #define CHACHA_OUTPUT(x){\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 0, x0 + m_state[0]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 1, x1 + m_state[1]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 2, x2 + m_state[2]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 3, x3 + m_state[3]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 4, x4 + m_state[4]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 5, x5 + m_state[5]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 6, x6 + m_state[6]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 7, x7 + m_state[7]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 8, x8 + m_state[8]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 9, x9 + m_state[9]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 10, x10 + m_state[10]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 11, x11 + m_state[11]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 12, x12 + m_state[12]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 13, x13 + m_state[13]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 14, x14 + m_state[14]);\
+ CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 15, x15 + m_state[15]);}
+
+ CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(CHACHA_OUTPUT, BYTES_PER_ITERATION);
+ #undef CHACHA_OUTPUT
#endif
- if (++m_state[12] == 0)
- m_state[13]++;
- }
+ if (++m_state[12] == 0)
+ m_state[13]++;
+ }
}
NAMESPACE_END