crypt.c 9.5 KB

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