Botan
1.11.15
|
00001 /* 00002 * Filters 00003 * (C) 1999-2007 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_FILTERS_H__ 00009 #define BOTAN_FILTERS_H__ 00010 00011 #include <botan/block_cipher.h> 00012 #include <botan/stream_cipher.h> 00013 #include <botan/hash.h> 00014 #include <botan/mac.h> 00015 00016 #include <botan/pipe.h> 00017 #include <botan/basefilt.h> 00018 #include <botan/key_filt.h> 00019 #include <botan/data_snk.h> 00020 00021 #include <botan/scan_name.h> 00022 00023 #if defined(BOTAN_HAS_CODEC_FILTERS) 00024 #include <botan/b64_filt.h> 00025 #include <botan/hex_filt.h> 00026 #endif 00027 00028 namespace Botan { 00029 00030 /** 00031 * Stream Cipher Filter 00032 */ 00033 class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter 00034 { 00035 public: 00036 00037 std::string name() const { return m_cipher->name(); } 00038 00039 /** 00040 * Write input data 00041 * @param input data 00042 * @param input_len length of input in bytes 00043 */ 00044 void write(const byte input[], size_t input_len); 00045 00046 bool valid_iv_length(size_t iv_len) const 00047 { return m_cipher->valid_iv_length(iv_len); } 00048 00049 /** 00050 * Set the initialization vector for this filter. 00051 * @param iv the initialization vector to set 00052 */ 00053 void set_iv(const InitializationVector& iv); 00054 00055 /** 00056 * Set the key of this filter. 00057 * @param key the key to set 00058 */ 00059 void set_key(const SymmetricKey& key) { m_cipher->set_key(key); } 00060 00061 Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); } 00062 00063 /** 00064 * Construct a stream cipher filter. 00065 * @param cipher a cipher object to use 00066 */ 00067 StreamCipher_Filter(StreamCipher* cipher); 00068 00069 /** 00070 * Construct a stream cipher filter. 00071 * @param cipher a cipher object to use 00072 * @param key the key to use inside this filter 00073 */ 00074 StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key); 00075 00076 /** 00077 * Construct a stream cipher filter. 00078 * @param cipher the name of the desired cipher 00079 */ 00080 StreamCipher_Filter(const std::string& cipher); 00081 00082 /** 00083 * Construct a stream cipher filter. 00084 * @param cipher the name of the desired cipher 00085 * @param key the key to use inside this filter 00086 */ 00087 StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key); 00088 private: 00089 secure_vector<byte> buffer; 00090 std::unique_ptr<StreamCipher> m_cipher; 00091 }; 00092 00093 /** 00094 * Hash Filter. 00095 */ 00096 class BOTAN_DLL Hash_Filter : public Filter 00097 { 00098 public: 00099 void write(const byte input[], size_t len) { m_hash->update(input, len); } 00100 void end_msg(); 00101 00102 std::string name() const { return m_hash->name(); } 00103 00104 /** 00105 * Construct a hash filter. 00106 * @param hash_fun the hash function to use 00107 * @param len the output length of this filter. Leave the default 00108 * value 0 if you want to use the full output of the hashfunction 00109 * hash. Otherwise, specify a smaller value here so that the 00110 * output of the hash algorithm will be cut off. 00111 */ 00112 Hash_Filter(HashFunction* hash, size_t len = 0) : 00113 OUTPUT_LENGTH(len), m_hash(hash) {} 00114 00115 /** 00116 * Construct a hash filter. 00117 * @param request the name of the hash algorithm to use 00118 * @param len the output length of this filter. Leave the default 00119 * value 0 if you want to use the full output of the hashfunction 00120 * hash. Otherwise, specify a smaller value here so that the 00121 * output of the hash algorithm will be cut off. 00122 */ 00123 Hash_Filter(const std::string& request, size_t len = 0); 00124 00125 private: 00126 const size_t OUTPUT_LENGTH; 00127 std::unique_ptr<HashFunction> m_hash; 00128 }; 00129 00130 /** 00131 * MessageAuthenticationCode Filter. 00132 */ 00133 class BOTAN_DLL MAC_Filter : public Keyed_Filter 00134 { 00135 public: 00136 void write(const byte input[], size_t len) { m_mac->update(input, len); } 00137 void end_msg(); 00138 00139 std::string name() const { return m_mac->name(); } 00140 00141 /** 00142 * Set the key of this filter. 00143 * @param key the key to set 00144 */ 00145 void set_key(const SymmetricKey& key) { m_mac->set_key(key); } 00146 00147 Key_Length_Specification key_spec() const override { return m_mac->key_spec(); } 00148 00149 /** 00150 * Construct a MAC filter. The MAC key will be left empty. 00151 * @param mac the MAC to use 00152 * @param out_len the output length of this filter. Leave the default 00153 * value 0 if you want to use the full output of the 00154 * MAC. Otherwise, specify a smaller value here so that the 00155 * output of the MAC will be cut off. 00156 */ 00157 MAC_Filter(MessageAuthenticationCode* mac, 00158 size_t out_len = 0) : 00159 OUTPUT_LENGTH(out_len), 00160 m_mac(mac) 00161 { 00162 } 00163 00164 /** 00165 * Construct a MAC filter. 00166 * @param mac the MAC to use 00167 * @param key the MAC key to use 00168 * @param out_len the output length of this filter. Leave the default 00169 * value 0 if you want to use the full output of the 00170 * MAC. Otherwise, specify a smaller value here so that the 00171 * output of the MAC will be cut off. 00172 */ 00173 MAC_Filter(MessageAuthenticationCode* mac, 00174 const SymmetricKey& key, 00175 size_t out_len = 0) : 00176 OUTPUT_LENGTH(out_len), 00177 m_mac(mac) 00178 { 00179 m_mac->set_key(key); 00180 } 00181 00182 /** 00183 * Construct a MAC filter. The MAC key will be left empty. 00184 * @param mac the name of the MAC to use 00185 * @param len the output length of this filter. Leave the default 00186 * value 0 if you want to use the full output of the 00187 * MAC. Otherwise, specify a smaller value here so that the 00188 * output of the MAC will be cut off. 00189 */ 00190 MAC_Filter(const std::string& mac, size_t len = 0); 00191 00192 /** 00193 * Construct a MAC filter. 00194 * @param mac the name of the MAC to use 00195 * @param key the MAC key to use 00196 * @param len the output length of this filter. Leave the default 00197 * value 0 if you want to use the full output of the 00198 * MAC. Otherwise, specify a smaller value here so that the 00199 * output of the MAC will be cut off. 00200 */ 00201 MAC_Filter(const std::string& mac, const SymmetricKey& key, 00202 size_t len = 0); 00203 private: 00204 const size_t OUTPUT_LENGTH; 00205 std::unique_ptr<MessageAuthenticationCode> m_mac; 00206 }; 00207 00208 } 00209 00210 #endif