Use the base64 encoding/decoding functions from vodozemac

This commit is contained in:
Damir Jelić
2023-09-08 10:58:30 +02:00
parent 7e06ad130c
commit 72e3079aab
11 changed files with 16 additions and 47 deletions

3
Cargo.lock generated
View File

@@ -3150,7 +3150,6 @@ dependencies = [
"async-std",
"async-trait",
"atomic",
"base64 0.21.2",
"bs58",
"byteorder",
"cbc",
@@ -6431,7 +6430,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "vodozemac"
version = "0.4.0"
source = "git+https://github.com/matrix-org/vodozemac/?rev=e3b658526f6f1dd0a9065c1c96346b796712c425#e3b658526f6f1dd0a9065c1c96346b796712c425"
source = "git+https://github.com/matrix-org/vodozemac/?rev=23098d391d9a91db7d72a0ce19e69559b4340b13#23098d391d9a91db7d72a0ce19e69559b4340b13"
dependencies = [
"aes",
"arrayvec",

View File

@@ -48,7 +48,7 @@ tracing = { version = "0.1.36", default-features = false, features = ["std"] }
tracing-core = "0.1.30"
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "e20b9c2b72144ef51a381c6b321ac810a4fbfdbe" }
uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs", rev = "e20b9c2b72144ef51a381c6b321ac810a4fbfdbe" }
vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "e3b658526f6f1dd0a9065c1c96346b796712c425" }
vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "23098d391d9a91db7d72a0ce19e69559b4340b13" }
zeroize = "1.6.0"
# Default release profile, select with `--release`

View File

@@ -31,7 +31,6 @@ aes = "0.8.1"
atomic = "0.5.1"
async-std = { version = "1.12.0", features = ["unstable"] }
async-trait = { workspace = true }
base64 = { workspace = true }
bs58 = { version = "0.5.0", optional = true }
byteorder = { workspace = true }
cbc = { version = "0.1.2", features = ["std"], optional = true }

View File

