Przeglądaj źródła

Got ElGamal and AES handshake working

thajohns 11 miesięcy temu
rodzic
commit
3990c4578d
8 zmienionych plików z 168 dodań i 14 usunięć
  1. 10
    2
      aes.c
  2. 1
    0
      aes.h
  3. 19
    1
      client.c
  4. 79
    10
      comm.c
  5. 4
    0
      comm.h
  6. 2
    0
      randsource.c
  7. 43
    0
      server.c
  8. 10
    1
      sgprime.c

+ 10
- 2
aes.c Wyświetl plik

@@ -136,7 +136,7 @@ void initializeSBox()
136 136
 static uint32_t wordSBox(uint32_t word)
137 137
 {
138 138
   return
139
-    (sbox[word]) |
139
+    (sbox[(uint8_t) word]) |
140 140
     (sbox[(uint8_t) (word >> 8)] << 8) |
141 141
     (sbox[(uint8_t) (word >> 16)] << 16) |
142 142
     (sbox[(uint8_t) (word >> 24)] << 24);
@@ -227,7 +227,7 @@ void AESRound(uint32_t state[4], uint32_t key[60])
227 227
   }
228 228
 }
229 229
 
230
-void dataToState(char data[16], uint32_t state[4]);
230
+void dataToState(char data[16], uint32_t state[4])
231 231
 {
232 232
   int ii;
233 233
   for (ii = 0; ii < 4; ii++)
@@ -252,6 +252,14 @@ void stateToData(uint32_t state[4], char data[16])
252 252
   }
253 253
 }
254 254
 
255
+void AESRound_data(char data[16], uint32_t key[60])
256
+{
257
+  uint32_t state[4];
258
+  dataToState(data, state);
259
+  AESRound(state, key);
260
+  stateToData(state, data);
261
+}
262
+
255 263
 
256 264
 //int main()
257 265
 //{

+ 1
- 0
aes.h Wyświetl plik

@@ -8,5 +8,6 @@ void expandKey(uint32_t short_key[8], uint32_t expanded_key[60]);
8 8
 void AESRound(uint32_t state[4], uint32_t key[60]);
9 9
 void dataToState(char data[16], uint32_t state[4]);
10 10
 void stateToData(uint32_t state[4], char data[16]);
11
+void AESRound_data(char data[16], uint32_t key[60]);
11 12
 
12 13
 #endif

+ 19
- 1
client.c Wyświetl plik

@@ -1,10 +1,28 @@
1 1
 
2
+
3
+#include <stdio.h>
4
+#include <errno.h>
2 5
 #include "comm.h"
6
+#include "aes.h"
3 7
 
4 8
 int main()
5 9
 {
6 10
   struct session sess;
11
+  initializeSBox();
12
+  sess_init(&sess);
13
+
14
+  if (do_resolve("127.0.0.1", NULL, &sess.params))
15
+  {
16
+    perror("could not open socket");
17
+    return 1;
18
+  }
19
+
20
+  if (do_connect(&sess))
21
+  {
22
+    perror("could not connect");
23
+    return 1;
24
+  }
7 25
 
8
-  do_resolve("127.0.0.1", NULL, &sess.params);
9 26
   do_unresolve(sess.params);
27
+  sess_destroy(&sess);
10 28
 }

+ 79
- 10
comm.c Wyświetl plik

@@ -15,7 +15,39 @@
15 15
 #include <string.h>
16 16
 #include <errno.h>
17 17
 
18
-size_t eight = 8;
18
+#define RAND_SIZE 512
19
+
20
+void sess_init(struct session *sess)
21
+{
22
+  mpz_t seed;
23
+  mpz_init(seed);
24
+
25
+  char *randbuff = malloc(RAND_SIZE);
26
+  gmp_randinit_default(sess->rs);
27
+  fill_random(randbuff, RAND_SIZE);
28
+
29
+  mpz_import(seed, RAND_SIZE, 1, 1, 0, 0, randbuff);
30
+  gmp_randseed(sess->rs, seed);
31
+
32
+  free(randbuff);
33
+
34
+  mpz_init(sess->eg.m);
35
+  mpz_init(sess->eg.g);
36
+  mpz_init(sess->eg.p);
37
+  mpz_init(sess->eg.x);
38
+
39
+  mpz_clear(seed);
40
+}
41
+
42
+void sess_destroy(struct session *sess)
43
+{
44
+  mpz_clear(sess->eg.m);
45
+  mpz_clear(sess->eg.g);
46
+  mpz_clear(sess->eg.p);
47
+  mpz_clear(sess->eg.x);
48
+
49
+  gmp_randclear(sess->rs);
50
+}
19 51
 
