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

Fix feature gate for AES #361

Merged
merged 1 commit into from
Jan 8, 2024
Merged
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
1 change: 1 addition & 0 deletions crates/scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

### Patch

- Fix board API feature gates for AES-128-CCM and AES-256-GCM
- Remove unreachable `multivalue` feature gates
- Correctly gate events of nested APIs
- Rename `debug` internal feature to `internal-debug`
Expand Down
24 changes: 12 additions & 12 deletions crates/scheduler/src/call/crypto/ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@
// limitations under the License.

use wasefire_applet_api::crypto::ccm::{self as api, Api};
#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
use wasefire_board_api::crypto::aead::{Api as _, Array};
use wasefire_board_api::Api as Board;
#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
use wasefire_board_api::{self as board, Support};

#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
use crate::applet::store::MemoryApi;
#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
use crate::Trap;
use crate::{DispatchSchedulerCall, SchedulerCall};

pub fn process<B: Board>(call: Api<DispatchSchedulerCall<B>>) {
match call {
Api::IsSupported(call) => is_supported(call),
Api::Encrypt(call) => or_trap!("board-api-aes128-ccm", encrypt(call)),
Api::Decrypt(call) => or_trap!("board-api-aes128-ccm", decrypt(call)),
Api::Encrypt(call) => or_trap!("board-api-crypto-aes128-ccm", encrypt(call)),
Api::Decrypt(call) => or_trap!("board-api-crypto-aes128-ccm", decrypt(call)),
}
}

fn is_supported<B: Board>(call: SchedulerCall<B, api::is_supported::Sig>) {
let api::is_supported::Params {} = call.read();
#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
let supported = bool::from(board::crypto::Aes128Ccm::<B>::SUPPORT) as u32;
#[cfg(not(feature = "board-api-aes128-ccm"))]
#[cfg(not(feature = "board-api-crypto-aes128-ccm"))]
let supported = 0;
call.reply(Ok(api::is_supported::Results { supported: supported.into() }))
}

#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
fn encrypt<B: Board>(mut call: SchedulerCall<B, api::encrypt::Sig>) {
let api::encrypt::Params { key, iv, len, clear, cipher } = call.read();
let scheduler = call.scheduler();
Expand All @@ -61,7 +61,7 @@ fn encrypt<B: Board>(mut call: SchedulerCall<B, api::encrypt::Sig>) {
call.reply(results);
}

#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
fn decrypt<B: Board>(mut call: SchedulerCall<B, api::decrypt::Sig>) {
let api::decrypt::Params { key, iv, len, cipher, clear } = call.read();
let scheduler = call.scheduler();
Expand All @@ -81,12 +81,12 @@ fn decrypt<B: Board>(mut call: SchedulerCall<B, api::decrypt::Sig>) {
call.reply(results);
}

#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
fn expand_iv(iv: &[u8]) -> Array<typenum::U13> {
core::array::from_fn(|i| i.checked_sub(5).map(|i| iv[i]).unwrap_or(0)).into()
}

#[cfg(feature = "board-api-aes128-ccm")]
#[cfg(feature = "board-api-crypto-aes128-ccm")]
fn ensure_support<B: Board>() -> Result<(), Trap> {
match bool::from(board::crypto::Aes128Ccm::<B>::SUPPORT) {
true => Ok(()),
Expand Down
28 changes: 14 additions & 14 deletions crates/scheduler/src/call/crypto/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,49 @@
// limitations under the License.

use wasefire_applet_api::crypto::gcm::{self as api, Api};
#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
use wasefire_board_api::crypto::aead::Api as _;
use wasefire_board_api::Api as Board;
#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
use wasefire_board_api::{self as board, Support as _};

#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
use crate::applet::store::MemoryApi;
#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
use crate::Trap;
use crate::{DispatchSchedulerCall, SchedulerCall};

pub fn process<B: Board>(call: Api<DispatchSchedulerCall<B>>) {
match call {
Api::Support(call) => support(call),
Api::TagLength(call) => or_trap!("board-api-aes256-gcm", tag_length(call)),
Api::Encrypt(call) => or_trap!("board-api-aes256-gcm", encrypt(call)),
Api::Decrypt(call) => or_trap!("board-api-aes256-gcm", decrypt(call)),
Api::TagLength(call) => or_trap!("board-api-crypto-aes256-gcm", tag_length(call)),
Api::Encrypt(call) => or_trap!("board-api-crypto-aes256-gcm", encrypt(call)),
Api::Decrypt(call) => or_trap!("board-api-crypto-aes256-gcm", decrypt(call)),
}
}

fn support<B: Board>(call: SchedulerCall<B, api::support::Sig>) {
let api::support::Params {} = call.read();
#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
let support = {
use wasefire_applet_api::crypto::gcm::Support;
let support = board::crypto::Aes256Gcm::<B>::SUPPORT;
(support.no_copy as u32) << Support::NoCopy as u32
| (support.in_place_no_copy as u32) << Support::InPlaceNoCopy as u32
};
#[cfg(not(feature = "board-api-aes256-gcm"))]
#[cfg(not(feature = "board-api-crypto-aes256-gcm"))]
let support = 0;
call.reply(Ok(api::support::Results { support: support.into() }))
}

#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
fn tag_length<B: Board>(call: SchedulerCall<B, api::tag_length::Sig>) {
let api::tag_length::Params {} = call.read();
let len = (tag_len::<B>() as u32).into();
call.reply(Ok(api::tag_length::Results { len }))
}

#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
fn encrypt<B: Board>(mut call: SchedulerCall<B, api::encrypt::Sig>) {
let api::encrypt::Params { key, iv, aad, aad_len, length, clear, cipher, tag } = call.read();
let scheduler = call.scheduler();
Expand All @@ -75,7 +75,7 @@ fn encrypt<B: Board>(mut call: SchedulerCall<B, api::encrypt::Sig>) {
call.reply(results);
}

#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
fn decrypt<B: Board>(mut call: SchedulerCall<B, api::decrypt::Sig>) {
let api::decrypt::Params { key, iv, aad, aad_len, tag, length, cipher, clear } = call.read();
let scheduler = call.scheduler();
Expand All @@ -95,13 +95,13 @@ fn decrypt<B: Board>(mut call: SchedulerCall<B, api::decrypt::Sig>) {
call.reply(results);
}

#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
const fn tag_len<B: Board>() -> usize {
use typenum::Unsigned;
<board::crypto::Aes256Gcm<B> as board::crypto::aead::Api<_, _>>::Tag::USIZE
}

#[cfg(feature = "board-api-aes256-gcm")]
#[cfg(feature = "board-api-crypto-aes256-gcm")]
fn ensure_support<B: Board>() -> Result<(), Trap> {
match bool::from(board::crypto::Aes256Gcm::<B>::SUPPORT) {
true => Ok(()),
Expand Down