Skip to content

Commit

Permalink
update kwp to prerelease
Browse files Browse the repository at this point in the history
  • Loading branch information
makavity committed Jul 28, 2024
1 parent a64b0ae commit f0dd452
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/belt-kwp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand Down
63 changes: 53 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ members = [

[profile.dev]
opt-level = 2

#[patch.crates-io]
#belt-block = { git = "https://github.com/RustCrypto/block-ciphers.git" }
6 changes: 3 additions & 3 deletions belt-kwp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
14 changes: 7 additions & 7 deletions belt-kwp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![warn(missing_docs, rust_2018_idioms)]

mod error;
mod utils;

pub use error::{Error, Result};

Expand All @@ -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
Expand All @@ -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<GenericArray<u8, U32>> for BeltKwp {
fn from(kek: GenericArray<u8, U32>) -> Self {
impl From<Array<u8, U32>> for BeltKwp {
fn from(kek: Array<u8, U32>) -> Self {
BeltKwp::new(&kek)
}
}
Expand All @@ -49,7 +49,7 @@ impl TryFrom<&[u8]> for BeltKwp {

fn try_from(value: &[u8]) -> Result<Self> {
if value.len() == 32 {
Ok(BeltKwp::new(value.into()))
Ok(BeltKwp::new(value.try_into().unwrap()))
} else {
Err(Error::InvalidKekSize { size: value.len() })
}
Expand All @@ -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<u8, U32>) -> Self {
pub fn new(key: &Array<u8, U32>) -> Self {
Self {
key: to_u32::<8>(key),
key: utils::to_u32::<8>(key),
}
}

Expand Down
14 changes: 14 additions & 0 deletions belt-kwp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<const N: usize>(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
}

0 comments on commit f0dd452

Please sign in to comment.