#ifndef INCLUDED_BOBCAT_ENCRYPTBUF_
#define INCLUDED_BOBCAT_ENCRYPTBUF_

// https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption

#include <iosfwd>
#include <string>

#include <bobcat/cryptbuf>
#include "../cryptbuf/cryptbuf"

namespace FBB
{

class IRandStream;

class EncryptBuf: public CryptBuf
{
    EVP_CIPHER_CTX     *d_ctx;

    std::string         d_encrypted;
    bool                d_eoi = false;  // eoi() was called

    std::string         d_iv;           // maybe initialize iv ourselves
    std::string         d_key;
    std::ostream       &d_outStream;

    public:
        EncryptBuf(std::ostream &outStream, char const *type, 
                   std::string key, std::string const &iv,
                   size_t bufSize = 1024);

        ~EncryptBuf() override;

        size_t keyLength() const;                   // bytes
        size_t blockLength() const;                 // bytes
        size_t ivLength() const;                    // bytes

        bool setRounds(size_t nRounds);             // RC5 8, 12 or 16
        size_t rounds() const;                      // RC5

        std::string const &iv() const;

        void eoi();                                 // .f

    protected:
        EVP_CIPHER_CTX *cipherCtx();

    private:
        int overflow(int ch) override;
        void eoi_() override;                       // .cc

        void prepareIV();
        void prepareKey();
        void setMD(char const *type);

        void flushBuffer();

        static void setChar(char &ch, IRandStream &irs);
};

#include "iv.f"
#include "eoi.f"


}   // FBB

