mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-28 19:57:48 +02:00
fix code syle(spaces->tabs, tabulations)
Signed-off-by: R4SAS <r4sas@i2pmail.org>
This commit is contained in:
parent
78640532e1
commit
44f6683c41
115 changed files with 3206 additions and 3161 deletions
|
@ -17,21 +17,21 @@ namespace i2p
|
|||
namespace data
|
||||
{
|
||||
static EC_POINT * BlindPublicKeyECDSA (const EC_GROUP * group, const EC_POINT * pub, const uint8_t * seed)
|
||||
{
|
||||
{
|
||||
BN_CTX * ctx = BN_CTX_new ();
|
||||
BN_CTX_start (ctx);
|
||||
BIGNUM * q = BN_CTX_get (ctx);
|
||||
BIGNUM * q = BN_CTX_get (ctx);
|
||||
EC_GROUP_get_order (group, q, ctx);
|
||||
// calculate alpha = seed mod q
|
||||
BIGNUM * alpha = BN_CTX_get (ctx);
|
||||
BN_bin2bn (seed, 64, alpha); // seed is in BigEndian
|
||||
BN_bin2bn (seed, 64, alpha); // seed is in BigEndian
|
||||
BN_mod (alpha, alpha, q, ctx); // % q
|
||||
// A' = BLIND_PUBKEY(A, alpha) = A + DERIVE_PUBLIC(alpha)
|
||||
auto p = EC_POINT_new (group);
|
||||
EC_POINT_mul (group, p, alpha, nullptr, nullptr, ctx); // B*alpha
|
||||
EC_POINT_add (group, p, pub, p, ctx); // pub + B*alpha
|
||||
BN_CTX_end (ctx);
|
||||
BN_CTX_free (ctx);
|
||||
BN_CTX_free (ctx);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -39,18 +39,18 @@ namespace data
|
|||
{
|
||||
BN_CTX * ctx = BN_CTX_new ();
|
||||
BN_CTX_start (ctx);
|
||||
BIGNUM * q = BN_CTX_get (ctx);
|
||||
BIGNUM * q = BN_CTX_get (ctx);
|
||||
EC_GROUP_get_order (group, q, ctx);
|
||||
// calculate alpha = seed mod q
|
||||
BIGNUM * alpha = BN_CTX_get (ctx);
|
||||
BN_bin2bn (seed, 64, alpha); // seed is in BigEndian
|
||||
BN_mod (alpha, alpha, q, ctx); // % q
|
||||
BN_bin2bn (seed, 64, alpha); // seed is in BigEndian
|
||||
BN_mod (alpha, alpha, q, ctx); // % q
|
||||
BN_add (alpha, alpha, priv); // alpha = alpha + priv
|
||||
// a' = BLIND_PRIVKEY(a, alpha) = (a + alpha) mod q
|
||||
// a' = BLIND_PRIVKEY(a, alpha) = (a + alpha) mod q
|
||||
BN_mod (blindedPriv, alpha, q, ctx); // % q
|
||||
BN_CTX_end (ctx);
|
||||
BN_CTX_free (ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void BlindEncodedPublicKeyECDSA (size_t publicKeyLen, const EC_GROUP * group, const uint8_t * pub, const uint8_t * seed, uint8_t * blindedPub)
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ namespace data
|
|||
EC_POINT_get_affine_coordinates_GFp (group, p1, x, y, NULL);
|
||||
EC_POINT_free (p1);
|
||||
i2p::crypto::bn2buf (x, blindedPub, publicKeyLen/2);
|
||||
i2p::crypto::bn2buf (y, blindedPub + publicKeyLen/2, publicKeyLen/2);
|
||||
i2p::crypto::bn2buf (y, blindedPub + publicKeyLen/2, publicKeyLen/2);
|
||||
BN_free (x); BN_free (y);
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ namespace data
|
|||
i2p::crypto::bn2buf (x, blindedPub, publicKeyLen/2);
|
||||
i2p::crypto::bn2buf (y, blindedPub + publicKeyLen/2, publicKeyLen/2);
|
||||
BN_free (x); BN_free (y);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Fn, typename...Args>
|
||||
static size_t BlindECDSA (i2p::data::SigningKeyType sigType, const uint8_t * key, const uint8_t * seed, Fn blind, Args&&...args)
|
||||
|
@ -97,7 +97,7 @@ namespace data
|
|||
{
|
||||
case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
|
||||
{
|
||||
publicKeyLength = i2p::crypto::ECDSAP256_KEY_LENGTH;
|
||||
publicKeyLength = i2p::crypto::ECDSAP256_KEY_LENGTH;
|
||||
group = EC_GROUP_new_by_curve_name (NID_X9_62_prime256v1);
|
||||
break;
|
||||
}
|
||||
|
@ -116,18 +116,18 @@ namespace data
|
|||
default:
|
||||
LogPrint (eLogError, "Blinding: signature type ", (int)sigType, " is not ECDSA");
|
||||
}
|
||||
if (group)
|
||||
if (group)
|
||||
{
|
||||
blind (publicKeyLength, group, key, seed, std::forward<Args>(args)...);
|
||||
EC_GROUP_free (group);
|
||||
}
|
||||
}
|
||||
return publicKeyLength;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
|
||||
const uint8_t B33_TWO_BYTES_SIGTYPE_FLAG = 0x01;
|
||||
const uint8_t B33_PER_SECRET_FLAG = 0x02; // not used for now
|
||||
const uint8_t B33_PER_SECRET_FLAG = 0x02; // not used for now
|
||||
const uint8_t B33_PER_CLIENT_AUTH_FLAG = 0x04;
|
||||
|
||||
BlindedPublicKey::BlindedPublicKey (std::shared_ptr<const IdentityEx> identity, bool clientAuth):
|
||||
|
@ -138,7 +138,7 @@ namespace data
|
|||
m_PublicKey.resize (len);
|
||||
memcpy (m_PublicKey.data (), identity->GetSigningPublicKeyBuffer (), len);
|
||||
m_SigType = identity->GetSigningKeyType ();
|
||||
m_BlindedSigType = m_SigType;
|
||||
m_BlindedSigType = m_SigType;
|
||||
}
|
||||
|
||||
BlindedPublicKey::BlindedPublicKey (const std::string& b33):
|
||||
|
@ -150,12 +150,12 @@ namespace data
|
|||
{
|
||||
LogPrint (eLogError, "Blinding: malformed b33 ", b33);
|
||||
return;
|
||||
}
|
||||
uint32_t checksum = crc32 (0, addr + 3, l - 3);
|
||||
}
|
||||
uint32_t checksum = crc32 (0, addr + 3, l - 3);
|
||||
// checksum is Little Endian
|
||||
addr[0] ^= checksum; addr[1] ^= (checksum >> 8); addr[2] ^= (checksum >> 16);
|
||||
addr[0] ^= checksum; addr[1] ^= (checksum >> 8); addr[2] ^= (checksum >> 16);
|
||||
uint8_t flags = addr[0];
|
||||
size_t offset = 1;
|
||||
size_t offset = 1;
|
||||
if (flags & B33_TWO_BYTES_SIGTYPE_FLAG) // two bytes signatures
|
||||
{
|
||||
m_SigType = bufbe16toh (addr + offset); offset += 2;
|
||||
|
@ -178,7 +178,7 @@ namespace data
|
|||
memcpy (m_PublicKey.data (), addr + offset, len);
|
||||
}
|
||||
else
|
||||
LogPrint (eLogError, "Blinding: public key in b33 address is too short for signature type ", (int)m_SigType);
|
||||
LogPrint (eLogError, "Blinding: public key in b33 address is too short for signature type ", (int)m_SigType);
|
||||
}
|
||||
else
|
||||
LogPrint (eLogError, "Blinding: unknown signature type ", (int)m_SigType, " in b33");
|
||||
|
@ -189,25 +189,25 @@ namespace data
|
|||
if (m_PublicKey.size () > 32) return ""; // assume 25519
|
||||
uint8_t addr[35]; char str[60]; // TODO: define actual length
|
||||
uint8_t flags = 0;
|
||||
if (m_IsClientAuth) flags |= B33_PER_CLIENT_AUTH_FLAG;
|
||||
if (m_IsClientAuth) flags |= B33_PER_CLIENT_AUTH_FLAG;
|
||||
addr[0] = flags; // flags
|
||||
addr[1] = m_SigType; // sig type
|
||||
addr[2] = m_BlindedSigType; // blinded sig type
|
||||
memcpy (addr + 3, m_PublicKey.data (), m_PublicKey.size ());
|
||||
uint32_t checksum = crc32 (0, addr + 3, m_PublicKey.size ());
|
||||
uint32_t checksum = crc32 (0, addr + 3, m_PublicKey.size ());
|
||||
// checksum is Little Endian
|
||||
addr[0] ^= checksum; addr[1] ^= (checksum >> 8); addr[2] ^= (checksum >> 16);
|
||||
addr[0] ^= checksum; addr[1] ^= (checksum >> 8); addr[2] ^= (checksum >> 16);
|
||||
auto l = ByteStreamToBase32 (addr, m_PublicKey.size () + 3, str, 60);
|
||||
return std::string (str, str + l);
|
||||
}
|
||||
|
||||
void BlindedPublicKey::GetCredential (uint8_t * credential) const
|
||||
{
|
||||
// A = destination's signing public key
|
||||
// A = destination's signing public key
|
||||
// stA = signature type of A, 2 bytes big endian
|
||||
uint16_t stA = htobe16 (GetSigType ());
|
||||
// stA1 = signature type of blinded A, 2 bytes big endian
|
||||
uint16_t stA1 = htobe16 (GetBlindedSigType ());
|
||||
uint16_t stA1 = htobe16 (GetBlindedSigType ());
|
||||
// credential = H("credential", A || stA || stA1)
|
||||
H ("credential", { {GetPublicKey (), GetPublicKeyLen ()}, {(const uint8_t *)&stA, 2}, {(const uint8_t *)&stA1, 2} }, credential);
|
||||
}
|
||||
|
@ -224,15 +224,15 @@ namespace data
|
|||
{
|
||||
uint16_t stA = htobe16 (GetSigType ()), stA1 = htobe16 (GetBlindedSigType ());
|
||||
uint8_t salt[32];
|
||||
//seed = HKDF(H("I2PGenerateAlpha", keydata), datestring || secret, "i2pblinding1", 64)
|
||||
//seed = HKDF(H("I2PGenerateAlpha", keydata), datestring || secret, "i2pblinding1", 64)
|
||||
H ("I2PGenerateAlpha", { {GetPublicKey (), GetPublicKeyLen ()}, {(const uint8_t *)&stA, 2}, {(const uint8_t *)&stA1, 2} }, salt);
|
||||
i2p::crypto::HKDF (salt, (const uint8_t *)date, 8, "i2pblinding1", seed);
|
||||
}
|
||||
|
||||
size_t BlindedPublicKey::GetBlindedKey (const char * date, uint8_t * blindedKey) const
|
||||
{
|
||||
uint8_t seed[64];
|
||||
GenerateAlpha (date, seed);
|
||||
uint8_t seed[64];
|
||||
GenerateAlpha (date, seed);
|
||||
|
||||
size_t publicKeyLength = 0;
|
||||
switch (m_SigType)
|
||||
|
@ -244,7 +244,7 @@ namespace data
|
|||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519:
|
||||
case i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
|
||||
i2p::crypto::GetEd25519 ()->BlindPublicKey (GetPublicKey (), seed, blindedKey);
|
||||
i2p::crypto::GetEd25519 ()->BlindPublicKey (GetPublicKey (), seed, blindedKey);
|
||||
publicKeyLength = i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH;
|
||||
break;
|
||||
default:
|
||||
|
@ -255,8 +255,8 @@ namespace data
|
|||
|
||||
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);
|
||||
uint8_t seed[64];
|
||||
GenerateAlpha (date, seed);
|
||||
size_t publicKeyLength = 0;
|
||||
switch (m_SigType)
|
||||
{
|
||||
|
@ -272,15 +272,15 @@ namespace data
|
|||
default:
|
||||
LogPrint (eLogError, "Blinding: can't blind signature type ", (int)m_SigType);
|
||||
}
|
||||
return publicKeyLength;
|
||||
return publicKeyLength;
|
||||
}
|
||||
|
||||
void BlindedPublicKey::H (const std::string& p, const std::vector<std::pair<const uint8_t *, size_t> >& bufs, uint8_t * hash) const
|
||||
void BlindedPublicKey::H (const std::string& p, const std::vector<std::pair<const uint8_t *, size_t> >& bufs, uint8_t * hash) const
|
||||
{
|
||||
SHA256_CTX ctx;
|
||||
SHA256_Init (&ctx);
|
||||
SHA256_Update (&ctx, p.c_str (), p.length ());
|
||||
for (const auto& it: bufs)
|
||||
for (const auto& it: bufs)
|
||||
SHA256_Update (&ctx, it.first, it.second);
|
||||
SHA256_Final (hash, &ctx);
|
||||
}
|
||||
|
@ -289,15 +289,15 @@ namespace data
|
|||
{
|
||||
i2p::data::IdentHash hash;
|
||||
uint8_t blinded[128];
|
||||
size_t publicKeyLength = 0;
|
||||
size_t publicKeyLength = 0;
|
||||
if (date)
|
||||
publicKeyLength = GetBlindedKey (date, blinded);
|
||||
else
|
||||
{
|
||||
char currentDate[9];
|
||||
i2p::util::GetCurrentDate (currentDate);
|
||||
publicKeyLength = GetBlindedKey (currentDate, blinded);
|
||||
}
|
||||
publicKeyLength = GetBlindedKey (currentDate, blinded);
|
||||
}
|
||||
if (publicKeyLength)
|
||||
{
|
||||
auto stA1 = htobe16 (m_BlindedSigType);
|
||||
|
@ -308,10 +308,9 @@ namespace data
|
|||
SHA256_Final ((uint8_t *)hash, &ctx);
|
||||
}
|
||||
else
|
||||
LogPrint (eLogError, "Blinding: blinded key type ", (int)m_BlindedSigType, " is not supported");
|
||||
LogPrint (eLogError, "Blinding: blinded key type ", (int)m_BlindedSigType, " is not supported");
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue