From b5c61af47234efe76a5017a8feff87268c6367a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 8 Dec 2020 16:10:46 +0100 Subject: [PATCH] crypto: Move the base64 helpers into a common module. --- .../src/file_encryption/attachments.rs | 2 +- .../src/file_encryption/key_export.rs | 6 ++-- matrix_sdk_crypto/src/file_encryption/mod.rs | 18 ---------- matrix_sdk_crypto/src/lib.rs | 1 + matrix_sdk_crypto/src/olm/account.rs | 2 +- .../src/olm/signing/pk_signing.rs | 10 +----- matrix_sdk_crypto/src/utilities.rs | 36 +++++++++++++++++++ 7 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 matrix_sdk_crypto/src/utilities.rs diff --git a/matrix_sdk_crypto/src/file_encryption/attachments.rs b/matrix_sdk_crypto/src/file_encryption/attachments.rs index 675e9ed7..b578d71c 100644 --- a/matrix_sdk_crypto/src/file_encryption/attachments.rs +++ b/matrix_sdk_crypto/src/file_encryption/attachments.rs @@ -33,7 +33,7 @@ use aes_ctr::{ use base64::DecodeError; use sha2::{Digest, Sha256}; -use super::{decode, decode_url_safe, encode, encode_url_safe}; +use crate::utilities::{decode, decode_url_safe, encode, encode_url_safe}; const IV_SIZE: usize = 16; const KEY_SIZE: usize = 32; diff --git a/matrix_sdk_crypto/src/file_encryption/key_export.rs b/matrix_sdk_crypto/src/file_encryption/key_export.rs index b881ec64..da394edf 100644 --- a/matrix_sdk_crypto/src/file_encryption/key_export.rs +++ b/matrix_sdk_crypto/src/file_encryption/key_export.rs @@ -27,8 +27,10 @@ use hmac::{Hmac, Mac, NewMac}; use pbkdf2::pbkdf2; use sha2::{Sha256, Sha512}; -use super::{decode, encode, DecodeError}; -use crate::olm::ExportedRoomKey; +use crate::{ + olm::ExportedRoomKey, + utilities::{decode, encode, DecodeError}, +}; const SALT_SIZE: usize = 16; const IV_SIZE: usize = 16; diff --git a/matrix_sdk_crypto/src/file_encryption/mod.rs b/matrix_sdk_crypto/src/file_encryption/mod.rs index 45ccd310..41e0b045 100644 --- a/matrix_sdk_crypto/src/file_encryption/mod.rs +++ b/matrix_sdk_crypto/src/file_encryption/mod.rs @@ -3,21 +3,3 @@ mod key_export; pub use attachments::{AttachmentDecryptor, AttachmentEncryptor, DecryptorError}; 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) -} - -pub fn encode(input: impl AsRef<[u8]>) -> String { - encode_config(input, STANDARD_NO_PAD) -} - -pub 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 0a1f0300..f37cf97e 100644 --- a/matrix_sdk_crypto/src/lib.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -36,6 +36,7 @@ pub mod olm; mod requests; mod session_manager; pub mod store; +mod utilities; mod verification; pub use error::{MegolmError, OlmError}; diff --git a/matrix_sdk_crypto/src/olm/account.rs b/matrix_sdk_crypto/src/olm/account.rs index a6beb442..9b6551bd 100644 --- a/matrix_sdk_crypto/src/olm/account.rs +++ b/matrix_sdk_crypto/src/olm/account.rs @@ -54,10 +54,10 @@ use olm_rs::{ use crate::{ error::{EventError, OlmResult, SessionCreationError}, - file_encryption::encode, identities::ReadOnlyDevice, requests::UploadSigningKeysRequest, store::{Changes, Store}, + utilities::encode, OlmError, }; diff --git a/matrix_sdk_crypto/src/olm/signing/pk_signing.rs b/matrix_sdk_crypto/src/olm/signing/pk_signing.rs index fb552349..661c7a61 100644 --- a/matrix_sdk_crypto/src/olm/signing/pk_signing.rs +++ b/matrix_sdk_crypto/src/olm/signing/pk_signing.rs @@ -16,7 +16,6 @@ use aes_gcm::{ aead::{generic_array::GenericArray, Aead, NewAead}, Aes256Gcm, }; -use base64::{decode_config, encode_config, DecodeError, URL_SAFE_NO_PAD}; use getrandom::getrandom; use matrix_sdk_common::{ encryption::DeviceKeys, @@ -42,19 +41,12 @@ use matrix_sdk_common::{ use crate::{ error::SignatureError, identities::{MasterPubkey, SelfSigningPubkey, UserSigningPubkey}, + utilities::{decode_url_safe as decode, encode_url_safe as encode, DecodeError}, UserIdentity, }; const NONCE_SIZE: usize = 12; -fn encode>(input: T) -> String { - encode_config(input, URL_SAFE_NO_PAD) -} - -fn decode>(input: T) -> Result, DecodeError> { - decode_config(input, URL_SAFE_NO_PAD) -} - /// Error type reporting failures in the Signign operations. #[derive(Debug, Error)] pub enum SigningError { diff --git a/matrix_sdk_crypto/src/utilities.rs b/matrix_sdk_crypto/src/utilities.rs new file mode 100644 index 00000000..fcf6dc5a --- /dev/null +++ b/matrix_sdk_crypto/src/utilities.rs @@ -0,0 +1,36 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub use base64::DecodeError; +use base64::{decode_config, encode_config, STANDARD_NO_PAD, URL_SAFE_NO_PAD}; + +/// Decode the input as base64 with no padding. +pub fn decode(input: impl AsRef<[u8]>) -> Result, DecodeError> { + decode_config(input, STANDARD_NO_PAD) +} + +/// Decode the input as URL safe base64 with no padding. +pub fn decode_url_safe(input: impl AsRef<[u8]>) -> Result, DecodeError> { + decode_config(input, URL_SAFE_NO_PAD) +} + +/// Encode the input as base64 with no padding. +pub fn encode(input: impl AsRef<[u8]>) -> String { + encode_config(input, STANDARD_NO_PAD) +} + +/// Encode the input as URL safe base64 with no padding. +pub fn encode_url_safe(input: impl AsRef<[u8]>) -> String { + encode_config(input, URL_SAFE_NO_PAD) +}