Simple AES chat program
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.2 KiB

#include <stdio.h>
#include <errno.h>
#include "comm.h"
#include "aes.h"
int main(int argc, char **argv)
{
struct session sess;
initializeSBox();
sess_init(&sess);
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");
return 1;
}
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))
{
fprintf(stderr, "not a valid key file");
return 2;
}
fclose(keyf);
if (do_resolve(argv[2], argv[3], &sess.params))
{
perror("could not open socket");
return 1;
}
if (do_receive(&sess))
{
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);
}