crypt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 *) 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 *) 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 *) 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. int lfprintf (FILE *stream, const char *format, ...)
  109. {
  110. va_list va;
  111. char buf[2048] = "", *ln = NULL, *nln = NULL, *tmp = NULL;
  112. int res;
  113. va_start(va, format);
  114. egg_vsnprintf(buf, sizeof buf, format, va);
  115. va_end(va);
  116. ln = buf;
  117. while (ln && *ln) {
  118. if ((nln = strchr(ln, '\n')))
  119. *nln++ = 0;
  120. tmp = encrypt_string(settings.salt1, ln);
  121. res = fprintf(stream, "%s\n", tmp);
  122. free(tmp);
  123. if (res == EOF)
  124. return EOF;
  125. ln = nln;
  126. }
  127. return 0;
  128. }
  129. void Encrypt_File(char *infile, char *outfile)
  130. {
  131. FILE *f = NULL, *f2 = NULL;
  132. bool std = 0;
  133. if (!strcmp(outfile, "STDOUT"))
  134. std = 1;
  135. f = fopen(infile, "r");
  136. if(!f)
  137. return;
  138. if (!std) {
  139. f2 = fopen(outfile, "w");
  140. if (!f2)
  141. return;
  142. } else {
  143. printf("----------------------------------START----------------------------------\n");
  144. }
  145. char *buf = (char *) calloc(1, 1024);
  146. while (fgets(buf, 1024, f) != NULL) {
  147. remove_crlf(buf);
  148. if (std)
  149. printf("%s\n", encrypt_string(settings.salt1, buf));
  150. else
  151. lfprintf(f2, "%s\n", buf);
  152. buf[0] = 0;
  153. }
  154. free(buf);
  155. if (std)
  156. printf("-----------------------------------END-----------------------------------\n");
  157. fclose(f);
  158. if (f2)
  159. fclose(f2);
  160. }
  161. void Decrypt_File(char *infile, char *outfile)
  162. {
  163. FILE *f = NULL, *f2 = NULL;
  164. bool std = 0;
  165. if (!strcmp(outfile, "STDOUT"))
  166. std = 1;
  167. f = fopen(infile, "r");
  168. if (!f)
  169. return;
  170. if (!std) {
  171. f2 = fopen(outfile, "w");
  172. if (!f2)
  173. return;
  174. } else {
  175. printf("----------------------------------START----------------------------------\n");
  176. }
  177. char *buf = (char *) calloc(1, 2048);
  178. while (fgets(buf, 2048, f) != NULL) {
  179. char *temps = NULL;
  180. remove_crlf(buf);
  181. temps = (char *) decrypt_string(settings.salt1, buf);
  182. if (!std)
  183. fprintf(f2, "%s\n",temps);
  184. else
  185. printf("%s\n", temps);
  186. free(temps);
  187. buf[0] = 0;
  188. }
  189. free(buf);
  190. if (std)
  191. printf("-----------------------------------END-----------------------------------\n");
  192. fclose(f);
  193. if (f2)
  194. fclose(f2);
  195. }
  196. char *MD5(const char *string)
  197. {
  198. static char md5string[MD5_HASH_LENGTH + 1] = "";
  199. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  200. MD5_CTX ctx;
  201. MD5_Init(&ctx);
  202. MD5_Update(&ctx, string, strlen(string));
  203. MD5_Final(md5out, &ctx);
  204. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  205. OPENSSL_cleanse(&ctx, sizeof(ctx));
  206. return md5string;
  207. }
  208. char *
  209. MD5FILE(const char *bin)
  210. {
  211. FILE *f = NULL;
  212. if (!(f = fopen(bin, "rb")))
  213. return "";
  214. static char md5string[MD5_HASH_LENGTH + 1] = "";
  215. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buffer[1024] = "";
  216. MD5_CTX ctx;
  217. size_t binsize = 0, len = 0;
  218. MD5_Init(&ctx);
  219. while ((len = fread(buffer, 1, sizeof buffer, f))) {
  220. binsize += len;
  221. MD5_Update(&ctx, buffer, len);
  222. }
  223. MD5_Final(md5out, &ctx);
  224. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  225. OPENSSL_cleanse(&ctx, sizeof(ctx));
  226. return md5string;
  227. }
  228. char *SHA1(const char *string)
  229. {
  230. static char sha1string[SHA_HASH_LENGTH + 1] = "";
  231. unsigned char sha1out[SHA_HASH_LENGTH + 1] = "";
  232. SHA_CTX ctx;
  233. SHA1_Init(&ctx);
  234. SHA1_Update(&ctx, string, strlen(string));
  235. SHA1_Final(sha1out, &ctx);
  236. strncpyz(sha1string, btoh(sha1out, SHA_DIGEST_LENGTH), sizeof(sha1string));
  237. OPENSSL_cleanse(&ctx, sizeof(ctx));
  238. return sha1string;
  239. }
  240. /* convert binary hashes to hex */
  241. char *btoh(const unsigned char *md, size_t len)
  242. {
  243. char buf[100] = "", *ret = NULL;
  244. for (size_t i = 0; i < len; i+=4) {
  245. sprintf(&(buf[i << 1]), "%02x", md[i]);
  246. sprintf(&(buf[(i + 1) << 1]), "%02x", md[i + 1]);
  247. sprintf(&(buf[(i + 2) << 1]), "%02x", md[i + 2]);
  248. sprintf(&(buf[(i + 3) << 1]), "%02x", md[i + 3]);
  249. }
  250. ret = buf;
  251. return ret;
  252. }