CryptoKey.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <string.h>
  2. #include "Log.h"
  3. #include "Gost.h"
  4. #include "CryptoKey.h"
  5. namespace i2p
  6. {
  7. namespace crypto
  8. {
  9. ElGamalEncryptor::ElGamalEncryptor (const uint8_t * pub)
  10. {
  11. memcpy (m_PublicKey, pub, 256);
  12. }
  13. void ElGamalEncryptor::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding)
  14. {
  15. ElGamalEncrypt (m_PublicKey, data, encrypted, ctx, zeroPadding);
  16. }
  17. ElGamalDecryptor::ElGamalDecryptor (const uint8_t * priv)
  18. {
  19. memcpy (m_PrivateKey, priv, 256);
  20. }
  21. bool ElGamalDecryptor::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding)
  22. {
  23. return ElGamalDecrypt (m_PrivateKey, encrypted, data, ctx, zeroPadding);
  24. }
  25. ECIESP256Encryptor::ECIESP256Encryptor (const uint8_t * pub)
  26. {
  27. m_Curve = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1);
  28. m_PublicKey = EC_POINT_new (m_Curve);
  29. BIGNUM * x = BN_bin2bn (pub, 32, nullptr);
  30. BIGNUM * y = BN_bin2bn (pub + 32, 32, nullptr);
  31. if (!EC_POINT_set_affine_coordinates_GFp (m_Curve, m_PublicKey, x, y, nullptr))
  32. LogPrint (eLogError, "ECICS P256 invalid public key");
  33. BN_free (x); BN_free (y);
  34. }
  35. ECIESP256Encryptor::~ECIESP256Encryptor ()
  36. {
  37. if (m_Curve) EC_GROUP_free (m_Curve);
  38. if (m_PublicKey) EC_POINT_free (m_PublicKey);
  39. }
  40. void ECIESP256Encryptor::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding)
  41. {
  42. if (m_Curve && m_PublicKey)
  43. ECIESEncrypt (m_Curve, m_PublicKey, data, encrypted, ctx, zeroPadding);
  44. }
  45. ECIESP256Decryptor::ECIESP256Decryptor (const uint8_t * priv)
  46. {
  47. m_Curve = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1);
  48. m_PrivateKey = BN_bin2bn (priv, 32, nullptr);
  49. }
  50. ECIESP256Decryptor::~ECIESP256Decryptor ()
  51. {
  52. if (m_Curve) EC_GROUP_free (m_Curve);
  53. if (m_PrivateKey) BN_free (m_PrivateKey);
  54. }
  55. bool ECIESP256Decryptor::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding)
  56. {
  57. if (m_Curve && m_PrivateKey)
  58. return ECIESDecrypt (m_Curve, m_PrivateKey, encrypted, data, ctx, zeroPadding);
  59. return false;
  60. }
  61. void CreateECIESP256RandomKeys (uint8_t * priv, uint8_t * pub)
  62. {
  63. EC_GROUP * curve = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1);
  64. EC_POINT * p = nullptr;
  65. BIGNUM * key = nullptr;
  66. GenerateECIESKeyPair (curve, key, p);
  67. bn2buf (key, priv, 32);
  68. RAND_bytes (priv + 32, 224);
  69. BN_free (key);
  70. BIGNUM * x = BN_new (), * y = BN_new ();
  71. EC_POINT_get_affine_coordinates_GFp (curve, p, x, y, NULL);
  72. bn2buf (x, pub, 32);
  73. bn2buf (y, pub + 32, 32);
  74. RAND_bytes (pub + 64, 192);
  75. EC_POINT_free (p);
  76. BN_free (x); BN_free (y);
  77. EC_GROUP_free (curve);
  78. }
  79. ECIESGOSTR3410Encryptor::ECIESGOSTR3410Encryptor (const uint8_t * pub)
  80. {
  81. auto& curve = GetGOSTR3410Curve (eGOSTR3410CryptoProA);
  82. m_PublicKey = EC_POINT_new (curve->GetGroup ());
  83. BIGNUM * x = BN_bin2bn (pub, 32, nullptr);
  84. BIGNUM * y = BN_bin2bn (pub + 32, 32, nullptr);
  85. if (!EC_POINT_set_affine_coordinates_GFp (curve->GetGroup (), m_PublicKey, x, y, nullptr))
  86. LogPrint (eLogError, "ECICS GOST R 34.10 invalid public key");
  87. BN_free (x); BN_free (y);
  88. }
  89. ECIESGOSTR3410Encryptor::~ECIESGOSTR3410Encryptor ()
  90. {
  91. if (m_PublicKey) EC_POINT_free (m_PublicKey);
  92. }
  93. void ECIESGOSTR3410Encryptor::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding)
  94. {
  95. if (m_PublicKey)
  96. ECIESEncrypt (GetGOSTR3410Curve (eGOSTR3410CryptoProA)->GetGroup (), m_PublicKey, data, encrypted, ctx, zeroPadding);
  97. }
  98. ECIESGOSTR3410Decryptor::ECIESGOSTR3410Decryptor (const uint8_t * priv)
  99. {
  100. m_PrivateKey = BN_bin2bn (priv, 32, nullptr);
  101. }
  102. ECIESGOSTR3410Decryptor::~ECIESGOSTR3410Decryptor ()
  103. {
  104. if (m_PrivateKey) BN_free (m_PrivateKey);
  105. }
  106. bool ECIESGOSTR3410Decryptor::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding)
  107. {
  108. if (m_PrivateKey)
  109. return ECIESDecrypt (GetGOSTR3410Curve (eGOSTR3410CryptoProA)->GetGroup (), m_PrivateKey, encrypted, data, ctx, zeroPadding);
  110. return false;
  111. }
  112. void CreateECIESGOSTR3410RandomKeys (uint8_t * priv, uint8_t * pub)
  113. {
  114. auto& curve = GetGOSTR3410Curve (eGOSTR3410CryptoProA);
  115. EC_POINT * p = nullptr;
  116. BIGNUM * key = nullptr;
  117. GenerateECIESKeyPair (curve->GetGroup (), key, p);
  118. bn2buf (key, priv, 32);
  119. RAND_bytes (priv + 32, 224);
  120. BN_free (key);
  121. BIGNUM * x = BN_new (), * y = BN_new ();
  122. EC_POINT_get_affine_coordinates_GFp (curve->GetGroup (), p, x, y, NULL);
  123. bn2buf (x, pub, 32);
  124. bn2buf (y, pub + 32, 32);
  125. RAND_bytes (pub + 64, 192);
  126. EC_POINT_free (p);
  127. BN_free (x); BN_free (y);
  128. }
  129. }
  130. }