diff --git a/sachy-crypto/src/lib.rs b/sachy-crypto/src/lib.rs --- a/sachy-crypto/src/lib.rs +++ b/sachy-crypto/src/lib.rs @@ -153,12 +153,12 @@ } impl SendingState<'_> { - pub fn encrypt(&mut self, msg: &mut alloc::vec::Vec) -> Result<(), ProtoError> { + pub fn encrypt(&mut self, msg: &mut alloc::vec::Vec, associated_data: &[u8]) -> Result<(), ProtoError> { let counter = self.counter.to_be_bytes(); self.transport.encrypt_in_place( &self.transport.mix_nonce(&counter, Role::Client), - &counter, + associated_data, msg, )?; @@ -180,12 +180,12 @@ } impl ReceivingState<'_> { - pub fn decrypt(&mut self, msg: &mut alloc::vec::Vec) -> Result<(), ProtoError> { + pub fn decrypt(&mut self, msg: &mut alloc::vec::Vec, associated_data: &[u8]) -> Result<(), ProtoError> { let counter = self.counter.to_be_bytes(); self.transport.decrypt_in_place( &self.transport.mix_nonce(&counter, Role::Server), - &counter, + associated_data, msg, )?; @@ -387,33 +387,35 @@ let orig = b"Test Message, Please ignore."; + let ad = b"random"; + let mut msg = orig.to_vec(); // a -> b - alice_send.encrypt(&mut msg)?; + alice_send.encrypt(&mut msg, ad)?; assert_ne!(orig.as_slice(), msg.as_slice()); let ct1 = msg.clone(); - bob_recv.decrypt(&mut msg)?; + bob_recv.decrypt(&mut msg, ad)?; // a -> b - alice_send.encrypt(&mut msg)?; + alice_send.encrypt(&mut msg, b"")?; assert_ne!(msg.as_slice(), ct1.as_slice()); let ct2 = msg.clone(); - bob_recv.decrypt(&mut msg)?; + bob_recv.decrypt(&mut msg, b"")?; // b -> a - bob_send.encrypt(&mut msg)?; + bob_send.encrypt(&mut msg, ad)?; // None of the ciphertexts should match each other assert_ne!(msg.as_slice(), ct1.as_slice()); assert_ne!(msg.as_slice(), ct2.as_slice()); assert_ne!(ct1.as_slice(), ct2.as_slice()); - alice_recv.decrypt(&mut msg)?; + alice_recv.decrypt(&mut msg, ad)?; assert_eq!(orig.as_slice(), msg.as_slice());