2019-10-12 11:14:38 +00:00
|
|
|
// xts.cpp - written and placed in the public domain by Jeffrey Walton
|
|
|
|
|
2019-10-14 03:13:08 +00:00
|
|
|
// Aarch32, Aarch64, Altivec and X86_64 include SIMD as part of the
|
|
|
|
// base architecture. We can use the SIMD code below without an
|
|
|
|
// architecture option. No runtime tests are required. Unfortunately,
|
|
|
|
// we can't use it on Altivec because an architecture switch is required.
|
|
|
|
// The updated XorBuffer gains 0.3 to 1.5 cpb on the architectures for
|
|
|
|
// 16-byte block sizes.
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "xts.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "modes.h"
|
2019-10-13 20:17:37 +00:00
|
|
|
#include "cpu.h"
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
#if defined(CRYPTOPP_DEBUG)
|
|
|
|
# include "aes.h"
|
|
|
|
# include "threefish.h"
|
|
|
|
#endif
|
|
|
|
|
2019-10-13 05:51:22 +00:00
|
|
|
// 0.3 to 0.4 cpb profit
|
2019-10-13 06:08:58 +00:00
|
|
|
#if defined(__SSE2__) || defined(_M_X64)
|
2019-12-29 20:42:00 +00:00
|
|
|
# include <emmintrin.h>
|
2019-10-19 17:23:01 +00:00
|
|
|
// Clang intrinsic casts
|
|
|
|
# define M128_CAST(x) ((__m128i *)(void *)(x))
|
|
|
|
# define CONST_M128_CAST(x) ((const __m128i *)(const void *)(x))
|
2019-10-13 05:51:22 +00:00
|
|
|
#endif
|
|
|
|
|
2019-10-17 15:14:43 +00:00
|
|
|
|
|
|
|
#if defined(__aarch32__) || defined(__aarch64__) || defined(_M_ARM64)
|
|
|
|
# if (CRYPTOPP_ARM_NEON_HEADER)
|
|
|
|
# include <arm_neon.h>
|
|
|
|
# endif
|
2019-10-13 05:51:22 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-11 16:37:03 +00:00
|
|
|
#if defined(__ALTIVEC__)
|
|
|
|
# include "ppc_simd.h"
|
|
|
|
#endif
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
ANONYMOUS_NAMESPACE_BEGIN
|
|
|
|
|
2019-10-13 05:51:22 +00:00
|
|
|
using namespace CryptoPP;
|
|
|
|
|
2019-10-14 03:13:08 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
|
|
|
|
|
|
|
|
using CryptoPP::AES;
|
|
|
|
using CryptoPP::XTS_Mode;
|
|
|
|
using CryptoPP::Threefish512;
|
|
|
|
|
|
|
|
void Modes_TestInstantiations()
|
|
|
|
{
|
|
|
|
XTS_Mode<AES>::Encryption m0;
|
|
|
|
XTS_Mode<AES>::Decryption m1;
|
|
|
|
XTS_Mode<AES>::Encryption m2;
|
|
|
|
XTS_Mode<AES>::Decryption m3;
|
|
|
|
|
|
|
|
#if CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS
|
|
|
|
XTS_Mode<Threefish512>::Encryption m4;
|
|
|
|
XTS_Mode<Threefish512>::Decryption m5;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // CRYPTOPP_DEBUG
|
|
|
|
|
2019-10-13 05:51:22 +00:00
|
|
|
inline void XorBuffer(byte *output, const byte *input, const byte *mask, size_t count)
|
|
|
|
{
|
2019-10-13 07:05:18 +00:00
|
|
|
CRYPTOPP_ASSERT(count >= 16 && (count % 16 == 0));
|
2019-10-13 05:51:22 +00:00
|
|
|
|
2019-10-19 17:23:01 +00:00
|
|
|
#if defined(CRYPTOPP_DISABLE_ASM)
|
2020-04-11 15:40:37 +00:00
|
|
|
xorbuf(output, input, mask, count);
|
2019-10-19 17:23:01 +00:00
|
|
|
|
|
|
|
#elif defined(__SSE2__) || defined(_M_X64)
|
2019-10-13 05:51:22 +00:00
|
|
|
for (size_t i=0; i<count; i+=16)
|
2019-10-13 20:17:37 +00:00
|
|
|
_mm_storeu_si128(M128_CAST(output+i),
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(CONST_M128_CAST(input+i)),
|
|
|
|
_mm_loadu_si128(CONST_M128_CAST(mask+i))));
|
2019-10-13 05:51:22 +00:00
|
|
|
|
|
|
|
#elif defined(__aarch32__) || defined(__aarch64__) || defined(_M_ARM64)
|
|
|
|
for (size_t i=0; i<count; i+=16)
|
|
|
|
vst1q_u8(output+i, veorq_u8(vld1q_u8(input+i), vld1q_u8(mask+i)));
|
|
|
|
|
2020-04-11 16:37:03 +00:00
|
|
|
#elif defined(__ALTIVEC__)
|
|
|
|
for (size_t i=0; i<count; i+=16)
|
|
|
|
VecStore(VecXor(VecLoad(input+i), VecLoad(mask+i)), output+i);
|
|
|
|
|
2019-10-13 05:51:22 +00:00
|
|
|
#else
|
|
|
|
xorbuf(output, input, mask, count);
|
|
|
|
#endif
|
|
|
|
}
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
inline void XorBuffer(byte *buf, const byte *mask, size_t count)
|
|
|
|
{
|
|
|
|
XorBuffer(buf, buf, mask, count);
|
|
|
|
}
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
// Borrowed from CMAC, but little-endian representation
|
2019-10-13 20:17:37 +00:00
|
|
|
inline void GF_Double(byte *out, const byte* in, unsigned int len)
|
2019-10-12 11:14:38 +00:00
|
|
|
{
|
2019-10-12 18:20:52 +00:00
|
|
|
#if defined(_M_X64) || defined(_M_ARM64) || defined(_LP64) || defined(__LP64__)
|
2019-10-12 11:14:38 +00:00
|
|
|
word64 carry = 0, x;
|
|
|
|
for (size_t i=0, idx=0; i<len/8; ++i, idx+=8)
|
|
|
|
{
|
2019-10-13 20:17:37 +00:00
|
|
|
x = GetWord<word64>(false, LITTLE_ENDIAN_ORDER, in+idx);
|
2019-10-12 11:14:38 +00:00
|
|
|
word64 y = (x >> 63); x = (x << 1) + carry;
|
2019-10-13 20:17:37 +00:00
|
|
|
PutWord<word64>(false, LITTLE_ENDIAN_ORDER, out+idx, x);
|
2019-10-12 11:14:38 +00:00
|
|
|
carry = y;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
word32 carry = 0, x;
|
|
|
|
for (size_t i=0, idx=0; i<len/4; ++i, idx+=4)
|
|
|
|
{
|
2019-10-13 20:17:37 +00:00
|
|
|
x = GetWord<word32>(false, LITTLE_ENDIAN_ORDER, in+idx);
|
2019-10-12 11:14:38 +00:00
|
|
|
word32 y = (x >> 31); x = (x << 1) + carry;
|
2019-10-13 20:17:37 +00:00
|
|
|
PutWord<word32>(false, LITTLE_ENDIAN_ORDER, out+idx, x);
|
2019-10-12 11:14:38 +00:00
|
|
|
carry = y;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS
|
|
|
|
|
|
|
|
CRYPTOPP_ASSERT(IsPowerOf2(len));
|
2019-10-13 05:51:22 +00:00
|
|
|
CRYPTOPP_ASSERT(len >= 16);
|
2019-10-12 11:14:38 +00:00
|
|
|
CRYPTOPP_ASSERT(len <= 128);
|
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
byte* k = out;
|
2019-10-12 11:14:38 +00:00
|
|
|
if (carry)
|
|
|
|
{
|
|
|
|
switch (len)
|
|
|
|
{
|
|
|
|
case 16:
|
|
|
|
{
|
|
|
|
const size_t LEIDX = 16-1;
|
|
|
|
k[LEIDX-15] ^= 0x87;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 32:
|
|
|
|
{
|
|
|
|
// https://crypto.stackexchange.com/q/9815/10496
|
|
|
|
// Polynomial x^256 + x^10 + x^5 + x^2 + 1
|
|
|
|
const size_t LEIDX = 32-1;
|
|
|
|
k[LEIDX-30] ^= 4;
|
|
|
|
k[LEIDX-31] ^= 0x25;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 64:
|
|
|
|
{
|
|
|
|
// https://crypto.stackexchange.com/q/9815/10496
|
|
|
|
// Polynomial x^512 + x^8 + x^5 + x^2 + 1
|
|
|
|
const size_t LEIDX = 64-1;
|
|
|
|
k[LEIDX-62] ^= 1;
|
|
|
|
k[LEIDX-63] ^= 0x25;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 128:
|
|
|
|
{
|
|
|
|
// https://crypto.stackexchange.com/q/9815/10496
|
|
|
|
// Polynomial x^1024 + x^19 + x^6 + x + 1
|
|
|
|
const size_t LEIDX = 128-1;
|
|
|
|
k[LEIDX-125] ^= 8;
|
|
|
|
k[LEIDX-126] ^= 0x00;
|
|
|
|
k[LEIDX-127] ^= 0x43;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
CRYPTOPP_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
CRYPTOPP_ASSERT(len == 16);
|
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
byte* k = out;
|
2019-10-12 11:14:38 +00:00
|
|
|
if (carry)
|
|
|
|
{
|
|
|
|
k[0] ^= 0x87;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif // CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS
|
|
|
|
}
|
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
inline void GF_Double(byte *inout, unsigned int len)
|
|
|
|
{
|
|
|
|
GF_Double(inout, inout, len);
|
|
|
|
}
|
|
|
|
|
2019-10-14 03:13:08 +00:00
|
|
|
ANONYMOUS_NAMESPACE_END
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-14 03:13:08 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-14 03:13:08 +00:00
|
|
|
void XTS_ModeBase::ThrowIfInvalidBlockSize(size_t length)
|
2019-10-12 11:14:38 +00:00
|
|
|
{
|
|
|
|
#if CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS
|
2019-10-14 03:13:08 +00:00
|
|
|
CRYPTOPP_ASSERT(length >= 16 && length <= 128 && IsPowerOf2(length));
|
|
|
|
if (length < 16 || length > 128 || !IsPowerOf2(length))
|
|
|
|
throw InvalidArgument(AlgorithmName() + ": block size of underlying block cipher is not valid");
|
|
|
|
#else
|
|
|
|
CRYPTOPP_ASSERT(length == 16);
|
|
|
|
if (length != 16)
|
|
|
|
throw InvalidArgument(AlgorithmName() + ": block size of underlying block cipher is not 16");
|
2019-10-12 11:14:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:31:39 +00:00
|
|
|
void XTS_ModeBase::ThrowIfInvalidKeyLength(size_t length)
|
|
|
|
{
|
2019-10-14 03:13:08 +00:00
|
|
|
CRYPTOPP_ASSERT(length % 2 == 0);
|
|
|
|
if (!GetBlockCipher().IsValidKeyLength((length+1)/2))
|
2019-10-13 08:31:39 +00:00
|
|
|
throw InvalidKeyLength(AlgorithmName(), length);
|
|
|
|
}
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
void XTS_ModeBase::SetKey(const byte *key, size_t length, const NameValuePairs ¶ms)
|
|
|
|
{
|
2019-10-13 08:31:39 +00:00
|
|
|
ThrowIfInvalidKeyLength(length);
|
2019-10-14 03:13:08 +00:00
|
|
|
ThrowIfInvalidBlockSize(BlockSize());
|
2019-10-12 14:41:55 +00:00
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
const size_t klen = length/2;
|
|
|
|
AccessBlockCipher().SetKey(key+0, klen, params);
|
|
|
|
AccessTweakCipher().SetKey(key+klen, klen, params);
|
|
|
|
|
|
|
|
ResizeBuffers();
|
|
|
|
|
|
|
|
size_t ivLength;
|
|
|
|
const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
|
|
|
|
Resynchronize(iv, (int)ivLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XTS_ModeBase::Resynchronize(const byte *iv, int ivLength)
|
|
|
|
{
|
|
|
|
BlockOrientedCipherModeBase::Resynchronize(iv, ivLength);
|
2019-10-13 20:17:37 +00:00
|
|
|
std::memcpy(m_xregister, m_register, ivLength);
|
|
|
|
GetTweakCipher().ProcessBlock(m_xregister);
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XTS_ModeBase::Resynchronize(word64 sector, ByteOrder order)
|
|
|
|
{
|
|
|
|
SecByteBlock iv(GetTweakCipher().BlockSize());
|
|
|
|
PutWord<word64>(false, order, iv, sector);
|
|
|
|
std::memset(iv+8, 0x00, iv.size()-8);
|
|
|
|
|
2019-10-19 16:01:06 +00:00
|
|
|
BlockOrientedCipherModeBase::Resynchronize(iv, (int)iv.size());
|
2019-10-13 20:17:37 +00:00
|
|
|
std::memcpy(m_xregister, iv, iv.size());
|
|
|
|
GetTweakCipher().ProcessBlock(m_xregister);
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XTS_ModeBase::ResizeBuffers()
|
|
|
|
{
|
|
|
|
BlockOrientedCipherModeBase::ResizeBuffers();
|
2019-10-13 20:17:37 +00:00
|
|
|
m_xworkspace.New(GetBlockCipher().BlockSize()*ParallelBlocks);
|
|
|
|
m_xregister.New(GetBlockCipher().BlockSize()*ParallelBlocks);
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XTS_ModeBase::ProcessData(byte *outString, const byte *inString, size_t length)
|
|
|
|
{
|
2019-10-14 03:13:08 +00:00
|
|
|
// data unit is multiple of 16 bytes
|
|
|
|
CRYPTOPP_ASSERT(length % BlockSize() == 0);
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
const unsigned int blockSize = GetBlockCipher().BlockSize();
|
2019-10-13 20:17:37 +00:00
|
|
|
const size_t parallelSize = blockSize*ParallelBlocks;
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
// encrypt the data unit, optimal size at a time
|
2020-04-11 15:40:37 +00:00
|
|
|
while (length >= parallelSize)
|
2019-10-12 11:14:38 +00:00
|
|
|
{
|
2019-10-14 03:13:08 +00:00
|
|
|
// If this fires the GF_Double'ing below is not in sync
|
|
|
|
CRYPTOPP_ASSERT(ParallelBlocks == 4);
|
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
// m_xregister[0] always points to the next tweak.
|
|
|
|
GF_Double(m_xregister+1*blockSize, m_xregister+0*blockSize, blockSize);
|
|
|
|
GF_Double(m_xregister+2*blockSize, m_xregister+1*blockSize, blockSize);
|
|
|
|
GF_Double(m_xregister+3*blockSize, m_xregister+2*blockSize, blockSize);
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
// merge the tweak into the input block
|
2020-04-11 15:40:37 +00:00
|
|
|
XorBuffer(m_xworkspace, inString, m_xregister, parallelSize);
|
2019-10-13 20:17:37 +00:00
|
|
|
|
|
|
|
// encrypt one block, merge the tweak into the output block
|
2020-04-11 15:40:37 +00:00
|
|
|
GetBlockCipher().AdvancedProcessBlocks(m_xworkspace, m_xregister, outString, parallelSize, BlockTransformation::BT_AllowParallel);
|
2019-10-13 20:17:37 +00:00
|
|
|
|
2019-10-14 03:13:08 +00:00
|
|
|
// m_xregister[0] always points to the next tweak.
|
2020-04-11 15:40:37 +00:00
|
|
|
GF_Double(m_xregister+0, m_xregister+3*blockSize, blockSize);
|
|
|
|
|
|
|
|
inString += parallelSize;
|
|
|
|
outString += parallelSize;
|
|
|
|
length -= parallelSize;
|
2019-10-13 20:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt the data unit, blocksize at a time
|
2020-04-11 15:40:37 +00:00
|
|
|
while (length)
|
2019-10-13 20:17:37 +00:00
|
|
|
{
|
|
|
|
// merge the tweak into the input block
|
2020-04-11 15:40:37 +00:00
|
|
|
XorBuffer(m_xworkspace, inString, m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 14:28:46 +00:00
|
|
|
// encrypt one block
|
2019-10-13 20:17:37 +00:00
|
|
|
GetBlockCipher().ProcessBlock(m_xworkspace);
|
2019-10-13 14:28:46 +00:00
|
|
|
|
|
|
|
// merge the tweak into the output block
|
2020-04-11 15:40:37 +00:00
|
|
|
XorBuffer(outString, m_xworkspace, m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
// Multiply T by alpha
|
2019-10-13 20:17:37 +00:00
|
|
|
GF_Double(m_xregister, blockSize);
|
2020-04-11 15:40:37 +00:00
|
|
|
|
|
|
|
inString += blockSize;
|
|
|
|
outString += blockSize;
|
|
|
|
length -= blockSize;
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t XTS_ModeBase::ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
|
|
|
|
{
|
2019-10-13 08:57:46 +00:00
|
|
|
// need at least a full AES block
|
|
|
|
CRYPTOPP_ASSERT(inLength >= BlockSize());
|
|
|
|
|
|
|
|
if (inLength < BlockSize())
|
|
|
|
throw InvalidArgument("XTS: message is too short for ciphertext stealing");
|
|
|
|
|
2019-10-12 11:14:38 +00:00
|
|
|
if (IsForwardTransformation())
|
|
|
|
return ProcessLastPlainBlock(outString, outLength, inString, inLength);
|
|
|
|
else
|
|
|
|
return ProcessLastCipherBlock(outString, outLength, inString, inLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t XTS_ModeBase::ProcessLastPlainBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
|
|
|
|
{
|
|
|
|
// ensure output buffer is large enough
|
|
|
|
CRYPTOPP_ASSERT(outLength >= inLength);
|
|
|
|
|
|
|
|
const unsigned int blockSize = GetBlockCipher().BlockSize();
|
2019-10-13 20:17:37 +00:00
|
|
|
const size_t blocks = inLength / blockSize;
|
|
|
|
const size_t tail = inLength % blockSize;
|
2019-10-12 20:01:46 +00:00
|
|
|
outLength = inLength;
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
if (tail == 0)
|
|
|
|
{
|
|
|
|
// Allow ProcessData to handle all the full blocks
|
|
|
|
ProcessData(outString, inString, inLength);
|
|
|
|
return inLength;
|
|
|
|
}
|
|
|
|
else if (blocks > 1)
|
|
|
|
{
|
|
|
|
// Allow ProcessData to handle full blocks except one
|
|
|
|
const size_t head = (blocks-1)*blockSize;
|
|
|
|
ProcessData(outString, inString, inLength-head);
|
|
|
|
|
2019-10-12 20:04:11 +00:00
|
|
|
outString += head;
|
2019-10-13 20:17:37 +00:00
|
|
|
inString += head; inLength -= head;
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///// handle the full block /////
|
|
|
|
|
|
|
|
// merge the tweak into the input block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(m_xworkspace, inString, m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 14:42:10 +00:00
|
|
|
// encrypt one block
|
2019-10-13 20:17:37 +00:00
|
|
|
GetBlockCipher().ProcessBlock(m_xworkspace);
|
2019-10-13 14:28:46 +00:00
|
|
|
|
2019-10-13 14:42:10 +00:00
|
|
|
// merge the tweak into the output block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(outString, m_xworkspace, m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
// Multiply T by alpha
|
2019-10-13 20:17:37 +00:00
|
|
|
GF_Double(m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
///// handle final partial block /////
|
|
|
|
|
|
|
|
inString += blockSize;
|
|
|
|
outString += blockSize;
|
|
|
|
const size_t len = inLength-blockSize;
|
|
|
|
|
|
|
|
// copy in the final plaintext bytes
|
2019-10-13 20:17:37 +00:00
|
|
|
std::memcpy(m_xworkspace, inString, len);
|
2019-10-12 11:14:38 +00:00
|
|
|
// and copy out the final ciphertext bytes
|
|
|
|
std::memcpy(outString, outString-blockSize, len);
|
|
|
|
// "steal" ciphertext to complete the block
|
2019-10-13 20:17:37 +00:00
|
|
|
std::memcpy(m_xworkspace+len, outString-blockSize+len, blockSize-len);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
// merge the tweak into the input block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(m_xworkspace, m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 14:42:10 +00:00
|
|
|
// encrypt one block
|
2019-10-13 20:17:37 +00:00
|
|
|
GetBlockCipher().ProcessBlock(m_xworkspace);
|
2019-10-13 14:42:10 +00:00
|
|
|
|
|
|
|
// merge the tweak into the previous output block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(outString-blockSize, m_xworkspace, m_xregister, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-12 20:01:46 +00:00
|
|
|
return outLength;
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t XTS_ModeBase::ProcessLastCipherBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
|
|
|
|
{
|
|
|
|
// ensure output buffer is large enough
|
|
|
|
CRYPTOPP_ASSERT(outLength >= inLength);
|
|
|
|
|
|
|
|
const unsigned int blockSize = GetBlockCipher().BlockSize();
|
2019-10-13 20:17:37 +00:00
|
|
|
const size_t blocks = inLength / blockSize;
|
|
|
|
const size_t tail = inLength % blockSize;
|
2019-10-12 20:01:46 +00:00
|
|
|
outLength = inLength;
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
if (tail == 0)
|
|
|
|
{
|
|
|
|
// Allow ProcessData to handle all the full blocks
|
|
|
|
ProcessData(outString, inString, inLength);
|
|
|
|
return inLength;
|
|
|
|
}
|
|
|
|
else if (blocks > 1)
|
|
|
|
{
|
|
|
|
// Allow ProcessData to handle full blocks except one
|
|
|
|
const size_t head = (blocks-1)*blockSize;
|
|
|
|
ProcessData(outString, inString, inLength-head);
|
|
|
|
|
2019-10-12 20:04:11 +00:00
|
|
|
outString += head;
|
2019-10-13 20:17:37 +00:00
|
|
|
inString += head; inLength -= head;
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 20:17:37 +00:00
|
|
|
#define poly1 (m_xregister+0*blockSize)
|
|
|
|
#define poly2 (m_xregister+1*blockSize)
|
|
|
|
GF_Double(poly2, poly1, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
///// handle final partial block /////
|
|
|
|
|
|
|
|
inString += blockSize;
|
|
|
|
outString += blockSize;
|
|
|
|
const size_t len = inLength-blockSize;
|
|
|
|
|
|
|
|
// merge the tweak into the input block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(m_xworkspace, inString-blockSize, poly2, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 14:28:46 +00:00
|
|
|
// encrypt one block
|
2019-10-13 20:17:37 +00:00
|
|
|
GetBlockCipher().ProcessBlock(m_xworkspace);
|
2019-10-13 14:28:46 +00:00
|
|
|
|
|
|
|
// merge the tweak into the output block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(m_xworkspace, poly2, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
// copy in the final plaintext bytes
|
|
|
|
std::memcpy(outString-blockSize, inString, len);
|
|
|
|
// and copy out the final ciphertext bytes
|
2019-10-13 20:17:37 +00:00
|
|
|
std::memcpy(outString, m_xworkspace, len);
|
2019-10-12 11:14:38 +00:00
|
|
|
// "steal" ciphertext to complete the block
|
2019-10-13 20:17:37 +00:00
|
|
|
std::memcpy(outString-blockSize+len, m_xworkspace+len, blockSize-len);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
|
|
|
///// handle the full previous block /////
|
|
|
|
|
|
|
|
inString -= blockSize;
|
|
|
|
outString -= blockSize;
|
|
|
|
|
2019-10-13 14:42:10 +00:00
|
|
|
// merge the tweak into the input block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(m_xworkspace, outString, poly1, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-13 14:28:46 +00:00
|
|
|
// encrypt one block
|
2019-10-13 20:17:37 +00:00
|
|
|
GetBlockCipher().ProcessBlock(m_xworkspace);
|
2019-10-13 14:28:46 +00:00
|
|
|
|
|
|
|
// merge the tweak into the output block
|
2019-10-13 20:17:37 +00:00
|
|
|
XorBuffer(outString, m_xworkspace, poly1, blockSize);
|
2019-10-12 11:14:38 +00:00
|
|
|
|
2019-10-12 20:01:46 +00:00
|
|
|
return outLength;
|
2019-10-12 11:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|