/
dhsimple.c
67 lines (49 loc) · 1.61 KB
/
dhsimple.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
#include "dhuser.h"
#include "dhutils.h"
#include "dhrandom.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
int status = -1;
dhuser_t alice, bob;
s_memclr(&alice, sizeof(dhuser_t));
s_memclr(&bob, sizeof(dhuser_t));
if(argc != 2)
goto err;
int ac = dh_init(&alice, SERVER);
int bc = dh_init(&bob, CLIENT);
if(ac != 0 || bc != 0)
goto err;
int mod_len = check_size(atoi(argv[1]),0);
if(mod_len < 0)
goto err;
if(dh_generateParameters(&alice, mod_len, mod_len, mod_len) < 0)
goto err;
if(dh_setParameters(&bob, mod_len, mod_len, mod_len, alice.P, alice.G) < 0)
goto err;
if(dh_generatePrivateKey(&alice) < 0 || dh_generatePrivateKey(&bob) < 0)
goto err;
dh_generateSharedKey(&alice); dh_generateSharedKey(&bob);
if(dh_computeSecret(&alice, bob.Shared_E) < 0 || dh_computeSecret(&bob, alice.Shared_F) < 0)
goto err;
char* alice_hash = dh_computePublicHash(&alice);
char* bob_hash = dh_computePublicHash(&bob);
if(!alice_hash || !bob_hash)
goto err;
byte sig_buf[2048];
s_memclr(sig_buf, 2048);
unsigned int sig_len = sizeof(sig_buf);
sign(alice_hash, sig_buf, &sig_len);
printf("%u\n",sig_len);
if(verify(bob_hash, sig_buf, sig_len) != 1)
printf("Authentication failed\n");
else
printf("Secret sharing successful\n");
status = 0;
err:
if(alice_hash) delete(alice_hash, strlen(alice_hash));
if(bob_hash) delete(bob_hash, strlen(bob_hash));
dh_destroy(&alice);
dh_destroy(&bob);
return status;
}