-
Notifications
You must be signed in to change notification settings - Fork 0
/
dh_assign_1.c
101 lines (94 loc) · 2.89 KB
/
dh_assign_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<stdio.h>
#include<gmp.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[] ) {
int opt;
char * filename;
mpz_t p,g,a,b,A,B, A_s,B_s;
mpz_init (p);
mpz_init (g);
mpz_init (a);
mpz_init (b);
mpz_init (A);
mpz_init (B);
mpz_init (A_s);
mpz_init (B_s);
// put ':' in the starting of the
// string so that program can
//distinguish between '?' and ':'
while((opt = getopt(argc, argv,":o:p:g:a:b:h")) != -1)
{
switch(opt)
{
case 'p':
mpz_set_str(p, optarg,10);
break;
case 'g':
mpz_set_str(g, optarg,10);
break;
case 'a':
mpz_set_str(a, optarg,10);
break;
case 'b':
mpz_set_str(b, optarg,10);
break;
case 'o':
filename = (char *)malloc((strlen(optarg)+1)*sizeof(char));
if (filename == NULL){
puts("Memory allocation error.");
return 1;
}
strcpy(filename,optarg);
break;
case 'h':
printf("The argument -p will include the will be the public prime number.\nThe argument -g will be the public primitive root of the previous prime number.\nThe argument -a will be the private key of user A.\nThe argument -b will be the private key of user B.\nThe command line tool will return the public key of user A, the public key of user B, andthe shared secret. The output file must be in the following format:<public key A>, <public key B>, <shared secret>");
return 1;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
// optind is for the extra arguments
// which are not parsed
for(; optind < argc; optind++){
printf("extra arguments: %s\n", argv[optind]);
}
//Calculating the values for alice and bob that will be exchanged and then the shared message.
mpz_powm(A,g,a,p);
mpz_powm(B,g,b,p);
mpz_powm(A_s,B,a,p);
mpz_powm(B_s,A,b,p);
//Checking if both the messages alice and bob received are the same.
if(mpz_cmp(A_s,B_s) == 0){
FILE *fp;
fp = fopen(filename, "w");
if(fp == NULL)
{
printf("Error!");
exit(1);
}
//Write values to output file.
gmp_fprintf(fp,"<%Zd>, <%Zd>, <%Zd>",A, B, A_s);
fclose(fp);
//Free the space occupied.
mpz_clear(p);
mpz_clear(g);
mpz_clear(a);
mpz_clear(b);
mpz_clear(A);
mpz_clear(B);
mpz_clear(A_s);
mpz_clear(B_s);
free(filename);
}
else{
printf("Error something wrong happened, message is not the same!");
}
}