From e40c139ff19fa0053058fe1aefc0c88afb98a480 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 22 May 2019 16:15:11 -0400 Subject: [PATCH] blind ECDSA private key --- libi2pd/Blinding.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- libi2pd/Blinding.h | 2 +- libi2pd/LeaseSet.cpp | 6 +++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/libi2pd/Blinding.cpp b/libi2pd/Blinding.cpp index f4d6040e..334d5f2b 100644 --- a/libi2pd/Blinding.cpp +++ b/libi2pd/Blinding.cpp @@ -9,6 +9,7 @@ #include "Timestamp.h" #include "I2PEndian.h" #include "Ed25519.h" +#include "Signature.h" #include "Blinding.h" namespace i2p @@ -51,7 +52,7 @@ namespace data BN_CTX_free (ctx); } - void BlindPrivateKeyECDSA (size_t publicKeyLen, const EC_GROUP * group, const uint8_t * priv, const uint8_t * seed, uint8_t * blindedPriv, uint8_t * blindedPub) + static void BlindPrivateKeyECDSA (size_t publicKeyLen, const EC_GROUP * group, const uint8_t * priv, const uint8_t * seed, uint8_t * blindedPriv, uint8_t * blindedPub) { BIGNUM * a = BN_bin2bn (priv, publicKeyLen/2, NULL); BIGNUM * a1 = BN_new (); @@ -166,11 +167,47 @@ namespace data i2p::crypto::GetEd25519 ()->BlindPublicKey (GetPublicKey (), seed, blindedKey); } - void BlindedPublicKey::BlindPrivateKey (const uint8_t * priv, const char * date, uint8_t * blindedPriv, uint8_t * blindedPub) const + size_t BlindedPublicKey::BlindPrivateKey (const uint8_t * priv, const char * date, uint8_t * blindedPriv, uint8_t * blindedPub) const { uint8_t seed[64]; GenerateAlpha (date, seed); - i2p::crypto::GetEd25519 ()->BlindPrivateKey (priv, seed, blindedPriv, blindedPub); + size_t publicKeyLength = 0; + switch (m_SigType) + { + case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256: + { + publicKeyLength = i2p::crypto::ECDSAP256_KEY_LENGTH; + EC_GROUP * group = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1); + BlindPrivateKeyECDSA (publicKeyLength, group, priv, seed, blindedPriv, blindedPub); + EC_GROUP_free (group); + break; + } + case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384: + { + publicKeyLength = i2p::crypto::ECDSAP384_KEY_LENGTH; + EC_GROUP * group = EC_GROUP_new_by_curve_name (NID_secp384r1); + BlindPrivateKeyECDSA (publicKeyLength, group, priv, seed, blindedPriv, blindedPub); + EC_GROUP_free (group); + break; + } + case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521: + { + publicKeyLength = i2p::crypto::ECDSAP521_KEY_LENGTH; + EC_GROUP * group = EC_GROUP_new_by_curve_name (NID_secp521r1); + BlindPrivateKeyECDSA (publicKeyLength, group, priv, seed, blindedPriv, blindedPub); + EC_GROUP_free (group); + break; + } + case i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519: + { + i2p::crypto::GetEd25519 ()->BlindPrivateKey (priv, seed, blindedPriv, blindedPub); + publicKeyLength = i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH; + break; + } + default: + LogPrint (eLogError, "Blinding: can't blind signature type ", (int)m_SigType); + } + return publicKeyLength; } void BlindedPublicKey::H (const std::string& p, const std::vector >& bufs, uint8_t * hash) const diff --git a/libi2pd/Blinding.h b/libi2pd/Blinding.h index 56e0d4cd..f0387063 100644 --- a/libi2pd/Blinding.h +++ b/libi2pd/Blinding.h @@ -25,7 +25,7 @@ namespace data void GetSubcredential (const uint8_t * blinded, size_t len, uint8_t * subcredential) const; // 32 bytes void GetBlindedKey (const char * date, uint8_t * blindedKey) const; // blinded key 32 bytes, date is 8 chars "YYYYMMDD" - void BlindPrivateKey (const uint8_t * priv, const char * date, uint8_t * blindedPriv, uint8_t * blindedPub) const; // blinded key 32 bytes, date is 8 chars "YYYYMMDD" + size_t BlindPrivateKey (const uint8_t * priv, const char * date, uint8_t * blindedPriv, uint8_t * blindedPub) const; // date is 8 chars "YYYYMMDD", return public key length i2p::data::IdentHash GetStoreHash (const char * date = nullptr) const; // date is 8 chars "YYYYMMDD", use current if null private: diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index ad09ab2a..5e2b859e 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -727,12 +727,12 @@ namespace data auto timestamp = i2p::util::GetSecondsSinceEpoch (); char date[9]; i2p::util::GetDateString (timestamp, date); - uint8_t blindedPriv[32], blindedPub[32]; - blindedKey.BlindPrivateKey (keys.GetSigningPrivateKey (), date, blindedPriv, blindedPub); + uint8_t blindedPriv[64], blindedPub[128]; // 64 and 128 max + size_t publicKeyLen = blindedKey.BlindPrivateKey (keys.GetSigningPrivateKey (), date, blindedPriv, blindedPub); std::unique_ptr blindedSigner (i2p::data::PrivateKeys::CreateSigner (blindedKeyType, blindedPriv)); auto offset = 1; htobe16buf (m_Buffer + offset, blindedKeyType); offset += 2; // Blinded Public Key Sig Type - memcpy (m_Buffer + offset, blindedPub, 32); offset += 32; // Blinded Public Key + memcpy (m_Buffer + offset, blindedPub, publicKeyLen); offset += publicKeyLen; // Blinded Public Key htobe32buf (m_Buffer + offset, timestamp); offset += 4; // published timestamp (seconds) auto nextMidnight = (timestamp/86400LL + 1)*86400LL; // 86400 = 24*3600 seconds auto expirationTime = ls->GetExpirationTime ()/1000LL;