aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream/ctr
diff options
context:
space:
mode:
Diffstat (limited to 'src/stream/ctr')
-rw-r--r--src/stream/ctr/ctr.cpp141
-rw-r--r--src/stream/ctr/ctr.h49
-rw-r--r--src/stream/ctr/info.txt13
3 files changed, 203 insertions, 0 deletions
diff --git a/src/stream/ctr/ctr.cpp b/src/stream/ctr/ctr.cpp
new file mode 100644
index 000000000..5f0880fa5
--- /dev/null
+++ b/src/stream/ctr/ctr.cpp
@@ -0,0 +1,141 @@
+/*
+* CTR-BE Mode Cipher
+* (C) 1999-2009 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/ctr.h>
+#include <botan/xor_buf.h>
+
+namespace Botan {
+
+/*
+* CTR-BE Constructor
+*/
+
+CTR_BE::CTR_BE(BlockCipher* ciph) :
+ StreamCipher(ciph->MINIMUM_KEYLENGTH,
+ ciph->MAXIMUM_KEYLENGTH,
+ ciph->KEYLENGTH_MULTIPLE),
+ permutation(ciph)
+ {
+ position = 0;
+
+ counter.create(permutation->BLOCK_SIZE * BOTAN_PARALLEL_BLOCKS_CTR);
+ buffer.create(permutation->BLOCK_SIZE * BOTAN_PARALLEL_BLOCKS_CTR);
+ }
+
+/*
+* CTR_BE Destructor
+*/
+CTR_BE::~CTR_BE()
+ {
+ delete permutation;
+ }
+
+/*
+* Zeroize
+*/
+void CTR_BE::clear()
+ {
+ permutation->clear();
+ buffer.clear();
+ counter.clear();
+ position = 0;
+ }
+
+/*
+* Set the key
+*/
+void CTR_BE::key_schedule(const byte key[], u32bit key_len)
+ {
+ permutation->set_key(key, key_len);
+
+ // Set a default all-zeros IV
+ set_iv(0, 0);
+ }
+
+/*
+* Return the name of this type
+*/
+std::string CTR_BE::name() const
+ {
+ return ("CTR-BE(" + permutation->name() + ")");
+ }
+
+/*
+* CTR-BE Encryption/Decryption
+*/
+void CTR_BE::cipher(const byte in[], byte out[], u32bit length)
+ {
+ while(length >= buffer.size() - position)
+ {
+ xor_buf(out, in, buffer.begin() + position, buffer.size() - position);
+ length -= (buffer.size() - position);
+ in += (buffer.size() - position);
+ out += (buffer.size() - position);
+ increment_counter();
+ }
+ xor_buf(out, in, buffer.begin() + position, length);
+ position += length;
+ }
+
+/*
+* Set CTR-BE IV
+*/
+void CTR_BE::set_iv(const byte iv[], u32bit iv_len)
+ {
+ if(!valid_iv_length(iv_len))
+ throw Invalid_IV_Length(name(), iv_len);
+
+ const u32bit BLOCK_SIZE = permutation->BLOCK_SIZE;
+
+ counter.clear();
+
+ counter.copy(0, iv, iv_len);
+
+ const u32bit PARALLEL_BLOCKS = counter.size() / BLOCK_SIZE;
+
+ for(u32bit i = 1; i != PARALLEL_BLOCKS; ++i)
+ {
+ counter.copy(i*BLOCK_SIZE,
+ counter.begin() + (i-1)*BLOCK_SIZE, BLOCK_SIZE);
+
+ for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j)
+ if(++counter[i*BLOCK_SIZE+j])
+ break;
+ }
+
+ permutation->encrypt_n(counter, buffer, PARALLEL_BLOCKS);
+ position = 0;
+ }
+
+/*
+* Increment the counter and update the buffer
+*/
+void CTR_BE::increment_counter()
+ {
+ const u32bit PARALLEL_BLOCKS = counter.size() / permutation->BLOCK_SIZE;
+
+ for(u32bit i = 0; i != PARALLEL_BLOCKS; ++i)
+ {
+ byte* this_ctr = counter + i*permutation->BLOCK_SIZE;
+
+ byte last_byte = this_ctr[permutation->BLOCK_SIZE-1];
+ last_byte += PARALLEL_BLOCKS;
+
+ if(this_ctr[permutation->BLOCK_SIZE-1] > last_byte)
+ for(s32bit j = permutation->BLOCK_SIZE - 2; j >= 0; --j)
+ if(++this_ctr[j])
+ break;
+
+ this_ctr[permutation->BLOCK_SIZE-1] = last_byte;
+ }
+
+ permutation->encrypt_n(counter, buffer, PARALLEL_BLOCKS);
+
+ position = 0;
+ }
+
+}
diff --git a/src/stream/ctr/ctr.h b/src/stream/ctr/ctr.h
new file mode 100644
index 000000000..5f94170cc
--- /dev/null
+++ b/src/stream/ctr/ctr.h
@@ -0,0 +1,49 @@
+/*
+* CTR-BE Mode
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_CTR_BE_H__
+#define BOTAN_CTR_BE_H__
+
+#include <botan/block_cipher.h>
+#include <botan/stream_cipher.h>
+
+namespace Botan {
+
+/*
+* CTR-BE (Counter, big-endian)
+*/
+class BOTAN_DLL CTR_BE : public StreamCipher
+ {
+ public:
+ void cipher(const byte in[], byte out[], u32bit length);
+
+ void set_iv(const byte iv[], u32bit iv_len);
+
+ bool valid_iv_length(u32bit iv_len) const
+ { return (iv_len <= permutation->BLOCK_SIZE); }
+
+ std::string name() const;
+
+ CTR_BE* clone() const
+ { return new CTR_BE(permutation->clone()); }
+
+ void clear();
+
+ CTR_BE(BlockCipher*);
+ ~CTR_BE();
+ private:
+ void key_schedule(const byte key[], u32bit key_len);
+ void increment_counter();
+
+ BlockCipher* permutation;
+ SecureVector<byte> counter, buffer;
+ u32bit position;
+ };
+
+}
+
+#endif
diff --git a/src/stream/ctr/info.txt b/src/stream/ctr/info.txt
new file mode 100644
index 000000000..ac4f3f710
--- /dev/null
+++ b/src/stream/ctr/info.txt
@@ -0,0 +1,13 @@
+define CTR_BE
+
+load_on auto
+
+<add>
+ctr.cpp
+ctr.h
+</add>
+
+<requires>
+block
+stream
+</requires>