@@ -20,18 +20,19 @@ NAMESPACE_BEGIN(CryptoPP)
2020#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
2121void ChaCha_TestInstantiations ()
2222{
23- ChaCha8::Encryption x1;
24- ChaCha12::Encryption x2;
25- ChaCha20::Encryption x3;
23+ ChaCha8::Encryption x;
2624}
2725#endif
2826
29- template <unsigned int R>
30- void ChaCha_Policy<R>::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)
27+ void ChaCha_Policy::CipherSetKey (const NameValuePairs ¶ms, const byte *key, size_t length)
3128{
3229 CRYPTOPP_UNUSED (params);
3330 CRYPTOPP_ASSERT (length == 16 || length == 32 );
3431
32+ m_rounds = params.GetIntValueWithDefault (Name::Rounds (), 20 );
33+ if (!(m_rounds == 8 || m_rounds == 12 || m_rounds == 20 ))
34+ throw InvalidRounds (ChaCha::StaticAlgorithmName (), m_rounds);
35+
3536 // "expand 16-byte k" or "expand 32-byte k"
3637 m_state[0 ] = 0x61707865 ;
3738 m_state[1 ] = (length == 16 ) ? 0x3120646e : 0x3320646e ;
@@ -45,8 +46,7 @@ void ChaCha_Policy<R>::CipherSetKey(const NameValuePairs ¶ms, const byte *ke
4546 get2 (m_state[8 ])(m_state[9 ])(m_state[10 ])(m_state[11 ]);
4647}
4748
48- template <unsigned int R>
49- void ChaCha_Policy<R>::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
49+ void ChaCha_Policy::CipherResynchronize (byte *keystreamBuffer, const byte *IV, size_t length)
5050{
5151 CRYPTOPP_UNUSED (keystreamBuffer), CRYPTOPP_UNUSED (length);
5252 CRYPTOPP_ASSERT (length==8 );
@@ -56,11 +56,10 @@ void ChaCha_Policy<R>::CipherResynchronize(byte *keystreamBuffer, const byte *IV
5656 get (m_state[14 ])(m_state[15 ]);
5757}
5858
59- template <unsigned int R>
60- void ChaCha_Policy<R>::SeekToIteration(lword iterationCount)
59+ void ChaCha_Policy::SeekToIteration (lword iterationCount)
6160{
6261 CRYPTOPP_UNUSED (iterationCount);
63- throw NotImplemented (std::string (ChaCha_Info<R> ::StaticAlgorithmName ()) + " : SeekToIteration is not yet implemented" );
62+ throw NotImplemented (std::string (ChaCha_Info::StaticAlgorithmName ()) + " : SeekToIteration is not yet implemented" );
6463
6564 // TODO: these were Salsa20, and Wei re-arranged the state array for SSE2 operations.
6665 // If we can generate some out-of-band test vectors, then test and implement. Also
@@ -69,8 +68,7 @@ void ChaCha_Policy<R>::SeekToIteration(lword iterationCount)
6968 // m_state[5] = (word32)SafeRightShift<32>(iterationCount);
7069}
7170
72- template <unsigned int R>
73- unsigned int ChaCha_Policy<R>::GetAlignment() const
71+ unsigned int ChaCha_Policy::GetAlignment () const
7472{
7573#if CRYPTOPP_SSE2_ASM_AVAILABLE && 0
7674 if (HasSSE2 ())
@@ -80,8 +78,7 @@ unsigned int ChaCha_Policy<R>::GetAlignment() const
8078 return GetAlignmentOf<word32>();
8179}
8280
83- template <unsigned int R>
84- unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const
81+ unsigned int ChaCha_Policy::GetOptimalBlockSize () const
8582{
8683#if CRYPTOPP_SSE2_ASM_AVAILABLE && 0
8784 if (HasSSE2 ())
@@ -91,8 +88,7 @@ unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const
9188 return BYTES_PER_ITERATION;
9289}
9390
94- template <unsigned int R>
95- void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
91+ void ChaCha_Policy::OperateKeystream (KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
9692{
9793 word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
9894
@@ -103,7 +99,7 @@ void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *outp
10399 x8 = m_state[8 ]; x9 = m_state[9 ]; x10 = m_state[10 ]; x11 = m_state[11 ];
104100 x12 = m_state[12 ]; x13 = m_state[13 ]; x14 = m_state[14 ]; x15 = m_state[15 ];
105101
106- for (int i = static_cast <int >(ROUNDS ); i > 0 ; i -= 2 )
102+ for (int i = static_cast <int >(m_rounds ); i > 0 ; i -= 2 )
107103 {
108104 CHACHA_QUARTER_ROUND (x0, x4, x8, x12);
109105 CHACHA_QUARTER_ROUND (x1, x5, x9, x13);
@@ -144,8 +140,4 @@ void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *outp
144140 }
145141}
146142
147- template class ChaCha_Policy <8 >;
148- template class ChaCha_Policy <12 >;
149- template class ChaCha_Policy <20 >;
150-
151143NAMESPACE_END
0 commit comments