20 52
 int do_resolve(char *address, char *portstr, struct sock_params *params)
21 53
 {
@@ -51,12 +83,14 @@ int do_resolve(char *address, char *portstr, struct sock_params *params)
51 83
       {
52 84
         return err;
53 85
       }
86
+      ((struct sockaddr_in*) ai->ai_addr)->sin_port = htons(port);
54 87
     } else if (1 == sscanf(portstr, "%hu", &port))
55 88
     {
56 89
       if ((err = getaddrinfo(address, NULL, &hints, &ai)))
57 90
       {
58 91
         return err;
59 92
       }
93
+      ((struct sockaddr_in*) ai->ai_addr)->sin_port = htons(port);
60 94
     } else
61 95
     {
62 96
       if ((err = getaddrinfo(address, portstr, &hints, &ai)))
@@ -87,21 +121,21 @@ int send_num(mpz_t n, struct session *sess)
87 121
 {
88 122
   size_t nbytes;
89 123
   void *buf = mpz_export(NULL, &nbytes, 1, 1, 1, 0, n);
90
-  if (nbytes > (1 << 16))
124
+  if (nbytes >= (1 << 16))
91 125
   {
92 126
     free(buf);
93 127
     return -1;
94 128
   }
95 129
   uint16_t size = htons((uint16_t) nbytes);
96 130
   write(sess->params.sock, &size, 2);
97
-  write(sess->params.sock, buf, size);
131
+  write(sess->params.sock, buf, nbytes);
98 132
   free(buf);
99 133
   return 0;
100 134
 }
101 135
 
102 136
 int recv_num(mpz_t n, struct session *sess)
103 137
 {
104
-  uint16_t size;
138
+  uint16_t size = 0;
105 139
   read(sess->params.sock, &size, 2);
106 140
   size_t nbytes = ntohs(size);
107 141
   void *buf = malloc(nbytes);
@@ -111,6 +145,27 @@ int recv_num(mpz_t n, struct session *sess)
111 145
   return 0;
112 146
 }
113 147
 
148
+int send_data(char data[16], struct session *sess)
149
+{
150
+  write(sess->params.sock, data, 16);
151
+  return 0;
152
+}
153
+
154
+int recv_data(char data[16], struct session *sess)
155
+{
156
+  read(sess->params.sock, data, 16);
157
+  return 0;
158
+}
159
+
160
+void initial_aes_handshake(struct session *sess)
161
+{
162
+  fill_random(sess->last_sent_enc, 16);
163
+  send_data(sess->last_sent_enc, sess);
164
+  AESRound_data(sess->last_sent_enc, sess->key);
165
+  recv_data(sess->last_recv_enc, sess);
166
+  AESRound_data(sess->last_recv_enc, sess->key);
167
+}
168
+
114 169
 int do_connect(struct session *sess)
115 170
 {
116 171
   int en = connect(sess->params.sock, sess->params.addr, sess->params.addrlen);
@@ -129,17 +184,22 @@ int do_connect(struct session *sess)
129 184
   mpz_init(k);
130 185
   mpz_init(h);
131 186
 
187
+
132 188
   mpz_urandomm(a, sess->rs, sess->eg.m);
133 189
   mpz_import(k, 8, 1, 4, 1, 0, rshort_key);
134 190
   mpz_mod(k, k, sess->eg.m);
135
-  mpz_export(short_key, &eight, 1, 4, 1, 0, k);
191
+
192
+  size_t nbytes;
193
+  void *buf = mpz_export(NULL, &nbytes, -1, 4, 1, 0, k);
194
+  memcpy(short_key, buf, 32);
195
+  free(buf);
196
+  expandKey(short_key, sess->key);
197
+
136 198
   mpz_powm(h, sess->eg.g, a, sess->eg.m);
137 199
   mpz_powm(a, sess->eg.p, a, sess->eg.m);
138 200
   mpz_mul(a, k, a);
139 201
   mpz_mod(a, a, sess->eg.m);
140 202
 
141
-  expandKey(short_key, sess->key);
142
-
143 203
   send_num(h, sess);
144 204
   send_num(a, sess);
145 205
 
@@ -147,6 +207,8 @@ int do_connect(struct session *sess)
147 207
   mpz_clear(k);
148 208
   mpz_clear(h);
149 209
 
210
+  initial_aes_handshake(sess);
211
+
150 212
   return 0;
151 213
 }
152 214
 
@@ -163,6 +225,8 @@ int do_receive(struct session *sess)
163 225
     return errno;
164 226
   close(sess->params.sock);
165 227
   sess->params.sock = en;
228
+  struct linger linger = {0, 0};
229
+  setsockopt(sess->params.sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(struct linger));
166 230
 
167 231
   uint32_t short_key[8] = {0};
168 232
 
@@ -178,18 +242,23 @@ int do_receive(struct session *sess)
178 242
   recv_num(h, sess);
179 243
   recv_num(a, sess);
180 244
 
181
-  mpz_neg(f, sess->eg.m);
182
-  mpz_powm(f, h, sess->eg.x, f);
245
+  mpz_neg(f, sess->eg.x);
246
+  mpz_powm(f, h, f, sess->eg.m);
183 247
   mpz_mul(a, a, f);
184 248
   mpz_mod(a, a, sess->eg.m);
185 249
 
186
-  mpz_export(short_key, &eight, 1, 4, 1, 0, a);
250
+  size_t nbytes;
251
+  void *buf = mpz_export(NULL, &nbytes, -1, 4, 1, 0, a);
252
+  memcpy(short_key, buf, 32);
253
+  free(buf);
187 254
   expandKey(short_key, sess->key);
188 255
 
189 256
   mpz_clear(h);
190 257
   mpz_clear(f);
191 258
   mpz_clear(a);
192 259
 
260
+  initial_aes_handshake(sess);
261
+
193 262
   return 0;
194 263
 }
