From 2729f01e0f330e9032374355dd74e96449ade893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 23 Jul 2020 14:26:50 +0200 Subject: [PATCH] crypto: Move the emoji/decimal sas calculation out of the Sas object. --- matrix_sdk_crypto/src/verification/mod.rs | 74 +++++++++++++++++++++- matrix_sdk_crypto/src/verification/sas.rs | 77 ++++------------------- 2 files changed, 86 insertions(+), 65 deletions(-) diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index 7b187ee5..45d7b935 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -31,7 +31,7 @@ struct SasIds { other_device: Device, } -fn get_emoji(index: u8) -> (&'static str, &'static str) { +fn emoji_from_index(index: u8) -> (&'static str, &'static str) { match index { 0 => ("🐶", "Dog"), 1 => ("🐱", "Cat"), @@ -203,3 +203,75 @@ fn get_mac_content(sas: &OlmSas, ids: &SasIds, flow_id: &str) -> MacEventContent mac, } } + +fn extra_info_sas(ids: &SasIds, flow_id: &str, we_started: bool) -> String { + if we_started { + format!( + "MATRIX_KEY_VERIFICATION_SAS{first_user}{first_device}\ + {second_user}{second_device}{transaction_id}", + first_user = ids.account.user_id(), + first_device = ids.account.device_id(), + second_user = ids.other_device.user_id(), + second_device = ids.other_device.device_id(), + transaction_id = flow_id, + ) + } else { + format!( + "MATRIX_KEY_VERIFICATION_SAS{first_user}{first_device}\ + {second_user}{second_device}{transaction_id}", + first_user = ids.other_device.user_id(), + first_device = ids.other_device.device_id(), + second_user = ids.account.user_id(), + second_device = ids.account.device_id(), + transaction_id = flow_id, + ) + } +} + +fn get_emoji( + sas: &OlmSas, + ids: &SasIds, + flow_id: &str, + we_started: bool, +) -> Vec<(&'static str, &'static str)> { + let bytes: Vec = sas + .generate_bytes(&extra_info_sas(&ids, &flow_id, we_started), 6) + .expect("Can't generate bytes") + .into_iter() + .map(|b| b as u64) + .collect(); + + let mut num: u64 = bytes[0] << 40; + num += bytes[1] << 32; + num += bytes[2] << 24; + num += bytes[3] << 16; + num += bytes[4] << 8; + num += bytes[5]; + + let numbers = vec![ + ((num >> 42) & 63) as u8, + ((num >> 36) & 63) as u8, + ((num >> 30) & 63) as u8, + ((num >> 24) & 63) as u8, + ((num >> 18) & 63) as u8, + ((num >> 12) & 63) as u8, + ((num >> 6) & 63) as u8, + ]; + + numbers.into_iter().map(emoji_from_index).collect() +} + +fn get_decimal(sas: &OlmSas, ids: &SasIds, flow_id: &str, we_started: bool) -> (u32, u32, u32) { + let bytes: Vec = sas + .generate_bytes(&extra_info_sas(&ids, &flow_id, we_started), 5) + .expect("Can't generate bytes") + .into_iter() + .map(|b| b as u32) + .collect(); + + let first = bytes[0] << 5 | bytes[1] >> 3; + let second = (bytes[1] & 0x7) << 10 | bytes[2] << 2 | bytes[3] >> 6; + let third = (bytes[3] & 0x3F) << 7 | bytes[4] >> 1; + + (first + 1000, second + 1000, third + 1000) +} diff --git a/matrix_sdk_crypto/src/verification/sas.rs b/matrix_sdk_crypto/src/verification/sas.rs index 1535889f..8162ffc6 100644 --- a/matrix_sdk_crypto/src/verification/sas.rs +++ b/matrix_sdk_crypto/src/verification/sas.rs @@ -30,7 +30,7 @@ use matrix_sdk_common::events::{ use matrix_sdk_common::identifiers::{DeviceId, UserId}; use matrix_sdk_common::uuid::Uuid; -use super::{get_emoji, get_mac_content, receive_mac_event, SasIds}; +use super::{get_decimal, get_emoji, get_mac_content, receive_mac_event, SasIds}; use crate::{Account, Device}; struct AcceptedProtocols { @@ -234,73 +234,22 @@ impl Sas { } } - fn extra_info(&self) -> String { - if self.state.we_started { - format!( - "MATRIX_KEY_VERIFICATION_SAS{first_user}{first_device}\ - {second_user}{second_device}{transaction_id}", - first_user = self.ids.account.user_id(), - first_device = self.ids.account.device_id(), - second_user = self.ids.other_device.user_id(), - second_device = self.ids.other_device.device_id(), - transaction_id = self.verification_flow_id, - ) - } else { - format!( - "MATRIX_KEY_VERIFICATION_SAS{first_user}{first_device}\ - {second_user}{second_device}{transaction_id}", - first_user = self.ids.other_device.user_id(), - first_device = self.ids.other_device.device_id(), - second_user = self.ids.account.user_id(), - second_device = self.ids.account.device_id(), - transaction_id = self.verification_flow_id, - ) - } - } - fn get_emoji(&self) -> Vec<(&'static str, &'static str)> { - let bytes: Vec = self - .inner - .generate_bytes(&self.extra_info(), 6) - .expect("Can't generate bytes") - .into_iter() - .map(|b| b as u64) - .collect(); - - let mut num: u64 = bytes[0] << 40; - num += bytes[1] << 32; - num += bytes[2] << 24; - num += bytes[3] << 16; - num += bytes[4] << 8; - num += bytes[5]; - - let numbers = vec![ - ((num >> 42) & 63) as u8, - ((num >> 36) & 63) as u8, - ((num >> 30) & 63) as u8, - ((num >> 24) & 63) as u8, - ((num >> 18) & 63) as u8, - ((num >> 12) & 63) as u8, - ((num >> 6) & 63) as u8, - ]; - - numbers.into_iter().map(get_emoji).collect() + get_emoji( + &self.inner, + &self.ids, + &self.verification_flow_id, + self.state.we_started, + ) } fn get_decimal(&self) -> (u32, u32, u32) { - let bytes: Vec = self - .inner - .generate_bytes(&self.extra_info(), 5) - .expect("Can't generate bytes") - .into_iter() - .map(|b| b as u32) - .collect(); - - let first = bytes[0] << 5 | bytes[1] >> 3; - let second = (bytes[1] & 0x7) << 10 | bytes[2] << 2 | bytes[3] >> 6; - let third = (bytes[3] & 0x3F) << 7 | bytes[4] >> 1; - - (first + 1000, second + 1000, third + 1000) + get_decimal( + &self.inner, + &self.ids, + &self.verification_flow_id, + self.state.we_started, + ) } fn into_mac_received(self, event: &ToDeviceEvent) -> Sas {