forked from kokke/tiny-ECDH-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbob.c
126 lines (100 loc) · 2.71 KB
/
bob.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <assert.h>
#include "ecdh.c"
typedef struct {
uint32_t a, b, c, d;
} prng_t;
static prng_t prng_ctx;
static uint32_t prng_rotate(uint32_t x, uint32_t k)
{
return (x << k) | (x >> (32 - k));
}
static uint32_t prng_next()
{
uint32_t e = prng_ctx.a - prng_rotate(prng_ctx.b, 27);
prng_ctx.a = prng_ctx.b ^ prng_rotate(prng_ctx.c, 17);
prng_ctx.b = prng_ctx.c + prng_ctx.d;
prng_ctx.c = prng_ctx.d + e;
prng_ctx.d = e + prng_ctx.a;
return prng_ctx.d;
}
static void prng_init(uint32_t seed)
{
uint32_t i;
prng_ctx.a = 0xf1ea5eed;
prng_ctx.b = prng_ctx.c = prng_ctx.d = seed;
for (i = 0; i < 31; ++i)
{
(void) prng_next();
}
}
int main(int argc , char *argv[])
{
//socket的建立
int sockfd = 0;
sockfd = socket(AF_INET , SOCK_STREAM , 0);
if (sockfd == -1){
printf("Fail to create a socket.");
}
//socket的連線
struct sockaddr_in info;
bzero(&info,sizeof(info));
info.sin_family = PF_INET;
//localhost test
info.sin_addr.s_addr = inet_addr("127.0.0.1");
info.sin_port = htons(50000);
int err = connect(sockfd,(struct sockaddr *)&info,sizeof(info));
if(err==-1){
printf("Connection error");
return -1;
}
prng_init((0xbad ^ 0xc0ffee ^ 42) | 0xcafebabe | 666);
/* Generate a private key */
static uint8_t prvkey[ECC_PRV_KEY_SIZE];
for (int i = 0; i < ECC_PRV_KEY_SIZE; i++) {
prvkey[i] = prng_next();
}
/* Generate a public key */
static uint8_t pubkey[ECC_PUB_KEY_SIZE];
assert(ecdh_generate_keys(pubkey, prvkey));
printf("Bob private key: ");
for (int i=0; i<ECC_PRV_KEY_SIZE; i++) {
printf("%d ", prvkey[i]);
}
printf("\n");
printf("Bob public key: ");
for (int i=0; i<ECC_PUB_KEY_SIZE; i++) {
printf("%d ", pubkey[i]);
}
printf("\n");
/* Handshake Process*/
uint8_t buf[256];
/* TODO: Verify identity */
/* Send Bob public key */
write(sockfd, pubkey, sizeof(pubkey));
/* Wait for server public key */
read(sockfd, buf, sizeof(buf));
printf("Received server public key: ");
for (int i=0; i<ECC_PUB_KEY_SIZE; i++) {
printf("%d ", buf[i]);
}
printf("\n");
/* TODO: Validation */
/* Compute and print shared secret */
static uint8_t seckey[ECC_PUB_KEY_SIZE];
assert(ecdh_shared_secret(prvkey, buf, seckey));
printf("Shared secret is: ");
for (int i=0; i<ECC_PUB_KEY_SIZE; i++) {
printf("%d ", seckey[i]);
}
printf("\n");
printf("close Socket\n");
close(sockfd);
return 0;
}