Skip to content

Commit

Permalink
Updated ieee802154, Updated formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
blueluna committed Dec 26, 2023
1 parent 892f447 commit 26f119f
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 153 deletions.
58 changes: 38 additions & 20 deletions psila-crypto-rust-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
use psila_crypto::{CryptoBackend, Error, BLOCK_SIZE};

use aes::{
cipher::{BlockEncrypt, NewBlockCipher},
Aes128,
cipher::{BlockEncrypt, NewBlockCipher}
};
use ccm::{
aead::{generic_array::GenericArray, AeadInPlace, NewAead},
consts::{U13, U16, U4, U8},
Ccm,
};


#[cfg(test)]
mod test;

Expand All @@ -26,9 +25,7 @@ pub struct RustCryptoBackend {

impl Default for RustCryptoBackend {
fn default() -> Self {
Self {
cipher: None,
}
Self { cipher: None }
}
}

Expand Down Expand Up @@ -58,7 +55,11 @@ impl CryptoBackend for RustCryptoBackend {
}
4 => {
let cipher = AesCcmMic4::new(key);
match cipher.encrypt_in_place_detached(nonce, additional_data, &mut message_output[..payload_len]) {
match cipher.encrypt_in_place_detached(
nonce,
additional_data,
&mut message_output[..payload_len],
) {
Ok(tag) => {
mic.copy_from_slice(tag.as_slice());
Ok(payload_len)
Expand All @@ -68,7 +69,11 @@ impl CryptoBackend for RustCryptoBackend {
}
8 => {
let cipher = AesCcmMic8::new(key);
match cipher.encrypt_in_place_detached(nonce, additional_data, &mut message_output[..payload_len]) {
match cipher.encrypt_in_place_detached(
nonce,
additional_data,
&mut message_output[..payload_len],
) {
Ok(tag) => {
mic.copy_from_slice(tag.as_slice());
Ok(payload_len)
Expand All @@ -78,7 +83,11 @@ impl CryptoBackend for RustCryptoBackend {
}
16 => {
let cipher = AesCcmMic16::new(key);
match cipher.encrypt_in_place_detached(nonce, additional_data, &mut message_output[..payload_len]) {
match cipher.encrypt_in_place_detached(
nonce,
additional_data,
&mut message_output[..payload_len],
) {
Ok(tag) => {
mic.copy_from_slice(tag.as_slice());
Ok(payload_len)
Expand Down Expand Up @@ -110,29 +119,42 @@ impl CryptoBackend for RustCryptoBackend {
let payload_len = message.len();
message_output[..payload_len].copy_from_slice(&message);
match mic.len() {
0 => {
Err(Error::NotImplemented)
}
0 => Err(Error::NotImplemented),
4 => {
let tag: &GenericArray<u8, U4> = GenericArray::from_slice(mic);
let cipher = AesCcmMic4::new(key);
match cipher.decrypt_in_place_detached(nonce, additional_data, &mut message_output[..payload_len], tag) {
match cipher.decrypt_in_place_detached(
nonce,
additional_data,
&mut message_output[..payload_len],
tag,
) {
Ok(_) => Ok(payload_len),
Err(_e) => Err(Error::BackendError),
}
}
8 => {
let tag: &GenericArray<u8, U8> = GenericArray::from_slice(mic);
let cipher = AesCcmMic8::new(key);
match cipher.decrypt_in_place_detached(nonce, additional_data, &mut message_output[..payload_len], tag) {
match cipher.decrypt_in_place_detached(
nonce,
additional_data,
&mut message_output[..payload_len],
tag,
) {
Ok(_) => Ok(payload_len),
Err(_e) => Err(Error::BackendError),
}
}
16 => {
let tag: &GenericArray<u8, U16> = GenericArray::from_slice(mic);
let cipher = AesCcmMic16::new(key);
match cipher.decrypt_in_place_detached(nonce, additional_data, &mut message_output[..payload_len], tag) {
match cipher.decrypt_in_place_detached(
nonce,
additional_data,
&mut message_output[..payload_len],
tag,
) {
Ok(_) => Ok(payload_len),
Err(_e) => Err(Error::BackendError),
}
Expand All @@ -147,9 +169,7 @@ impl CryptoBackend for RustCryptoBackend {
self.cipher = Some(aes);
Ok(())
}
Err(_) => {
Err(Error::InvalidKeySize)
}
Err(_) => Err(Error::InvalidKeySize),
}
}

Expand All @@ -165,8 +185,7 @@ impl CryptoBackend for RustCryptoBackend {
output.copy_from_slice(input);
let block: &mut GenericArray<u8, U16> = GenericArray::from_mut_slice(output);
cipher.encrypt_block(block);
}
else {
} else {
return Err(Error::InvalidKey);
}
Ok(())
Expand All @@ -176,4 +195,3 @@ impl CryptoBackend for RustCryptoBackend {
self.aes128_ecb_encrypt_process_block(input, output)
}
}

42 changes: 20 additions & 22 deletions psila-crypto-rust-crypto/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use psila_crypto::CryptoBackend;
use crate::RustCryptoBackend;
use psila_crypto::CryptoBackend;

#[test]
fn test_key_hash_default_link_key() {
Expand All @@ -13,8 +13,8 @@ fn test_key_hash_default_link_key() {
.unwrap();

let correct_key = [
0x4b, 0xab, 0x0f, 0x17, 0x3e, 0x14, 0x34, 0xa2, 0xd5, 0x72, 0xe1, 0xc1, 0xef, 0x47,
0x87, 0x82,
0x4b, 0xab, 0x0f, 0x17, 0x3e, 0x14, 0x34, 0xa2, 0xd5, 0x72, 0xe1, 0xc1, 0xef, 0x47, 0x87,
0x82,
];

assert_eq!(hashed_key, correct_key);
Expand All @@ -27,36 +27,34 @@ fn test_key_hash_2() {

// Specification test vectors, C.6.1 Test Vector Set 1
let key = [
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D,
0x4E, 0x4F,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E,
0x4F,
];
let mut hashed_key = [0u8; 16];
provider.hash_key(&key, 0xc0, &mut hashed_key).unwrap();
let correct_key = [
0x45, 0x12, 0x80, 0x7B, 0xF9, 0x4C, 0xB3, 0x40, 0x0F, 0x0E, 0x2C, 0x25, 0xFB, 0x76,
0xE9, 0x99,
0x45, 0x12, 0x80, 0x7B, 0xF9, 0x4C, 0xB3, 0x40, 0x0F, 0x0E, 0x2C, 0x25, 0xFB, 0x76, 0xE9,
0x99,
];

assert_eq!(hashed_key, correct_key);
}



#[test]
fn test_decryption_and_authentication_check_1() {
let mut crypt = RustCryptoBackend::default();

let key = [
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD,
0xCE, 0xCF,
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
0xCF,
];
let nonce = [
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0x03, 0x02, 0x01, 0x00, 0x06,
];
const ENCRYPTED: [u8; 31] = [
0x1A, 0x55, 0xA3, 0x6A, 0xBB, 0x6C, 0x61, 0x0D, 0x06, 0x6B, 0x33, 0x75, 0x64, 0x9C,
0xEF, 0x10, 0xD4, 0x66, 0x4E, 0xCA, 0xD8, 0x54, 0xA8, 0x0A, 0x89, 0x5C, 0xC1, 0xD8,
0xFF, 0x94, 0x69,
0x1A, 0x55, 0xA3, 0x6A, 0xBB, 0x6C, 0x61, 0x0D, 0x06, 0x6B, 0x33, 0x75, 0x64, 0x9C, 0xEF,
0x10, 0xD4, 0x66, 0x4E, 0xCA, 0xD8, 0x54, 0xA8, 0x0A, 0x89, 0x5C, 0xC1, 0xD8, 0xFF, 0x94,
0x69,
];
let a = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
// M, length of the authentication field in octets 0, 4, 6, 8, 10, 12, 14, 16
Expand All @@ -73,8 +71,8 @@ fn test_decryption_and_authentication_check_1() {
assert_eq!(used, 23);

const CLEAR_TEXT: [u8; 23] = [
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
];

assert_eq!(message, CLEAR_TEXT);
Expand All @@ -85,19 +83,19 @@ fn test_encryption_and_authentication_check_1() {
let mut crypt = RustCryptoBackend::default();

let key = [
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD,
0xCE, 0xCF,
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
0xCF,
];
let nonce = [
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0x03, 0x02, 0x01, 0x00, 0x06,
];
const CLEAR_TEXT: [u8; 23] = [
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
];
const ENCRYPTED: [u8; 23] = [
0x1A, 0x55, 0xA3, 0x6A, 0xBB, 0x6C, 0x61, 0x0D, 0x06, 0x6B, 0x33, 0x75, 0x64, 0x9C,
0xEF, 0x10, 0xD4, 0x66, 0x4E, 0xCA, 0xD8, 0x54, 0xA8,
0x1A, 0x55, 0xA3, 0x6A, 0xBB, 0x6C, 0x61, 0x0D, 0x06, 0x6B, 0x33, 0x75, 0x64, 0x9C, 0xEF,
0x10, 0xD4, 0x66, 0x4E, 0xCA, 0xD8, 0x54, 0xA8,
];
let mic = [0x0A, 0x89, 0x5C, 0xC1, 0xD8, 0xFF, 0x94, 0x69];
let a = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
Expand Down
7 changes: 4 additions & 3 deletions psila-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ default = []
core = ["heapless"]

[dependencies]
byte = "0.2.4"
byteorder = { version = "1", default-features = false }
bitflags = "1.3"
hash32 = "0.2"
hash32-derive = "0.1"
heapless = { version = "0.7", optional = true }
ieee802154 = "0.3"
psila-crypto = { path = "../psila-crypto" }
heapless = { version = "0.7.7", optional = true }
ieee802154 = { git = "https://github.com/blueluna/ieee-802.15.4.git" }
psila-crypto = { path = "../psila-crypto" }
3 changes: 1 addition & 2 deletions psila-data/src/cluster_library/commands/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ impl Pack<DiscoverAttributes, Error> for DiscoverAttributes {
pub type DiscoverAttributeVec = std::vec::Vec<(AttributeIdentifier, AttributeDataType)>;

#[cfg(feature = "core")]
pub type DiscoverAttributeVec =
heapless::Vec<(AttributeIdentifier, AttributeDataType), 16>;
pub type DiscoverAttributeVec = heapless::Vec<(AttributeIdentifier, AttributeDataType), 16>;

/// Discover attributes response
#[derive(Clone, Debug, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions psila-data/src/cluster_library/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extended_enum!(
WriteAttributesResponse => 0x04,
/// Write attributes, do not generate a response
WriteAttributesNoResponse => 0x05,
/// Configure reporting request for attribtues
/// Configure reporting request for attributes
ConfigureReporting => 0x06,
/// Report configuration response
ConfigureReportingResponse => 0x07,
Expand All @@ -44,7 +44,7 @@ extended_enum!(
ReportAttributes => 0x0a,
/// Default response
DefaultResponse => 0x0b,
/// Disover attribtues request
/// Discover attributes request
DiscoverAttributes => 0x0c,
/// Discover attributes response
DiscoverAttributesResponse => 0x0d,
Expand Down
6 changes: 5 additions & 1 deletion psila-data/src/common/types/types_core.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::convert::TryFrom;
use heapless::{self, String, Vec};

use crate::pack::Pack;
Expand Down Expand Up @@ -52,7 +53,10 @@ impl Pack<CharacterString, Error> for CharacterString {
return Err(Error::WrongNumberOfBytes);
}
match core::str::from_utf8(&data[1..=length]) {
Ok(value) => Ok((CharacterString::from(value), length + 1)),
Ok(value) => match CharacterString::try_from(value) {
Ok(string) => Ok((string, length + 1)),
Err(_) => Err(Error::InvalidValue),
},
Err(_) => Err(Error::InvalidValue),
}
}
Expand Down
5 changes: 4 additions & 1 deletion psila-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub use utils::clear;

#[cfg(all(test, not(feature = "core")))]
mod tests {
use byte::BytesExt;
use ieee802154::mac::{self};

use super::application_service::ApplicationServiceHeader;
Expand All @@ -54,7 +55,9 @@ mod tests {
0x12, 0x67, 0x4c, 0xf4, 0x8d, 0xce, 0xa0, 0xa0, 0x70, 0x0f, 0x0b, 0xcd, 0xbc, 0x0a,
0xf4,
];
let mac = mac::Frame::decode(&data[..], false).unwrap();
let mac = data
.read_with::<mac::Frame>(&mut 0, mac::FooterMode::None)
.unwrap();
let payload = mac.payload;
let (_nwk, used) = NetworkHeader::unpack(&payload[..]).unwrap();
let payload = &payload[used..];
Expand Down
2 changes: 1 addition & 1 deletion psila-data/src/network/commands/route_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct RouteRequest {
pub destination_address: AddressType,
/// Path cost
pub path_cost: u8,
/// Optional extended destionation address
/// Optional extended destination address
pub destination_ieee_address: Option<ExtendedAddress>,
}

Expand Down
1 change: 0 additions & 1 deletion psila-data/src/security/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ where
&aad,
&mut output_payload,
)?;

Ok(used)
}

Expand Down
5 changes: 3 additions & 2 deletions psila-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ authors = ["Erik Svensson <erik.public@gmail.com>"]
edition = "2018"

[dependencies]
clap = "2.32"
byte = "0.2.4"
byteorder = { version = "1", default-features = false }
clap = "2.32"
slice-deque = "0.3"
ieee802154 = "0.3"
ieee802154 = { git = "https://github.com/blueluna/ieee-802.15.4.git" }
esercom = { git = "https://github.com/blueluna/esercom.git", branch = "master" }
psila-data = { path = "../psila-data" }
psila-crypto-rust-crypto = { path = "../psila-crypto-rust-crypto" }
Expand Down
Loading

0 comments on commit 26f119f

Please sign in to comment.