|
|
|
@ -875,10 +875,21 @@ namespace i2p
@@ -875,10 +875,21 @@ namespace i2p
|
|
|
|
|
|
|
|
|
|
bool RouterContext::DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data) |
|
|
|
|
{ |
|
|
|
|
if (!m_TunnelDecryptor) return false; |
|
|
|
|
if (IsECIES ()) |
|
|
|
|
return DecryptECIESTunnelBuildRecord (encrypted, data, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if (!m_InitialNoiseState) return false; |
|
|
|
|
if (!m_TunnelDecryptor) return false; |
|
|
|
|
BN_CTX * ctx = BN_CTX_new (); |
|
|
|
|
bool success = m_TunnelDecryptor->Decrypt (encrypted, data, ctx, false); |
|
|
|
|
BN_CTX_free (ctx); |
|
|
|
|
return success; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool RouterContext::DecryptECIESTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, size_t clearTextSize) |
|
|
|
|
{ |
|
|
|
|
if (!m_InitialNoiseState || !m_TunnelDecryptor) return false; |
|
|
|
|
// m_InitialNoiseState is h = SHA256(h || hepk)
|
|
|
|
|
m_CurrentNoiseState.reset (new i2p::crypto::NoiseSymmetricState (*m_InitialNoiseState)); |
|
|
|
|
m_CurrentNoiseState->MixHash (encrypted, 32); // h = SHA256(h || sepk)
|
|
|
|
@ -892,21 +903,24 @@ namespace i2p
@@ -892,21 +903,24 @@ namespace i2p
|
|
|
|
|
encrypted += 32; |
|
|
|
|
uint8_t nonce[12]; |
|
|
|
|
memset (nonce, 0, 12); |
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (encrypted, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE, |
|
|
|
|
m_CurrentNoiseState->m_H, 32, m_CurrentNoiseState->m_CK + 32, nonce, data, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE, false)) // decrypt
|
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (encrypted, clearTextSize, m_CurrentNoiseState->m_H, 32, |
|
|
|
|
m_CurrentNoiseState->m_CK + 32, nonce, data, clearTextSize, false)) // decrypt
|
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogWarning, "Router: Tunnel record AEAD decryption failed"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
m_CurrentNoiseState->MixHash (encrypted, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE + 16); // h = SHA256(h || ciphertext)
|
|
|
|
|
m_CurrentNoiseState->MixHash (encrypted, clearTextSize + 16); // h = SHA256(h || ciphertext)
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool RouterContext::DecryptTunnelShortRequestRecord (const uint8_t * encrypted, uint8_t * data) |
|
|
|
|
{ |
|
|
|
|
if (IsECIES ()) |
|
|
|
|
return DecryptECIESTunnelBuildRecord (encrypted, data, SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
BN_CTX * ctx = BN_CTX_new (); |
|
|
|
|
bool success = m_TunnelDecryptor->Decrypt (encrypted, data, ctx, false); |
|
|
|
|
BN_CTX_free (ctx); |
|
|
|
|
return success; |
|
|
|
|
LogPrint (eLogWarning, "Router: Can't decrypt short request record on non-ECIES router"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|