crypt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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
  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. char *decrypt_string(const char *keydata, char *in)
  48. {
  49. size_t len = strlen(in);
  50. char *buf = NULL, *res = NULL;
  51. if (keydata && *keydata) {
  52. buf = b64dec((const unsigned char *) in, &len);
  53. res = (char *) aes_decrypt_ecb_binary(keydata, (unsigned char *) buf, &len);
  54. OPENSSL_cleanse(buf, len);
  55. free(buf);
  56. return res;
  57. } else {
  58. res = (char *) my_calloc(1, len + 1);
  59. strlcpy(res, in, len + 1);
  60. return res;
  61. }
  62. }
  63. /**
  64. * @brief Decrypt a string
  65. * @param key The key to decrypt with
  66. * @param data The string to decrypt
  67. * @return A new, decrypted string
  68. */
  69. bd::String decrypt_string(const bd::String& key, const bd::String& data) {
  70. if (!key) return data;
  71. size_t len = data.length();
  72. char *bdata = (char*) aes_decrypt_ecb_binary(key.c_str(), (unsigned char*) data.c_str(), &len);
  73. bd::String decrypted(bdata, len);
  74. OPENSSL_cleanse(bdata, len);
  75. free(bdata);
  76. return decrypted;
  77. }
  78. int salted_sha1cmp(const char *salted_hash, const char *string) {
  79. char* cmp = salted_sha1(string, &salted_hash[1]); //Pass in the salt from the given hash
  80. int n = strcmp(salted_hash, cmp);
  81. free(cmp);
  82. return n;
  83. }
  84. char *salted_sha1(const char *in, const char* saltin)
  85. {
  86. char *tmp = NULL, buf[101] = "", *ret = NULL;
  87. size_t ret_size = 0;
  88. /* Create a 5 byte salt */
  89. char salt[SHA1_SALT_LEN + 1] = "";
  90. if (saltin) {
  91. strlcpy(salt, saltin, sizeof(salt));
  92. } else {
  93. make_rand_str(salt, sizeof(salt) - 1);
  94. }
  95. /* SHA1 the salt+password */
  96. simple_snprintf(buf, sizeof(buf), STR("%s%s"), salt, in);
  97. tmp = SHA1(buf);
  98. ret_size = SHA1_SALTED_LEN + 1;
  99. ret = (char *) my_calloc(1, ret_size);
  100. simple_snprintf(ret, ret_size, STR("+%s$%s"), salt, tmp);
  101. /* Wipe cleartext pass from sha1 buffers/tmp */
  102. SHA1(NULL);
  103. return ret;
  104. }
  105. void Encrypt_File(char *infile, char *outfile)
  106. {
  107. const char salt1[] = SALT1;
  108. bd::Stream stream_in;
  109. EncryptedStream stream_out(salt1);
  110. stream_in.loadFile(infile);
  111. stream_out << bd::String(stream_in);
  112. stream_out.writeFile(outfile);
  113. }
  114. void Decrypt_File(char *infile, char *outfile)
  115. {
  116. const char salt1[] = SALT1;
  117. bd::Stream stream_out;
  118. EncryptedStream stream_in(salt1);
  119. stream_in.loadFile(infile);
  120. stream_out << bd::String(stream_in);
  121. stream_out.writeFile(outfile);
  122. }
  123. char *MD5(const char *string)
  124. {
  125. static int n = 0;
  126. static char ret[5][MD5_HASH_LENGTH + 1];
  127. //Cleanse the current buffer
  128. if (!string) {
  129. OPENSSL_cleanse(ret[n], MD5_HASH_LENGTH + 1);
  130. return NULL;
  131. }
  132. char* md5string = ret[n++];
  133. unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
  134. MD5_CTX ctx;
  135. MD5_Init(&ctx);
  136. MD5_Update(&ctx, string, strlen(string));
  137. MD5_Final(md5out, &ctx);
  138. btoh(md5out, MD5_DIGEST_LENGTH, md5string, MD5_HASH_LENGTH + 1);
  139. OPENSSL_cleanse(&ctx, sizeof(ctx));
  140. if (n == 5) n = 0;
  141. return md5string;
  142. }
  143. int md5cmp(const char *hash, const char *string) {
  144. int n = strcmp(hash, MD5(string));
  145. MD5(NULL);
  146. return n;
  147. }
  148. char *SHA1(const char *string)
  149. {
  150. static int n = 0;
  151. static char ret[5][SHA_HASH_LENGTH + 1];
  152. //Cleanse the current buffer
  153. if (!string) {
  154. OPENSSL_cleanse(ret[n], SHA_HASH_LENGTH + 1);
  155. return NULL;
  156. }
  157. char* sha1string = ret[n++];
  158. unsigned char sha1out[SHA_HASH_LENGTH + 1] = "";
  159. SHA_CTX ctx;
  160. SHA1_Init(&ctx);
  161. SHA1_Update(&ctx, string, strlen(string));
  162. SHA1_Final(sha1out, &ctx);
  163. btoh(sha1out, SHA_DIGEST_LENGTH, sha1string, SHA_HASH_LENGTH + 1);
  164. OPENSSL_cleanse(&ctx, sizeof(ctx));
  165. if (n == 5) n = 0;
  166. return sha1string;
  167. }
  168. int sha1cmp(const char *hash, const char *string) {
  169. int n = strcmp(hash, SHA1(string));
  170. SHA1(NULL);
  171. return n;
  172. }
  173. void btoh(const unsigned char *md, size_t md_len, char *buf, const size_t buf_len)
  174. {
  175. #define doblock(n) simple_snprintf(&(buf[(i + n) << 1]), buf_len - ((i + n) << 1), "%02x", md[i + n]);
  176. for (size_t i = 0; i < md_len; i+=4) {
  177. doblock(0);
  178. doblock(1);
  179. doblock(2);
  180. doblock(3);
  181. }
  182. }