/**
 *--------------------------------------------------------------------\n
 *          HSLU T&A Hochschule Luzern Technik+Architektur            \n
 *--------------------------------------------------------------------\n
 *
 * \brief         ASYD assignment crypto 04
 * \file
 * \author        Basil Estermann, basil.estermann@stud.hslu.ch
 *                Simon Frei, simon.frei@stud.hslu.ch
 *                Jonas Arnold, jonas.arnold@stud.hslu.ch
 * \date          16.03.2023
 *
 *--------------------------------------------------------------------
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "encryptionArithmetic.h"
#include "DSA.h"

#define KEY_LENGTH 16
#define DIVISOR 160

int generateDsaKey(void);
int calculateSessionKey(void);
void printLargeNumberLine(char* descriptor, t_encryptionArithmetic* number, uint16_t size);

void main(void) {

    int result = 0;

    printf("------ Generate DSA Key ------\n\n");
    result = generateDsaKey();
    if (result != 0) {
        return result;
    }

    /*printf("------ Calculate Sessionkey ------\n\n");
    result = calculateSessionKey();
    if (result != 0) {
        return result;
    }*/

    return 0;
}

int generateDsaKey(void) {
    /*const char* p_string = "C8462B074900AD6622E5FE3F945BD2C6D570FAFF63BDCF4287AAC84DEF28BA0D7BF3EF9D3FF4A5D0821BDB1FBEED32CA5DE56B1832B648F40C5C205EA8694F828DF9C1583E284AC773F595809E442BB7D5E256B1C68E7E80C782D9469D351D5E9C434A2CBD2A1AB4527B11B405128A2146F12882742A732EADE720ECE2BC1B4D";
    const char* q_string = "810E78D2D6C8B0BB8A15FFEA19EBED45D6CB0495";
    const char* d_string = "9778729279583412";*/
    const char* p_string = "3B";
    const char* q_string = "1D";
    const char* d_string = "10";

    // P has to be a prime number!
    t_encryptionArithmetic p;
    encryptionArithmetic_Init(&p, KEY_LENGTH);
    if (encryptionArithmetic_stringToHex(p_string, p.number, KEY_LENGTH) == false) {
        printf("ERROR: Creating p.");
        return 1000000;
    }

    t_encryptionArithmetic q;
    encryptionArithmetic_Init(&q, KEY_LENGTH);
    if (encryptionArithmetic_stringToHex(q_string, q.number, KEY_LENGTH) == false) {
        printf("ERROR: Creating q.");
        return 1000001;
    }

    // ord(a) = k = q
    t_encryptionArithmetic alpha_k;
    encryptionArithmetic_Init(&alpha_k, KEY_LENGTH);
    t_encryptionArithmetic alpha;
    encryptionArithmetic_Init(&alpha, 64);
    const char* alpha_string = "20000000";
    if (encryptionArithmetic_stringToHex(alpha_string, alpha.number, 64) == false) {
        printf("ERROR: Creating q.");
        return 1000001;
    }
    moduloOperation(&alpha, &q, KEY_LENGTH);
    //for (*alpha.number = 2; *alpha.number < *p.number; *alpha.number = *alpha.number + 1) {
    ////for(uint32_t i = 2; i < UINT32_MAX; i++){
    //    //encryptionArithmetic_copyNumber(&i, alpha.number, KEY_LENGTH);
    //    square(&alpha, &q, &alpha_k, KEY_LENGTH);
    //    moduloOperation(&alpha_k, &q, KEY_LENGTH);
    //    if (*alpha_k.number == 1) {
    //        printf("Found alpha k where = 1");
    //        break;
    //    }
    //}

    t_encryptionArithmetic d;
    encryptionArithmetic_Init(&d, KEY_LENGTH);
    if (encryptionArithmetic_stringToHex(d_string, d.number, KEY_LENGTH) == false) {
        printf("ERROR: Creating d.");
        return 1000003;
    }

    /* initialize and calculate beta */
    t_encryptionArithmetic beta;
    encryptionArithmetic_Init(&beta, KEY_LENGTH);
    squareAndMultiply(&alpha, &d, &p, &beta, KEY_LENGTH);

    printf("-- public key --");
    printLargeNumberLine("p", &p, KEY_LENGTH);
    printLargeNumberLine("q", &q, KEY_LENGTH);
    printLargeNumberLine("alpha", &alpha, KEY_LENGTH);
    printLargeNumberLine("beta", &beta, KEY_LENGTH);

    return 0;
}

int calculateSessionKey(void) {

    return 0;
}

void printLargeNumberLine(char* descriptor, t_encryptionArithmetic* number, uint16_t size)
{
    uint16_t numLength = encryptionArithmetic_numberSize((*number).number, size);
    printf("\n%s:\t0x", descriptor);
    for (int16_t i = numLength / 32; i >= 0; i--) {
        printf("%X", *((*number).number + i));
    }
    printf("\n");
}