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.

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