diff --git a/src/lib.rs b/src/lib.rs index dac2429..1c2a9c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,13 @@ +use curves::EdCurvePoint; use rug::Integer; /* - TODO: + TODO: - refactor z, z_x, z_y into message - fix tag authentication failure - update usage examples in docs - Fix benches -*/ +*/ /// Module for all EC operations. pub mod curves; @@ -20,24 +21,6 @@ pub mod sha3 { /// Module for encrypt, decrypt, and sign functions. pub mod ops; -#[derive(Debug)] -/// An object containing the necessary fields for symmetric encryptions and decryptions. -pub struct SymmetricCryptogram { - /// nonce - pub z: Vec, -} - -#[derive(Debug)] -/// An object containing the necessary fields for asymmetric encryptions and decryptions. -pub struct ECCryptogram { - /// Z_x is the x coordinate of the public nonce - pub z_x: Integer, - /// Z_y is the y coordinate of the public nonce - pub z_y: Integer, - /// c represents the ciphertext of an encryption - pub t: Vec, -} - #[derive(Debug)] /// An object containing the necessary fields for Schnorr signatures. pub struct Signature { @@ -61,10 +44,8 @@ impl Clone for Signature { pub struct KeyPair { /// String indicating the owner of the key, can be arbitrary pub owner: String, - /// Curve Point X coordinate - pub pub_x: Integer, - /// Curve Point Y coordinate - pub pub_y: Integer, + /// Public encryption key + pub pub_key: EdCurvePoint, /// value representing secret scalar, None if KeyType is PUBLIC pub priv_key: Vec, /// Date key was generated diff --git a/src/ops.rs b/src/ops.rs index bb236ad..2afed3e 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -1,5 +1,5 @@ use crate::curves::{ - order, ArbitraryPoint, EdCurvePoint, + order, EdCurvePoint, EdCurves::{self, E448}, Generator, }; @@ -12,7 +12,7 @@ use crate::sha3::{ }, sponge::{sponge_absorb, sponge_squeeze}, }; -use crate::{ECCryptogram, KeyPair, Signature, SymmetricCryptogram}; +use crate::{KeyPair, Signature}; use rug::Integer; use std::borrow::{Borrow, BorrowMut}; @@ -85,17 +85,8 @@ pub fn cshake(x: &mut Vec, l: u64, n: &str, s: &str, d: u64) -> Vec { /// * `return -> Vec`: kmac_xof of `x` under `k` /// ## Usage: /// ``` -/// use capycrypt::ops::kmac_xof; -/// -/// let key_str = "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"; -/// let s_str = "My Tagged Application"; -/// let mut key_bytes = hex::decode(key_str).unwrap(); -/// let mut data = hex::decode("00010203").unwrap(); -/// -/// let res = kmac_xof(&mut key_bytes, &mut data, 64, &s_str, 512); -/// assert_eq!(hex::encode(res), "1755133f1534752a") /// ``` -pub fn kmac_xof(k: &mut Vec, x: &mut Vec, l: u64, s: &str, d: u64) -> Vec { +pub fn kmac_xof(k: &mut Vec, x: &Vec, l: u64, s: &str, d: u64) -> Vec { let mut encode_k = encode_string(k); let bytepad_w = match d { 256 => 168, @@ -103,7 +94,7 @@ pub fn kmac_xof(k: &mut Vec, x: &mut Vec, l: u64, s: &str, d: u64) -> Ve _ => panic!("Value must be either 256 or 512"), }; let mut bp = byte_pad(&mut encode_k, bytepad_w); - bp.append(x); + bp.append(&mut x.to_owned()); let mut right_enc = right_encode(0); // SP 800-185 4.3.1 KMAC with Arbitrary-Length Output bp.append(&mut right_enc); cshake(&mut bp, l, "KMAC", s, d) @@ -118,15 +109,10 @@ impl Hashable for Message { /// * `return -> Vec`: containing result of shake operation of size 512 bits /// ## Usage: /// ``` - /// use capycrypt::ops::compute_sha3_hash; - /// use hex::ToHex; - /// - /// let digest = compute_sha3_hash(&mut "test".as_bytes().to_vec(), 224).encode_hex::(); - /// assert!(digest == "3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b"); /// ``` fn compute_sha3_hash(&mut self, d: u64) { self.digest = match d { - 224 | 256 | 384 | 512 => Some(Box::new(shake(&mut self.msg, d))), + 224 | 256 | 384 | 512 => Some(shake(&mut self.msg, d)), _ => panic!("Value must be either 224, 256. 384, or 512"), } } @@ -142,17 +128,10 @@ impl Hashable for Message { /// * `return -> Vec`: `t` ← kmac_xof(pw, m, 512, “T”) as ```Vec``` of size `l` /// ## Usage: /// ``` - /// use capycrypt::ops::compute_tagged_hash; - /// let mut pw = "".as_bytes().to_vec(); - /// let mut message = "".as_bytes().to_vec(); - /// let mut s = "".to_owned(); - /// let digest = compute_tagged_hash(&mut pw, &mut message, &mut s, 256); - /// let expected = "3f9259e80b35e0719c26025f7e38a4a38172bf1142a6a9c1930e50df03904312"; - /// assert_eq!(hex::encode(digest), expected); /// ``` fn compute_tagged_hash(&mut self, pw: &mut Vec, s: &mut str, d: u64) { self.digest = match d { - 224 | 256 | 384 | 512 => Some(Box::new(kmac_xof(pw, &mut self.msg, d, s, d))), + 224 | 256 | 384 | 512 => Some(kmac_xof(pw, &self.msg, d, s, d)), _ => panic!("Value must be either 224, 256. 384, or 512"), } } @@ -175,39 +154,18 @@ impl PwEncryptable for Message { /// /// ## Usage: /// ``` - /// use capycrypt::ops::encrypt_with_pw; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// - /// let pw = get_random_bytes(64); - /// let mut message = Box::new(hex::decode("C0C1C2C3C4C5C6C7").unwrap().to_owned()); - /// let mut encryption = Box::new(encrypt_with_pw(&mut pw.clone(), &mut message, 256)); /// ``` - fn encrypt_with_pw(&mut self, pw: &mut Vec, d: u64) { + fn pw_encrypt(&mut self, pw: &[u8], d: u64) { let z = get_random_bytes(512); let mut ke_ka = z.clone(); - ke_ka.append(pw); - let ke_ka = kmac_xof(&mut ke_ka, &mut vec![], 1024, "S", d); - let c = kmac_xof( - &mut ke_ka[..64].to_vec(), - &mut vec![], - (self.msg.len() * 8) as u64, - "SKE", - d, - ); - println!("message before encryption: {:?}", self.msg); + ke_ka.append(&mut pw.to_owned()); + let ke_ka = kmac_xof(&mut ke_ka, &vec![], 1024, "S", d); + let ke = &mut ke_ka[..64].to_vec(); + let ka = &mut ke_ka[64..].to_vec(); + self.digest = Some(kmac_xof(ka, &self.msg, 512, "SKA", d)); + let c = kmac_xof(ke, &vec![], (self.msg.len() * 8) as u64, "SKE", d); xor_bytes(self.msg.borrow_mut(), &c); - println!("message after encryption: {:?}", self.msg); - self.digest = Some(Box::new(kmac_xof( - &mut ke_ka[64..].to_vec(), - &mut self.msg.clone(), - 512, - "SKA", - d, - ))); - println!("message after tag: {:?}", self.msg); - - self.sym_params = Some(SymmetricCryptogram { z }); - println!("tag: {:?}", self.digest.as_mut().unwrap()); + self.sym_nonce = Some(z); } /// # Symmetric Decryption @@ -224,44 +182,18 @@ impl PwEncryptable for Message { /// * `return -> bool`: t` == t, result of tag verification /// ## Usage: /// ``` - /// use capycrypt::ops; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// use std::borrow::BorrowMut; /// - /// let pw = get_random_bytes(32); - /// let mut message = Box::new(hex::decode("C0C1C2C3C4C5C6C7").unwrap().to_owned()); - /// let mut encryption = Box::new(operations::encrypt_with_pw(&mut pw.clone(), &mut message, 256)); - /// assert!(operations::decrypt_with_pw(&mut pw.clone(), &mut encryption.borrow_mut(), 256)); /// ``` - fn decrypt_with_pw(&mut self, pw: &mut Vec, d: u64) { - self.sym_params.as_mut().unwrap().z.append(pw); - let ke_ka = kmac_xof( - &mut self.sym_params.as_mut().unwrap().z, - &mut vec![], - 1024, - "S", - d, - ); - let m = kmac_xof( - &mut ke_ka[..64].to_vec(), - &mut vec![], - (self.msg.len() * 8) as u64, - "SKE", - d, - ); - println!("message before decryption: {:?}", self.msg); + fn pw_decrypt(&mut self, pw: &[u8], d: u64) { + let mut z_pw = self.sym_nonce.clone().unwrap(); + z_pw.append(&mut pw.to_owned()); + let ke_ka = kmac_xof(&mut z_pw, &vec![], 1024, "S", d); + let ke = &mut ke_ka[..64].to_vec(); + let ka = &mut ke_ka[64..].to_vec(); + let m = kmac_xof(ke, &vec![], (self.msg.len() * 8) as u64, "SKE", d); xor_bytes(&mut self.msg, &m); - println!("message after decryption: {:?}", self.msg); - self.op_result = Some( - self.digest.as_mut().unwrap() - == &mut Box::new(kmac_xof( - &mut ke_ka[64..].to_vec(), - &mut self.msg.clone(), - 512, - "SKA", - d, - )), - ); + let new_t = &kmac_xof(ka, &self.msg, 512, "SKA", d); + self.op_result = Some(self.digest.as_mut().unwrap() == new_t); } } @@ -280,30 +212,18 @@ impl KeyPair { /// verification key 𝑉 is hashed together with the message 𝑚 /// and the nonce 𝑈: hash (𝑚, 𝑈, 𝑉) . /// ## Usage: + /// ``` /// ``` - /// use capycrypt::curve::Curves; - /// use capycrypt::curve::{CurvePoint, Point}; - /// use capycrypt::ops::gen_keypair; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// const SELECTED_CURVE: Curves = Curves::E448; - /// - /// let pw = get_random_bytes(32); - /// let owner = "test key".to_string(); - /// let key_obj = gen_keypair(&mut pw.clone(), owner, 256); - /// let x = key_obj.pub_x; - /// let y = key_obj.pub_y; - /// let pub_key = CurvePoint::point(SELECTED_CURVE, x, y); - /// ``` - fn gen_keypair(&mut self, pw: &mut Vec, owner: String, d: u64) { - let s: Integer = - (bytes_to_big(kmac_xof(pw, &mut vec![], 512, "K", d)) * 4) % order(SELECTED_CURVE); + pub fn new(pw: &Vec, owner: String, d: u64) -> KeyPair { + let s: Integer = (bytes_to_big(kmac_xof(&mut pw.to_owned(), &vec![], 512, "K", d)) * 4) + % order(SELECTED_CURVE); let v = EdCurvePoint::generator(SELECTED_CURVE, false) * (s); - - self.owner = owner; - self.priv_key = pw.to_vec(); - self.pub_x = v.x; - self.pub_y = v.y; - self.date_created = get_date_and_time_as_string(); + KeyPair { + owner, + pub_key: v, + priv_key: pw.to_vec(), + date_created: get_date_and_time_as_string(), + } } } @@ -325,47 +245,22 @@ impl KeyEncryptable for Message { /// * `return -> ECCryptogram` : cryptogram: (𝑍, c, t) = 𝑍||c||t /// ## Usage: /// ``` - /// use capycrypt::ops; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// use std::borrow::BorrowMut; - /// use capycrypt::curves::EdCurves; - /// use capycrypt::curves::{CurvePoint, Point}; - /// - /// // Box the message to support arbitrary size and best performance - /// let pw = get_random_bytes(32); - /// let owner = "test key".to_string(); - /// let mut message = Box::new(get_random_bytes(5242880).to_owned()); //5mb - /// - /// // Select curve and generate keypair - /// const SELECTED_CURVE: Curves = Curves::E448; - /// let key_obj = ops::gen_keypair(&mut pw.clone(), owner, 256); - /// let x = key_obj.pub_x; - /// let y = key_obj.pub_y; - /// let pub_key = CurvePoint::point(SELECTED_CURVE, x, y); - /// - /// // Assert decryption correctness - /// let mut enc = operations::encrypt_with_key(pub_key, &mut message, 256); - /// let res = ops::decrypt_with_key(&mut pw.clone(), enc.borrow_mut(), 256); - /// assert!(res); /// ``` - fn encrypt_with_key(&mut self, pub_key: EdCurvePoint, d: u64) { + fn key_encrypt(&mut self, pub_key: EdCurvePoint, d: u64) { let k: Integer = (bytes_to_big(get_random_bytes(64)) * 4) % order(SELECTED_CURVE); let w = pub_key * k.clone(); let z = EdCurvePoint::generator(SELECTED_CURVE, false) * k; - let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &mut vec![], 1024, "PK", d); + let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &vec![], 1024, "PK", d); let ke = &mut ke_ka[..64].to_vec(); let ka = &mut ke_ka[64..].to_vec(); - + let t = kmac_xof(&mut ka.clone(), self.msg.borrow(), 512, "PKA", d); let len = (self.msg.len() * 8) as u64; - let c = kmac_xof(ke, &mut vec![], len, "PKE", d); + let c = kmac_xof(ke, &vec![], len, "PKE", d); xor_bytes(&mut self.msg, &c); - let t = kmac_xof(&mut ka.clone(), self.msg.borrow_mut(), 512, "PKA", d); + self.msg = Box::new(c); - self.ecc_params = Some(ECCryptogram { - z_x: z.x, - z_y: z.y, - t, - }) + self.digest = Some(t); + self.asym_nonce = Some(z); } /// # Asymmetric Decryption @@ -385,47 +280,22 @@ impl KeyEncryptable for Message { /// * `return -> bool`: Decryption of cryptogram ```𝑍||c||t iff t` = t``` /// ## Usage: /// ``` - /// use capycrypt::ops; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// use std::borrow::BorrowMut; - /// use capycrypt::curves::EdCurves; - /// use capycrypt::curves::{EdCurvePoint, ArbitraryPoint}; - /// - /// // Box the message to support arbitrary size and best performance - /// let pw = get_random_bytes(32); - /// let owner = "test key".to_string(); - /// let mut message = Box::new(get_random_bytes(5242880).to_owned()); //5mb - /// - /// // Select curve and generate key - /// const SELECTED_CURVE: EdCurves = EdCurves::E448; - /// let key_obj = ops::gen_keypair(&mut pw.clone(), owner, 256); - /// let x = key_obj.pub_x; - /// let y = key_obj.pub_y; - /// let pub_key = EdCurvePoint::ArbitraryPoint(SELECTED_CURVE, x, y); - /// - /// // Assert decryption correctness - /// let mut enc = operations::encrypt_with_key(pub_key, &mut message, 256); - /// let res = ops::decrypt_with_key(&mut pw.clone(), enc.borrow_mut(), 256); - /// assert!(res); /// ``` - fn decrypt_with_key(&mut self, pw: &mut [u8], d: u64) { - let z = EdCurvePoint::arbitrary_point( - SELECTED_CURVE, - self.ecc_params.as_mut().unwrap().z_x.clone(), - self.ecc_params.as_mut().unwrap().z_y.clone(), - ); - let s: Integer = (bytes_to_big(kmac_xof(&mut pw.to_owned(), &mut vec![], 512, "K", d)) * 4) - % z.clone().n; + fn key_decrypt(&mut self, pw: &[u8], d: u64) { + let z = self.asym_nonce.clone().unwrap(); + + let s: Integer = + (bytes_to_big(kmac_xof(&mut pw.to_owned(), &vec![], 512, "K", d)) * 4) % z.clone().n; let w = z * s; - let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &mut vec![], 1024, "PK", d); + let ke_ka = kmac_xof(&mut big_to_bytes(w.x), &vec![], 1024, "PK", d); let ke = &mut ke_ka[..64].to_vec(); let ka = &mut ke_ka[64..].to_vec(); let len = self.msg.len() * 8; - let m = Box::new(kmac_xof(ke, &mut vec![], (len) as u64, "PKE", d)); + let m = Box::new(kmac_xof(ke, &vec![], (len) as u64, "PKE", d)); xor_bytes(&mut self.msg, m.borrow()); - let t_p = kmac_xof(&mut ka.clone(), &mut self.msg, 512, "PKA", d); - self.op_result = Some(t_p == self.ecc_params.as_mut().unwrap().t); + let t_p = kmac_xof(&mut ka.clone(), &self.msg, 512, "PKA", d); + self.op_result = Some(t_p == self.digest.clone().unwrap()); } } @@ -444,28 +314,9 @@ impl Signable for Message { /// * `return -> Signature`: signature: (`ℎ`, `𝑍`) /// ## Usage /// ``` - /// use capycrypt::curve::Curves; - /// use capycrypt::curve::{CurvePoint, Point}; - /// use capycrypt::ops; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// use std::borrow::BorrowMut; - /// - /// let mut message = Box::new(get_random_bytes(5242880).to_owned()); - /// let pw = get_random_bytes(32); - /// - /// const SELECTED_CURVE: Curves = Curves::E448; - /// let key_obj = operations::gen_keypair(&mut pw.clone(), "test".to_string(), 256); - /// let x = key_obj.pub_x; - /// let y = key_obj.pub_y; - /// - /// // Sign then verify - /// let key = CurvePoint::point(SELECTED_CURVE, x, y); - /// let sig = operations::sign_with_key(&mut pw.clone(), &mut message, 256); - /// let res = operations::verify_signature(&sig, key, &mut message, 256); - /// assert!(res); /// ``` - fn sign_with_key(&mut self, pw: &mut Vec, d: u64) { - let s: Integer = bytes_to_big(kmac_xof(pw, &mut vec![], 512, "K", d)) * 4; + fn sign(&mut self, pw: &mut Vec, d: u64) { + let s: Integer = bytes_to_big(kmac_xof(pw, &vec![], 512, "K", d)) * 4; let mut s_bytes = big_to_bytes(s.clone()); let k: Integer = @@ -492,27 +343,8 @@ impl Signable for Message { /// * `return`: true if, and only if, kmac_xof(𝑈ₓ , m, 512, “T”) = h /// ## Usage /// ``` - /// use capycrypt::curve::Curves; - /// use capycrypt::curve::{CurvePoint, Point}; - /// use capycrypt::ops; - /// use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; - /// use std::borrow::BorrowMut; - /// - /// let mut message = Box::new(get_random_bytes(5242880).to_owned()); - /// let pw = get_random_bytes(32); - /// - /// const SELECTED_CURVE: Curves = Curves::E448; - /// let key_obj = operations::gen_keypair(&mut pw.clone(), "test".to_string(), 256); - /// let x = key_obj.pub_x; - /// let y = key_obj.pub_y; - /// - /// // Sign then verify - /// let key = CurvePoint::point(SELECTED_CURVE, x, y); - /// let sig = operations::sign_with_key(&mut pw.clone(), &mut message, 256); - /// let res = operations::verify_signature(&sig, key, &mut message, 256); - /// assert!(res); /// ``` - fn verify_signature(&mut self, sig: &Signature, pub_key: EdCurvePoint, d: u64) { + fn verify(&mut self, sig: &Signature, pub_key: EdCurvePoint, d: u64) { let mut u = EdCurvePoint::generator(SELECTED_CURVE, false) * sig.z.clone(); let hv = pub_key * (bytes_to_big(sig.h.clone())); u = u + &hv; @@ -521,13 +353,26 @@ impl Signable for Message { } } +impl Message { + pub fn new(data: &mut Vec) -> Message { + Message { + msg: Box::new(data.to_owned()), + sym_nonce: None, + asym_nonce: None, + digest: None, + op_result: None, + signature: None, + } + } +} + #[derive(Debug)] /// Message type for which cryptographic traits are defined. pub struct Message { pub msg: Box>, - pub digest: Option>>, - pub sym_params: Option, - pub ecc_params: Option, + pub sym_nonce: Option>, + pub asym_nonce: Option, + pub digest: Option>, pub op_result: Option, pub signature: Option, } @@ -538,16 +383,16 @@ pub trait Hashable { } pub trait PwEncryptable { - fn encrypt_with_pw(&mut self, pw: &mut Vec, d: u64); - fn decrypt_with_pw(&mut self, pw: &mut Vec, d: u64); + fn pw_encrypt(&mut self, pw: &[u8], d: u64); + fn pw_decrypt(&mut self, pw: &[u8], d: u64); } pub trait KeyEncryptable { - fn encrypt_with_key(&mut self, pub_key: EdCurvePoint, d: u64); - fn decrypt_with_key(&mut self, pw: &mut [u8], d: u64); + fn key_encrypt(&mut self, pub_key: EdCurvePoint, d: u64); + fn key_decrypt(&mut self, pw: &[u8], d: u64); } pub trait Signable { - fn sign_with_key(&mut self, pw: &mut Vec, d: u64); - fn verify_signature(&mut self, sig: &Signature, pub_key: EdCurvePoint, d: u64); + fn sign(&mut self, pw: &mut Vec, d: u64); + fn verify(&mut self, sig: &Signature, pub_key: EdCurvePoint, d: u64); } diff --git a/tests/ops_tests.rs b/tests/ops_tests.rs index 0b434c4..00e06ad 100644 --- a/tests/ops_tests.rs +++ b/tests/ops_tests.rs @@ -1,66 +1,46 @@ #[cfg(test)] -pub mod model_tests { - use capycrypt::curves::EdCurves::{self, E448}; - use capycrypt::curves::{ArbitraryPoint, EdCurvePoint}; - use capycrypt::ops::{Hashable, KeyEncryptable, Message, PwEncryptable, Signable}; +pub mod ops_tests { + use capycrypt::ops::{KeyEncryptable, Message, PwEncryptable, Signable}; use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes; + use capycrypt::KeyPair; use std::borrow::BorrowMut; use std::time::Instant; - const SELECTED_CURVE: EdCurves = E448; #[test] pub fn test_sym_enc_512() { let pw = get_random_bytes(64); + let mut msg = Message::new(&mut get_random_bytes(5242880)); - let mut msg = Message { - msg: Box::new(get_random_bytes(52).to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + msg.pw_encrypt(&mut pw.clone(), 512); + msg.pw_decrypt(&mut pw.clone(), 512); - // let mut message = hex::decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7").unwrap(); - msg.encrypt_with_pw(&mut pw.clone(), 512); - msg.decrypt_with_pw(&mut pw.clone(), 512); let res = msg.op_result.unwrap(); - println!("result: {:?}", res); assert!(res); } -} -// #[test] -// pub fn test_sym_enc_256() { -// let pw = get_random_bytes(64); + #[test] + pub fn test_sym_enc_256() { + let pw = get_random_bytes(64); + let mut msg = Message::new(&mut get_random_bytes(5242880)); -// let mut msg = Message { -// data: Box::new(get_random_bytes(5242880).to_owned()), -// sym_params: None, -// ecc_params: None, -// op_result: None, -// signature: None, -// }; -// // let mut message = hex::decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7").unwrap(); -// msg.encrypt_with_pw(&mut pw.clone(), 256); -// msg.decrypt_with_pw(&mut pw.clone(), 256); -// let res = msg.op_result.unwrap(); -// assert!(res); -// } + msg.pw_encrypt(&mut pw.clone(), 256); + msg.pw_decrypt(&mut pw.clone(), 256); -// #[test] -// fn test_key_gen_enc_dec_256() { -// //check conversion to and from bytes. -// let pw = get_random_bytes(32); -// let owner = "test key".to_string(); -// let mut message = Box::new(get_random_bytes(5242880).to_owned()); //5mb -// let key_obj = gen_keypair(&mut pw.clone(), owner, 256); -// let x = key_obj.pub_x; -// let y = key_obj.pub_y; -// let pub_key = EdCurvePoint::arbitrary_point(SELECTED_CURVE, x, y); -// let mut enc = encrypt_with_key(pub_key, &mut message, 256); -// let res = decrypt_with_key(&mut pw.clone(), enc.borrow_mut(), 256); -// assert!(res); -// } + let res = msg.op_result.unwrap(); + assert!(res); + } + #[test] + fn test_key_gen_enc_dec_256() { + //check conversion to and from bytes. + let mut msg = Message::new(&mut get_random_bytes(5242880)); + let key_pair = KeyPair::new(&get_random_bytes(32), "test key".to_string(), 256); + + msg.key_encrypt(key_pair.pub_key, 256); + msg.key_decrypt(&key_pair.priv_key, 256); + + let res = msg.op_result.unwrap(); + assert!(res); + } +} // #[test] // fn test_key_gen_enc_dec_512() { diff --git a/tests/sponge_tests.rs b/tests/sponge_tests.rs index 81947dc..1584066 100644 --- a/tests/sponge_tests.rs +++ b/tests/sponge_tests.rs @@ -157,26 +157,12 @@ mod sponge_tests { } #[test] fn test_shake_224() { - let mut data = Message { - msg: Box::new("".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("".as_bytes().to_owned())); let expected = "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7"; data.compute_sha3_hash(224); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); - let mut data = Message { - msg: Box::new("test".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("test".as_bytes().to_owned())); let expected = "3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b"; data.compute_sha3_hash(224); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); @@ -184,26 +170,12 @@ fn test_shake_224() { #[test] fn test_shake_256() { - let mut data = Message { - msg: Box::new("".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("".as_bytes().to_owned())); let expected = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"; data.compute_sha3_hash(256); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); - let mut data = Message { - msg: Box::new("test".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("test".as_bytes().to_owned())); let expected = "36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80"; data.compute_sha3_hash(256); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); @@ -211,26 +183,12 @@ fn test_shake_256() { #[test] fn test_shake_384() { - let mut data = Message { - msg: Box::new("".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("".as_bytes().to_owned())); let expected = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004"; data.compute_sha3_hash(384); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); - let mut data = Message { - msg: Box::new("test".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("test".as_bytes().to_owned())); let expected = "e516dabb23b6e30026863543282780a3ae0dccf05551cf0295178d7ff0f1b41eecb9db3ff219007c4e097260d58621bd"; data.compute_sha3_hash(384); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); @@ -238,14 +196,7 @@ fn test_shake_384() { #[test] fn test_shake_512() { - let mut data = Message { - msg: Box::new("test".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("test".as_bytes().to_owned())); let expected = "9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7b710110c0a678288166e7fbe796883a4f2e9b3ca9f484f521d0ce464345cc1aec96779149c14"; data.compute_sha3_hash(512); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); @@ -255,14 +206,7 @@ fn test_shake_512() { fn test_compute_tagged_hash_256() { let mut s = "".to_owned(); let mut pw = "".as_bytes().to_vec(); - let mut data = Message { - msg: Box::new("".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("".as_bytes().to_owned())); let expected = "3f9259e80b35e0719c26025f7e38a4a38172bf1142a6a9c1930e50df03904312"; data.compute_tagged_hash(&mut pw, &mut s, 256); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected); @@ -272,14 +216,7 @@ fn test_compute_tagged_hash_256() { fn test_compute_tagged_hash_512() { let mut s = "".to_owned(); let mut pw = "test".as_bytes().to_vec(); - let mut data = Message { - msg: Box::new("".as_bytes().to_owned()), - digest: None, - sym_params: None, - ecc_params: None, - op_result: None, - signature: None, - }; + let mut data = Message::new(&mut Box::new("".as_bytes().to_owned())); let expected = "0f9b5dcd47dc08e08a173bbe9a57b1a65784e318cf93cccb7f1f79f186ee1caeff11b12f8ca3a39db82a63f4ca0b65836f5261ee64644ce5a88456d3d30efbed"; data.compute_tagged_hash(&mut pw, &mut s, 512); assert!(hex::encode(data.digest.unwrap().to_vec()) == expected);