Browse Source

32 bytes private key for ECIESx25519

pull/1575/head
orignal 4 years ago
parent
commit
b12fa97a38
  1. 20
      libi2pd/Identity.cpp
  2. 1
      libi2pd/Identity.h

20
libi2pd/Identity.cpp

@ -476,7 +476,7 @@ namespace data
size_t PrivateKeys::GetFullLen () const size_t PrivateKeys::GetFullLen () const
{ {
size_t ret = m_Public->GetFullLen () + 256 + m_Public->GetSigningPrivateKeyLen (); size_t ret = m_Public->GetFullLen () + GetPrivateKeyLen () + m_Public->GetSigningPrivateKeyLen ();
if (IsOfflineSignature ()) if (IsOfflineSignature ())
ret += m_OfflineSignature.size () + m_TransientSigningPrivateKeyLen; ret += m_OfflineSignature.size () + m_TransientSigningPrivateKeyLen;
return ret; return ret;
@ -486,9 +486,10 @@ namespace data
{ {
m_Public = std::make_shared<IdentityEx>(); m_Public = std::make_shared<IdentityEx>();
size_t ret = m_Public->FromBuffer (buf, len); size_t ret = m_Public->FromBuffer (buf, len);
if (!ret || ret + 256 > len) return 0; // overflow auto cryptoKeyLen = GetPrivateKeyLen ();
memcpy (m_PrivateKey, buf + ret, 256); // private key always 256 if (!ret || ret + cryptoKeyLen > len) return 0; // overflow
ret += 256; memcpy (m_PrivateKey, buf + ret, cryptoKeyLen);
ret += cryptoKeyLen;
size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen (); size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen ();
if(signingPrivateKeySize + ret > len || signingPrivateKeySize > 128) return 0; // overflow if(signingPrivateKeySize + ret > len || signingPrivateKeySize > 128) return 0; // overflow
memcpy (m_SigningPrivateKey, buf + ret, signingPrivateKeySize); memcpy (m_SigningPrivateKey, buf + ret, signingPrivateKeySize);
@ -540,8 +541,9 @@ namespace data
size_t PrivateKeys::ToBuffer (uint8_t * buf, size_t len) const size_t PrivateKeys::ToBuffer (uint8_t * buf, size_t len) const
{ {
size_t ret = m_Public->ToBuffer (buf, len); size_t ret = m_Public->ToBuffer (buf, len);
memcpy (buf + ret, m_PrivateKey, 256); // private key always 256 auto cryptoKeyLen = GetPrivateKeyLen ();
ret += 256; memcpy (buf + ret, m_PrivateKey, cryptoKeyLen);
ret += cryptoKeyLen;
size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen (); size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen ();
if(ret + signingPrivateKeySize > len) return 0; // overflow if(ret + signingPrivateKeySize > len) return 0; // overflow
if (IsOfflineSignature ()) if (IsOfflineSignature ())
@ -657,6 +659,12 @@ namespace data
return IsOfflineSignature () ? m_TransientSignatureLen : m_Public->GetSignatureLen (); return IsOfflineSignature () ? m_TransientSignatureLen : m_Public->GetSignatureLen ();
} }
size_t PrivateKeys::GetPrivateKeyLen () const
{
// private key length always 256, but type 4
return (m_Public->GetCryptoKeyType () == CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET) ? 32 : 256;
}
uint8_t * PrivateKeys::GetPadding() uint8_t * PrivateKeys::GetPadding()
{ {
if(m_Public->GetSigningKeyType () == SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519) if(m_Public->GetSigningKeyType () == SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519)

1
libi2pd/Identity.h

@ -183,6 +183,7 @@ namespace data
void CreateSigner () const; void CreateSigner () const;
void CreateSigner (SigningKeyType keyType) const; void CreateSigner (SigningKeyType keyType) const;
size_t GetPrivateKeyLen () const;
private: private:

Loading…
Cancel
Save