/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 * * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#pragma once

#include <array>
#include <iostream>
#include <cstdint>
#include <vector>

namespace netplus {
    // Define DES constants and types
    using block64 = std::vector<uint8_t>; // 64-bit data block (8 bytes)
    using key56 = std::vector<uint8_t>;   // 56-bit effective key (8 bytes, where 8th bit is parity)

    /**
     * @brief Implements the Data Encryption Standard (DES) algorithm.
     */
    class des {
    public:
        /**
         * @brief Constructor for the DES class.
         * @param key The 64-bit key (only 56 bits are used, 8 bits are parity).
         */
        des(const block64& key);

        // Destructor
        ~des();

        /**
         * @brief Encrypts a 64-bit data block.
         * @param plaintext The 64-bit block to encrypt.
         * @return The 64-bit ciphertext block.
         */
        block64 encrypt(const block64& plaintext) const;

        /**
         * @brief Decrypts a 64-bit data block.
         * @param ciphertext The 64-bit block to decrypt.
         * @return The 64-bit plaintext block.
         */
        block64 decrypt(const block64& ciphertext) const;

    private:
        uint64_t m_masterKey;
        std::array<uint64_t, 16> m_roundKeys48{}; // Storage for 16 round keys (48-bit, stored as 6-byte block64)

        // --- Core DES Algorithm Steps ---
        void generateSubKeys();
        uint64_t initialPermutation(const block64& block) const;
        block64 finalPermutation(const uint64_t preoutput) const;
        block64 feistelFunction(const block64& rightHalf,uint64_t roundKey48) const;
    };

    class trippledes {
    public:
        trippledes(const block64& key1, const block64& key2, const block64& key3);
         /**
         * @brief Encrypts a 64-bit data block.
         * @param plaintext The 64-bit block to encrypt.
         * @return The 64-bit ciphertext block.
         */
        block64 encrypt(const block64& plaintext) const;

        /**
         * @brief Decrypts a 64-bit data block.
         * @param ciphertext The 64-bit block to decrypt.
         * @return The 64-bit plaintext block.
         */
        block64 decrypt(const block64& ciphertext) const;
    private:
        des m_des1; // DES instance for K1
        des m_des2; // DES instance for K2
        des m_des3; // DES instance for K3
    };

} // namespace netplus
