diff --git a/src/message.rs b/src/message.rs index bed59e9..a96a2a2 100644 --- a/src/message.rs +++ b/src/message.rs @@ -380,170 +380,4 @@ impl SerializedMessage } #[cfg(test)] -mod tests -{ - use super::*; - - /// Generic message test function - fn message_serial_generic(s: S, d: D) - where S: MessageSender, - D: MessageReceiver - { - eprintln!("=== Message serialisation with tc, rc: S: {}, D: {}", std::any::type_name::(), std::any::type_name::()); - let message = MessageBuilder::for_sender::() - .create(format!("This is a string, and some random data: {:?}", aes::AesKey::generate().unwrap())) - .expect("Failed to create message"); - println!(">> Created message: {:?}", message); - let serialised = message.serialise(s).expect("Failed to serialise message"); - println!(">> Serialised message: {:?}", serialised); - let binary = serialised.into_bytes(); - println!(">> Written to {} bytes", binary.len()); - let read = SerializedMessage::from_bytes(&binary).expect("Failed to read serialised message from binary"); - println!(">> Read from bytes: {:?}", read); - let deserialised = Message::deserialize(&read, d).expect("Failed to deserialise message"); - println!(">> Deserialised message: {:?}", deserialised); - - assert_eq!(message, deserialised, "Messages not identical"); - assert_eq!(message.into_value(), deserialised.into_value(), "Message values not identical"); - eprintln!("=== Passed (S: {}, D: {}) ===", std::any::type_name::(), std::any::type_name::()); - } - - #[test] - fn message_serial_basic() - { - message_serial_generic((), ()); - } - - - #[test] - fn message_serial_sign() - { - let rsa_priv = rsa::RsaPrivateKey::generate().unwrap(); - struct Sign(rsa::RsaPrivateKey); - struct Verify(rsa::RsaPublicKey); - - impl MessageSender for Sign - { - const CAP_SIGN: bool = true; - fn sign_data(&self, data: &[u8]) -> Option { - Some(rsa::sign_slice(data, &self.0).expect("Failed to sign")) - } - } - impl MessageReceiver for Verify - { - fn verify_data(&self, data: &[u8], sig: &rsa::Signature) -> Option> { - Some(sig.verify_slice(data, &self.0).map_err(Into::into)) - } - } - let verify = Verify(rsa_priv.get_public_parts()); - println!("Signing priv-key: {:?}", rsa_priv); - println!("Verifying pub-key: {:?}", verify.0); - message_serial_generic(Sign(rsa_priv), verify); - } - - #[test] - fn message_serial_encrypt() - { - color_eyre::install().unwrap(); - let rsa_priv = rsa::RsaPrivateKey::generate().unwrap(); - struct Dec(rsa::RsaPrivateKey); - struct Enc(rsa::RsaPublicKey); - - - impl MessageSender for Enc - { - const CAP_ENCRYPT: bool = true; - - fn encrypt_key(&self, key: &aes::AesKey) -> Option<[u8; RSA_BLOCK_SIZE]> { - let mut output = [0u8; RSA_BLOCK_SIZE]; - let w = rsa::encrypt_slice_sync(key, &self.0, &mut &mut output[..]).expect("Failed to encrypt session key"); - assert_eq!(w, output.len()); - - Some(output) - } - } - impl MessageReceiver for Dec - { - fn decrypt_key(&self, enc_key: &[u8; RSA_BLOCK_SIZE]) -> Option> { - let mut output = aes::AesKey::empty(); - match rsa::decrypt_slice_sync(enc_key, &self.0, &mut output.as_mut()) { - Ok(sz) => assert_eq!(sz, - output.as_ref().len()), - Err(err) => return Some(Err(err.into())), - } - Some(Ok(output)) - } - } - - let enc = Enc(rsa_priv.get_public_parts()); - println!("Encrypting pub-key: {:?}", enc.0); - println!("Decrypting priv-key: {:?}", rsa_priv); - message_serial_generic(enc, Dec(rsa_priv)); - } - #[test] - fn message_serial_encrypt_sign() - { - let rsa_priv = rsa::RsaPrivateKey::generate().unwrap(); - let rsa_priv2 = rsa::RsaPrivateKey::generate().unwrap(); - - struct Dec(rsa::RsaPrivateKey, rsa::RsaPublicKey); - struct Enc(rsa::RsaPublicKey, rsa::RsaPrivateKey); - - impl MessageSender for Enc - { - const CAP_ENCRYPT: bool = true; - const CAP_SIGN: bool = true; - - fn sign_data(&self, data: &[u8]) -> Option { - Some(rsa::sign_slice(data, &self.1).expect("Failed to sign")) - } - fn encrypt_key(&self, key: &aes::AesKey) -> Option<[u8; RSA_BLOCK_SIZE]> { - let mut output = [0u8; RSA_BLOCK_SIZE]; - let w = rsa::encrypt_slice_sync(key, &self.0, &mut &mut output[..]).expect("Failed to encrypt session key"); - assert_eq!(w, output.len()); - - Some(output) - } - } - impl MessageReceiver for Dec - { - fn verify_data(&self, data: &[u8], sig: &rsa::Signature) -> Option> { - Some(sig.verify_slice(data, &self.1).map_err(Into::into)) - } - fn decrypt_key(&self, enc_key: &[u8; RSA_BLOCK_SIZE]) -> Option> { - let mut output = aes::AesKey::empty(); - match rsa::decrypt_slice_sync(enc_key, &self.0, &mut output.as_mut()) { - Ok(sz) => assert_eq!(sz, - output.as_ref().len()), - Err(err) => return Some(Err(err.into())), - } - Some(Ok(output)) - } - } - - let enc = Enc(rsa_priv.get_public_parts(), rsa_priv2.clone()); - println!("Encrypting pub-key: {:?}", enc.0); - println!("Decrypting priv-key: {:?}", rsa_priv); - message_serial_generic(enc, Dec(rsa_priv, rsa_priv2.get_public_parts())); - } - - #[test] - fn rsa_bullshit() - { - use rsa::HasPublicComponents; - let rsa = rsa::openssl::rsa::Rsa::generate(cryptohelpers::consts::RSA_KEY_BITS).unwrap(); - eprintln!("rn: {}, re: {},", rsa.n(), rsa.e()); - let rsa_comp: rsa::RsaPrivateKey = rsa.clone().into(); - eprintln!("n: {:?}, e: {:?}", rsa_comp.n(), rsa_comp.e()); - let rsa2: rsa::openssl::rsa::Rsa<_> = rsa_comp.get_rsa_pub().unwrap(); - eprintln!("n2: {}, e2: {}", rsa2.n(), rsa2.e()); - - let mut data = Vec::new(); - data.extend_from_slice(aes::AesKey::generate().unwrap().as_ref()); - - let rend = rsa::encrypt_slice_to_vec(data, &rsa2).unwrap(); - - assert_eq!(rend.len(), RSA_BLOCK_SIZE); - } - //TODO: message_serial_sign(), message_serial_encrypted(), message_serial_encrypted_sign() -} +mod tests; diff --git a/src/message/tests.rs b/src/message/tests.rs new file mode 100644 index 0000000..07cfe7c --- /dev/null +++ b/src/message/tests.rs @@ -0,0 +1,163 @@ +use super::*; + +/// Generic message test function +fn message_serial_generic(s: S, d: D) +where S: MessageSender, + D: MessageReceiver +{ + eprintln!("=== Message serialisation with tc, rc: S: {}, D: {}", std::any::type_name::(), std::any::type_name::()); + let message = MessageBuilder::for_sender::() + .create(format!("This is a string, and some random data: {:?}", aes::AesKey::generate().unwrap())) + .expect("Failed to create message"); + println!(">> Created message: {:?}", message); + let serialised = message.serialise(s).expect("Failed to serialise message"); + println!(">> Serialised message: {:?}", serialised); + let binary = serialised.into_bytes(); + println!(">> Written to {} bytes", binary.len()); + let read = SerializedMessage::from_bytes(&binary).expect("Failed to read serialised message from binary"); + println!(">> Read from bytes: {:?}", read); + let deserialised = Message::deserialize(&read, d).expect("Failed to deserialise message"); + println!(">> Deserialised message: {:?}", deserialised); + + assert_eq!(message, deserialised, "Messages not identical"); + assert_eq!(message.into_value(), deserialised.into_value(), "Message values not identical"); + eprintln!("=== Passed (S: {}, D: {}) ===", std::any::type_name::(), std::any::type_name::()); +} + +#[test] +fn message_serial_basic() +{ + message_serial_generic((), ()); +} + + +#[test] +fn message_serial_sign() +{ + let rsa_priv = rsa::RsaPrivateKey::generate().unwrap(); + struct Sign(rsa::RsaPrivateKey); + struct Verify(rsa::RsaPublicKey); + + impl MessageSender for Sign + { + const CAP_SIGN: bool = true; + fn sign_data(&self, data: &[u8]) -> Option { + Some(rsa::sign_slice(data, &self.0).expect("Failed to sign")) + } + } + impl MessageReceiver for Verify + { + fn verify_data(&self, data: &[u8], sig: &rsa::Signature) -> Option> { + Some(sig.verify_slice(data, &self.0).map_err(Into::into)) + } + } + let verify = Verify(rsa_priv.get_public_parts()); + println!("Signing priv-key: {:?}", rsa_priv); + println!("Verifying pub-key: {:?}", verify.0); + message_serial_generic(Sign(rsa_priv), verify); +} + +#[test] +fn message_serial_encrypt() +{ + color_eyre::install().unwrap(); + let rsa_priv = rsa::RsaPrivateKey::generate().unwrap(); + struct Dec(rsa::RsaPrivateKey); + struct Enc(rsa::RsaPublicKey); + + + impl MessageSender for Enc + { + const CAP_ENCRYPT: bool = true; + + fn encrypt_key(&self, key: &aes::AesKey) -> Option<[u8; RSA_BLOCK_SIZE]> { + let mut output = [0u8; RSA_BLOCK_SIZE]; + let w = rsa::encrypt_slice_sync(key, &self.0, &mut &mut output[..]).expect("Failed to encrypt session key"); + assert_eq!(w, output.len()); + + Some(output) + } + } + impl MessageReceiver for Dec + { + fn decrypt_key(&self, enc_key: &[u8; RSA_BLOCK_SIZE]) -> Option> { + let mut output = aes::AesKey::empty(); + match rsa::decrypt_slice_sync(enc_key, &self.0, &mut output.as_mut()) { + Ok(sz) => assert_eq!(sz, + output.as_ref().len()), + Err(err) => return Some(Err(err.into())), + } + Some(Ok(output)) + } + } + + let enc = Enc(rsa_priv.get_public_parts()); + println!("Encrypting pub-key: {:?}", enc.0); + println!("Decrypting priv-key: {:?}", rsa_priv); + message_serial_generic(enc, Dec(rsa_priv)); +} +#[test] +fn message_serial_encrypt_sign() +{ + let rsa_priv = rsa::RsaPrivateKey::generate().unwrap(); + let rsa_priv2 = rsa::RsaPrivateKey::generate().unwrap(); + + struct Dec(rsa::RsaPrivateKey, rsa::RsaPublicKey); + struct Enc(rsa::RsaPublicKey, rsa::RsaPrivateKey); + + impl MessageSender for Enc + { + const CAP_ENCRYPT: bool = true; + const CAP_SIGN: bool = true; + + fn sign_data(&self, data: &[u8]) -> Option { + Some(rsa::sign_slice(data, &self.1).expect("Failed to sign")) + } + fn encrypt_key(&self, key: &aes::AesKey) -> Option<[u8; RSA_BLOCK_SIZE]> { + let mut output = [0u8; RSA_BLOCK_SIZE]; + let w = rsa::encrypt_slice_sync(key, &self.0, &mut &mut output[..]).expect("Failed to encrypt session key"); + assert_eq!(w, output.len()); + + Some(output) + } + } + impl MessageReceiver for Dec + { + fn verify_data(&self, data: &[u8], sig: &rsa::Signature) -> Option> { + Some(sig.verify_slice(data, &self.1).map_err(Into::into)) + } + fn decrypt_key(&self, enc_key: &[u8; RSA_BLOCK_SIZE]) -> Option> { + let mut output = aes::AesKey::empty(); + match rsa::decrypt_slice_sync(enc_key, &self.0, &mut output.as_mut()) { + Ok(sz) => assert_eq!(sz, + output.as_ref().len()), + Err(err) => return Some(Err(err.into())), + } + Some(Ok(output)) + } + } + + let enc = Enc(rsa_priv.get_public_parts(), rsa_priv2.clone()); + println!("Encrypting pub-key: {:?}", enc.0); + println!("Decrypting priv-key: {:?}", rsa_priv); + message_serial_generic(enc, Dec(rsa_priv, rsa_priv2.get_public_parts())); +} + +#[test] +fn rsa_bullshit() +{ + use rsa::HasPublicComponents; + let rsa = rsa::openssl::rsa::Rsa::generate(cryptohelpers::consts::RSA_KEY_BITS).unwrap(); + eprintln!("rn: {}, re: {},", rsa.n(), rsa.e()); + let rsa_comp: rsa::RsaPrivateKey = rsa.clone().into(); + eprintln!("n: {:?}, e: {:?}", rsa_comp.n(), rsa_comp.e()); + let rsa2: rsa::openssl::rsa::Rsa<_> = rsa_comp.get_rsa_pub().unwrap(); + eprintln!("n2: {}, e2: {}", rsa2.n(), rsa2.e()); + + let mut data = Vec::new(); + data.extend_from_slice(aes::AesKey::generate().unwrap().as_ref()); + + let rend = rsa::encrypt_slice_to_vec(data, &rsa2).unwrap(); + + assert_eq!(rend.len(), RSA_BLOCK_SIZE); +}