1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use super::{AssociatedData, Bicrypter, CryptError, Decrypter, Encrypter};
use std::sync::Arc;

/// Splits bicrypter into encrypter and decrypter halves that point to
/// the same underlying bicrypter via arc
pub fn split<B>(bicrypter: B) -> (EncrypterHalf<B>, DecrypterHalf<B>)
where
    B: Bicrypter,
{
    let arc_self = Arc::new(bicrypter);
    let arc_self_2 = Arc::clone(&arc_self);

    let e_half = EncrypterHalf {
        encrypter: arc_self,
    };
    let d_half = DecrypterHalf {
        decrypter: arc_self_2,
    };
    (e_half, d_half)
}

/// Splits bicrypter into encryper and decrypter halves by cloning the bicrypter,
pub fn clone_split<B>(original: B) -> (B, B)
where
    B: Bicrypter + Clone,
{
    let clone = original.clone();
    (original, clone)
}

pub struct EncrypterHalf<E>
where
    E: Encrypter,
{
    encrypter: Arc<E>,
}

impl<E> Encrypter for EncrypterHalf<E>
where
    E: Encrypter,
{
    fn encrypt(
        &self,
        buffer: &[u8],
        associated_data: &AssociatedData,
    ) -> Result<Vec<u8>, CryptError> {
        self.encrypter.encrypt(buffer, associated_data)
    }

    fn new_encrypt_associated_data(&self) -> AssociatedData {
        self.encrypter.new_encrypt_associated_data()
    }
}

pub struct DecrypterHalf<D>
where
    D: Decrypter,
{
    decrypter: Arc<D>,
}

impl<D> Decrypter for DecrypterHalf<D>
where
    D: Decrypter,
{
    fn decrypt(
        &self,
        buffer: &[u8],
        associated_data: &AssociatedData,
    ) -> Result<Vec<u8>, CryptError> {
        self.decrypter.decrypt(buffer, associated_data)
    }
}