Skip to content

Commit

Permalink
add basic process in encrypt_public_key
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed May 28, 2019
1 parent 362883b commit c493c1e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
serde = "1.0"
bincode = "1.0"
byteorder = "1"
sha3 = "0.8"
rand = "0.6"
aes-soft = "0.3"
79 changes: 68 additions & 11 deletions src/encrypt_public_key.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
use crate::traits::{HashAlgorithm, PublicKeyAlgorithm, SymmetricAlgorithm};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use rand::Rng;
use std::io::Cursor;

pub fn encrypt<T: PublicKeyAlgorithm, S: SymmetricAlgorithm, H: HashAlgorithm>(
plaintext: Vec<u8>,
receiver_pk: &T::PublicKey,
self_sk: &T::SecretKey,
use crate::traits::{HashAlgorithm, PublicKeyAlgorithm, SignatureAlgorithm, SymmetricAlgorithm};

pub fn encrypt<
P: PublicKeyAlgorithm,
S: SymmetricAlgorithm,
H: HashAlgorithm,
I: SignatureAlgorithm,
>(
mut plaintext: Vec<u8>,
receiver_pk: &P::PublicKey,
self_sk: &I::SignKey,
) -> Result<Vec<u8>, ()> {
Err(())
let hash_data = H::hash(&plaintext[..]);
let mut signature = I::sign(&hash_data, self_sk);
plaintext.append(&mut signature);

// TODO zip plaintext

let session_bytes: Vec<u8> = (0..S::KEY_LENGTH)
.map(|_| rand::thread_rng().gen::<u8>())
.collect();
let session_key = S::from_bytes(&session_bytes[..]);
let mut ciphertext = S::encrypt(&plaintext[..], &session_key);
let mut cek = P::encrypt(&session_bytes[..], receiver_pk);

let mut last_data = vec![];

let mut wtr = vec![];
wtr.write_u32::<BigEndian>(cek.len() as u32).unwrap_or(());

last_data.append(&mut wtr);
last_data.append(&mut cek);
last_data.append(&mut ciphertext);

// TODO ASCII radix-64

Ok(last_data)
}

pub fn decrypt<T: PublicKeyAlgorithm, S: SymmetricAlgorithm, H: HashAlgorithm>(
ciphertext: Vec<u8>,
sender_pk: &T::PublicKey,
self_sk: &T::SecretKey,
pub fn decrypt<
P: PublicKeyAlgorithm,
S: SymmetricAlgorithm,
H: HashAlgorithm,
I: SignatureAlgorithm,
>(
mut data: Vec<u8>,
sender_vk: &I::VerifyKey,
self_sk: &P::SecretKey,
) -> Result<Vec<u8>, ()> {
Err(())
// TODO ASCII radix-64

let (length, cipher) = data.split_at_mut(4);
let mut rdr = Cursor::new(length);
let length = rdr.read_u32::<BigEndian>().unwrap_or(0);
let (cek, ciphertext) = cipher.split_at_mut(length as usize);
let session_bytes = P::decrypt(cek, self_sk);
let session_key = S::from_bytes(&session_bytes[..]);
let mut plaintext = S::decrypt(ciphertext, &session_key);

// TODO unzip

let (signature, plaintext) = plaintext.split_at_mut(I::SIGNATURE_LENGTH);

let hash_data = H::hash(plaintext);
if !I::verify(&hash_data, &signature, sender_vk) {
return Err(());
}

Ok(plaintext.to_vec())
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod encrypt_dh;
pub mod encrypt_public_key;
pub mod store;
pub mod traits;
pub mod util;
18 changes: 11 additions & 7 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@ use serde::ser::Serialize;
pub trait SignatureAlgorithm {
type SignKey: Serialize + DeserializeOwned;
type VerifyKey: Serialize + DeserializeOwned;
const SIGNATURE_LENGTH: usize;

fn sign(plain: &Vec<u8>, sign_key: &Self::SignKey) -> Vec<u8>;
fn sign(plain: &[u8], sign_key: &Self::SignKey) -> Vec<u8>;

fn verify(plain: &Vec<u8>, sign: &Vec<u8>, verify_key: &Self::VerifyKey) -> bool;
fn verify(plain: &[u8], sign: &[u8], verify_key: &Self::VerifyKey) -> bool;
}

pub trait PublicKeyAlgorithm {
type PublicKey: Serialize + DeserializeOwned;
type SecretKey: Serialize + DeserializeOwned;

fn encrypt(plain: &Vec<u8>, public_key: &Self::PublicKey) -> Vec<u8>;
fn encrypt(plain: &[u8], public_key: &Self::PublicKey) -> Vec<u8>;

fn decrypt(cipher: &Vec<u8>, secret_key: &Self::SecretKey) -> Vec<u8>;
fn decrypt(cipher: &[u8], secret_key: &Self::SecretKey) -> Vec<u8>;
}

pub trait SymmetricAlgorithm {
type Key: Serialize + DeserializeOwned;
const KEY_LENGTH: usize;

fn encrypt(plain: &Vec<u8>, session_key: &Self::Key) -> Vec<u8>;
fn encrypt(plain: &[u8], session_key: &Self::Key) -> Vec<u8>;

fn decrypt(cipher: &Vec<u8>, session_key: &Self::Key) -> Vec<u8>;
fn decrypt(cipher: &[u8], session_key: &Self::Key) -> Vec<u8>;

fn from_bytes(bytes: &[u8]) -> Self::Key;
}

pub trait HashAlgorithm {
fn hash(data: Vec<u8>) -> Vec<u8>;
fn hash(data: &[u8]) -> Vec<u8>;
}
1 change: 1 addition & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 5 additions & 4 deletions tests/tmp_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ pub struct Ed25519;
impl SignatureAlgorithm for Ed25519 {
type SignKey = [u8; 32];
type VerifyKey = [u8; 32];
const SIGNATURE_LENGTH: usize = 64;

fn sign(_plain: &Vec<u8>, _sign_key: &Self::SignKey) -> Vec<u8> {
fn sign(_plain: &[u8], _sign_key: &Self::SignKey) -> Vec<u8> {
vec![]
}

fn verify(_plain: &Vec<u8>, _sign: &Vec<u8>, _verify_key: &Self::VerifyKey) -> bool {
fn verify(_plain: &[u8], _sign: &[u8], _verify_key: &Self::VerifyKey) -> bool {
true
}
}
Expand All @@ -21,11 +22,11 @@ impl PublicKeyAlgorithm for RSA {
type PublicKey = [u8; 32];
type SecretKey = [u8; 32];

fn encrypt(_plain: &Vec<u8>, _public_key: &Self::PublicKey) -> Vec<u8> {
fn encrypt(_plain: &[u8], _public_key: &Self::PublicKey) -> Vec<u8> {
vec![]
}

fn decrypt(_cipher: &Vec<u8>, _secret_key: &Self::SecretKey) -> Vec<u8> {
fn decrypt(_cipher: &[u8], _secret_key: &Self::SecretKey) -> Vec<u8> {
vec![]
}
}

0 comments on commit c493c1e

Please sign in to comment.