From 51f3d90224677ceeeb333603f7bb78f9eab12849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 14 Sep 2020 17:06:36 +0200 Subject: [PATCH] crypto: Move the file encryption modules under a submodule. --- .../src/{ => file_encryption}/attachments.rs | 20 +++------------- .../src/{ => file_encryption}/key_export.rs | 10 +------- matrix_sdk_crypto/src/file_encryption/mod.rs | 23 +++++++++++++++++++ matrix_sdk_crypto/src/lib.rs | 6 ++--- 4 files changed, 29 insertions(+), 30 deletions(-) rename matrix_sdk_crypto/src/{ => file_encryption}/attachments.rs (93%) rename matrix_sdk_crypto/src/{ => file_encryption}/key_export.rs (97%) create mode 100644 matrix_sdk_crypto/src/file_encryption/mod.rs diff --git a/matrix_sdk_crypto/src/attachments.rs b/matrix_sdk_crypto/src/file_encryption/attachments.rs similarity index 93% rename from matrix_sdk_crypto/src/attachments.rs rename to matrix_sdk_crypto/src/file_encryption/attachments.rs index c97d30d4..69606254 100644 --- a/matrix_sdk_crypto/src/attachments.rs +++ b/matrix_sdk_crypto/src/file_encryption/attachments.rs @@ -21,7 +21,6 @@ use serde::{Deserialize, Serialize}; use matrix_sdk_common::events::room::JsonWebKey; -use base64::{decode_config, encode_config, DecodeError, STANDARD_NO_PAD, URL_SAFE_NO_PAD}; use getrandom::getrandom; use aes_ctr::{ @@ -30,26 +29,12 @@ use aes_ctr::{ }; use sha2::{Digest, Sha256}; +use super::{decode, decode_url_safe, encode, encode_url_safe}; + const IV_SIZE: usize = 16; const KEY_SIZE: usize = 32; const VERSION: u8 = 1; -fn decode(input: impl AsRef<[u8]>) -> Result, DecodeError> { - decode_config(input, STANDARD_NO_PAD) -} - -fn decode_url_safe(input: impl AsRef<[u8]>) -> Result, DecodeError> { - decode_config(input, URL_SAFE_NO_PAD) -} - -fn encode(input: impl AsRef<[u8]>) -> String { - encode_config(input, STANDARD_NO_PAD) -} - -fn encode_url_safe(input: impl AsRef<[u8]>) -> String { - encode_config(input, URL_SAFE_NO_PAD) -} - pub struct AttachmentDecryptor<'a, R: 'a + Read> { inner_reader: &'a mut R, expected_hash: Vec, @@ -78,6 +63,7 @@ impl<'a, R: Read> Read for AttachmentDecryptor<'a, R> { impl<'a, R: Read + 'a> AttachmentDecryptor<'a, R> { fn new(input: &'a mut R, info: EncryptionInfo) -> AttachmentDecryptor<'a, R> { + // TODO check the version let hash = decode(info.hashes.get("sha256").unwrap()).unwrap(); // TODO Use zeroizing here. let key = decode_url_safe(info.web_key.k).unwrap(); diff --git a/matrix_sdk_crypto/src/key_export.rs b/matrix_sdk_crypto/src/file_encryption/key_export.rs similarity index 97% rename from matrix_sdk_crypto/src/key_export.rs rename to matrix_sdk_crypto/src/file_encryption/key_export.rs index 5d3e6436..08fd1b48 100644 --- a/matrix_sdk_crypto/src/key_export.rs +++ b/matrix_sdk_crypto/src/file_encryption/key_export.rs @@ -16,7 +16,6 @@ use serde_json::Error as SerdeError; use std::io::{Cursor, Read, Seek, SeekFrom}; use thiserror::Error; -use base64::{decode_config, encode_config, DecodeError, STANDARD_NO_PAD}; use byteorder::{BigEndian, ReadBytesExt}; use getrandom::getrandom; @@ -28,6 +27,7 @@ use hmac::{Hmac, Mac, NewMac}; use pbkdf2::pbkdf2; use sha2::{Sha256, Sha512}; +use super::{decode, encode, DecodeError}; use crate::olm::ExportedRoomKey; const SALT_SIZE: usize = 16; @@ -39,14 +39,6 @@ const VERSION: u8 = 1; const HEADER: &str = "-----BEGIN MEGOLM SESSION DATA-----"; const FOOTER: &str = "-----END MEGOLM SESSION DATA-----"; -fn decode(input: impl AsRef<[u8]>) -> Result, DecodeError> { - decode_config(input, STANDARD_NO_PAD) -} - -fn encode(input: impl AsRef<[u8]>) -> String { - encode_config(input, STANDARD_NO_PAD) -} - /// Error representing a failure during key export or import. #[derive(Error, Debug)] pub enum KeyExportError { diff --git a/matrix_sdk_crypto/src/file_encryption/mod.rs b/matrix_sdk_crypto/src/file_encryption/mod.rs new file mode 100644 index 00000000..8b78ad72 --- /dev/null +++ b/matrix_sdk_crypto/src/file_encryption/mod.rs @@ -0,0 +1,23 @@ +#[allow(dead_code)] +mod attachments; +mod key_export; + +pub use key_export::{decrypt_key_export, encrypt_key_export}; + +use base64::{decode_config, encode_config, DecodeError, STANDARD_NO_PAD, URL_SAFE_NO_PAD}; + +fn decode(input: impl AsRef<[u8]>) -> Result, DecodeError> { + decode_config(input, STANDARD_NO_PAD) +} + +fn decode_url_safe(input: impl AsRef<[u8]>) -> Result, DecodeError> { + decode_config(input, URL_SAFE_NO_PAD) +} + +fn encode(input: impl AsRef<[u8]>) -> String { + encode_config(input, STANDARD_NO_PAD) +} + +fn encode_url_safe(input: impl AsRef<[u8]>) -> String { + encode_config(input, URL_SAFE_NO_PAD) +} diff --git a/matrix_sdk_crypto/src/lib.rs b/matrix_sdk_crypto/src/lib.rs index 0024dc11..dbc29869 100644 --- a/matrix_sdk_crypto/src/lib.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -27,11 +27,9 @@ )] #![cfg_attr(feature = "docs", feature(doc_cfg))] -#[allow(dead_code)] -mod attachments; mod error; +mod file_encryption; mod identities; -mod key_export; mod machine; pub mod olm; mod requests; @@ -39,10 +37,10 @@ pub mod store; mod verification; pub use error::{MegolmError, OlmError}; +pub use file_encryption::{decrypt_key_export, encrypt_key_export}; pub use identities::{ Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity, }; -pub use key_export::{decrypt_key_export, encrypt_key_export}; pub use machine::OlmMachine; pub(crate) use olm::Account; pub use olm::EncryptionSettings;