crypt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. unsigned char *
  19. encrypt_binary(const char *keydata, 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. strlcpy(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. 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. strlcpy(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);
  73. bdata = encrypt_binary(keydata, (unsigned char *) in, &len);
  74. if (keydata && *keydata) {
  75. res = b64enc(bdata, len);
  76. OPENSSL_cleanse(bdata, len);
  77. free(bdata);
  78. return res;
  79. } else {
  80. return (char *) bdata;
  81. }
  82. }
  83. char *decrypt_string(const char *keydata, char *in)
  84. {
  85. size_t len = strlen(in);
  86. char *buf = NULL, *res = NULL;
  87. if (keydata && *keydata) {
  88. buf = b64dec((const unsigned char *) in, &len);
  89. res = (char *) decrypt_binary(keydata, (unsigned char *) buf, &len);
  90. OPENSSL_cleanse(buf, len);
  91. free(buf);
  92. return res;
  93. } else {
  94. res = (char *) my_calloc(1, len + 1);
  95. strlcpy(res, in, len + 1);
  96. return res;
  97. }
  98. }
  99. void encrypt_cmd_pass(char *in, char *out)
  100. {
  101. char *tmp = NULL;
  102. if (strlen(in) > MAXPASSLEN)
  103. in[MAXPASSLEN] = 0;
  104. tmp = encrypt_string(in, in);
  105. strlcpy(out, "+", 2);
  106. strlcat(out, tmp, MAXPASSLEN + 1);
  107. out[MAXPASSLEN] = 0;
  108. free(tmp);
  109. }
  110. static char *user_key(struct userrec *u)
  111. {
  112. /* FIXME: fix after 1.2.3 */
  113. return u->handle;
  114. }
  115. char *encrypt_pass(struct userrec *u, char *in)
  116. {
  117. char *tmp = NULL, buf[101] = "", *ret = NULL;
  118. size_t ret_size = 0;
  119. if (strlen(in) > MAXPASSLEN)
  120. in[MAXPASSLEN] = 0;
  121. simple_snprintf(buf, sizeof(buf), STR("%s-%s"), settings.salt2, in);
  122. tmp = encrypt_string(user_key(u), buf);
  123. OPENSSL_cleanse(buf, sizeof(buf));
  124. ret_size = strlen(tmp) + 1 + 1;
  125. ret = (char *) my_calloc(1, ret_size);
  126. simple_snprintf(ret, ret_size, STR("+%s"), tmp);
  127. free(tmp);
  128. return ret;
  129. }
  130. char *decrypt_pass(struct userrec *u)
  131. {
  132. char *tmp = NULL, *p = NULL, *ret = NULL, *pass = NULL;
  133. pass = (char *) get_user(&USERENTRY_PASS, u);
  134. if (pass && pass[0] == '+') {
  135. tmp = decrypt_string(user_key(u), &pass[1]);
  136. if ((p = strchr(tmp, '-')))
  137. ret = strdup(++p);
  138. free(tmp);
  139. }
  140. if (!ret)
  141. ret = (char *) my_calloc(1, 1);
  142. return ret;
  143. }
  144. /*
  145. static char *passkey()
  146. {
  147. static char key[SHA1_HASH_LENGTH + 1] = "";
  148. if (key[0])
  149. return key;
  150. char *tmp = my_calloc(1, 512);
  151. simple_snprintf(tmp, sizeof(tmp), "%s-%s.%s!%s", settings.salt1, settings.salt2, settings.packname, settings.bdhash);
  152. key = SHA1(tmp);
  153. free(tmp);
  154. egg_bzero(tmp, 512);
  155. return key;
  156. }
  157. void encrypt_pass_new(char *s1, char *s2)
  158. {
  159. char *tmp = NULL;
  160. if (strlen(s1) > MAXPASSLEN)
  161. s1[MAXPASSLEN] = 0;
  162. tmp = encrypt_string(s1, passkey);
  163. strcpy(s2, "+");
  164. strlcat(s2, tmp, MAXPASSLEN + 1);
  165. s2[MAXPASSLEN] = 0;
  166. free(tmp);
  167. }
  168. */
  169. int lfprintf (FILE *stream, const char *format, ...)
  170. {
  171. va_list va;
  172. char buf[2048] = "", *ln = NULL, *nln = NULL, *tmp = NULL;
  173. int res;
  174. va_start(va, format);
  175. egg_vsnprintf(buf, sizeof buf, format, va);
  176. va_end(va);
  177. ln = buf;
  178. while (ln && *ln) {
  179. if ((nln = strchr(ln, '\n')))
  180. *nln++ = 0;
  181. tmp = encrypt_string(settings.salt1, ln);
  182. res = fprintf(stream, "%s\n", tmp);
  183. free(tmp);
  184. if (res == EOF)
  185. return EOF;
  186. ln = nln;
  187. }
  188. return 0;
  189. }
  190. void Encrypt_File(char *infile, char *outfile)
  191. {
  192. FILE *f = NULL, *f2 = NULL;
  193. bool std = 0;
  194. if (!strcmp(outfile, "STDOUT"))
  195. std = 1;
  196. f = fopen(infile, "r");
  197. if(!f)
  198. return;
  199. if (!std) {
  200. f2 = fopen(outfile, "w");
  201. if (!f2)
  202. return;
  203. } else {
  204. printf(STR("----------------------------------START----------------------------------\n"));
  205. }
  206. char *buf = (char *) my_calloc(1, 1024);
  207. while (fgets(buf, 1024, f) != NULL) {
  208. remove_crlf(buf);
  209. if (std)
  210. printf("%s\n", encrypt_string(settings.salt1, buf));
  211. else
  212. lfprintf(f2, "%s\n", buf);
  213. buf[0] = 0;
  214. }
  215. free(buf);
  216. if (std)
  217. printf(STR("-----------------------------------END-----------------------------------\n"));
  218. fclose(f);
  219. if (f2)
  220. fclose(f2);
  221. }
  222. void Decrypt_File(char *infile, char *outfile)
  223. {
  224. FILE *f = NULL, *f2 = NULL;
  225. bool std = 0;
  226. if (!strcmp(outfile, "STDOUT"))
  227. std = 1;
  228. f = fopen(infile, "r");
  229. if (!f)
  230. return;
  231. if (!std) {
  232. f2 = fopen(outfile, "w");
  233. if (!f2)
  234. return;
  235. } else {
  236. printf(STR("----------------------------------START----------------------------------\n"));
  237. }
  238. char *buf = (char *) my_calloc(1, 2048);
  239. while (fgets(buf, 2048, f) != NULL) {
  240. char *temps = NULL;
  241. remove_crlf(buf);
  242. temps = (char *) decrypt_string(settings.salt1, buf);
  243. if (!std)
  244. fprintf(f2, "%s\n",temps);
  245. else
  246. printf("%s\n", temps);
  247. free(temps);
  248. buf[0] = 0;
  249. }
  250. free(buf);
  251. if (std)
  252. printf(STR("-----------------------------------END-----------------------------------\n"));
  253. fclose(f);
  254. if (f2)
  255. fclose(f2);
  256. }
  257. char *MD5(const char *string)
  258. {
  259. static int n = 0;
  260. static char ret[5][MD5_HASH_LENGTH + 1];
  261. //Cleanse the current buffer
  262. if (!string) {
  263. OPENSSL_cleanse(ret[n], MD5_HASH_LENGTH + 1);
  264. return NULL;
  265. }
  266. char* md5string = ret[n++];
  267. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  268. MD5_CTX ctx;
  269. MD5_Init(&ctx);
  270. MD5_Update(&ctx, string, strlen(string));
  271. MD5_Final(md5out, &ctx);
  272. strlcpy(md5string, btoh(md5out, MD5_DIGEST_LENGTH), MD5_HASH_LENGTH + 1);
  273. OPENSSL_cleanse(&ctx, sizeof(ctx));
  274. if (n == 5) n = 0;
  275. return md5string;
  276. }
  277. int md5cmp(const char *hash, const char *string) {
  278. int n = strcmp(hash, MD5(string));
  279. MD5(NULL);
  280. return n;
  281. }
  282. char *
  283. MD5FILE(const char *bin)
  284. {
  285. FILE *f = NULL;
  286. if (!(f = fopen(bin, "rb")))
  287. return "";
  288. static char md5string[MD5_HASH_LENGTH + 1] = "";
  289. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buffer[1024] = "";
  290. MD5_CTX ctx;
  291. size_t binsize = 0, len = 0;
  292. MD5_Init(&ctx);
  293. while ((len = fread(buffer, 1, sizeof buffer, f))) {
  294. binsize += len;
  295. MD5_Update(&ctx, buffer, len);
  296. }
  297. MD5_Final(md5out, &ctx);
  298. strlcpy(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  299. OPENSSL_cleanse(&ctx, sizeof(ctx));
  300. return md5string;
  301. }
  302. char *SHA1(const char *string)
  303. {
  304. static int n = 0;
  305. static char ret[5][SHA_HASH_LENGTH + 1];
  306. //Cleanse the current buffer
  307. if (!string) {
  308. OPENSSL_cleanse(ret[n], SHA_HASH_LENGTH + 1);
  309. return NULL;
  310. }
  311. char* sha1string = ret[n++];
  312. unsigned char sha1out[SHA_HASH_LENGTH + 1] = "";
  313. SHA_CTX ctx;
  314. SHA1_Init(&ctx);
  315. SHA1_Update(&ctx, string, strlen(string));
  316. SHA1_Final(sha1out, &ctx);
  317. strlcpy(sha1string, btoh(sha1out, SHA_DIGEST_LENGTH), SHA_HASH_LENGTH + 1);
  318. OPENSSL_cleanse(&ctx, sizeof(ctx));
  319. if (n == 5) n = 0;
  320. return sha1string;
  321. }
  322. int sha1cmp(const char *hash, const char *string) {
  323. int n = strcmp(hash, SHA1(string));
  324. SHA1(NULL);
  325. return n;
  326. }
  327. /* convert binary hashes to hex */
  328. char *btoh(const unsigned char *md, size_t len)
  329. {
  330. char buf[100] = "", *ret = NULL;
  331. for (size_t i = 0; i < len; i+=4) {
  332. sprintf(&(buf[i << 1]), "%02x", md[i]);
  333. sprintf(&(buf[(i + 1) << 1]), "%02x", md[i + 1]);
  334. sprintf(&(buf[(i + 2) << 1]), "%02x", md[i + 2]);
  335. sprintf(&(buf[(i + 3) << 1]), "%02x", md[i + 3]);
  336. }
  337. ret = buf;
  338. return ret;
  339. }
  340. #ifdef k
  341. void do_crypt_console()
  342. {
  343. char inbuf[1024] = "";
  344. int which = 5;
  345. char *p = NULL;
  346. printf("Crypt menu:\n");
  347. printf("-----------\n");
  348. printf("1) String\n");
  349. printf("2) File\n");
  350. printf("1) MD5\n");
  351. printf("2) SHA1\n");
  352. printf("3) AES256 (binary)\n");
  353. printf("4) AES256+base64\n");
  354. printf("5) exit\n");
  355. printf("\n");
  356. printf("[5]: ");
  357. fgets(inbuf, sizeof(inbuf), stdin);
  358. if ((p = strchr(inbuf, '\n')))
  359. *p = 0;
  360. which = atoi(inbuf);
  361. switch (which) {
  362. case
  363. }
  364. #endif