Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes security an optinal default feature #68

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features
- name: Clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --no-default-features
format:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -49,6 +55,10 @@ jobs:
run: |
set -ex
cargo build
- name: Build no security
run: |
set -ex
cargo build --no-default-features
test:
runs-on: ubuntu-latest
steps:
Expand All @@ -63,4 +73,8 @@ jobs:
- name: Test
run: |
set -ex
cargo test
cargo test
- name: Test no security
run: |
set -ex
cargo test --no-default-features
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ categories = ["embedded", "network-programming", "no-std"]
keywords = ["WPAN"]

[features]
default = ["security"]
security = ["cipher", "ccm"]

[dependencies]
hash32 = "0.2.1"
hash32-derive = "0.1"
byte = "0.2.7"
defmt = { version = ">=0.2.0,<0.4", optional = true }
cipher = {version = "0.3.0", default-features = false, optional = true}
ccm = { version = "0.4.0", default-features = false, optional = true}

[dependencies.ccm]
version = "0.4.0"
default-features = false

[dependencies.cipher]
version = "0.3.0"
default-features = false

[dependencies.serde]
version = "1.0"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@

#[macro_use]
mod utils;
#[allow(unused_imports)]
pub mod mac;
44 changes: 31 additions & 13 deletions src/mac/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const ASSOCIATION_PERMIT: u8 = 0b1000_0000;
impl TryRead<'_> for SuperframeSpecification {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
check_len(&bytes, 2)?;
check_len(bytes, 2)?;
let byte: u8 = bytes.read(offset)?;
let beacon_order = BeaconOrder::from(byte & 0x0f);
let superframe_order = SuperframeOrder::from((byte >> 4) & 0x0f);
Expand Down Expand Up @@ -130,8 +130,8 @@ impl TryRead<'_> for SuperframeSpecification {
impl TryWrite for SuperframeSpecification {
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
let offset = &mut 0;
let bo = u8::from(self.beacon_order.clone());
let so = u8::from(self.superframe_order.clone());
let bo = u8::from(self.beacon_order);
let so = u8::from(self.superframe_order);
bytes.write(offset, (bo & 0x0f) | (so << 4))?;
let ble = if self.battery_life_extension {
BATTERY_LIFE_EXTENSION
Expand Down Expand Up @@ -189,10 +189,16 @@ impl GuaranteedTimeSlotDescriptor {
}
}

impl Default for GuaranteedTimeSlotDescriptor {
fn default() -> Self {
Self::new()
}
}

impl TryRead<'_> for GuaranteedTimeSlotDescriptor {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
check_len(&bytes, 3)?;
check_len(bytes, 3)?;
let short_address = bytes.read(offset)?;
let byte: u8 = bytes.read(offset)?;
let starting_slot = byte & 0x0f;
Expand Down Expand Up @@ -259,6 +265,12 @@ impl GuaranteedTimeSlotInformation {
}
}

impl Default for GuaranteedTimeSlotInformation {
fn default() -> Self {
Self::new()
}
}

