mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
667 lines
22 KiB
667 lines
22 KiB
11 years ago
|
#include <time.h>
|
||
|
#include <stdio.h>
|
||
9 years ago
|
#include "Crypto.h"
|
||
10 years ago
|
#include "I2PEndian.h"
|
||
9 years ago
|
#include "Log.h"
|
||
|
#include "Identity.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace data
|
||
|
{
|
||
11 years ago
|
Identity& Identity::operator=(const Keys& keys)
|
||
|
{
|
||
|
// copy public and signing keys together
|
||
|
memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey));
|
||
9 years ago
|
memset (certificate, 0, sizeof (certificate));
|
||
11 years ago
|
return *this;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
size_t Identity::FromBuffer (const uint8_t * buf, size_t len)
|
||
|
{
|
||
9 years ago
|
if ( len < DEFAULT_IDENTITY_SIZE ) {
|
||
|
// buffer too small, don't overflow
|
||
|
return 0;
|
||
|
}
|
||
11 years ago
|
memcpy (publicKey, buf, DEFAULT_IDENTITY_SIZE);
|
||
|
return DEFAULT_IDENTITY_SIZE;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
IdentHash Identity::Hash () const
|
||
|
{
|
||
|
IdentHash hash;
|
||
9 years ago
|
SHA256(publicKey, DEFAULT_IDENTITY_SIZE, hash);
|
||
10 years ago
|
return hash;
|
||
|
}
|
||
|
|
||
10 years ago
|
IdentityEx::IdentityEx ():
|
||
8 years ago
|
m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
|
||
10 years ago
|
{
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
IdentityEx::IdentityEx(const uint8_t * publicKey, const uint8_t * signingKey, SigningKeyType type):
|
||
|
m_IsVerifierCreated (false)
|
||
10 years ago
|
{
|
||
|
memcpy (m_StandardIdentity.publicKey, publicKey, sizeof (m_StandardIdentity.publicKey));
|
||
10 years ago
|
if (type != SIGNING_KEY_TYPE_DSA_SHA1)
|
||
10 years ago
|
{
|
||
10 years ago
|
size_t excessLen = 0;
|
||
|
uint8_t * excessBuf = nullptr;
|
||
|
switch (type)
|
||
|
{
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
|
||
|
{
|
||
|
size_t padding = 128 - i2p::crypto::ECDSAP256_KEY_LENGTH; // 64 = 128 - 64
|
||
9 years ago
|
RAND_bytes (m_StandardIdentity.signingKey, padding);
|
||
10 years ago
|
memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::ECDSAP256_KEY_LENGTH);
|
||
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
|
||
|
{
|
||
|
size_t padding = 128 - i2p::crypto::ECDSAP384_KEY_LENGTH; // 32 = 128 - 96
|
||
9 years ago
|
RAND_bytes (m_StandardIdentity.signingKey, padding);
|
||
10 years ago
|
memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::ECDSAP384_KEY_LENGTH);
|
||
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
|
||
|
{
|
||
|
memcpy (m_StandardIdentity.signingKey, signingKey, 128);
|
||
10 years ago
|
excessLen = i2p::crypto::ECDSAP521_KEY_LENGTH - 128; // 4 = 132 - 128
|
||
10 years ago
|
excessBuf = new uint8_t[excessLen];
|
||
|
memcpy (excessBuf, signingKey + 128, excessLen);
|
||
|
break;
|
||
|
}
|
||
10 years ago
|
case SIGNING_KEY_TYPE_RSA_SHA256_2048:
|
||
|
{
|
||
|
memcpy (m_StandardIdentity.signingKey, signingKey, 128);
|
||
|
excessLen = i2p::crypto::RSASHA2562048_KEY_LENGTH - 128; // 128 = 256 - 128
|
||
|
excessBuf = new uint8_t[excessLen];
|
||
|
memcpy (excessBuf, signingKey + 128, excessLen);
|
||
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA384_3072:
|
||
|
{
|
||
|
memcpy (m_StandardIdentity.signingKey, signingKey, 128);
|
||
|
excessLen = i2p::crypto::RSASHA3843072_KEY_LENGTH - 128; // 256 = 384 - 128
|
||
|
excessBuf = new uint8_t[excessLen];
|
||
|
memcpy (excessBuf, signingKey + 128, excessLen);
|
||
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA512_4096:
|
||
|
{
|
||
|
memcpy (m_StandardIdentity.signingKey, signingKey, 128);
|
||
|
excessLen = i2p::crypto::RSASHA5124096_KEY_LENGTH - 128; // 384 = 512 - 128
|
||
|
excessBuf = new uint8_t[excessLen];
|
||
|
memcpy (excessBuf, signingKey + 128, excessLen);
|
||
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
|
||
10 years ago
|
{
|
||
8 years ago
|
size_t padding = 128 - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH; // 96 = 128 - 32
|
||
9 years ago
|
RAND_bytes (m_StandardIdentity.signingKey, padding);
|
||
10 years ago
|
memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH);
|
||
10 years ago
|
break;
|
||
|
}
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256_TEST:
|
||
8 years ago
|
{
|
||
8 years ago
|
// 256
|
||
|
size_t padding = 128 - i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH; // 64 = 128 - 64
|
||
8 years ago
|
RAND_bytes (m_StandardIdentity.signingKey, padding);
|
||
8 years ago
|
memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH);
|
||
|
break;
|
||
|
}
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
|
||
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512_TEST:
|
||
8 years ago
|
{
|
||
|
// 512
|
||
|
// no padding, key length is 128
|
||
|
memcpy (m_StandardIdentity.signingKey, signingKey, i2p::crypto::GOSTR3410_512_PUBLIC_KEY_LENGTH);
|
||
8 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported");
|
||
10 years ago
|
}
|
||
10 years ago
|
m_ExtendedLen = 4 + excessLen; // 4 bytes extra + excess length
|
||
10 years ago
|
// fill certificate
|
||
9 years ago
|
m_StandardIdentity.certificate[0] = CERTIFICATE_TYPE_KEY;
|
||
|
htobe16buf (m_StandardIdentity.certificate + 1, m_ExtendedLen);
|
||
10 years ago
|
// fill extended buffer
|
||
10 years ago
|
m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
|
||
10 years ago
|
htobe16buf (m_ExtendedBuffer, type);
|
||
|
htobe16buf (m_ExtendedBuffer + 2, CRYPTO_KEY_TYPE_ELGAMAL);
|
||
10 years ago
|
if (excessLen && excessBuf)
|
||
|
{
|
||
|
memcpy (m_ExtendedBuffer + 4, excessBuf, excessLen);
|
||
|
delete[] excessBuf;
|
||
|
}
|
||
|
// calculate ident hash
|
||
10 years ago
|
uint8_t * buf = new uint8_t[GetFullLen ()];
|
||
|
ToBuffer (buf, GetFullLen ());
|
||
9 years ago
|
SHA256(buf, GetFullLen (), m_IdentHash);
|
||
10 years ago
|
delete[] buf;
|
||
10 years ago
|
}
|
||
|
else // DSA-SHA1
|
||
|
{
|
||
|
memcpy (m_StandardIdentity.signingKey, signingKey, sizeof (m_StandardIdentity.signingKey));
|
||
9 years ago
|
memset (m_StandardIdentity.certificate, 0, sizeof (m_StandardIdentity.certificate));
|
||
10 years ago
|
m_IdentHash = m_StandardIdentity.Hash ();
|
||
10 years ago
|
m_ExtendedLen = 0;
|
||
|
m_ExtendedBuffer = nullptr;
|
||
|
}
|
||
|
CreateVerifier ();
|
||
|
}
|
||
|
|
||
10 years ago
|
IdentityEx::IdentityEx (const uint8_t * buf, size_t len):
|
||
8 years ago
|
m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
|
||
10 years ago
|
{
|
||
|
FromBuffer (buf, len);
|
||
|
}
|
||
|
|
||
|
IdentityEx::IdentityEx (const IdentityEx& other):
|
||
8 years ago
|
m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
|
||
10 years ago
|
{
|
||
|
*this = other;
|
||
|
}
|
||
9 years ago
|
|
||
|
IdentityEx::IdentityEx (const Identity& standard):
|
||
8 years ago
|
m_IsVerifierCreated (false), m_ExtendedLen (0), m_ExtendedBuffer (nullptr)
|
||
9 years ago
|
{
|
||
|
*this = standard;
|
||
|
}
|
||
10 years ago
|
|
||
|
IdentityEx::~IdentityEx ()
|
||
|
{
|
||
|
delete[] m_ExtendedBuffer;
|
||
|
}
|
||
|
|
||
|
IdentityEx& IdentityEx::operator=(const IdentityEx& other)
|
||
|
{
|
||
|
memcpy (&m_StandardIdentity, &other.m_StandardIdentity, DEFAULT_IDENTITY_SIZE);
|
||
|
m_IdentHash = other.m_IdentHash;
|
||
|
|
||
|
delete[] m_ExtendedBuffer;
|
||
|
m_ExtendedLen = other.m_ExtendedLen;
|
||
|
if (m_ExtendedLen > 0)
|
||
|
{
|
||
|
m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
|
||
|
memcpy (m_ExtendedBuffer, other.m_ExtendedBuffer, m_ExtendedLen);
|
||
|
}
|
||
|
else
|
||
|
m_ExtendedBuffer = nullptr;
|
||
10 years ago
|
|
||
10 years ago
|
m_Verifier = nullptr;
|
||
8 years ago
|
m_IsVerifierCreated = false;
|
||
10 years ago
|
|
||
10 years ago
|
return *this;
|
||
|
}
|
||
|
|
||
10 years ago
|
IdentityEx& IdentityEx::operator=(const Identity& standard)
|
||
|
{
|
||
|
m_StandardIdentity = standard;
|
||
|
m_IdentHash = m_StandardIdentity.Hash ();
|
||
|
|
||
|
delete[] m_ExtendedBuffer;
|
||
|
m_ExtendedBuffer = nullptr;
|
||
|
m_ExtendedLen = 0;
|
||
10 years ago
|
|
||
10 years ago
|
m_Verifier = nullptr;
|
||
8 years ago
|
m_IsVerifierCreated = false;
|
||
10 years ago
|
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t IdentityEx::FromBuffer (const uint8_t * buf, size_t len)
|
||
|
{
|
||
10 years ago
|
if (len < DEFAULT_IDENTITY_SIZE)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Identity: buffer length ", len, " is too small");
|
||
10 years ago
|
return 0;
|
||
|
}
|
||
10 years ago
|
memcpy (&m_StandardIdentity, buf, DEFAULT_IDENTITY_SIZE);
|
||
10 years ago
|
|
||
9 years ago
|
if(m_ExtendedBuffer) delete[] m_ExtendedBuffer;
|
||
|
m_ExtendedBuffer = nullptr;
|
||
|
|
||
9 years ago
|
m_ExtendedLen = bufbe16toh (m_StandardIdentity.certificate + 1);
|
||
|
if (m_ExtendedLen)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (m_ExtendedLen + DEFAULT_IDENTITY_SIZE <= len)
|
||
|
{
|
||
|
m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
|
||
|
memcpy (m_ExtendedBuffer, buf + DEFAULT_IDENTITY_SIZE, m_ExtendedLen);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Identity: Certificate length ", m_ExtendedLen, " exceeds buffer length ", len - DEFAULT_IDENTITY_SIZE);
|
||
9 years ago
|
m_ExtendedLen = 0;
|
||
10 years ago
|
return 0;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
m_ExtendedLen = 0;
|
||
|
m_ExtendedBuffer = nullptr;
|
||
|
}
|
||
9 years ago
|
SHA256(buf, GetFullLen (), m_IdentHash);
|
||
10 years ago
|
|
||
10 years ago
|
m_Verifier = nullptr;
|
||
10 years ago
|
|
||
10 years ago
|
return GetFullLen ();
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t IdentityEx::ToBuffer (uint8_t * buf, size_t len) const
|
||
9 years ago
|
{
|
||
9 years ago
|
const size_t fullLen = GetFullLen();
|
||
9 years ago
|
if (fullLen > len) return 0; // buffer is too small and may overflow somewhere else
|
||
10 years ago
|
memcpy (buf, &m_StandardIdentity, DEFAULT_IDENTITY_SIZE);
|
||
|
if (m_ExtendedLen > 0 && m_ExtendedBuffer)
|
||
|
memcpy (buf + DEFAULT_IDENTITY_SIZE, m_ExtendedBuffer, m_ExtendedLen);
|
||
9 years ago
|
return fullLen;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
size_t IdentityEx::FromBase64(const std::string& s)
|
||
|
{
|
||
9 years ago
|
const size_t slen = s.length();
|
||
9 years ago
|
std::vector<uint8_t> buf(slen); // binary data can't exceed base64
|
||
|
const size_t len = Base64ToByteStream (s.c_str(), slen, buf.data(), slen);
|
||
|
return FromBuffer (buf.data(), len);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
std::string IdentityEx::ToBase64 () const
|
||
|
{
|
||
9 years ago
|
const size_t bufLen = GetFullLen();
|
||
|
const size_t strLen = Base64EncodingBufferSize(bufLen);
|
||
9 years ago
|
std::vector<uint8_t> buf(bufLen);
|
||
|
std::vector<char> str(strLen);
|
||
|
size_t l = ToBuffer (buf.data(), bufLen);
|
||
|
size_t l1 = i2p::data::ByteStreamToBase64 (buf.data(), l, str.data(), strLen);
|
||
|
return std::string (str.data(), l1);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
size_t IdentityEx::GetSigningPublicKeyLen () const
|
||
10 years ago
|
{
|
||
10 years ago
|
if (!m_Verifier) CreateVerifier ();
|
||
|
if (m_Verifier)
|
||
10 years ago
|
return m_Verifier->GetPublicKeyLen ();
|
||
|
return 128;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
size_t IdentityEx::GetSigningPrivateKeyLen () const
|
||
|
{
|
||
|
if (!m_Verifier) CreateVerifier ();
|
||
|
if (m_Verifier)
|
||
|
return m_Verifier->GetPrivateKeyLen ();
|
||
|
return GetSignatureLen ()/2;
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t IdentityEx::GetSignatureLen () const
|
||
10 years ago
|
{
|
||
|
if (!m_Verifier) CreateVerifier ();
|
||
10 years ago
|
if (m_Verifier)
|
||
|
return m_Verifier->GetSignatureLen ();
|
||
9 years ago
|
return i2p::crypto::DSA_SIGNATURE_LENGTH;
|
||
10 years ago
|
}
|
||
10 years ago
|
bool IdentityEx::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
||
10 years ago
|
{
|
||
10 years ago
|
if (!m_Verifier) CreateVerifier ();
|
||
10 years ago
|
if (m_Verifier)
|
||
|
return m_Verifier->Verify (buf, len, signature);
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
|
SigningKeyType IdentityEx::GetSigningKeyType () const
|
||
|
{
|
||
8 years ago
|
if (m_StandardIdentity.certificate[0] == CERTIFICATE_TYPE_KEY && m_ExtendedLen >= 2)
|
||
10 years ago
|
return bufbe16toh (m_ExtendedBuffer); // signing key
|
||
10 years ago
|
return SIGNING_KEY_TYPE_DSA_SHA1;
|
||
|
}
|
||
10 years ago
|
|
||
|
CryptoKeyType IdentityEx::GetCryptoKeyType () const
|
||
|
{
|
||
8 years ago
|
if (m_StandardIdentity.certificate[0] == CERTIFICATE_TYPE_KEY && m_ExtendedLen >= 4)
|
||
10 years ago
|
return bufbe16toh (m_ExtendedBuffer + 2); // crypto key
|
||
10 years ago
|
return CRYPTO_KEY_TYPE_ELGAMAL;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void IdentityEx::CreateVerifier () const
|
||
10 years ago
|
{
|
||
8 years ago
|
if (m_Verifier) return; // don't create again
|
||
10 years ago
|
auto keyType = GetSigningKeyType ();
|
||
|
switch (keyType)
|
||
|
{
|
||
|
case SIGNING_KEY_TYPE_DSA_SHA1:
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto::DSAVerifier (m_StandardIdentity.signingKey));
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
|
||
10 years ago
|
{
|
||
|
size_t padding = 128 - i2p::crypto::ECDSAP256_KEY_LENGTH; // 64 = 128 - 64
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto::ECDSAP256Verifier (m_StandardIdentity.signingKey + padding));
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
|
||
|
{
|
||
|
size_t padding = 128 - i2p::crypto::ECDSAP384_KEY_LENGTH; // 32 = 128 - 96
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto::ECDSAP384Verifier (m_StandardIdentity.signingKey + padding));
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
|
||
|
{
|
||
|
uint8_t signingKey[i2p::crypto::ECDSAP521_KEY_LENGTH];
|
||
|
memcpy (signingKey, m_StandardIdentity.signingKey, 128);
|
||
|
size_t excessLen = i2p::crypto::ECDSAP521_KEY_LENGTH - 128; // 4 = 132- 128
|
||
|
memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto::ECDSAP521Verifier (signingKey));
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
case SIGNING_KEY_TYPE_RSA_SHA256_2048:
|
||
|
{
|
||
|
uint8_t signingKey[i2p::crypto::RSASHA2562048_KEY_LENGTH];
|
||
|
memcpy (signingKey, m_StandardIdentity.signingKey, 128);
|
||
|
size_t excessLen = i2p::crypto::RSASHA2562048_KEY_LENGTH - 128; // 128 = 256- 128
|
||
|
memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto:: RSASHA2562048Verifier (signingKey));
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA384_3072:
|
||
|
{
|
||
|
uint8_t signingKey[i2p::crypto::RSASHA3843072_KEY_LENGTH];
|
||
|
memcpy (signingKey, m_StandardIdentity.signingKey, 128);
|
||
|
size_t excessLen = i2p::crypto::RSASHA3843072_KEY_LENGTH - 128; // 256 = 384- 128
|
||
|
memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto:: RSASHA3843072Verifier (signingKey));
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA512_4096:
|
||
|
{
|
||
|
uint8_t signingKey[i2p::crypto::RSASHA5124096_KEY_LENGTH];
|
||
|
memcpy (signingKey, m_StandardIdentity.signingKey, 128);
|
||
|
size_t excessLen = i2p::crypto::RSASHA5124096_KEY_LENGTH - 128; // 384 = 512- 128
|
||
|
memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto:: RSASHA5124096Verifier (signingKey));
|
||
10 years ago
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
|
||
10 years ago
|
{
|
||
10 years ago
|
size_t padding = 128 - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH; // 96 = 128 - 32
|
||
9 years ago
|
UpdateVerifier (new i2p::crypto::EDDSA25519Verifier (m_StandardIdentity.signingKey + padding));
|
||
10 years ago
|
break;
|
||
|
}
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256_TEST:
|
||
8 years ago
|
{
|
||
8 years ago
|
size_t padding = 128 - i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH; // 64 = 128 - 64
|
||
8 years ago
|
UpdateVerifier (new i2p::crypto::GOSTR3410_256_Verifier (i2p::crypto::eGOSTR3410CryptoProA, m_StandardIdentity.signingKey + padding));
|
||
8 years ago
|
break;
|
||
|
}
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512_TEST:
|
||
8 years ago
|
{
|
||
8 years ago
|
// zero padding
|
||
8 years ago
|
UpdateVerifier (new i2p::crypto::GOSTR3410_512_Verifier (i2p::crypto::eGOSTR3410TC26A512, m_StandardIdentity.signingKey));
|
||
8 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint (eLogError, "Identity: Signing key type ", (int)keyType, " is not supported");
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
void IdentityEx::UpdateVerifier (i2p::crypto::Verifier * verifier) const
|
||
|
{
|
||
8 years ago
|
if (!m_Verifier)
|
||
8 years ago
|
{
|
||
|
auto created = m_IsVerifierCreated.exchange (true);
|
||
|
if (!created)
|
||
|
m_Verifier.reset (verifier);
|
||
|
else
|
||
8 years ago
|
{
|
||
8 years ago
|
delete verifier;
|
||
8 years ago
|
int count = 0;
|
||
|
while (!m_Verifier && count < 500) // 5 seconds
|
||
|
{
|
||
|
std::this_thread::sleep_for (std::chrono::milliseconds(10));
|
||
|
count++;
|
||
|
}
|
||
|
if (!m_Verifier)
|
||
|
LogPrint (eLogError, "Identity: couldn't get verifier in 5 seconds");
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
else
|
||
|
delete verifier;
|
||
|
}
|
||
|
|
||
9 years ago
|
void IdentityEx::DropVerifier () const
|
||
10 years ago
|
{
|
||
9 years ago
|
// TODO: potential race condition with Verify
|
||
8 years ago
|
m_IsVerifierCreated = false;
|
||
|
m_Verifier = nullptr;
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
PrivateKeys& PrivateKeys::operator=(const Keys& keys)
|
||
|
{
|
||
9 years ago
|
m_Public = std::make_shared<IdentityEx>(Identity (keys));
|
||
10 years ago
|
memcpy (m_PrivateKey, keys.privateKey, 256); // 256
|
||
9 years ago
|
memcpy (m_SigningPrivateKey, keys.signingPrivateKey, m_Public->GetSigningPrivateKeyLen ());
|
||
10 years ago
|
m_Signer = nullptr;
|
||
10 years ago
|
CreateSigner ();
|
||
11 years ago
|
return *this;
|
||
|
}
|
||
|
|
||
10 years ago
|
PrivateKeys& PrivateKeys::operator=(const PrivateKeys& other)
|
||
|
{
|
||
9 years ago
|
m_Public = std::make_shared<IdentityEx>(*other.m_Public);
|
||
10 years ago
|
memcpy (m_PrivateKey, other.m_PrivateKey, 256); // 256
|
||
9 years ago
|
memcpy (m_SigningPrivateKey, other.m_SigningPrivateKey, m_Public->GetSigningPrivateKeyLen ());
|
||
10 years ago
|
m_Signer = nullptr;
|
||
10 years ago
|
CreateSigner ();
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t PrivateKeys::FromBuffer (const uint8_t * buf, size_t len)
|
||
|
{
|
||
8 years ago
|
m_Public = std::make_shared<IdentityEx>();
|
||
|
size_t ret = m_Public->FromBuffer (buf, len);
|
||
|
if (!ret || ret + 256 > len) return 0; // overflow
|
||
10 years ago
|
memcpy (m_PrivateKey, buf + ret, 256); // private key always 256
|
||
|
ret += 256;
|
||
9 years ago
|
size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen ();
|
||
9 years ago
|
if(signingPrivateKeySize + ret > len) return 0; // overflow
|
||
10 years ago
|
memcpy (m_SigningPrivateKey, buf + ret, signingPrivateKeySize);
|
||
|
ret += signingPrivateKeySize;
|
||
10 years ago
|
m_Signer = nullptr;
|
||
10 years ago
|
CreateSigner ();
|
||
10 years ago
|
return ret;
|
||
|
}
|
||
|
|
||
|
size_t PrivateKeys::ToBuffer (uint8_t * buf, size_t len) const
|
||
|
{
|
||
9 years ago
|
size_t ret = m_Public->ToBuffer (buf, len);
|
||
10 years ago
|
memcpy (buf + ret, m_PrivateKey, 256); // private key always 256
|
||
|
ret += 256;
|
||
9 years ago
|
size_t signingPrivateKeySize = m_Public->GetSigningPrivateKeyLen ();
|
||
|
if(ret + signingPrivateKeySize > len) return 0; // overflow
|
||
10 years ago
|
memcpy (buf + ret, m_SigningPrivateKey, signingPrivateKeySize);
|
||
|
ret += signingPrivateKeySize;
|
||
|
return ret;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
size_t PrivateKeys::FromBase64(const std::string& s)
|
||
|
{
|
||
|
uint8_t * buf = new uint8_t[s.length ()];
|
||
|
size_t l = i2p::data::Base64ToByteStream (s.c_str (), s.length (), buf, s.length ());
|
||
|
size_t ret = FromBuffer (buf, l);
|
||
|
delete[] buf;
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
std::string PrivateKeys::ToBase64 () const
|
||
|
{
|
||
|
uint8_t * buf = new uint8_t[GetFullLen ()];
|
||
|
char * str = new char[GetFullLen ()*2];
|
||
|
size_t l = ToBuffer (buf, GetFullLen ());
|
||
|
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, str, GetFullLen ()*2);
|
||
|
str[l1] = 0;
|
||
|
delete[] buf;
|
||
10 years ago
|
std::string ret(str);
|
||
10 years ago
|
delete[] str;
|
||
10 years ago
|
return ret;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void PrivateKeys::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
||
|
{
|
||
9 years ago
|
if (!m_Signer)
|
||
8 years ago
|
CreateSigner();
|
||
|
m_Signer->Sign (buf, len, signature);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void PrivateKeys::CreateSigner () const
|
||
10 years ago
|
{
|
||
8 years ago
|
if (m_Signer) return;
|
||
9 years ago
|
switch (m_Public->GetSigningKeyType ())
|
||
10 years ago
|
{
|
||
|
case SIGNING_KEY_TYPE_DSA_SHA1:
|
||
8 years ago
|
m_Signer.reset (new i2p::crypto::DSASigner (m_SigningPrivateKey, m_Public->GetStandardIdentity ().signingKey));
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
|
||
9 years ago
|
m_Signer.reset (new i2p::crypto::ECDSAP256Signer (m_SigningPrivateKey));
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
|
||
9 years ago
|
m_Signer.reset (new i2p::crypto::ECDSAP384Signer (m_SigningPrivateKey));
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
|
||
9 years ago
|
m_Signer.reset (new i2p::crypto::ECDSAP521Signer (m_SigningPrivateKey));
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SIGNING_KEY_TYPE_RSA_SHA256_2048:
|
||
9 years ago
|
m_Signer.reset (new i2p::crypto::RSASHA2562048Signer (m_SigningPrivateKey));
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA384_3072:
|
||
9 years ago
|
m_Signer.reset (new i2p::crypto::RSASHA3843072Signer (m_SigningPrivateKey));
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA512_4096:
|
||
9 years ago
|
m_Signer.reset (new i2p::crypto::RSASHA5124096Signer (m_SigningPrivateKey));
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
|
||
8 years ago
|
m_Signer.reset (new i2p::crypto::EDDSA25519Signer (m_SigningPrivateKey, m_Public->GetStandardIdentity ().certificate - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH));
|
||
10 years ago
|
break;
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256_TEST:
|
||
8 years ago
|
m_Signer.reset (new i2p::crypto::GOSTR3410_256_Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey));
|
||
8 years ago
|
break;
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512_TEST:
|
||
8 years ago
|
m_Signer.reset (new i2p::crypto::GOSTR3410_512_Signer (i2p::crypto::eGOSTR3410TC26A512, m_SigningPrivateKey));
|
||
8 years ago
|
break;
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported");
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
PrivateKeys PrivateKeys::CreateRandomKeys (SigningKeyType type)
|
||
|
{
|
||
10 years ago
|
if (type != SIGNING_KEY_TYPE_DSA_SHA1)
|
||
10 years ago
|
{
|
||
|
PrivateKeys keys;
|
||
10 years ago
|
// signature
|
||
10 years ago
|
uint8_t signingPublicKey[512]; // signing public key is 512 bytes max
|
||
10 years ago
|
switch (type)
|
||
|
{
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
|
||
9 years ago
|
i2p::crypto::CreateECDSAP256RandomKeys (keys.m_SigningPrivateKey, signingPublicKey);
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
|
||
9 years ago
|
i2p::crypto::CreateECDSAP384RandomKeys (keys.m_SigningPrivateKey, signingPublicKey);
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
|
||
9 years ago
|
i2p::crypto::CreateECDSAP521RandomKeys (keys.m_SigningPrivateKey, signingPublicKey);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SIGNING_KEY_TYPE_RSA_SHA256_2048:
|
||
9 years ago
|
i2p::crypto::CreateRSARandomKeys (i2p::crypto::RSASHA2562048_KEY_LENGTH, keys.m_SigningPrivateKey, signingPublicKey);
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA384_3072:
|
||
9 years ago
|
i2p::crypto::CreateRSARandomKeys (i2p::crypto::RSASHA3843072_KEY_LENGTH, keys.m_SigningPrivateKey, signingPublicKey);
|
||
10 years ago
|
break;
|
||
|
case SIGNING_KEY_TYPE_RSA_SHA512_4096:
|
||
9 years ago
|
i2p::crypto::CreateRSARandomKeys (i2p::crypto::RSASHA5124096_KEY_LENGTH, keys.m_SigningPrivateKey, signingPublicKey);
|
||
10 years ago
|
break;
|
||
9 years ago
|
case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
|
||
|
i2p::crypto::CreateEDDSA25519RandomKeys (keys.m_SigningPrivateKey, signingPublicKey);
|
||
8 years ago
|
break;
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256_TEST:
|
||
8 years ago
|
i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProA, keys.m_SigningPrivateKey, signingPublicKey);
|
||
8 years ago
|
break;
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
|
||
8 years ago
|
case SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512_TEST:
|
||
8 years ago
|
i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26A512, keys.m_SigningPrivateKey, signingPublicKey);
|
||
8 years ago
|
break;
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1");
|
||
10 years ago
|
return PrivateKeys (i2p::data::CreateRandomKeys ()); // DSA-SHA1
|
||
|
}
|
||
10 years ago
|
// encryption
|
||
|
uint8_t publicKey[256];
|
||
9 years ago
|
i2p::crypto::GenerateElGamalKeyPair (keys.m_PrivateKey, publicKey);
|
||
10 years ago
|
// identity
|
||
9 years ago
|
keys.m_Public = std::make_shared<IdentityEx> (publicKey, signingPublicKey, type);
|
||
10 years ago
|
|
||
|
keys.CreateSigner ();
|
||
10 years ago
|
return keys;
|
||
|
}
|
||
|
return PrivateKeys (i2p::data::CreateRandomKeys ()); // DSA-SHA1
|
||
|
}
|
||
10 years ago
|
|
||
11 years ago
|
Keys CreateRandomKeys ()
|
||
|
{
|
||
|
Keys keys;
|
||
|
// encryption
|
||
9 years ago
|
i2p::crypto::GenerateElGamalKeyPair(keys.privateKey, keys.publicKey);
|
||
11 years ago
|
// signing
|
||
9 years ago
|
i2p::crypto::CreateDSARandomKeys (keys.signingPrivateKey, keys.signingKey);
|
||
11 years ago
|
return keys;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
IdentHash CreateRoutingKey (const IdentHash& ident)
|
||
11 years ago
|
{
|
||
|
uint8_t buf[41]; // ident + yyyymmdd
|
||
|
memcpy (buf, (const uint8_t *)ident, 32);
|
||
|
time_t t = time (nullptr);
|
||
|
struct tm tm;
|
||
11 years ago
|
#ifdef _WIN32
|
||
|
gmtime_s(&tm, &t);
|
||
10 years ago
|
sprintf_s((char *)(buf + 32), 9, "%04i%02i%02i", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||
11 years ago
|
#else
|
||
|
gmtime_r(&t, &tm);
|
||
10 years ago
|
sprintf((char *)(buf + 32), "%04i%02i%02i", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||
11 years ago
|
#endif
|
||
10 years ago
|
IdentHash key;
|
||
9 years ago
|
SHA256(buf, 40, key);
|
||
11 years ago
|
return key;
|
||
|
}
|
||
|
|
||
10 years ago
|
XORMetric operator^(const IdentHash& key1, const IdentHash& key2)
|
||
11 years ago
|
{
|
||
|
XORMetric m;
|
||
8 years ago
|
#if defined(__AVX__) // for AVX
|
||
|
__asm__
|
||
|
(
|
||
|
"vmovups %1, %%ymm0 \n"
|
||
|
"vmovups %2, %%ymm1 \n"
|
||
|
"vxorps %%ymm0, %%ymm1, %%ymm1 \n"
|
||
|
"vmovups %%ymm1, %0 \n"
|
||
|
: "=m"(*m.metric)
|
||
|
: "m"(*key1), "m"(*key2)
|
||
|
: "memory", "%xmm0", "%xmm1" // should be replaced by %ymm0/1 once supported by compiler
|
||
|
);
|
||
|
#else
|
||
10 years ago
|
const uint64_t * hash1 = key1.GetLL (), * hash2 = key2.GetLL ();
|
||
|
m.metric_ll[0] = hash1[0] ^ hash2[0];
|
||
|
m.metric_ll[1] = hash1[1] ^ hash2[1];
|
||
|
m.metric_ll[2] = hash1[2] ^ hash2[2];
|
||
|
m.metric_ll[3] = hash1[3] ^ hash2[3];
|
||
8 years ago
|
#endif
|
||
|
|
||
11 years ago
|
return m;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|