Botan  1.11.15
src/lib/base/buf_comp.h
Go to the documentation of this file.
00001 /*
00002 * Buffered Computation
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_BUFFERED_COMPUTATION_H__
00009 #define BOTAN_BUFFERED_COMPUTATION_H__
00010 
00011 #include <botan/secmem.h>
00012 #include <botan/get_byte.h>
00013 #include <string>
00014 
00015 namespace Botan {
00016 
00017 /**
00018 * This class represents any kind of computation which uses an internal
00019 * state, such as hash functions or MACs
00020 */
00021 class BOTAN_DLL Buffered_Computation
00022    {
00023    public:
00024       /**
00025       * @return length of the output of this function in bytes
00026       */
00027       virtual size_t output_length() const = 0;
00028 
00029       /**
00030       * Add new input to process.
00031       * @param in the input to process as a byte array
00032       * @param length of param in in bytes
00033       */
00034       void update(const byte in[], size_t length) { add_data(in, length); }
00035 
00036       /**
00037       * Add new input to process.
00038       * @param in the input to process as a secure_vector
00039       */
00040       void update(const secure_vector<byte>& in)
00041          {
00042          add_data(&in[0], in.size());
00043          }
00044 
00045       /**
00046       * Add new input to process.
00047       * @param in the input to process as a std::vector
00048       */
00049       void update(const std::vector<byte>& in)
00050          {
00051          add_data(&in[0], in.size());
00052          }
00053 
00054       /**
00055       * Add an integer in big-endian order
00056       * @param in the value
00057       */
00058       template<typename T> void update_be(const T in)
00059          {
00060          for(size_t i = 0; i != sizeof(T); ++i)
00061             {
00062             byte b = get_byte(i, in);
00063             add_data(&b, 1);
00064             }
00065          }
00066 
00067       /**
00068       * Add new input to process.
00069       * @param str the input to process as a std::string. Will be interpreted
00070       * as a byte array based on
00071       * the strings encoding.
00072       */
00073       void update(const std::string& str)
00074          {
00075          add_data(reinterpret_cast<const byte*>(str.data()), str.size());
00076          }
00077 
00078       /**
00079       * Process a single byte.
00080       * @param in the byte to process
00081       */
00082       void update(byte in) { add_data(&in, 1); }
00083 
00084       /**
00085       * Complete the computation and retrieve the
00086       * final result.
00087       * @param out The byte array to be filled with the result.
00088       * Must be of length output_length()
00089       */
00090       void final(byte out[]) { final_result(out); }
00091 
00092       /**
00093       * Complete the computation and retrieve the
00094       * final result.
00095       * @return secure_vector holding the result
00096       */
00097       secure_vector<byte> final()
00098          {
00099          secure_vector<byte> output(output_length());
00100          final_result(&output[0]);
00101          return output;
00102          }
00103 
00104       template<typename Alloc>
00105          void final(std::vector<byte, Alloc>& out)
00106          {
00107          out.resize(output_length());
00108          final_result(&out[0]);
00109          }
00110 
00111       /**
00112       * Update and finalize computation. Does the same as calling update()
00113       * and final() consecutively.
00114       * @param in the input to process as a byte array
00115       * @param length the length of the byte array
00116       * @result the result of the call to final()
00117       */
00118       secure_vector<byte> process(const byte in[], size_t length)
00119          {
00120          add_data(in, length);
00121          return final();
00122          }
00123 
00124       /**
00125       * Update and finalize computation. Does the same as calling update()
00126       * and final() consecutively.
00127       * @param in the input to process
00128       * @result the result of the call to final()
00129       */
00130       secure_vector<byte> process(const secure_vector<byte>& in)
00131          {
00132          add_data(&in[0], in.size());
00133          return final();
00134          }
00135 
00136       /**
00137       * Update and finalize computation. Does the same as calling update()
00138       * and final() consecutively.
00139       * @param in the input to process
00140       * @result the result of the call to final()
00141       */
00142       secure_vector<byte> process(const std::vector<byte>& in)
00143          {
00144          add_data(&in[0], in.size());
00145          return final();
00146          }
00147 
00148       /**
00149       * Update and finalize computation. Does the same as calling update()
00150       * and final() consecutively.
00151       * @param in the input to process as a string
00152       * @result the result of the call to final()
00153       */
00154       secure_vector<byte> process(const std::string& in)
00155          {
00156          update(in);
00157          return final();
00158          }
00159 
00160       virtual ~Buffered_Computation() {}
00161    private:
00162       /**
00163       * Add more data to the computation
00164       * @param input is an input buffer
00165       * @param length is the length of input in bytes
00166       */
00167       virtual void add_data(const byte input[], size_t length) = 0;
00168 
00169       /**
00170       * Write the final output to out
00171       * @param out is an output buffer of output_length()
00172       */
00173       virtual void final_result(byte out[]) = 0;
00174    };
00175 
00176 }
00177 
00178 #endif