2017-01-27 12:05:45 +00:00
|
|
|
// seal.cpp - originally written and placed in the public domain by Wei Dai
|
2015-11-05 06:59:46 +00:00
|
|
|
// updated to SEAL 3.0 by Leonard Janke
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "seal.h"
|
2017-05-26 05:51:44 +00:00
|
|
|
#include "cpu.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
#include "sha.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "secblock.h"
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2016-10-18 02:00:31 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
|
2015-11-05 06:59:46 +00:00
|
|
|
void SEAL_TestInstantiations()
|
|
|
|
{
|
|
|
|
SEAL<>::Encryption x;
|
|
|
|
}
|
2015-11-18 20:32:28 +00:00
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
struct SEAL_Gamma
|
|
|
|
{
|
|
|
|
SEAL_Gamma(const byte *key)
|
|
|
|
: H(5), Z(5), D(16), lastIndex(0xffffffff)
|
|
|
|
{
|
|
|
|
GetUserKey(BIG_ENDIAN_ORDER, H.begin(), 5, key, 20);
|
2023-04-15 20:45:02 +00:00
|
|
|
std::memset(D, 0, 64);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
word32 Apply(word32 i);
|
|
|
|
|
|
|
|
SecBlock<word32> H, Z, D;
|
|
|
|
word32 lastIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
word32 SEAL_Gamma::Apply(word32 i)
|
|
|
|
{
|
|
|
|
word32 shaIndex = i/5;
|
|
|
|
if (shaIndex != lastIndex)
|
|
|
|
{
|
2023-04-15 20:45:02 +00:00
|
|
|
std::memcpy(Z, H, 20);
|
2017-08-13 20:05:39 +00:00
|
|
|
D[0] = shaIndex;
|
2017-04-24 09:05:14 +00:00
|
|
|
SHA1::Transform(Z, D);
|
2015-11-05 06:59:46 +00:00
|
|
|
lastIndex = shaIndex;
|
|
|
|
}
|
2017-05-26 05:51:44 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
return Z[i%5];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class B>
|
|
|
|
void SEAL_Policy<B>::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)
|
|
|
|
{
|
|
|
|
CRYPTOPP_UNUSED(length);
|
|
|
|
m_insideCounter = m_outsideCounter = m_startCount = 0;
|
|
|
|
|
|
|
|
unsigned int L = params.GetIntValueWithDefault("NumberOfOutputBitsPerPositionIndex", 32*1024);
|
|
|
|
m_iterationsPerCount = L / 8192;
|
|
|
|
|
|
|
|
SEAL_Gamma gamma(key);
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i=0; i<512; i++)
|
|
|
|
m_T[i] = gamma.Apply(i);
|
|
|
|
|
|
|
|
for (i=0; i<256; i++)
|
|
|
|
m_S[i] = gamma.Apply(0x1000+i);
|
|
|
|
|
|
|
|
m_R.New(4*(L/8192));
|
|
|
|
|
|
|
|
for (i=0; i<m_R.size(); i++)
|
|
|
|
m_R[i] = gamma.Apply(0x2000+i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class B>
|
|
|
|
void SEAL_Policy<B>::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
|
|
|
|
{
|
|
|
|
CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(IV), CRYPTOPP_UNUSED(length);
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(length==4);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
m_outsideCounter = IV ? GetWord<word32>(false, BIG_ENDIAN_ORDER, IV) : 0;
|
|
|
|
m_startCount = m_outsideCounter;
|
|
|
|
m_insideCounter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class B>
|
|
|
|
void SEAL_Policy<B>::SeekToIteration(lword iterationCount)
|
|
|
|
{
|
|
|
|
m_outsideCounter = m_startCount + (unsigned int)(iterationCount / m_iterationsPerCount);
|
|
|
|
m_insideCounter = (unsigned int)(iterationCount % m_iterationsPerCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class B>
|
|
|
|
void SEAL_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
|
|
|
|
{
|
|
|
|
word32 a, b, c, d, n1, n2, n3, n4;
|
|
|
|
unsigned int p, q;
|
|
|
|
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(IsAlignedOn(m_T.begin(),GetAlignmentOf<word32>()));
|
2015-11-05 06:59:46 +00:00
|
|
|
for (size_t iteration = 0; iteration < iterationCount; ++iteration)
|
|
|
|
{
|
2016-01-24 23:27:44 +00:00
|
|
|
#define Ttab(x) *(word32 *)(void*)((byte *)m_T.begin()+x)
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
a = m_outsideCounter ^ m_R[4*m_insideCounter];
|
2017-11-25 07:52:19 +00:00
|
|
|
b = rotrConstant<8>(m_outsideCounter) ^ m_R[4*m_insideCounter+1];
|
|
|
|
c = rotrConstant<16>(m_outsideCounter) ^ m_R[4 * m_insideCounter + 2];
|
|
|
|
d = rotrConstant<24>(m_outsideCounter) ^ m_R[4 * m_insideCounter + 3];
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
for (unsigned int j=0; j<2; j++)
|
|
|
|
{
|
|
|
|
p = a & 0x7fc;
|
|
|
|
b += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
a = rotrConstant<9>(a);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = b & 0x7fc;
|
|
|
|
c += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
b = rotrConstant<9>(b);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = c & 0x7fc;
|
|
|
|
d += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
c = rotrConstant<9>(c);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = d & 0x7fc;
|
|
|
|
a += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
d = rotrConstant<9>(d);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n1 = d, n2 = b, n3 = a, n4 = c;
|
|
|
|
|
|
|
|
p = a & 0x7fc;
|
|
|
|
b += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
a = rotrConstant<9>(a);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = b & 0x7fc;
|
|
|
|
c += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
b = rotrConstant<9>(b);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = c & 0x7fc;
|
|
|
|
d += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
c = rotrConstant<9>(c);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = d & 0x7fc;
|
|
|
|
a += Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
d = rotrConstant<9>(d);
|
2016-09-06 06:51:16 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
// generate 8192 bits
|
|
|
|
for (unsigned int i=0; i<64; i++)
|
|
|
|
{
|
|
|
|
p = a & 0x7fc;
|
2017-11-25 07:52:19 +00:00
|
|
|
a = rotrConstant<9>(a);
|
2015-11-05 06:59:46 +00:00
|
|
|
b += Ttab(p);
|
|
|
|
b ^= a;
|
|
|
|
|
|
|
|
q = b & 0x7fc;
|
2017-11-25 07:52:19 +00:00
|
|
|
b = rotrConstant<9>(b);
|
2015-11-05 06:59:46 +00:00
|
|
|
c ^= Ttab(q);
|
|
|
|
c += b;
|
|
|
|
|
|
|
|
p = (p+c) & 0x7fc;
|
2017-11-25 07:52:19 +00:00
|
|
|
c = rotrConstant<9>(c);
|
2015-11-05 06:59:46 +00:00
|
|
|
d += Ttab(p);
|
|
|
|
d ^= c;
|
|
|
|
|
|
|
|
q = (q+d) & 0x7fc;
|
2017-11-25 07:52:19 +00:00
|
|
|
d = rotrConstant<9>(d);
|
2015-11-05 06:59:46 +00:00
|
|
|
a ^= Ttab(q);
|
|
|
|
a += d;
|
|
|
|
|
|
|
|
p = (p+a) & 0x7fc;
|
|
|
|
b ^= Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
a = rotrConstant<9>(a);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
q = (q+b) & 0x7fc;
|
|
|
|
c += Ttab(q);
|
2017-11-25 07:52:19 +00:00
|
|
|
b = rotrConstant<9>(b);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
p = (p+c) & 0x7fc;
|
|
|
|
d ^= Ttab(p);
|
2017-11-25 07:52:19 +00:00
|
|
|
c = rotrConstant<9>(c);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
q = (q+d) & 0x7fc;
|
2017-11-25 07:52:19 +00:00
|
|
|
d = rotrConstant<9>(d);
|
2015-11-05 06:59:46 +00:00
|
|
|
a += Ttab(q);
|
|
|
|
|
|
|
|
#define SEAL_OUTPUT(x) \
|
|
|
|
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, b + m_S[4*i+0]);\
|
|
|
|
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 1, c ^ m_S[4*i+1]);\
|
|
|
|
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 2, d + m_S[4*i+2]);\
|
|
|
|
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 3, a ^ m_S[4*i+3]);
|
|
|
|
|
|
|
|
CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(SEAL_OUTPUT, 4*4);
|
|
|
|
|
|
|
|
if (i & 1)
|
|
|
|
{
|
|
|
|
a += n3;
|
|
|
|
b += n4;
|
|
|
|
c ^= n3;
|
|
|
|
d ^= n4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a += n1;
|
2016-09-06 06:51:16 +00:00
|
|
|
b += n2;
|
2015-11-05 06:59:46 +00:00
|
|
|
c ^= n1;
|
|
|
|
d ^= n2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++m_insideCounter == m_iterationsPerCount)
|
|
|
|
{
|
|
|
|
++m_outsideCounter;
|
|
|
|
m_insideCounter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a = b = c = d = n1 = n2 = n3 = n4 = 0;
|
|
|
|
p = q = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template class SEAL_Policy<BigEndian>;
|
|
|
|
template class SEAL_Policy<LittleEndian>;
|
|
|
|
|
|
|
|
NAMESPACE_END
|