crypt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include <bdlib/src/String.h>
  15. char *encrypt_string(const char *keydata, char *in)
  16. {
  17. size_t len = 0;
  18. unsigned char *bdata = NULL;
  19. char *res = NULL;
  20. len = strlen(in);
  21. bdata = aes_encrypt_ecb_binary(keydata, (unsigned char *) in, &len);
  22. if (keydata && *keydata) {
  23. res = b64enc(bdata, len);
  24. OPENSSL_cleanse(bdata, len);
  25. free(bdata);
  26. return res;
  27. } else {
  28. return (char *) bdata;
  29. }
  30. }
  31. /**
  32. * @brief Encrypt a string
  33. * @param key The key to encrypt with
  34. * @param data The string to encrypt
  35. * @return A new, encrypted string
  36. */
  37. bd::String encrypt_string(const bd::String& key, const bd::String& data) {
  38. if (!key) return data;
  39. size_t len = data.length();
  40. char *bdata = (char*) aes_encrypt_ecb_binary(key.c_str(), (unsigned char*) data.c_str(), &len);
  41. bd::String encrypted(bdata, len);
  42. free(bdata);
  43. return encrypted;
  44. }
  45. char *decrypt_string(const char *keydata, char *in)
  46. {
  47. size_t len = strlen(in);
  48. char *buf = NULL, *res = NULL;
  49. if (keydata && *keydata) {
  50. buf = b64dec((const unsigned char *) in, &len);
  51. res = (char *) aes_decrypt_ecb_binary(keydata, (unsigned char *) buf, &len);
  52. OPENSSL_cleanse(buf, len);
  53. free(buf);
  54. return res;
  55. } else {
  56. res = (char *) my_calloc(1, len + 1);
  57. strlcpy(res, in, len + 1);
  58. return res;
  59. }
  60. }
  61. /**
  62. * @brief Decrypt a string
  63. * @param key The key to decrypt with
  64. * @param data The string to decrypt
  65. * @return A new, decrypted string
  66. */
  67. bd::String decrypt_string(const bd::String& key, const bd::String& data) {
  68. if (!key) return data;
  69. size_t len = data.length();
  70. char *bdata = (char*) aes_decrypt_ecb_binary(key.c_str(), (unsigned char*) data.c_str(), &len);
  71. bd::String decrypted(bdata, len);
  72. OPENSSL_cleanse(bdata, len);
  73. free(bdata);
  74. return decrypted;
  75. }
  76. char *salted_sha1(const char *in, const char* saltin)
  77. {
  78. char *tmp = NULL, buf[101] = "", *ret = NULL;
  79. size_t ret_size = 0;
  80. /* Create a 5 byte salt */
  81. char salt[SHA1_SALT_LEN + 1] = "";
  82. if (saltin) {
  83. strlcpy(salt, saltin, sizeof(salt));
  84. } else {
  85. make_rand_str(salt, sizeof(salt) - 1);
  86. }
  87. /* SHA1 the salt+password */
  88. simple_snprintf(buf, sizeof(buf), STR("%s%s"), salt, in);
  89. tmp = SHA1(buf);
  90. ret_size = SHA1_SALTED_LEN + 1;
  91. ret = (char *) my_calloc(1, ret_size);
  92. simple_snprintf(ret, ret_size, STR("+%s$%s"), salt, tmp);
  93. /* Wipe cleartext pass from sha1 buffers/tmp */
  94. SHA1(NULL);
  95. return ret;
  96. }
  97. int lfprintf (FILE *stream, const char *format, ...)
  98. {
  99. va_list va;
  100. char buf[2048] = "", *ln = NULL, *nln = NULL, *tmp = NULL;
  101. int res;
  102. va_start(va, format);
  103. egg_vsnprintf(buf, sizeof buf, format, va);
  104. va_end(va);
  105. ln = buf;
  106. const char salt1[] = SALT1;
  107. while (ln && *ln) {
  108. if ((nln = strchr(ln, '\n')))
  109. *nln++ = 0;
  110. tmp = encrypt_string(salt1, ln);
  111. res = fprintf(stream, "%s\n", tmp);
  112. free(tmp);
  113. if (res == EOF)
  114. return EOF;
  115. ln = nln;
  116. }
  117. return 0;
  118. }
  119. void Encrypt_File(char *infile, char *outfile)
  120. {
  121. FILE *f = NULL, *f2 = NULL;
  122. bool std = 0;
  123. if (!strcmp(outfile, "STDOUT"))
  124. std = 1;
  125. f = fopen(infile, "r");
  126. if(!f)
  127. return;
  128. if (!std) {
  129. f2 = fopen(outfile, "w");
  130. if (!f2)
  131. return;
  132. } else {
  133. printf(STR("----------------------------------START----------------------------------\n"));
  134. }
  135. char *buf = (char *) my_calloc(1, 1024);
  136. const char salt1[] = SALT1;
  137. while (fgets(buf, 1024, f) != NULL) {
  138. remove_crlf(buf);
  139. if (std)
  140. printf("%s\n", encrypt_string(salt1, buf));
  141. else
  142. lfprintf(f2, "%s\n", buf);
  143. buf[0] = 0;
  144. }
  145. free(buf);
  146. if (std)
  147. printf(STR("-----------------------------------END-----------------------------------\n"));
  148. fclose(f);
  149. if (f2)
  150. fclose(f2);
  151. }
  152. void Decrypt_File(char *infile, char *outfile)
  153. {
  154. FILE *f = NULL, *f2 = NULL;
  155. bool std = 0;
  156. if (!strcmp(outfile, "STDOUT"))
  157. std = 1;
  158. f = fopen(infile, "r");
  159. if (!f)
  160. return;
  161. if (!std) {
  162. f2 = fopen(outfile, "w");
  163. if (!f2)
  164. return;
  165. } else {
  166. printf(STR("----------------------------------START----------------------------------\n"));
  167. }
  168. char *buf = (char *) my_calloc(1, 2048);
  169. const char salt1[] = SALT1;
  170. while (fgets(buf, 2048, f) != NULL) {
  171. char *temps = NULL;
  172. remove_crlf(buf);
  173. temps = (char *) decrypt_string(salt1, buf);
  174. if (!std)
  175. fprintf(f2, "%s\n",temps);
  176. else
  177. printf("%s\n", temps);
  178. free(temps);
  179. buf[0] = 0;
  180. }
  181. free(buf);
  182. if (std)
  183. printf(STR("-----------------------------------END-----------------------------------\n"));
  184. fclose(f);
  185. if (f2)
  186. fclose(f2);
  187. }
  188. char *MD5(const char *string)
  189. {
  190. static int n = 0;
  191. static char ret[5][MD5_HASH_LENGTH + 1];
  192. //Cleanse the current buffer
  193. if (!string) {
  194. OPENSSL_cleanse(ret[n], MD5_HASH_LENGTH + 1);
  195. return NULL;
  196. }
  197. char* md5string = ret[n++];
  198. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  199. MD5_CTX ctx;
  200. MD5_Init(&ctx);
  201. MD5_Update(&ctx, string, strlen(string));
  202. MD5_Final(md5out, &ctx);
  203. btoh(md5out, MD5_DIGEST_LENGTH, md5string, MD5_HASH_LENGTH + 1);
  204. OPENSSL_cleanse(&ctx, sizeof(ctx));
  205. if (n == 5) n = 0;
  206. return md5string;
  207. }
  208. int md5cmp(const char *hash, const char *string) {
  209. int n = strcmp(hash, MD5(string));
  210. MD5(NULL);
  211. return n;
  212. }
  213. char *
  214. MD5FILE(const char *bin)
  215. {
  216. FILE *f = NULL;
  217. if (!(f = fopen(bin, "rb")))
  218. return "";
  219. static char md5string[MD5_HASH_LENGTH + 1] = "";
  220. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buffer[1024] = "";
  221. MD5_CTX ctx;
  222. size_t binsize = 0, len = 0;
  223. MD5_Init(&ctx);
  224. while ((len = fread(buffer, 1, sizeof buffer, f))) {
  225. binsize += len;
  226. MD5_Update(&ctx, buffer, len);
  227. }
  228. MD5_Final(md5out, &ctx);
  229. btoh(md5out, MD5_DIGEST_LENGTH, md5string, sizeof(md5string));
  230. OPENSSL_cleanse(&ctx, sizeof(ctx));
  231. return md5string;
  232. }
  233. char *SHA1(const char *string)
  234. {
  235. static int n = 0;
  236. static char ret[5][SHA_HASH_LENGTH + 1];
  237. //Cleanse the current buffer
  238. if (!string) {
  239. OPENSSL_cleanse(ret[n], SHA_HASH_LENGTH + 1);
  240. return NULL;
  241. }
  242. char* sha1string = ret[n++];
  243. unsigned char sha1out[SHA_HASH_LENGTH + 1] = "";
  244. SHA_CTX ctx;
  245. SHA1_Init(&ctx);
  246. SHA1_Update(&ctx, string, strlen(string));
  247. SHA1_Final(sha1out, &ctx);
  248. btoh(sha1out, SHA_DIGEST_LENGTH, sha1string, SHA_HASH_LENGTH + 1);
  249. OPENSSL_cleanse(&ctx, sizeof(ctx));
  250. if (n == 5) n = 0;
  251. return sha1string;
  252. }
  253. int sha1cmp(const char *hash, const char *string) {
  254. int n = strcmp(hash, SHA1(string));
  255. SHA1(NULL);
  256. return n;
  257. }
  258. void btoh(const unsigned char *md, size_t md_len, char *buf, const size_t buf_len)
  259. {
  260. #define doblock(n) simple_snprintf(&(buf[(i + n) << 1]), buf_len - ((i + n) << 1), "%02x", md[i + n]);
  261. for (size_t i = 0; i < md_len; i+=4) {
  262. doblock(0);
  263. doblock(1);
  264. doblock(2);
  265. doblock(3);
  266. }
  267. }