2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (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/>.
|
|
|
|
|
2016-02-01 15:22:42 +01:00
|
|
|
//! Ethcore crypto.
|
|
|
|
|
2016-07-25 20:19:33 +02:00
|
|
|
use bigint::uint::*;
|
|
|
|
use bigint::hash::*;
|
2016-01-12 17:40:55 +01:00
|
|
|
use bytes::*;
|
2016-01-08 13:32:52 +01:00
|
|
|
use secp256k1::{key, Secp256k1};
|
2015-11-29 02:11:56 +01:00
|
|
|
use rand::os::OsRng;
|
2016-03-22 17:17:50 +01:00
|
|
|
use sha3::Hashable;
|
2016-05-21 00:12:51 +02:00
|
|
|
use std::fmt;
|
2015-11-29 02:11:56 +01:00
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Secret key for secp256k1 EC operations. 256 bit generic "hash" data.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub type Secret = H256;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Public key for secp256k1 EC operations. 512 bit generic "hash" data.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub type Public = H512;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Signature for secp256k1 EC operations; encodes two 256-bit curve points
|
|
|
|
/// and a third sign bit. 520 bit generic "hash" data.
|
2016-01-07 23:59:50 +01:00
|
|
|
pub type Signature = H520;
|
|
|
|
|
2016-01-17 14:09:42 +01:00
|
|
|
lazy_static! {
|
|
|
|
static ref SECP256K1: Secp256k1 = Secp256k1::new();
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:05:17 +02:00
|
|
|
/// Create a new signature from the R, S and V componenets.
|
|
|
|
pub fn signature_from_rsv(r: &H256, s: &H256, v: u8) -> Signature {
|
|
|
|
let mut ret: Signature = Signature::new();
|
|
|
|
(&mut ret[0..32]).copy_from_slice(r);
|
|
|
|
(&mut ret[32..64]).copy_from_slice(s);
|
|
|
|
|
|
|
|
ret[64] = v;
|
|
|
|
ret
|
|
|
|
}
|
2016-01-12 17:40:55 +01:00
|
|
|
|
2016-08-03 18:05:17 +02:00
|
|
|
/// Convert transaction to R, S and V components.
|
|
|
|
pub fn signature_to_rsv(s: &Signature) -> (U256, U256, u8) {
|
|
|
|
(U256::from(&s.as_slice()[0..32]), U256::from(&s.as_slice()[32..64]), s[64])
|
2016-01-07 23:59:50 +01:00
|
|
|
}
|
2015-11-29 02:11:56 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Crypto error
|
2015-11-29 02:11:56 +01:00
|
|
|
pub enum CryptoError {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Invalid secret key
|
2015-11-29 02:11:56 +01:00
|
|
|
InvalidSecret,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Invalid public key
|
2015-11-29 02:11:56 +01:00
|
|
|
InvalidPublic,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Invalid EC signature
|
2015-11-29 02:11:56 +01:00
|
|
|
InvalidSignature,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Invalid AES message
|
2015-11-29 02:11:56 +01:00
|
|
|
InvalidMessage,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// IO Error
|
2015-11-29 02:11:56 +01:00
|
|
|
Io(::std::io::Error),
|
|
|
|
}
|
|
|
|
|
2016-05-21 00:12:51 +02:00
|
|
|
impl fmt::Display for CryptoError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let msg = match *self {
|
|
|
|
CryptoError::InvalidSecret => "Invalid secret key".into(),
|
|
|
|
CryptoError::InvalidPublic => "Invalid public key".into(),
|
|
|
|
CryptoError::InvalidSignature => "Invalid EC signature".into(),
|
|
|
|
CryptoError::InvalidMessage => "Invalid AES message".into(),
|
|
|
|
CryptoError::Io(ref err) => format!("I/O error: {}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
f.write_fmt(format_args!("Crypto error ({})", msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
impl From<::secp256k1::Error> for CryptoError {
|
|
|
|
fn from(e: ::secp256k1::Error) -> CryptoError {
|
|
|
|
match e {
|
|
|
|
::secp256k1::Error::InvalidMessage => CryptoError::InvalidMessage,
|
|
|
|
::secp256k1::Error::InvalidPublicKey => CryptoError::InvalidPublic,
|
|
|
|
::secp256k1::Error::InvalidSecretKey => CryptoError::InvalidSecret,
|
2016-01-12 00:55:42 +01:00
|
|
|
_ => CryptoError::InvalidSignature,
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<::std::io::Error> for CryptoError {
|
|
|
|
fn from(err: ::std::io::Error) -> CryptoError {
|
|
|
|
CryptoError::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
/// secp256k1 Key pair
|
|
|
|
///
|
2015-12-03 15:11:40 +01:00
|
|
|
/// Use `create()` to create a new random key pair.
|
|
|
|
///
|
2015-11-29 02:11:56 +01:00
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util;
|
|
|
|
/// use ethcore_util::crypto::*;
|
|
|
|
/// use ethcore_util::hash::*;
|
|
|
|
/// fn main() {
|
|
|
|
/// let pair = KeyPair::create().unwrap();
|
|
|
|
/// let message = H256::random();
|
2015-12-02 12:07:46 +01:00
|
|
|
/// let signature = ec::sign(pair.secret(), &message).unwrap();
|
2015-11-29 02:11:56 +01:00
|
|
|
///
|
2015-12-02 12:07:46 +01:00
|
|
|
/// assert!(ec::verify(pair.public(), &signature, &message).unwrap());
|
|
|
|
/// assert_eq!(ec::recover(&signature, &message).unwrap(), *pair.public());
|
2015-11-29 02:11:56 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub struct KeyPair {
|
|
|
|
secret: Secret,
|
|
|
|
public: Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyPair {
|
|
|
|
/// Create a pair from secret key
|
|
|
|
pub fn from_secret(secret: Secret) -> Result<KeyPair, CryptoError> {
|
2016-01-17 14:09:42 +01:00
|
|
|
let context = &SECP256K1;
|
|
|
|
let s: key::SecretKey = try!(key::SecretKey::from_slice(context, &secret));
|
|
|
|
let pub_key = try!(key::PublicKey::from_secret_key(context, &s));
|
|
|
|
let serialized = pub_key.serialize_vec(context, false);
|
2015-11-29 02:11:56 +01:00
|
|
|
let p: Public = Public::from_slice(&serialized[1..65]);
|
|
|
|
Ok(KeyPair {
|
|
|
|
secret: secret,
|
|
|
|
public: p,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/// Create a new random key pair
|
|
|
|
pub fn create() -> Result<KeyPair, CryptoError> {
|
2016-01-17 14:09:42 +01:00
|
|
|
let context = &SECP256K1;
|
2015-11-29 02:11:56 +01:00
|
|
|
let mut rng = try!(OsRng::new());
|
|
|
|
let (sec, publ) = try!(context.generate_keypair(&mut rng));
|
2016-01-17 14:09:42 +01:00
|
|
|
let serialized = publ.serialize_vec(context, false);
|
2015-11-29 02:11:56 +01:00
|
|
|
let p: Public = Public::from_slice(&serialized[1..65]);
|
2016-05-28 21:48:42 +02:00
|
|
|
|
|
|
|
let mut s = Secret::new();
|
|
|
|
s.copy_from_slice(&sec[0..32]);
|
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
Ok(KeyPair {
|
|
|
|
secret: s,
|
|
|
|
public: p,
|
|
|
|
})
|
|
|
|
}
|
2016-03-22 17:17:50 +01:00
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
/// Returns public key
|
|
|
|
pub fn public(&self) -> &Public {
|
|
|
|
&self.public
|
|
|
|
}
|
2016-03-22 17:17:50 +01:00
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
/// Returns private key
|
|
|
|
pub fn secret(&self) -> &Secret {
|
|
|
|
&self.secret
|
|
|
|
}
|
2016-01-07 23:59:50 +01:00
|
|
|
|
2016-03-22 17:17:50 +01:00
|
|
|
/// Returns address.
|
|
|
|
pub fn address(&self) -> Address {
|
|
|
|
Address::from(self.public.sha3())
|
|
|
|
}
|
|
|
|
|
2016-01-07 23:59:50 +01:00
|
|
|
/// Sign a message with our secret key.
|
2016-01-08 13:32:52 +01:00
|
|
|
pub fn sign(&self, message: &H256) -> Result<Signature, CryptoError> { ec::sign(&self.secret, message) }
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// EC functions
|
2016-04-06 10:07:24 +02:00
|
|
|
#[cfg_attr(feature="dev", allow(similar_names))]
|
2015-11-30 16:38:55 +01:00
|
|
|
pub mod ec {
|
2016-07-25 20:19:33 +02:00
|
|
|
use bigint::hash::*;
|
|
|
|
use bigint::uint::*;
|
2016-01-12 23:44:30 +01:00
|
|
|
use standard::*;
|
2015-11-30 16:38:55 +01:00
|
|
|
use crypto::*;
|
2016-01-08 13:32:52 +01:00
|
|
|
use crypto::{self};
|
2015-11-30 16:38:55 +01:00
|
|
|
|
|
|
|
/// Recovers Public key from signed message hash.
|
|
|
|
pub fn recover(signature: &Signature, message: &H256) -> Result<Public, CryptoError> {
|
|
|
|
use secp256k1::*;
|
2016-01-17 14:09:42 +01:00
|
|
|
let context = &crypto::SECP256K1;
|
|
|
|
let rsig = try!(RecoverableSignature::from_compact(context, &signature[0..64], try!(RecoveryId::from_i32(signature[64] as i32))));
|
2015-11-30 16:38:55 +01:00
|
|
|
let publ = try!(context.recover(&try!(Message::from_slice(&message)), &rsig));
|
2016-01-17 14:09:42 +01:00
|
|
|
let serialized = publ.serialize_vec(context, false);
|
2015-11-30 16:38:55 +01:00
|
|
|
let p: Public = Public::from_slice(&serialized[1..65]);
|
2016-01-08 13:32:52 +01:00
|
|
|
//TODO: check if it's the zero key and fail if so.
|
2015-11-30 16:38:55 +01:00
|
|
|
Ok(p)
|
|
|
|
}
|
|
|
|
/// Returns siganture of message hash.
|
|
|
|
pub fn sign(secret: &Secret, message: &H256) -> Result<Signature, CryptoError> {
|
2016-01-12 23:44:30 +01:00
|
|
|
// TODO: allow creation of only low-s signatures.
|
2016-05-28 21:48:42 +02:00
|
|
|
use secp256k1::{Message, key};
|
|
|
|
|
2016-01-17 14:09:42 +01:00
|
|
|
let context = &crypto::SECP256K1;
|
2016-05-28 21:48:42 +02:00
|
|
|
// no way to create from raw byte array.
|
2015-11-30 16:38:55 +01:00
|
|
|
let sec: &key::SecretKey = unsafe { ::std::mem::transmute(secret) };
|
|
|
|
let s = try!(context.sign_recoverable(&try!(Message::from_slice(&message)), sec));
|
2016-01-17 14:09:42 +01:00
|
|
|
let (rec_id, data) = s.serialize_compact(context);
|
2016-05-28 21:48:42 +02:00
|
|
|
let mut signature = crypto::Signature::new();
|
2015-11-30 16:38:55 +01:00
|
|
|
signature.clone_from_slice(&data);
|
|
|
|
signature[64] = rec_id.to_i32() as u8;
|
2016-01-18 23:46:33 +01:00
|
|
|
|
2016-08-03 18:05:17 +02:00
|
|
|
let (_, s, v) = signature_to_rsv(&signature);
|
2016-01-18 23:46:33 +01:00
|
|
|
let secp256k1n = U256::from_str("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141").unwrap();
|
|
|
|
if !is_low_s(&s) {
|
2016-08-03 18:05:17 +02:00
|
|
|
signature = super::signature_from_rsv(&H256::from_slice(&signature[0..32]), &H256::from(secp256k1n - s), v ^ 1);
|
2016-01-18 23:46:33 +01:00
|
|
|
}
|
2015-11-30 16:38:55 +01:00
|
|
|
Ok(signature)
|
|
|
|
}
|
2016-04-06 10:07:24 +02:00
|
|
|
|
2015-11-30 16:38:55 +01:00
|
|
|
/// Verify signature.
|
|
|
|
pub fn verify(public: &Public, signature: &Signature, message: &H256) -> Result<bool, CryptoError> {
|
|
|
|
use secp256k1::*;
|
2016-01-17 14:09:42 +01:00
|
|
|
let context = &crypto::SECP256K1;
|
|
|
|
let rsig = try!(RecoverableSignature::from_compact(context, &signature[0..64], try!(RecoveryId::from_i32(signature[64] as i32))));
|
|
|
|
let sig = rsig.to_standard(context);
|
2015-11-30 16:38:55 +01:00
|
|
|
|
2016-05-28 21:48:42 +02:00
|
|
|
let pdata: [u8; 65] = {
|
|
|
|
let mut temp = [4u8; 65];
|
|
|
|
(&mut temp[1..65]).copy_from_slice(public);
|
|
|
|
temp
|
|
|
|
};
|
|
|
|
|
2016-01-17 14:09:42 +01:00
|
|
|
let publ = try!(key::PublicKey::from_slice(context, &pdata));
|
2015-11-30 16:38:55 +01:00
|
|
|
match context.verify(&try!(Message::from_slice(&message)), &sig, &publ) {
|
|
|
|
Ok(_) => Ok(true),
|
|
|
|
Err(Error::IncorrectSignature) => Ok(false),
|
2016-02-16 10:42:56 +01:00
|
|
|
Err(x) => Err(CryptoError::from(x))
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 13:32:52 +01:00
|
|
|
|
2016-01-12 23:44:30 +01:00
|
|
|
/// Check if this is a "low" signature.
|
|
|
|
pub fn is_low(sig: &Signature) -> bool {
|
2016-01-18 23:46:33 +01:00
|
|
|
H256::from_slice(&sig[32..64]) <= h256_from_hex("7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0")
|
2016-01-12 23:44:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if this is a "low" signature.
|
|
|
|
pub fn is_low_s(s: &U256) -> bool {
|
|
|
|
s <= &U256::from_str("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0").unwrap()
|
|
|
|
}
|
|
|
|
|
2016-01-08 13:32:52 +01:00
|
|
|
/// Check if each component of the signature is in range.
|
|
|
|
pub fn is_valid(sig: &Signature) -> bool {
|
|
|
|
sig[64] <= 1 &&
|
|
|
|
H256::from_slice(&sig[0..32]) < h256_from_hex("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141") &&
|
|
|
|
H256::from_slice(&sig[32..64]) < h256_from_hex("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141") &&
|
|
|
|
H256::from_slice(&sig[32..64]) >= h256_from_u64(1) &&
|
|
|
|
H256::from_slice(&sig[0..32]) >= h256_from_u64(1)
|
|
|
|
}
|
2015-11-30 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// ECDH functions
|
2016-04-06 10:07:24 +02:00
|
|
|
#[cfg_attr(feature="dev", allow(similar_names))]
|
2015-11-30 16:38:55 +01:00
|
|
|
pub mod ecdh {
|
2016-05-28 21:48:42 +02:00
|
|
|
use hash::FixedHash;
|
|
|
|
use crypto::{self, Secret, Public, CryptoError};
|
2015-11-30 16:38:55 +01:00
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Agree on a shared secret
|
2016-05-28 21:48:42 +02:00
|
|
|
pub fn agree(secret: &Secret, public: &Public) -> Result<Secret, CryptoError> {
|
|
|
|
use secp256k1::{ecdh, key};
|
|
|
|
|
2016-01-17 14:09:42 +01:00
|
|
|
let context = &crypto::SECP256K1;
|
2016-05-28 21:48:42 +02:00
|
|
|
let pdata = {
|
|
|
|
let mut temp = [4u8; 65];
|
|
|
|
(&mut temp[1..65]).copy_from_slice(&public[0..64]);
|
|
|
|
temp
|
|
|
|
};
|
|
|
|
|
2016-01-17 14:09:42 +01:00
|
|
|
let publ = try!(key::PublicKey::from_slice(context, &pdata));
|
2016-05-28 21:48:42 +02:00
|
|
|
// no way to create SecretKey from raw byte array.
|
2015-11-30 16:38:55 +01:00
|
|
|
let sec: &key::SecretKey = unsafe { ::std::mem::transmute(secret) };
|
2016-07-26 20:31:25 +02:00
|
|
|
let shared = ecdh::SharedSecret::new_raw(context, &publ, sec);
|
2016-05-28 21:48:42 +02:00
|
|
|
|
|
|
|
let mut s = crypto::Secret::new();
|
|
|
|
s.copy_from_slice(&shared[0..32]);
|
2015-11-30 16:38:55 +01:00
|
|
|
Ok(s)
|
|
|
|
}
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
2015-11-30 16:38:55 +01:00
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// ECIES function
|
2016-04-06 10:07:24 +02:00
|
|
|
#[cfg_attr(feature="dev", allow(similar_names))]
|
2015-11-30 16:38:55 +01:00
|
|
|
pub mod ecies {
|
|
|
|
use hash::*;
|
|
|
|
use bytes::*;
|
|
|
|
use crypto::*;
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Encrypt a message with a public key
|
2016-02-22 23:05:27 +01:00
|
|
|
pub fn encrypt(public: &Public, shared_mac: &[u8], plain: &[u8]) -> Result<Bytes, CryptoError> {
|
2015-11-30 16:38:55 +01:00
|
|
|
use ::rcrypto::digest::Digest;
|
|
|
|
use ::rcrypto::sha2::Sha256;
|
|
|
|
use ::rcrypto::hmac::Hmac;
|
|
|
|
use ::rcrypto::mac::Mac;
|
|
|
|
let r = try!(KeyPair::create());
|
|
|
|
let z = try!(ecdh::agree(r.secret(), public));
|
|
|
|
let mut key = [0u8; 32];
|
|
|
|
let mut mkey = [0u8; 32];
|
|
|
|
kdf(&z, &[0u8; 0], &mut key);
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
let mkey_material = &key[16..32];
|
|
|
|
hasher.input(mkey_material);
|
|
|
|
hasher.result(&mut mkey);
|
|
|
|
let ekey = &key[0..16];
|
|
|
|
|
|
|
|
let mut msg = vec![0u8; (1 + 64 + 16 + plain.len() + 32)];
|
|
|
|
msg[0] = 0x04u8;
|
|
|
|
{
|
|
|
|
let msgd = &mut msg[1..];
|
|
|
|
r.public().copy_to(&mut msgd[0..64]);
|
|
|
|
{
|
|
|
|
let cipher = &mut msgd[(64 + 16)..(64 + 16 + plain.len())];
|
|
|
|
aes::encrypt(ekey, &H128::new(), plain, cipher);
|
|
|
|
}
|
|
|
|
let mut hmac = Hmac::new(Sha256::new(), &mkey);
|
|
|
|
{
|
|
|
|
let cipher_iv = &msgd[64..(64 + 16 + plain.len())];
|
|
|
|
hmac.input(cipher_iv);
|
|
|
|
}
|
2016-02-22 23:05:27 +01:00
|
|
|
hmac.input(shared_mac);
|
2015-11-30 16:38:55 +01:00
|
|
|
hmac.raw_result(&mut msgd[(64 + 16 + plain.len())..]);
|
|
|
|
}
|
|
|
|
Ok(msg)
|
|
|
|
}
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Decrypt a message with a secret key
|
2016-02-22 23:05:27 +01:00
|
|
|
pub fn decrypt(secret: &Secret, shared_mac: &[u8], encrypted: &[u8]) -> Result<Bytes, CryptoError> {
|
2015-12-02 12:07:46 +01:00
|
|
|
use ::rcrypto::digest::Digest;
|
|
|
|
use ::rcrypto::sha2::Sha256;
|
|
|
|
use ::rcrypto::hmac::Hmac;
|
|
|
|
use ::rcrypto::mac::Mac;
|
|
|
|
|
2015-12-03 15:11:40 +01:00
|
|
|
let meta_len = 1 + 64 + 16 + 32;
|
2015-12-02 12:07:46 +01:00
|
|
|
if encrypted.len() < meta_len || encrypted[0] < 2 || encrypted[0] > 4 {
|
|
|
|
return Err(CryptoError::InvalidMessage); //invalid message: publickey
|
|
|
|
}
|
2015-12-03 15:11:40 +01:00
|
|
|
|
2015-12-02 12:07:46 +01:00
|
|
|
let e = &encrypted[1..];
|
|
|
|
let p = Public::from_slice(&e[0..64]);
|
|
|
|
let z = try!(ecdh::agree(secret, &p));
|
|
|
|
let mut key = [0u8; 32];
|
|
|
|
kdf(&z, &[0u8; 0], &mut key);
|
|
|
|
let ekey = &key[0..16];
|
|
|
|
let mkey_material = &key[16..32];
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
let mut mkey = [0u8; 32];
|
|
|
|
hasher.input(mkey_material);
|
|
|
|
hasher.result(&mut mkey);
|
|
|
|
|
|
|
|
let clen = encrypted.len() - meta_len;
|
2015-12-03 15:11:40 +01:00
|
|
|
let cipher_with_iv = &e[64..(64+16+clen)];
|
|
|
|
let cipher_iv = &cipher_with_iv[0..16];
|
|
|
|
let cipher_no_iv = &cipher_with_iv[16..];
|
2015-12-02 12:07:46 +01:00
|
|
|
let msg_mac = &e[(64+16+clen)..];
|
|
|
|
|
|
|
|
// Verify tag
|
|
|
|
let mut hmac = Hmac::new(Sha256::new(), &mkey);
|
2015-12-03 15:11:40 +01:00
|
|
|
hmac.input(cipher_with_iv);
|
2016-02-22 23:05:27 +01:00
|
|
|
hmac.input(shared_mac);
|
2015-12-02 12:07:46 +01:00
|
|
|
let mut mac = H256::new();
|
|
|
|
hmac.raw_result(&mut mac);
|
|
|
|
if &mac[..] != msg_mac {
|
|
|
|
return Err(CryptoError::InvalidMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut msg = vec![0u8; clen];
|
2015-12-03 15:11:40 +01:00
|
|
|
aes::decrypt(ekey, cipher_iv, cipher_no_iv, &mut msg[..]);
|
2015-12-02 12:07:46 +01:00
|
|
|
Ok(msg)
|
|
|
|
}
|
|
|
|
|
2015-11-30 16:38:55 +01:00
|
|
|
fn kdf(secret: &Secret, s1: &[u8], dest: &mut [u8]) {
|
|
|
|
use ::rcrypto::digest::Digest;
|
|
|
|
use ::rcrypto::sha2::Sha256;
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
// SEC/ISO/Shoup specify counter size SHOULD be equivalent
|
|
|
|
// to size of hash output, however, it also notes that
|
|
|
|
// the 4 bytes is okay. NIST specifies 4 bytes.
|
|
|
|
let mut ctr = 1u32;
|
|
|
|
let mut written = 0usize;
|
|
|
|
while written < dest.len() {
|
|
|
|
let ctrs = [(ctr >> 24) as u8, (ctr >> 16) as u8, (ctr >> 8) as u8, ctr as u8];
|
|
|
|
hasher.input(&ctrs);
|
|
|
|
hasher.input(secret);
|
|
|
|
hasher.input(s1);
|
|
|
|
hasher.result(&mut dest[written..(written + 32)]);
|
|
|
|
hasher.reset();
|
|
|
|
written += 32;
|
|
|
|
ctr += 1;
|
|
|
|
}
|
|
|
|
}
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
2015-11-30 16:38:55 +01:00
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// AES encryption
|
2015-11-30 16:38:55 +01:00
|
|
|
pub mod aes {
|
|
|
|
use ::rcrypto::blockmodes::*;
|
|
|
|
use ::rcrypto::aessafe::*;
|
|
|
|
use ::rcrypto::symmetriccipher::*;
|
|
|
|
use ::rcrypto::buffer::*;
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Encrypt a message
|
2015-12-03 15:11:40 +01:00
|
|
|
pub fn encrypt(k: &[u8], iv: &[u8], plain: &[u8], dest: &mut [u8]) {
|
|
|
|
let mut encryptor = CtrMode::new(AesSafe128Encryptor::new(k), iv.to_vec());
|
2015-11-30 16:38:55 +01:00
|
|
|
encryptor.encrypt(&mut RefReadBuffer::new(plain), &mut RefWriteBuffer::new(dest), true).expect("Invalid length or padding");
|
|
|
|
}
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Decrypt a message
|
2015-12-03 15:11:40 +01:00
|
|
|
pub fn decrypt(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) {
|
|
|
|
let mut encryptor = CtrMode::new(AesSafe128Encryptor::new(k), iv.to_vec());
|
2015-11-30 16:38:55 +01:00
|
|
|
encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut RefWriteBuffer::new(dest), true).expect("Invalid length or padding");
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 16:38:55 +01:00
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use hash::*;
|
|
|
|
use crypto::*;
|
|
|
|
|
2016-01-07 23:59:50 +01:00
|
|
|
// TODO: tests for sign/recover roundtrip, at least.
|
|
|
|
|
2015-11-29 02:11:56 +01:00
|
|
|
#[test]
|
|
|
|
fn test_signature() {
|
|
|
|
let pair = KeyPair::create().unwrap();
|
|
|
|
let message = H256::random();
|
2015-12-02 12:07:46 +01:00
|
|
|
let signature = ec::sign(pair.secret(), &message).unwrap();
|
2015-11-29 02:11:56 +01:00
|
|
|
|
2015-12-02 12:07:46 +01:00
|
|
|
assert!(ec::verify(pair.public(), &signature, &message).unwrap());
|
|
|
|
assert_eq!(ec::recover(&signature, &message).unwrap(), *pair.public());
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_key() {
|
2016-01-07 23:59:50 +01:00
|
|
|
assert!(KeyPair::from_secret(h256_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")).is_err());
|
|
|
|
assert!(KeyPair::from_secret(h256_from_hex("0000000000000000000000000000000000000000000000000000000000000000")).is_err());
|
|
|
|
assert!(KeyPair::from_secret(h256_from_hex("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141")).is_err());
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_key() {
|
2016-01-07 23:59:50 +01:00
|
|
|
let pair = KeyPair::from_secret(h256_from_hex("6f7b0d801bc7b5ce7bbd930b84fd0369b3eb25d09be58d64ba811091046f3aa2")).unwrap();
|
2015-11-29 02:11:56 +01:00
|
|
|
assert_eq!(pair.public().hex(), "101b3ef5a4ea7a1c7928e24c4c75fd053c235d7b80c22ae5c03d145d0ac7396e2a4ffff9adee3133a7b05044a5cee08115fd65145e5165d646bde371010d803c");
|
|
|
|
}
|
2016-02-22 23:05:27 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ecies_shared() {
|
|
|
|
let kp = KeyPair::create().unwrap();
|
|
|
|
let message = b"So many books, so little time";
|
|
|
|
|
|
|
|
let shared = b"shared";
|
|
|
|
let wrong_shared = b"incorrect";
|
|
|
|
let encrypted = ecies::encrypt(kp.public(), shared, message).unwrap();
|
|
|
|
assert!(encrypted[..] != message[..]);
|
|
|
|
assert_eq!(encrypted[0], 0x04);
|
|
|
|
|
|
|
|
assert!(ecies::decrypt(kp.secret(), wrong_shared, &encrypted).is_err());
|
|
|
|
let decrypted = ecies::decrypt(kp.secret(), shared, &encrypted).unwrap();
|
|
|
|
assert_eq!(decrypted[..message.len()], message[..]);
|
|
|
|
}
|
2015-11-29 02:11:56 +01:00
|
|
|
}
|