Repo of no-std crates for my personal embedded projects
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Allow associated data param for encrypt/decrypt

+12 -10
+12 -10
sachy-crypto/src/lib.rs
··· 153 153 } 154 154 155 155 impl SendingState<'_> { 156 - pub fn encrypt(&mut self, msg: &mut alloc::vec::Vec<u8>) -> Result<(), ProtoError> { 156 + pub fn encrypt(&mut self, msg: &mut alloc::vec::Vec<u8>, associated_data: &[u8]) -> Result<(), ProtoError> { 157 157 let counter = self.counter.to_be_bytes(); 158 158 159 159 self.transport.encrypt_in_place( 160 160 &self.transport.mix_nonce(&counter, Role::Client), 161 - &counter, 161 + associated_data, 162 162 msg, 163 163 )?; 164 164 ··· 180 180 } 181 181 182 182 impl ReceivingState<'_> { 183 - pub fn decrypt(&mut self, msg: &mut alloc::vec::Vec<u8>) -> Result<(), ProtoError> { 183 + pub fn decrypt(&mut self, msg: &mut alloc::vec::Vec<u8>, associated_data: &[u8]) -> Result<(), ProtoError> { 184 184 let counter = self.counter.to_be_bytes(); 185 185 186 186 self.transport.decrypt_in_place( 187 187 &self.transport.mix_nonce(&counter, Role::Server), 188 - &counter, 188 + associated_data, 189 189 msg, 190 190 )?; 191 191 ··· 387 387 388 388 let orig = b"Test Message, Please ignore."; 389 389 390 + let ad = b"random"; 391 + 390 392 let mut msg = orig.to_vec(); 391 393 392 394 // a -> b 393 - alice_send.encrypt(&mut msg)?; 395 + alice_send.encrypt(&mut msg, ad)?; 394 396 395 397 assert_ne!(orig.as_slice(), msg.as_slice()); 396 398 let ct1 = msg.clone(); 397 399 398 - bob_recv.decrypt(&mut msg)?; 400 + bob_recv.decrypt(&mut msg, ad)?; 399 401 400 402 // a -> b 401 - alice_send.encrypt(&mut msg)?; 403 + alice_send.encrypt(&mut msg, b"")?; 402 404 403 405 assert_ne!(msg.as_slice(), ct1.as_slice()); 404 406 let ct2 = msg.clone(); 405 407 406 - bob_recv.decrypt(&mut msg)?; 408 + bob_recv.decrypt(&mut msg, b"")?; 407 409 408 410 // b -> a 409 - bob_send.encrypt(&mut msg)?; 411 + bob_send.encrypt(&mut msg, ad)?; 410 412 411 413 // None of the ciphertexts should match each other 412 414 assert_ne!(msg.as_slice(), ct1.as_slice()); 413 415 assert_ne!(msg.as_slice(), ct2.as_slice()); 414 416 assert_ne!(ct1.as_slice(), ct2.as_slice()); 415 417 416 - alice_recv.decrypt(&mut msg)?; 418 + alice_recv.decrypt(&mut msg, ad)?; 417 419 418 420 assert_eq!(orig.as_slice(), msg.as_slice()); 419 421