crypt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * crypt.c -- handles:
  3. * psybnc crypt()
  4. * File encryption
  5. *
  6. */
  7. #include "main.h"
  8. #define CRYPT_BLOCKSIZE AES_BLOCK_SIZE
  9. #define CRYPT_KEYBITS 256
  10. #define CRYPT_KEYSIZE (CRYPT_KEYBITS / 8)
  11. AES_KEY e_key, d_key;
  12. char *encrypt_binary(const char *keydata, unsigned char *data, int *datalen)
  13. {
  14. int newdatalen = *datalen, blockcount = 0, blockndx = 0;
  15. unsigned char *newdata = NULL;
  16. /* First pad indata to CRYPT_BLOCKSIZE multiplum */
  17. if (newdatalen % CRYPT_BLOCKSIZE) /* more than 1 block? */
  18. newdatalen += (CRYPT_BLOCKSIZE - (newdatalen % CRYPT_BLOCKSIZE));
  19. newdata = (unsigned char *) nmalloc(newdatalen);
  20. egg_memcpy(newdata, data, *datalen);
  21. if (newdatalen != *datalen)
  22. egg_bzero((void *) &newdata[*datalen], (newdatalen - *datalen));
  23. *datalen = newdatalen;
  24. if ((!keydata) || (!keydata[0])) {
  25. /* No key, no encryption */
  26. egg_memcpy(newdata, data, newdatalen);
  27. } else {
  28. char key[CRYPT_KEYSIZE + 1];
  29. strncpyz(key, keydata, sizeof(key));
  30. // strncpyz(&key[sizeof(key) - strlen(keydata)], keydata, sizeof(key));
  31. AES_set_encrypt_key(key, CRYPT_KEYBITS, &e_key);
  32. /* Now loop through the blocks and crypt them */
  33. blockcount = newdatalen / CRYPT_BLOCKSIZE;
  34. for (blockndx = blockcount - 1; blockndx >= 0; blockndx--) {
  35. AES_encrypt(&newdata[blockndx * CRYPT_BLOCKSIZE], &newdata[blockndx * CRYPT_BLOCKSIZE], &e_key);
  36. }
  37. }
  38. return newdata;
  39. }
  40. char *decrypt_binary(const char *keydata, unsigned char *data, int datalen)
  41. {
  42. int blockcount = 0, blockndx = 0;
  43. unsigned char *newdata = NULL;
  44. datalen -= datalen % CRYPT_BLOCKSIZE;
  45. newdata = (unsigned char *) nmalloc(datalen);
  46. egg_memcpy(newdata, data, datalen);
  47. if ((!keydata) || (!keydata[0])) {
  48. /* No key, no decryption */
  49. } else {
  50. /* Init/fetch key */
  51. char key[CRYPT_KEYSIZE + 1];
  52. strncpyz(key, keydata, sizeof(key));
  53. // strncpy(&key[sizeof(key) - strlen(keydata)], keydata, sizeof(key));
  54. AES_set_decrypt_key(key, CRYPT_KEYBITS, &d_key);
  55. /* Now loop through the blocks and crypt them */
  56. blockcount = datalen / CRYPT_BLOCKSIZE;
  57. for (blockndx = blockcount - 1; blockndx >= 0; blockndx--) {
  58. AES_decrypt(&newdata[blockndx * CRYPT_BLOCKSIZE], &newdata[blockndx * CRYPT_BLOCKSIZE], &d_key);
  59. }
  60. }
  61. return newdata;
  62. }
  63. const char base64[64] = ".\\0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  64. const char base64r[256] = {
  65. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0,
  69. 0, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  70. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 0, 1, 0, 0, 0,
  71. 0, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  72. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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. };
  82. char *encrypt_string(const char *keydata, char *data)
  83. {
  84. int l, i, t;
  85. unsigned char *bdata;
  86. char *res;
  87. l = strlen(data) + 1;
  88. bdata = encrypt_binary(keydata, data, &l);
  89. if ((keydata) && (keydata[0])) {
  90. res = nmalloc((l * 4) / 3 + 5);
  91. #define DB(x) ((unsigned char) (x+i<l ? bdata[x+i] : 0))
  92. for (i = 0, t = 0; i < l; i += 3, t += 4) {
  93. res[t] = base64[DB(0) >> 2];
  94. res[t + 1] = base64[((DB(0) & 3) << 4) | (DB(1) >> 4)];
  95. res[t + 2] = base64[((DB(1) & 0x0F) << 2) | (DB(2) >> 6)];
  96. res[t + 3] = base64[(DB(2) & 0x3F)];
  97. }
  98. #undef DB
  99. res[t] = 0;
  100. nfree(bdata);
  101. return res;
  102. } else {
  103. return bdata;
  104. }
  105. }
  106. char *decrypt_string(const char *keydata, char *data)
  107. {
  108. int i, l, t;
  109. char *buf, *res;
  110. l = strlen(data);
  111. if ((keydata) && (keydata[0])) {
  112. buf = nmalloc((l * 3) / 4 + 6);
  113. #define DB(x) ((unsigned char) (x+i<l ? base64r[(unsigned char) data[x+i]] : 0))
  114. for (i = 0, t = 0; i < l; i += 4, t += 3) {
  115. buf[t] = (DB(0) << 2) + (DB(1) >> 4);
  116. buf[t + 1] = ((DB(1) & 0x0F) << 4) + (DB(2) >> 2);
  117. buf[t + 2] = ((DB(2) & 3) << 6) + DB(3);
  118. };
  119. #undef DB
  120. t += 3;
  121. t -= (t % 4);
  122. res = decrypt_binary(keydata, buf, t);
  123. nfree(buf);
  124. return res;
  125. } else {
  126. res = nmalloc(l + 1);
  127. strcpy(res, data);
  128. return res;
  129. }
  130. }
  131. void encrypt_pass(char *s1, char *s2)
  132. {
  133. /* fix this, standard eggs don't allow this long password hashes */
  134. char *tmp;
  135. if (strlen(s1) > 15)
  136. s1[15] = 0;
  137. tmp = encrypt_string(s1, s1);
  138. strcpy(s2, "+");
  139. strncat(s2, tmp, 15);
  140. s2[15] = 0;
  141. nfree(tmp);
  142. }
  143. int lfprintf EGG_VARARGS_DEF(FILE *, arg1)
  144. {
  145. va_list va;
  146. char buf[8192], *ln, *nln, *tmp, *format;
  147. int res;
  148. FILE *stream;
  149. stream = EGG_VARARGS_START(FILE *, arg1, va);
  150. format = va_arg(va, char *);
  151. egg_vsnprintf(buf, sizeof buf, format, va);
  152. ln = buf;
  153. while ((ln) && (ln[0])) {
  154. nln = strchr(ln, '\n');
  155. if (nln)
  156. *nln++ = 0;
  157. tmp = encrypt_string(SALT1, ln);
  158. res = fprintf(stream, "%s\n", tmp);
  159. nfree(tmp);
  160. if (res == EOF)
  161. return EOF;
  162. ln = nln;
  163. }
  164. return 0;
  165. }
  166. void EncryptFile(char *infile, char *outfile)
  167. {
  168. char buf[8192];
  169. FILE *f, *f2 = NULL;
  170. int std = 0;
  171. if (!strcmp(outfile, "STDOUT"))
  172. std = 1;
  173. f = fopen(infile, "r");
  174. if(!f)
  175. return;
  176. if (!std) {
  177. f2 = fopen(outfile, "w");
  178. if (!f2)
  179. return;
  180. } else {
  181. printf("----------------------------------START----------------------------------\n");
  182. }
  183. while (fscanf(f,"%[^\n]\n",buf) != EOF) {
  184. if (std)
  185. printf("%s\n", encrypt_string(SALT1, buf));
  186. else
  187. lfprintf(f2, "%s\n", buf);
  188. }
  189. if (std)
  190. printf("-----------------------------------END-----------------------------------\n");
  191. fclose(f);
  192. if (f2)
  193. fclose(f2);
  194. }
  195. void DecryptFile(char *infile, char *outfile)
  196. {
  197. char buf[8192], *temps;
  198. FILE *f, *f2 = NULL;
  199. int std = 0;
  200. if (!strcmp(outfile, "STDOUT"))
  201. std = 1;
  202. f = fopen(infile, "r");
  203. if (!f)
  204. return;
  205. if (!std) {
  206. f2 = fopen(outfile, "w");
  207. if (!f2)
  208. return;
  209. } else {
  210. printf("----------------------------------START----------------------------------\n");
  211. }
  212. while (fscanf(f,"%[^\n]\n",buf) != EOF) {
  213. temps = (char *) decrypt_string(SALT1, buf);
  214. if (!std)
  215. fprintf(f2, "%s\n",temps);
  216. else
  217. printf("%s\n", temps);
  218. nfree(temps);
  219. }
  220. if (std)
  221. printf("-----------------------------------END-----------------------------------\n");
  222. fclose(f);
  223. if (f2)
  224. fclose(f2);
  225. }
  226. char *md5(const char *string)
  227. {
  228. static char md5string[MD5_HASH_LENGTH + 1] = "";
  229. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  230. MD5_CTX ctx;
  231. MD5_Init(&ctx);
  232. MD5_Update(&ctx, string, strlen(string));
  233. MD5_Final(md5out, &ctx);
  234. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
  235. return md5string;
  236. }