Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use struct for dehydrated device return value
Browse files Browse the repository at this point in the history
uhoreg committed Jan 9, 2025
1 parent f234c45 commit 23d3900
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/olm/account/mod.rs
Original file line number Diff line number Diff line change
@@ -92,6 +92,15 @@ pub struct InboundCreationResult {
pub plaintext: Vec<u8>,
}

/// Return type for the creation of a dehydrated device.
#[derive(Debug)]
pub struct DehydratedDeviceResult {
/// The encrypted dehydrated device, as a base64-encoded string.
pub ciphertext: String,
/// The nonce used for encrypting, as a base64-encoded string.
pub nonce: String,
}

/// An Olm [`Account`] manages all cryptographic keys used on a device.
pub struct Account {
/// A permanent Ed25519 key used for signing. Also known as the fingerprint
@@ -464,7 +473,7 @@ impl Account {
pub fn to_dehydrated_device(
&self,
key: &[u8; 32],
) -> Result<(String, String), crate::DehydratedDeviceError> {
) -> Result<DehydratedDeviceResult, crate::DehydratedDeviceError> {
use matrix_pickle::Encode;

use self::dehydrated_device::Pickle;
@@ -479,7 +488,10 @@ impl Account {
encoded.zeroize();
let ciphertext = ciphertext?;

Ok((base64_encode(ciphertext), base64_encode(nonce)))
Ok(DehydratedDeviceResult {
ciphertext: base64_encode(ciphertext),
nonce: base64_encode(nonce),
})
}

/// Create an [`Account`] object from a dehydrated device.
@@ -1434,7 +1446,7 @@ mod test {
alice.generate_one_time_keys(alice.max_number_of_one_time_keys());
alice.generate_fallback_key();

let (alice_dehydrated, nonce) =
let alice_dehydrated_result =
alice.to_dehydrated_device(&PICKLE_KEY).expect("Should be able to dehydrate device");

let mut bob_session = bob.create_outbound_session(
@@ -1451,9 +1463,12 @@ mod test {
let message = "It's a secret to everybody";
let olm_message = bob_session.encrypt(message);

let mut alice_rehydrated =
Account::from_dehydrated_device(&alice_dehydrated, &nonce, &PICKLE_KEY)
.expect("Should be able to rehydrate device");
let mut alice_rehydrated = Account::from_dehydrated_device(
&alice_dehydrated_result.ciphertext,
&alice_dehydrated_result.nonce,
&PICKLE_KEY,
)
.expect("Should be able to rehydrate device");

if let OlmMessage::PreKey(m) = olm_message {
let InboundCreationResult { session: alice_session, plaintext } =
@@ -1475,12 +1490,17 @@ mod test {
alice.generate_one_time_keys(alice.max_number_of_one_time_keys());
alice.generate_fallback_key();

let (alice_dehydrated, nonce) =
let alice_dehydrated_result =
alice.to_dehydrated_device(&PICKLE_KEY).expect("Should be able to dehydrate device");
assert!(Account::from_dehydrated_device(&alice_dehydrated, &nonce, &[1; 32],).is_err());
assert!(Account::from_dehydrated_device(
&alice_dehydrated_result.ciphertext,
&alice_dehydrated_result.nonce,
&[1; 32],
)
.is_err());

assert!(Account::from_dehydrated_device(
&alice_dehydrated,
&alice_dehydrated_result.ciphertext,
"WrongNonce123456",
&PICKLE_KEY,
)

0 comments on commit 23d3900

Please sign in to comment.