aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/block/rc5
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/rc5
parent9efa3be92442afb3d0b69890a36c7f122df18eda (diff)
Move lib into src
Diffstat (limited to 'src/lib/block/rc5')
-rw-r--r--src/lib/block/rc5/info.txt1
-rw-r--r--src/lib/block/rc5/rc5.cpp135
-rw-r--r--src/lib/block/rc5/rc5.h42
3 files changed, 178 insertions, 0 deletions
diff --git a/src/lib/block/rc5/info.txt b/src/lib/block/rc5/info.txt
new file mode 100644
index 000000000..2c2c613ba
--- /dev/null
+++ b/src/lib/block/rc5/info.txt
@@ -0,0 +1 @@
+define RC5 20131128
diff --git a/src/lib/block/rc5/rc5.cpp b/src/lib/block/rc5/rc5.cpp
new file mode 100644
index 000000000..f370e6cfb
--- /dev/null
+++ b/src/lib/block/rc5/rc5.cpp
@@ -0,0 +1,135 @@
+/*
+* RC5
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/rc5.h>
+#include <botan/loadstor.h>
+#include <botan/rotate.h>
+#include <botan/parsing.h>
+#include <algorithm>
+
+namespace Botan {
+
+/*
+* RC5 Encryption
+*/
+void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const
+ {
+ for(size_t i = 0; i != blocks; ++i)
+ {
+ u32bit A = load_le<u32bit>(in, 0);
+ u32bit B = load_le<u32bit>(in, 1);
+
+ A += S[0]; B += S[1];
+ for(size_t j = 0; j != rounds; j += 4)
+ {
+ A = rotate_left(A ^ B, B % 32) + S[2*j+2];
+ B = rotate_left(B ^ A, A % 32) + S[2*j+3];
+
+ A = rotate_left(A ^ B, B % 32) + S[2*j+4];
+ B = rotate_left(B ^ A, A % 32) + S[2*j+5];
+
+ A = rotate_left(A ^ B, B % 32) + S[2*j+6];
+ B = rotate_left(B ^ A, A % 32) + S[2*j+7];
+
+ A = rotate_left(A ^ B, B % 32) + S[2*j+8];
+ B = rotate_left(B ^ A, A % 32) + S[2*j+9];
+ }
+
+ store_le(out, A, B);
+
+ in += BLOCK_SIZE;
+ out += BLOCK_SIZE;
+ }
+ }
+
+/*
+* RC5 Decryption
+*/
+void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const
+ {
+ for(size_t i = 0; i != blocks; ++i)
+ {
+ u32bit A = load_le<u32bit>(in, 0);
+ u32bit B = load_le<u32bit>(in, 1);
+
+ for(size_t j = rounds; j != 0; j -= 4)
+ {
+ B = rotate_right(B - S[2*j+1], A % 32) ^ A;
+ A = rotate_right(A - S[2*j ], B % 32) ^ B;
+
+ B = rotate_right(B - S[2*j-1], A % 32) ^ A;
+ A = rotate_right(A - S[2*j-2], B % 32) ^ B;
+
+ B = rotate_right(B - S[2*j-3], A % 32) ^ A;
+ A = rotate_right(A - S[2*j-4], B % 32) ^ B;
+
+ B = rotate_right(B - S[2*j-5], A % 32) ^ A;
+ A = rotate_right(A - S[2*j-6], B % 32) ^ B;
+ }
+ B -= S[1]; A -= S[0];
+
+ store_le(out, A, B);
+
+ in += BLOCK_SIZE;
+ out += BLOCK_SIZE;
+ }
+ }
+
+/*
+* RC5 Key Schedule
+*/
+void RC5::key_schedule(const byte key[], size_t length)
+ {
+ S.resize(2*rounds + 2);
+
+ const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1);
+ const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size());
+
+ S[0] = 0xB7E15163;
+ for(size_t i = 1; i != S.size(); ++i)
+ S[i] = S[i-1] + 0x9E3779B9;
+
+ secure_vector<u32bit> K(8);
+
+ for(s32bit i = length-1; i >= 0; --i)
+ K[i/4] = (K[i/4] << 8) + key[i];
+
+ u32bit A = 0, B = 0;
+
+ for(size_t i = 0; i != MIX_ROUNDS; ++i)
+ {
+ A = rotate_left(S[i % S.size()] + A + B, 3);
+ B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32);
+ S[i % S.size()] = A;
+ K[i % WORD_KEYLENGTH] = B;
+ }
+ }
+
+void RC5::clear()
+ {
+ zap(S);
+ }
+
+/*
+* Return the name of this type
+*/
+std::string RC5::name() const
+ {
+ return "RC5(" + std::to_string(rounds) + ")";
+ }
+
+/*
+* RC5 Constructor
+*/
+RC5::RC5(size_t r) : rounds(r)
+ {
+ if(rounds < 8 || rounds > 32 || (rounds % 4 != 0))
+ throw Invalid_Argument("RC5: Invalid number of rounds " +
+ std::to_string(rounds));
+ }
+
+}
diff --git a/src/lib/block/rc5/rc5.h b/src/lib/block/rc5/rc5.h
new file mode 100644
index 000000000..9055974e5
--- /dev/null
+++ b/src/lib/block/rc5/rc5.h
@@ -0,0 +1,42 @@
+/*
+* RC5
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_RC5_H__
+#define BOTAN_RC5_H__
+
+#include <botan/block_cipher.h>
+
+namespace Botan {
+
+/**
+* RC5
+*/
+class BOTAN_DLL RC5 : public Block_Cipher_Fixed_Params<8, 1, 32>
+ {
+ 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;
+ BlockCipher* clone() const { return new RC5(rounds); }
+
+ /**
+ * @param rounds the number of RC5 rounds to run. Must be between
+ * 8 and 32 and a multiple of 4.
+ */
+ RC5(size_t rounds);
+ private:
+ void key_schedule(const byte[], size_t);
+
+ size_t rounds;
+ secure_vector<u32bit> S;
+ };
+
+}
+
+#endif