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

#include "parameters.h"
#include "server.cpp"
#include "rsa.cpp"
#include "subject.cpp"
#include "zemd_attacker.cpp"
#include "zemd_attacker_plain.cpp"
#include "cca_attacker.cpp"
#include "measuringSubject.cpp"

int sc_main(int argc, char* argv[])
{
  sc_fifo<bigint> subjectFifo;
  sc_fifo<bigint> secondFifo;

   sc_signal<bool> cipherProceededSig;


   
  /* Plain RSA-crypted communication */
  if(argc == 1){

    Server server("Server", 1000);
    Subject subject("Subject", -1);
    
    subject.out(secondFifo);
    subject.in(subjectFifo);
    subject.cipherProceeded_out(cipherProceededSig);
        
    server.out(subjectFifo);
    server.in(secondFifo);
    server.cipherProceeded_in(cipherProceededSig);

    sc_start();  
  /* ZEMD Attack */
  }else if(!strcmp(argv[1], "ZEMD")){
    int msgCount = 200;
    if(argc == 3){
      msgCount = atoi(argv[2]);
    }
    ZEMD_attacker_plain attacker("Attacker",msgCount);
    Subject subject("Subject", 0);
   
    subject.out(secondFifo);
    subject.in(subjectFifo);
    subject.cipherProceeded_out(cipherProceededSig);
    
    attacker.out(subjectFifo);
    attacker.in(secondFifo);
    attacker.cipherProceeded_in(cipherProceededSig);

   sc_start(); 
  /* tweaked ZEMD Attack */
  }else if(!strcmp(argv[1], "tweakedZEMD")){
    int msgCount = 200;
    int runsPerBit = 3;
    if(argc >= 3){
      msgCount = atoi(argv[2]);
    }
    if(argc == 4){
       runsPerBit = atoi(argv[3]);
    }
    ZEMD_Attacker attacker("Attacker",msgCount, runsPerBit);
    Subject subject("Subject", 0);
   
    subject.out(secondFifo);
    subject.in(subjectFifo);
    subject.cipherProceeded_out(cipherProceededSig);
    attacker.out(subjectFifo);
    attacker.in(secondFifo);
    attacker.cipherProceeded_in(cipherProceededSig);

   sc_start(); 
  /* CCA Attack */
  }else if(!strcmp(argv[1], "CCA")){
    int msgCount = 500;
    if(argc == 3){
      msgCount = atoi(argv[2]);
    }
    CCA_Attacker attacker("Attacker", msgCount);
    Subject subject("Subject", 1);
   
    subject.out(secondFifo);
    subject.in(subjectFifo);
    subject.cipherProceeded_out(cipherProceededSig);

    attacker.out(subjectFifo);
    attacker.in(secondFifo);
    attacker.cipherProceeded_in(cipherProceededSig);

    sc_start(); 
  }else if(!strcmp(argv[1], "powerHWMeasurement")){
  
    measuringSubject mSubject("measuringSubject");
    sc_start();  
  }
    
  RessourceHandler::getInstance()->logToFile();
  return 0;
}