Botan  1.11.15
src/lib/stream/stream_cipher.h
Go to the documentation of this file.
00001 /*
00002 * Stream Cipher
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_STREAM_CIPHER_H__
00009 #define BOTAN_STREAM_CIPHER_H__
00010 
00011 #include <botan/transform.h>
00012 #include <botan/sym_algo.h>
00013 #include <botan/scan_name.h>
00014 
00015 namespace Botan {
00016 
00017 /**
00018 * Base class for all stream ciphers
00019 */
00020 class BOTAN_DLL StreamCipher : public SymmetricAlgorithm
00021    {
00022    public:
00023       /**
00024       * Encrypt or decrypt a message
00025       * @param in the plaintext
00026       * @param out the byte array to hold the output, i.e. the ciphertext
00027       * @param len the length of both in and out in bytes
00028       */
00029       virtual void cipher(const byte in[], byte out[], size_t len) = 0;
00030 
00031       /**
00032       * Encrypt or decrypt a message
00033       * @param buf the plaintext / ciphertext
00034       * @param len the length of buf in bytes
00035       */
00036       void cipher1(byte buf[], size_t len)
00037          { cipher(buf, buf, len); }
00038 
00039       template<typename Alloc>
00040          void encipher(std::vector<byte, Alloc>& inout)
00041          { cipher(&inout[0], &inout[0], inout.size()); }
00042 
00043       template<typename Alloc>
00044          void encrypt(std::vector<byte, Alloc>& inout)
00045          { cipher(&inout[0], &inout[0], inout.size()); }
00046 
00047       template<typename Alloc>
00048          void decrypt(std::vector<byte, Alloc>& inout)
00049          { cipher(&inout[0], &inout[0], inout.size()); }
00050 
00051       /**
00052       * Resync the cipher using the IV
00053       * @param iv the initialization vector
00054       * @param iv_len the length of the IV in bytes
00055       */
00056       virtual void set_iv(const byte[], size_t iv_len)
00057          {
00058          if(iv_len)
00059             throw Invalid_IV_Length(name(), iv_len);
00060          }
00061 
00062       /**
00063       * @param iv_len the length of the IV in bytes
00064       * @return if the length is valid for this algorithm
00065       */
00066       virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
00067 
00068       /**
00069       * Get a new object representing the same algorithm as *this
00070       */
00071       virtual StreamCipher* clone() const = 0;
00072 
00073       typedef SCAN_Name Spec;
00074    };
00075 
00076 }
00077 
00078 #endif