/*
       EVP_enc_null()
           Null cipher: does nothing.

DES:
        Avoid, unless required, uses 8 byte buffer and 8 byte key
        EVP_des_cbc(), 
        EVP_des_ecb(), 
        EVP_des_cfb(),
        EVP_des_ofb()
            DES in CBC, ECB, CFB and OFB modes respectively.

3DES:
        Uses 8 byte buffer and 2x8 byte key for the two keys

        EVP_des_ede_cbc(), 
        EVP_des_ede(), 
        EVP_des_ede_ofb(),
        EVP_des_ede_cfb()
           Two key triple DES in CBC, ECB, CFB and OFB modes respectively.

3DES 3keys:
        Uses 8 byte buffer and 3x8 byte key for the 3 8 byte keys.

        EVP_des_ede3_cbc(), 
        EVP_des_ede3(), 
        EVP_des_ede3_ofb(),
        EVP_des_ede3_cfb()
           Three key triple DES in CBC, ECB, CFB and OFB modes respectively.

DESX:
        --> BOOK !!

       EVP_desx_cbc()
           DESX algorithm in CBC mode.

const EVP_CIPHER *EVP_des_ecb();
const EVP_CIPHER *EVP_des_ede();
const EVP_CIPHER *EVP_des_ede3();
const EVP_CIPHER *EVP_des_ede_ecb();
const EVP_CIPHER *EVP_des_ede3_ecb();
const EVP_CIPHER *EVP_des_cfb64();

# define EVP_des_cfb EVP_des_cfb64
const EVP_CIPHER *EVP_des_cfb1();
const EVP_CIPHER *EVP_des_cfb8();
const EVP_CIPHER *EVP_des_ede_cfb64();
# define EVP_des_ede_cfb EVP_des_ede_cfb64

#if 0
    const EVP_CIPHER *EVP_des_ede_cfb1();
    const EVP_CIPHER *EVP_des_ede_cfb8();
#endif

const EVP_CIPHER *EVP_des_ede3_cfb64();
# define EVP_des_ede3_cfb EVP_des_ede3_cfb64
const EVP_CIPHER *EVP_des_ede3_cfb1();
const EVP_CIPHER *EVP_des_ede3_cfb8();
const EVP_CIPHER *EVP_des_ofb();
const EVP_CIPHER *EVP_des_ede_ofb();
const EVP_CIPHER *EVP_des_ede3_ofb();
const EVP_CIPHER *EVP_des_cbc();
const EVP_CIPHER *EVP_des_ede_cbc();
const EVP_CIPHER *EVP_des_ede3_cbc();
const EVP_CIPHER *EVP_desx_cbc();



RC4:

       EVP_rc4()
           RC4 stream cipher. This is a variable key length cipher with
           default key length 128 bits.

#ifndef OPENSSL_NO_RC4
const EVP_CIPHER *EVP_rc4();
const EVP_CIPHER *EVP_rc4_40();
#endif


RC4_40:
       EVP_rc4_40()
           RC4 stream cipher with 40 bit key length. This is obsolete and new
           code should use EVP_rc4() and the EVP_CIPHER_CTX_set_key_length()
           function.

IDEA:
        EVP_idea_cbc(),
        EVP_idea_ecb(), 
        EVP_idea_cfb(),
        EVP_idea_ofb() 
           IDEA encryption algorithm in CBC, ECB, CFB and OFB modes
           respectively.

#ifndef OPENSSL_NO_IDEA
const EVP_CIPHER *EVP_idea_ecb();
const EVP_CIPHER *EVP_idea_cfb64();
# define EVP_idea_cfb EVP_idea_cfb64
const EVP_CIPHER *EVP_idea_ofb();
const EVP_CIPHER *EVP_idea_cbc();
#endif


RC2:
        EVP_rc2_cbc(), 
        EVP_rc2_ecb(), 
        EVP_rc2_cfb(),
        EVP_rc2_ofb()
           RC2 encryption algorithm in CBC, ECB, CFB and OFB modes
           respectively. This is a variable key length cipher with an
           additional parameter called "effective key bits" or "effective key
           length".  By default both are set to 128 bits.

#ifndef OPENSSL_NO_RC2
const EVP_CIPHER *EVP_rc2_ecb();
const EVP_CIPHER *EVP_rc2_cbc();
const EVP_CIPHER *EVP_rc2_40_cbc();
const EVP_CIPHER *EVP_rc2_64_cbc();
const EVP_CIPHER *EVP_rc2_cfb64();
# define EVP_rc2_cfb EVP_rc2_cfb64
const EVP_CIPHER *EVP_rc2_ofb();
#endif

RC2-40:
RC2-64:
       EVP_rc2_40_cbc(), EVP_rc2_64_cbc()
           RC2 algorithm in CBC mode with a default key length and effective
           key length of 40 and 64 bits.  These are obsolete and new code
           should use EVP_rc2_cbc(), EVP_CIPHER_CTX_set_key_length() and
           EVP_CIPHER_CTX_ctrl() to set the key length and effective key
           length.

BLOWFISH:
        EVP_bf_cbc(), 
        EVP_bf_ecb(), 
        EVP_bf_cfb(), 
        EVP_bf_ofb();
           Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes
           respectively. This is a variable key length cipher.

#ifndef OPENSSL_NO_BF
const EVP_CIPHER *EVP_bf_ecb();
const EVP_CIPHER *EVP_bf_cbc();
const EVP_CIPHER *EVP_bf_cfb64();
# define EVP_bf_cfb EVP_bf_cfb64
const EVP_CIPHER *EVP_bf_ofb();
#endif

CAST:
        EVP_cast5_cbc(), 
        EVP_cast5_ecb(), 
        EVP_cast5_cfb(),
        EVP_cast5_ofb()
            CAST encryption algorithm in CBC, ECB, CFB and OFB modes
            respectively. This is a variable key length cipher.

#ifndef OPENSSL_NO_CAST
const EVP_CIPHER *EVP_cast5_ecb();
const EVP_CIPHER *EVP_cast5_cbc();
const EVP_CIPHER *EVP_cast5_cfb64();
# define EVP_cast5_cfb EVP_cast5_cfb64
const EVP_CIPHER *EVP_cast5_ofb();
#endif


RC5:

       EVP_rc5_32_12_16_cbc(), EVP_rc5_32_12_16_ecb(),
       EVP_rc5_32_12_16_cfb(), EVP_rc5_32_12_16_ofb()
           RC5 encryption algorithm in CBC, ECB, CFB and OFB modes
           respectively. This is a variable key length cipher with an
           additional "number of rounds" parameter. By default the key length
           is set to 128 bits and 12 rounds.

#ifndef OPENSSL_NO_RC5
const EVP_CIPHER *EVP_rc5_32_12_16_cbc();
const EVP_CIPHER *EVP_rc5_32_12_16_ecb();
const EVP_CIPHER *EVP_rc5_32_12_16_cfb64();
# define EVP_rc5_32_12_16_cfb EVP_rc5_32_12_16_cfb64
const EVP_CIPHER *EVP_rc5_32_12_16_ofb();
#endif



#if 0
# ifdef OPENSSL_OPENBSD_DEV_CRYPTO
const EVP_CIPHER *EVP_dev_crypto_des_ede3_cbc();
const EVP_CIPHER *EVP_dev_crypto_rc4();
const EVP_MD *EVP_dev_crypto_md5();
# endif
#endif

#ifndef OPENSSL_NO_AES
const EVP_CIPHER *EVP_aes_128_ecb();
const EVP_CIPHER *EVP_aes_128_cbc();
const EVP_CIPHER *EVP_aes_128_cfb1();
const EVP_CIPHER *EVP_aes_128_cfb8();
const EVP_CIPHER *EVP_aes_128_cfb128();
# define EVP_aes_128_cfb EVP_aes_128_cfb128
const EVP_CIPHER *EVP_aes_128_ofb();

#if 0
    const EVP_CIPHER *EVP_aes_128_ctr();
#endif

const EVP_CIPHER *EVP_aes_192_ecb();
const EVP_CIPHER *EVP_aes_192_cbc();
const EVP_CIPHER *EVP_aes_192_cfb1();
const EVP_CIPHER *EVP_aes_192_cfb8();
const EVP_CIPHER *EVP_aes_192_cfb128();
# define EVP_aes_192_cfb EVP_aes_192_cfb128
const EVP_CIPHER *EVP_aes_192_ofb();

#if 0
    const EVP_CIPHER *EVP_aes_192_ctr();
#endif

const EVP_CIPHER *EVP_aes_256_ecb();
const EVP_CIPHER *EVP_aes_256_cbc();
const EVP_CIPHER *EVP_aes_256_cfb1();
const EVP_CIPHER *EVP_aes_256_cfb8();
const EVP_CIPHER *EVP_aes_256_cfb128();
# define EVP_aes_256_cfb EVP_aes_256_cfb128
const EVP_CIPHER *EVP_aes_256_ofb();

    const EVP_CIPHER *EVP_aes_256_ctr();

In cryptography, Camellia is a block cipher that has been evaluated favorably
by several organisations, including the European Union's NESSIE project (a
selected algorithm), and the Japanese CRYPTREC project (a recommended
algorithm). The cipher was developed jointly by Mitsubishi and NTT in 2000,
and has similar design elements to earlier block ciphers (MISTY1 and E2) from
these companies.

Camellia has a block size of 128 bits, and can use 128-bit, 192-bit or 256-bit
keys - the same interface as the Advanced Encryption Standard. It is a
Feistel cipher with either 18 rounds (if the key is 128 bits) or 24 rounds (if
the key is 192 or 256 bits). Every six rounds, a logical transformation layer
is applied: the so-called "FL-function" or its inverse. Camellia uses four 8 x
8-bit S-boxes with input and output affine transformations and logical
operations. The cipher also uses input and output key whitening. The diffusion
layer uses a linear transformation based on an MDS matrix with a branch number
of 5.

On June, 18 2008, support for the adopted Camellia cipher was added to the
final release of Mozilla Firefox 3.[citation needed]

http://en.wikipedia.org/wiki/Camellia_(cipher)


    const EVP_CIPHER *EVP_camellia_128_ecb();
    const EVP_CIPHER *EVP_camellia_128_cbc();
    const EVP_CIPHER *EVP_camellia_128_cfb1();
    const EVP_CIPHER *EVP_camellia_128_cfb8();
    const EVP_CIPHER *EVP_camellia_128_cfb128();
    # define EVP_camellia_128_cfb EVP_camellia_128_cfb128
    const EVP_CIPHER *EVP_camellia_128_ofb();
    const EVP_CIPHER *EVP_camellia_192_ecb();
    const EVP_CIPHER *EVP_camellia_192_cbc();
    const EVP_CIPHER *EVP_camellia_192_cfb1();
    const EVP_CIPHER *EVP_camellia_192_cfb8();
    const EVP_CIPHER *EVP_camellia_192_cfb128();
    # define EVP_camellia_192_cfb EVP_camellia_192_cfb128
    const EVP_CIPHER *EVP_camellia_192_ofb();
    const EVP_CIPHER *EVP_camellia_256_ecb();
    const EVP_CIPHER *EVP_camellia_256_cbc();
    const EVP_CIPHER *EVP_camellia_256_cfb1();
    const EVP_CIPHER *EVP_camellia_256_cfb8();
    const EVP_CIPHER *EVP_camellia_256_cfb128();
    # define EVP_camellia_256_cfb EVP_camellia_256_cfb128
    const EVP_CIPHER *EVP_camellia_256_ofb();

#ifndef OPENSSL_NO_SEED
const EVP_CIPHER *EVP_seed_ecb();
const EVP_CIPHER *EVP_seed_cbc();
const EVP_CIPHER *EVP_seed_cfb128();
# define EVP_seed_cfb EVP_seed_cfb128
const EVP_CIPHER *EVP_seed_ofb();
#endif

*/
        
#endif



