// ---------------------------------------------------------------------------
// - BlockCipher.hpp                                                         -
// - afnix:sec module - block 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_BLOCKCIPHER_HPP
#define  AFNIX_BLOCKCIPHER_HPP

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

namespace afnix {

  /// The BlockCipher class is an abstract class that is used to implement 
  /// a symmetric block cipher. By default the cipher operates in encryption
  /// mode. When the reverse flag is set, the decryption mode is activated.
  /// For a block cipher, a block size controls the cipher operations. The
  /// class also provides the mechanism to pad a block. The default padding
  /// mode is the NIST 800-38A mode.
  /// @author amaury darsch

  class BlockCipher : public Cipher {
  public:
    /// padding mode
    enum t_pmod {
      PAD_NONE, // no padding
      PAD_BITM, // bit padding
      PAD_X923, // ANSI X.923
      PAD_N800  // NIST 800-38A, PKCS#7, RFC 2130 padding mode
    };

  protected:
    /// the cipher block size
    long d_cbsz;
    /// the cipher padding mode
    t_pmod d_pmod;

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

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

    /// set the block 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 cipher block size
    virtual long getcbsz (void) const;

    /// set the block cipher padding mode
    /// @param pmod the padding mode to set
    virtual void setpmod (const t_pmod pmod);

    /// @return the padding mode
    virtual t_pmod getpmod (void) const;

    /// get the normal waist from a file size
    /// @param size the size to normalize
    virtual t_long waist (const t_long size) const;

    /// generate a block padding if any
    /// @param bi    the block buffer
    /// @param size  the block size
    /// @return true if the block has been padded
    virtual bool mkpad (t_byte* bi, const long size) const;

    /// generate the next padded block if any
    /// @param bi the block buffer
    /// @return true if the block has been padded
    virtual bool mknxp (t_byte* bi) const;

    /// get the number of unpadded bytes
    /// @param bo   the block buffer
    /// @param size the block size
    /// @return the number of valid bytes
    virtual long getub (const t_byte* bo) const;

    /// encode a block buffer into another one
    /// @param bo the output buffer
    /// @param bi the input buffer
    virtual void encode (t_byte* bo, const t_byte* bi) =0;

    /// decode a block buffer into another one
    /// @param bo the output buffer
    /// @param bi the input buffer
    virtual void decode (t_byte* bo, const t_byte* bi) =0;

    /// process a block buffer into another one
    /// @param bo the output buffer
    /// @param bi the input buffer
    virtual void process (t_byte* bo, const t_byte* bi);

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

  public:
    /// evaluate an object data member
    /// @param robj  robj the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to evaluate
    static Object* meval (Runnable* robj, Nameset* nset, const long quark);

    /// @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
