diff --git a/.github/workflows/belt-kwp.yml b/.github/workflows/belt-kwp.yml index 8e18fc3..16f9f09 100644 --- a/.github/workflows/belt-kwp.yml +++ b/.github/workflows/belt-kwp.yml @@ -22,7 +22,7 @@ jobs: strategy: matrix: rust: - - 1.56.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi diff --git a/Cargo.lock b/Cargo.lock index 217d170..f637610 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" dependencies = [ "cfg-if", - "cipher", + "cipher 0.4.3", "cpufeatures", ] @@ -19,16 +19,16 @@ name = "aes-kw" version = "0.2.1" dependencies = [ "aes", - "hex-literal", + "hex-literal 0.3.4", ] [[package]] name = "belt-block" -version = "0.1.2" +version = "0.2.0-pre.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9aa1eef3994e2ccd304a78fe3fea4a73e5792007f85f09b79bb82143ca5f82b" +checksum = "3342994f00336827ff9ae0e20e21d38eb88aa4e40aa1795a2a4d14d7af264a5b" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] @@ -36,7 +36,7 @@ name = "belt-kwp" version = "0.0.0" dependencies = [ "belt-block", - "hex-literal", + "hex-literal 0.4.1", ] [[package]] @@ -51,8 +51,18 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d1873270f8f7942c191139cb8a40fd228da6c3fd2fc376d7e92d47aa14aeb59e" dependencies = [ - "crypto-common", - "inout", + "crypto-common 0.1.6", + "inout 0.1.3", +] + +[[package]] +name = "cipher" +version = "0.5.0-pre.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c71c893d5a1e8257048dbb29954d2e1f85f091a150304f1defe4ca2806da5d3f" +dependencies = [ + "crypto-common 0.2.0-rc.0", + "inout 0.2.0-rc.0", ] [[package]] @@ -74,6 +84,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.0-rc.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c070b79a496dccd931229780ad5bbedd535ceff6c3565605a8e440e18e1aa2b" +dependencies = [ + "hybrid-array", +] + [[package]] name = "generic-array" version = "0.14.6" @@ -90,6 +109,21 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "hybrid-array" +version = "0.2.0-rc.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d306b679262030ad8813a82d4915fc04efff97776e4db7f8eb5137039d56400" +dependencies = [ + "typenum", +] + [[package]] name = "inout" version = "0.1.3" @@ -99,6 +133,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "inout" +version = "0.2.0-rc.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc33218cf9ce7b927426ee4ad3501bcc5d8c26bf5fb4a82849a083715aca427" +dependencies = [ + "hybrid-array", +] + [[package]] name = "libc" version = "0.2.134" @@ -107,9 +150,9 @@ checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb" [[package]] name = "typenum" -version = "1.15.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "version_check" diff --git a/Cargo.toml b/Cargo.toml index 429f12e..d39dbe5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,6 @@ members = [ [profile.dev] opt-level = 2 + +#[patch.crates-io] +#belt-block = { git = "https://github.com/RustCrypto/block-ciphers.git" } \ No newline at end of file diff --git a/belt-kwp/Cargo.toml b/belt-kwp/Cargo.toml index 3ccafcb..7cae308 100644 --- a/belt-kwp/Cargo.toml +++ b/belt-kwp/Cargo.toml @@ -10,13 +10,13 @@ keywords = ["crypto", "BELT-KW", "KW", "BELT-KWP", "KWP"] categories = ["cryptography", "no-std"] readme = "README.md" edition = "2021" -rust-version = "1.56" +rust-version = "1.65" [dependencies] -belt-block = "0.1.2" +belt-block = "=0.2.0-pre.1" [dev-dependencies] -hex-literal = "0.3" +hex-literal = "0.4" [features] alloc = [] diff --git a/belt-kwp/src/lib.rs b/belt-kwp/src/lib.rs index d7944d2..4a14344 100644 --- a/belt-kwp/src/lib.rs +++ b/belt-kwp/src/lib.rs @@ -9,6 +9,7 @@ #![warn(missing_docs, rust_2018_idioms)] mod error; +mod utils; pub use error::{Error, Result}; @@ -22,8 +23,7 @@ extern crate std; use alloc::vec::Vec; use belt_block::{ belt_wblock_dec, belt_wblock_enc, - cipher::{consts::U32, generic_array::GenericArray}, - to_u32, + cipher::{consts::U32, array::Array}, }; /// Block size for BelT-KWP @@ -32,8 +32,8 @@ pub const SEMIBLOCK_LEN: usize = 8; /// Size of an BelT-block "semiblock" in bytes. pub const IV_LEN: usize = 16; -impl From> for BeltKwp { - fn from(kek: GenericArray) -> Self { +impl From> for BeltKwp { + fn from(kek: Array) -> Self { BeltKwp::new(&kek) } } @@ -49,7 +49,7 @@ impl TryFrom<&[u8]> for BeltKwp { fn try_from(value: &[u8]) -> Result { if value.len() == 32 { - Ok(BeltKwp::new(value.into())) + Ok(BeltKwp::new(value.try_into().unwrap())) } else { Err(Error::InvalidKekSize { size: value.len() }) } @@ -66,9 +66,9 @@ pub struct BeltKwp { impl BeltKwp { /// Constructs a new Kek based on the appropriate raw key material. - pub fn new(key: &GenericArray) -> Self { + pub fn new(key: &Array) -> Self { Self { - key: to_u32::<8>(key), + key: utils::to_u32::<8>(key), } } diff --git a/belt-kwp/src/utils.rs b/belt-kwp/src/utils.rs new file mode 100644 index 0000000..e105256 --- /dev/null +++ b/belt-kwp/src/utils.rs @@ -0,0 +1,14 @@ +/// Helper function for transforming BelT keys and blocks from a byte array +/// to an array of `u32`s. +/// +/// # Panics +/// If length of `src` is not equal to `4 * N`. +// #[inline(always)] +pub(crate) fn to_u32(src: &[u8]) -> [u32; N] { + assert_eq!(src.len(), 4 * N); + let mut res = [0u32; N]; + res.iter_mut() + .zip(src.chunks_exact(4)) + .for_each(|(dst, src)| *dst = u32::from_le_bytes(src.try_into().unwrap())); + res +}