00001 /* 00002 * Block Cipher Base Class 00003 * (C) 1999-2009 Jack Lloyd 00004 * 00005 * Distributed under the terms of the Botan license 00006 */ 00007 00008 #ifndef BOTAN_BLOCK_CIPHER_H__ 00009 #define BOTAN_BLOCK_CIPHER_H__ 00010 00011 #include <botan/sym_algo.h> 00012 00013 namespace Botan { 00014 00015 /** 00016 * This class represents a block cipher object. 00017 */ 00018 class BOTAN_DLL BlockCipher : public SymmetricAlgorithm 00019 { 00020 public: 00021 /** 00022 * BlockCipher constructor 00023 * @param block_size the size of blocks this cipher processes 00024 * @param key_min the minimum key size 00025 * @param key_max the maximum key size 00026 * @param key_mod the modulo restriction on the key size 00027 */ 00028 BlockCipher(u32bit block_size, 00029 u32bit key_min, 00030 u32bit key_max = 0, 00031 u32bit key_mod = 1) : 00032 SymmetricAlgorithm(key_min, key_max, key_mod), 00033 BLOCK_SIZE(block_size) {} 00034 00035 virtual ~BlockCipher() {} 00036 00037 /** 00038 * The block size of this algorithm. 00039 */ 00040 const u32bit BLOCK_SIZE; 00041 00042 /** 00043 * @return native parallelism of this cipher in blocks 00044 */ 00045 virtual u32bit parallelism() const { return 1; } 00046 00047 /** 00048 * @return prefererred parallelism of this cipher in bytes 00049 */ 00050 u32bit parallel_bytes() const 00051 { 00052 return parallelism() * BLOCK_SIZE * BOTAN_BLOCK_CIPHER_PAR_MULT; 00053 } 00054 00055 /** 00056 * Encrypt a block. 00057 * @param in The plaintext block to be encrypted as a byte array. 00058 * Must be of length BLOCK_SIZE. 00059 * @param out The byte array designated to hold the encrypted block. 00060 * Must be of length BLOCK_SIZE. 00061 */ 00062 void encrypt(const byte in[], byte out[]) const 00063 { encrypt_n(in, out, 1); } 00064 00065 /** 00066 * Decrypt a block. 00067 * @param in The ciphertext block to be decypted as a byte array. 00068 * Must be of length BLOCK_SIZE. 00069 * @param out The byte array designated to hold the decrypted block. 00070 * Must be of length BLOCK_SIZE. 00071 */ 00072 void decrypt(const byte in[], byte out[]) const 00073 { decrypt_n(in, out, 1); } 00074 00075 /** 00076 * Encrypt a block. 00077 * @param block the plaintext block to be encrypted 00078 * Must be of length BLOCK_SIZE. Will hold the result when the function 00079 * has finished. 00080 */ 00081 void encrypt(byte block[]) const { encrypt_n(block, block, 1); } 00082 00083 /** 00084 * Decrypt a block. 00085 * @param block the ciphertext block to be decrypted 00086 * Must be of length BLOCK_SIZE. Will hold the result when the function 00087 * has finished. 00088 */ 00089 void decrypt(byte block[]) const { decrypt_n(block, block, 1); } 00090 00091 /** 00092 * Encrypt one or more blocks 00093 * @param in the input buffer (multiple of BLOCK_SIZE) 00094 * @param out the output buffer (same size as in) 00095 * @param blocks the number of blocks to process 00096 */ 00097 virtual void encrypt_n(const byte in[], byte out[], 00098 u32bit blocks) const = 0; 00099 00100 /** 00101 * Decrypt one or more blocks 00102 * @param in the input buffer (multiple of BLOCK_SIZE) 00103 * @param out the output buffer (same size as in) 00104 * @param blocks the number of blocks to process 00105 */ 00106 virtual void decrypt_n(const byte in[], byte out[], 00107 u32bit blocks) const = 0; 00108 00109 /** 00110 * Get a new object representing the same algorithm as *this 00111 */ 00112 virtual BlockCipher* clone() const = 0; 00113 00114 /** 00115 * Zeroize internal state 00116 */ 00117 virtual void clear() = 0; 00118 }; 00119 00120 } 00121 00122 #endif
1.5.8