@@ -25,9 +25,7 @@ use hkdf::Hkdf;
use hmac::{digest::MacError, Hmac, Mac as MacT};
use sha2::Sha256;
use thiserror::Error;
use vodozemac::{Curve25519PublicKey, Curve25519SecretKey, KeyError, SharedSecret};
use crate::utilities::base64_decode;
use vodozemac::{base64_decode, Curve25519PublicKey, Curve25519SecretKey, KeyError, SharedSecret};
type Aes256CbcEnc = cbc::Encryptor<Aes256>;
type Aes256CbcDec = cbc::Decryptor<Aes256>;
@@ -171,7 +169,7 @@ impl PkEncryption {
#[derive(Debug, Error)]
pub enum MessageDecodeError {
#[error(transparent)]
Base64(#[from] base64::DecodeError),
Base64(#[from] vodozemac::Base64DecodeError),
#[error(transparent)]
Key(#[from] KeyError),
}
@@ -214,10 +212,9 @@ pub enum Error {
#[cfg(test)]
mod tests {
use olm_rs::pk::{OlmPkDecryption, OlmPkEncryption, PkMessage};
use vodozemac::Curve25519PublicKey;
use vodozemac::{base64_encode, Curve25519PublicKey};
use super::{Message, MessageDecodeError, PkDecryption, PkEncryption};
use crate::utilities::base64_encode;
impl TryFrom<PkMessage> for Message {
type Error = MessageDecodeError;

View File

@@ -44,7 +44,7 @@ pub enum DecodeError {
Base58(#[from] bs58::decode::Error),
/// The recovery key isn't valid base64.
#[error(transparent)]
Base64(#[from] base64::DecodeError),
Base64(#[from] vodozemac::Base64DecodeError),
/// The recovery key is too short, we couldn't read enough data.
#[error(transparent)]
Io(#[from] std::io::Error),
@@ -121,7 +121,7 @@ impl BackupDecryptionKey {
/// Try to create a [`BackupDecryptionKey`] from a base64 export.
pub fn from_base64(key: &str) -> Result<Self, DecodeError> {
let decoded = Zeroizing::new(crate::utilities::base64_decode(key)?);
let decoded = Zeroizing::new(vodozemac::base64_decode(key)?);
if decoded.len() != Self::KEY_SIZE {
Err(DecodeError::Length(Self::KEY_SIZE, decoded.len()))

View File

@@ -21,7 +21,6 @@ use aes::{
cipher::{generic_array::GenericArray, KeyIvInit, StreamCipher},
Aes256,
};
use base64::DecodeError;
use rand::{thread_rng, RngCore};
use ruma::{
events::room::{EncryptedFile, JsonWebKey, JsonWebKeyInit},
@@ -83,7 +82,7 @@ pub enum DecryptorError {
/// Some data in the encrypted attachment coldn't be decoded, this may be a
/// hash, the secret key, or the initialization vector.
#[error(transparent)]
Decode(#[from] DecodeError),
Decode(#[from] vodozemac::Base64DecodeError),
/// A hash is missing from the encryption info.
#[error("The encryption info is missing a hash")]
MissingHash,

View File

@@ -25,12 +25,10 @@ use rand::{thread_rng, RngCore};
use serde_json::Error as SerdeError;
use sha2::{Sha256, Sha512};
use thiserror::Error;
use vodozemac::{base64_decode, base64_encode};
use zeroize::Zeroize;
use crate::{
olm::ExportedRoomKey,
utilities::{base64_decode, base64_encode, DecodeError},
};
use crate::olm::ExportedRoomKey;
type Aes256Ctr = ctr::Ctr128BE<Aes256>;
@@ -63,7 +61,7 @@ pub enum KeyExportError {
Json(#[from] SerdeError),
/// The key export string isn't valid base64.
#[error(transparent)]
Decode(#[from] DecodeError),
Decode(#[from] vodozemac::Base64DecodeError),
/// The key export doesn't all the required fields.
#[error(transparent)]
Io(#[from] std::io::Error),

View File

@@ -44,6 +44,7 @@ use sha2::{Digest, Sha256};
use tokio::sync::Mutex;
use tracing::{debug, field::debug, info, instrument, trace, warn, Span};
use vodozemac::{
base64_encode,
olm::{
Account as InnerAccount, AccountPickle, IdentityKeys, OlmMessage,
OneTimeKeyGenerationResult, PreKeyMessage, SessionConfig,
@@ -73,7 +74,6 @@ use crate::{
},
CrossSigningKey, DeviceKeys, EventEncryptionAlgorithm, MasterPubkey, OneTimeKey, SignedKey,
},
utilities::base64_encode,
CryptoStoreError, OlmError, SignatureError,
};
@@ -602,7 +602,8 @@ impl ReadOnlyAccount {
/// encoded as base64 will be used for the device ID.
pub fn new(user_id: &UserId) -> Self {
let account = InnerAccount::new();
let device_id: OwnedDeviceId = base64_encode(account.identity_keys().curve25519.as_bytes()).into();
let device_id: OwnedDeviceId =
base64_encode(account.identity_keys().curve25519.as_bytes()).into();
Self::new_helper(account, user_id, &device_id)
}

View File

@@ -16,7 +16,7 @@ use ruma::{encryption::KeyUsage, DeviceKeyAlgorithm, DeviceKeyId, OwnedUserId};
use serde::{Deserialize, Serialize};
use serde_json::{Error as JsonError, Value};
use thiserror::Error;
use vodozemac::{Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature, KeyError};
use vodozemac::{DecodeError, Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature, KeyError};
use zeroize::Zeroize;
use crate::{
@@ -26,7 +26,6 @@ use crate::{
CrossSigningKey, DeviceKeys, MasterPubkey, SelfSigningPubkey, Signatures, SigningKeys,
UserSigningPubkey,
},
utilities::DecodeError,
ReadOnlyUserIdentity,
};

View File

@@ -58,7 +58,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::Mutex;
use tracing::{info, warn};
use vodozemac::{megolm::SessionOrdering, Curve25519PublicKey};
use vodozemac::{base64_encode, megolm::SessionOrdering, Curve25519PublicKey};
use zeroize::Zeroize;
use crate::{
@@ -71,7 +71,6 @@ use crate::{
ReadOnlyAccount, Session,
},
types::{events::room_key_withheld::RoomKeyWithheldEvent, EventEncryptionAlgorithm},
utilities::base64_encode,
verification::VerificationMachine,
CrossSigningStatus, ReadOnlyOwnUserIdentity,
};

View File

@@ -20,30 +20,8 @@ use std::{
time::Duration,
};
pub use base64::DecodeError;
use base64::{
alphabet,
engine::{general_purpose, GeneralPurpose},
Engine,
};
use matrix_sdk_common::instant::Instant;
const STANDARD_NO_PAD: GeneralPurpose = GeneralPurpose::new(
&alphabet::STANDARD,
general_purpose::NO_PAD
.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
);
/// Decode the input as base64 with no padding.
pub fn base64_decode(input: impl AsRef<[u8]>) -> Result<Vec<u8>, DecodeError> {
STANDARD_NO_PAD.decode(input)
}
/// Encode the input as base64 with no padding.
pub fn base64_encode(input: impl AsRef<[u8]>) -> String {
STANDARD_NO_PAD.encode(input)
}
#[cfg(test)]
pub(crate) fn json_convert<T, U>(value: &T) -> serde_json::Result<U>
where