Skip to content

Commit

Permalink
fix: remove clones
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Capybara committed Nov 1, 2023
1 parent e2cc2ba commit 5c6233d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ let pw = get_random_bytes(64);
// Get 5mb random data
let mut msg = Message::new(get_random_bytes(5242880));
// Encrypt the data with 256 bits of security
msg.pw_encrypt(&mut pw.clone(), 512);
msg.pw_encrypt(&pw, 512);
// Decrypt the data
msg.pw_decrypt(&mut pw.clone(), 512);
msg.pw_decrypt(&pw, 512);
// Verify operation success
assert!(msg.op_result.unwrap());
```
Expand Down
4 changes: 2 additions & 2 deletions benches/benchmark_e222_224.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const BIT_SECURITY: u64 = 224;

/// Symmetric encrypt and decrypt roundtrip
fn sym_enc(pw: &mut Vec<u8>, mut msg: Message) {
msg.pw_encrypt(&mut pw.clone(), BIT_SECURITY);
msg.pw_decrypt(&mut pw.clone(), BIT_SECURITY);
msg.pw_encrypt(&pw, BIT_SECURITY);
msg.pw_decrypt(&pw, BIT_SECURITY);
}

/// Asymmetric encrypt and decrypt roundtrip + keygen
Expand Down
4 changes: 2 additions & 2 deletions benches/benchmark_e521_512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const BIT_SECURITY: u64 = 512;

/// Symmetric encrypt and decrypt roundtrip
fn sym_enc(pw: &mut Vec<u8>, mut msg: Message) {
msg.pw_encrypt(&mut pw.clone(), BIT_SECURITY);
msg.pw_decrypt(&mut pw.clone(), BIT_SECURITY);
msg.pw_encrypt(&pw, BIT_SECURITY);
msg.pw_decrypt(&pw, BIT_SECURITY);
}

/// Asymmetric encrypt and decrypt roundtrip + keygen
Expand Down
71 changes: 36 additions & 35 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,15 @@ fn shake(n: &mut Vec<u8>, d: u64) -> Vec<u8> {
/// Implements FIPS 202 Section 3. Returns: customizable and
/// domain-seperated length `L` SHA3XOF hash of input string.
/// ## Arguments:
/// * `x: &mut Vec<u8>`: input message as ```Vec<u8>```
/// * `x: &Vec<u8>`: input message as ```Vec<u8>```
/// * `l: u64`: requested output length
/// * `n: &str`: optional function name string
/// * `s: &str`: option customization string
/// ## Returns:
/// * `return -> Vec<u8>`: SHA3XOF hash of length `l` of input message `x`
pub fn cshake(x: &mut Vec<u8>, l: u64, n: &str, s: &str, d: u64) -> Vec<u8> {
if n.is_empty() && s.is_empty() {
shake(x, l);
}
pub fn cshake(x: &[u8], l: u64, n: &str, s: &str, d: u64) -> Vec<u8> {
let mut encoded_n = encode_string(&n.as_bytes().to_vec());
let encoded_s = encode_string(&s.as_bytes().to_vec());

encoded_n.extend_from_slice(&encoded_s);

let bytepad_w = match d {
Expand All @@ -80,17 +76,21 @@ pub fn cshake(x: &mut Vec<u8>, l: u64, n: &str, s: &str, d: u64) -> Vec<u8> {
};

let mut out = byte_pad(&mut encoded_n, bytepad_w);

out.append(x);
out.extend_from_slice(x);
out.push(0x04);

if n.is_empty() && s.is_empty() {
shake(&mut out, l);
}

sponge_squeeze(&mut sponge_absorb(&mut out, d), l, 1600 - d)
}

/// # Keyed Message Authtentication
/// Generates keyed hash for given input as specified in NIST SP 800-185 section 4.
/// ## Arguments:
/// * `k: &mut Vec<u8>`: key. SP 800 185 8.4.1 KMAC Key Length requires key length >= d
/// * `x: &mut Vec<u8>`: byte-oriented message
/// * `k: &Vec<u8>`: key. SP 800 185 8.4.1 KMAC Key Length requires key length >= d
/// * `x: &Vec<u8>`: byte-oriented message
/// * `l: u64`: requested bit output length
/// * `s: &str`: customization string
/// * `d: u64`: the security parameter for the operation. NIST-standard values for d consist of the following:
Expand All @@ -99,7 +99,7 @@ pub fn cshake(x: &mut Vec<u8>, l: u64, n: &str, s: &str, d: u64) -> Vec<u8> {
///
/// ## Returns:
/// * `return -> Vec<u8>`: kmac_xof of `x` under `k`
pub fn kmac_xof(k: &Vec<u8>, x: &Vec<u8>, l: u64, s: &str, d: u64) -> Vec<u8> {
pub fn kmac_xof(k: &Vec<u8>, x: &[u8], l: u64, s: &str, d: u64) -> Vec<u8> {
let mut encode_k = encode_string(k);
let bytepad_w = match d {
224 => 172,
Expand All @@ -109,10 +109,12 @@ pub fn kmac_xof(k: &Vec<u8>, x: &Vec<u8>, l: u64, s: &str, d: u64) -> Vec<u8> {
_ => panic!("Unsupported security strength. Must be 224, 384, 256, or 512"),
};
let mut bp = byte_pad(&mut encode_k, bytepad_w);
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)

// Extend bp with contents of x and right_encode(0)
bp.extend_from_slice(x);
bp.extend_from_slice(&right_encode(0)); // SP 800-185 4.3.1 KMAC with Arbitrary-Length Output

cshake(&bp, l, "KMAC", s, d)
}

impl Hashable for Message {
Expand Down Expand Up @@ -196,21 +198,21 @@ impl PwEncryptable for Message {
/// // Get 5mb random data
/// let mut msg = Message::new(get_random_bytes(5242880));
/// // Encrypt the data with 512 bits of security
/// msg.pw_encrypt(&mut pw.clone(), 512);
/// msg.pw_encrypt(&pw, 512);
/// // Decrypt the data
/// msg.pw_decrypt(&mut pw.clone(), 512);
/// msg.pw_decrypt(&pw, 512);
/// // Verify operation success
/// assert!(msg.op_result.unwrap());
/// ```
fn pw_encrypt(&mut self, pw: &[u8], d: u64) {
let z = get_random_bytes(512);
let mut ke_ka = z.clone();
ke_ka.append(&mut pw.to_owned());
let ke_ka = kmac_xof(&ke_ka, &vec![], 1024, "S", d);
let ke = &mut ke_ka[..64].to_vec();
let ka = &mut ke_ka[64..].to_vec();
let ke_ka = kmac_xof(&ke_ka, &[], 1024, "S", d);
let ke = &ke_ka[..64].to_vec();
let ka = &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);
let c = kmac_xof(ke, &[], (self.msg.len() * 8) as u64, "SKE", d);
xor_bytes(&mut self.msg, &c);
self.sym_nonce = Some(z);
}
Expand Down Expand Up @@ -243,19 +245,19 @@ impl PwEncryptable for Message {
/// // Get 5mb random data
/// let mut msg = Message::new(get_random_bytes(5242880));
/// // Encrypt the data with 512 bits of security
/// msg.pw_encrypt(&mut pw.clone(), 512);
/// msg.pw_encrypt(&pw, 512);
/// // Decrypt the data
/// msg.pw_decrypt(&mut pw.clone(), 512);
/// msg.pw_decrypt(&pw, 512);
/// // Verify operation success
/// assert!(msg.op_result.unwrap());
/// ```
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(&z_pw, &vec![], 1024, "S", d);
let ke_ka = kmac_xof(&z_pw, &[], 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);
let m = kmac_xof(ke, &[], (self.msg.len() * 8) as u64, "SKE", d);
xor_bytes(&mut self.msg, &m);
let new_t = &kmac_xof(ka, &self.msg, 512, "SKA", d);
self.op_result = Some(self.digest.as_mut().unwrap() == new_t);
Expand Down Expand Up @@ -290,8 +292,7 @@ impl KeyPair {
/// ```
pub fn new(pw: &Vec<u8>, owner: String, curve: EdCurves, d: u64) -> KeyPair {
// Timing sidechannel on variable keysize is mitigated here due to modding by curve order.
let s: Integer =
(bytes_to_big(kmac_xof(pw, &vec![], 512, "K", d)) * 4) % order(SELECTED_CURVE);
let s: Integer = (bytes_to_big(kmac_xof(pw, &[], 512, "K", d)) * 4) % order(SELECTED_CURVE);

let pub_key = EdCurvePoint::generator(curve, false) * (s);

Expand Down Expand Up @@ -343,12 +344,12 @@ impl KeyEncryptable for Message {
let w = pub_key.clone() * k.clone();
let z = EdCurvePoint::generator(pub_key.curve, false) * k;

let ke_ka = kmac_xof(&big_to_bytes(w.x), &vec![], 1024, "PK", d);
let ke_ka = kmac_xof(&big_to_bytes(w.x), &[], 1024, "PK", d);
let ke = &mut ke_ka[..64].to_vec();
let ka = &mut ke_ka[64..].to_vec();

let t = kmac_xof(ka, &self.msg, 512, "PKA", d);
let c = kmac_xof(ke, &vec![], (self.msg.len() * 8) as u64, "PKE", d);
let c = kmac_xof(ke, &[], (self.msg.len() * 8) as u64, "PKE", d);
xor_bytes(&mut self.msg, &c);

self.digest = Some(t);
Expand Down Expand Up @@ -404,17 +405,17 @@ impl KeyEncryptable for Message {
fn key_decrypt(&mut self, pw: &[u8], d: u64) {
let z = self.asym_nonce.clone().unwrap();
let s: Integer =
(bytes_to_big(kmac_xof(&pw.to_owned(), &vec![], 512, "K", d)) * 4) % z.clone().n;
(bytes_to_big(kmac_xof(&pw.to_owned(), &[], 512, "K", d)) * 4) % z.clone().n;
let w = z * s;

let ke_ka = kmac_xof(&big_to_bytes(w.x), &vec![], 1024, "PK", d);
let ke_ka = kmac_xof(&big_to_bytes(w.x), &[], 1024, "PK", d);
let ke = &mut ke_ka[..64].to_vec();
let ka = &mut ke_ka[64..].to_vec();

let m = Box::new(kmac_xof(ke, &vec![], (self.msg.len() * 8) as u64, "PKE", d));
let m = Box::new(kmac_xof(ke, &[], (self.msg.len() * 8) as u64, "PKE", d));
xor_bytes(&mut self.msg, &m);
let t_p = kmac_xof(&ka.clone(), &self.msg, 512, "PKA", d);
self.op_result = Some(t_p == self.digest.clone().unwrap());
let t_p = kmac_xof(ka, &self.msg, 512, "PKA", d);
self.op_result = Some(t_p == self.digest.as_deref().unwrap());
}
}

Expand Down Expand Up @@ -453,7 +454,7 @@ impl Signable for Message {
/// msg.sign(&key_pair, 512);
/// ```
fn sign(&mut self, key: &KeyPair, d: u64) {
let s: Integer = bytes_to_big(kmac_xof(&key.priv_key, &vec![], 512, "K", d)) * 4;
let s: Integer = bytes_to_big(kmac_xof(&key.priv_key, &[], 512, "K", d)) * 4;
let s_bytes = big_to_bytes(s.clone());

let k: Integer = bytes_to_big(kmac_xof(&s_bytes, &self.msg, 512, "N", d)) * 4;
Expand Down
4 changes: 1 addition & 3 deletions src/sha3/aux_functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// NIST 800-185 compliant functions.
pub mod nist_800_185 {
use std::borrow::BorrowMut;

use byteorder::{BigEndian, WriteBytesExt};

/// # NIST SP 800-185 2.3.3
Expand All @@ -25,7 +23,7 @@ pub mod nist_800_185 {
/// * `return`: left_encode(len(`s`)) + `s`
pub fn encode_string(s: &Vec<u8>) -> Vec<u8> {
let mut encoded = left_encode((s.len() * 8) as u64);
encoded.append(s.clone().borrow_mut());
encoded.append(&mut s.clone());
encoded
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ops_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub mod ops_tests {
let pw = get_random_bytes(64);
let mut msg = Message::new(get_random_bytes(5242880));

msg.pw_encrypt(&mut pw.clone(), 512);
msg.pw_decrypt(&mut pw.clone(), 512);
msg.pw_encrypt(&pw, 256);
msg.pw_decrypt(&pw, 256);

assert!(msg.op_result.unwrap());
}
Expand All @@ -21,8 +21,8 @@ pub mod ops_tests {
let pw = get_random_bytes(64);
let mut msg = Message::new(get_random_bytes(5242880));

msg.pw_encrypt(&mut pw.clone(), 256);
msg.pw_decrypt(&mut pw.clone(), 256);
msg.pw_encrypt(&pw, 256);
msg.pw_decrypt(&pw, 256);

assert!(msg.op_result.unwrap());
}
Expand Down
14 changes: 7 additions & 7 deletions tests/sponge_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ mod sponge_tests {
fn test_kmac_256() {
let key_str = "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f";
let s_str = "My Tagged Application";
let mut key_bytes = hex::decode(key_str).unwrap();
let data = hex::decode("00010203").unwrap();
let res = kmac_xof(&mut key_bytes, &data, 64, &s_str, 512);
let key_bytes = hex::decode(key_str).unwrap();
let mut data = hex::decode("00010203").unwrap();
let res = kmac_xof(&key_bytes, &mut data, 64, &s_str, 512);
let expected = "1755133f1534752a";
assert_eq!(hex::encode(res), expected)
}
Expand All @@ -24,10 +24,10 @@ mod sponge_tests {
let key_str = "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f";
let s_str = "My Tagged Application";

let mut key_bytes = hex::decode(key_str).unwrap();
let data = hex::decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7").unwrap();
let key_bytes = hex::decode(key_str).unwrap();
let mut data = hex::decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7").unwrap();

let res = kmac_xof(&mut key_bytes, &data, 512, &s_str, 512);
let res = kmac_xof(&key_bytes, &mut data, 512, &s_str, 512);
let expected = "d5be731c954ed7732846bb59dbe3a8e30f83e77a4bff4459f2f1c2b4ecebb8ce67ba01c62e8ab8578d2d499bd1bb276768781190020a306a97de281dcc30305d";
assert_eq!(hex::encode(res), expected)
}
Expand Down Expand Up @@ -203,7 +203,7 @@ fn test_shake_512() {

#[test]
fn test_compute_tagged_hash_256() {
let s = "".to_owned();
let s = "".to_string();
let mut pw = "".as_bytes().to_vec();
let mut data = Message::new(vec![]);
let expected = "3f9259e80b35e0719c26025f7e38a4a38172bf1142a6a9c1930e50df03904312";
Expand Down

0 comments on commit 5c6233d

Please sign in to comment.