Skip to content

Commit e2be0cd

Browse files
committed
Make ChaCha an Salsa use the same design pattern
1 parent 3e55bfc commit e2be0cd

File tree

5 files changed

+31
-60
lines changed

5 files changed

+31
-60
lines changed

TestVectors/chacha.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AlgorithmType: SymmetricCipher
2-
Name: ChaCha8
2+
Name: ChaCha
33
Source: http://tools.ietf.org/html/draft-strombergson-chacha-test-vectors
44
Comment: TC1 - All zero key and IV (16-byte key).
55
Key: r16 00
@@ -40,7 +40,7 @@ Ciphertext: 2b8f4bb3798306ca5130d47c4f8d4ed13aa0edccc1be6942090faeeca0d7599b7ff0
4040
Test: Encrypt
4141

4242
AlgorithmType: SymmetricCipher
43-
Name: ChaCha12
43+
Name: ChaCha
4444
Source: http://tools.ietf.org/html/draft-strombergson-chacha-test-vectors
4545
Comment: TC1 - All zero key and IV (16-byte key).
4646
Key: r16 00
@@ -81,7 +81,7 @@ Ciphertext: 64b8bdf87b828c4b6dbaf7ef698de03df8b33f635714418f9836ade59be1296946c9
8181
Test: Encrypt
8282

8383
AlgorithmType: SymmetricCipher
84-
Name: ChaCha20
84+
Name: ChaCha
8585
Source: http://tools.ietf.org/html/draft-strombergson-chacha-test-vectors
8686
Comment: TC1 - All zero key and IV (16-byte key).
8787
Key: r16 00

bench2.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ void Benchmark2(double t, double hertz)
138138
BenchMarkByName<SymmetricCipher>("Salsa20");
139139
BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
140140
BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
141-
BenchMarkByName<SymmetricCipher>("ChaCha8");
142-
BenchMarkByName<SymmetricCipher>("ChaCha12");
143-
BenchMarkByName<SymmetricCipher>("ChaCha20");
141+
BenchMarkByName<SymmetricCipher>("ChaCha");
142+
BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha/12", MakeParameters(Name::Rounds(), 12));
143+
BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha/8", MakeParameters(Name::Rounds(), 8));
144144
BenchMarkByName<SymmetricCipher>("Sosemanuk");
145145
BenchMarkByName<SymmetricCipher>("Rabbit");
146146
BenchMarkByName<SymmetricCipher>("RabbitWithIV");

chacha.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ NAMESPACE_BEGIN(CryptoPP)
2020
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
2121
void 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 &params, const byte *key, size_t length)
27+
void ChaCha_Policy::CipherSetKey(const NameValuePairs &params, 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 &params, 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-
151143
NAMESPACE_END

chacha.h

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,18 @@ NAMESPACE_BEGIN(CryptoPP)
2020

2121
/// \brief ChaCha stream cipher information
2222
/// \since Crypto++ 5.6.4
23-
template <unsigned int R>
24-
struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>, public FixedRounds<R>
23+
struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
2524
{
26-
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {
27-
return (R==8?"ChaCha8":(R==12?"ChaCha12":(R==20?"ChaCha20":"ChaCha")));
25+
static const char* StaticAlgorithmName() {
26+
return "ChaCha";
2827
}
2928
};
3029

3130
/// \brief ChaCha stream cipher implementation
3231
/// \since Crypto++ 5.6.4
33-
template <unsigned int R>
3432
class CRYPTOPP_NO_VTABLE ChaCha_Policy : public AdditiveCipherConcretePolicy<word32, 16>
3533
{
3634
protected:
37-
CRYPTOPP_CONSTANT(ROUNDS=FixedRounds<R>::ROUNDS)
3835
void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
3936
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
4037
void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
@@ -47,33 +44,17 @@ class CRYPTOPP_NO_VTABLE ChaCha_Policy : public AdditiveCipherConcretePolicy<wor
4744
int m_rounds;
4845
};
4946

50-
/// \brief ChaCha8 stream cipher
51-
/// \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
52-
/// \since Crypto++ 5.6.4
53-
struct ChaCha8 : public ChaCha_Info<8>, public SymmetricCipherDocumentation
54-
{
55-
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<8>, AdditiveCipherTemplate<> >, ChaCha_Info<8> > Encryption;
56-
typedef Encryption Decryption;
57-
};
58-
59-
/// \brief ChaCha12 stream cipher
60-
/// \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
61-
/// \since Crypto++ 5.6.4
62-
struct ChaCha12 : public ChaCha_Info<12>, public SymmetricCipherDocumentation
63-
{
64-
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<12>, AdditiveCipherTemplate<> >, ChaCha_Info<12> > Encryption;
65-
typedef Encryption Decryption;
66-
};
67-
68-
/// \brief ChaCha20 stream cipher
69-
/// \details Bernstein and ECRYPT's ChaCha is _slightly_ different from the TLS working roup's implementation for
70-
/// cipher suites <tt>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>,
71-
/// <tt>TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256</tt>, and <tt>TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>.
47+
/// \brief ChaCha stream cipher
48+
/// \details Bernstein and ECRYPT's ChaCha is _slightly_ different from the TLS working
49+
/// group's implementation for cipher suites
50+
/// <tt>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>,
51+
/// <tt>TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256</tt>, and
52+
/// <tt>TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>.
7253
/// \sa <a href="http://cr.yp.to/chacha/chacha-20080208.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
7354
/// \since Crypto++ 5.6.4
74-
struct ChaCha20 : public ChaCha_Info<20>, public SymmetricCipherDocumentation
55+
struct ChaCha : public ChaCha_Info, public SymmetricCipherDocumentation
7556
{
76-
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<20>, AdditiveCipherTemplate<> >, ChaCha_Info<20> > Encryption;
57+
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy, AdditiveCipherTemplate<> >, ChaCha_Info > Encryption;
7758
typedef Encryption Decryption;
7859
};
7960

regtest2.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ void RegisterFactories3()
9292

9393
RegisterSymmetricCipherDefaultFactories<Salsa20>();
9494
RegisterSymmetricCipherDefaultFactories<XSalsa20>();
95-
RegisterSymmetricCipherDefaultFactories<ChaCha8>();
96-
RegisterSymmetricCipherDefaultFactories<ChaCha12>();
97-
RegisterSymmetricCipherDefaultFactories<ChaCha20>();
95+
RegisterSymmetricCipherDefaultFactories<ChaCha>();
9896
RegisterSymmetricCipherDefaultFactories<Sosemanuk>();
9997
RegisterSymmetricCipherDefaultFactories<Rabbit>();
10098
RegisterSymmetricCipherDefaultFactories<RabbitWithIV>();

0 commit comments

Comments
 (0)