1 /* 2 * Block Cipher Base Class 3 * (C) 1999-2009 Jack Lloyd 4 * 5 * Distributed under the terms of the Botan license 6 */ 7 8 #ifndef BOTAN_BLOCK_CIPHER_H__ 9 #define BOTAN_BLOCK_CIPHER_H__ 10 11 #include <botan/sym_algo.h> 12 13 namespace Botan { 14 15 /** 16 * This class represents a block cipher object. 17 */ 18 class BOTAN_DLL BlockCipher : public SymmetricAlgorithm 19 { 20 public: 21 22 /** 23 * @return block size of this algorithm 24 */ 25 virtual size_t block_size() const = 0; 26 27 /** 28 * @return native parallelism of this cipher in blocks 29 */ parallelism()30 virtual size_t parallelism() const { return 1; } 31 32 /** 33 * @return prefererred parallelism of this cipher in bytes 34 */ parallel_bytes()35 size_t parallel_bytes() const 36 { 37 return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT; 38 } 39 40 /** 41 * Encrypt a block. 42 * @param in The plaintext block to be encrypted as a byte array. 43 * Must be of length block_size(). 44 * @param out The byte array designated to hold the encrypted block. 45 * Must be of length block_size(). 46 */ encrypt(const byte in[],byte out[])47 void encrypt(const byte in[], byte out[]) const 48 { encrypt_n(in, out, 1); } 49 50 /** 51 * Decrypt a block. 52 * @param in The ciphertext block to be decypted as a byte array. 53 * Must be of length block_size(). 54 * @param out The byte array designated to hold the decrypted block. 55 * Must be of length block_size(). 56 */ decrypt(const byte in[],byte out[])57 void decrypt(const byte in[], byte out[]) const 58 { decrypt_n(in, out, 1); } 59 60 /** 61 * Encrypt a block. 62 * @param block the plaintext block to be encrypted 63 * Must be of length block_size(). Will hold the result when the function 64 * has finished. 65 */ encrypt(byte block[])66 void encrypt(byte block[]) const { encrypt_n(block, block, 1); } 67 68 /** 69 * Decrypt a block. 70 * @param block the ciphertext block to be decrypted 71 * Must be of length block_size(). Will hold the result when the function 72 * has finished. 73 */ decrypt(byte block[])74 void decrypt(byte block[]) const { decrypt_n(block, block, 1); } 75 76 /** 77 * Encrypt one or more blocks 78 * @param in the input buffer (multiple of block_size()) 79 * @param out the output buffer (same size as in) 80 * @param blocks the number of blocks to process 81 */ 82 virtual void encrypt_n(const byte in[], byte out[], 83 size_t blocks) const = 0; 84 85 /** 86 * Decrypt one or more blocks 87 * @param in the input buffer (multiple of block_size()) 88 * @param out the output buffer (same size as in) 89 * @param blocks the number of blocks to process 90 */ 91 virtual void decrypt_n(const byte in[], byte out[], 92 size_t blocks) const = 0; 93 94 /** 95 * Get a new object representing the same algorithm as *this 96 */ 97 virtual BlockCipher* clone() const = 0; 98 }; 99 100 /** 101 * Represents a block cipher with a single fixed block size 102 */ 103 template<size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1> 104 class Block_Cipher_Fixed_Params : public BlockCipher 105 { 106 public: 107 enum { BLOCK_SIZE = BS }; block_size()108 size_t block_size() const { return BS; } 109 key_spec()110 Key_Length_Specification key_spec() const 111 { 112 return Key_Length_Specification(KMIN, KMAX, KMOD); 113 } 114 }; 115 116 } 117 118 #endif 119