crypt.c 7.3 KB

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