From ab4d31de088c9d25f76c46df63e8b4022ba3f571 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Thu, 16 Mar 2023 16:15:38 +0100 Subject: [PATCH] implemented creation of DSA key --- ASYD_Cryptograhpy/SW04-DSA/main.c | 129 ++++++++++++++---------------- 1 file changed, 58 insertions(+), 71 deletions(-) diff --git a/ASYD_Cryptograhpy/SW04-DSA/main.c b/ASYD_Cryptograhpy/SW04-DSA/main.c index 7e9bc55..133aeec 100644 --- a/ASYD_Cryptograhpy/SW04-DSA/main.c +++ b/ASYD_Cryptograhpy/SW04-DSA/main.c @@ -17,110 +17,87 @@ #include #include #include +#include #include "encryptionArithmetic.h" #include "DSA.h" +#define KEY_LENGTH 1024 +#define DIVISOR 160 -#define KEY_LENGTH 160 - -int createKeypair(void); +int generateDsaKey(void); int calculateSessionKey(void); +void printLargeNumberLine(char* descriptor, t_encryptionArithmetic* number, uint16_t size); void main(void) { int result = 0; - printf("------ Calculate Keypair ------\n\n"); - result = createKeypair(); + printf("------ Generate DSA Key ------\n\n"); + result = generateDsaKey(); if (result != 0) { return result; } - printf("------ Calculate Sessionkey ------\n\n"); + /*printf("------ Calculate Sessionkey ------\n\n"); result = calculateSessionKey(); if (result != 0) { return result; - } + }*/ return 0; } -int createKeypair(void) { - /*** CREATE KEYPAIR ***/ - const char* P_string = "3203431780337000"; - const char* alpha_string = "AFFE12345678AFFE"; - const char* Priv_a_string = "9778729279583412"; - const char* Priv_b_string = "4825234752983495"; +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! 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."); + // 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 alpha; - encryptionArithmetic_Init(&alpha, KEY_LENGTH); - if (encryptionArithmetic_stringToHex(alpha_string, alpha.number, KEY_LENGTH) == false) { - printf("ERROR: Creating alpha."); + t_encryptionArithmetic q; + encryptionArithmetic_Init(&q, KEY_LENGTH); + if (encryptionArithmetic_stringToHex(q_string, q.number, KEY_LENGTH) == false) { + printf("ERROR: Creating q."); return 1000001; } - 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 1000003; - } - - t_encryptionArithmetic priv_b; - encryptionArithmetic_Init(&priv_b, KEY_LENGTH); - if (encryptionArithmetic_stringToHex(Priv_b_string, priv_b.number, KEY_LENGTH) == false) { - printf("ERROR: Creating priv b."); - return 1000004; + // 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; + } } - /* initialize and calculate public key a */ - t_encryptionArithmetic pub_a; - encryptionArithmetic_Init(&pub_a, KEY_LENGTH); - squareAndMultiply(&alpha, &priv_a, &P, &pub_a, KEY_LENGTH); - printf("Calculated public key for person A: 0x"); - for (int num_bytes = KEY_LENGTH / 64; num_bytes >= 0; num_bytes--) { - printf("%X", *((uint32_t*)(pub_a.number + num_bytes))); - } - printf("\n"); - - /* initialize and calculate public key b */ - t_encryptionArithmetic pub_b; - encryptionArithmetic_Init(&pub_b, KEY_LENGTH); - squareAndMultiply(&alpha, &priv_b, &P, &pub_b, KEY_LENGTH); - printf("Calculated public key for person B: 0x"); - for (int num_bytes = KEY_LENGTH / 64; num_bytes >= 0; num_bytes--) { - printf("%X", *((uint32_t*)(pub_b.number + num_bytes))); + t_encryptionArithmetic d; + encryptionArithmetic_Init(&d, KEY_LENGTH); + if (encryptionArithmetic_stringToHex(d_string, d.number, KEY_LENGTH) == false) { + printf("ERROR: Creating d."); + return 1000003; } - printf("\n"); + /* initialize and calculate beta */ + t_encryptionArithmetic beta; + encryptionArithmetic_Init(&beta, KEY_LENGTH); + squareAndMultiply(&alpha, &d, &p, &beta, KEY_LENGTH); - /* 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"); - - /* initialize and calculate session key for person b */ - t_encryptionArithmetic session_key_b; - encryptionArithmetic_Init(&session_key_b, KEY_LENGTH); - squareAndMultiply(&pub_a, &priv_b, &P, &session_key_b, KEY_LENGTH); - printf("Calculated session key for person B: 0x"); - for (int num_bytes = KEY_LENGTH / 64; num_bytes >= 0; num_bytes--) { - printf("%X", *((uint32_t*)(session_key_b.number + num_bytes))); - } - printf("\n"); + 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; } @@ -165,4 +142,14 @@ int calculateSessionKey(void) { 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"); } \ No newline at end of file