x25519.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "x25519.h"
  11. #include "cppcodec/cppcodec/base64_default_rfc4648.hpp"
  12. #include <iostream>
  13. #include <memory>
  14. namespace FriendlyCrypto {
  15. X25519Keys::X25519Keys()
  16. {
  17. m_Ctx = EVP_PKEY_CTX_new_id (NID_X25519, NULL);
  18. m_Pkey = nullptr;
  19. }
  20. X25519Keys::~X25519Keys()
  21. {
  22. EVP_PKEY_CTX_free (m_Ctx);
  23. if (m_Pkey) EVP_PKEY_free (m_Pkey);
  24. }
  25. void X25519Keys::generateKeys() noexcept
  26. {
  27. if (m_Pkey)
  28. {
  29. EVP_PKEY_free (m_Pkey);
  30. m_Pkey = nullptr;
  31. }
  32. EVP_PKEY_keygen_init (m_Ctx);
  33. EVP_PKEY_keygen (m_Ctx, &m_Pkey);
  34. EVP_PKEY_CTX_free (m_Ctx);
  35. m_Ctx = EVP_PKEY_CTX_new (m_Pkey, NULL);
  36. size_t len = 32;
  37. EVP_PKEY_get_raw_public_key (m_Pkey, m_publicKey.data(), &len);
  38. }
  39. const std::array<uint8_t, 32> X25519Keys::getPublicKey() const noexcept
  40. {
  41. return m_publicKey;
  42. }
  43. const std::array<uint8_t, 32> X25519Keys::getSecretKey() const noexcept
  44. {
  45. std::array<uint8_t, 32> priv;
  46. size_t len = 32;
  47. EVP_PKEY_get_raw_private_key (m_Pkey, priv.data(), &len);
  48. return priv;
  49. }
  50. const std::array<uint8_t, 32> X25519Keys::agree (const std::array<uint8_t, 32> &pub) const noexcept
  51. {
  52. std::array<uint8_t, 32> shared;
  53. if (pub.size() < 32 or (pub[31] & 0x80)) return shared; // not x25519 key
  54. EVP_PKEY_derive_init (m_Ctx);
  55. auto pkey = EVP_PKEY_new_raw_public_key (EVP_PKEY_X25519, NULL, pub.data(), 32);
  56. if (!pkey) return shared;
  57. EVP_PKEY_derive_set_peer (m_Ctx, pkey);
  58. size_t len = 32;
  59. EVP_PKEY_derive (m_Ctx, shared.data(), &len);
  60. EVP_PKEY_free (pkey);
  61. return shared;
  62. }
  63. const std::array<uint8_t, 32> X25519Keys::agree (const std::string &pub) const noexcept
  64. {
  65. std::vector<uint8_t> bytes;
  66. try {
  67. bytes = cppcodec::base64_rfc4648::decode (pub);
  68. } catch (...) {
  69. return std::array<uint8_t, 32>();
  70. }
  71. return agree (bytes.data(), bytes.size());
  72. }
  73. const std::array<uint8_t, 32> X25519Keys::agree (const uint8_t *pub, size_t size) const noexcept
  74. {
  75. if (size != 32)
  76. {
  77. return std::array<uint8_t, 32>();
  78. }
  79. std::array<uint8_t, 32> key;
  80. for (int i = 0; i < 32; i++)
  81. {
  82. key[i] = pub[i];
  83. }
  84. return agree(key);
  85. }
  86. void X25519Keys::setSecretKey (const uint8_t * priv, bool calculatePublic) noexcept
  87. {
  88. if (m_Ctx) EVP_PKEY_CTX_free (m_Ctx);
  89. if (m_Pkey) EVP_PKEY_free (m_Pkey);
  90. m_Pkey = EVP_PKEY_new_raw_private_key (EVP_PKEY_X25519, NULL, priv, 32);
  91. m_Ctx = EVP_PKEY_CTX_new (m_Pkey, NULL);
  92. if (calculatePublic)
  93. {
  94. size_t len = 32;
  95. EVP_PKEY_get_raw_public_key (m_Pkey, m_publicKey.data(), &len);
  96. }
  97. }
  98. void X25519Keys::setSecretKey (const std::string &priv, bool calculatePublic)
  99. {
  100. std::vector<uint8_t> keyBytes = cppcodec::base64_rfc4648::decode(priv);
  101. setSecretKey (keyBytes, calculatePublic);
  102. }
  103. void X25519Keys::setSecretKey (const std::vector<uint8_t>& priv, bool calculatePublic)
  104. {
  105. if (priv.size() != 32)
  106. {
  107. throw std::runtime_error ("X25519Keys::setSecretKey priv array size != 32");
  108. }
  109. setSecretKey (priv.data(), calculatePublic);
  110. }
  111. void X25519Keys::setSecretKey (const std::array<uint8_t, 32> &priv, bool calculatePublic) noexcept
  112. {
  113. setSecretKey (priv.data(), calculatePublic);
  114. }
  115. const std::string X25519Keys::getPublicKeyBase64String() const noexcept
  116. {
  117. return cppcodec::base64_rfc4648::encode (getPublicKey().data(), getPublicKey().size());
  118. }
  119. const std::string X25519Keys::getSecretKeyBase64String() const noexcept
  120. {
  121. return cppcodec::base64_rfc4648::encode (getSecretKey().data(), getSecretKey().size());
  122. }
  123. } // namespace