Skip to content

Commit

Permalink
Replace references to "counter" with "index".
Browse files Browse the repository at this point in the history
There is still a bit of a mix between the terms left in Megolm, but
those should be cleaned up in a separate PR.
  • Loading branch information
dkasak committed Feb 9, 2024
1 parent ab2dd81 commit bc32d46
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
50 changes: 23 additions & 27 deletions src/olm/messages/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
pub(crate) mac: InterolmMessageMac,
}
Expand All @@ -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<u8>,
) -> Self {
Self {
version: Self::VERSION,
ratchet_key,
counter,
previous_counter,
index,
previous_index,
ciphertext,
mac: InterolmMessageMac([0u8; Mac::TRUNCATED_LEN]),
}
Expand All @@ -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 })
}
}
}
Expand All @@ -348,8 +344,8 @@ impl InterolmMessage {
pub fn to_mac_bytes(&self) -> Vec<u8> {
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()
Expand All @@ -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()
}
}
Expand All @@ -395,9 +391,9 @@ struct InterolmProtoBufMessage {
#[prost(bytes, tag = "1")]
ratchet_key: Vec<u8>,
#[prost(uint32, tag = "2")]
counter: u32,
index: u32,
#[prost(uint32, tag = "3")]
previous_counter: u32,
previous_index: u32,
#[prost(bytes, tag = "4")]
ciphertext: Vec<u8>,
}
Expand All @@ -410,8 +406,8 @@ impl InterolmProtoBufMessage {
const CIPHER_TAG: &'static [u8; 1] = b"\x22";

fn encode_manual(&self) -> Vec<u8> {
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();

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/olm/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/olm/session/double_ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/olm/session/message_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
);

Expand Down
6 changes: 3 additions & 3 deletions src/olm/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(),
),
),
Expand Down

0 comments on commit bc32d46

Please sign in to comment.