// ---------------------------------------------------------------------------
// - PublicCipher.hpp                                                        -
// - afnix:sec module - public cipher class definition                       -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2008 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_PUBLICCIPHER_HPP
#define  AFNIX_PUBLICCIPHER_HPP

#ifndef  AFNIX_CIPHER_HPP
#include "Cipher.hpp"
#endif

namespace afnix {

  /// The PublicCipher class is an abstract class that is used to implement 
  /// an asymmetric cipher. An asymmetric cipher or public key cipher is
  /// designed to operate with a public key and a secret key. Depending
  /// on the use model, the public key might be used to crypt the data, and
  /// the secret key to decrypt. The basic assumption around a public cipher
  /// is that the secret key cannot be derived from the public key.
  /// @author amaury darsch

  class PublicCipher : public Cipher {
  protected:
    /// the message block size
    long d_mbsz;
    /// the crypted block size
    long d_cbsz;

  public:
    /// create a cipher by name and key
    /// @param name the cipher name
    /// @param key  the cipher key
    PublicCipher (const String& name, const Key& key);

    /// @return the class name
    String repr (void) const;

    /// set the public cipher key
    /// @param key the key to set
    void setkey (const Key& key);

    /// stream an input buffer into an output buffer
    /// @param ob the output buffer to write
    /// @param ib the input  buffer to read
    /// @return the number of processed bytes
    t_long stream (Buffer& ob , Buffer& ib);

    /// stream an input stream into an output stream
    /// @param os the output stream to write
    /// @param is the input stream to read
    /// @return the number of processed bytes
    t_long stream (Output& os , Input& is);

    /// @return the message block size
    virtual long getmbsz (void) const;

    /// @return the crypted block size
    virtual long getcbsz (void) const;

    /// process a message buffer into a block buffer
    /// @param bo   the output buffer
    /// @param bi   the input buffer
    /// @param size the input buffer size
    virtual long process (t_byte* bo, const t_byte* bi, const long size) =0;

  private:
    // make the copy constructor private
    PublicCipher (const PublicCipher&);
    // make the assignment operator private
    PublicCipher& operator = (const PublicCipher&);

  public:
    /// @return true if the given quark is defined
    bool isquark (const long quark, const bool hflg) const;

    /// apply thsi object with a set of arguments and a quark
    /// @param robj  the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to apply these arguments
    /// @param argv  the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
		   Vector* argv);
  };
}

#endif
