crypt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * crypt.c -- handles:
  3. * psybnc crypt()
  4. * File encryption
  5. *
  6. */
  7. #include "common.h"
  8. #include "crypt.h"
  9. #include "settings.h"
  10. #include "misc.h"
  11. #include "base64.h"
  12. #include "src/crypto/crypto.h"
  13. #include <stdarg.h>
  14. #define CRYPT_BLOCKSIZE AES_BLOCK_SIZE
  15. #define CRYPT_KEYBITS 256
  16. #define CRYPT_KEYSIZE (CRYPT_KEYBITS >> 3)
  17. AES_KEY e_key, d_key;
  18. static unsigned char *
  19. encrypt_binary(const char *keydata, const unsigned char *in, size_t *inlen)
  20. {
  21. size_t len = *inlen;
  22. int blocks = 0, block = 0;
  23. unsigned char *out = NULL;
  24. /* First pad indata to CRYPT_BLOCKSIZE multiple */
  25. if (len % CRYPT_BLOCKSIZE) /* more than 1 block? */
  26. len += (CRYPT_BLOCKSIZE - (len % CRYPT_BLOCKSIZE));
  27. out = (unsigned char *) my_calloc(1, len + 1);
  28. egg_memcpy(out, in, *inlen);
  29. *inlen = len;
  30. if (!keydata || !*keydata) {
  31. /* No key, no encryption */
  32. egg_memcpy(out, in, len);
  33. } else {
  34. char key[CRYPT_KEYSIZE + 1] = "";
  35. strncpyz(key, keydata, sizeof(key));
  36. AES_set_encrypt_key((const unsigned char *) key, CRYPT_KEYBITS, &e_key);
  37. /* Now loop through the blocks and crypt them */
  38. blocks = len / CRYPT_BLOCKSIZE;
  39. for (block = blocks - 1; block >= 0; block--)
  40. AES_encrypt(&out[block * CRYPT_BLOCKSIZE], &out[block * CRYPT_BLOCKSIZE], &e_key);
  41. }
  42. out[len] = 0;
  43. return out;
  44. }
  45. static unsigned char *
  46. decrypt_binary(const char *keydata, unsigned char *in, size_t len)
  47. {
  48. int blocks = 0, block = 0;
  49. unsigned char *out = NULL;
  50. len -= len % CRYPT_BLOCKSIZE;
  51. out = (unsigned char *) my_calloc(1, len + 1);
  52. egg_memcpy(out, in, len);
  53. if (!keydata || !*keydata) {
  54. /* No key, no decryption */
  55. } else {
  56. /* Init/fetch key */
  57. char key[CRYPT_KEYSIZE + 1] = "";
  58. strncpyz(key, keydata, sizeof(key));
  59. AES_set_decrypt_key((const unsigned char *) key, CRYPT_KEYBITS, &d_key);
  60. /* Now loop through the blocks and crypt them */
  61. blocks = len / CRYPT_BLOCKSIZE;
  62. for (block = blocks - 1; block >= 0; block--)
  63. AES_decrypt(&out[block * CRYPT_BLOCKSIZE], &out[block * CRYPT_BLOCKSIZE], &d_key);
  64. }
  65. return out;
  66. }
  67. char *encrypt_string(const char *keydata, char *in)
  68. {
  69. size_t len = 0;
  70. unsigned char *bdata = NULL;
  71. char *res = NULL;
  72. len = strlen(in) + 1;
  73. bdata = encrypt_binary(keydata, (unsigned char *) in, &len);
  74. if (keydata && *keydata) {
  75. res = b64enc(bdata, len);
  76. free(bdata);
  77. return res;
  78. } else {
  79. return (char *) bdata;
  80. }
  81. }
  82. char *decrypt_string(const char *keydata, char *in)
  83. {
  84. size_t len = strlen(in);
  85. char *buf = NULL, *res = NULL;
  86. if (keydata && *keydata) {
  87. buf = b64dec((const unsigned char *) in, &len);
  88. res = (char *) decrypt_binary(keydata, (unsigned char *) buf, len);
  89. free(buf);
  90. return res;
  91. } else {
  92. res = (char *) my_calloc(1, len + 1);
  93. strcpy(res, in);
  94. return res;
  95. }
  96. }
  97. void encrypt_pass(char *s1, char *s2)
  98. {
  99. char *tmp = NULL;
  100. if (strlen(s1) > MAXPASSLEN)
  101. s1[MAXPASSLEN] = 0;
  102. tmp = encrypt_string(s1, s1);
  103. strcpy(s2, "+");
  104. strncat(s2, tmp, MAXPASSLEN);
  105. s2[MAXPASSLEN] = 0;
  106. free(tmp);
  107. }
  108. /*
  109. static char *passkey()
  110. {
  111. static char key[SHA1_HASH_LENGTH + 1] = "";
  112. if (key[0])
  113. return key;
  114. char *tmp = my_calloc(1, 512);
  115. sprintf(tmp, "%s-%s.%s!%s", settings.salt1, settings.salt2, settings.packname, settings.bdhash);
  116. key = SHA1(tmp);
  117. free(tmp);
  118. egg_bzero(tmp, 512);
  119. return key;
  120. }
  121. void encrypt_pass_new(char *s1, char *s2)
  122. {
  123. char *tmp = NULL;
  124. if (strlen(s1) > MAXPASSLEN)
  125. s1[MAXPASSLEN] = 0;
  126. tmp = encrypt_string(s1, passkey);
  127. strcpy(s2, "+");
  128. strncat(s2, tmp, MAXPASSLEN);
  129. s2[MAXPASSLEN] = 0;
  130. free(tmp);
  131. }
  132. */
  133. int lfprintf (FILE *stream, const char *format, ...)
  134. {
  135. va_list va;
  136. char buf[2048] = "", *ln = NULL, *nln = NULL, *tmp = NULL;
  137. int res;
  138. va_start(va, format);
  139. egg_vsnprintf(buf, sizeof buf, format, va);
  140. va_end(va);
  141. ln = buf;
  142. while (ln && *ln) {
  143. if ((nln = strchr(ln, '\n')))
  144. *nln++ = 0;
  145. tmp = encrypt_string(settings.salt1, ln);
  146. res = fprintf(stream, "%s\n", tmp);
  147. free(tmp);
  148. if (res == EOF)
  149. return EOF;
  150. ln = nln;
  151. }
  152. return 0;
  153. }
  154. void Encrypt_File(char *infile, char *outfile)
  155. {
  156. FILE *f = NULL, *f2 = NULL;
  157. bool std = 0;
  158. if (!strcmp(outfile, "STDOUT"))
  159. std = 1;
  160. f = fopen(infile, "r");
  161. if(!f)
  162. return;
  163. if (!std) {
  164. f2 = fopen(outfile, "w");
  165. if (!f2)
  166. return;
  167. } else {
  168. printf("----------------------------------START----------------------------------\n");
  169. }
  170. char *buf = (char *) my_calloc(1, 1024);
  171. while (fgets(buf, 1024, f) != NULL) {
  172. remove_crlf(buf);
  173. if (std)
  174. printf("%s\n", encrypt_string(settings.salt1, buf));
  175. else
  176. lfprintf(f2, "%s\n", buf);
  177. buf[0] = 0;
  178. }
  179. free(buf);
  180. if (std)
  181. printf("-----------------------------------END-----------------------------------\n");
  182. fclose(f);
  183. if (f2)
  184. fclose(f2);
  185. }
  186. void Decrypt_File(char *infile, char *outfile)
  187. {
  188. FILE *f = NULL, *f2 = NULL;
  189. bool std = 0;
  190. if (!strcmp(outfile, "STDOUT"))
  191. std = 1;
  192. f = fopen(infile, "r");
  193. if (!f)
  194. return;
  195. if (!std) {
  196. f2 = fopen(outfile, "w");
  197. if (!f2)
  198. return;
  199. } else {
  200. printf("----------------------------------START----------------------------------\n");
  201. }
  202. char *buf = (char *) my_calloc(1, 2048);
  203. while (fgets(buf, 2048, f) != NULL) {
  204. char *temps = NULL;
  205. remove_crlf(buf);
  206. temps = (char *) decrypt_string(settings.salt1, buf);
  207. if (!std)
  208. fprintf(f2, "%s\n",temps);
  209. else
  210. printf("%s\n", temps);
  211. free(temps);
  212. buf[0] = 0;
  213. }
  214. free(buf);
  215. if (std)
  216. printf("-----------------------------------END-----------------------------------\n");
  217. fclose(f);
  218. if (f2)
  219. fclose(f2);
  220. }
  221. char *MD5(const char *string)
  222. {
  223. static char md5string[MD5_HASH_LENGTH + 1] = "";
  224. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  225. MD5_CTX ctx;
  226. MD5_Init(&ctx);
  227. MD5_Update(&ctx, string, strlen(string));
  228. MD5_Final(md5out, &ctx);
  229. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  230. OPENSSL_cleanse(&ctx, sizeof(ctx));
  231. return md5string;
  232. }
  233. char *
  234. MD5FILE(const char *bin)
  235. {
  236. FILE *f = NULL;
  237. if (!(f = fopen(bin, "rb")))
  238. return "";
  239. static char md5string[MD5_HASH_LENGTH + 1] = "";
  240. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buffer[1024] = "";
  241. MD5_CTX ctx;
  242. size_t binsize = 0, len = 0;
  243. MD5_Init(&ctx);
  244. while ((len = fread(buffer, 1, sizeof buffer, f))) {
  245. binsize += len;
  246. MD5_Update(&ctx, buffer, len);
  247. }
  248. MD5_Final(md5out, &ctx);
  249. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  250. OPENSSL_cleanse(&ctx, sizeof(ctx));
  251. return md5string;
  252. }
  253. char *SHA1(const char *string)
  254. {
  255. static char sha1string[SHA_HASH_LENGTH + 1] = "";
  256. unsigned char sha1out[SHA_HASH_LENGTH + 1] = "";
  257. SHA_CTX ctx;
  258. SHA1_Init(&ctx);
  259. SHA1_Update(&ctx, string, strlen(string));
  260. SHA1_Final(sha1out, &ctx);
  261. strncpyz(sha1string, btoh(sha1out, SHA_DIGEST_LENGTH), sizeof(sha1string));
  262. OPENSSL_cleanse(&ctx, sizeof(ctx));
  263. return sha1string;
  264. }
  265. /* convert binary hashes to hex */
  266. char *btoh(const unsigned char *md, size_t len)
  267. {
  268. char buf[100] = "", *ret = NULL;
  269. for (size_t i = 0; i < len; i+=4) {
  270. sprintf(&(buf[i << 1]), "%02x", md[i]);
  271. sprintf(&(buf[(i + 1) << 1]), "%02x", md[i + 1]);
  272. sprintf(&(buf[(i + 2) << 1]), "%02x", md[i + 2]);
  273. sprintf(&(buf[(i + 3) << 1]), "%02x", md[i + 3]);
  274. }
  275. ret = buf;
  276. return ret;
  277. }