crypt.c 9.7 KB

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