Botan  1.11.15
src/lib/filters/algo_filt.cpp
Go to the documentation of this file.
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 #include <botan/filters.h>
00009 #include <botan/internal/algo_registry.h>
00010 #include <algorithm>
00011 
00012 namespace Botan {
00013 
00014 /*
00015 * StreamCipher_Filter Constructor
00016 */
00017 StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher) :
00018    buffer(DEFAULT_BUFFERSIZE), m_cipher(cipher)
00019    {
00020    }
00021 
00022 /*
00023 * StreamCipher_Filter Constructor
00024 */
00025 StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher,
00026                                          const SymmetricKey& key) :
00027    buffer(DEFAULT_BUFFERSIZE),
00028    m_cipher(cipher)
00029    {
00030    m_cipher->set_key(key);
00031    }
00032 
00033 /*
00034 * StreamCipher_Filter Constructor
00035 */
00036 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) :
00037    buffer(DEFAULT_BUFFERSIZE)
00038    {
00039    m_cipher.reset(Algo_Registry<StreamCipher>::global_registry().make(sc_name));
00040    }
00041 
00042 /*
00043 * StreamCipher_Filter Constructor
00044 */
00045 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name,
00046                                          const SymmetricKey& key) :
00047    buffer(DEFAULT_BUFFERSIZE)
00048    {
00049    m_cipher.reset(Algo_Registry<StreamCipher>::global_registry().make(sc_name));
00050    m_cipher->set_key(key);
00051    }
00052 
00053 /*
00054 * Set the IV of a stream cipher
00055 */
00056 void StreamCipher_Filter::set_iv(const InitializationVector& iv)
00057    {
00058    m_cipher->set_iv(iv.begin(), iv.length());
00059    }
00060 
00061 /*
00062 * Write data into a StreamCipher_Filter
00063 */
00064 void StreamCipher_Filter::write(const byte input[], size_t length)
00065    {
00066    while(length)
00067       {
00068       size_t copied = std::min<size_t>(length, buffer.size());
00069       m_cipher->cipher(input, &buffer[0], copied);
00070       send(buffer, copied);
00071       input += copied;
00072       length -= copied;
00073       }
00074    }
00075 
00076 /*
00077 * Hash_Filter Constructor
00078 */
00079 Hash_Filter::Hash_Filter(const std::string& algo_spec,
00080                          size_t len) :
00081    OUTPUT_LENGTH(len)
00082    {
00083    m_hash.reset(Algo_Registry<HashFunction>::global_registry().make(algo_spec));
00084    }
00085 
00086 /*
00087 * Complete a calculation by a Hash_Filter
00088 */
00089 void Hash_Filter::end_msg()
00090    {
00091    secure_vector<byte> output = m_hash->final();
00092    if(OUTPUT_LENGTH)
00093       send(output, std::min<size_t>(OUTPUT_LENGTH, output.size()));
00094    else
00095       send(output);
00096    }
00097 
00098 /*
00099 * MAC_Filter Constructor
00100 */
00101 MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) :
00102    OUTPUT_LENGTH(len)
00103    {
00104    m_mac.reset(Algo_Registry<MessageAuthenticationCode>::global_registry().make(mac_name));
00105    }
00106 
00107 /*
00108 * MAC_Filter Constructor
00109 */
00110 MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key,
00111                        size_t len) : OUTPUT_LENGTH(len)
00112    {
00113    m_mac.reset(Algo_Registry<MessageAuthenticationCode>::global_registry().make(mac_name));
00114    m_mac->set_key(key);
00115    }
00116 
00117 /*
00118 * Complete a calculation by a MAC_Filter
00119 */
00120 void MAC_Filter::end_msg()
00121    {
00122    secure_vector<byte> output = m_mac->final();
00123    if(OUTPUT_LENGTH)
00124       send(output, std::min<size_t>(OUTPUT_LENGTH, output.size()));
00125    else
00126       send(output);
00127    }
00128 
00129 }