aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/stream
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/stream')
-rw-r--r--src/lib/stream/ctr/ctr.cpp90
-rw-r--r--src/lib/stream/ctr/ctr.h18
-rw-r--r--src/lib/stream/ofb/ofb.cpp72
-rw-r--r--src/lib/stream/ofb/ofb.h14
4 files changed, 67 insertions, 127 deletions
diff --git a/src/lib/stream/ctr/ctr.cpp b/src/lib/stream/ctr/ctr.cpp
index 87ec86c65..ad0fd363a 100644
--- a/src/lib/stream/ctr/ctr.cpp
+++ b/src/lib/stream/ctr/ctr.cpp
@@ -1,6 +1,6 @@
/*
* Counter mode
-* (C) 1999-2011 Jack Lloyd
+* (C) 1999-2011,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -10,101 +10,72 @@
namespace Botan {
-/*
-* CTR-BE Constructor
-*/
-
CTR_BE::CTR_BE(BlockCipher* ciph) :
- permutation(ciph),
- counter(256 * permutation->block_size()),
- buffer(counter.size()),
- position(0)
+ m_cipher(ciph),
+ m_counter(256 * m_cipher->block_size()),
+ m_pad(m_counter.size()),
+ m_pad_pos(0)
{
}
-/*
-* CTR_BE Destructor
-*/
-CTR_BE::~CTR_BE()
- {
- delete permutation;
- }
-
-/*
-* Zeroize
-*/
void CTR_BE::clear()
{
- permutation->clear();
- zeroise(buffer);
- zeroise(counter);
- position = 0;
+ m_cipher->clear();
+ zeroise(m_pad);
+ zeroise(m_counter);
+ m_pad_pos = 0;
}
-/*
-* Set the key
-*/
void CTR_BE::key_schedule(const byte key[], size_t key_len)
{
- permutation->set_key(key, key_len);
+ m_cipher->set_key(key, key_len);
// Set a default all-zeros IV
set_iv(nullptr, 0);
}
-/*
-* Return the name of this type
-*/
std::string CTR_BE::name() const
{
- return ("CTR-BE(" + permutation->name() + ")");
+ return ("CTR-BE(" + m_cipher->name() + ")");
}
-/*
-* CTR-BE Encryption/Decryption
-*/
void CTR_BE::cipher(const byte in[], byte out[], size_t length)
{
- while(length >= buffer.size() - position)
+ while(length >= m_pad.size() - m_pad_pos)
{
- xor_buf(out, in, &buffer[position], buffer.size() - position);
- length -= (buffer.size() - position);
- in += (buffer.size() - position);
- out += (buffer.size() - position);
+ xor_buf(out, in, &m_pad[m_pad_pos], m_pad.size() - m_pad_pos);
+ length -= (m_pad.size() - m_pad_pos);
+ in += (m_pad.size() - m_pad_pos);
+ out += (m_pad.size() - m_pad_pos);
increment_counter();
}
- xor_buf(out, in, &buffer[position], length);
- position += length;
+ xor_buf(out, in, &m_pad[m_pad_pos], length);
+ m_pad_pos += length;
}
-/*
-* Set CTR-BE IV
-*/
void CTR_BE::set_iv(const byte iv[], size_t iv_len)
{
if(!valid_iv_length(iv_len))
throw Invalid_IV_Length(name(), iv_len);
- const size_t bs = permutation->block_size();
+ const size_t bs = m_cipher->block_size();
- zeroise(counter);
+ zeroise(m_counter);
- buffer_insert(counter, 0, iv, iv_len);
+ buffer_insert(m_counter, 0, iv, iv_len);
- /*
- * Set counter blocks to IV, IV + 1, ... IV + 255
- */
+ // Set m_counter blocks to IV, IV + 1, ... IV + 255
for(size_t i = 1; i != 256; ++i)
{
- buffer_insert(counter, i*bs, &counter[(i-1)*bs], bs);
+ buffer_insert(m_counter, i*bs, &m_counter[(i-1)*bs], bs);
for(size_t j = 0; j != bs; ++j)
- if(++counter[i*bs + (bs - 1 - j)])
+ if(++m_counter[i*bs + (bs - 1 - j)])
break;
}
- permutation->encrypt_n(&counter[0], &buffer[0], 256);
- position = 0;
+ m_cipher->encrypt_n(&m_counter[0], &m_pad[0], 256);
+ m_pad_pos = 0;
}
/*
@@ -112,7 +83,7 @@ void CTR_BE::set_iv(const byte iv[], size_t iv_len)
*/
void CTR_BE::increment_counter()
{
- const size_t bs = permutation->block_size();
+ const size_t bs = m_cipher->block_size();
/*
* Each counter value always needs to be incremented by 256,
@@ -122,13 +93,12 @@ void CTR_BE::increment_counter()
for(size_t i = 0; i != 256; ++i)
{
for(size_t j = 1; j != bs; ++j)
- if(++counter[i*bs + (bs - 1 - j)])
+ if(++m_counter[i*bs + (bs - 1 - j)])
break;
}
- permutation->encrypt_n(&counter[0], &buffer[0], 256);
-
- position = 0;
+ m_cipher->encrypt_n(&m_counter[0], &m_pad[0], 256);
+ m_pad_pos = 0;
}
}
diff --git a/src/lib/stream/ctr/ctr.h b/src/lib/stream/ctr/ctr.h
index 84cf9ed5d..c262f4ad9 100644
--- a/src/lib/stream/ctr/ctr.h
+++ b/src/lib/stream/ctr/ctr.h
@@ -10,6 +10,7 @@
#include <botan/block_cipher.h>
#include <botan/stream_cipher.h>
+#include <memory>
namespace Botan {
@@ -24,17 +25,17 @@ class BOTAN_DLL CTR_BE : public StreamCipher
void set_iv(const byte iv[], size_t iv_len);
bool valid_iv_length(size_t iv_len) const
- { return (iv_len <= permutation->block_size()); }
+ { return (iv_len <= m_cipher->block_size()); }
Key_Length_Specification key_spec() const
{
- return permutation->key_spec();
+ return m_cipher->key_spec();
}
std::string name() const;
CTR_BE* clone() const
- { return new CTR_BE(permutation->clone()); }
+ { return new CTR_BE(m_cipher->clone()); }
void clear();
@@ -42,18 +43,13 @@ class BOTAN_DLL CTR_BE : public StreamCipher
* @param cipher the underlying block cipher to use
*/
CTR_BE(BlockCipher* cipher);
-
- CTR_BE(const CTR_BE&) = delete;
- CTR_BE& operator=(const CTR_BE&) = delete;
-
- ~CTR_BE();
private:
void key_schedule(const byte key[], size_t key_len);
void increment_counter();
- BlockCipher* permutation;
- secure_vector<byte> counter, buffer;
- size_t position;
+ std::unique_ptr<BlockCipher> m_cipher;
+ secure_vector<byte> m_counter, m_pad;
+ size_t m_pad_pos;
};
}
diff --git a/src/lib/stream/ofb/ofb.cpp b/src/lib/stream/ofb/ofb.cpp
index 1137a58af..9d01c7712 100644
--- a/src/lib/stream/ofb/ofb.cpp
+++ b/src/lib/stream/ofb/ofb.cpp
@@ -1,93 +1,67 @@
/*
* OFB Mode
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/ofb.h>
#include <botan/internal/xor_buf.h>
-#include <algorithm>
namespace Botan {
-/*
-* OFB Constructor
-*/
-OFB::OFB(BlockCipher* ciph) : permutation(ciph)
+OFB::OFB(BlockCipher* cipher) :
+ m_cipher(cipher),
+ m_buffer(m_cipher->block_size()),
+ m_buf_pos(0)
{
- position = 0;
- buffer.resize(permutation->block_size());
}
-/*
-* OFB Destructor
-*/
-OFB::~OFB()
- {
- delete permutation;
- }
-
-/*
-* Zeroize
-*/
void OFB::clear()
{
- permutation->clear();
- zeroise(buffer);
- position = 0;
+ m_cipher->clear();
+ zeroise(m_buffer);
+ m_buf_pos = 0;
}
-/*
-* Set the key
-*/
void OFB::key_schedule(const byte key[], size_t key_len)
{
- permutation->set_key(key, key_len);
+ m_cipher->set_key(key, key_len);
// Set a default all-zeros IV
set_iv(nullptr, 0);
}
-/*
-* Return the name of this type
-*/
std::string OFB::name() const
{
- return ("OFB(" + permutation->name() + ")");
+ return "OFB(" + m_cipher->name() + ")";
}
-/*
-* CTR-BE Encryption/Decryption
-*/
void OFB::cipher(const byte in[], byte out[], size_t length)
{
- while(length >= buffer.size() - position)
+ while(length >= m_buffer.size() - m_buf_pos)
{
- xor_buf(out, in, &buffer[position], buffer.size() - position);
- length -= (buffer.size() - position);
- in += (buffer.size() - position);
- out += (buffer.size() - position);
- permutation->encrypt(buffer);
- position = 0;
+ xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
+ length -= (m_buffer.size() - m_buf_pos);
+ in += (m_buffer.size() - m_buf_pos);
+ out += (m_buffer.size() - m_buf_pos);
+ m_cipher->encrypt(m_buffer);
+ m_buf_pos = 0;
}
- xor_buf(out, in, &buffer[position], length);
- position += length;
+ xor_buf(out, in, &m_buffer[m_buf_pos], length);
+ m_buf_pos += length;
}
-/*
-* Set CTR-BE IV
-*/
void OFB::set_iv(const byte iv[], size_t iv_len)
{
if(!valid_iv_length(iv_len))
throw Invalid_IV_Length(name(), iv_len);
- zeroise(buffer);
- buffer_insert(buffer, 0, iv, iv_len);
+ zeroise(m_buffer);
+ buffer_insert(m_buffer, 0, iv, iv_len);
- permutation->encrypt(buffer);
- position = 0;
+ m_cipher->encrypt(m_buffer);
+ m_buf_pos = 0;
}
}
diff --git a/src/lib/stream/ofb/ofb.h b/src/lib/stream/ofb/ofb.h
index 9d4fd882f..0d08dac0a 100644
--- a/src/lib/stream/ofb/ofb.h
+++ b/src/lib/stream/ofb/ofb.h
@@ -10,6 +10,7 @@
#include <botan/stream_cipher.h>
#include <botan/block_cipher.h>
+#include <memory>
namespace Botan {
@@ -24,17 +25,17 @@ class BOTAN_DLL OFB : public StreamCipher
void set_iv(const byte iv[], size_t iv_len);
bool valid_iv_length(size_t iv_len) const
- { return (iv_len <= permutation->block_size()); }
+ { return (iv_len <= m_cipher->block_size()); }
Key_Length_Specification key_spec() const
{
- return permutation->key_spec();
+ return m_cipher->key_spec();
}
std::string name() const;
OFB* clone() const
- { return new OFB(permutation->clone()); }
+ { return new OFB(m_cipher->clone()); }
void clear();
@@ -42,13 +43,12 @@ class BOTAN_DLL OFB : public StreamCipher
* @param cipher the underlying block cipher to use
*/
OFB(BlockCipher* cipher);
- ~OFB();
private:
void key_schedule(const byte key[], size_t key_len);
- BlockCipher* permutation;
- secure_vector<byte> buffer;
- size_t position;
+ std::unique_ptr<BlockCipher> m_cipher;
+ secure_vector<byte> m_buffer;
+ size_t m_buf_pos;
};
}