2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2017-07-14 20:40:28 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Encryption schemes supported by RPC layer.
|
|
|
|
|
2018-05-05 11:02:33 +02:00
|
|
|
use crypto::aes_gcm::{Encryptor, Decryptor};
|
|
|
|
use ethkey::crypto::ecies;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2017-07-14 20:40:28 +02:00
|
|
|
use ethkey::{self, Public, Secret};
|
2018-12-28 10:33:49 +01:00
|
|
|
use memzero::Memzero;
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
/// Length of AES key
|
|
|
|
pub const AES_KEY_LEN: usize = 32;
|
|
|
|
/// Length of AES nonce (IV)
|
|
|
|
pub const AES_NONCE_LEN: usize = 12;
|
|
|
|
|
|
|
|
// nonce used for encryption when broadcasting
|
|
|
|
const BROADCAST_IV: [u8; AES_NONCE_LEN] = [0xff; AES_NONCE_LEN];
|
|
|
|
|
|
|
|
// how to encode aes key/nonce.
|
|
|
|
enum AesEncode {
|
|
|
|
AppendedNonce, // receiver known, random nonce appended.
|
|
|
|
OnTopics(Vec<H256>), // receiver knows topics but not key. nonce global.
|
|
|
|
}
|
|
|
|
|
|
|
|
enum EncryptionInner {
|
2018-04-11 13:57:12 +02:00
|
|
|
AES(Memzero<[u8; AES_KEY_LEN]>, [u8; AES_NONCE_LEN], AesEncode),
|
2017-07-14 20:40:28 +02:00
|
|
|
ECIES(Public),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encryption good for single usage.
|
|
|
|
pub struct EncryptionInstance(EncryptionInner);
|
|
|
|
|
|
|
|
impl EncryptionInstance {
|
|
|
|
/// ECIES encryption using public key. Fails if invalid public key.
|
|
|
|
pub fn ecies(public: Public) -> Result<Self, &'static str> {
|
|
|
|
if !ethkey::public_is_valid(&public) {
|
|
|
|
return Err("Invalid public key");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(EncryptionInstance(EncryptionInner::ECIES(public)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 256-bit AES GCM encryption with given nonce.
|
|
|
|
/// It is extremely insecure to reuse nonces.
|
|
|
|
///
|
|
|
|
/// If generating nonces with a secure RNG, limit uses such that
|
|
|
|
/// the chance of collision is negligible.
|
2018-04-11 13:57:12 +02:00
|
|
|
pub fn aes(key: Memzero<[u8; AES_KEY_LEN]>, nonce: [u8; AES_NONCE_LEN]) -> Self {
|
2017-07-14 20:40:28 +02:00
|
|
|
EncryptionInstance(EncryptionInner::AES(key, nonce, AesEncode::AppendedNonce))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Broadcast encryption for the message based on the given topics.
|
|
|
|
///
|
|
|
|
/// Key reuse here is extremely dangerous. It should be randomly generated
|
|
|
|
/// with a secure RNG.
|
2018-04-11 13:57:12 +02:00
|
|
|
pub fn broadcast(key: Memzero<[u8; AES_KEY_LEN]>, topics: Vec<H256>) -> Self {
|
2017-07-14 20:40:28 +02:00
|
|
|
EncryptionInstance(EncryptionInner::AES(key, BROADCAST_IV, AesEncode::OnTopics(topics)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encrypt the supplied plaintext
|
2018-05-05 11:02:33 +02:00
|
|
|
pub fn encrypt(self, plain: &[u8]) -> Option<Vec<u8>> {
|
2017-07-14 20:40:28 +02:00
|
|
|
match self.0 {
|
|
|
|
EncryptionInner::AES(key, nonce, encode) => {
|
|
|
|
match encode {
|
|
|
|
AesEncode::AppendedNonce => {
|
2018-05-05 11:02:33 +02:00
|
|
|
let mut enc = Encryptor::aes_256_gcm(&*key).ok()?;
|
|
|
|
let mut buf = enc.encrypt(&nonce, plain.to_vec()).ok()?;
|
2017-07-14 20:40:28 +02:00
|
|
|
buf.extend(&nonce[..]);
|
2018-05-05 11:02:33 +02:00
|
|
|
Some(buf)
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
AesEncode::OnTopics(topics) => {
|
|
|
|
let mut buf = Vec::new();
|
2018-04-11 13:57:12 +02:00
|
|
|
for mut t in topics {
|
|
|
|
xor(&mut t.0, &key);
|
|
|
|
buf.extend(&t.0);
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
2018-05-05 11:02:33 +02:00
|
|
|
let mut enc = Encryptor::aes_256_gcm(&*key).ok()?;
|
|
|
|
enc.offset(buf.len());
|
|
|
|
buf.extend(plain);
|
|
|
|
let ciphertext = enc.encrypt(&nonce, buf).ok()?;
|
|
|
|
Some(ciphertext)
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EncryptionInner::ECIES(valid_public) => {
|
2018-05-05 11:02:33 +02:00
|
|
|
ecies::encrypt(&valid_public, &[], plain).ok()
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:57:12 +02:00
|
|
|
#[inline]
|
|
|
|
fn xor(a: &mut [u8; 32], b: &[u8; 32]) {
|
|
|
|
for i in 0 .. 32 {
|
|
|
|
a[i] ^= b[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 20:40:28 +02:00
|
|
|
enum AesExtract {
|
2018-04-11 13:57:12 +02:00
|
|
|
AppendedNonce(Memzero<[u8; AES_KEY_LEN]>), // extract appended nonce.
|
2017-07-14 20:40:28 +02:00
|
|
|
OnTopics(usize, usize, H256), // number of topics, index we know, topic we know.
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DecryptionInner {
|
|
|
|
AES(AesExtract),
|
|
|
|
ECIES(Secret),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decryption instance good for single usage.
|
|
|
|
pub struct DecryptionInstance(DecryptionInner);
|
|
|
|
|
|
|
|
impl DecryptionInstance {
|
|
|
|
/// ECIES decryption using secret key. Fails if invalid secret.
|
|
|
|
pub fn ecies(secret: Secret) -> Result<Self, &'static str> {
|
|
|
|
secret.check_validity().map_err(|_| "Invalid secret key")?;
|
|
|
|
|
|
|
|
Ok(DecryptionInstance(DecryptionInner::ECIES(secret)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 256-bit AES GCM decryption with appended nonce.
|
2018-04-11 13:57:12 +02:00
|
|
|
pub fn aes(key: Memzero<[u8; AES_KEY_LEN]>) -> Self {
|
2017-07-14 20:40:28 +02:00
|
|
|
DecryptionInstance(DecryptionInner::AES(AesExtract::AppendedNonce(key)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode broadcast based on number of topics and known topic.
|
|
|
|
/// Known topic index may not be larger than num topics - 1.
|
|
|
|
pub fn broadcast(num_topics: usize, topic_idx: usize, known_topic: H256) -> Result<Self, &'static str> {
|
|
|
|
if topic_idx >= num_topics { return Err("topic index out of bounds") }
|
|
|
|
|
|
|
|
Ok(DecryptionInstance(DecryptionInner::AES(AesExtract::OnTopics(num_topics, topic_idx, known_topic))))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decrypt ciphertext. Fails if it's an invalid message.
|
|
|
|
pub fn decrypt(self, ciphertext: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
match self.0 {
|
|
|
|
DecryptionInner::AES(extract) => {
|
|
|
|
match extract {
|
|
|
|
AesExtract::AppendedNonce(key) => {
|
2018-05-05 11:02:33 +02:00
|
|
|
if ciphertext.len() < AES_NONCE_LEN {
|
|
|
|
return None
|
|
|
|
}
|
2017-07-14 20:40:28 +02:00
|
|
|
// nonce is the suffix of ciphertext.
|
|
|
|
let mut nonce = [0; AES_NONCE_LEN];
|
|
|
|
let nonce_offset = ciphertext.len() - AES_NONCE_LEN;
|
|
|
|
nonce.copy_from_slice(&ciphertext[nonce_offset..]);
|
2018-05-05 11:02:33 +02:00
|
|
|
Decryptor::aes_256_gcm(&*key).ok()?
|
|
|
|
.decrypt(&nonce, Vec::from(&ciphertext[..nonce_offset]))
|
|
|
|
.ok()
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
AesExtract::OnTopics(num_topics, known_index, known_topic) => {
|
2018-05-05 11:02:33 +02:00
|
|
|
if ciphertext.len() < num_topics * 32 {
|
|
|
|
return None
|
|
|
|
}
|
2017-07-14 20:40:28 +02:00
|
|
|
let mut salted_topic = H256::new();
|
|
|
|
salted_topic.copy_from_slice(&ciphertext[(known_index * 32)..][..32]);
|
2018-04-11 13:57:12 +02:00
|
|
|
let key = Memzero::from((salted_topic ^ known_topic).0);
|
2017-07-14 20:40:28 +02:00
|
|
|
let offset = num_topics * 32;
|
2018-05-05 11:02:33 +02:00
|
|
|
Decryptor::aes_256_gcm(&*key).ok()?
|
|
|
|
.decrypt(&BROADCAST_IV, Vec::from(&ciphertext[offset..]))
|
|
|
|
.ok()
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DecryptionInner::ECIES(secret) => {
|
|
|
|
// secret is checked for validity, so only fails on invalid message.
|
2018-05-05 11:02:33 +02:00
|
|
|
ecies::decrypt(&secret, &[], ciphertext).ok()
|
2017-07-14 20:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encrypt_asymmetric() {
|
|
|
|
use ethkey::{Generator, Random};
|
|
|
|
|
|
|
|
let key_pair = Random.generate().unwrap();
|
|
|
|
let test_message = move |message: &[u8]| {
|
|
|
|
let instance = EncryptionInstance::ecies(key_pair.public().clone()).unwrap();
|
2018-05-05 11:02:33 +02:00
|
|
|
let ciphertext = instance.encrypt(&message).unwrap();
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
if !message.is_empty() {
|
|
|
|
assert!(&ciphertext[..message.len()] != message)
|
|
|
|
}
|
|
|
|
|
|
|
|
let instance = DecryptionInstance::ecies(key_pair.secret().clone()).unwrap();
|
|
|
|
let decrypted = instance.decrypt(&ciphertext).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(message, &decrypted[..])
|
|
|
|
};
|
|
|
|
|
|
|
|
test_message(&[1, 2, 3, 4, 5]);
|
|
|
|
test_message(&[]);
|
|
|
|
test_message(&[255; 512]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encrypt_symmetric() {
|
|
|
|
use rand::{Rng, OsRng};
|
|
|
|
|
|
|
|
let mut rng = OsRng::new().unwrap();
|
|
|
|
let mut test_message = move |message: &[u8]| {
|
2018-04-11 13:57:12 +02:00
|
|
|
let key = Memzero::from(rng.gen::<[u8; 32]>());
|
2017-07-14 20:40:28 +02:00
|
|
|
|
2018-04-11 13:57:12 +02:00
|
|
|
let instance = EncryptionInstance::aes(key.clone(), rng.gen());
|
2018-05-05 11:02:33 +02:00
|
|
|
let ciphertext = instance.encrypt(message).unwrap();
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
if !message.is_empty() {
|
|
|
|
assert!(&ciphertext[..message.len()] != message)
|
|
|
|
}
|
|
|
|
|
|
|
|
let instance = DecryptionInstance::aes(key);
|
|
|
|
let decrypted = instance.decrypt(&ciphertext).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(message, &decrypted[..])
|
|
|
|
};
|
|
|
|
|
|
|
|
test_message(&[1, 2, 3, 4, 5]);
|
|
|
|
test_message(&[]);
|
|
|
|
test_message(&[255; 512]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encrypt_broadcast() {
|
|
|
|
use rand::{Rng, OsRng};
|
|
|
|
|
|
|
|
let mut rng = OsRng::new().unwrap();
|
|
|
|
|
|
|
|
let mut test_message = move |message: &[u8]| {
|
|
|
|
let all_topics = (0..5).map(|_| rng.gen()).collect::<Vec<_>>();
|
|
|
|
let known_idx = 2;
|
|
|
|
let known_topic = all_topics[2];
|
2018-04-11 13:57:12 +02:00
|
|
|
let key = Memzero::from(rng.gen::<[u8; 32]>());
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
let instance = EncryptionInstance::broadcast(key, all_topics);
|
2018-05-05 11:02:33 +02:00
|
|
|
let ciphertext = instance.encrypt(message).unwrap();
|
2017-07-14 20:40:28 +02:00
|
|
|
|
|
|
|
if !message.is_empty() {
|
|
|
|
assert!(&ciphertext[..message.len()] != message)
|
|
|
|
}
|
|
|
|
|
|
|
|
let instance = DecryptionInstance::broadcast(5, known_idx, known_topic).unwrap();
|
|
|
|
|
|
|
|
let decrypted = instance.decrypt(&ciphertext).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(message, &decrypted[..])
|
|
|
|
};
|
|
|
|
|
|
|
|
test_message(&[1, 2, 3, 4, 5]);
|
|
|
|
test_message(&[]);
|
|
|
|
test_message(&[255; 512]);
|
|
|
|
}
|
|
|
|
}
|