Skip to content

Commit

Permalink
Introduce Failure type for scheduler replies (#465)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Cretin <cretin@google.com>
  • Loading branch information
lukeyeh and ia0 committed May 24, 2024
1 parent 05a4475 commit 7389b22
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 161 deletions.
1 change: 1 addition & 0 deletions crates/scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

### Patch

- Add `Failure` type to simplify try-blocks for `Scheduler::reply()` arguments
- Update `store` errors mapping
- Use explicit conversion from `Error` to `Trap`
- Simplify `#[cfg(all)]` attributes between board and applet features
Expand Down
8 changes: 5 additions & 3 deletions crates/scheduler/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro_rules! or_trap {
#[cfg(feature = $feature)]
$name($call);
#[cfg(not(feature = $feature))]
$call.reply_(Err(crate::Trap));
$call.reply_(Err(crate::Trap.into()));
}};
}

Expand All @@ -33,7 +33,9 @@ macro_rules! or_fail {
#[cfg(feature = $feature)]
$name($call);
#[cfg(not(feature = $feature))]
$call.reply_(Ok(Err(wasefire_error::Error::world(wasefire_error::Code::NotImplemented))));
$call.reply_(Err(crate::Failure::Error(wasefire_error::Error::world(
wasefire_error::Code::NotImplemented,
))));
}};
}

Expand Down Expand Up @@ -113,5 +115,5 @@ pub fn process<B: Board>(call: Api<DispatchSchedulerCall<B>>) {

fn syscall<B: Board>(call: SchedulerCall<B, api::syscall::Sig>) {
let api::syscall::Params { x1, x2, x3, x4 } = call.read();
call.reply(B::syscall(*x1, *x2, *x3, *x4).ok_or(Trap));
call.reply(try { B::syscall(*x1, *x2, *x3, *x4).ok_or(Trap)?? });
}
6 changes: 3 additions & 3 deletions crates/scheduler/src/call/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn count<B: Board>(call: SchedulerCall<B, api::count::Sig>) {
let count = board::Button::<B>::SUPPORT as u32;
#[cfg(not(feature = "board-api-button"))]
let count = 0;
call.reply(Ok(Ok(count)));
call.reply(Ok(count));
}

#[cfg(feature = "board-api-button")]
Expand All @@ -57,7 +57,7 @@ fn register<B: Board>(mut call: SchedulerCall<B, api::register::Sig>) {
.map_err(|_| Error::user(Code::InvalidState))?;
board::Button::<B>::enable(button)?;
};
call.reply(Ok(result));
call.reply(result);
}

#[cfg(feature = "board-api-button")]
Expand All @@ -70,5 +70,5 @@ fn unregister<B: Board>(mut call: SchedulerCall<B, api::unregister::Sig>) {
.disable_event(Key { button }.into())
.map_err(|_| Error::user(Code::InvalidState))?;
};
call.reply(Ok(result));
call.reply(result);
}
6 changes: 3 additions & 3 deletions crates/scheduler/src/call/crypto/ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn is_supported<B: Board>(call: SchedulerCall<B, api::is_supported::Sig>) {
let supported = bool::from(board::crypto::Aes128Ccm::<B>::SUPPORT) as u32;
#[cfg(not(feature = "board-api-crypto-aes128-ccm"))]
let supported = 0;
call.reply(Ok(Ok(supported)))
call.reply(Ok(supported))
}

