/*****************************************************************************
 *
 * Copyright (c) 2018-19, Josef Treus
 * Westfälische Wilhelms-Universität Münster, AG Embedded Systems
 * All rights reserved.
 *
 * This file is part of SysCSec
 *
 * SysCSec is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * SysCSec 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.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SysCSec.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  Please report any problems or bugs to: sysc.sec@uni-muenster.de
 *
 ****************************************************************************/

#ifndef SUBJECT_CPP
#define SUBJECT_CPP

#include "rsa.cpp"
#include "parameters.h"
#include "Utils.cpp"
#include <chrono>

/* The subject generates a private and a public key using the RSA library and sends the public key to the server.
 * Then, it waits for data to be decrypted.
 * Param: sc_module_name name : name of the subject
 *        int kind : kind of encryption (0 = Plain SQ & MP; 1 = Always MP)
*/

SC_MODULE(Subject)   
{
    sc_fifo_in<bigint> in;
    sc_fifo_out<bigint> out;  
    sc_out<bool> cipherProceeded_out;

    bool kindOfDecipherAlgorithm = false;
    bool printDec = false;
    bigint e;
    bigint d;
    bigint n;

    /* Executed at startup. Executed RSA algorithm and  public key.
     * Wartet dann auf Input zum entschlüsseln.
     * Param: -
     * Return: -
     */
    inline void subject_main(){
        bigint keySet[3];
        int time = 0;
        /* generate RSA keys until d and n are equal
         * (prerequisite for a ZEMD attack)
         */
        do{
            time = std::chrono::system_clock::now().time_since_epoch().count();
            default_random_engine generator (time);
            uniform_real_distribution<double> dist (0, 99999999999);
            int seed = dist(generator);
            rsa_generate_keys(seed, keySet);
        }while(Utils::unsignificantBitCount(keySet[0].to_int(),NBITS) != Utils::unsignificantBitCount(keySet[2].to_int(),NBITS));
        
        
        this->d = keySet[0];
        this->e = keySet[1];
        this->n = keySet[2];
        // public key is distributed. hopefully no evil attackers are eavesdropping ^^'
        out->write(e);
        out->write(n);
        
        int i = 0;
        /* as soon as new data is arrived, it is decrypted
         */
        while(true){
            wait(in->data_written_event());
            bigint msg = in->read();
            if(printDec){
                cout << "decrypted: " << decipher(kindOfDecipherAlgorithm,true, msg, d, n) << endl;
            }else{
                decipher(kindOfDecipherAlgorithm,true, msg, d, n);
            }
            cipherProceeded_out.write((i%2 == 0)?true:false);
            i++;
        }
    }
    SC_HAS_PROCESS(Subject);
	Subject(sc_module_name name, int kind): sc_module(name), cipherProceeded_out("cPi"), in("in"), out("out"){
        if(kind == -1){
            this->printDec = true;
        }else if(kind == 1){
            this->kindOfDecipherAlgorithm = true;
        }
        
        SC_THREAD(subject_main);
    }
};

#endif

