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.

comm.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. read(sess->params.sock, data, 16);
  140. return 0;
  141. }
  142. void initial_aes_handshake(struct session *sess)
  143. {
  144. fill_random(sess->last_sent_enc, 16);
  145. send_data(sess->last_sent_enc, sess);
  146. AESRound_data(sess->last_sent_enc, sess->key);
  147. recv_data(sess->last_recv_enc, sess);
  148. AESRound_data(sess->last_recv_enc, sess->key);
  149. }
  150. int do_connect(struct session *sess)
  151. {
  152. int en = connect(sess->params.sock, sess->params.addr, sess->params.addrlen);
  153. if (en != 0)
  154. return en;
  155. uint32_t rshort_key[8], short_key[8] = {0};
  156. fill_random(rshort_key, 32);
  157. recv_num(sess->eg.m, sess);
  158. recv_num(sess->eg.g, sess);
  159. recv_num(sess->eg.p, sess);
  160. mpz_t a, k, h;
  161. mpz_init(a);
  162. mpz_init(k);
  163. mpz_init(h);
  164. mpz_urandomm(a, sess->rs, sess->eg.m);
  165. mpz_import(k, 8, 1, 4, 1, 0, rshort_key);
  166. mpz_mod(k, k, sess->eg.m);
  167. size_t nbytes;
  168. void *buf = mpz_export(NULL, &nbytes, -1, 4, 1, 0, k);
  169. memcpy(short_key, buf, 32);
  170. free(buf);
  171. expandKey(short_key, sess->key);
  172. mpz_powm(h, sess->eg.g, a, sess->eg.m);
  173. mpz_powm(a, sess->eg.p, a, sess->eg.m);
  174. mpz_mul(a, k, a);
  175. mpz_mod(a, a, sess->eg.m);
  176. send_num(h, sess);
  177. send_num(a, sess);
  178. mpz_clear(a);
  179. mpz_clear(k);
  180. mpz_clear(h);
  181. initial_aes_handshake(sess);
  182. return 0;
  183. }
  184. int do_receive(struct session *sess)
  185. {
  186. int en = bind(sess->params.sock, sess->params.addr, sess->params.addrlen);
  187. if (en != 0)
  188. return en;
  189. en = listen(sess->params.sock, 1);
  190. if (en != 0)
  191. return en;
  192. en = accept(sess->params.sock, NULL, NULL);
  193. if (en == -1)
  194. return errno;
  195. close(sess->params.sock);
  196. sess->params.sock = en;
  197. struct linger linger = {0, 0};
  198. setsockopt(sess->params.sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(struct linger));
  199. uint32_t short_key[8] = {0};
  200. send_num(sess->eg.m, sess);
  201. send_num(sess->eg.g, sess);
  202. send_num(sess->eg.p, sess);
  203. mpz_t h, f, a;
  204. mpz_init(h);
  205. mpz_init(f);
  206. mpz_init(a);
  207. recv_num(h, sess);
  208. recv_num(a, sess);
  209. mpz_neg(f, sess->eg.x);
  210. mpz_powm(f, h, f, sess->eg.m);
  211. mpz_mul(a, a, f);
  212. mpz_mod(a, a, sess->eg.m);
  213. size_t nbytes;
  214. void *buf = mpz_export(NULL, &nbytes, -1, 4, 1, 0, a);
  215. memcpy(short_key, buf, 32);
  216. free(buf);
  217. expandKey(short_key, sess->key);
  218. mpz_clear(h);
  219. mpz_clear(f);
  220. mpz_clear(a);
  221. initial_aes_handshake(sess);
  222. return 0;
  223. }