      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

    // Copyright 2015, 2016, 2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    use std::iter::repeat;
    use ethkey::Secret;
    use {json, Error, crypto};
    use crypto::Keccak256;
    use random::Random;
    use smallvec::SmallVec;
    use account::{Cipher, Kdf, Aes128Ctr, Pbkdf2, Prf};

    /// Encrypted data
    #[derive(Debug, PartialEq, Clone)]
    pub struct Crypto {
    	/// Encryption parameters
    	pub cipher: Cipher,
    	/// Encrypted data buffer
    	pub ciphertext: Vec<u8>,
    	/// Key derivation function parameters
    	pub kdf: Kdf,
    	/// Message authentication code
    	pub mac: [u8; 32],
    }

    impl From<json::Crypto> for Crypto {
    	fn from(json: json::Crypto) -> Self {
    		Crypto {
    			cipher: json.cipher.into(),
    			ciphertext: json.ciphertext.into(),
    			kdf: json.kdf.into(),
    			mac: json.mac.into(),
    		}
    	}
    }

    impl Into<json::Crypto> for Crypto {
    	fn into(self) -> json::Crypto {
    		json::Crypto {
    			cipher: self.cipher.into(),
    			ciphertext: self.ciphertext.into(),
    			kdf: self.kdf.into(),
    			mac: self.mac.into(),
    		}
    	}
    }

    impl Crypto {
    	pub fn with_secret(secret: &Secret, password: &str, iterations: u32) -> Self {
    		Crypto::with_plain(&*secret, password, iterations)
    	}

    	pub fn with_plain(plain: &[u8], password: &str, iterations: u32) -> Self {
    		let salt: [u8; 32] = Random::random();
    		let iv: [u8; 16] = Random::random();

    		// two parts of derived key
    		// DK = [ DK[0..15] DK[16..31] ] = [derived_left_bits, derived_right_bits]
    		let (derived_left_bits, derived_right_bits) = crypto::derive_key_iterations(password, &salt, iterations);

    		// preallocated (on-stack in case of `Secret`) buffer to hold cipher
    		// length = length(plain) as we are using CTR-approach
    		let plain_len = plain.len();
    		let mut ciphertext: SmallVec<[u8; 32]> = SmallVec::new();
    		ciphertext.grow(plain_len);
    		ciphertext.extend(repeat(0).take(plain_len));

    		// aes-128-ctr with initial vector of iv
    		crypto::aes::encrypt(&derived_left_bits, &iv, plain, &mut *ciphertext);

    		// KECCAK(DK[16..31] ++ <ciphertext>), where DK[16..31] - derived_right_bits
    		let mac = crypto::derive_mac(&derived_right_bits, &*ciphertext).keccak256();

    		Crypto {
    			cipher: Cipher::Aes128Ctr(Aes128Ctr {
    				iv: iv,
    			}),
    			ciphertext: (*ciphertext).to_vec(),
    			kdf: Kdf::Pbkdf2(Pbkdf2 {
    				dklen: crypto::KEY_LENGTH as u32,
    				salt: salt,
    				c: iterations,
    				prf: Prf::HmacSha256,
    			}),
    			mac: mac,
    		}
    	}

    	pub fn secret(&self, password: &str) -> Result<Secret, Error> {
    		if self.ciphertext.len() > 32 {
    			return Err(Error::InvalidSecret);
    		}

    		let secret = self.do_decrypt(password, 32)?;
    		Ok(Secret::from_slice(&secret)?)
    	}

    	pub fn decrypt(&self, password: &str) -> Result<Vec<u8>, Error> {
    		let expected_len = self.ciphertext.len();
    		self.do_decrypt(password, expected_len)
    	}

    	fn do_decrypt(&self, password: &str, expected_len: usize) -> Result<Vec<u8>, Error> {
    		let (derived_left_bits, derived_right_bits) = match self.kdf {
    			Kdf::Pbkdf2(ref params) => crypto::derive_key_iterations(password, &params.salt, params.c),
    			Kdf::Scrypt(ref params) => crypto::derive_key_scrypt(password, &params.salt, params.n, params.p, params.r)?,
    		};

    		let mac = crypto::derive_mac(&derived_right_bits, &self.ciphertext).keccak256();

    		if mac != self.mac {
    			return Err(Error::InvalidPassword);
    		}

    		let mut plain: SmallVec<[u8; 32]> = SmallVec::new();
    		plain.grow(expected_len);
    		plain.extend(repeat(0).take(expected_len));

    		match self.cipher {
    			Cipher::Aes128Ctr(ref params) => {
    				// checker by callers
    				debug_assert!(expected_len >= self.ciphertext.len());

    				let from = expected_len - self.ciphertext.len();
    				crypto::aes::decrypt(&derived_left_bits, &params.iv, &self.ciphertext, &mut plain[from..]);
    				Ok(plain.into_iter().collect())
    			},
    		}
    	}
    }

    #[cfg(test)]
    mod tests {
    	use ethkey::{Generator, Random};
    	use super::Crypto;

    	#[test]
    	fn crypto_with_secret_create() {
    		let keypair = Random.generate().unwrap();
    		let crypto = Crypto::with_secret(keypair.secret(), "this is sparta", 10240);
    		let secret = crypto.secret("this is sparta").unwrap();
    		assert_eq!(keypair.secret(), &secret);
    	}

    	#[test]
    	#[should_panic]
    	fn crypto_with_secret_invalid_password() {
    		let keypair = Random.generate().unwrap();
    		let crypto = Crypto::with_secret(keypair.secret(), "this is sparta", 10240);
    		let _ = crypto.secret("this is sparta!").unwrap();
    	}

    	#[test]
    	fn crypto_with_null_plain_data() {
    		let original_data = b"";
    		let crypto = Crypto::with_plain(&original_data[..], "this is sparta", 10240);
    		let decrypted_data = crypto.decrypt("this is sparta").unwrap();
    		assert_eq!(original_data[..], *decrypted_data);
    	}

    	#[test]
    	fn crypto_with_tiny_plain_data() {
    		let original_data = b"{}";
    		let crypto = Crypto::with_plain(&original_data[..], "this is sparta", 10240);
    		let decrypted_data = crypto.decrypt("this is sparta").unwrap();
    		assert_eq!(original_data[..], *decrypted_data);
    	}

    	#[test]
    	fn crypto_with_huge_plain_data() {
    		let original_data: Vec<_> = (1..65536).map(|i| (i % 256) as u8).collect();
    		let crypto = Crypto::with_plain(&original_data, "this is sparta", 10240);
    		let decrypted_data = crypto.decrypt("this is sparta").unwrap();
    		assert_eq!(&original_data, &decrypted_data);
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)