x25519.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Based on
  3. * 1. OpenSSL lib
  4. * 2. PurpleI2P source code
  5. * 3. cppcodec lib
  6. *
  7. * PUBLIC DOMAIN C++ WRAPPER
  8. * acetone, 2022
  9. */
  10. #ifndef X25519_H
  11. #define X25519_H
  12. #include <string>
  13. #include <array>
  14. #include <vector>
  15. #include <openssl/evp.h>
  16. namespace FriendlyCrypto {
  17. class X25519Keys
  18. {
  19. public:
  20. X25519Keys();
  21. ~X25519Keys();
  22. void generateKeys() noexcept;
  23. void setSecretKey (const uint8_t * priv, bool calculatePublic = false) noexcept;
  24. void setSecretKey (const std::array<uint8_t, 32>& priv, bool calculatePublic = false) noexcept;
  25. void setSecretKey (const std::vector<uint8_t>& priv, bool calculatePublic = false) ;
  26. void setSecretKey (const std::string& priv, bool calculatePublic = false) ;
  27. const std::array<uint8_t, 32> getPublicKey() const noexcept;
  28. const std::array<uint8_t, 32> getSecretKey() const noexcept;
  29. const std::array<uint8_t, 32> agree (const std::array<uint8_t, 32>& pub) const noexcept;
  30. const std::array<uint8_t, 32> agree (const std::string& pub) const noexcept;
  31. const std::array<uint8_t, 32> agree (const uint8_t* pub, size_t size = 32) const noexcept;
  32. const std::string getPublicKeyBase64String() const noexcept;
  33. const std::string getSecretKeyBase64String() const noexcept;
  34. private:
  35. std::array<uint8_t, 32> m_publicKey {0};
  36. EVP_PKEY_CTX * m_Ctx;
  37. EVP_PKEY * m_Pkey;
  38. };
  39. } // namespace
  40. #endif // X25519_H