/**
 *--------------------------------------------------------------------\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 1024
#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 = "9999999999999999";
    const char* q_string = "AFFE12345678AFFE";
    const char* d_string = "9778729279583412";

    // 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, KEY_LENGTH);
    for (uint32_t i = 0; i < UINT32_MAX; i++) {
        square(&alpha, &q, &alpha_k, KEY_LENGTH);
        moduloOperation(&alpha_k, &q, KEY_LENGTH);
        if (alpha.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) {

    /*** Calculate Session Key ***/
    const char* P_string = "3203431780337000";
    const char* Priv_a_string = "9778729279583412";
    const char* Pub_b_string = "13BED5BE5045000";

    // P has to be a prime number! Large prime number (64bit): 3203431780337000
    t_encryptionArithmetic P;
    encryptionArithmetic_Init(&P, KEY_LENGTH);
    if (encryptionArithmetic_stringToHex(P_string, P.number, KEY_LENGTH) == false) {
        printf("ERROR: Creating P.");
        return 1100000;
    }

    t_encryptionArithmetic priv_a;
    encryptionArithmetic_Init(&priv_a, KEY_LENGTH);
    if (encryptionArithmetic_stringToHex(Priv_a_string, priv_a.number, KEY_LENGTH) == false) {
        printf("ERROR: Creating priv a.");
        return 1100003;
    }

    t_encryptionArithmetic pub_b;
    encryptionArithmetic_Init(&pub_b, KEY_LENGTH);
    if (encryptionArithmetic_stringToHex(Pub_b_string, pub_b.number, KEY_LENGTH) == false) {
        printf("ERROR: Creating pub b.");
        return 1100004;
    }

    /* initialize and calculate session key for person a */
    t_encryptionArithmetic session_key_a;
    encryptionArithmetic_Init(&session_key_a, KEY_LENGTH);
    squareAndMultiply(&pub_b, &priv_a, &P, &session_key_a, KEY_LENGTH);
    printf("Calculated session key for person A: 0x");
    for (int num_bytes = KEY_LENGTH / 64; num_bytes >= 0; num_bytes--) {
        printf("%X", *((uint32_t*)(session_key_a.number + num_bytes)));
    }
    printf("\n");

    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");
}