Browse Source

Started client

thajohns 11 months ago
parent
commit
c035d01823
4 changed files with 256 additions and 0 deletions
  1. 12
    0
      aes.h
  2. 10
    0
      client.c
  3. 195
    0
      comm.c
  4. 39
    0
      comm.h

+ 12
- 0
aes.h View File

@@ -0,0 +1,12 @@
1
+#ifndef AES_H
2
+#define AES_H
3
+
4
+#include <stdint.h>
5
+
6
+void initializeSBox();
7
+void expandKey(uint32_t short_key[8], uint32_t expanded_key[60]);
8
+void AESRound(uint32_t state[4], uint32_t key[60]);
9
+void dataToState(char data[16], uint32_t state[4]);
10
+void stateToData(uint32_t state[4], char data[16]);
11
+
12
+#endif

+ 10
- 0
client.c View File

@@ -0,0 +1,10 @@
1
+
2
+#include "comm.h"
3
+
4
+int main()
5
+{
6
+  struct session sess;
7
+
8
+  do_resolve("127.0.0.1", NULL, &sess.params);
9
+  do_unresolve(sess.params);
10
+}

+ 195
- 0
comm.c View File

@@ -0,0 +1,195 @@
1
+
2
+#include "comm.h"
3
+#include "randsource.h"
4
+#include "aes.h"
5
+
6
+#include <gmp.h>
7
+#include <stdlib.h>
8
+#include <stdio.h>
9
+#include <unistd.h>
10
+#include <stdint.h>
11
+#include <sys/types.h>
12
+#include <sys/socket.h>
13
+#include <sys/un.h>
14
+#include <netdb.h>
15
+#include <string.h>
16
+#include <errno.h>
17
+
18
+size_t eight = 8;
19
+
20
+int do_resolve(char *address, char *portstr, struct sock_params *params)
21
+{
22
+  if (strspn(address, "unix:") == 5)
23
+  {
24
+    address += 5;
25
+    int sock = socket(AF_UNIX, SOCK_STREAM, 0);
26
+    if (sock == -1)
27
+      return errno;
28
+    struct sockaddr_un *addr = malloc(sizeof(struct sockaddr_un));;
29
+    addr->sun_family = AF_UNIX;  // Polymorphism in C is pretty cool.
30
+    strncpy(addr->sun_path, address, sizeof(addr->sun_path) - 1);
31
+    addr->sun_path[sizeof(addr->sun_path) - 1] = 0;
32
+    params->sock = sock;
33
+    params->addr = (struct sockaddr*) addr;
34
+    params->addrlen = sizeof(struct sockaddr_un);
35
+    params->freeme = NULL;
36
+  } else
37
+  {
38
+    struct addrinfo hints =
39
+    {
40
+      .ai_flags = 0,
41
+      .ai_family = AF_UNSPEC,
42
+      .ai_socktype = SOCK_STREAM,
43
+      .ai_protocol = 0,
44
+    };
45
+    struct addrinfo *ai;
46
+    unsigned short port = 7140;
47
+    int err;
48
+    if (portstr == NULL)
49
+    {
50
+      if ((err = getaddrinfo(address, NULL, &hints, &ai)))
51
+      {
52
+        return err;
53
+      }
54
+    } else if (1 == sscanf(portstr, "%hu", &port))
55
+    {
56
+      if ((err = getaddrinfo(address, NULL, &hints, &ai)))
57
+      {
58
+        return err;
59
+      }
60
+    } else
61
+    {
62
+      if ((err = getaddrinfo(address, portstr, &hints, &ai)))
63
+      {
64
+        return err;
65
+      }
66
+    }
67
+    int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
68
+    if (sock == -1)
69
+      return errno;
70
+    params->sock = sock;
71
+    params->addr = ai->ai_addr;
72
+    params->addrlen = ai->ai_addrlen;
73
+    params->freeme = ai;
74
+  }
75
+  return 0;
76
+}
77
+
78
+void do_unresolve(struct sock_params params)
79
+{
80
+  close(params.sock);
81
+  if (params.freeme == NULL)
82
+    free(params.addr);
83
+  freeaddrinfo(params.freeme);
84
+}
85
+
86
+int send_num(mpz_t n, struct session *sess)
87
+{
88
+  size_t nbytes;
89
+  void *buf = mpz_export(NULL, &nbytes, 1, 1, 1, 0, n);
90
+  if (nbytes > (1 << 16))
91
+  {
92
+    free(buf);
93
+    return -1;
94
+  }
95
+  uint16_t size = htons((uint16_t) nbytes);
96
+  write(sess->params.sock, &size, 2);
97
+  write(sess->params.sock, buf, size);
98
+  free(buf);
99
+  return 0;
100
+}
101
+
102
+int recv_num(mpz_t n, struct session *sess)
103
+{
104
+  uint16_t size;
105
+  read(sess->params.sock, &size, 2);
106
+  size_t nbytes = ntohs(size);
107
+  void *buf = malloc(nbytes);
108
+  read(sess->params.sock, buf, nbytes);
109
+  mpz_import(n, nbytes, 1, 1, 1, 0, buf);
110
+  free(buf);
111
+  return 0;
112
+}
113
+
114
+int do_connect(struct session *sess)
115
+{
116
+  int en = connect(sess->params.sock, sess->params.addr, sess->params.addrlen);
117
+  if (en != 0)
118
+    return en;
119
+
120
+  uint32_t rshort_key[8], short_key[8] = {0};
121
+  fill_random(rshort_key, 32);
122
+
123
+  recv_num(sess->eg.m, sess);
124
+  recv_num(sess->eg.g, sess);
125
+  recv_num(sess->eg.p, sess);
126
+
127
+  mpz_t a, k, h;
128
+  mpz_init(a);
129
+  mpz_init(k);
130
+  mpz_init(h);
131
+
132
+  mpz_urandomm(a, sess->rs, sess->eg.m);
133
+  mpz_import(k, 8, 1, 4, 1, 0, rshort_key);
134
+  mpz_mod(k, k, sess->eg.m);
135
+  mpz_export(short_key, &eight, 1, 4, 1, 0, k);
136
+  mpz_powm(h, sess->eg.g, a, sess->eg.m);
137
+  mpz_powm(a, sess->eg.p, a, sess->eg.m);
138
+  mpz_mul(a, k, a);
139
+  mpz_mod(a, a, sess->eg.m);
140
+
141
+  expandKey(short_key, sess->key);
142
+
143
+  send_num(h, sess);
144
+  send_num(a, sess);
145
+
146
+  mpz_clear(a);
147
+  mpz_clear(k);
148
+  mpz_clear(h);
149
+
150
+  return 0;
151
+}
152
+
153
+int do_receive(struct session *sess)
154
+{
155
+  int en = bind(sess->params.sock, sess->params.addr, sess->params.addrlen);
156
+  if (en != 0)
157
+    return en;
158
+  en = listen(sess->params.sock, 1);
159
+  if (en != 0)
160
+    return en;
161
+  en = accept(sess->params.sock, NULL, NULL);
162
+  if (en == -1)
163
+    return errno;
164
+  close(sess->params.sock);
165
+  sess->params.sock = en;
166
+
167
+  uint32_t short_key[8] = {0};
168
+
169
+  send_num(sess->eg.m, sess);
170
+  send_num(sess->eg.g, sess);
171
+  send_num(sess->eg.p, sess);
172
+
173
+  mpz_t h, f, a;
174
+  mpz_init(h);
175
+  mpz_init(f);
176
+  mpz_init(a);
177
+
178
+  recv_num(h, sess);
179
+  recv_num(a, sess);
180
+
181
+  mpz_neg(f, sess->eg.m);
182
+  mpz_powm(f, h, sess->eg.x, f);
183
+  mpz_mul(a, a, f);
184
+  mpz_mod(a, a, sess->eg.m);
185
+
186
+  mpz_export(short_key, &eight, 1, 4, 1, 0, a);
187
+  expandKey(short_key, sess->key);
188
+
189
+  mpz_clear(h);
190
+  mpz_clear(f);
191
+  mpz_clear(a);
192
+
193
+  return 0;
194
+}
195
+

+ 39
- 0
comm.h View File

@@ -0,0 +1,39 @@
1
+#ifndef COMM_H
2
+#define COMM_H
3
+
4
+#include <sys/types.h>
5
+#include <sys/socket.h>
6
+#include <gmp.h>
7
+#include <stdint.h>
8
+
9
+struct sock_params
10
+{
11
+  int sock;
12
+  struct sockaddr *addr;
13
+  socklen_t addrlen;
14
+  struct addrinfo *freeme;
15
+};
16
+
17
+struct elgamal
18
+{
19
+  mpz_t m;
20
+  mpz_t g;
21
+  mpz_t p;
22
+  mpz_t x;
23
+};
24
+
25
+struct session
26
+{
27
+  uint32_t key[60];
28
+  struct sock_params params;
29
+  struct elgamal eg;
30
+  gmp_randstate_t rs;
31
+};
32
+
33
+int do_resolve(char *address, char *portstr, struct sock_params *params);
34
+void do_unresolve(struct sock_params params);
35
+int do_connect(struct session *sess);
36
+int do_receive(struct session *sess);
37
+
38
+#endif
39
+

Loading…
Cancel
Save