195 264
 

+ 4
- 0
comm.h Wyświetl plik

@@ -28,8 +28,12 @@ struct session
28 28
   struct sock_params params;
29 29
   struct elgamal eg;
30 30
   gmp_randstate_t rs;
31
+  char last_sent_enc[16];
32
+  char last_recv_enc[16];
31 33
 };
32 34
 
35
+void sess_init(struct session *sess);
36
+void sess_destroy(struct session *sess);
33 37
 int do_resolve(char *address, char *portstr, struct sock_params *params);
34 38
 void do_unresolve(struct sock_params params);
35 39
 int do_connect(struct session *sess);

+ 2
- 0
randsource.c Wyświetl plik

@@ -4,6 +4,8 @@
4 4
 #include <stdlib.h>
5 5
 #include <errno.h>
6 6
 
7
+// This function must return 0 on success, nonzero otherwise. Its job is to 
8
+// fill the buffer of the size given witih randomness.
7 9
 int fill_random(void *buf, int size)
8 10
 {
9 11
   FILE *random = fopen("/dev/urandom", "r");

+ 43
- 0
server.c Wyświetl plik

@@ -0,0 +1,43 @@
1
+
2
+
3
+#include <stdio.h>
4
+#include <errno.h>
5
+#include "comm.h"
6
+#include "aes.h"
7
+
8
+int main()
9
+{
10
+  struct session sess;
11
+  initializeSBox();
12
+  sess_init(&sess);
13
+
14
+  FILE *keyf = fopen("key.out", "r");
15
+  if (keyf == NULL)
16
+  {
17
+    perror("could not open key file");
18
+    return 1;
19
+  }
20
+
21
+  if (4 != gmp_fscanf(keyf, "m=%Zd\ng=%Zd\nx=%Zd\np=%Zd\n", sess.eg.m, sess.eg.g, sess.eg.x, sess.eg.p))
22
+  {
23
+    fprintf(stderr, "not a valid key file");
24
+    return 2;
25
+  }
26
+
27
+  fclose(keyf);
28
+
29
+  if (do_resolve("127.0.0.1", NULL, &sess.params))
30
+  {
31
+    perror("could not open socket");
32
+    return 1;
33
+  }
34
+
35
+  if (do_receive(&sess))
36
+  {
37
+    perror("could not accept");
38
+    return 1;
39
+  }
40
+
41
+  do_unresolve(sess.params);
42
+  sess_destroy(&sess);
43
+}

+ 10
- 1
sgprime.c Wyświetl plik

@@ -1,5 +1,14 @@
1 1
 
2
-#define PRIMORIAL_CAP_CAP 10000000
2
+// This value is tuned for generating large keys, or small keys many times. Higher values see
3
+// diminishing returns in efficiency (and though I cannot prove it, I believe that there is no
4
+// upper bound on the efficiency attainable by setting it higher, but it just isn't worth it.)
5
+// The larger this number, the longer build_sieve() will take (and thus make_difflist()). Also,
6
+// larger values of this number will use more memory. I wouldn't recommend setting it any higher,
7
+// but for generating small numbers of small keys, setting it lower is a good idea.
8
+// Another thing I forgot: Larger values will tend to make multithreaded searches where the
9
+// number of threads is not a multiple of many small primes whose primorial is less then this
10
+// number take much longer and use more memory.
11
+#define PRIMORIAL_CAP_CAP 1000000
3 12
 
4 13
 #include <stdlib.h>
5 14
 #include <stdio.h>

Ładowanie…
Anuluj
Zapisz