Skip to content

Commit 73ba1af

Browse files
committed
don't create EVP_CIPHER_CTX for each AEAD/Chacha20/Poly1305 message
1 parent dcbe6cf commit 73ba1af

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

libi2pd/ECIESX25519AEADRatchetSession.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -725,23 +725,24 @@ namespace garlic
725725

726726
bool ECIESX25519AEADRatchetSession::NewExistingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen)
727727
{
728+
auto owner = GetOwner ();
729+
if (!owner) return false;
728730
uint8_t nonce[12];
729731
auto index = m_SendTagset->GetNextIndex ();
730732
CreateNonce (index, nonce); // tag's index
731733
uint64_t tag = m_SendTagset->GetNextSessionTag ();
732734
if (!tag)
733735
{
734736
LogPrint (eLogError, "Garlic: Can't create new ECIES-X25519-AEAD-Ratchet tag for send tagset");
735-
if (GetOwner ())
736-
GetOwner ()->RemoveECIESx25519Session (m_RemoteStaticKey);
737+
owner->RemoveECIESx25519Session (m_RemoteStaticKey);
737738
return false;
738739
}
739740
memcpy (out, &tag, 8);
740741
// ad = The session tag, 8 bytes
741742
// ciphertext = ENCRYPT(k, n, payload, ad)
742743
uint8_t key[32];
743744
m_SendTagset->GetSymmKey (index, key);
744-
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len, out, 8, key, nonce, out + 8, outLen - 8, true)) // encrypt
745+
if (!owner->AEADChaCha20Poly1305Encrypt (payload, len, out, 8, key, nonce, out + 8, outLen - 8))
745746
{
746747
LogPrint (eLogWarning, "Garlic: Payload section AEAD encryption failed");
747748
return false;
@@ -760,34 +761,35 @@ namespace garlic
760761
uint8_t * payload = buf + 8;
761762
uint8_t key[32];
762763
receiveTagset->GetSymmKey (index, key);
763-
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 16, buf, 8, key, nonce, payload, len - 16, false)) // decrypt
764+
auto owner = GetOwner ();
765+
if (!owner) return true; // drop message
766+
767+
if (!owner->AEADChaCha20Poly1305Decrypt (payload, len - 16, buf, 8, key, nonce, payload, len - 16))
764768
{
765769
LogPrint (eLogWarning, "Garlic: Payload section AEAD decryption failed");
766770
return false;
767771
}
768772
HandlePayload (payload, len - 16, receiveTagset, index);
769-
if (GetOwner ())
773+
774+
int moreTags = 0;
775+
if (owner->GetNumRatchetInboundTags () > 0) // override in settings?
770776
{
771-
int moreTags = 0;
772-
if (GetOwner ()->GetNumRatchetInboundTags () > 0) // override in settings?
773-
{
774-
if (receiveTagset->GetNextIndex () - index < GetOwner ()->GetNumRatchetInboundTags ()/2)
775-
moreTags = GetOwner ()->GetNumRatchetInboundTags ();
776-
index -= GetOwner ()->GetNumRatchetInboundTags (); // trim behind
777-
}
778-
else
779-
{
780-
moreTags = (receiveTagset->GetTagSetID () > 0) ? ECIESX25519_MAX_NUM_GENERATED_TAGS : // for non first tagset
781-
(ECIESX25519_MIN_NUM_GENERATED_TAGS + (index >> 1)); // N/2
782-
if (moreTags > ECIESX25519_MAX_NUM_GENERATED_TAGS) moreTags = ECIESX25519_MAX_NUM_GENERATED_TAGS;
783-
moreTags -= (receiveTagset->GetNextIndex () - index);
784-
index -= ECIESX25519_MAX_NUM_GENERATED_TAGS; // trim behind
785-
}
786-
if (moreTags > 0)
787-
GenerateMoreReceiveTags (receiveTagset, moreTags);
788-
if (index > 0)
789-
receiveTagset->SetTrimBehind (index);
777+
if (receiveTagset->GetNextIndex () - index < owner->GetNumRatchetInboundTags ()/2)
778+
moreTags = owner->GetNumRatchetInboundTags ();
779+
index -= owner->GetNumRatchetInboundTags (); // trim behind
780+
}
781+
else
782+
{
783+
moreTags = (receiveTagset->GetTagSetID () > 0) ? ECIESX25519_MAX_NUM_GENERATED_TAGS : // for non first tagset
784+
(ECIESX25519_MIN_NUM_GENERATED_TAGS + (index >> 1)); // N/2
785+
if (moreTags > ECIESX25519_MAX_NUM_GENERATED_TAGS) moreTags = ECIESX25519_MAX_NUM_GENERATED_TAGS;
786+
moreTags -= (receiveTagset->GetNextIndex () - index);
787+
index -= ECIESX25519_MAX_NUM_GENERATED_TAGS; // trim behind
790788
}
789+
if (moreTags > 0)
790+
GenerateMoreReceiveTags (receiveTagset, moreTags);
791+
if (index > 0)
792+
receiveTagset->SetTrimBehind (index);
791793
return true;
792794
}
793795

libi2pd/Garlic.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,5 +1103,17 @@ namespace garlic
11031103
m_PayloadBuffer = new uint8_t[I2NP_MAX_MESSAGE_SIZE];
11041104
return m_PayloadBuffer;
11051105
}
1106+
1107+
bool GarlicDestination::AEADChaCha20Poly1305Encrypt (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen,
1108+
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len)
1109+
{
1110+
return m_Encryptor.Encrypt (msg, msgLen, ad, adLen, key, nonce, buf, len);
1111+
}
1112+
1113+
bool GarlicDestination::AEADChaCha20Poly1305Decrypt (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen,
1114+
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len)
1115+
{
1116+
return m_Decryptor.Decrypt (msg, msgLen, ad, adLen, key, nonce, buf, len);
1117+
}
11061118
}
11071119
}

libi2pd/Garlic.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ namespace garlic
242242
void RemoveDeliveryStatusSession (uint32_t msgID);
243243
std::shared_ptr<I2NPMessage> WrapMessageForRouter (std::shared_ptr<const i2p::data::RouterInfo> router,
244244
std::shared_ptr<I2NPMessage> msg);
245+
246+
bool AEADChaCha20Poly1305Encrypt (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen,
247+
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len);
248+
bool AEADChaCha20Poly1305Decrypt (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen,
249+
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len);
245250

246251
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
247252
void AddECIESx25519Key (const uint8_t * key, uint64_t tag); // one tag
@@ -295,7 +300,10 @@ namespace garlic
295300
// DeliveryStatus
296301
std::mutex m_DeliveryStatusSessionsMutex;
297302
std::unordered_map<uint32_t, GarlicRoutingSessionPtr> m_DeliveryStatusSessions; // msgID -> session
298-
303+
// encryption
304+
i2p::crypto::AEADChaCha20Poly1305Encryptor m_Encryptor;
305+
i2p::crypto::AEADChaCha20Poly1305Decryptor m_Decryptor;
306+
299307
public:
300308

301309
// for HTTP only

0 commit comments

Comments
 (0)