-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add WithError trait to check for error after using RustCrypto API #429
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
- You can just run
./scripts/ci.sh
to run all CI locally. However, if you don't want to modify the changelogs just yet, you can run./scripts/ci-tests.sh
which runs the tests of all crates, and./scripts/hwci.sh host --no-default-features
which runs all applet tests. This last one will exerce those APIs (in particular thehash_test
andec_test
for example).
Actually, thinking about it, I think a better approach that would statically avoid mistakes, is to define a wrapper API on top of board::crypto::Api that actually return errors such that last_error is handled internally. It would look like this:
pub struct HashApi<T: Hash>(T);
pub struct HmacApi<T: Hmac>(T);
impl<T: Hash> HashApi<T> {
pub fn new() -> Result<Self, Error> { ... last_error ... }
pub fn update(&mut self, data: &[u8]) -> Result<(), Error> { ... last_error ... }
...
}
// same for Hmac
- I use rustfmt (through rust-analyzer, but this shouldn't change anything) which should use the top-level rustfmt.toml configuration file.
cargo fmt
should also work the same.
By the way, I've merged #431 to ensure the board API is documented. You'll have to rebase (the conflicts should be trivial, that PR is just adding documentation). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the work! This is going in the right direction from what I can see. It seems you still need to do the same as new
but for update
and finalize
.
Good idea! (I also had a similar feeling -- need something similar to RAII in C++.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good, it's getting closer. I suspect there might be some difficulties with imports eventually, but we'll deal with them once the rest is fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I suspect after this iteration, it should be pretty close to code-complete. Then it will be about following the CI errors and fixing them.
crates/board/src/crypto.rs
Outdated
#[cfg(feature = "software-crypto-sha256")] | ||
impl LastError for sha2::Sha256 { | ||
fn last_error(&self) -> Result<(), Error> { | ||
// TODO: Implement the error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct. The RustCrypto implementation does not fail. I'll remove the comments in my review commit. Actually I introduced a NoError
helper trait.
crates/board/src/crypto.rs
Outdated
where D: Support<bool> + Default + BlockSizeUser + Update + FixedOutputReset + HashMarker + LastError | ||
{ | ||
fn last_error(&self) -> Result<(), Error> { | ||
// TODO: Implement the error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this one needs to propagate the hash last error. I'll update it in my commit because I actually realized the LastError
signature is too strong. We don't always have access to the object. So I'm changing it to WithError
which makes it harder to implement, but it works in our internal use-case.
Fixes google#176 Co-authored-by: Zhou Fang <[email protected]> Co-authored-by: Julien Cretin <[email protected]>
To resolve #176.
Please let me know any issue or improvement. Thanks!
I was wondering if there is any existing test in
wasefire/examples
that could be used to check if these changes break anything, especially usingFixedOutputReset
for theHmac
trait.What Rust format tool do you use? As you can see, I use a different format tool and I'm happy to change to yours.