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
//! Code for encryption.

mod sodium;

use super::*;

#[derive(Debug, Serialize, Deserialize)]
pub enum DecryptionKey {
    Sodium(::sda_protocol::byte_arrays::B32)
}

/// Description of an encryption keypair.
#[derive(Debug, Serialize, Deserialize)]
pub struct EncryptionKeypair {
    pub ek: EncryptionKey,
    pub dk: DecryptionKey,
}

pub trait EncryptorConstruction<S> {
    /// Create a new encryptor for the specificed encryption key and scheme.
    fn new_share_encryptor(&self, ek: &EncryptionKey, scheme: &S) -> SdaClientResult<Box<ShareEncryptor>>;
}

pub trait ShareEncryptor {
    /// Encrypt shares.
    fn encrypt(&self, shares: &[Share]) -> SdaClientResult<Encryption>;
}

pub trait DecryptorConstruction<ID, S> {
    /// Create a new decryptor for the specified keypair and scheme.
    fn new_share_decryptor(&self, id: &ID, scheme: &S) -> SdaClientResult<Box<ShareDecryptor>>;
}

pub trait ShareDecryptor {
    /// Decrypt shares.
    fn decrypt(&self, encryption: &Encryption) -> SdaClientResult<Vec<Share>>;
}

impl EncryptorConstruction<AdditiveEncryptionScheme> for CryptoModule {
    fn new_share_encryptor(&self, ek: &EncryptionKey, scheme: &AdditiveEncryptionScheme) -> SdaClientResult<Box<ShareEncryptor>> {
        match *scheme {
            AdditiveEncryptionScheme::Sodium => {
                let encryptor = sodium::Encryptor::new(ek)?;
                Ok(Box::new(encryptor))
            }
        }
    }
}

impl DecryptorConstruction<EncryptionKeyId, AdditiveEncryptionScheme> for CryptoModule {
    fn new_share_decryptor(&self, id: &EncryptionKeyId, scheme: &AdditiveEncryptionScheme) -> SdaClientResult<Box<ShareDecryptor>> {
        match *scheme {
            AdditiveEncryptionScheme::Sodium => {
                let decryptor = sodium::Decryptor::new(id, &self.keystore)?;
                Ok(Box::new(decryptor))
            }
        }
    }
}