-
Notifications
You must be signed in to change notification settings - Fork 27
Added macro calls for sign output size and export key buffer size #31
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
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
#![allow(deprecated)] | ||
#[cfg(feature = "with-mbed-crypto")] | ||
use crate::initialized; | ||
#[cfg(feature = "with-mbed-crypto")] | ||
use crate::types::algorithm::AsymmetricSignature; | ||
use crate::types::algorithm::{Algorithm, Cipher}; | ||
#[cfg(feature = "with-mbed-crypto")] | ||
use crate::types::status::Status; | ||
|
@@ -305,6 +307,50 @@ impl Attributes { | |
get_attributes_res?; | ||
Ok(attributes?) | ||
} | ||
|
||
/// Sufficient size for a buffer to export the key, if supported | ||
#[cfg(feature = "with-mbed-crypto")] | ||
pub fn export_key_output_size(self) -> Result<usize> { | ||
Attributes::export_key_output_size_base(self.key_type, self.bits) | ||
} | ||
|
||
/// Sufficient size for a buffer to export the public key, if supported | ||
#[cfg(feature = "with-mbed-crypto")] | ||
pub fn export_public_key_output_size(self) -> Result<usize> { | ||
match self.key_type { | ||
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. The specification says that this macro allows for
So I think you can add DH both for key pair and public key as a valid type as well. 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. Is DH the same as DSA? I've been looking at this to try and make sure its not possible to pass it something invalid. 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.
No, Diffie-Hellman is a type of key exchange, Digital Signature Algorithm is (as its name says) an asymmetric signature algorithm. For DH public keys, the spec dictates:
Which brings me to another realisation - that method there, 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. Ah, I see, you're using the spec-provided macros, I think that's alright (although, will those disappear in the future?). But we do have to make sure we document whether this method refers to the full key or just the public part |
||
Type::RsaKeyPair | ||
| Type::RsaPublicKey | ||
| Type::EccKeyPair { .. } | ||
| Type::EccPublicKey { .. } | ||
| Type::DhKeyPair { .. } | ||
| Type::DhPublicKey { .. } => { | ||
let pub_type = self.key_type.key_type_public_key_of_key_pair()?; | ||
Attributes::export_key_output_size_base(pub_type, self.bits) | ||
Comment on lines
+327
to
+328
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. That's clean! |
||
} | ||
_ => Err(Error::InvalidArgument), | ||
} | ||
} | ||
|
||
/// Sufficient size for a buffer to export the given key type, if supported | ||
#[cfg(feature = "with-mbed-crypto")] | ||
fn export_key_output_size_base(key_type: Type, bits: usize) -> Result<usize> { | ||
let size = | ||
unsafe { psa_crypto_sys::PSA_EXPORT_KEY_OUTPUT_SIZE(key_type.try_into()?, bits) }; | ||
if size > 0 { | ||
Ok(size) | ||
} else { | ||
Err(Error::NotSupported) | ||
} | ||
Comment on lines
+337
to
+343
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. Same as what Hugues said, this works fine, no need to change, just for reference: match unsafe {
psa_crypto_sys::PSA_EXPORT_KEY_OUTPUT_SIZE(key_type.try_into()?, bits)
} {
0 => Err(Error::NotSupported),
val => Ok(val),
} 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. Ah yes, I was sure there would be a more succinct way of writing that bit, didn't think of doing it like your example! |
||
} | ||
|
||
/// Sufficient buffer size for a signature using the given key, if the key is supported | ||
#[cfg(feature = "with-mbed-crypto")] | ||
pub fn sign_output_size(self, alg: AsymmetricSignature) -> Result<usize> { | ||
self.compatible_with_alg(Algorithm::AsymmetricSignature(alg))?; | ||
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. This is perfectly fine like this (no need to change), just showing for reference that you can also do: self.compatible_with_alg(alg.into())?; |
||
Ok(unsafe { | ||
psa_crypto_sys::PSA_SIGN_OUTPUT_SIZE(self.key_type.try_into()?, self.bits, alg.into()) | ||
}) | ||
} | ||
} | ||
|
||
/// The lifetime of a key indicates where it is stored and which application and system actions | ||
|
@@ -407,6 +453,27 @@ impl Type { | |
_ => false, | ||
} | ||
} | ||
|
||
/// If key is public or key pair, returns the corresponding public key type. | ||
#[cfg(feature = "with-mbed-crypto")] | ||
pub fn key_type_public_key_of_key_pair(self) -> Result<Type> { | ||
match self { | ||
Type::RsaKeyPair | ||
| Type::RsaPublicKey | ||
| Type::EccKeyPair { .. } | ||
| Type::EccPublicKey { .. } | ||
| Type::DhKeyPair { .. } | ||
| Type::DhPublicKey { .. } => { | ||
Ok( | ||
unsafe { | ||
psa_crypto_sys::PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(self.try_into()?) | ||
} | ||
.try_into()?, | ||
) | ||
} | ||
_ => Err(Error::InvalidArgument), | ||
} | ||
} | ||
} | ||
|
||
/// Enumeration of elliptic curve families supported. They are needed to create an ECC key. | ||
|
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 update of the example 💯