Botan  1.11.15
src/lib/modes/stream_mode.h
Go to the documentation of this file.
00001 /*
00002 * (C) 2015 Jack Lloyd
00003 *
00004 * Botan is released under the Simplified BSD License (see license.txt)
00005 */
00006 
00007 #ifndef BOTAN_STREAM_MODE_H__
00008 #define BOTAN_STREAM_MODE_H__
00009 
00010 #include <botan/cipher_mode.h>
00011 #include <botan/stream_cipher.h>
00012 
00013 namespace Botan {
00014 
00015 class BOTAN_DLL Stream_Cipher_Mode : public Cipher_Mode
00016    {
00017    public:
00018       Stream_Cipher_Mode(StreamCipher* cipher) : m_cipher(cipher) {}
00019 
00020       void update(secure_vector<byte>& buf, size_t offset) override
00021          {
00022          if(offset < buf.size())
00023             m_cipher->cipher1(&buf[offset], buf.size() - offset);
00024          }
00025 
00026       void finish(secure_vector<byte>& buf, size_t offset) override
00027          { return update(buf, offset); }
00028 
00029       size_t output_length(size_t input_length) const override { return input_length; }
00030 
00031       size_t update_granularity() const override { return 64; /* arbitrary */ }
00032 
00033       size_t minimum_final_size() const override { return 0; }
00034 
00035       size_t default_nonce_length() const override { return 0; }
00036 
00037       bool valid_nonce_length(size_t nonce_len) const override
00038          { return m_cipher->valid_iv_length(nonce_len); }
00039 
00040       Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); }
00041 
00042       std::string name() const override { return m_cipher->name(); }
00043 
00044       void clear() override { return m_cipher->clear(); }
00045 
00046    private:
00047       secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override
00048          {
00049          m_cipher->set_iv(nonce, nonce_len);
00050          return secure_vector<byte>();
00051          }
00052 
00053       void key_schedule(const byte key[], size_t length)
00054          {
00055          m_cipher->set_key(key, length);
00056          }
00057 
00058       std::unique_ptr<StreamCipher> m_cipher;
00059    };
00060 
00061 }
00062 
00063 #endif