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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//! # JSON serialize for crypto field (UTC / JSON)

use super::util::KECCAK256_BYTES;
use super::{Cipher, CryptoType, Error, KdfParams, KeyFile, Salt, CIPHER_IV_BYTES};
use hex;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::default::Default;

byte_array_struct!(Mac, KECCAK256_BYTES);
byte_array_struct!(Iv, CIPHER_IV_BYTES);

/// `Keyfile` related crypto attributes
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CoreCrypto {
    /// Cipher
    pub cipher: Cipher,

    /// Cipher text
    pub cipher_text: Vec<u8>,

    /// Params for `Cipher`
    pub cipher_params: CipherParams,

    /// Key derivation funciton
    pub kdf_params: KdfParams,

    /// HMAC authentication code
    pub mac: Mac,
}

/// Serialization representation for `CoreCrypto`
#[derive(Serialize, Deserialize, Debug)]
struct SerCoreCrypto {
    pub cipher: Cipher,

    #[serde(rename = "ciphertext")]
    pub cipher_text: String,

    #[serde(rename = "cipherparams")]
    pub cipher_params: CipherParams,

    pub kdf: String,

    #[serde(rename = "kdfparams")]
    pub kdf_params: KdfParams,

    pub mac: Mac,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CipherParams {
    pub iv: Iv,
}

impl Default for CipherParams {
    fn default() -> Self {
        CipherParams {
            iv: Iv::from([0; CIPHER_IV_BYTES]),
        }
    }
}

impl CoreCrypto {
    /// Try to create crypto attributes from
    /// corresponding Keyfile (simple or HDWallet keyfile)
    pub fn try_from(kf: &KeyFile) -> Result<Self, Error> {
        match kf.crypto {
            CryptoType::Core(ref core) => Ok(CoreCrypto {
                cipher: core.cipher,
                cipher_text: core.cipher_text.clone(),
                cipher_params: core.cipher_params.clone(),
                kdf_params: KdfParams {
                    kdf: core.kdf_params.kdf,
                    dklen: core.kdf_params.dklen,
                    salt: Salt::from(core.kdf_params.salt.0),
                },
                mac: Mac::from(core.mac.0),
            }),
            _ => Err(Error::NotFound),
        }
    }
}

impl Default for CoreCrypto {
    fn default() -> Self {
        Self {
            cipher: Cipher::default(),
            cipher_text: vec![],
            cipher_params: CipherParams::default(),
            kdf_params: KdfParams::default(),
            mac: Mac::default(),
        }
    }
}

impl Into<KeyFile> for CoreCrypto {
    fn into(self) -> KeyFile {
        KeyFile {
            crypto: CryptoType::Core(self),
            ..Default::default()
        }
    }
}

impl From<SerCoreCrypto> for CoreCrypto {
    fn from(ser: SerCoreCrypto) -> Self {
        CoreCrypto {
            cipher: ser.cipher,
            cipher_text: hex::decode(ser.cipher_text).unwrap(),
            cipher_params: ser.cipher_params,
            kdf_params: ser.kdf_params,
            mac: ser.mac,
        }
    }
}

impl Into<SerCoreCrypto> for CoreCrypto {
    fn into(self) -> SerCoreCrypto {
        SerCoreCrypto {
            cipher: self.cipher,
            cipher_text: hex::encode(self.cipher_text),
            cipher_params: self.cipher_params,
            kdf: self.kdf_params.kdf.to_string(),
            kdf_params: self.kdf_params,
            mac: self.mac,
        }
    }
}

impl<'de> Deserialize<'de> for CoreCrypto {
    fn deserialize<D>(deserializer: D) -> Result<CoreCrypto, D::Error>
    where
        D: Deserializer<'de>,
    {
        let ser: SerCoreCrypto = SerCoreCrypto::deserialize(deserializer)?;
        Ok(ser.into())
    }
}

impl Serialize for CoreCrypto {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let ser: SerCoreCrypto = self.clone().into();
        ser.serialize(serializer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use keystore::{Kdf, Prf};
    use tests::*;

    const KDF_PARAMS_PBKDF2: &'static str = r#"{
        "c": 10240,
        "dklen": 32,
        "prf": "hmac-sha256",
        "salt": "095a4028fa2474bb2191f9fc1d876c79a9ff76ed029aa7150d37da785a00175b"
    }"#;

    const PBKDF2_TEXT: &'static str = r#"{
      "cipher": "aes-128-ctr",
      "cipherparams": {
        "iv": "58d54158c3e27131b0a0f2b91201aedc"
      },
      "ciphertext": "9c9e3ebbf01a512f3bea41ac6fe7676344c0da77236b38847c02718ec9b66126",
      "kdf": "pbkdf2",
      "kdfparams": {
        "c": 10240,
        "dklen": 32,
        "prf": "hmac-sha256",
        "salt": "095a4028fa2474bb2191f9fc1d876c79a9ff76ed029aa7150d37da785a00175b"
      },
      "mac": "83c175d2ef1229ab10eb6726500a4303ab729e6e44dfaac274fe75c870b23a63"
    }"#;

