Browse Source

Started client

master
thajohns 2 years ago
parent
commit
c035d01823
  1. 12
      aes.h
  2. 10
      client.c
  3. 195
      comm.c
  4. 39
      comm.h

12
aes.h

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

10
client.c

@ -0,0 +1,10 @@
#include "comm.h"
int main()
{
struct session sess;
do_resolve("127.0.0.1", NULL, &sess.params);
do_unresolve(sess.params);
}

195
comm.c

@ -0,0 +1,195 @@
#include "comm.h"
#include "randsource.h"
#include "aes.h"
#include <gmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
size_t eight = 8;
int do_resolve(char *address, char *portstr, struct sock_params *params)
{
if (strspn(address, "unix:") == 5)
{
address += 5;
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1)
return errno;
struct sockaddr_un *addr = malloc(sizeof(struct sockaddr_un));;
addr->sun_family = AF_UNIX; // Polymorphism in C is pretty cool.
strncpy(addr->sun_path, address, sizeof(addr->sun_path) - 1);
addr->sun_path[sizeof(addr->sun_path) - 1] = 0;
params->sock = sock;
params->addr = (struct sockaddr*) addr;
params->addrlen = sizeof(struct sockaddr_un);
params->freeme = NULL;
} else
{
struct addrinfo hints =
{
.ai_flags = 0,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0,
};
struct addrinfo *ai;
unsigned short port = 7140;
int err;
if (portstr == NULL)
{
if ((err = getaddrinfo(address, NULL, &hints, &ai)))
{
return err;
}
} else if (1 == sscanf(portstr, "%hu", &port))
{
if ((err = getaddrinfo(address, NULL, &hints, &ai)))
{
return err;
}
} else
{
if ((err = getaddrinfo(address, portstr, &hints, &ai)))
{
return err;
}
}
int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sock == -1)
return errno;
params->sock = sock;
params->addr = ai->ai_addr;
params->addrlen = ai->ai_addrlen;
params->freeme = ai;
}
return 0;
}
void do_unresolve(struct sock_params params)
{
close(params.sock);
if (params.freeme == NULL)
free(params.addr);
freeaddrinfo(params.freeme);
}
int send_num(mpz_t n, struct session *sess)
{
size_t nbytes;
void *buf = mpz_export(NULL, &nbytes, 1, 1, 1, 0, n);
if (nbytes > (1 << 16))
{
free(buf);
return -1;
}
uint16_t size = htons((uint16_t) nbytes);
write(sess->params.sock, &size, 2);
write(sess->params.sock, buf, size);
free(buf);
return 0;
}
int recv_num(mpz_t n, struct session *sess)
{
uint16_t size;
read(sess->params.sock, &size, 2);
size_t nbytes = ntohs(size);
void *buf = malloc(nbytes);
read(sess->params.sock, buf, nbytes);
mpz_import(n, nbytes, 1, 1, 1, 0, buf);
free(buf);
return 0;
}
int do_connect(struct session *sess)
{
int en = connect(sess->params.sock, sess->params.addr, sess->params.addrlen);
if (en != 0)
return en;
uint32_t rshort_key[8], short_key[8] = {0};
fill_random(rshort_key, 32);
recv_num(sess->eg.m, sess);
recv_num(sess->eg.g, sess);
recv_num(sess->eg.p, sess);
mpz_t a, k, h;
mpz_init(a);
mpz_init(k);
mpz_init(h);
mpz_urandomm(a, sess->rs, sess->eg.m);
mpz_import(k, 8, 1, 4, 1, 0, rshort_key);
mpz_mod(k, k, sess->eg.m);
mpz_export(short_key, &eight, 1, 4, 1, 0, k);
mpz_powm(h, sess->eg.g, a, sess->eg.m);
mpz_powm(a, sess->eg.p, a, sess->eg.m);
mpz_mul(a, k, a);
mpz_mod(a, a, sess->eg.m);
expandKey(short_key, sess->key);
send_num(h, sess);
send_num(a, sess);
mpz_clear(a);
mpz_clear(k);
mpz_clear(h);
return 0;
}
int do_receive(struct session *sess)
{
int en = bind(sess->params.sock, sess->params.addr, sess->params.addrlen);
if (en != 0)
return en;
en = listen(sess->params.sock, 1);
if (en != 0)
return en;
en = accept(sess->params.sock, NULL, NULL);
if (en == -1)
return errno;
close(sess->params.sock);
sess->params.sock = en;
uint32_t short_key[8] = {0};
send_num(sess->eg.m, sess);
send_num(sess->eg.g, sess);
send_num(sess->eg.p, sess);
mpz_t h, f, a;
mpz_init(h);
mpz_init(f);
mpz_init(a);
recv_num(h, sess);
recv_num(a, sess);
mpz_neg(f, sess->eg.m);
mpz_powm(f, h, sess->eg.x, f);
mpz_mul(a, a, f);
mpz_mod(a, a, sess->eg.m);
mpz_export(short_key, &eight, 1, 4, 1, 0, a);
expandKey(short_key, sess->key);
mpz_clear(h);
mpz_clear(f);
mpz_clear(a);
return 0;
}

39
comm.h

@ -0,0 +1,39 @@
#ifndef COMM_H
#define COMM_H
#include <sys/types.h>
#include <sys/socket.h>
#include <gmp.h>
#include <stdint.h>
struct sock_params
{
int sock;
struct sockaddr *addr;
socklen_t addrlen;
struct addrinfo *freeme;
};
struct elgamal
{
mpz_t m;
mpz_t g;
mpz_t p;
mpz_t x;
};
struct session
{
uint32_t key[60];
struct sock_params params;
struct elgamal eg;
gmp_randstate_t rs;
};
int do_resolve(char *address, char *portstr, struct sock_params *params);
void do_unresolve(struct sock_params params);
int do_connect(struct session *sess);
int do_receive(struct session *sess);
#endif
Loading…
Cancel
Save