-
Notifications
You must be signed in to change notification settings - Fork 28
Part of moving Parsec to use psa-crypto #28
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
Changes from all commits
19b70b0
3d1580e
d361726
6fd4469
fd73ed1
d539aed
6f34968
2d7b6ae
999187c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ pub fn sign_hash( | |
let mut signature_length = 0; | ||
let handle = key.handle()?; | ||
|
||
Status::from(unsafe { | ||
let sign_res = Status::from(unsafe { | ||
psa_crypto_sys::psa_sign_hash( | ||
handle, | ||
alg.into(), | ||
|
@@ -75,10 +75,9 @@ pub fn sign_hash( | |
&mut signature_length, | ||
) | ||
}) | ||
.to_result()?; | ||
|
||
.to_result(); | ||
key.close_handle(handle)?; | ||
|
||
sign_res?; | ||
Comment on lines
-79
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these cases where you need to do a cleanup operation that may fail (after some other core operation might've failed in the first place) we have to decide: if both fail, which error do we want to be returned? I'd say the error on the "core" operation should take precedence, in which case the two branches (as in, if the core operation failed or not) will be different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I wish we had a better approach to this kind of problem where you have some cleanup to do whenever you exit the function. In this case it's fine since there's not much branching, but in some cases it might end up being a pain having to call that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally had it returning the first operation error, then the close handle error but after a brief discussion with @hug-dev, it was changed to how it is now. I think the reason was the PSA API states keys aren't used, IDs are, so it should be getting removed at some point (soon? 😄) anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think that makes sense for this case, my comment was more general - we should have a statement somewhere as to how to prioritize these errors throughout the library. I'd even say that this applies to Parsec too. The last error to occur is not necessarily the one we should return. |
||
Ok(signature_length) | ||
} | ||
|
||
|
@@ -130,7 +129,7 @@ pub fn verify_hash(key: Id, alg: AsymmetricSignature, hash: &[u8], signature: &[ | |
|
||
let handle = key.handle()?; | ||
|
||
Status::from(unsafe { | ||
let verify_res = Status::from(unsafe { | ||
psa_crypto_sys::psa_verify_hash( | ||
handle, | ||
alg.into(), | ||
|
@@ -140,7 +139,7 @@ pub fn verify_hash(key: Id, alg: AsymmetricSignature, hash: &[u8], signature: &[ | |
signature.len(), | ||
) | ||
}) | ||
.to_result()?; | ||
|
||
key.close_handle(handle) | ||
.to_result(); | ||
key.close_handle(handle)?; | ||
verify_res | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,16 @@ | |
//! # PSA Key types | ||
|
||
#![allow(deprecated)] | ||
|
||
#[cfg(feature = "with-mbed-crypto")] | ||
use crate::initialized; | ||
use crate::types::algorithm::{Algorithm, Cipher}; | ||
#[cfg(feature = "with-mbed-crypto")] | ||
use crate::types::status::Status; | ||
use crate::types::status::{Error, Result}; | ||
#[cfg(feature = "with-mbed-crypto")] | ||
use core::convert::{TryFrom, TryInto}; | ||
use log::error; | ||
pub use psa_crypto_sys::{self, psa_key_id_t, PSA_KEY_ID_USER_MAX, PSA_KEY_ID_USER_MIN}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Native definition of the attributes needed to fully describe | ||
|
@@ -255,6 +257,54 @@ impl Attributes { | |
pub(crate) fn reset(attributes: &mut psa_crypto_sys::psa_key_attributes_t) { | ||
unsafe { psa_crypto_sys::psa_reset_key_attributes(attributes) }; | ||
} | ||
|
||
/// Gets the attributes for a given key ID | ||
/// | ||
/// The `Id` structure can be created with the `from_persistent_key_id` constructor on `Id`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # use psa_crypto::operations::key_management; | ||
/// # use psa_crypto::types::key::{Attributes, Type, Lifetime, Policy, UsageFlags}; | ||
/// # use psa_crypto::types::algorithm::{AsymmetricSignature, Hash}; | ||
/// # let mut attributes = Attributes { | ||
/// # key_type: Type::RsaKeyPair, | ||
/// # bits: 1024, | ||
/// # lifetime: Lifetime::Volatile, | ||
/// # policy: Policy { | ||
/// # usage_flags: UsageFlags { | ||
/// # sign_hash: true, | ||
/// # sign_message: true, | ||
/// # verify_hash: true, | ||
/// # verify_message: true, | ||
/// # ..Default::default() | ||
/// # }, | ||
/// # permitted_algorithms: AsymmetricSignature::RsaPkcs1v15Sign { | ||
/// # hash_alg: Hash::Sha256.into(), | ||
/// # }.into(), | ||
/// # }, | ||
/// # }; | ||
/// psa_crypto::init().unwrap(); | ||
/// let my_key_id = key_management::generate(attributes, None).unwrap(); | ||
/// //... | ||
/// let key_attributes = Attributes::from_key_id(my_key_id); | ||
/// ``` | ||
#[cfg(feature = "with-mbed-crypto")] | ||
pub fn from_key_id(key_id: Id) -> Result<Self> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth having a test for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is an example, let's say it is fine for now but think about adding one in the future 🤡 |
||
initialized()?; | ||
let mut key_attributes = unsafe { psa_crypto_sys::psa_key_attributes_init() }; | ||
let handle = key_id.handle()?; | ||
let get_attributes_res = Status::from(unsafe { | ||
psa_crypto_sys::psa_get_key_attributes(handle, &mut key_attributes) | ||
}) | ||
.to_result(); | ||
let attributes = Attributes::try_from(key_attributes); | ||
Attributes::reset(&mut key_attributes); | ||
key_id.close_handle(handle)?; | ||
get_attributes_res?; | ||
Ok(attributes?) | ||
} | ||
} | ||
|
||
/// The lifetime of a key indicates where it is stored and which application and system actions | ||
|
@@ -473,7 +523,7 @@ pub struct UsageFlags { | |
#[cfg(feature = "with-mbed-crypto")] | ||
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct Id { | ||
pub(crate) id: psa_crypto_sys::psa_key_id_t, | ||
pub(crate) id: psa_key_id_t, | ||
pub(crate) handle: Option<psa_crypto_sys::psa_key_handle_t>, | ||
} | ||
|
||
|
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.
That is actually quite clever 😄 Never thought of doing it like that before.