aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/engine/core_engine/core_modes.cpp8
-rw-r--r--src/filters/modes/cfb/cfb.cpp175
-rw-r--r--src/filters/modes/cfb/cfb.h84
-rw-r--r--src/filters/modes/cfb/info.txt5
-rw-r--r--src/modes/cfb/cfb.cpp150
-rw-r--r--src/modes/cfb/cfb.h91
-rw-r--r--src/modes/cfb/info.txt1
7 files changed, 246 insertions, 268 deletions
diff --git a/src/engine/core_engine/core_modes.cpp b/src/engine/core_engine/core_modes.cpp
index c2c34234f..e949d5f9b 100644
--- a/src/engine/core_engine/core_modes.cpp
+++ b/src/engine/core_engine/core_modes.cpp
@@ -16,7 +16,7 @@
#include <botan/cts.h>
#endif
-#if defined(BOTAN_HAS_CFB)
+#if defined(BOTAN_HAS_MODE_CFB)
#include <botan/cfb.h>
#endif
@@ -206,13 +206,13 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher,
#endif
-#if defined(BOTAN_HAS_CFB)
+#if defined(BOTAN_HAS_MODE_CFB)
if(mode_name == "CFB")
{
if(direction == ENCRYPTION)
- return new CFB_Encryption(block_cipher->clone(), bits);
+ return new Transformation_Filter(new CFB_Encryption(block_cipher->clone(), bits));
else
- return new CFB_Decryption(block_cipher->clone(), bits);
+ return new Transformation_Filter(new CFB_Decryption(block_cipher->clone(), bits));
}
#endif
diff --git a/src/filters/modes/cfb/cfb.cpp b/src/filters/modes/cfb/cfb.cpp
deleted file mode 100644
index 2b7cca84a..000000000
--- a/src/filters/modes/cfb/cfb.cpp
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
-* CFB Mode
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/cfb.h>
-#include <botan/parsing.h>
-#include <botan/internal/xor_buf.h>
-#include <algorithm>
-
-namespace Botan {
-
-/*
-* CFB Encryption Constructor
-*/
-CFB_Encryption::CFB_Encryption(BlockCipher* ciph, size_t fback_bits)
- {
- cipher = ciph;
- feedback = fback_bits ? fback_bits / 8: cipher->block_size();
-
- buffer.resize(cipher->block_size());
- state.resize(cipher->block_size());
- position = 0;
-
- if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
- throw Invalid_Argument("CFB_Encryption: Invalid feedback size " +
- std::to_string(fback_bits));
- }
-
-/*
-* CFB Encryption Constructor
-*/
-CFB_Encryption::CFB_Encryption(BlockCipher* ciph,
- const SymmetricKey& key,
- const InitializationVector& iv,
- size_t fback_bits)
- {
- cipher = ciph;
- feedback = fback_bits ? fback_bits / 8: cipher->block_size();
-
- buffer.resize(cipher->block_size());
- state.resize(cipher->block_size());
- position = 0;
-
- if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
- throw Invalid_Argument("CFB_Encryption: Invalid feedback size " +
- std::to_string(fback_bits));
-
- set_key(key);
- set_iv(iv);
- }
-
-void CFB_Encryption::set_iv(const InitializationVector& iv)
- {
- if(!valid_iv_length(iv.length()))
- throw Invalid_IV_Length(name(), iv.length());
-
- state = iv.bits_of();
- zeroise(buffer);
- position = 0;
-
- cipher->encrypt(&state[0], &buffer[0]);
- }
-
-/*
-* Encrypt data in CFB mode
-*/
-void CFB_Encryption::write(const byte input[], size_t length)
- {
- while(length)
- {
- size_t xored = std::min(feedback - position, length);
- xor_buf(&buffer[position], input, xored);
- send(&buffer[position], xored);
- input += xored;
- length -= xored;
- position += xored;
-
- if(position == feedback)
- {
- for(size_t j = 0; j != cipher->block_size() - feedback; ++j)
- state[j] = state[j + feedback];
-
- buffer_insert(state, cipher->block_size() - feedback,
- &buffer[0], feedback);
-
- cipher->encrypt(state, buffer);
- position = 0;
- }
- }
- }
-
-/*
-* CFB Decryption Constructor
-*/
-CFB_Decryption::CFB_Decryption(BlockCipher* ciph, size_t fback_bits)
- {
- cipher = ciph;
- feedback = fback_bits ? fback_bits / 8: cipher->block_size();
-
- buffer.resize(cipher->block_size());
- state.resize(cipher->block_size());
- position = 0;
-
- if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
- throw Invalid_Argument("CFB_Decryption: Invalid feedback size " +
- std::to_string(fback_bits));
- }
-
-/*
-* CFB Decryption Constructor
-*/
-CFB_Decryption::CFB_Decryption(BlockCipher* ciph,
- const SymmetricKey& key,
- const InitializationVector& iv,
- size_t fback_bits)
- {
- cipher = ciph;
- feedback = fback_bits ? fback_bits / 8: cipher->block_size();
-
- buffer.resize(cipher->block_size());
- state.resize(cipher->block_size());
- position = 0;
-
- if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
- throw Invalid_Argument("CFB_Decryption: Invalid feedback size " +
- std::to_string(fback_bits));
-
- set_key(key);
- set_iv(iv);
- }
-
-void CFB_Decryption::set_iv(const InitializationVector& iv)
- {
- if(!valid_iv_length(iv.length()))
- throw Invalid_IV_Length(name(), iv.length());
-
- state = iv.bits_of();
- zeroise(buffer);
- position = 0;
-
- cipher->encrypt(state, buffer);
- }
-
-/*
-* Decrypt data in CFB mode
-*/
-void CFB_Decryption::write(const byte input[], size_t length)
- {
- while(length)
- {
- size_t xored = std::min(feedback - position, length);
- xor_buf(&buffer[position], input, xored);
- send(&buffer[position], xored);
- buffer_insert(buffer, position, input, xored);
- input += xored;
- length -= xored;
- position += xored;
- if(position == feedback)
- {
- for(size_t j = 0; j != cipher->block_size() - feedback; ++j)
- state[j] = state[j + feedback];
-
- buffer_insert(state, cipher->block_size() - feedback,
- &buffer[0], feedback);
-
- cipher->encrypt(state, buffer);
- position = 0;
- }
- }
- }
-
-}
diff --git a/src/filters/modes/cfb/cfb.h b/src/filters/modes/cfb/cfb.h
deleted file mode 100644
index 02154ebb9..000000000
--- a/src/filters/modes/cfb/cfb.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
-* CFB Mode
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_CFB_H__
-#define BOTAN_CFB_H__
-
-#include <botan/block_cipher.h>
-#include <botan/key_filt.h>
-
-namespace Botan {
-
-/**
-* CFB Encryption
-*/
-class BOTAN_DLL CFB_Encryption : public Keyed_Filter
- {
- public:
- std::string name() const { return cipher->name() + "/CFB"; }
-
- void set_iv(const InitializationVector&);
-
- void set_key(const SymmetricKey& key) { cipher->set_key(key); }
-
- Key_Length_Specification key_spec() const override { return cipher->key_spec(); }
-
- bool valid_iv_length(size_t iv_len) const
- { return (iv_len == cipher->block_size()); }
-
- CFB_Encryption(BlockCipher* cipher, size_t feedback = 0);
-
- CFB_Encryption(BlockCipher* cipher,
- const SymmetricKey& key,
- const InitializationVector& iv,
- size_t feedback = 0);
-
- ~CFB_Encryption() { delete cipher; }
- private:
- void write(const byte[], size_t);
-
- BlockCipher* cipher;
- secure_vector<byte> buffer, state;
- size_t position, feedback;
- };
-
-/**
-* CFB Decryption
-*/
-class BOTAN_DLL CFB_Decryption : public Keyed_Filter
- {
- public:
- std::string name() const { return cipher->name() + "/CFB"; }
-
- void set_iv(const InitializationVector&);
-
- void set_key(const SymmetricKey& key) { cipher->set_key(key); }
-
- Key_Length_Specification key_spec() const override { return cipher->key_spec(); }
-
- bool valid_iv_length(size_t iv_len) const
- { return (iv_len == cipher->block_size()); }
-
- CFB_Decryption(BlockCipher* cipher, size_t feedback = 0);
-
- CFB_Decryption(BlockCipher* cipher,
- const SymmetricKey& key,
- const InitializationVector& iv,
- size_t feedback = 0);
-
- ~CFB_Decryption() { delete cipher; }
- private:
- void write(const byte[], size_t);
-
- BlockCipher* cipher;
- secure_vector<byte> buffer, state;
- size_t position, feedback;
- };
-
-}
-
-#endif
diff --git a/src/filters/modes/cfb/info.txt b/src/filters/modes/cfb/info.txt
deleted file mode 100644
index eb2cc69ba..000000000
--- a/src/filters/modes/cfb/info.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-define CFB
-
-<requires>
-block
-</requires>
diff --git a/src/modes/cfb/cfb.cpp b/src/modes/cfb/cfb.cpp
new file mode 100644
index 000000000..2edb917b4
--- /dev/null
+++ b/src/modes/cfb/cfb.cpp
@@ -0,0 +1,150 @@
+/*
+* CFB Mode
+* (C) 1999-2007,2013 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/cfb.h>
+#include <botan/parsing.h>
+#include <botan/internal/xor_buf.h>
+
+namespace Botan {
+
+CFB_Mode::CFB_Mode(BlockCipher* cipher, size_t feedback_bits) :
+ m_cipher(cipher),
+ m_feedback_bytes(feedback_bits ? feedback_bits / 8 : cipher->block_size())
+ {
+ if(feedback_bits % 8 || feedback() > cipher->block_size())
+ throw std::invalid_argument(name() + ": feedback bits " +
+ std::to_string(feedback_bits) + " not supported");
+ }
+
+void CFB_Mode::clear()
+ {
+ m_cipher->clear();
+ m_shift_register.clear();
+ }
+
+std::string CFB_Mode::name() const
+ {
+ if(feedback() == cipher().block_size())
+ return cipher().name() + "/CFB";
+ else
+ return cipher().name() + "/CFB(" + std::to_string(feedback()*8) + ")";
+ }
+
+size_t CFB_Mode::output_length(size_t input_length) const
+ {
+ return input_length;
+ }
+
+size_t CFB_Mode::update_granularity() const
+ {
+ return feedback();
+ }
+
+size_t CFB_Mode::minimum_final_size() const
+ {
+ return 0;
+ }
+
+Key_Length_Specification CFB_Mode::key_spec() const
+ {
+ return cipher().key_spec();
+ }
+
+size_t CFB_Mode::default_nonce_size() const
+ {
+ return cipher().block_size();
+ }
+
+bool CFB_Mode::valid_nonce_length(size_t n) const
+ {
+ return (n == cipher().block_size());
+ }
+
+void CFB_Mode::key_schedule(const byte key[], size_t length)
+ {
+ m_cipher->set_key(key, length);
+ }
+
+secure_vector<byte> CFB_Mode::start(const byte nonce[], size_t nonce_len)
+ {
+ if(!valid_nonce_length(nonce_len))
+ throw Invalid_IV_Length(name(), nonce_len);
+
+ m_shift_register.assign(nonce, nonce + nonce_len);
+ m_keystream_buf.resize(m_shift_register.size());
+ cipher().encrypt(m_shift_register, m_keystream_buf);
+
+ return secure_vector<byte>();
+ }
+
+void CFB_Encryption::update(secure_vector<byte>& buffer, size_t offset)
+ {
+ BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
+ size_t sz = buffer.size() - offset;
+ byte* buf = &buffer[offset];
+
+ const size_t BS = cipher().block_size();
+
+ secure_vector<byte>& state = shift_register();
+ const size_t shift = feedback();
+
+ while(sz)
+ {
+ const size_t took = std::min(shift, sz);
+ xor_buf(&buf[0], &keystream_buf()[0], took);
+
+ // Assumes feedback-sized block except for last input
+ copy_mem(&state[0], &state[shift], BS - shift);
+ copy_mem(&state[BS-shift], &buf[0], shift);
+ cipher().encrypt(state, keystream_buf());
+
+ buf += took;
+ sz -= took;
+ }
+ }
+
+void CFB_Encryption::finish(secure_vector<byte>& buffer, size_t offset)
+ {
+ update(buffer, offset);
+ }
+
+void CFB_Decryption::update(secure_vector<byte>& buffer, size_t offset)
+ {
+ BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
+ size_t sz = buffer.size() - offset;
+ byte* buf = &buffer[offset];
+
+ const size_t BS = cipher().block_size();
+
+ secure_vector<byte>& state = shift_register();
+ const size_t shift = feedback();
+
+ while(sz)
+ {
+ const size_t took = std::min(shift, sz);
+
+ // first update shift register with ciphertext
+ copy_mem(&state[0], &state[shift], BS - shift);
+ copy_mem(&state[BS-shift], &buf[0], took);
+
+ // then decrypt
+ xor_buf(&buf[0], &keystream_buf()[0], took);
+
+ // then update keystream
+ cipher().encrypt(state, keystream_buf());
+
+ buf += took;
+ sz -= took;
+ }
+ }
+
+void CFB_Decryption::finish(secure_vector<byte>& buffer, size_t offset)
+ {
+ update(buffer, offset);
+ }
+
+}
diff --git a/src/modes/cfb/cfb.h b/src/modes/cfb/cfb.h
new file mode 100644
index 000000000..19e5b4bb0
--- /dev/null
+++ b/src/modes/cfb/cfb.h
@@ -0,0 +1,91 @@
+/*
+* CFB mode
+* (C) 1999-2007,2013 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_MODE_CFB_H__
+#define BOTAN_MODE_CFB_H__
+
+#include <botan/transform.h>
+#include <botan/block_cipher.h>
+#include <botan/mode_pad.h>
+#include <memory>
+
+namespace Botan {
+
+/**
+* CFB Mode
+*/
+class CFB_Mode : public Transformation
+ {
+ public:
+ secure_vector<byte> start(const byte nonce[], size_t nonce_len) override;
+
+ std::string name() const override;
+
+ size_t update_granularity() const override;
+
+ size_t minimum_final_size() const override;
+
+ Key_Length_Specification key_spec() const override;
+
+ size_t output_length(size_t input_length) const override;
+
+ size_t default_nonce_size() const override;
+
+ bool valid_nonce_length(size_t n) const override;
+
+ void clear();
+ protected:
+ CFB_Mode(BlockCipher* cipher, size_t feedback_bits);
+
+ const BlockCipher& cipher() const { return *m_cipher; }
+
+ size_t feedback() const { return m_feedback_bytes; }
+
+ secure_vector<byte>& shift_register() { return m_shift_register; }
+
+ secure_vector<byte>& keystream_buf() { return m_keystream_buf; }
+
+ private:
+ void key_schedule(const byte key[], size_t length) override;
+
+ std::unique_ptr<BlockCipher> m_cipher;
+ secure_vector<byte> m_shift_register;
+ secure_vector<byte> m_keystream_buf;
+ size_t m_feedback_bytes;
+ };
+
+/**
+* CFB Encryption
+*/
+class BOTAN_DLL CFB_Encryption : public CFB_Mode
+ {
+ public:
+ CFB_Encryption(BlockCipher* cipher, size_t feedback_bits) :
+ CFB_Mode(cipher, feedback_bits) {}
+
+ void update(secure_vector<byte>& blocks, size_t offset) override;
+
+ void finish(secure_vector<byte>& final_block, size_t offset) override;
+ };
+
+/**
+* CFB Decryption
+*/
+class BOTAN_DLL CFB_Decryption : public CFB_Mode
+ {
+ public:
+ CFB_Decryption(BlockCipher* cipher, size_t feedback_bits) :
+ CFB_Mode(cipher, feedback_bits) {}
+
+ void update(secure_vector<byte>& blocks, size_t offset) override;
+
+ void finish(secure_vector<byte>& final_block, size_t offset) override;
+ };
+
+}
+
+#endif
diff --git a/src/modes/cfb/info.txt b/src/modes/cfb/info.txt
new file mode 100644
index 000000000..797459180
--- /dev/null
+++ b/src/modes/cfb/info.txt
@@ -0,0 +1 @@
+define MODE_CFB