Signature.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <memory>
  2. #include "Log.h"
  3. #include "Signature.h"
  4. namespace i2p
  5. {
  6. namespace crypto
  7. {
  8. #if OPENSSL_EDDSA
  9. EDDSA25519Verifier::EDDSA25519Verifier ():
  10. m_Pkey (nullptr)
  11. {
  12. m_MDCtx = EVP_MD_CTX_create ();
  13. }
  14. EDDSA25519Verifier::~EDDSA25519Verifier ()
  15. {
  16. EVP_MD_CTX_destroy (m_MDCtx);
  17. if (m_Pkey) EVP_PKEY_free (m_Pkey);
  18. }
  19. void EDDSA25519Verifier::SetPublicKey (const uint8_t * signingKey)
  20. {
  21. m_Pkey = EVP_PKEY_new_raw_public_key (EVP_PKEY_ED25519, NULL, signingKey, 32);
  22. EVP_DigestVerifyInit (m_MDCtx, NULL, NULL, NULL, m_Pkey);
  23. }
  24. bool EDDSA25519Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
  25. {
  26. return EVP_DigestVerify (m_MDCtx, signature, 64, buf, len);
  27. }
  28. #else
  29. EDDSA25519Verifier::EDDSA25519Verifier ()
  30. {
  31. }
  32. EDDSA25519Verifier::~EDDSA25519Verifier ()
  33. {
  34. }
  35. void EDDSA25519Verifier::SetPublicKey (const uint8_t * signingKey)
  36. {
  37. memcpy (m_PublicKeyEncoded, signingKey, EDDSA25519_PUBLIC_KEY_LENGTH);
  38. BN_CTX * ctx = BN_CTX_new ();
  39. m_PublicKey = GetEd25519 ()->DecodePublicKey (m_PublicKeyEncoded, ctx);
  40. BN_CTX_free (ctx);
  41. }
  42. bool EDDSA25519Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
  43. {
  44. uint8_t digest[64];
  45. SHA512_CTX ctx;
  46. SHA512_Init (&ctx);
  47. SHA512_Update (&ctx, signature, EDDSA25519_SIGNATURE_LENGTH/2); // R
  48. SHA512_Update (&ctx, m_PublicKeyEncoded, EDDSA25519_PUBLIC_KEY_LENGTH); // public key
  49. SHA512_Update (&ctx, buf, len); // data
  50. SHA512_Final (digest, &ctx);
  51. return GetEd25519 ()->Verify (m_PublicKey, digest, signature);
  52. }
  53. #endif
  54. EDDSA25519SignerCompat::EDDSA25519SignerCompat (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey)
  55. {
  56. // expand key
  57. Ed25519::ExpandPrivateKey (signingPrivateKey, m_ExpandedPrivateKey);
  58. // generate and encode public key
  59. BN_CTX * ctx = BN_CTX_new ();
  60. auto publicKey = GetEd25519 ()->GeneratePublicKey (m_ExpandedPrivateKey, ctx);
  61. GetEd25519 ()->EncodePublicKey (publicKey, m_PublicKeyEncoded, ctx);
  62. if (signingPublicKey && memcmp (m_PublicKeyEncoded, signingPublicKey, EDDSA25519_PUBLIC_KEY_LENGTH))
  63. {
  64. // keys don't match, it means older key with 0x1F
  65. LogPrint (eLogWarning, "Older EdDSA key detected");
  66. m_ExpandedPrivateKey[EDDSA25519_PRIVATE_KEY_LENGTH - 1] &= 0xDF; // drop third bit
  67. publicKey = GetEd25519 ()->GeneratePublicKey (m_ExpandedPrivateKey, ctx);
  68. GetEd25519 ()->EncodePublicKey (publicKey, m_PublicKeyEncoded, ctx);
  69. }
  70. BN_CTX_free (ctx);
  71. }
  72. EDDSA25519SignerCompat::~EDDSA25519SignerCompat ()
  73. {
  74. }
  75. void EDDSA25519SignerCompat::Sign (const uint8_t * buf, int len, uint8_t * signature) const
  76. {
  77. GetEd25519 ()->Sign (m_ExpandedPrivateKey, m_PublicKeyEncoded, buf, len, signature);
  78. }
  79. #if OPENSSL_EDDSA
  80. EDDSA25519Signer::EDDSA25519Signer (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey):
  81. m_Fallback (nullptr)
  82. {
  83. m_Pkey = EVP_PKEY_new_raw_private_key (EVP_PKEY_ED25519, NULL, signingPrivateKey, 32);
  84. uint8_t publicKey[EDDSA25519_PUBLIC_KEY_LENGTH];
  85. size_t len = EDDSA25519_PUBLIC_KEY_LENGTH;
  86. EVP_PKEY_get_raw_public_key (m_Pkey, publicKey, &len);
  87. if (signingPublicKey && memcmp (publicKey, signingPublicKey, EDDSA25519_PUBLIC_KEY_LENGTH))
  88. {
  89. LogPrint (eLogWarning, "EdDSA public key mismatch. Fallback");
  90. EVP_PKEY_free (m_Pkey);
  91. m_Fallback = new EDDSA25519SignerCompat (signingPrivateKey, signingPublicKey);
  92. }
  93. else
  94. {
  95. m_MDCtx = EVP_MD_CTX_create ();
  96. EVP_DigestSignInit (m_MDCtx, NULL, NULL, NULL, m_Pkey);
  97. }
  98. }
  99. EDDSA25519Signer::~EDDSA25519Signer ()
  100. {
  101. if (m_Fallback) delete m_Fallback;
  102. else
  103. {
  104. EVP_MD_CTX_destroy (m_MDCtx);
  105. EVP_PKEY_free (m_Pkey);
  106. }
  107. }
  108. void EDDSA25519Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const
  109. {
  110. if (m_Fallback) return m_Fallback->Sign (buf, len, signature);
  111. else
  112. {
  113. size_t l = 64;
  114. uint8_t sig[64]; // temporary buffer for signature. openssl issue #7232
  115. EVP_DigestSign (m_MDCtx, sig, &l, buf, len);
  116. memcpy (signature, sig, 64);
  117. }
  118. }
  119. #endif
  120. }
  121. }