crypt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "src/crypto/crypto.h"
  12. #include <stdarg.h>
  13. #define CRYPT_BLOCKSIZE AES_BLOCK_SIZE
  14. #define CRYPT_KEYBITS 256
  15. #define CRYPT_KEYSIZE (CRYPT_KEYBITS / 8)
  16. AES_KEY e_key, d_key;
  17. char *encrypt_binary(const char *keydata, unsigned char *data, int *datalen)
  18. {
  19. int newdatalen = *datalen, blockcount = 0, blockndx = 0;
  20. unsigned char *newdata = NULL;
  21. /* First pad indata to CRYPT_BLOCKSIZE multiplum */
  22. if (newdatalen % CRYPT_BLOCKSIZE) /* more than 1 block? */
  23. newdatalen += (CRYPT_BLOCKSIZE - (newdatalen % CRYPT_BLOCKSIZE));
  24. newdata = (unsigned char *) calloc(1, newdatalen);
  25. egg_memcpy(newdata, data, *datalen);
  26. if (newdatalen != *datalen)
  27. egg_bzero((void *) &newdata[*datalen], (newdatalen - *datalen));
  28. *datalen = newdatalen;
  29. if ((!keydata) || (!keydata[0])) {
  30. /* No key, no encryption */
  31. egg_memcpy(newdata, data, newdatalen);
  32. } else {
  33. char key[CRYPT_KEYSIZE + 1] = "";
  34. strncpyz(key, keydata, sizeof(key));
  35. /* strncpyz(&key[sizeof(key) - strlen(keydata)], keydata, sizeof(key)); */
  36. AES_set_encrypt_key(key, CRYPT_KEYBITS, &e_key);
  37. /* Now loop through the blocks and crypt them */
  38. blockcount = newdatalen / CRYPT_BLOCKSIZE;
  39. for (blockndx = blockcount - 1; blockndx >= 0; blockndx--) {
  40. AES_encrypt(&newdata[blockndx * CRYPT_BLOCKSIZE], &newdata[blockndx * CRYPT_BLOCKSIZE], &e_key);
  41. }
  42. }
  43. return newdata;
  44. }
  45. char *decrypt_binary(const char *keydata, unsigned char *data, int datalen)
  46. {
  47. int blockcount = 0, blockndx = 0;
  48. unsigned char *newdata = NULL;
  49. datalen -= datalen % CRYPT_BLOCKSIZE;
  50. newdata = (unsigned char *) malloc(datalen);
  51. egg_memcpy(newdata, data, datalen);
  52. if ((!keydata) || (!keydata[0])) {
  53. /* No key, no decryption */
  54. } else {
  55. /* Init/fetch key */
  56. char key[CRYPT_KEYSIZE + 1] = "";
  57. strncpyz(key, keydata, sizeof(key));
  58. /* strncpy(&key[sizeof(key) - strlen(keydata)], keydata, sizeof(key)); */
  59. AES_set_decrypt_key(key, CRYPT_KEYBITS, &d_key);
  60. /* Now loop through the blocks and crypt them */
  61. blockcount = datalen / CRYPT_BLOCKSIZE;
  62. for (blockndx = blockcount - 1; blockndx >= 0; blockndx--) {
  63. AES_decrypt(&newdata[blockndx * CRYPT_BLOCKSIZE], &newdata[blockndx * CRYPT_BLOCKSIZE], &d_key);
  64. }
  65. }
  66. return newdata;
  67. }
  68. const char base64[64] = ".\\0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  69. const char base64r[256] = {
  70. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  71. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0,
  74. 0, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  75. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 0, 1, 0, 0, 0,
  76. 0, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  77. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. };
  87. char *encrypt_string(const char *keydata, char *data)
  88. {
  89. int l, i, t;
  90. unsigned char *bdata = NULL;
  91. char *res = NULL;
  92. l = strlen(data) + 1;
  93. bdata = encrypt_binary(keydata, data, &l);
  94. if ((keydata) && (keydata[0])) {
  95. res = malloc((l * 4) / 3 + 5);
  96. #define DB(x) ((unsigned char) (x+i<l ? bdata[x+i] : 0))
  97. for (i = 0, t = 0; i < l; i += 3, t += 4) {
  98. res[t] = base64[DB(0) >> 2];
  99. res[t + 1] = base64[((DB(0) & 3) << 4) | (DB(1) >> 4)];
  100. res[t + 2] = base64[((DB(1) & 0x0F) << 2) | (DB(2) >> 6)];
  101. res[t + 3] = base64[(DB(2) & 0x3F)];
  102. }
  103. #undef DB
  104. res[t] = 0;
  105. free(bdata);
  106. return res;
  107. } else {
  108. return bdata;
  109. }
  110. }
  111. char *decrypt_string(const char *keydata, char *data)
  112. {
  113. int i, l, t;
  114. char *buf = NULL, *res = NULL;
  115. l = strlen(data);
  116. if ((keydata) && (keydata[0])) {
  117. buf = malloc((l * 3) / 4 + 6);
  118. #define DB(x) ((unsigned char) (x+i<l ? base64r[(unsigned char) data[x+i]] : 0))
  119. for (i = 0, t = 0; i < l; i += 4, t += 3) {
  120. buf[t] = (DB(0) << 2) + (DB(1) >> 4);
  121. buf[t + 1] = ((DB(1) & 0x0F) << 4) + (DB(2) >> 2);
  122. buf[t + 2] = ((DB(2) & 3) << 6) + DB(3);
  123. };
  124. #undef DB
  125. t += 3;
  126. t -= (t % 4);
  127. res = decrypt_binary(keydata, buf, t);
  128. free(buf);
  129. return res;
  130. } else {
  131. res = malloc(l + 1);
  132. strcpy(res, data);
  133. return res;
  134. }
  135. }
  136. void encrypt_pass(char *s1, char *s2)
  137. {
  138. char *tmp = NULL;
  139. if (strlen(s1) > 15)
  140. s1[15] = 0;
  141. tmp = encrypt_string(s1, s1);
  142. strcpy(s2, "+");
  143. strncat(s2, tmp, 15);
  144. s2[15] = 0;
  145. free(tmp);
  146. }
  147. int lfprintf (FILE *stream, ...)
  148. {
  149. va_list va;
  150. char buf[8192], *ln = NULL, *nln = NULL, *tmp = NULL, *format = NULL;
  151. int res;
  152. buf[0] = 0;
  153. va_start(va, stream);
  154. format = va_arg(va, char *);
  155. egg_vsnprintf(buf, sizeof buf, format, va);
  156. va_end(va);
  157. ln = buf;
  158. while ((ln) && (ln[0])) {
  159. nln = strchr(ln, '\n');
  160. if (nln)
  161. *nln++ = 0;
  162. tmp = encrypt_string(SALT1, ln);
  163. res = fprintf(stream, "%s\n", tmp);
  164. free(tmp);
  165. if (res == EOF)
  166. return EOF;
  167. ln = nln;
  168. }
  169. return 0;
  170. }
  171. void Encrypt_File(char *infile, char *outfile)
  172. {
  173. char buf[8192];
  174. FILE *f = NULL, *f2 = NULL;
  175. int std = 0;
  176. if (!strcmp(outfile, "STDOUT"))
  177. std = 1;
  178. f = fopen(infile, "r");
  179. if(!f)
  180. return;
  181. if (!std) {
  182. f2 = fopen(outfile, "w");
  183. if (!f2)
  184. return;
  185. } else {
  186. printf("----------------------------------START----------------------------------\n");
  187. }
  188. buf[0] = 0;
  189. while (fscanf(f, "%[^\n]\n", buf) != EOF) {
  190. if (std)
  191. printf("%s\n", encrypt_string(SALT1, buf));
  192. else
  193. lfprintf(f2, "%s\n", buf);
  194. }
  195. if (std)
  196. printf("-----------------------------------END-----------------------------------\n");
  197. fclose(f);
  198. if (f2)
  199. fclose(f2);
  200. }
  201. void Decrypt_File(char *infile, char *outfile)
  202. {
  203. char buf[8192], *temps = NULL;
  204. FILE *f = NULL, *f2 = NULL;
  205. int std = 0;
  206. if (!strcmp(outfile, "STDOUT"))
  207. std = 1;
  208. f = fopen(infile, "r");
  209. if (!f)
  210. return;
  211. if (!std) {
  212. f2 = fopen(outfile, "w");
  213. if (!f2)
  214. return;
  215. } else {
  216. printf("----------------------------------START----------------------------------\n");
  217. }
  218. buf[0] = 0;
  219. while (fscanf(f, "%[^\n]\n", buf) != EOF) {
  220. temps = (char *) decrypt_string(SALT1, buf);
  221. if (!std)
  222. fprintf(f2, "%s\n",temps);
  223. else
  224. printf("%s\n", temps);
  225. free(temps);
  226. }
  227. if (std)
  228. printf("-----------------------------------END-----------------------------------\n");
  229. fclose(f);
  230. if (f2)
  231. fclose(f2);
  232. }
  233. char *MD5(const char *string)
  234. {
  235. static char md5string[MD5_HASH_LENGTH + 1] = "";
  236. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  237. MD5_CTX ctx;
  238. MD5_Init(&ctx);
  239. MD5_Update(&ctx, string, strlen(string));
  240. MD5_Final(md5out, &ctx);
  241. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  242. OPENSSL_cleanse(&ctx, sizeof(ctx));
  243. return md5string;
  244. }
  245. char *
  246. MD5FILE(const char *bin)
  247. {
  248. static char md5string[MD5_HASH_LENGTH + 1] = "";
  249. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buffer[1024] = "";
  250. MD5_CTX ctx;
  251. size_t binsize = 0, len = 0;
  252. FILE *f = NULL;
  253. if (!(f = fopen(bin, "rb")))
  254. return "";
  255. MD5_Init(&ctx);
  256. while ((len = fread(buffer, 1, sizeof buffer, f))) {
  257. binsize += len;
  258. MD5_Update(&ctx, buffer, len);
  259. }
  260. MD5_Final(md5out, &ctx);
  261. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  262. OPENSSL_cleanse(&ctx, sizeof(ctx));
  263. return md5string;
  264. }
  265. char *SHA1(const char *string)
  266. {
  267. static char sha1string[SHA_HASH_LENGTH + 1] = "";
  268. unsigned char sha1out[SHA_HASH_LENGTH + 1] = "";
  269. SHA_CTX ctx;
  270. SHA1_Init(&ctx);
  271. SHA1_Update(&ctx, string, strlen(string));
  272. SHA1_Final(sha1out, &ctx);
  273. strncpyz(sha1string, btoh(sha1out, SHA_DIGEST_LENGTH), sizeof(sha1string));
  274. OPENSSL_cleanse(&ctx, sizeof(ctx));
  275. return sha1string;
  276. }
  277. /* convert binary hashes to hex */
  278. char *btoh(const unsigned char *md, int len)
  279. {
  280. int i;
  281. char buf[100] = "", *ret = NULL;
  282. for (i = 0; i < len; i++)
  283. sprintf(&(buf[i*2]), "%02x", md[i]);
  284. ret = buf;
  285. return ret;
  286. }