summaryrefslogtreecommitdiffstats
path: root/botan/src/modes/ctr
diff options
context:
space:
mode:
Diffstat (limited to 'botan/src/modes/ctr')
-rw-r--r--botan/src/modes/ctr/ctr.cpp75
-rw-r--r--botan/src/modes/ctr/ctr.h31
-rw-r--r--botan/src/modes/ctr/info.txt15
3 files changed, 121 insertions, 0 deletions
diff --git a/botan/src/modes/ctr/ctr.cpp b/botan/src/modes/ctr/ctr.cpp
new file mode 100644
index 0000000..9eb42ec
--- /dev/null
+++ b/botan/src/modes/ctr/ctr.cpp
@@ -0,0 +1,75 @@
+/*
+* CTR Mode
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/ctr.h>
+#include <botan/xor_buf.h>
+#include <algorithm>
+
+namespace Botan {
+
+/*
+* CTR-BE Constructor
+*/
+CTR_BE::CTR_BE(BlockCipher* ciph) :
+ BlockCipherMode(ciph, "CTR-BE", ciph->BLOCK_SIZE, 1)
+ {
+ }
+
+/*
+* CTR-BE Constructor
+*/
+CTR_BE::CTR_BE(BlockCipher* ciph, const SymmetricKey& key,
+ const InitializationVector& iv) :
+ BlockCipherMode(ciph, "CTR-BE", ciph->BLOCK_SIZE, 1)
+ {
+ set_key(key);
+ set_iv(iv);
+ }
+
+/*
+* CTR-BE Encryption/Decryption
+*/
+void CTR_BE::write(const byte input[], u32bit length)
+ {
+ u32bit copied = std::min(BLOCK_SIZE - position, length);
+ xor_buf(buffer + position, input, copied);
+ send(buffer + position, copied);
+ input += copied;
+ length -= copied;
+ position += copied;
+
+ if(position == BLOCK_SIZE)
+ increment_counter();
+
+ while(length >= BLOCK_SIZE)
+ {
+ xor_buf(buffer, input, BLOCK_SIZE);
+ send(buffer, BLOCK_SIZE);
+
+ input += BLOCK_SIZE;
+ length -= BLOCK_SIZE;
+ increment_counter();
+ }
+
+ xor_buf(buffer + position, input, length);
+ send(buffer + position, length);
+ position += length;
+ }
+
+/*
+* Increment the counter and update the buffer
+*/
+void CTR_BE::increment_counter()
+ {
+ for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j)
+ if(++state[j])
+ break;
+ cipher->encrypt(state, buffer);
+ position = 0;
+ }
+
+}
diff --git a/botan/src/modes/ctr/ctr.h b/botan/src/modes/ctr/ctr.h
new file mode 100644
index 0000000..aa0db57
--- /dev/null
+++ b/botan/src/modes/ctr/ctr.h
@@ -0,0 +1,31 @@
+/*
+* CTR Mode
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_COUNTER_MODE_H__
+#define BOTAN_COUNTER_MODE_H__
+
+#include <botan/modebase.h>
+#include <botan/modebase.h>
+
+namespace Botan {
+
+/*
+* CTR-BE Mode
+*/
+class BOTAN_DLL CTR_BE : public BlockCipherMode
+ {
+ public:
+ CTR_BE(BlockCipher*);
+ CTR_BE(BlockCipher*, const SymmetricKey&, const InitializationVector&);
+ private:
+ void write(const byte[], u32bit);
+ void increment_counter();
+ };
+
+}
+
+#endif
diff --git a/botan/src/modes/ctr/info.txt b/botan/src/modes/ctr/info.txt
new file mode 100644
index 0000000..cb291a2
--- /dev/null
+++ b/botan/src/modes/ctr/info.txt
@@ -0,0 +1,15 @@
+realname "CTR block cipher mode"
+
+define CTR
+
+load_on auto
+
+<add>
+ctr.cpp
+ctr.h
+</add>
+
+<requires>
+modes
+</requires>
+