-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic process in encrypt_public_key
- Loading branch information
1 parent
362883b
commit c493c1e
Showing
7 changed files
with
90 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters