aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/block/xtea
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-10 03:41:59 +0000
committerlloyd <[email protected]>2014-01-10 03:41:59 +0000
commit6894dca64c04936d07048c0e8cbf7e25858548c3 (patch)
tree5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/block/xtea
parent9efa3be92442afb3d0b69890a36c7f122df18eda (diff)
Move lib into src
Diffstat (limited to 'src/lib/block/xtea')
-rw-r--r--src/lib/block/xtea/info.txt1
-rw-r--r--src/lib/block/xtea/xtea.cpp146
-rw-r--r--src/lib/block/xtea/xtea.h40
3 files changed, 187 insertions, 0 deletions
diff --git a/src/lib/block/xtea/info.txt b/src/lib/block/xtea/info.txt
new file mode 100644
index 000000000..e2cc8ac34
--- /dev/null
+++ b/src/lib/block/xtea/info.txt
@@ -0,0 +1 @@
+define XTEA 20131128
diff --git a/src/lib/block/xtea/xtea.cpp b/src/lib/block/xtea/xtea.cpp
new file mode 100644
index 000000000..165a6ea6a
--- /dev/null
+++ b/src/lib/block/xtea/xtea.cpp
@@ -0,0 +1,146 @@
+/*
+* XTEA
+* (C) 1999-2009 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/xtea.h>
+#include <botan/loadstor.h>
+
+namespace Botan {
+
+namespace {
+
+void xtea_encrypt_4(const byte in[32], byte out[32], const u32bit EK[64])
+ {
+ u32bit L0, R0, L1, R1, L2, R2, L3, R3;
+ load_be(in, L0, R0, L1, R1, L2, R2, L3, R3);
+
+ for(size_t i = 0; i != 32; ++i)
+ {
+ L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ EK[2*i];
+ L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ EK[2*i];
+ L2 += (((R2 << 4) ^ (R2 >> 5)) + R2) ^ EK[2*i];
+ L3 += (((R3 << 4) ^ (R3 >> 5)) + R3) ^ EK[2*i];
+
+ R0 += (((L0 << 4) ^ (L0 >> 5)) + L0) ^ EK[2*i+1];
+ R1 += (((L1 << 4) ^ (L1 >> 5)) + L1) ^ EK[2*i+1];
+ R2 += (((L2 << 4) ^ (L2 >> 5)) + L2) ^ EK[2*i+1];
+ R3 += (((L3 << 4) ^ (L3 >> 5)) + L3) ^ EK[2*i+1];
+ }
+
+ store_be(out, L0, R0, L1, R1, L2, R2, L3, R3);
+ }
+
+void xtea_decrypt_4(const byte in[32], byte out[32], const u32bit EK[64])
+ {
+ u32bit L0, R0, L1, R1, L2, R2, L3, R3;
+ load_be(in, L0, R0, L1, R1, L2, R2, L3, R3);
+
+ for(size_t i = 0; i != 32; ++i)
+ {
+ R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ EK[63 - 2*i];
+ R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ EK[63 - 2*i];
+ R2 -= (((L2 << 4) ^ (L2 >> 5)) + L2) ^ EK[63 - 2*i];
+ R3 -= (((L3 << 4) ^ (L3 >> 5)) + L3) ^ EK[63 - 2*i];
+
+ L0 -= (((R0 << 4) ^ (R0 >> 5)) + R0) ^ EK[62 - 2*i];
+ L1 -= (((R1 << 4) ^ (R1 >> 5)) + R1) ^ EK[62 - 2*i];
+ L2 -= (((R2 << 4) ^ (R2 >> 5)) + R2) ^ EK[62 - 2*i];
+ L3 -= (((R3 << 4) ^ (R3 >> 5)) + R3) ^ EK[62 - 2*i];
+ }
+
+ store_be(out, L0, R0, L1, R1, L2, R2, L3, R3);
+ }
+
+}
+
+/*
+* XTEA Encryption
+*/
+void XTEA::encrypt_n(const byte in[], byte out[], size_t blocks) const
+ {
+ while(blocks >= 4)
+ {
+ xtea_encrypt_4(in, out, &(this->EK[0]));
+ in += 4 * BLOCK_SIZE;
+ out += 4 * BLOCK_SIZE;
+ blocks -= 4;
+ }
+
+ for(size_t i = 0; i != blocks; ++i)
+ {
+ u32bit L = load_be<u32bit>(in, 0);
+ u32bit R = load_be<u32bit>(in, 1);
+
+ for(size_t j = 0; j != 32; ++j)
+ {
+ L += (((R << 4) ^ (R >> 5)) + R) ^ EK[2*j];
+ R += (((L << 4) ^ (L >> 5)) + L) ^ EK[2*j+1];
+ }
+
+ store_be(out, L, R);
+
+ in += BLOCK_SIZE;
+ out += BLOCK_SIZE;
+ }
+ }
+
+/*
+* XTEA Decryption
+*/
+void XTEA::decrypt_n(const byte in[], byte out[], size_t blocks) const
+ {
+ while(blocks >= 4)
+ {
+ xtea_decrypt_4(in, out, &(this->EK[0]));
+ in += 4 * BLOCK_SIZE;
+ out += 4 * BLOCK_SIZE;
+ blocks -= 4;
+ }
+
+ for(size_t i = 0; i != blocks; ++i)
+ {
+ u32bit L = load_be<u32bit>(in, 0);
+ u32bit R = load_be<u32bit>(in, 1);
+
+ for(size_t j = 0; j != 32; ++j)
+ {
+ R -= (((L << 4) ^ (L >> 5)) + L) ^ EK[63 - 2*j];
+ L -= (((R << 4) ^ (R >> 5)) + R) ^ EK[62 - 2*j];
+ }
+
+ store_be(out, L, R);
+
+ in += BLOCK_SIZE;
+ out += BLOCK_SIZE;
+ }
+ }
+
+/*
+* XTEA Key Schedule
+*/
+void XTEA::key_schedule(const byte key[], size_t)
+ {
+ EK.resize(64);
+
+ secure_vector<u32bit> UK(4);
+ for(size_t i = 0; i != 4; ++i)
+ UK[i] = load_be<u32bit>(key, i);
+
+ u32bit D = 0;
+ for(size_t i = 0; i != 64; i += 2)
+ {
+ EK[i ] = D + UK[D % 4];
+ D += 0x9E3779B9;
+ EK[i+1] = D + UK[(D >> 11) % 4];
+ }
+ }
+
+void XTEA::clear()
+ {
+ zap(EK);
+ }
+
+}
diff --git a/src/lib/block/xtea/xtea.h b/src/lib/block/xtea/xtea.h
new file mode 100644
index 000000000..42acc35a5
--- /dev/null
+++ b/src/lib/block/xtea/xtea.h
@@ -0,0 +1,40 @@
+/*
+* XTEA
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_XTEA_H__
+#define BOTAN_XTEA_H__
+
+#include <botan/block_cipher.h>
+
+namespace Botan {
+
+/**
+* XTEA
+*/
+class BOTAN_DLL XTEA : public Block_Cipher_Fixed_Params<8, 16>
+ {
+ public:
+ void encrypt_n(const byte in[], byte out[], size_t blocks) const;
+ void decrypt_n(const byte in[], byte out[], size_t blocks) const;
+
+ void clear();
+ std::string name() const { return "XTEA"; }
+ BlockCipher* clone() const { return new XTEA; }
+ protected:
+ /**
+ * @return const reference to the key schedule
+ */
+ const secure_vector<u32bit>& get_EK() const { return EK; }
+
+ private:
+ void key_schedule(const byte[], size_t);
+ secure_vector<u32bit> EK;
+ };
+
+}
+
+#endif