Browse Source

Finished chat loop

master
thajohns 2 years ago
parent
commit
98388bf803
  1. 33
      client.c
  2. 22
      comm.c
  3. 2
      comm.h
  4. 35
      server.c

33
client.c

@ -5,13 +5,19 @@
#include "comm.h"
#include "aes.h"
int main()
int main(int argc, char **argv)
{
struct session sess;
initializeSBox();
sess_init(&sess);
if (do_resolve("127.0.0.1", NULL, &sess.params))
if (argc != 3)
{
printf("Usage: %s <bind address> <port>\n", argv[0]);
return 0;
}
if (do_resolve(argv[1], argv[2], &sess.params))
{
perror("could not open socket");
return 1;
@ -23,6 +29,29 @@ int main()
return 1;
}
int byte;
while (1)
{
byte = getchar();
if (byte == EOF)
break;
send_encrypted_byte(&sess, byte);
if (byte == '\n')
{
send_encrypted_byte(&sess, 0);
while ((byte = recv_encrypted_byte(&sess)))
{
if (byte == EOF)
{
goto break2;
}
printf("%c", byte);
}
}
}
break2:
do_unresolve(sess.params);
sess_destroy(&sess);
}

22
comm.c

@ -153,7 +153,8 @@ int send_data(char data[16], struct session *sess)
int recv_data(char data[16], struct session *sess)
{
read(sess->params.sock, data, 16);
if (!read(sess->params.sock, data, 16))
return 1;
return 0;
}
@ -262,3 +263,22 @@ int do_receive(struct session *sess)
return 0;
}
void send_encrypted_byte(struct session *sess, char byte)
{
fill_random(sess->last_sent_enc, 15);
sess->last_sent_enc[15] ^= byte;
send_data(sess->last_sent_enc, sess);
AESRound_data(sess->last_sent_enc, sess->key);
}
int recv_encrypted_byte(struct session *sess)
{
char buffer[16];
memcpy(buffer, sess->last_recv_enc, 16);
if (recv_data(sess->last_recv_enc, sess))
return EOF;
buffer[15] ^= sess->last_recv_enc[15];
AESRound_data(sess->last_recv_enc, sess->key);
return buffer[15];
}

2
comm.h

@ -38,6 +38,8 @@ 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);
void send_encrypted_byte(struct session *sess, char byte);
int recv_encrypted_byte(struct session *sess);
#endif

35
server.c

@ -5,13 +5,19 @@
#include "comm.h"
#include "aes.h"
int main()
int main(int argc, char **argv)
{
struct session sess;
initializeSBox();
sess_init(&sess);
FILE *keyf = fopen("key.out", "r");
if (argc != 4)
{
printf("Usage: %s <key file> <bind address> <port>\n", argv[0]);
return 0;
}
FILE *keyf = fopen(argv[1], "r");
if (keyf == NULL)
{
perror("could not open key file");
@ -26,7 +32,7 @@ int main()
fclose(keyf);
if (do_resolve("127.0.0.1", NULL, &sess.params))
if (do_resolve(argv[2], argv[3], &sess.params))
{
perror("could not open socket");
return 1;
@ -37,6 +43,29 @@ int main()
perror("could not accept");
return 1;
}
int byte;
while (1)
{
byte = getchar();
if (byte == EOF)
break;
send_encrypted_byte(&sess, byte);
if (byte == '\n')
{
send_encrypted_byte(&sess, 0);
while ((byte = recv_encrypted_byte(&sess)))
{
if (byte == EOF)
{
goto break2;
}
printf("%c", byte);
}
}
}
break2:
do_unresolve(sess.params);
sess_destroy(&sess);

Loading…
Cancel
Save