crypto: Move the file encryption modules under a submodule.

master
Damir Jelić 2020-09-14 17:06:36 +02:00
parent 1a140ecc2f
commit 51f3d90224
4 changed files with 29 additions and 30 deletions

View File

@ -21,7 +21,6 @@ use serde::{Deserialize, Serialize};
use matrix_sdk_common::events::room::JsonWebKey; 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 getrandom::getrandom;
use aes_ctr::{ use aes_ctr::{
@ -30,26 +29,12 @@ use aes_ctr::{
}; };
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use super::{decode, decode_url_safe, encode, encode_url_safe};
const IV_SIZE: usize = 16; const IV_SIZE: usize = 16;
const KEY_SIZE: usize = 32; const KEY_SIZE: usize = 32;
const VERSION: u8 = 1; const VERSION: u8 = 1;
fn decode(input: impl AsRef<[u8]>) -> Result<Vec<u8>, DecodeError> {
decode_config(input, STANDARD_NO_PAD)
}
fn decode_url_safe(input: impl AsRef<[u8]>) -> Result<Vec<u8>, 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> { pub struct AttachmentDecryptor<'a, R: 'a + Read> {
inner_reader: &'a mut R, inner_reader: &'a mut R,
expected_hash: Vec<u8>, expected_hash: Vec<u8>,
@ -78,6 +63,7 @@ impl<'a, R: Read> Read for AttachmentDecryptor<'a, R> {
impl<'a, R: Read + 'a> AttachmentDecryptor<'a, R> { impl<'a, R: Read + 'a> AttachmentDecryptor<'a, R> {
fn new(input: &'a mut R, info: EncryptionInfo) -> 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(); let hash = decode(info.hashes.get("sha256").unwrap()).unwrap();
// TODO Use zeroizing here. // TODO Use zeroizing here.
let key = decode_url_safe(info.web_key.k).unwrap(); let key = decode_url_safe(info.web_key.k).unwrap();

View File

@ -16,7 +16,6 @@ use serde_json::Error as SerdeError;
use std::io::{Cursor, Read, Seek, SeekFrom}; use std::io::{Cursor, Read, Seek, SeekFrom};
use thiserror::Error; use thiserror::Error;
use base64::{decode_config, encode_config, DecodeError, STANDARD_NO_PAD};
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use getrandom::getrandom; use getrandom::getrandom;
@ -28,6 +27,7 @@ use hmac::{Hmac, Mac, NewMac};
use pbkdf2::pbkdf2; use pbkdf2::pbkdf2;
use sha2::{Sha256, Sha512}; use sha2::{Sha256, Sha512};
use super::{decode, encode, DecodeError};
use crate::olm::ExportedRoomKey; use crate::olm::ExportedRoomKey;
const SALT_SIZE: usize = 16; const SALT_SIZE: usize = 16;
@ -39,14 +39,6 @@ const VERSION: u8 = 1;
const HEADER: &str = "-----BEGIN MEGOLM SESSION DATA-----"; const HEADER: &str = "-----BEGIN MEGOLM SESSION DATA-----";
const FOOTER: &str = "-----END MEGOLM SESSION DATA-----"; const FOOTER: &str = "-----END MEGOLM SESSION DATA-----";
fn decode(input: impl AsRef<[u8]>) -> Result<Vec<u8>, 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. /// Error representing a failure during key export or import.
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum KeyExportError { pub enum KeyExportError {

View File

@ -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<Vec<u8>, DecodeError> {
decode_config(input, STANDARD_NO_PAD)
}
fn decode_url_safe(input: impl AsRef<[u8]>) -> Result<Vec<u8>, 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)
}

View File

@ -27,11 +27,9 @@
)] )]
#![cfg_attr(feature = "docs", feature(doc_cfg))] #![cfg_attr(feature = "docs", feature(doc_cfg))]
#[allow(dead_code)]
mod attachments;
mod error; mod error;
mod file_encryption;
mod identities; mod identities;
mod key_export;
mod machine; mod machine;
pub mod olm; pub mod olm;
mod requests; mod requests;
@ -39,10 +37,10 @@ pub mod store;
mod verification; mod verification;
pub use error::{MegolmError, OlmError}; pub use error::{MegolmError, OlmError};
pub use file_encryption::{decrypt_key_export, encrypt_key_export};
pub use identities::{ pub use identities::{
Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity, Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity,
}; };
pub use key_export::{decrypt_key_export, encrypt_key_export};
pub use machine::OlmMachine; pub use machine::OlmMachine;
pub(crate) use olm::Account; pub(crate) use olm::Account;
pub use olm::EncryptionSettings; pub use olm::EncryptionSettings;