diff --git a/src/olm/messages/message.rs b/src/olm/messages/message.rs index 14063bd4..87074104 100644 --- a/src/olm/messages/message.rs +++ b/src/olm/messages/message.rs @@ -266,8 +266,11 @@ impl Debug for Message { pub struct InterolmMessage { pub(crate) version: u8, pub(crate) ratchet_key: Curve25519PublicKey, - pub(crate) counter: u32, - pub(crate) previous_counter: u32, + /// The current chain index + pub(crate) index: u32, + /// The chain index at the point in time when the DH ratchet was previously + /// ratcheted + pub(crate) previous_index: u32, pub(crate) ciphertext: Vec, pub(crate) mac: InterolmMessageMac, } @@ -278,15 +281,15 @@ impl InterolmMessage { pub(crate) fn new( ratchet_key: Curve25519PublicKey, - counter: u32, - previous_counter: u32, + index: u32, + previous_index: u32, ciphertext: Vec, ) -> Self { Self { version: Self::VERSION, ratchet_key, - counter, - previous_counter, + index, + previous_index, ciphertext, mac: InterolmMessageMac([0u8; Mac::TRUNCATED_LEN]), } @@ -313,18 +316,11 @@ impl InterolmMessage { } else { let mac = InterolmMessageMac(mac_slice.try_into().expect("Can never happen")); let ratchet_key = Curve25519PublicKey::from_slice(&decoded.ratchet_key)?; - let counter = decoded.counter; - let previous_counter = decoded.previous_counter; + let index = decoded.index; + let previous_index = decoded.previous_index; let ciphertext = decoded.ciphertext; - Ok(InterolmMessage { - version, - ratchet_key, - counter, - previous_counter, - ciphertext, - mac, - }) + Ok(InterolmMessage { version, ratchet_key, index, previous_index, ciphertext, mac }) } } } @@ -348,8 +344,8 @@ impl InterolmMessage { pub fn to_mac_bytes(&self) -> Vec { InterolmProtoBufMessage { ratchet_key: self.ratchet_key.to_interolm_bytes().to_vec(), - counter: self.counter, - previous_counter: self.previous_counter, + index: self.index, + previous_index: self.previous_index, ciphertext: self.ciphertext.clone(), } .encode_manual() @@ -368,13 +364,13 @@ impl<'a> From<&'a InterolmMessage> for AnyNormalMessage<'a> { impl Debug for InterolmMessage { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let Self { version, ratchet_key, counter, previous_counter, ciphertext: _, mac: _ } = self; + let Self { version, ratchet_key, index, previous_index, ciphertext: _, mac: _ } = self; f.debug_struct("InterolmMessage") .field("version", version) .field("ratchet_key", ratchet_key) - .field("counter", counter) - .field("previous_counter", previous_counter) + .field("index", index) + .field("previous_index", previous_index) .finish() } } @@ -395,9 +391,9 @@ struct InterolmProtoBufMessage { #[prost(bytes, tag = "1")] ratchet_key: Vec, #[prost(uint32, tag = "2")] - counter: u32, + index: u32, #[prost(uint32, tag = "3")] - previous_counter: u32, + previous_index: u32, #[prost(bytes, tag = "4")] ciphertext: Vec, } @@ -410,8 +406,8 @@ impl InterolmProtoBufMessage { const CIPHER_TAG: &'static [u8; 1] = b"\x22"; fn encode_manual(&self) -> Vec { - let counter = self.counter.to_var_int(); - let previous_counter = self.previous_counter.to_var_int(); + let index = self.index.to_var_int(); + let previous_index = self.previous_index.to_var_int(); let ratchet_len = self.ratchet_key.len().to_var_int(); let ciphertext_len = self.ciphertext.len().to_var_int(); @@ -421,9 +417,9 @@ impl InterolmProtoBufMessage { &ratchet_len, &self.ratchet_key, Self::INDEX_TAG.as_ref(), - &counter, + &index, Self::PREVIOUS_INDEX_TAG.as_ref(), - &previous_counter, + &previous_index, Self::CIPHER_TAG.as_ref(), &ciphertext_len, &self.ciphertext, diff --git a/src/olm/messages/mod.rs b/src/olm/messages/mod.rs index 1870c898..646f3205 100644 --- a/src/olm/messages/mod.rs +++ b/src/olm/messages/mod.rs @@ -67,7 +67,7 @@ impl AnyNormalMessage<'_> { pub(crate) fn chain_index(&self) -> u64 { match self { AnyNormalMessage::Native(m) => m.chain_index, - AnyNormalMessage::Interolm(m) => m.counter.into(), + AnyNormalMessage::Interolm(m) => m.index.into(), } } } diff --git a/src/olm/session/double_ratchet.rs b/src/olm/session/double_ratchet.rs index d48b5ae7..89d9169a 100644 --- a/src/olm/session/double_ratchet.rs +++ b/src/olm/session/double_ratchet.rs @@ -70,13 +70,13 @@ impl DoubleRatchet { config: &SessionConfig, session_creator: SessionCreator, session_keys: &SessionKeys, - previous_counter: u32, + previous_index: u32, plaintext: &[u8], ) -> InterolmMessage { self.next_message_key(config).encrypt_interolm( session_keys, session_creator, - previous_counter, + previous_index, plaintext, ) } diff --git a/src/olm/session/message_key.rs b/src/olm/session/message_key.rs index 7d940c17..cc0d6be4 100644 --- a/src/olm/session/message_key.rs +++ b/src/olm/session/message_key.rs @@ -92,7 +92,7 @@ impl MessageKey { self, session_keys: &SessionKeys, session_creator: SessionCreator, - previous_counter: u32, + previous_index: u32, plaintext: &[u8], ) -> InterolmMessage { let cipher = Cipher::new_interolm(&self.key); @@ -102,7 +102,7 @@ impl MessageKey { let mut message = InterolmMessage::new( *self.ratchet_key.as_ref(), self.index.try_into().expect("Interolm doesn't support encrypting more than 2^32 messages with a single sender chain"), - previous_counter, + previous_index, ciphertext, ); diff --git a/src/olm/session/mod.rs b/src/olm/session/mod.rs index 223ff0db..101dafc4 100644 --- a/src/olm/session/mod.rs +++ b/src/olm/session/mod.rs @@ -124,13 +124,13 @@ impl ChainStore { } #[cfg(feature = "interolm")] - fn previous_counter(&self) -> u32 { + fn previous_index(&self) -> u32 { match self.previous_chain() { Some(chain) => { if chain.hkdf_ratchet.chain_index() > 0 { (chain.hkdf_ratchet.chain_index() - 1) .try_into() - .expect("Interolm counter should fit into u32") + .expect("Interolm chain index should fit into u32") } else { 0 } @@ -388,7 +388,7 @@ impl Session { &self.config, self.session_creator, &self.session_keys, - self.receiving_chains.previous_counter(), + self.receiving_chains.previous_index(), plaintext.as_ref(), ), ),