CryptoOpenSSL.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef BELL_CRYPTOOPENSSL_H
  2. #define BELL_CRYPTOOPENSSL_H
  3. #include <vector>
  4. #include <string>
  5. #include <memory>
  6. #include <openssl/engine.h>
  7. #include <openssl/rand.h>
  8. #include <openssl/err.h>
  9. #include <openssl/dh.h>
  10. #include <openssl/bn.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/sha.h>
  14. #include <openssl/hmac.h>
  15. #include <openssl/aes.h>
  16. #include <openssl/modes.h>
  17. #define DH_KEY_SIZE 96
  18. static unsigned char DHPrime[] = {
  19. /* Well-known Group 1, 768-bit prime */
  20. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
  21. 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,
  22. 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e,
  23. 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
  24. 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e,
  25. 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a,
  26. 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14,
  27. 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
  28. 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4,
  29. 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff,
  30. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  31. };
  32. static unsigned char DHGenerator[1] = {2};
  33. class CryptoOpenSSL {
  34. private:
  35. DH* dhContext = nullptr;
  36. SHA_CTX sha1Context;
  37. public:
  38. CryptoOpenSSL();
  39. ~CryptoOpenSSL();
  40. // Base64
  41. std::vector<uint8_t> base64Decode(const std::string& data);
  42. std::string base64Encode(const std::vector<uint8_t>& data);
  43. // Sha1
  44. void sha1Init();
  45. void sha1Update(const std::string& s);
  46. void sha1Update(const std::vector<uint8_t>& vec);
  47. void connectSSL(std::string url);
  48. int readSSL(uint8_t* buf, int len);
  49. int writeSSL(uint8_t* buf, int len);
  50. void closeSSL();
  51. std::string sha1Final();
  52. std::vector<uint8_t> sha1FinalBytes();
  53. // HMAC SHA1
  54. std::vector<uint8_t> sha1HMAC(const std::vector<uint8_t>& inputKey, const std::vector<uint8_t>& message);
  55. // AES CTR
  56. void aesCTRXcrypt(const std::vector<uint8_t>& key, std::vector<uint8_t>& iv, uint8_t* buffer, size_t nbytes);
  57. // AES ECB
  58. void aesECBdecrypt(const std::vector<uint8_t>& key, std::vector<uint8_t>& data);
  59. // Diffie Hellman
  60. std::vector<uint8_t> publicKey;
  61. std::vector<uint8_t> privateKey;
  62. void dhInit();
  63. std::vector<uint8_t> dhCalculateShared(const std::vector<uint8_t>& remoteKey);
  64. // PBKDF2
  65. std::vector<uint8_t> pbkdf2HmacSha1(const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, int iterations, int digestSize);
  66. // Random stuff
  67. std::vector<uint8_t> generateVectorWithRandomData(size_t length);
  68. };
  69. #endif