|
|
@ -770,7 +770,7 @@ namespace garlic |
|
|
|
if (m_NextSendRatchet->newKey) payloadLen += 32; |
|
|
|
if (m_NextSendRatchet->newKey) payloadLen += 32; |
|
|
|
} |
|
|
|
} |
|
|
|
uint8_t paddingSize = 0; |
|
|
|
uint8_t paddingSize = 0; |
|
|
|
if (payloadLen) |
|
|
|
if (payloadLen || ts > m_LastSentTimestamp + ECIESX25519_SEND_INACTIVITY_TIMEOUT) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int delta = (int)ECIESX25519_OPTIMAL_PAYLOAD_SIZE - (int)payloadLen; |
|
|
|
int delta = (int)ECIESX25519_OPTIMAL_PAYLOAD_SIZE - (int)payloadLen; |
|
|
|
if (delta < 0 || delta > 3) // don't create padding if we are close to optimal size
|
|
|
|
if (delta < 0 || delta > 3) // don't create padding if we are close to optimal size
|
|
|
@ -791,83 +791,87 @@ namespace garlic |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
std::vector<uint8_t> v(payloadLen); |
|
|
|
std::vector<uint8_t> v(payloadLen); |
|
|
|
size_t offset = 0; |
|
|
|
if (payloadLen) |
|
|
|
// DateTime
|
|
|
|
{ |
|
|
|
if (first) |
|
|
|
m_LastSentTimestamp = ts; |
|
|
|
{ |
|
|
|
size_t offset = 0; |
|
|
|
v[offset] = eECIESx25519BlkDateTime; offset++; |
|
|
|
// DateTime
|
|
|
|
htobe16buf (v.data () + offset, 4); offset += 2; |
|
|
|
if (first) |
|
|
|
htobe32buf (v.data () + offset, ts/1000); offset += 4; // in seconds
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// LeaseSet
|
|
|
|
|
|
|
|
if (leaseSet) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
offset += CreateLeaseSetClove (leaseSet, ts, v.data () + offset, payloadLen - offset); |
|
|
|
|
|
|
|
if (!first) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
// ack request
|
|
|
|
v[offset] = eECIESx25519BlkDateTime; offset++; |
|
|
|
v[offset] = eECIESx25519BlkAckRequest; offset++; |
|
|
|
htobe16buf (v.data () + offset, 4); offset += 2; |
|
|
|
htobe16buf (v.data () + offset, 1); offset += 2; |
|
|
|
htobe32buf (v.data () + offset, ts/1000); offset += 4; // in seconds
|
|
|
|
v[offset] = 0; offset++; // flags
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// LeaseSet
|
|
|
|
// msg
|
|
|
|
if (leaseSet) |
|
|
|
if (msg && m_Destination) |
|
|
|
|
|
|
|
offset += CreateGarlicClove (msg, v.data () + offset, payloadLen - offset, true); |
|
|
|
|
|
|
|
// ack
|
|
|
|
|
|
|
|
if (m_AckRequests.size () > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
v[offset] = eECIESx25519BlkAck; offset++; |
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, m_AckRequests.size () * 4); offset += 2; |
|
|
|
|
|
|
|
for (auto& it: m_AckRequests) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
htobe16buf (v.data () + offset, it.first); offset += 2; |
|
|
|
offset += CreateLeaseSetClove (leaseSet, ts, v.data () + offset, payloadLen - offset); |
|
|
|
htobe16buf (v.data () + offset, it.second); offset += 2; |
|
|
|
if (!first) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// ack request
|
|
|
|
|
|
|
|
v[offset] = eECIESx25519BlkAckRequest; offset++; |
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, 1); offset += 2; |
|
|
|
|
|
|
|
v[offset] = 0; offset++; // flags
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
m_AckRequests.clear (); |
|
|
|
// msg
|
|
|
|
} |
|
|
|
if (msg && m_Destination) |
|
|
|
// next keys
|
|
|
|
offset += CreateGarlicClove (msg, v.data () + offset, payloadLen - offset, true); |
|
|
|
if (m_SendReverseKey) |
|
|
|
// ack
|
|
|
|
{ |
|
|
|
if (m_AckRequests.size () > 0) |
|
|
|
v[offset] = eECIESx25519BlkNextKey; offset++; |
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, m_NextReceiveRatchet->newKey ? 35 : 3); offset += 2; |
|
|
|
|
|
|
|
v[offset] = ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG; |
|
|
|
|
|
|
|
int keyID = m_NextReceiveRatchet->keyID - 1; |
|
|
|
|
|
|
|
if (m_NextReceiveRatchet->newKey) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
v[offset] |= ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG; |
|
|
|
v[offset] = eECIESx25519BlkAck; offset++; |
|
|
|
keyID++; |
|
|
|
htobe16buf (v.data () + offset, m_AckRequests.size () * 4); offset += 2; |
|
|
|
|
|
|
|
for (auto& it: m_AckRequests) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, it.first); offset += 2; |
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, it.second); offset += 2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_AckRequests.clear (); |
|
|
|
} |
|
|
|
} |
|
|
|
offset++; // flag
|
|
|
|
// next keys
|
|
|
|
htobe16buf (v.data () + offset, keyID); offset += 2; // keyid
|
|
|
|
if (m_SendReverseKey) |
|
|
|
if (m_NextReceiveRatchet->newKey) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
memcpy (v.data () + offset, m_NextReceiveRatchet->key->GetPublicKey (), 32); |
|
|
|
v[offset] = eECIESx25519BlkNextKey; offset++; |
|
|
|
offset += 32; // public key
|
|
|
|
htobe16buf (v.data () + offset, m_NextReceiveRatchet->newKey ? 35 : 3); offset += 2; |
|
|
|
|
|
|
|
v[offset] = ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG; |
|
|
|
|
|
|
|
int keyID = m_NextReceiveRatchet->keyID - 1; |
|
|
|
|
|
|
|
if (m_NextReceiveRatchet->newKey) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
v[offset] |= ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG; |
|
|
|
|
|
|
|
keyID++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
offset++; // flag
|
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, keyID); offset += 2; // keyid
|
|
|
|
|
|
|
|
if (m_NextReceiveRatchet->newKey) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
memcpy (v.data () + offset, m_NextReceiveRatchet->key->GetPublicKey (), 32); |
|
|
|
|
|
|
|
offset += 32; // public key
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_SendReverseKey = false; |
|
|
|
} |
|
|
|
} |
|
|
|
m_SendReverseKey = false; |
|
|
|
if (m_SendForwardKey) |
|
|
|
} |
|
|
|
|
|
|
|
if (m_SendForwardKey) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
v[offset] = eECIESx25519BlkNextKey; offset++; |
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, m_NextSendRatchet->newKey ? 35 : 3); offset += 2; |
|
|
|
|
|
|
|
v[offset] = m_NextSendRatchet->newKey ? ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG : ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG; |
|
|
|
|
|
|
|
if (!m_NextSendRatchet->keyID) v[offset] |= ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG; // for first key only
|
|
|
|
|
|
|
|
offset++; // flag
|
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, m_NextSendRatchet->keyID); offset += 2; // keyid
|
|
|
|
|
|
|
|
if (m_NextSendRatchet->newKey) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
memcpy (v.data () + offset, m_NextSendRatchet->key->GetPublicKey (), 32); |
|
|
|
v[offset] = eECIESx25519BlkNextKey; offset++; |
|
|
|
offset += 32; // public key
|
|
|
|
htobe16buf (v.data () + offset, m_NextSendRatchet->newKey ? 35 : 3); offset += 2; |
|
|
|
|
|
|
|
v[offset] = m_NextSendRatchet->newKey ? ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG : ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG; |
|
|
|
|
|
|
|
if (!m_NextSendRatchet->keyID) v[offset] |= ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG; // for first key only
|
|
|
|
|
|
|
|
offset++; // flag
|
|
|
|
|
|
|
|
htobe16buf (v.data () + offset, m_NextSendRatchet->keyID); offset += 2; // keyid
|
|
|
|
|
|
|
|
if (m_NextSendRatchet->newKey) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
memcpy (v.data () + offset, m_NextSendRatchet->key->GetPublicKey (), 32); |
|
|
|
|
|
|
|
offset += 32; // public key
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// padding
|
|
|
|
// padding
|
|
|
|
if (paddingSize) |
|
|
|
if (paddingSize) |
|
|
|
{ |
|
|
|
{ |
|
|
|
v[offset] = eECIESx25519BlkPadding; offset++; |
|
|
|
v[offset] = eECIESx25519BlkPadding; offset++; |
|
|
|
htobe16buf (v.data () + offset, paddingSize); offset += 2; |
|
|
|
htobe16buf (v.data () + offset, paddingSize); offset += 2; |
|
|
|
memset (v.data () + offset, 0, paddingSize); offset += paddingSize; |
|
|
|
memset (v.data () + offset, 0, paddingSize); offset += paddingSize; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return v; |
|
|
|
return v; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|