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