2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-04-09 14:03:21 +00:00
|
|
|
#include <memory>
|
2015-04-08 19:31:13 +00:00
|
|
|
#include "Log.h"
|
2015-04-08 17:21:49 +00:00
|
|
|
#include "Signature.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
#if OPENSSL_EDDSA
|
2019-03-29 17:15:32 +00:00
|
|
|
EDDSA25519Verifier::EDDSA25519Verifier ():
|
|
|
|
m_Pkey (nullptr)
|
2018-09-03 21:39:49 +00:00
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
m_MDCtx = EVP_MD_CTX_create ();
|
2018-09-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EDDSA25519Verifier::~EDDSA25519Verifier ()
|
|
|
|
{
|
|
|
|
EVP_MD_CTX_destroy (m_MDCtx);
|
2019-01-01 22:00:37 +00:00
|
|
|
if (m_Pkey) EVP_PKEY_free (m_Pkey);
|
2018-09-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 22:00:37 +00:00
|
|
|
void EDDSA25519Verifier::SetPublicKey (const uint8_t * signingKey)
|
|
|
|
{
|
|
|
|
m_Pkey = EVP_PKEY_new_raw_public_key (EVP_PKEY_ED25519, NULL, signingKey, 32);
|
|
|
|
EVP_DigestVerifyInit (m_MDCtx, NULL, NULL, NULL, m_Pkey);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 21:39:49 +00:00
|
|
|
bool EDDSA25519Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
|
|
|
{
|
|
|
|
return EVP_DigestVerify (m_MDCtx, signature, 64, buf, len);
|
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
|
|
|
#else
|
2019-01-01 22:00:37 +00:00
|
|
|
EDDSA25519Verifier::EDDSA25519Verifier ()
|
2015-04-09 14:03:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-03 21:39:49 +00:00
|
|
|
EDDSA25519Verifier::~EDDSA25519Verifier ()
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2019-01-01 22:00:37 +00:00
|
|
|
|
|
|
|
void EDDSA25519Verifier::SetPublicKey (const uint8_t * signingKey)
|
|
|
|
{
|
|
|
|
memcpy (m_PublicKeyEncoded, signingKey, EDDSA25519_PUBLIC_KEY_LENGTH);
|
|
|
|
BN_CTX * ctx = BN_CTX_new ();
|
|
|
|
m_PublicKey = GetEd25519 ()->DecodePublicKey (m_PublicKeyEncoded, ctx);
|
|
|
|
BN_CTX_free (ctx);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 20:28:52 +00:00
|
|
|
bool EDDSA25519Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
2015-04-08 20:18:16 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
uint8_t digest[64];
|
2015-11-26 15:25:51 +00:00
|
|
|
SHA512_CTX ctx;
|
|
|
|
SHA512_Init (&ctx);
|
|
|
|
SHA512_Update (&ctx, signature, EDDSA25519_SIGNATURE_LENGTH/2); // R
|
|
|
|
SHA512_Update (&ctx, m_PublicKeyEncoded, EDDSA25519_PUBLIC_KEY_LENGTH); // public key
|
2018-01-06 03:48:51 +00:00
|
|
|
SHA512_Update (&ctx, buf, len); // data
|
2015-11-26 15:25:51 +00:00
|
|
|
SHA512_Final (digest, &ctx);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2015-11-26 15:25:51 +00:00
|
|
|
return GetEd25519 ()->Verify (m_PublicKey, digest, signature);
|
2015-04-09 14:03:21 +00:00
|
|
|
}
|
2018-09-03 21:39:49 +00:00
|
|
|
#endif
|
2015-04-09 14:03:21 +00:00
|
|
|
|
2018-09-15 01:23:16 +00:00
|
|
|
EDDSA25519SignerCompat::EDDSA25519SignerCompat (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey)
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
// expand key
|
2018-06-05 16:53:13 +00:00
|
|
|
Ed25519::ExpandPrivateKey (signingPrivateKey, m_ExpandedPrivateKey);
|
2015-11-03 14:15:49 +00:00
|
|
|
// generate and encode public key
|
2018-01-06 03:48:51 +00:00
|
|
|
BN_CTX * ctx = BN_CTX_new ();
|
2015-11-23 14:26:32 +00:00
|
|
|
auto publicKey = GetEd25519 ()->GeneratePublicKey (m_ExpandedPrivateKey, ctx);
|
2018-01-06 03:48:51 +00:00
|
|
|
GetEd25519 ()->EncodePublicKey (publicKey, m_PublicKeyEncoded, ctx);
|
|
|
|
|
2017-01-08 02:20:09 +00:00
|
|
|
if (signingPublicKey && memcmp (m_PublicKeyEncoded, signingPublicKey, EDDSA25519_PUBLIC_KEY_LENGTH))
|
|
|
|
{
|
|
|
|
// keys don't match, it means older key with 0x1F
|
|
|
|
LogPrint (eLogWarning, "Older EdDSA key detected");
|
2018-01-06 03:48:51 +00:00
|
|
|
m_ExpandedPrivateKey[EDDSA25519_PRIVATE_KEY_LENGTH - 1] &= 0xDF; // drop third bit
|
2017-01-08 02:20:09 +00:00
|
|
|
publicKey = GetEd25519 ()->GeneratePublicKey (m_ExpandedPrivateKey, ctx);
|
2018-01-06 03:48:51 +00:00
|
|
|
GetEd25519 ()->EncodePublicKey (publicKey, m_PublicKeyEncoded, ctx);
|
2017-01-08 02:20:09 +00:00
|
|
|
}
|
2015-11-23 14:26:32 +00:00
|
|
|
BN_CTX_free (ctx);
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 01:23:16 +00:00
|
|
|
EDDSA25519SignerCompat::~EDDSA25519SignerCompat ()
|
2018-09-03 21:39:49 +00:00
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 01:23:16 +00:00
|
|
|
void EDDSA25519SignerCompat::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
2015-04-09 14:03:21 +00:00
|
|
|
{
|
2015-11-26 15:25:51 +00:00
|
|
|
GetEd25519 ()->Sign (m_ExpandedPrivateKey, m_PublicKeyEncoded, buf, len, signature);
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
|
|
|
#if OPENSSL_EDDSA
|
2018-09-15 01:23:16 +00:00
|
|
|
EDDSA25519Signer::EDDSA25519Signer (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey):
|
|
|
|
m_Fallback (nullptr)
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
2018-09-15 01:23:16 +00:00
|
|
|
m_Pkey = EVP_PKEY_new_raw_private_key (EVP_PKEY_ED25519, NULL, signingPrivateKey, 32);
|
2020-03-01 10:25:50 +00:00
|
|
|
uint8_t publicKey[EDDSA25519_PUBLIC_KEY_LENGTH];
|
2018-09-15 01:23:16 +00:00
|
|
|
size_t len = EDDSA25519_PUBLIC_KEY_LENGTH;
|
2018-09-15 01:54:45 +00:00
|
|
|
EVP_PKEY_get_raw_public_key (m_Pkey, publicKey, &len);
|
2019-02-22 18:17:43 +00:00
|
|
|
if (signingPublicKey && memcmp (publicKey, signingPublicKey, EDDSA25519_PUBLIC_KEY_LENGTH))
|
2018-09-15 01:23:16 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "EdDSA public key mismatch. Fallback");
|
|
|
|
EVP_PKEY_free (m_Pkey);
|
|
|
|
m_Fallback = new EDDSA25519SignerCompat (signingPrivateKey, signingPublicKey);
|
|
|
|
}
|
2018-09-16 22:08:59 +00:00
|
|
|
else
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
|
|
|
m_MDCtx = EVP_MD_CTX_create ();
|
2018-09-15 01:23:16 +00:00
|
|
|
EVP_DigestSignInit (m_MDCtx, NULL, NULL, NULL, m_Pkey);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2018-09-15 01:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EDDSA25519Signer::~EDDSA25519Signer ()
|
|
|
|
{
|
|
|
|
if (m_Fallback) delete m_Fallback;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EVP_MD_CTX_destroy (m_MDCtx);
|
|
|
|
EVP_PKEY_free (m_Pkey);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2018-09-15 01:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EDDSA25519Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
|
|
|
{
|
|
|
|
if (m_Fallback) return m_Fallback->Sign (buf, len, signature);
|
|
|
|
else
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
|
|
|
size_t l = 64;
|
2018-09-16 22:08:59 +00:00
|
|
|
uint8_t sig[64]; // temporary buffer for signature. openssl issue #7232
|
2020-03-01 10:25:50 +00:00
|
|
|
EVP_DigestSign (m_MDCtx, sig, &l, buf, len);
|
2018-09-16 22:08:59 +00:00
|
|
|
memcpy (signature, sig, 64);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-08 17:21:49 +00:00
|
|
|
}
|
|
|
|
}
|