2017-01-27 12:05:45 +00:00
|
|
|
// strciphr.cpp - originally written and placed in the public domain by Wei Dai
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2021-11-29 15:41:49 +00:00
|
|
|
// TODO: Figure out what is happening in ProcessData. The issue surfaced
|
|
|
|
// for CFB_CipherTemplate<BASE>::ProcessData when we cut-in Cryptogams
|
|
|
|
// AES ARMv7 asm. Then again in AdditiveCipherTemplate<S>::ProcessData
|
|
|
|
// for CTR mode with HIGHT, which is a 64-bit block cipher. In both cases,
|
|
|
|
// inString == outString leads to incorrect results. We think it relates
|
|
|
|
// to aliasing violations because inString == outString.
|
2021-03-17 16:17:27 +00:00
|
|
|
//
|
2021-11-29 15:41:49 +00:00
|
|
|
// Also see https://github.com/weidai11/cryptopp/issues/683,
|
|
|
|
// https://github.com/weidai11/cryptopp/issues/1010 and
|
|
|
|
// https://github.com/weidai11/cryptopp/issues/1088.
|
2021-03-17 16:17:27 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#ifndef CRYPTOPP_IMPORTS
|
|
|
|
|
|
|
|
#include "strciphr.h"
|
|
|
|
|
2018-07-06 05:22:38 +00:00
|
|
|
// Squash MS LNK4221 and libtool warnings
|
|
|
|
#ifndef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
|
|
|
|
extern const char STRCIPHER_FNAME[] = __FILE__;
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
void AdditiveCipherTemplate<S>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
|
|
|
|
{
|
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
|
|
|
policy.CipherSetKey(params, key, length);
|
|
|
|
m_leftOver = 0;
|
|
|
|
unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
|
|
|
|
m_buffer.New(bufferByteSize);
|
|
|
|
|
|
|
|
if (this->IsResynchronizable())
|
|
|
|
{
|
|
|
|
size_t ivLength;
|
|
|
|
const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
|
|
|
|
policy.CipherResynchronize(m_buffer, iv, ivLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
void AdditiveCipherTemplate<S>::GenerateBlock(byte *outString, size_t length)
|
|
|
|
{
|
|
|
|
if (m_leftOver > 0)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
const size_t len = STDMIN(m_leftOver, length);
|
2020-04-13 06:47:38 +00:00
|
|
|
std::memcpy(outString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
|
2018-07-10 09:00:02 +00:00
|
|
|
|
|
|
|
length -= len; m_leftOver -= len;
|
|
|
|
outString = PtrAdd(outString, len);
|
|
|
|
if (!length) {return;}
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
2017-09-05 20:28:00 +00:00
|
|
|
unsigned int bytesPerIteration = policy.GetBytesPerIteration();
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
if (length >= bytesPerIteration)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
const size_t iterations = length / bytesPerIteration;
|
2017-09-05 20:28:00 +00:00
|
|
|
policy.WriteKeystream(outString, iterations);
|
2015-11-05 06:59:46 +00:00
|
|
|
length -= iterations * bytesPerIteration;
|
2018-07-10 09:00:02 +00:00
|
|
|
outString = PtrAdd(outString, iterations * bytesPerIteration);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
size_t bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
|
|
|
|
size_t bufferIterations = bufferByteSize / bytesPerIteration;
|
|
|
|
|
2018-07-12 13:48:46 +00:00
|
|
|
policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
|
2020-04-13 06:47:38 +00:00
|
|
|
std::memcpy(outString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
|
2015-11-05 06:59:46 +00:00
|
|
|
m_leftOver = bufferByteSize - length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 15:41:49 +00:00
|
|
|
// TODO: Figure out what is happening in ProcessData. The issue surfaced
|
|
|
|
// for CFB_CipherTemplate<BASE>::ProcessData when we cut-in Cryptogams
|
|
|
|
// AES ARMv7 asm. Then again in AdditiveCipherTemplate<S>::ProcessData
|
|
|
|
// for CTR mode with HIGHT, which is a 64-bit block cipher. In both cases,
|
|
|
|
// inString == outString leads to incorrect results. We think it relates
|
|
|
|
// to aliasing violations because inString == outString.
|
|
|
|
//
|
|
|
|
// Also see https://github.com/weidai11/cryptopp/issues/683,
|
|
|
|
// https://github.com/weidai11/cryptopp/issues/1010 and
|
|
|
|
// https://github.com/weidai11/cryptopp/issues/1088.
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class S>
|
|
|
|
void AdditiveCipherTemplate<S>::ProcessData(byte *outString, const byte *inString, size_t length)
|
|
|
|
{
|
2021-03-17 02:01:24 +00:00
|
|
|
CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
|
|
|
|
CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
|
|
|
|
|
2021-11-29 00:57:28 +00:00
|
|
|
// If this assert fires, then outString == inString. You could experience a
|
|
|
|
// performance hit. Also see https://github.com/weidai11/cryptopp/issues/1088'
|
|
|
|
CRYPTOPP_ASSERT(outString != inString);
|
|
|
|
|
2021-03-17 02:01:24 +00:00
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
|
|
|
unsigned int bytesPerIteration = policy.GetBytesPerIteration();
|
|
|
|
|
2021-03-17 16:17:27 +00:00
|
|
|
byte* savedOutString = outString;
|
|
|
|
size_t savedLength = length;
|
|
|
|
bool copyOut = false;
|
|
|
|
|
|
|
|
if (inString == outString)
|
|
|
|
{
|
2021-04-10 03:37:29 +00:00
|
|
|
// No need to copy inString to outString.
|
|
|
|
// Just allocate the space.
|
|
|
|
m_tempOutString.New(length);
|
2021-03-17 16:17:27 +00:00
|
|
|
m_tempOutString.SetMark(0);
|
|
|
|
outString = m_tempOutString.BytePtr();
|
|
|
|
copyOut = true;
|
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if (m_leftOver > 0)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
const size_t len = STDMIN(m_leftOver, length);
|
2020-04-13 06:47:38 +00:00
|
|
|
xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2018-07-10 09:00:02 +00:00
|
|
|
inString = PtrAdd(inString, len);
|
|
|
|
outString = PtrAdd(outString, len);
|
2021-03-17 02:01:24 +00:00
|
|
|
length -= len; m_leftOver -= len;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 16:17:27 +00:00
|
|
|
if (!length) {
|
|
|
|
if (copyOut)
|
2021-03-18 03:19:46 +00:00
|
|
|
std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
|
2021-03-17 16:17:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-17 02:01:24 +00:00
|
|
|
|
|
|
|
const unsigned int alignment = policy.GetAlignment();
|
|
|
|
const bool inAligned = IsAlignedOn(inString, alignment);
|
|
|
|
const bool outAligned = IsAlignedOn(outString, alignment);
|
2021-03-21 12:06:10 +00:00
|
|
|
CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
if (policy.CanOperateKeystream() && length >= bytesPerIteration)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
const size_t iterations = length / bytesPerIteration;
|
2021-03-17 02:01:24 +00:00
|
|
|
KeystreamOperationFlags flags = static_cast<KeystreamOperationFlags>(
|
|
|
|
(inAligned ? EnumToInt(INPUT_ALIGNED) : 0) | (outAligned ? EnumToInt(OUTPUT_ALIGNED) : 0));
|
|
|
|
KeystreamOperation operation = KeystreamOperation(flags);
|
2015-11-05 06:59:46 +00:00
|
|
|
policy.OperateKeystream(operation, outString, inString, iterations);
|
|
|
|
|
2018-07-10 09:00:02 +00:00
|
|
|
inString = PtrAdd(inString, iterations * bytesPerIteration);
|
|
|
|
outString = PtrAdd(outString, iterations * bytesPerIteration);
|
2015-11-05 06:59:46 +00:00
|
|
|
length -= iterations * bytesPerIteration;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bufferByteSize = m_buffer.size();
|
|
|
|
size_t bufferIterations = bufferByteSize / bytesPerIteration;
|
|
|
|
|
|
|
|
while (length >= bufferByteSize)
|
|
|
|
{
|
|
|
|
policy.WriteKeystream(m_buffer, bufferIterations);
|
|
|
|
xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
|
2018-07-10 09:00:02 +00:00
|
|
|
|
|
|
|
inString = PtrAdd(inString, bufferByteSize);
|
|
|
|
outString = PtrAdd(outString, bufferByteSize);
|
2021-03-17 02:01:24 +00:00
|
|
|
length -= bufferByteSize;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
|
|
|
|
bufferIterations = bufferByteSize / bytesPerIteration;
|
|
|
|
|
2018-07-12 13:48:46 +00:00
|
|
|
policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
|
|
|
|
xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
|
2021-03-17 02:01:24 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
m_leftOver = bufferByteSize - length;
|
|
|
|
}
|
2021-03-17 16:17:27 +00:00
|
|
|
|
|
|
|
if (copyOut)
|
|
|
|
std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
void AdditiveCipherTemplate<S>::Resynchronize(const byte *iv, int length)
|
|
|
|
{
|
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
|
|
|
m_leftOver = 0;
|
|
|
|
m_buffer.New(GetBufferByteSize(policy));
|
|
|
|
policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BASE>
|
|
|
|
void AdditiveCipherTemplate<BASE>::Seek(lword position)
|
|
|
|
{
|
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
2021-03-17 02:01:24 +00:00
|
|
|
unsigned int bytesPerIteration = policy.GetBytesPerIteration();
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
policy.SeekToIteration(position / bytesPerIteration);
|
|
|
|
position %= bytesPerIteration;
|
|
|
|
|
|
|
|
if (position > 0)
|
|
|
|
{
|
2018-07-12 13:48:46 +00:00
|
|
|
policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
|
2021-03-17 02:01:24 +00:00
|
|
|
m_leftOver = bytesPerIteration - static_cast<unsigned int>(position);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_leftOver = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BASE>
|
|
|
|
void CFB_CipherTemplate<BASE>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
|
|
|
|
{
|
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
|
|
|
policy.CipherSetKey(params, key, length);
|
|
|
|
|
|
|
|
if (this->IsResynchronizable())
|
|
|
|
{
|
|
|
|
size_t ivLength;
|
|
|
|
const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
|
|
|
|
policy.CipherResynchronize(iv, ivLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_leftOver = policy.GetBytesPerIteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BASE>
|
|
|
|
void CFB_CipherTemplate<BASE>::Resynchronize(const byte *iv, int length)
|
|
|
|
{
|
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
|
|
|
policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
|
|
|
|
m_leftOver = policy.GetBytesPerIteration();
|
|
|
|
}
|
|
|
|
|
2021-11-29 15:41:49 +00:00
|
|
|
// TODO: Figure out what is happening in ProcessData. The issue surfaced
|
|
|
|
// for CFB_CipherTemplate<BASE>::ProcessData when we cut-in Cryptogams
|
|
|
|
// AES ARMv7 asm. Then again in AdditiveCipherTemplate<S>::ProcessData
|
|
|
|
// for CTR mode with HIGHT, which is a 64-bit block cipher. In both cases,
|
|
|
|
// inString == outString leads to incorrect results. We think it relates
|
|
|
|
// to aliasing violations because inString == outString.
|
|
|
|
//
|
|
|
|
// Also see https://github.com/weidai11/cryptopp/issues/683,
|
|
|
|
// https://github.com/weidai11/cryptopp/issues/1010 and
|
|
|
|
// https://github.com/weidai11/cryptopp/issues/1088.
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class BASE>
|
|
|
|
void CFB_CipherTemplate<BASE>::ProcessData(byte *outString, const byte *inString, size_t length)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2021-11-29 00:57:28 +00:00
|
|
|
// If this assert fires, then outString == inString. You could experience a
|
|
|
|
// performance hit. Also see https://github.com/weidai11/cryptopp/issues/1088'
|
|
|
|
CRYPTOPP_ASSERT(outString != inString);
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
PolicyInterface &policy = this->AccessPolicy();
|
2021-03-17 02:01:24 +00:00
|
|
|
unsigned int bytesPerIteration = policy.GetBytesPerIteration();
|
2015-11-05 06:59:46 +00:00
|
|
|
byte *reg = policy.GetRegisterBegin();
|
|
|
|
|
2021-03-17 16:17:27 +00:00
|
|
|
byte* savedOutString = outString;
|
|
|
|
size_t savedLength = length;
|
|
|
|
bool copyOut = false;
|
|
|
|
|
|
|
|
if (inString == outString)
|
|
|
|
{
|
2021-04-10 03:37:29 +00:00
|
|
|
// No need to copy inString to outString.
|
|
|
|
// Just allocate the space.
|
|
|
|
m_tempOutString.New(length);
|
2021-03-17 16:17:27 +00:00
|
|
|
m_tempOutString.SetMark(0);
|
|
|
|
outString = m_tempOutString.BytePtr();
|
|
|
|
copyOut = true;
|
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if (m_leftOver)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
const size_t len = STDMIN(m_leftOver, length);
|
|
|
|
CombineMessageAndShiftRegister(outString, PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2018-07-10 09:00:02 +00:00
|
|
|
inString = PtrAdd(inString, len);
|
|
|
|
outString = PtrAdd(outString, len);
|
2021-03-17 02:01:24 +00:00
|
|
|
m_leftOver -= len; length -= len;
|
2018-07-10 09:00:02 +00:00
|
|
|
}
|
2017-09-05 20:28:00 +00:00
|
|
|
|
2021-03-17 16:17:27 +00:00
|
|
|
if (!length) {
|
|
|
|
if (copyOut)
|
2021-03-18 03:19:46 +00:00
|
|
|
std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
|
2021-03-17 16:17:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-14 02:16:59 +00:00
|
|
|
|
|
|
|
const unsigned int alignment = policy.GetAlignment();
|
2021-03-17 02:01:24 +00:00
|
|
|
const bool inAligned = IsAlignedOn(inString, alignment);
|
|
|
|
const bool outAligned = IsAlignedOn(outString, alignment);
|
2021-03-21 12:06:10 +00:00
|
|
|
CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
|
2020-04-13 06:47:38 +00:00
|
|
|
|
|
|
|
if (policy.CanIterate() && length >= bytesPerIteration && outAligned)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2020-04-13 06:47:38 +00:00
|
|
|
CipherDir cipherDir = GetCipherDir(*this);
|
2021-03-17 16:17:27 +00:00
|
|
|
policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
|
|
|
|
|
2018-10-14 02:16:59 +00:00
|
|
|
const size_t remainder = length % bytesPerIteration;
|
|
|
|
inString = PtrAdd(inString, length - remainder);
|
|
|
|
outString = PtrAdd(outString, length - remainder);
|
|
|
|
length = remainder;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (length >= bytesPerIteration)
|
|
|
|
{
|
|
|
|
policy.TransformRegister();
|
|
|
|
CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
|
2021-03-17 02:01:24 +00:00
|
|
|
|
2018-07-10 09:00:02 +00:00
|
|
|
inString = PtrAdd(inString, bytesPerIteration);
|
|
|
|
outString = PtrAdd(outString, bytesPerIteration);
|
2021-03-17 02:01:24 +00:00
|
|
|
length -= bytesPerIteration;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
policy.TransformRegister();
|
|
|
|
CombineMessageAndShiftRegister(outString, reg, inString, length);
|
|
|
|
m_leftOver = bytesPerIteration - length;
|
|
|
|
}
|
2021-03-17 16:17:27 +00:00
|
|
|
|
|
|
|
if (copyOut)
|
|
|
|
std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BASE>
|
|
|
|
void CFB_EncryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
|
|
|
|
{
|
|
|
|
xorbuf(reg, message, length);
|
2020-04-13 06:47:38 +00:00
|
|
|
std::memcpy(output, reg, length);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BASE>
|
|
|
|
void CFB_DecryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
|
|
|
|
{
|
2018-07-10 09:00:02 +00:00
|
|
|
for (size_t i=0; i<length; i++)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
byte b = message[i];
|
|
|
|
output[i] = reg[i] ^ b;
|
|
|
|
reg[i] = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|