diff --git a/ASYD_Cryptograhpy/SW03-DHKE/main.c b/ASYD_Cryptograhpy/SW03-DHKE/main.c index 7bafdee..09935a9 100644 --- a/ASYD_Cryptograhpy/SW03-DHKE/main.c +++ b/ASYD_Cryptograhpy/SW03-DHKE/main.c @@ -8,7 +8,7 @@ * \author Basil Estermann, basil.estermann@stud.hslu.ch * Simon Frei, simon.frei@stud.hslu.ch * Jonas Arnold, jonas.arnold@stud.hslu.ch - * \date 10.03.2023 + * \date 14.03.2023 * *-------------------------------------------------------------------- */ @@ -23,51 +23,122 @@ #define KEY_LENGTH 64 +int createKeypair(void); +int calculateSessionKey(void); + void main(void) { - /* Initialize variables */ + printf("------ Calculate Keypair ------\n\n"); + int result = createKeypair(); + if (result != 0) { + return result; + } + + 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 = "AFFE425634534"; + const char* Priv_a_string = "9778729279583412"; + const char* Priv_b_string = "4825234752983495"; // P has to be a prime number! Large prime number (64bit): 3203431780337000 t_encryptionArithmetic P; encryptionArithmetic_Init(&P, KEY_LENGTH); - if (encryptionArithmetic_stringToHex("3203431780337000", P.number, KEY_LENGTH) == false) { + if (encryptionArithmetic_stringToHex(P_string, P.number, KEY_LENGTH) == false) { printf("ERROR: Creating P."); return 1000000; } - // alpha not required for session key generation - //t_encryptionArithmetic alpha; - //encryptionArithmetic_Init(&alpha, KEY_LENGTH); - //if (encryptionArithmetic_stringToHex("13", alpha.number, KEY_LENGTH) == false) { - // printf("ERROR: Creating alpha."); - // return 1000001; - //} - - t_encryptionArithmetic pub; - encryptionArithmetic_Init(&pub, KEY_LENGTH); - if (encryptionArithmetic_stringToHex("1370122177720875", pub.number, KEY_LENGTH) == false) { - printf("ERROR: Creating pub."); - return 1000002; + t_encryptionArithmetic alpha; + encryptionArithmetic_Init(&alpha, KEY_LENGTH); + if (encryptionArithmetic_stringToHex(alpha_string, alpha.number, KEY_LENGTH) == false) { + printf("ERROR: Creating alpha."); + return 1000001; } - t_encryptionArithmetic priv; - encryptionArithmetic_Init(&priv, KEY_LENGTH); - if (encryptionArithmetic_stringToHex("9778729279583412", priv.number, KEY_LENGTH) == false) { - printf("ERROR: Creating priv."); + 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 session_key; - encryptionArithmetic_Init(&session_key, KEY_LENGTH); + 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; + } + + /* 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); + /* 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); - /* Create session key */ - squareAndMultiply(&pub, &priv, &P, &session_key, 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%x\n", *session_key_a.number); + + /* 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%x\n", *session_key_b.number); + + return 0; +} - /* Write session key */ - printf("%i",*session_key.number); +int calculateSessionKey(void) { + + /*** Calculate Session Key ***/ + const char* P_string = "B8C6440FA3B64CB9"; + const char* Priv_a_string = "C8B2014E72001DD"; + const char* Pub_b_string = "69DF372122D2E892"; + + // 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%x\n", *session_key_a.number); return 0; } \ No newline at end of file