impl TryWrite for GuaranteedTimeSlotInformation {
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
let offset = &mut 0;
Expand All @@ -275,9 +287,9 @@ impl TryWrite for GuaranteedTimeSlotInformation {
for n in 0..self.slot_count {
let slot = self.slots[n];
if slot.direction_transmit() {
direction_mask = direction_mask | dir;
direction_mask |= dir;
}
dir = dir << 1;
dir <<= 1;
}
direction_mask
};
Expand Down Expand Up @@ -307,7 +319,7 @@ impl TryRead<'_> for GuaranteedTimeSlotInformation {
if slot_count > 0 {
check_len(&bytes[*offset..], 2 + (3 * slot_count))?;
let mut direction_mask: u8 = bytes.read(offset)?;
for n in 0..slot_count {
for slot_target in slots.iter_mut().take(slot_count) {
let mut slot: GuaranteedTimeSlotDescriptor =
bytes.read(offset)?;
let direction = if direction_mask & 0b1 == 0b1 {
Expand All @@ -316,8 +328,8 @@ impl TryRead<'_> for GuaranteedTimeSlotInformation {
Direction::Receive
};
slot.set_direction(direction);
direction_mask = direction_mask >> 1;
slots[n] = slot;
direction_mask >>= 1;
*slot_target = slot;
}
}
Ok((
Expand Down Expand Up @@ -383,6 +395,12 @@ impl PendingAddress {
}
}

impl Default for PendingAddress {
fn default() -> Self {
Self::new()
}
}

impl TryRead<'_> for PendingAddress {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
Expand All @@ -393,12 +411,12 @@ impl TryRead<'_> for PendingAddress {
let el = ((byte & EXTENDED_MASK) >> 4) as usize;
check_len(&bytes[*offset..], (sl * ss) + (el * es))?;
let mut short_addresses = [ShortAddress::broadcast(); 7];
for n in 0..sl {
short_addresses[n] = bytes.read(offset)?;
for short_address in short_addresses.iter_mut().take(sl) {
*short_address = bytes.read(offset)?;
}
let mut extended_addresses = [ExtendedAddress::broadcast(); 7];
for n in 0..el {
extended_addresses[n] = bytes.read(offset)?;
for extended_address in extended_addresses.iter_mut().take(el) {
*extended_address = bytes.read(offset)?;
}
Ok((
Self {
Expand Down
16 changes: 8 additions & 8 deletions src/mac/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ impl From<CapabilityInformation> for u8 {
fn from(ar: CapabilityInformation) -> Self {
let mut byte = 0u8;
if ar.full_function_device {
byte = byte | CAP_FFD;
byte |= CAP_FFD
}
if ar.mains_power {
byte = byte | CAP_MAINS_POWER;
byte |= CAP_MAINS_POWER
}
if ar.idle_receive {
byte = byte | CAP_IDLE_RECEIVE;
byte |= CAP_IDLE_RECEIVE
}
if ar.frame_protection {
byte = byte | CAP_FRAME_PROTECTION;
byte |= CAP_FRAME_PROTECTION
}
if ar.allocate_address {
byte = byte | CAP_ALLOCATE_ADDRESS;
byte |= CAP_ALLOCATE_ADDRESS
}
byte
}
Expand Down Expand Up @@ -157,7 +157,7 @@ impl TryWrite for CoordinatorRealignmentData {
impl TryRead<'_> for CoordinatorRealignmentData {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
check_len(&bytes, 7)?;
check_len(bytes, 7)?;
let pan_id = bytes.read(offset)?;
let coordinator_address = bytes.read(offset)?;
let channel = bytes.read(offset)?;
Expand Down Expand Up @@ -213,10 +213,10 @@ impl From<GuaranteedTimeSlotCharacteristics> for u8 {
fn from(gtsc: GuaranteedTimeSlotCharacteristics) -> Self {
let mut byte = gtsc.count & 0x0f;
if gtsc.receive_only {
byte = byte | GTSC_RECEIVE_ONLY;
byte |= GTSC_RECEIVE_ONLY
}
if gtsc.allocation {
byte = byte | GTSC_ALLOCATION;
byte |= GTSC_ALLOCATION;
}
byte
}
Expand Down
101 changes: 79 additions & 22 deletions src/mac/frame/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
//!
//! [`Header`]: struct.Header.html

use byte::{check_len, BytesExt, TryRead, TryWrite, LE};
use cipher::{consts::U16, BlockCipher, NewBlockCipher};
use hash32_derive::Hash32;

use super::frame_control::{mask, offset};
pub use super::frame_control::{AddressMode, FrameType, FrameVersion};
use super::security::AuxiliarySecurityHeader;
use super::security::SecurityContext;
use super::DecodeError;
use super::{
frame_control::{mask, offset},
security::{KeyDescriptorLookup, SecurityContext},
use crate::mac::frame::EncodeError;
#[cfg(not(feature = "security"))]
use crate::mac::frame::FrameSerDesContext;
use byte::{check_len, BytesExt, TryRead, TryWrite, LE};
use hash32_derive::Hash32;
#[cfg(feature = "security")]
use {
super::security::KeyDescriptorLookup,
cipher::{consts::U16, BlockCipher, NewBlockCipher},
};
use super::{security::AuxiliarySecurityHeader, EncodeError};

/// MAC frame header
///
/// External documentation for [MAC frame format start at 5.2]
Expand Down Expand Up @@ -88,18 +91,13 @@ impl Header {
// Frame control + sequence number
let mut len = 3;

for i in [self.destination, self.source].iter() {
match i {
Some(addr) => {
// pan ID
len += 2;
// Address length
match addr {
Address::Short(..) => len += 2,
Address::Extended(..) => len += 8,
}
}
_ => {}
for addr in [self.destination, self.source].iter().flatten() {
// pan ID
len += 2;
// Address length
match addr {
Address::Short(..) => len += 2,
Address::Extended(..) => len += 8,
}
}
len
Expand All @@ -115,7 +113,7 @@ impl TryRead<'_> for Header {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
// Make sure we have enough buffer for the Frame Control field
check_len(&bytes, 3)?;
check_len(bytes, 3)?;

/* Decode Frame Control Field */
let bits: u16 = bytes.read_with(offset, LE)?;
Expand Down Expand Up @@ -230,6 +228,7 @@ impl TryRead<'_> for Header {
}
}

#[cfg(feature = "security")]
impl<AEADBLKCIPH, KEYDESCLO>
TryWrite<&Option<&mut SecurityContext<AEADBLKCIPH, KEYDESCLO>>> for Header
where
Expand Down Expand Up @@ -306,6 +305,64 @@ where
}
}

#[cfg(not(feature = "security"))]
impl TryWrite<&Option<&mut SecurityContext>> for Header {
fn try_write(
self,
bytes: &mut [u8],
_sec_ctx: &Option<&mut SecurityContext>,
) -> byte::Result<usize> {
let offset = &mut 0;
let dest_addr_mode = AddressMode::from(self.destination);
let src_addr_mode = AddressMode::from(self.source);

let security = self.auxiliary_security_header.is_some();

let frame_control_raw = (self.frame_type as u16) << offset::FRAME_TYPE
| (security as u16) << offset::SECURITY
| (self.frame_pending as u16) << offset::PENDING
| (self.ack_request as u16) << offset::ACK
| (self.pan_id_compress as u16) << offset::PAN_ID_COMPRESS
| (dest_addr_mode as u16) << offset::DEST_ADDR_MODE
| (self.version as u16) << offset::VERSION
| (src_addr_mode as u16) << offset::SRC_ADDR_MODE;

bytes.write_with(offset, frame_control_raw, LE)?;

// Write Sequence Number
bytes.write(offset, self.seq)?;

if (self.destination.is_none() || self.source.is_none())
&& self.pan_id_compress
{
return Err(EncodeError::DisallowedPanIdCompress)?;
}

// Write addresses
if let Some(destination) = self.destination {
bytes.write_with(offset, destination, AddressEncoding::Normal)?;
}

match (self.source, self.pan_id_compress) {
(Some(source), true) => {
bytes.write_with(
offset,
source,
AddressEncoding::Compressed,
)?;
}
(Some(source), false) => {
bytes.write_with(offset, source, AddressEncoding::Normal)?;
}
(None, true) => {
panic!("frame control request compress source address without contain this address")
}
(None, false) => (),
}
Ok(*offset)
}
}

/// Personal Area Network Identifier
///
/// A 16-bit value that identifies a PAN
Expand Down
Loading
Loading