x25519.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <openssl/evp.h>
  2. #include <openssl/bn.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <iomanip>
  6. #include "Base.h"
  7. #define KEYSIZE 32
  8. size_t len = KEYSIZE;
  9. struct BoxKeys
  10. {
  11. uint8_t PublicKey[KEYSIZE];
  12. uint8_t PrivateKey[KEYSIZE];
  13. };
  14. BoxKeys getKeyPair()
  15. {
  16. BoxKeys keys;
  17. EVP_PKEY_CTX * Ctx;
  18. EVP_PKEY * Pkey = nullptr;
  19. Ctx = EVP_PKEY_CTX_new_id (NID_X25519, NULL);
  20. EVP_PKEY_keygen_init (Ctx);
  21. EVP_PKEY_keygen (Ctx, &Pkey);
  22. EVP_PKEY_get_raw_public_key (Pkey, keys.PublicKey, &len);
  23. EVP_PKEY_get_raw_private_key (Pkey, keys.PrivateKey, &len);
  24. EVP_PKEY_CTX_free(Ctx);
  25. EVP_PKEY_free(Pkey);
  26. return keys;
  27. }
  28. int main(int argc, char * argv[])
  29. {
  30. if (argc > 1)
  31. {
  32. std::string arg (argv[1]);
  33. if (arg == "--usage" || arg == "--help" || arg == "-h")
  34. {
  35. std::cout << "The x25519 keys are used for authentication with an encrypted LeaseSet.\n"
  36. << "Server side:\n"
  37. << " signaturetype = 11\n"
  38. << " i2cp.leaseSetType = 5\n"
  39. << " i2cp.leaseSetAuthType = 1\n"
  40. << " i2cp.leaseSetClient.dh.210 = clientName:PublicKey\n"
  41. << "Client side:\n"
  42. << " i2cp.leaseSetPrivKey = PrivateKey\n\n"
  43. << "https://i2pd.readthedocs.io/en/latest/user-guide/tunnels/" << std::endl;
  44. return 0;
  45. }
  46. }
  47. BoxKeys newKeys = getKeyPair();
  48. const size_t len_out = 50;
  49. char b64Public[len_out] = {0};
  50. char b64Private[len_out] = {0};
  51. i2p::data::ByteStreamToBase64 (newKeys.PublicKey, len, b64Public, len_out);
  52. std::cout << "PublicKey: ";
  53. for (int i = 0; b64Public[i] != 0; ++i)
  54. std::cout << b64Public[i];
  55. i2p::data::ByteStreamToBase64 (newKeys.PrivateKey, len, b64Private, len_out);
  56. std::cout << "\nPrivateKey: ";
  57. for (int i = 0; b64Private[i] != 0; ++i)
  58. std::cout << b64Private[i];
  59. std::cout << std::endl;
  60. return 0;
  61. }