Skip to content
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
71 changes: 71 additions & 0 deletions wrapper/rust/wolfssl-wolfcrypt/Cargo.lock

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

11 changes: 10 additions & 1 deletion wrapper/rust/wolfssl-wolfcrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,31 @@ std = []
rand_core = ["dep:rand_core"]
aead = ["dep:aead"]
cipher = ["dep:cipher"]
mac = ["digest/mac"]
digest = ["dep:digest"]
signature = ["dep:signature"]
password-hash = ["dep:password-hash", "password-hash/phc"]
kem = ["dep:kem", "hybrid-array/extra-sizes"]

[dependencies]
rand_core = { version = "0.10", optional = true, default-features = false }
aead = { version = "0.5", optional = true, default-features = false }
cipher = { version = "0.5", optional = true, default-features = false }
digest = { version = "0.11", optional = true, default-features = false, features = ["block-api"] }
signature = { version = "2.2", optional = true, default-features = false }
num-traits = { version = "0.2", default-features = false }
zeroize = { version = "1.3", default-features = false, features = ["derive"] }
password-hash = { version = "0.6.1", optional = true, default-features = false }
kem = { version = "0.3", optional = true, default-features = false }
hybrid-array = { version = "0.4.7", optional = true, default-features = false }

[dev-dependencies]
aead = { version = "0.5", features = ["alloc", "dev"] }
cipher = "0.5"
digest = { version = "0.11", features = ["dev"] }
digest = { version = "0.11", features = ["dev", "mac"] }
signature = "2.2"
password-hash = { version = "0.6.1", features = ["phc"] }
kem = "0.3"

[build-dependencies]
bindgen = "0.72.1"
Expand Down
2 changes: 1 addition & 1 deletion wrapper/rust/wolfssl-wolfcrypt/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FEATURES := rand_core,aead,cipher,digest,signature
FEATURES := rand_core,aead,cipher,digest,mac,signature,password-hash,kem
CARGO_FEATURE_FLAGS := --features $(FEATURES)

.PHONY: all
Expand Down
99 changes: 99 additions & 0 deletions wrapper/rust/wolfssl-wolfcrypt/src/cmac_mac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

/*!
RustCrypto `digest::Mac` trait implementations for the wolfCrypt CMAC types.

This module provides typed AES-CMAC wrappers with implementations of the
traits from the `digest` crate (`MacMarker`, `KeyInit`, `Update`,
`FixedOutput`) for each AES key size (128, 192, 256). With these
implementations the `digest::Mac` trait becomes available via its blanket
implementation, allowing these CMAC types to be used anywhere a RustCrypto
`Mac` is accepted.

Any failure returned by the underlying wolfCrypt call in a trait method will
result in a panic, matching the infallible signatures required by the
RustCrypto traits.
*/

use digest::consts::{U16, U24, U32};

macro_rules! impl_cmac_mac {
(
$(#[$attr:meta])*
$name:ident, key = $key_size:ty
) => {
$(#[$attr])*
pub struct $name {
cmac: crate::cmac::CMAC,
}

$(#[$attr])*
impl digest::MacMarker for $name {}

$(#[$attr])*
impl digest::OutputSizeUser for $name {
type OutputSize = U16;
}

$(#[$attr])*
impl digest::common::KeySizeUser for $name {
type KeySize = $key_size;
}

$(#[$attr])*
impl digest::KeyInit for $name {
fn new(key: &digest::Key<Self>) -> Self {
Self {
cmac: crate::cmac::CMAC::new(key.as_slice())
.expect("wolfCrypt CMAC init failed"),
}
}
}

$(#[$attr])*
impl digest::Update for $name {
fn update(&mut self, data: &[u8]) {
crate::cmac::CMAC::update(&mut self.cmac, data)
.expect("wolfCrypt CMAC update failed");
}
}

$(#[$attr])*
impl digest::FixedOutput for $name {
fn finalize_into(self, out: &mut digest::Output<Self>) {
self.cmac.finalize(out.as_mut_slice())
.expect("wolfCrypt CMAC finalize failed");
}
}
};
}

impl_cmac_mac! {
CmacAes128, key = U16
}

impl_cmac_mac! {
CmacAes192, key = U24
}

impl_cmac_mac! {
CmacAes256, key = U32
}
7 changes: 5 additions & 2 deletions wrapper/rust/wolfssl-wolfcrypt/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,9 @@ impl ECC {
/// }
/// ```
pub fn rs_hex_to_sig(r: &[u8], s: &[u8], dout: &mut [u8]) -> Result<usize, i32> {
if r.is_empty() || s.is_empty() || r[r.len() - 1] != 0 || s[s.len() - 1] != 0 {
return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
}
let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
let r_ptr = r.as_ptr() as *const core::ffi::c_char;
let s_ptr = s.as_ptr() as *const core::ffi::c_char;
Expand Down Expand Up @@ -1820,7 +1823,7 @@ impl ECC {
sys::wc_ecc_shared_secret(&mut self.wc_ecc_key,
&mut peer_key.wc_ecc_key, dout.as_mut_ptr(), &mut out_len)
};
if rc < 0 {
if rc != 0 {
return Err(rc);
}
Ok(out_len as usize)
Expand Down Expand Up @@ -1961,7 +1964,7 @@ impl ECC {
if rc != 0 {
return Err(rc);
}
Ok(res != 0)
Ok(res == 1)
}
}

Expand Down
Loading
Loading