Skip to content

Commit

Permalink
feat: add link id to signing config
Browse files Browse the repository at this point in the history
  • Loading branch information
pv42 committed Aug 22, 2024
1 parent 27469bf commit 44ebf42
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
16 changes: 10 additions & 6 deletions mavlink-core/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ 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,
}

// 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>,
}

Expand All @@ -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,
}
Expand All @@ -43,7 +48,6 @@ impl SigningData {
config,
state: Mutex::new(SigningState {
timestamp: 0,
link_id: 0,
stream_timestamps: HashMap::new(),
}),
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions mavlink/tests/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<mavlink::common::MavMessage, _>(&mut r).unwrap();
Expand All @@ -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::<mavlink::common::MavMessage, _>(&mut r).unwrap();
Expand All @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion mavlink/tests/tcp_loopback_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 44ebf42

Please sign in to comment.