diff --git a/matrix_sdk_crypto/src/verification/sas/helpers.rs b/matrix_sdk_crypto/src/verification/sas/helpers.rs index b4db2572..e505eec1 100644 --- a/matrix_sdk_crypto/src/verification/sas/helpers.rs +++ b/matrix_sdk_crypto/src/verification/sas/helpers.rs @@ -436,6 +436,43 @@ pub fn get_emoji( bytes_to_emoji(bytes) } +/// Get the index of the emoji of the short authentication string. +/// +/// Returns seven u8 numbers in the range from 0 to 63 inclusive, those numbers +/// can be converted to a unique emoji defined by the spec using the +/// [emoji_from_index](#method.emoji_from_index) method. +/// +/// # Arguments +/// +/// * `sas` - The Olm SAS object that can be used to generate bytes using the +/// shared secret. +/// +/// * `ids` - The ids that are used for this SAS authentication flow. +/// +/// * `flow_id` - The unique id that identifies this SAS verification process. +/// +/// * `we_started` - Flag signaling if the SAS process was started on our side. +/// +/// # Panics +/// +/// This will panic if the public key of the other side wasn't set. +pub fn get_emoji_index( + sas: &OlmSas, + ids: &SasIds, + their_pubkey: &str, + flow_id: &str, + we_started: bool, +) -> [u8; 7] { + let bytes = sas + .generate_bytes( + &extra_info_sas(&ids, &sas.public_key(), their_pubkey, &flow_id, we_started), + 6, + ) + .expect("Can't generate bytes"); + + bytes_to_emoji_index(bytes) +} + fn bytes_to_emoji_index(bytes: Vec) -> [u8; 7] { let bytes: Vec = bytes.iter().map(|b| *b as u64).collect(); // Join the 6 bytes into one 64 bit unsigned int. This u64 will contain 48 diff --git a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs index 03026e8f..89080b5b 100644 --- a/matrix_sdk_crypto/src/verification/sas/inner_sas.rs +++ b/matrix_sdk_crypto/src/verification/sas/inner_sas.rs @@ -351,6 +351,14 @@ impl InnerSas { } } + pub fn emoji_index(&self) -> Option<[u8; 7]> { + match self { + InnerSas::KeyRecieved(s) => Some(s.get_emoji_index()), + InnerSas::MacReceived(s) => Some(s.get_emoji_index()), + _ => None, + } + } + pub fn decimals(&self) -> Option<(u16, u16, u16)> { match self { InnerSas::KeyRecieved(s) => Some(s.get_decimal()), diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index 2fddb8f6..6a7c9332 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -633,6 +633,16 @@ impl Sas { self.inner.lock().unwrap().emoji() } + /// Get the index of the emoji representing the short auth string + /// + /// Returns None if we can't yet present the short auth string, otherwise + /// seven u8 numbers in the range from 0 to 63 inclusive which can be + /// converted to an emoji using the + /// [relevant spec entry](https://spec.matrix.org/unstable/client-server-api/#sas-method-emoji). + pub fn emoji_index(&self) -> Option<[u8; 7]> { + self.inner.lock().unwrap().emoji_index() + } + /// Get the decimal version of the short auth string. /// /// Returns None if we can't yet present the short auth string, otherwise a diff --git a/matrix_sdk_crypto/src/verification/sas/sas_state.rs b/matrix_sdk_crypto/src/verification/sas/sas_state.rs index 47cc9c3d..96bf25f9 100644 --- a/matrix_sdk_crypto/src/verification/sas/sas_state.rs +++ b/matrix_sdk_crypto/src/verification/sas/sas_state.rs @@ -47,7 +47,8 @@ use super::{ AcceptContent, CancelContent, DoneContent, KeyContent, MacContent, StartContent, }, helpers::{ - calculate_commitment, get_decimal, get_emoji, get_mac_content, receive_mac_event, SasIds, + calculate_commitment, get_decimal, get_emoji, get_emoji_index, get_mac_content, + receive_mac_event, SasIds, }, }; @@ -757,6 +758,20 @@ impl SasState { ) } + /// Get the index of the emoji of the short authentication string. + /// + /// Returns seven u8 numbers in the range from 0 to 63 inclusive, those numbers + /// can be converted to a unique emoji defined by the spec. + pub fn get_emoji_index(&self) -> [u8; 7] { + get_emoji_index( + &self.inner.lock().unwrap(), + &self.ids, + &self.state.their_pubkey, + self.verification_flow_id.as_str(), + self.state.we_started, + ) + } + /// Get the decimal version of the short authentication string. /// /// Returns a tuple containing three 4 digit integer numbers that represent @@ -978,6 +993,20 @@ impl SasState { ) } + /// Get the index of the emoji of the short authentication string. + /// + /// Returns seven u8 numbers in the range from 0 to 63 inclusive, those numbers + /// can be converted to a unique emoji defined by the spec. + pub fn get_emoji_index(&self) -> [u8; 7] { + get_emoji_index( + &self.inner.lock().unwrap(), + &self.ids, + &self.state.their_pubkey, + self.verification_flow_id.as_str(), + self.state.we_started, + ) + } + /// Get the decimal version of the short authentication string. /// /// Returns a tuple containing three 4 digit integer numbers that represent