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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::collections::HashMap;

use crate::types::{Ciphertext, SecretDigest, SecretKey, Signature};
use borsh::{BorshSerialize, BorshDeserialize};
use thiserror::Error;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Error, Debug, PartialEq, Eq)]
pub enum EncryptorError {
    #[error("Key gen failed")]
    KeyGenFailed,

    #[error("Encode failed")]
    EncodeFailed,

    #[error("Decode failed")]
    DecodeFailed,

    #[error("Rsa encrypt failed")]
    RsaEncryptFailed(String),

    #[error("Rsa decrypt failed")]
    RsaDecryptFailed(String),

    #[error("Sign failed: {0}")]
    SignFailed(String),

    #[error("Invalid result: {0}")]
    InvalidResult(String),

    #[error("Verify failed: {0}")]
    VerifyFailed(String),

    #[error("Aes encrypt failed")]
    AesEncryptFailed,

    #[error("Aes decrypt failed")]
    AesDecryptFailed,

    #[error("Public key not found")]
    PublicKeyNotfound,

    #[error("Failed to import public key")]
    ImportPublicKeyError,

    #[error("Failed to export public key")]
    ExportPublicKeyError,

    #[error("Failed to import private key")]
    ImportPrivateKeyError,

    #[error("Failed to export private key")]
    ExportPrivateKeyError,

    #[error("Invalid nonce")]
    InvalidNonce,

    #[error("Add public key error")]
    AddPublicKeyError,

    #[error("Read public key error")]
    ReadPublicKeyError,

    #[error("Missing secrets")]
    MissingSecret,
}

impl From<EncryptorError> for crate::error::Error {
    fn from(e: EncryptorError) -> Self {
        crate::error::Error::CryptoError(e.to_string())
    }
}

#[derive(Debug, PartialEq, Eq, Clone, BorshSerialize, BorshDeserialize)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodePublicKeyRaw {
    pub rsa: String,
    pub ec: String,
}

pub type EncryptorResult<T> = std::result::Result<T, EncryptorError>;

pub trait EncryptorT: std::fmt::Debug + Send + Sync {
    fn add_public_key(&self, addr: String, raw: &NodePublicKeyRaw) -> EncryptorResult<()>;

    fn export_public_key(&self, addr: Option<&str>) -> EncryptorResult<NodePublicKeyRaw>;

    fn gen_secret(&self) -> SecretKey;

    fn encrypt(&self, addr: Option<&str>, text: &[u8]) -> EncryptorResult<Vec<u8>>;

    fn decrypt(&self, text: &[u8]) -> EncryptorResult<Vec<u8>>;

    fn apply(&self, secret: &SecretKey, buf: &mut [u8]);

    fn apply_multi(&self, secret: Vec<SecretKey>, buf: &mut [u8]);

    fn sign_raw(&self, message: &[u8]) -> EncryptorResult<Vec<u8>>;

    fn verify_raw(
        &self,
        addr: Option<&str>,
        message: &[u8],
        signature: &[u8],
    ) -> EncryptorResult<()>;

    fn sign(&self, message: &[u8], signer: String) -> EncryptorResult<Signature>;

    fn verify(&self, message: &[u8], signature: &Signature) -> EncryptorResult<()>;

    fn shuffle(&self, items: &mut Vec<Ciphertext>);

    fn digest(&self, text: &[u8]) -> SecretDigest;

    fn decrypt_with_secrets(
        &self,
        ciphertext_map: HashMap<usize, Ciphertext>,
        mut secret_map: HashMap<usize, Vec<SecretKey>>,
        valid_options: &[String],
    ) -> EncryptorResult<HashMap<usize, String>> {
        let mut ret = HashMap::new();
        for (i, mut buf) in ciphertext_map.into_iter() {
            if let Some(secrets) = secret_map.remove(&i) {
                self.apply_multi(secrets, &mut buf);
                let value = String::from_utf8(buf).or(Err(EncryptorError::DecodeFailed))?;
                if !valid_options.contains(&value) {
                    return Err(EncryptorError::InvalidResult(value))?;
                }
                ret.insert(i, value);
            } else {
                return Err(EncryptorError::MissingSecret);
            }
        }
        Ok(ret)
    }
}

pub trait Digestable {
    fn digest(&self) -> String;
}

#[cfg(test)]
pub mod tests {
    use crate::types::{Ciphertext, SecretDigest, SecretKey, Signature};

    use super::{EncryptorResult, EncryptorT, NodePublicKeyRaw};

    #[derive(Debug, Default)]
    pub struct DummyEncryptor {}

    #[allow(unused)]
    impl EncryptorT for DummyEncryptor {
        fn add_public_key(&self, addr: String, raw: &NodePublicKeyRaw) -> EncryptorResult<()> {
            Ok(())
        }

        fn export_public_key(&self, addr: Option<&str>) -> EncryptorResult<NodePublicKeyRaw> {
            Ok(NodePublicKeyRaw {
                rsa: "".into(),
                ec: "".into(),
            })
        }

        fn gen_secret(&self) -> SecretKey {
            vec![0, 0, 0, 0]
        }

        fn encrypt(&self, addr: Option<&str>, text: &[u8]) -> EncryptorResult<Vec<u8>> {
            Ok(vec![0, 0, 0, 0])
        }

        fn decrypt(&self, text: &[u8]) -> EncryptorResult<Vec<u8>> {
            Ok(vec![0, 0, 0, 0])
        }

        fn apply(&self, secret: &SecretKey, buf: &mut [u8]) {}

        fn apply_multi(&self, secret: Vec<SecretKey>, buf: &mut [u8]) {}

        fn sign_raw(&self, message: &[u8]) -> EncryptorResult<Vec<u8>> {
            Ok(vec![0, 0, 0, 0])
        }

        fn verify_raw(
            &self,
            addr: Option<&str>,
            message: &[u8],
            signature: &[u8],
        ) -> EncryptorResult<()> {
            Ok(())
        }

        fn sign(&self, message: &[u8], signer: String) -> EncryptorResult<Signature> {
            Ok(Signature {
                signer,
                timestamp: 0,
                signature: "".into(),
            })
        }

        fn verify(&self, message: &[u8], signature: &Signature) -> EncryptorResult<()> {
            Ok(())
        }

        fn shuffle(&self, items: &mut Vec<Ciphertext>) {}

        fn digest(&self, text: &[u8]) -> SecretDigest {
            vec![0, 1, 2, 3]
        }
    }
}