#[cfg(feature = "board-api-crypto-aes128-ccm")]
Expand All @@ -55,7 +55,7 @@ fn encrypt<B: Board>(mut call: SchedulerCall<B, api::encrypt::Sig>) {
let clear = Some(memory.get(*clear, *len)?);
let (cipher, tag) = memory.get_mut(*cipher, *len + 4)?.split_at_mut(*len as usize);
let tag = tag.into();
board::crypto::Aes128Ccm::<B>::encrypt(key, &iv, aad, clear, cipher, tag)
board::crypto::Aes128Ccm::<B>::encrypt(key, &iv, aad, clear, cipher, tag)?
};
call.reply(result);
}
Expand All @@ -74,7 +74,7 @@ fn decrypt<B: Board>(mut call: SchedulerCall<B, api::decrypt::Sig>) {
let cipher = Some(cipher);
let tag = tag.into();
let clear = memory.get_mut(*clear, *len)?;
board::crypto::Aes128Ccm::<B>::decrypt(key, &iv, aad, cipher, tag, clear)
board::crypto::Aes128Ccm::<B>::decrypt(key, &iv, aad, cipher, tag, clear)?
};
call.reply(result);
}
Expand Down
26 changes: 13 additions & 13 deletions crates/scheduler/src/call/crypto/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn process<B: Board>(call: Api<DispatchSchedulerCall<B>>) {

fn is_supported<B: Board>(call: SchedulerCall<B, api::is_supported::Sig>) {
let api::is_supported::Params { curve } = call.read();
call.reply(convert_curve::<B>(*curve).map(|x| Ok(x.is_ok())))
call.reply(try { convert_curve::<B>(*curve)?.is_ok() })
}

#[cfg(feature = "internal-board-api-crypto-ecc")]
Expand All @@ -48,7 +48,7 @@ fn is_valid_scalar<B: Board>(mut call: SchedulerCall<B, api::is_valid_scalar::Si
let scheduler = call.scheduler();
let memory = scheduler.applet.memory();
let result = try {
Ok(match convert_curve::<B>(*curve)?? {
match convert_curve::<B>(*curve)?? {
#[cfg(feature = "board-api-crypto-p256")]
Curve::P256 => {
let n = memory.get_array::<32>(*n)?.into();
Expand All @@ -61,7 +61,7 @@ fn is_valid_scalar<B: Board>(mut call: SchedulerCall<B, api::is_valid_scalar::Si
}
#[allow(unreachable_patterns)]
_ => trap_use!(),
})
}
};
call.reply(result)
}
Expand All @@ -72,7 +72,7 @@ fn is_valid_point<B: Board>(mut call: SchedulerCall<B, api::is_valid_point::Sig>
let scheduler = call.scheduler();
let memory = scheduler.applet.memory();
let result = try {
Ok(match convert_curve::<B>(*curve)?? {
match convert_curve::<B>(*curve)?? {
#[cfg(feature = "board-api-crypto-p256")]
Curve::P256 => {
let x = memory.get_array::<32>(*x)?.into();
Expand All @@ -87,7 +87,7 @@ fn is_valid_point<B: Board>(mut call: SchedulerCall<B, api::is_valid_point::Sig>
}
#[allow(unreachable_patterns)]
_ => trap_use!(),
})
}
};
call.reply(result)
}
Expand All @@ -104,14 +104,14 @@ fn base_point_mul<B: Board>(mut call: SchedulerCall<B, api::base_point_mul::Sig>
let n = memory.get_array::<32>(*n)?.into();
let x = memory.get_array_mut::<32>(*x)?.into();
let y = memory.get_array_mut::<32>(*y)?.into();
board::crypto::P256::<B>::base_point_mul(n, x, y)
board::crypto::P256::<B>::base_point_mul(n, x, y)?
}
#[cfg(feature = "board-api-crypto-p384")]
Curve::P384 => {
let n = memory.get_array::<48>(*n)?.into();
let x = memory.get_array_mut::<48>(*x)?.into();
let y = memory.get_array_mut::<48>(*y)?.into();
board::crypto::P384::<B>::base_point_mul(n, x, y)
board::crypto::P384::<B>::base_point_mul(n, x, y)?
}
#[allow(unreachable_patterns)]
_ => trap_use!(),
Expand All @@ -134,7 +134,7 @@ fn point_mul<B: Board>(mut call: SchedulerCall<B, api::point_mul::Sig>) {
let in_y = memory.get_array::<32>(*in_y)?.into();
let out_x = memory.get_array_mut::<32>(*out_x)?.into();
let out_y = memory.get_array_mut::<32>(*out_y)?.into();
board::crypto::P256::<B>::point_mul(n, in_x, in_y, out_x, out_y)
board::crypto::P256::<B>::point_mul(n, in_x, in_y, out_x, out_y)?
}
#[cfg(feature = "board-api-crypto-p384")]
Curve::P384 => {
Expand All @@ -143,7 +143,7 @@ fn point_mul<B: Board>(mut call: SchedulerCall<B, api::point_mul::Sig>) {
let in_y = memory.get_array::<48>(*in_y)?.into();
let out_x = memory.get_array_mut::<48>(*out_x)?.into();
let out_y = memory.get_array_mut::<48>(*out_y)?.into();
board::crypto::P384::<B>::point_mul(n, in_x, in_y, out_x, out_y)
board::crypto::P384::<B>::point_mul(n, in_x, in_y, out_x, out_y)?
}
#[allow(unreachable_patterns)]
_ => trap_use!(),
Expand All @@ -165,15 +165,15 @@ fn ecdsa_sign<B: Board>(mut call: SchedulerCall<B, api::ecdsa_sign::Sig>) {
let message = memory.get_array::<32>(*message)?.into();
let r = memory.get_array_mut::<32>(*r)?.into();
let s = memory.get_array_mut::<32>(*s)?.into();
board::crypto::P256::<B>::ecdsa_sign(key, message, r, s)
board::crypto::P256::<B>::ecdsa_sign(key, message, r, s)?
}
#[cfg(feature = "board-api-crypto-p384")]
Curve::P384 => {
let key = memory.get_array::<48>(*key)?.into();
let message = memory.get_array::<48>(*message)?.into();
let r = memory.get_array_mut::<48>(*r)?.into();
let s = memory.get_array_mut::<48>(*s)?.into();
board::crypto::P384::<B>::ecdsa_sign(key, message, r, s)
board::crypto::P384::<B>::ecdsa_sign(key, message, r, s)?
}
#[allow(unreachable_patterns)]
_ => trap_use!(),
Expand All @@ -196,7 +196,7 @@ fn ecdsa_verify<B: Board>(mut call: SchedulerCall<B, api::ecdsa_verify::Sig>) {
let y = memory.get_array::<32>(*y)?.into();
let r = memory.get_array::<32>(*r)?.into();
let s = memory.get_array::<32>(*s)?.into();
board::crypto::P256::<B>::ecdsa_verify(message, x, y, r, s)
board::crypto::P256::<B>::ecdsa_verify(message, x, y, r, s)?
}
#[cfg(feature = "board-api-crypto-p384")]
Curve::P384 => {
Expand All @@ -205,7 +205,7 @@ fn ecdsa_verify<B: Board>(mut call: SchedulerCall<B, api::ecdsa_verify::Sig>) {
let y = memory.get_array::<48>(*y)?.into();
let r = memory.get_array::<48>(*r)?.into();
let s = memory.get_array::<48>(*s)?.into();
board::crypto::P384::<B>::ecdsa_verify(message, x, y, r, s)
board::crypto::P384::<B>::ecdsa_verify(message, x, y, r, s)?
}
#[allow(unreachable_patterns)]
_ => trap_use!(),
Expand Down
8 changes: 4 additions & 4 deletions crates/scheduler/src/call/crypto/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ fn support<B: Board>(call: SchedulerCall<B, api::support::Sig>) {
};
#[cfg(not(feature = "board-api-crypto-aes256-gcm"))]
let support = 0;
call.reply(Ok(Ok(support)))
call.reply(Ok(support))
}

#[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();
call.reply(Ok(Ok(tag_len::<B>() as u32)))
call.reply(Ok(tag_len::<B>() as u32))
}

#[cfg(feature = "board-api-crypto-aes256-gcm")]
Expand All @@ -68,7 +68,7 @@ fn encrypt<B: Board>(mut call: SchedulerCall<B, api::encrypt::Sig>) {
let cipher = memory.get_mut(*cipher, *length)?;
let tag_len = tag_len::<B>() as u32;
let tag = memory.get_mut(*tag, tag_len)?.into();
board::crypto::Aes256Gcm::<B>::encrypt(key, iv, aad, clear, cipher, tag)
board::crypto::Aes256Gcm::<B>::encrypt(key, iv, aad, clear, cipher, tag)?
};
call.reply(result);
}
Expand All @@ -87,7 +87,7 @@ fn decrypt<B: Board>(mut call: SchedulerCall<B, api::decrypt::Sig>) {
let tag = memory.get(*tag, tag_len)?.into();
let cipher = memory.get_opt(*cipher, *length)?;
let clear = memory.get_mut(*clear, *length)?;
board::crypto::Aes256Gcm::<B>::decrypt(key, iv, aad, cipher, tag, clear)
board::crypto::Aes256Gcm::<B>::decrypt(key, iv, aad, cipher, tag, clear)?
};
call.reply(result);
}
Expand Down
Loading

0 comments on commit 7389b22

Please sign in to comment.