|
|
|
@ -17,110 +17,87 @@ |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
#include <stdint.h> |
|
|
|
#include "encryptionArithmetic.h" |
|
|
|
#include "encryptionArithmetic.h" |
|
|
|
#include "DSA.h" |
|
|
|
#include "DSA.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define KEY_LENGTH 1024 |
|
|
|
|
|
|
|
#define DIVISOR 160 |
|
|
|
|
|
|
|
|
|
|
|
#define KEY_LENGTH 160 |
|
|
|
int generateDsaKey(void); |
|
|
|
|
|
|
|
|
|
|
|
int createKeypair(void); |
|
|
|
|
|
|
|
int calculateSessionKey(void); |
|
|
|
int calculateSessionKey(void); |
|
|
|
|
|
|
|
void printLargeNumberLine(char* descriptor, t_encryptionArithmetic* number, uint16_t size); |
|
|
|
|
|
|
|
|
|
|
|
void main(void) { |
|
|
|
void main(void) { |
|
|
|
|
|
|
|
|
|
|
|
int result = 0; |
|
|
|
int result = 0; |
|
|
|
|
|
|
|
|
|
|
|
printf("------ Calculate Keypair ------\n\n"); |
|
|
|
printf("------ Generate DSA Key ------\n\n"); |
|
|
|
result = createKeypair(); |
|
|
|
result = generateDsaKey(); |
|
|
|
if (result != 0) { |
|
|
|
if (result != 0) { |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("------ Calculate Sessionkey ------\n\n"); |
|
|
|
/*printf("------ Calculate Sessionkey ------\n\n");
|
|
|
|
result = calculateSessionKey(); |
|
|
|
result = calculateSessionKey(); |
|
|
|
if (result != 0) { |
|
|
|
if (result != 0) { |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int createKeypair(void) { |
|
|
|
int generateDsaKey(void) { |
|
|
|
/*** CREATE KEYPAIR ***/ |
|
|
|
const char* p_string = "9999999999999999"; |
|
|
|
const char* P_string = "3203431780337000"; |
|
|
|
const char* q_string = "AFFE12345678AFFE"; |
|
|
|
const char* alpha_string = "AFFE12345678AFFE"; |
|
|
|
const char* d_string = "9778729279583412"; |
|
|
|
const char* Priv_a_string = "9778729279583412"; |
|
|
|
|
|
|
|
const char* Priv_b_string = "4825234752983495"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P has to be a prime number! Large prime number (64bit): 3203431780337000
|
|
|
|
// P has to be a prime number!
|
|
|
|
t_encryptionArithmetic P; |
|
|
|
t_encryptionArithmetic p; |
|
|
|
encryptionArithmetic_Init(&P, KEY_LENGTH); |
|
|
|
encryptionArithmetic_Init(&p, KEY_LENGTH); |
|
|
|
if (encryptionArithmetic_stringToHex(P_string, P.number, KEY_LENGTH) == false) { |
|
|
|
if (encryptionArithmetic_stringToHex(p_string, p.number, KEY_LENGTH) == false) { |
|
|
|
printf("ERROR: Creating P."); |
|
|
|
printf("ERROR: Creating p."); |
|
|
|
return 1000000; |
|
|
|
return 1000000; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
t_encryptionArithmetic alpha; |
|
|
|
t_encryptionArithmetic q; |
|
|
|
encryptionArithmetic_Init(&alpha, KEY_LENGTH); |
|
|
|
encryptionArithmetic_Init(&q, KEY_LENGTH); |
|
|
|
if (encryptionArithmetic_stringToHex(alpha_string, alpha.number, KEY_LENGTH) == false) { |
|
|
|
if (encryptionArithmetic_stringToHex(q_string, q.number, KEY_LENGTH) == false) { |
|
|
|
printf("ERROR: Creating alpha."); |
|
|
|
printf("ERROR: Creating q."); |
|
|
|
return 1000001; |
|
|
|
return 1000001; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
t_encryptionArithmetic priv_a; |
|
|
|
// ord(a) = k = q
|
|
|
|
encryptionArithmetic_Init(&priv_a, KEY_LENGTH); |
|
|
|
t_encryptionArithmetic alpha_k; |
|
|
|
if (encryptionArithmetic_stringToHex(Priv_a_string, priv_a.number, KEY_LENGTH) == false) { |
|
|
|
encryptionArithmetic_Init(&alpha_k, KEY_LENGTH); |
|
|
|
printf("ERROR: Creating priv a."); |
|
|
|
t_encryptionArithmetic alpha; |
|
|
|
return 1000003; |
|
|
|
encryptionArithmetic_Init(&alpha, KEY_LENGTH); |
|
|
|
} |
|
|
|
for (uint32_t i = 0; i < UINT32_MAX; i++) { |
|
|
|
|
|
|
|
square(&alpha, &q, &alpha_k, KEY_LENGTH); |
|
|
|
t_encryptionArithmetic priv_b; |
|
|
|
moduloOperation(&alpha_k, &q, KEY_LENGTH); |
|
|
|
encryptionArithmetic_Init(&priv_b, KEY_LENGTH); |
|
|
|
if (alpha.number == 1) { |
|
|
|
if (encryptionArithmetic_stringToHex(Priv_b_string, priv_b.number, KEY_LENGTH) == false) { |
|
|
|
printf("Found alpha k where = 1"); |
|
|
|
printf("ERROR: Creating priv b."); |
|
|
|
break; |
|
|
|
return 1000004; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* initialize and calculate public key a */ |
|
|
|
t_encryptionArithmetic d; |
|
|
|
t_encryptionArithmetic pub_a; |
|
|
|
encryptionArithmetic_Init(&d, KEY_LENGTH); |
|
|
|
encryptionArithmetic_Init(&pub_a, KEY_LENGTH); |
|
|
|
if (encryptionArithmetic_stringToHex(d_string, d.number, KEY_LENGTH) == false) { |
|
|
|
squareAndMultiply(&alpha, &priv_a, &P, &pub_a, KEY_LENGTH); |
|
|
|
printf("ERROR: Creating d."); |
|
|
|
printf("Calculated public key for person A: 0x"); |
|
|
|
return 1000003; |
|
|
|
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))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
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 */ |
|
|
|
printf("-- public key --"); |
|
|
|
t_encryptionArithmetic session_key_a; |
|
|
|
printLargeNumberLine("p", &p, KEY_LENGTH); |
|
|
|
encryptionArithmetic_Init(&session_key_a, KEY_LENGTH); |
|
|
|
printLargeNumberLine("q", &q, KEY_LENGTH); |
|
|
|
squareAndMultiply(&pub_b, &priv_a, &P, &session_key_a, KEY_LENGTH); |
|
|
|
printLargeNumberLine("alpha", &alpha, KEY_LENGTH); |
|
|
|
printf("Calculated session key for person A: 0x"); |
|
|
|
printLargeNumberLine("beta", &beta, KEY_LENGTH); |
|
|
|
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"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -165,4 +142,14 @@ int calculateSessionKey(void) { |
|
|
|
printf("\n"); |
|
|
|
printf("\n"); |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
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"); |
|
|
|
} |
|
|
|
} |