crypto: encrypt in-place where it suits
This commit is contained in:
parent
9a1a75d623
commit
7f0c7466e4
@ -37,16 +37,16 @@ TEA::TEA (key_t _key):
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
TEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count)
|
||||
TEA::encrypt (uint32_t *restrict data, size_t count)
|
||||
{
|
||||
if (count % 2)
|
||||
throw std::invalid_argument ("TEA requires even data count");
|
||||
|
||||
auto last = src + count;
|
||||
while (src < last) {
|
||||
auto last = data + count;
|
||||
while (data < last) {
|
||||
uint32_t sum = 0;
|
||||
uint32_t v0 = src[0];
|
||||
uint32_t v1 = src[1];
|
||||
uint32_t v0 = data[0];
|
||||
uint32_t v1 = data[1];
|
||||
|
||||
for (unsigned i = 0; i < ITERATIONS; ++i) {
|
||||
sum += MAGIC;
|
||||
@ -54,28 +54,25 @@ TEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count
|
||||
v1 += ((v0 << 4) + m_key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + m_key[3]);
|
||||
}
|
||||
|
||||
dst[0] = v0;
|
||||
dst[1] = v1;
|
||||
|
||||
src += 2;
|
||||
dst += 2;
|
||||
*data++ = v0;
|
||||
*data++ = v1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
TEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count)
|
||||
TEA::decrypt (uint32_t *restrict data, size_t count)
|
||||
{
|
||||
if (count % 2)
|
||||
throw std::invalid_argument ("TEA requires even data count");
|
||||
|
||||
auto last = src + count;
|
||||
auto last = data + count;
|
||||
|
||||
while (src < last) {
|
||||
while (data < last) {
|
||||
uint32_t sum = MAGIC << 5;
|
||||
uint32_t v0 = src[0];
|
||||
uint32_t v1 = src[1];
|
||||
uint32_t v0 = data[0];
|
||||
uint32_t v1 = data[1];
|
||||
|
||||
for (unsigned i = 0; i < ITERATIONS; ++i) {
|
||||
v1 -= ((v0 << 4) + m_key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + m_key[3]);
|
||||
@ -83,10 +80,7 @@ TEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count
|
||||
sum -= MAGIC;
|
||||
}
|
||||
|
||||
dst[0] = v0;
|
||||
dst[1] = v1;
|
||||
|
||||
src += 2;
|
||||
dst += 2;
|
||||
*data++ = v0;
|
||||
*data++ = v1;
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +29,8 @@ namespace util { namespace crypto {
|
||||
|
||||
TEA (key_t);
|
||||
|
||||
void encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count);
|
||||
void decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count);
|
||||
void encrypt (uint32_t *restrict data, size_t count);
|
||||
void decrypt (uint32_t *restrict data, size_t count);
|
||||
|
||||
private:
|
||||
key_t m_key;
|
||||
|
@ -34,17 +34,17 @@ XTEA::XTEA (key_t _key):
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
XTEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count)
|
||||
XTEA::encrypt (uint32_t *restrict data, size_t count)
|
||||
{
|
||||
if (count % 2)
|
||||
throw std::invalid_argument ("XTEA requires even data count");
|
||||
|
||||
auto last = src + count;
|
||||
auto last = data + count;
|
||||
|
||||
while (src < last) {
|
||||
while (data < last) {
|
||||
uint32_t sum = 0;
|
||||
uint32_t v0 = src[0];
|
||||
uint32_t v1 = src[1];
|
||||
uint32_t v0 = data[0];
|
||||
uint32_t v1 = data[1];
|
||||
|
||||
for (unsigned i = 0; i < ITERATIONS; ++i) {
|
||||
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + m_key[sum & 3]);
|
||||
@ -52,28 +52,25 @@ XTEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t coun
|
||||
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + m_key[(sum >> 11) & 3]);
|
||||
}
|
||||
|
||||
dst[0] = v0;
|
||||
dst[1] = v1;
|
||||
|
||||
src += 2;
|
||||
dst += 2;
|
||||
*data++ = v0;
|
||||
*data++ = v1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
XTEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count)
|
||||
XTEA::decrypt (uint32_t *restrict data, size_t count)
|
||||
{
|
||||
if (count % 2)
|
||||
throw std::invalid_argument ("XTEA requires even data count");
|
||||
|
||||
auto last = src + count;
|
||||
auto last = data + count;
|
||||
|
||||
while (src < last) {
|
||||
while (data < last) {
|
||||
uint32_t sum = ITERATIONS * MAGIC;
|
||||
uint32_t v0 = src[0];
|
||||
uint32_t v1 = src[1];
|
||||
uint32_t v0 = data[0];
|
||||
uint32_t v1 = data[1];
|
||||
|
||||
for (unsigned i = 0; i < ITERATIONS; ++i) {
|
||||
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + m_key[(sum >> 11) & 3]);
|
||||
@ -81,9 +78,7 @@ XTEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t coun
|
||||
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + m_key[sum & 3]);
|
||||
}
|
||||
|
||||
dst[0] = v0;
|
||||
dst[1] = v1;
|
||||
|
||||
src += 2;
|
||||
*data++ = v0;
|
||||
*data++ = v1;
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +29,8 @@ namespace util { namespace crypto {
|
||||
|
||||
XTEA (key_t);
|
||||
|
||||
void encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count);
|
||||
void decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count);
|
||||
void encrypt (uint32_t *restrict data, size_t count);
|
||||
void decrypt (uint32_t *restrict data, size_t count);
|
||||
|
||||
private:
|
||||
key_t m_key;
|
||||
|
@ -7,7 +7,7 @@ int
|
||||
main ()
|
||||
{
|
||||
// test vectors from 'TeaCrypt', by Logan J. Drews.
|
||||
struct {
|
||||
static const struct {
|
||||
std::array<uint32_t,4> key;
|
||||
std::array<uint32_t,2> dec;
|
||||
std::array<uint32_t,2> enc;
|
||||
@ -44,10 +44,11 @@ main ()
|
||||
const auto &t = TESTS[i];
|
||||
util::crypto::TEA gen (t.key);
|
||||
|
||||
std::array<uint32_t,2> enc, dec;
|
||||
std::array<uint32_t,2> enc (t.dec);
|
||||
gen.encrypt (enc.data (), enc.size ());
|
||||
|
||||
gen.encrypt (enc.data (), t.dec.data (), t.dec.size ());
|
||||
gen.decrypt (dec.data (), t.enc.data (), t.enc.size ());
|
||||
std::array<uint32_t,2> dec (t.enc);
|
||||
gen.decrypt (dec.data (), dec.size ());
|
||||
|
||||
{
|
||||
std::ostringstream os;
|
||||
|
@ -6,7 +6,7 @@ int
|
||||
main ()
|
||||
{
|
||||
// test vectors from 'TeaCrypt', by Logan J. Drews.
|
||||
struct {
|
||||
static const struct {
|
||||
std::array<uint32_t,4> key;
|
||||
std::array<uint32_t,2> dec;
|
||||
std::array<uint32_t,2> enc;
|
||||
@ -43,20 +43,21 @@ main ()
|
||||
const auto &t = TESTS[i];
|
||||
util::crypto::XTEA gen (t.key);
|
||||
|
||||
std::array<uint32_t,2> enc, dec;
|
||||
std::array<uint32_t,2> enc (t.dec);
|
||||
gen.encrypt (enc.data (), enc.size ());
|
||||
|
||||
gen.encrypt (enc.data (), t.dec.data (), t.dec.size ());
|
||||
gen.decrypt (dec.data (), t.enc.data (), t.enc.size ());
|
||||
std::array<uint32_t,2> dec (t.enc);
|
||||
gen.decrypt (dec.data (), dec.size ());
|
||||
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << "TEA_enc " << i;
|
||||
os << "XTEA_enc " << i;
|
||||
tap.expect (enc == t.enc, os.str ());
|
||||
}
|
||||
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << "TEA_dec " << i;
|
||||
os << "XTEA_dec " << i;
|
||||
tap.expect (dec == t.dec, os.str ());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user