|
|
@ -52,30 +52,7 @@ namespace crypto |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if OPENSSL_EDDSA |
|
|
|
EDDSA25519SignerCompat::EDDSA25519SignerCompat (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey) |
|
|
|
EDDSA25519Signer::EDDSA25519Signer (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Pkey = EVP_PKEY_new_raw_private_key (EVP_PKEY_ED25519, NULL, signingPrivateKey, 32); |
|
|
|
|
|
|
|
// TODO: check public key
|
|
|
|
|
|
|
|
m_MDCtx = EVP_MD_CTX_create (); |
|
|
|
|
|
|
|
EVP_DigestSignInit (m_MDCtx, NULL, NULL, NULL, m_Pkey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EDDSA25519Signer::~EDDSA25519Signer () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
EVP_MD_CTX_destroy (m_MDCtx); |
|
|
|
|
|
|
|
EVP_PKEY_free (m_Pkey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EDDSA25519Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
size_t l = 64; |
|
|
|
|
|
|
|
EVP_DigestSignInit (m_MDCtx, NULL, NULL, NULL, NULL); |
|
|
|
|
|
|
|
EVP_DigestSign (m_MDCtx, signature, &l, buf, len); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
EDDSA25519Signer::EDDSA25519Signer (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
// expand key
|
|
|
|
// expand key
|
|
|
|
Ed25519::ExpandPrivateKey (signingPrivateKey, m_ExpandedPrivateKey); |
|
|
|
Ed25519::ExpandPrivateKey (signingPrivateKey, m_ExpandedPrivateKey); |
|
|
@ -95,13 +72,55 @@ namespace crypto |
|
|
|
BN_CTX_free (ctx); |
|
|
|
BN_CTX_free (ctx); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EDDSA25519SignerCompat::~EDDSA25519SignerCompat () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EDDSA25519SignerCompat::Sign (const uint8_t * buf, int len, uint8_t * signature) const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
GetEd25519 ()->Sign (m_ExpandedPrivateKey, m_PublicKeyEncoded, buf, len, signature); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if OPENSSL_EDDSA |
|
|
|
|
|
|
|
EDDSA25519Signer::EDDSA25519Signer (const uint8_t * signingPrivateKey, const uint8_t * signingPublicKey): |
|
|
|
|
|
|
|
m_Fallback (nullptr) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Pkey = EVP_PKEY_new_raw_private_key (EVP_PKEY_ED25519, NULL, signingPrivateKey, 32); |
|
|
|
|
|
|
|
uint8_t publicKey[EDDSA25519_PUBLIC_KEY_LENGTH]; |
|
|
|
|
|
|
|
size_t len = EDDSA25519_PUBLIC_KEY_LENGTH; |
|
|
|
|
|
|
|
EVP_PKEY_get_raw_public_key (pkey, publicKey, &len); |
|
|
|
|
|
|
|
if (memcmp (publicKey, signingPublicKey, EDDSA25519_PUBLIC_KEY_LENGTH)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogWarning, "EdDSA public key mismatch. Fallback"); |
|
|
|
|
|
|
|
EVP_PKEY_free (m_Pkey); |
|
|
|
|
|
|
|
m_Fallback = new EDDSA25519SignerCompat (signingPrivateKey, signingPublicKey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_MDCtx = EVP_MD_CTX_create (); |
|
|
|
|
|
|
|
EVP_DigestSignInit (m_MDCtx, NULL, NULL, NULL, m_Pkey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
EDDSA25519Signer::~EDDSA25519Signer () |
|
|
|
EDDSA25519Signer::~EDDSA25519Signer () |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (m_Fallback) delete m_Fallback; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
EVP_MD_CTX_destroy (m_MDCtx); |
|
|
|
|
|
|
|
EVP_PKEY_free (m_Pkey); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void EDDSA25519Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const |
|
|
|
void EDDSA25519Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
GetEd25519 ()->Sign (m_ExpandedPrivateKey, m_PublicKeyEncoded, buf, len, signature); |
|
|
|
if (m_Fallback) return m_Fallback->Sign (buf, len, signature); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
size_t l = 64; |
|
|
|
|
|
|
|
EVP_DigestSignInit (m_MDCtx, NULL, NULL, NULL, NULL); |
|
|
|
|
|
|
|
EVP_DigestSign (m_MDCtx, signature, &l, buf, len); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|