    const SCRYPT_TEXT: &'static str = r#"{
      "ciphertext": "c3dfc95ca91dce73fe8fc4ddbaed33bad522e04a6aa1af62bba2a0bb90092fa1",
      "cipherparams": {
        "iv": "9df1649dd1c50f2153917e3b9e7164e9"
      },
      "cipher": "aes-128-ctr",
      "kdf": "scrypt",
      "kdfparams": {
        "dklen": 32,
        "salt": "fd4acb81182a2c8fa959d180967b374277f2ccf2f7f401cb08d042cc785464b4",
        "n": 1024,
        "r": 8,
        "p": 1
      },
      "mac": "9f8a85347fd1a81f14b99f69e2b401d68fb48904efe6a66b357d8d1d61ab14e5"
    }"#;

    #[test]
    fn should_serialize_kdf_params() {
        let exp = KdfParams {
            kdf: Kdf::Pbkdf2 {
                prf: Prf::default(),
                c: 10240,
            },
            dklen: 32,
            salt: serde_json::from_str(
                "\"095a4028fa2474bb2191f9fc1d876c79a9ff76ed029aa7150d37da785a00175b\"",
            )
            .unwrap(),
        };

        let mut act: KdfParams = serde_json::from_str(KDF_PARAMS_PBKDF2).unwrap();
        act = serde_json::from_str::<KdfParams>(&serde_json::to_string(&act).unwrap()).unwrap();

        assert_eq!(act, exp);
    }

    #[test]
    fn should_serialize_pbkdf2_crypto() {
        let exp = CoreCrypto {
            cipher: Cipher::default(),
            cipher_text: Vec::from_hex(
                "9c9e3ebbf01a512f3bea41ac6fe7676344c0da77236b38847c02718ec9b66126",
            )
            .unwrap(),
            cipher_params: CipherParams {
                iv: serde_json::from_str("\"58d54158c3e27131b0a0f2b91201aedc\"").unwrap(),
            },
            kdf_params: KdfParams {
                kdf: Kdf::Pbkdf2 {
                    prf: Prf::default(),
                    c: 10240,
                },
                dklen: 32,
                salt: serde_json::from_str(
                    "\"095a4028fa2474bb2191f9fc1d876c79a9ff76ed029aa7150d37da785a00175b\"",
                )
                .unwrap(),
            },
            mac: serde_json::from_str(
                "\"83c175d2ef1229ab10eb6726500a4303ab729e6e44dfaac274fe75c870b23a63\"",
            )
            .unwrap(),
        };

        // just first encoding
        let mut act = serde_json::from_str::<CoreCrypto>(PBKDF2_TEXT).unwrap();
        act = serde_json::from_str::<CoreCrypto>(&serde_json::to_string(&act).unwrap()).unwrap();

        assert_eq!(act, exp);
    }

    #[test]
    fn should_serialize_scrypt_crypto() {
        let exp = CoreCrypto {
            cipher: Cipher::default(),
            cipher_text: Vec::from_hex(
                "c3dfc95ca91dce73fe8fc4ddbaed33bad522e04a6aa1af62bba2a0bb90092fa1",
            )
            .unwrap(),
            cipher_params: CipherParams {
                iv: serde_json::from_str("\"9df1649dd1c50f2153917e3b9e7164e9\"").unwrap(),
            },
            kdf_params: KdfParams {
                kdf: Kdf::Scrypt {
                    n: 1024,
                    r: 8,
                    p: 1,
                },
                dklen: 32,
                salt: serde_json::from_str(
                    "\"fd4acb81182a2c8fa959d180967b374277f2ccf2f7f401cb08d042cc785464b4\"",
                )
                .unwrap(),
            },
            mac: serde_json::from_str(
                "\"9f8a85347fd1a81f14b99f69e2b401d68fb48904efe6a66b357d8d1d61ab14e5\"",
            )
            .unwrap(),
        };

        // just first encoding
        let act = serde_json::from_str::<CoreCrypto>(SCRYPT_TEXT).unwrap();

        // verify encoding & decoding full cycle logic
        let act =
            serde_json::from_str::<CoreCrypto>(&serde_json::to_string(&act).unwrap()).unwrap();

        assert_eq!(act, exp);
    }

    #[test]
    fn should_not_decode_unknown_kdf_prf() {
        let text = PBKDF2_TEXT.replace(&Prf::default().to_string(), "unknown");

        assert!(serde_json::from_str::<CoreCrypto>(&text).is_err());
    }

    #[test]
    fn should_not_decode_unknown_cipher() {
        let text = SCRYPT_TEXT.replace(&Cipher::default().to_string(), "unknown");

        assert!(serde_json::from_str::<CoreCrypto>(&text).is_err());
    }

    #[test]
    fn should_not_decode_not_wrong_crypto() {
        assert!(serde_json::from_str::<CoreCrypto>("garbage").is_err());
    }
}