Adjusted ChaCha for dynamic round selection
I've adjusted the ChaCha code to allow selection of the round number at run-time and to improve readibility (slightly)pull/157/head
parent
da05d100e3
commit
275afb4955
20
chacha.cpp
20
chacha.cpp
|
|
@ -27,8 +27,7 @@ void ChaCha_TestInstantiations()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <unsigned int R>
|
void ChaCha_Policy::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)
|
||||||
void ChaCha_Policy<R>::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)
|
|
||||||
{
|
{
|
||||||
CRYPTOPP_UNUSED(params);
|
CRYPTOPP_UNUSED(params);
|
||||||
assert(length == 16 || length == 32);
|
assert(length == 16 || length == 32);
|
||||||
|
|
@ -46,8 +45,7 @@ void ChaCha_Policy<R>::CipherSetKey(const NameValuePairs ¶ms, const byte *ke
|
||||||
get2(m_state[8])(m_state[9])(m_state[10])(m_state[11]);
|
get2(m_state[8])(m_state[9])(m_state[10])(m_state[11]);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int R>
|
void ChaCha_Policy::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
|
||||||
void ChaCha_Policy<R>::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
|
|
||||||
{
|
{
|
||||||
CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
|
CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
|
||||||
assert(length==8);
|
assert(length==8);
|
||||||
|
|
@ -57,8 +55,7 @@ void ChaCha_Policy<R>::CipherResynchronize(byte *keystreamBuffer, const byte *IV
|
||||||
get(m_state[14])(m_state[15]);
|
get(m_state[14])(m_state[15]);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<unsigned int R>
|
void ChaCha_Policy::SeekToIteration(lword iterationCount)
|
||||||
void ChaCha_Policy<R>::SeekToIteration(lword iterationCount)
|
|
||||||
{
|
{
|
||||||
CRYPTOPP_UNUSED(iterationCount);
|
CRYPTOPP_UNUSED(iterationCount);
|
||||||
throw NotImplemented(std::string(ChaCha_Info<R>::StaticAlgorithmName()) + ": SeekToIteration is not yet implemented");
|
throw NotImplemented(std::string(ChaCha_Info<R>::StaticAlgorithmName()) + ": SeekToIteration is not yet implemented");
|
||||||
|
|
@ -70,8 +67,7 @@ void ChaCha_Policy<R>::SeekToIteration(lword iterationCount)
|
||||||
// m_state[5] = (word32)SafeRightShift<32>(iterationCount);
|
// m_state[5] = (word32)SafeRightShift<32>(iterationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<unsigned int R>
|
unsigned int ChaCha_Policy::GetAlignment() const
|
||||||
unsigned int ChaCha_Policy<R>::GetAlignment() const
|
|
||||||
{
|
{
|
||||||
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && 0
|
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && 0
|
||||||
if (HasSSE2())
|
if (HasSSE2())
|
||||||
|
|
@ -81,8 +77,7 @@ unsigned int ChaCha_Policy<R>::GetAlignment() const
|
||||||
return GetAlignmentOf<word32>();
|
return GetAlignmentOf<word32>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<unsigned int R>
|
unsigned int ChaCha_Policy::GetOptimalBlockSize() const
|
||||||
unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const
|
|
||||||
{
|
{
|
||||||
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && 0
|
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && 0
|
||||||
if (HasSSE2())
|
if (HasSSE2())
|
||||||
|
|
@ -92,8 +87,7 @@ unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const
|
||||||
return BYTES_PER_ITERATION;
|
return BYTES_PER_ITERATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<unsigned int R>
|
void ChaCha_Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
|
||||||
void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
|
|
||||||
{
|
{
|
||||||
word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
|
word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
|
||||||
|
|
||||||
|
|
@ -104,7 +98,7 @@ void ChaCha_Policy<R>::OperateKeystream(KeystreamOperation operation, byte *outp
|
||||||
x8 = m_state[8]; x9 = m_state[9]; x10 = m_state[10]; x11 = m_state[11];
|
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];
|
x12 = m_state[12]; x13 = m_state[13]; x14 = m_state[14]; x15 = m_state[15];
|
||||||
|
|
||||||
for (int i = static_cast<int>(ROUNDS); i > 0; i -= 2)
|
for (int i = static_cast<int>(m_rounds); i > 0; i -= 2)
|
||||||
{
|
{
|
||||||
CHACHA_QUARTER_ROUND(x0, x4, x8, x12);
|
CHACHA_QUARTER_ROUND(x0, x4, x8, x12);
|
||||||
CHACHA_QUARTER_ROUND(x1, x5, x9, x13);
|
CHACHA_QUARTER_ROUND(x1, x5, x9, x13);
|
||||||
|
|
|
||||||
54
chacha.h
54
chacha.h
|
|
@ -4,32 +4,37 @@
|
||||||
// family implementation at http://cr.yp.to/chacha.html.
|
// family implementation at http://cr.yp.to/chacha.html.
|
||||||
|
|
||||||
//! \file chacha.h
|
//! \file chacha.h
|
||||||
//! \brief Classes for ChaCha8, ChaCha12 and ChaCha20 stream ciphers
|
//! \brief Classes for the ChaCha family of stream ciphers
|
||||||
|
|
||||||
#ifndef CRYPTOPP_CHACHA_H
|
#ifndef CRYPTOPP_CHACHA_H
|
||||||
#define CRYPTOPP_CHACHA_H
|
#define CRYPTOPP_CHACHA_H
|
||||||
|
|
||||||
#include "strciphr.h"
|
#include "strciphr.h"
|
||||||
#include "secblock.h"
|
#include "secblock.h"
|
||||||
|
#include "algparam.h" // for MakeParameters()
|
||||||
|
|
||||||
NAMESPACE_BEGIN(CryptoPP)
|
NAMESPACE_BEGIN(CryptoPP)
|
||||||
|
|
||||||
//! \class ChaCha_Info
|
//! \class ChaCha_Info
|
||||||
//! \brief ChaCha stream cipher information
|
//! \brief ChaCha stream cipher information
|
||||||
|
struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
|
||||||
|
{
|
||||||
|
static const char *StaticAlgorithmName() {static const std::string name = "ChaCha"; return name.c_str();}
|
||||||
|
};
|
||||||
|
|
||||||
|
//! \class ChaChaFR_Info
|
||||||
|
//! \brief ChaCha stream cipher information for compile-time fixed rounds
|
||||||
template<unsigned int R>
|
template<unsigned int R>
|
||||||
struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>, public FixedRounds<R>
|
struct ChaChaFR_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>, public FixedRounds<R>
|
||||||
{
|
{
|
||||||
static const char *StaticAlgorithmName() { static const std::string name = "ChaCha" + IntToString(R); return name.c_str(); }
|
static const char *StaticAlgorithmName() { static const std::string name = "ChaCha" + IntToString(R); return name.c_str(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! \class ChaCha_Policy
|
//! \class ChaCha_Policy
|
||||||
//! \brief ChaCha stream cipher implementation
|
//! \brief ChaCha stream cipher implementation
|
||||||
template <unsigned int R>
|
|
||||||
class CRYPTOPP_NO_VTABLE ChaCha_Policy : public AdditiveCipherConcretePolicy<word32, 16>
|
class CRYPTOPP_NO_VTABLE ChaCha_Policy : public AdditiveCipherConcretePolicy<word32, 16>
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
CRYPTOPP_CONSTANT(ROUNDS=FixedRounds<R>::ROUNDS);
|
|
||||||
|
|
||||||
void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length);
|
void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length);
|
||||||
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
|
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
|
||||||
void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
|
void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
|
||||||
|
|
@ -39,33 +44,54 @@ protected:
|
||||||
unsigned int GetOptimalBlockSize() const;
|
unsigned int GetOptimalBlockSize() const;
|
||||||
|
|
||||||
FixedSizeAlignedSecBlock<word32, 16> m_state;
|
FixedSizeAlignedSecBlock<word32, 16> m_state;
|
||||||
|
unsigned int m_rounds;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! \class ChaCha_Policy
|
||||||
|
//! \brief ChaCha stream cipher implementation for fixed rounds
|
||||||
|
template<unsigned int R>
|
||||||
|
class CRYPTOPP_NO_VTABLE ChaChaFR_Policy : public ChaCha_Policy
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
CRYPTOPP_CONSTANT(ROUNDS = FixedRounds<R>::ROUNDS);
|
||||||
|
|
||||||
|
void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) { CRYPTOPP_UNUSED(params); ChaCha_Policy::CipherSetKey(MakeParameters(Name::Rounds(), ROUNDS), key, length); }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! \class ChaCha8
|
//! \class ChaCha8
|
||||||
//! \brief ChaCha8 stream cipher
|
//! \brief ChaCha8 stream cipher
|
||||||
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
||||||
struct ChaCha8 : public ChaCha_Info<8>, public SymmetricCipherDocumentation
|
struct ChaCha8 : public ChaChaFR_Info<8>, public SymmetricCipherDocumentation
|
||||||
{
|
{
|
||||||
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<8>, AdditiveCipherTemplate<> >, ChaCha_Info<8> > Encryption;
|
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaChaFR_Policy<8>, AdditiveCipherTemplate<> >, ChaChaFR_Info<8> > Encryption;
|
||||||
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<8>, AdditiveCipherTemplate<> >, ChaCha_Info<8> > Decryption;
|
typedef Encryption Decryption;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! \class ChaCha12
|
//! \class ChaCha12
|
||||||
//! \brief ChaCha12 stream cipher
|
//! \brief ChaCha12 stream cipher
|
||||||
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
||||||
struct ChaCha12 : public ChaCha_Info<12>, public SymmetricCipherDocumentation
|
struct ChaCha12 : public ChaChaFR_Info<12>, public SymmetricCipherDocumentation
|
||||||
{
|
{
|
||||||
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<12>, AdditiveCipherTemplate<> >, ChaCha_Info<12> > Encryption;
|
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaChaFR_Policy<12>, AdditiveCipherTemplate<> >, ChaChaFR_Info<12> > Encryption;
|
||||||
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<12>, AdditiveCipherTemplate<> >, ChaCha_Info<12> > Decryption;
|
typedef Encryption Decryption;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! \class ChaCha20
|
//! \class ChaCha20
|
||||||
//! \brief ChaCha20 stream cipher
|
//! \brief ChaCha20 stream cipher
|
||||||
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
||||||
struct ChaCha20 : public ChaCha_Info<20>, public SymmetricCipherDocumentation
|
struct ChaCha20 : public ChaChaFR_Info<20>, public SymmetricCipherDocumentation
|
||||||
{
|
{
|
||||||
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<20>, AdditiveCipherTemplate<> >, ChaCha_Info<20> > Encryption;
|
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaChaFR_Policy<20>, AdditiveCipherTemplate<> >, ChaChaFR_Info<20> > Encryption;
|
||||||
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy<20>, AdditiveCipherTemplate<> >, ChaCha_Info<20> > Decryption;
|
typedef Encryption Decryption;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! \class ChaCha
|
||||||
|
//! \brief ChaCha stream cipher
|
||||||
|
//! \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
|
||||||
|
struct ChaCha : public ChaCha_Info, public SymmetricCipherDocumentation
|
||||||
|
{
|
||||||
|
typedef SymmetricCipherFinal<ConcretePolicyHolder<ChaCha_Policy, AdditiveCipherTemplate<> >, ChaCha_Info > Encryption;
|
||||||
|
typedef Encryption Decryption;
|
||||||
};
|
};
|
||||||
|
|
||||||
NAMESPACE_END
|
NAMESPACE_END
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue