Botan  1.11.15
src/lib/block/block_cipher.h
Go to the documentation of this file.
00001 /*
00002 * Block Cipher Base Class
00003 * (C) 1999-2009 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_BLOCK_CIPHER_H__
00009 #define BOTAN_BLOCK_CIPHER_H__
00010 
00011 #include <botan/scan_name.h>
00012 #include <botan/sym_algo.h>
00013 
00014 namespace Botan {
00015 
00016 /**
00017 * This class represents a block cipher object.
00018 */
00019 class BOTAN_DLL BlockCipher : public SymmetricAlgorithm
00020    {
00021    public:
00022       typedef SCAN_Name Spec;
00023 
00024       /**
00025       * @return block size of this algorithm
00026       */
00027       virtual size_t block_size() const = 0;
00028 
00029       /**
00030       * @return native parallelism of this cipher in blocks
00031       */
00032       virtual size_t parallelism() const { return 1; }
00033 
00034       /**
00035       * @return prefererred parallelism of this cipher in bytes
00036       */
00037       size_t parallel_bytes() const
00038          {
00039          return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT;
00040          }
00041 
00042       /**
00043       * Encrypt a block.
00044       * @param in The plaintext block to be encrypted as a byte array.
00045       * Must be of length block_size().
00046       * @param out The byte array designated to hold the encrypted block.
00047       * Must be of length block_size().
00048       */
00049       void encrypt(const byte in[], byte out[]) const
00050          { encrypt_n(in, out, 1); }
00051 
00052       /**
00053       * Decrypt a block.
00054       * @param in The ciphertext block to be decypted as a byte array.
00055       * Must be of length block_size().
00056       * @param out The byte array designated to hold the decrypted block.
00057       * Must be of length block_size().
00058       */
00059       void decrypt(const byte in[], byte out[]) const
00060          { decrypt_n(in, out, 1); }
00061 
00062       /**
00063       * Encrypt a block.
00064       * @param block the plaintext block to be encrypted
00065       * Must be of length block_size(). Will hold the result when the function
00066       * has finished.
00067       */
00068       void encrypt(byte block[]) const { encrypt_n(block, block, 1); }
00069 
00070       /**
00071       * Decrypt a block.
00072       * @param block the ciphertext block to be decrypted
00073       * Must be of length block_size(). Will hold the result when the function
00074       * has finished.
00075       */
00076       void decrypt(byte block[]) const { decrypt_n(block, block, 1); }
00077 
00078       /**
00079       * Encrypt one or more blocks
00080       * @param block the input/output buffer (multiple of block_size())
00081       */
00082       template<typename Alloc>
00083       void encrypt(std::vector<byte, Alloc>& block) const
00084          {
00085          return encrypt_n(&block[0], &block[0], block.size() / block_size());
00086          }
00087 
00088       /**
00089       * Decrypt one or more blocks
00090       * @param block the input/output buffer (multiple of block_size())
00091       */
00092       template<typename Alloc>
00093       void decrypt(std::vector<byte, Alloc>& block) const
00094          {
00095          return decrypt_n(&block[0], &block[0], block.size() / block_size());
00096          }
00097 
00098       /**
00099       * Encrypt one or more blocks
00100       * @param in the input buffer (multiple of block_size())
00101       * @param out the output buffer (same size as in)
00102       */
00103       template<typename Alloc, typename Alloc2>
00104       void encrypt(const std::vector<byte, Alloc>& in,
00105                    std::vector<byte, Alloc2>& out) const
00106          {
00107          return encrypt_n(&in[0], &out[0], in.size() / block_size());
00108          }
00109 
00110       /**
00111       * Decrypt one or more blocks
00112       * @param in the input buffer (multiple of block_size())
00113       * @param out the output buffer (same size as in)
00114       */
00115       template<typename Alloc, typename Alloc2>
00116       void decrypt(const std::vector<byte, Alloc>& in,
00117                    std::vector<byte, Alloc2>& out) const
00118          {
00119          return decrypt_n(&in[0], &out[0], in.size() / block_size());
00120          }
00121 
00122       /**
00123       * Encrypt one or more blocks
00124       * @param in the input buffer (multiple of block_size())
00125       * @param out the output buffer (same size as in)
00126       * @param blocks the number of blocks to process
00127       */
00128       virtual void encrypt_n(const byte in[], byte out[],
00129                              size_t blocks) const = 0;
00130 
00131       /**
00132       * Decrypt one or more blocks
00133       * @param in the input buffer (multiple of block_size())
00134       * @param out the output buffer (same size as in)
00135       * @param blocks the number of blocks to process
00136       */
00137       virtual void decrypt_n(const byte in[], byte out[],
00138                              size_t blocks) const = 0;
00139 
00140       /**
00141       * @return new object representing the same algorithm as *this
00142       */
00143       virtual BlockCipher* clone() const = 0;
00144    };
00145 
00146 /**
00147 * Represents a block cipher with a single fixed block size
00148 */
00149 template<size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1>
00150 class Block_Cipher_Fixed_Params : public BlockCipher
00151    {
00152    public:
00153       enum { BLOCK_SIZE = BS };
00154       size_t block_size() const { return BS; }
00155 
00156       Key_Length_Specification key_spec() const
00157          {
00158          return Key_Length_Specification(KMIN, KMAX, KMOD);
00159          }
00160    };
00161 
00162 }
00163 
00164 #endif