crypt.c 6.8 KB

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