From 44ebf42aabe11f74fa43299fa74d547355274e38 Mon Sep 17 00:00:00 2001 From: pv42 Date: Thu, 22 Aug 2024 21:16:16 +0200 Subject: [PATCH] feat: add link id to signing config --- mavlink-core/src/signing.rs | 16 ++++++++++------ mavlink/tests/signing.rs | 6 +++--- mavlink/tests/tcp_loopback_tests.rs | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/mavlink-core/src/signing.rs b/mavlink-core/src/signing.rs index 6007901806..f1b129037c 100644 --- a/mavlink-core/src/signing.rs +++ b/mavlink-core/src/signing.rs @@ -9,6 +9,7 @@ use crate::MAVLINK_IFLAG_SIGNED; #[derive(Debug, Clone)] pub struct SigningConfig { secret_key: [u8; 32], + link_id: u8, pub(crate) sign_outgoing: bool, allow_unsigned: bool, } @@ -16,8 +17,6 @@ pub struct SigningConfig { // mutable state of signing per connection pub(crate) struct SigningState { timestamp: u64, - // currently link id is constant 0 - link_id: u8, stream_timestamps: HashMap<(u8, u8, u8), u64>, } @@ -28,9 +27,15 @@ pub struct SigningData { } impl SigningConfig { - pub fn new(secret_key: [u8; 32], sign_outgoing: bool, allow_unsigned: bool) -> Self { - SigningConfig { + pub fn new( + secret_key: [u8; 32], + link_id: u8, + sign_outgoing: bool, + allow_unsigned: bool, + ) -> Self { + Self { secret_key, + link_id, sign_outgoing, allow_unsigned, } @@ -43,7 +48,6 @@ impl SigningData { config, state: Mutex::new(SigningState { timestamp: 0, - link_id: 0, stream_timestamps: HashMap::new(), }), } @@ -108,7 +112,7 @@ impl SigningData { message .signature_timestamp_bytes_mut() .copy_from_slice(&ts_bytes[0..6]); - *message.signature_link_id_mut() = state.link_id; + *message.signature_link_id_mut() = self.config.link_id; let mut signature_buffer = [0u8; 6]; message.calculate_signature(&self.config.secret_key, &mut signature_buffer); diff --git a/mavlink/tests/signing.rs b/mavlink/tests/signing.rs index 5ac3837f9d..52ca63cd46 100644 --- a/mavlink/tests/signing.rs +++ b/mavlink/tests/signing.rs @@ -48,7 +48,7 @@ mod signing { #[test] pub fn test_verify() { - let signing_cfg = SigningConfig::new(SECRET_KEY, true, false); + let signing_cfg = SigningConfig::new(SECRET_KEY, 0, true, false); let signing_data = SigningData::from_config(signing_cfg); let mut r = PeekReader::new(HEARTBEAT_SIGNED); let msg = read_v2_raw_message::(&mut r).unwrap(); @@ -60,7 +60,7 @@ mod signing { #[test] pub fn test_invalid_ts() { - let signing_cfg = SigningConfig::new(SECRET_KEY, true, false); + let signing_cfg = SigningConfig::new(SECRET_KEY, 0, true, false); let signing_data = SigningData::from_config(signing_cfg); let mut r = PeekReader::new(HEARTBEAT_SIGNED); let mut msg = read_v2_raw_message::(&mut r).unwrap(); @@ -84,7 +84,7 @@ mod signing { }; message.serialize_message_for_signing(header, &heartbeat_message); - let signing_cfg = SigningConfig::new(SECRET_KEY, true, false); + let signing_cfg = SigningConfig::new(SECRET_KEY, 0, true, false); let signing_data = SigningData::from_config(signing_cfg); signing_data.sign_message(&mut message); assert!( diff --git a/mavlink/tests/tcp_loopback_tests.rs b/mavlink/tests/tcp_loopback_tests.rs index 7977c67256..3d68bf19be 100644 --- a/mavlink/tests/tcp_loopback_tests.rs +++ b/mavlink/tests/tcp_loopback_tests.rs @@ -15,7 +15,7 @@ mod test_tcp_connections { const RECEIVE_CHECK_COUNT: i32 = 5; #[cfg(feature = "signing")] - let singing_cfg_server = SigningConfig::new(test_shared::SECRET_KEY, true, false); + let singing_cfg_server = SigningConfig::new(test_shared::SECRET_KEY, 0, true, false); #[cfg(feature = "signing")] let singing_cfg_client = singing_cfg_server.clone();