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

Add documentation, examples and tests #16

Merged
merged 1 commit into from
May 27, 2020

Conversation

hug-dev
Copy link
Member

@hug-dev hug-dev commented May 26, 2020

Signed-off-by: Hugues de Valon hugues.devalon@arm.com

@hug-dev hug-dev added documentation Improvements or additions to documentation enhancement New feature or request labels May 26, 2020
@hug-dev hug-dev requested review from ionut-arm and egrimley-arm May 26, 2020 16:22
@hug-dev hug-dev self-assigned this May 26, 2020
Comment on lines +33 to +37
/// # verify_message: true,
/// # ..Default::default()
/// # },
/// # permitted_algorithms: AsymmetricSignature::RsaPkcs1v15Sign {
/// # hash_alg: Hash::Sha256.into(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do this line and the following line contain #?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is made to hide portions of the example so it only shows relevent parts. See here for details.

/// # Example
///
/// ```
/// # use psa_crypto::operations::key_management::generate;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@@ -35,6 +35,39 @@ impl Attributes {
}

/// Check export in a faillible way
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of this patch, but while you're there: faillible -> fallible

Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to have documentation and tests!! thanks! 💯 Left a buncha comments below

@@ -8,8 +8,44 @@ use crate::types::key::{Attributes, Id};
use crate::types::status::{Result, Status};
use core::convert::TryFrom;

/// Generate a key
pub fn generate_key(attributes: Attributes, id: Option<u32>) -> Result<Id> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you dropped the _key part of the name, because it's in key_management? Then you probably need to modify the examples to call it as key_management::generate(...), because it's confusing as to what generate is and where it is coming from.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you dropped the _key part of the name, because it's in key_management?

Yes, that's the reason!

Then you probably need to modify the examples to call it as key_management::generate(...), because it's confusing as to what generate is and where it is coming from.

Sure, will do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've only modified the examples in this file, the other still import the function directly 🤡 but nevermind, we can discuss under #17

Comment on lines 51 to 52
/// mac_alg: FullLengthMac::Hmac { hash_alg: Hash::Sha256 },
/// mac_length: 34,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming the mac_length is in bytes, not bits, in which case this is invalid because Sha256 has only 32 bytes (not that I'm saying we shouldn't allow that construction, just saying that maybe something below 32 would be better for an example)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will modify!

})
.try_into()
.unwrap()
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some checks that fail (i.e. with unwrap_err())?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will add!

Comment on lines 472 to 487
impl Default for UsageFlags {
fn default() -> Self {
UsageFlags {
export: false,
copy: false,
cache: false,
encrypt: false,
decrypt: false,
sign_message: false,
verify_message: false,
sign_hash: false,
verify_hash: false,
derive: false,
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not what deriving Default would obtain?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, you are right, I forgot that you can derive Default

Comment on lines +1119 to +1158
fn convert() {
let mut attrs = unsafe { psa_crypto_sys::psa_key_attributes_init() };
unsafe {
psa_crypto_sys::psa_set_key_lifetime(
&mut attrs,
psa_crypto_sys::PSA_KEY_LIFETIME_VOLATILE,
)
};
unsafe {
psa_crypto_sys::psa_set_key_usage_flags(
&mut attrs,
psa_crypto_sys::PSA_KEY_USAGE_SIGN | psa_crypto_sys::PSA_KEY_USAGE_VERIFY,
)
};
unsafe {
psa_crypto_sys::psa_set_key_algorithm(
&mut attrs,
psa_crypto_sys::PSA_ALG_ECDSA(psa_crypto_sys::PSA_ALG_SHA_256),
)
};
unsafe {
psa_crypto_sys::psa_set_key_type(
&mut attrs,
psa_crypto_sys::PSA_KEY_TYPE_ECC_KEY_PAIR(psa_crypto_sys::PSA_ECC_CURVE_SECP_K1),
)
};
unsafe { psa_crypto_sys::psa_set_key_bits(&mut attrs, 2048) };

assert_eq!(
Attributes {
key_type: Type::EccKeyPair {
curve_family: EccFamily::SecpK1,
},
bits: 2048,
lifetime: Lifetime::Volatile,
policy: Policy {
usage_flags: UsageFlags {
export: false,
copy: false,
cache: false,
encrypt: false,
decrypt: false,
sign_message: true,
verify_message: true,
sign_hash: true,
verify_hash: true,
derive: false,
},
permitted_algorithms: AsymmetricSignature::Ecdsa {
hash_alg: Hash::Sha256.into(),
}
.into(),
},
},
attrs.try_into().unwrap()
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 nice

Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
@@ -8,8 +8,44 @@ use crate::types::key::{Attributes, Id};
use crate::types::status::{Result, Status};
use core::convert::TryFrom;

/// Generate a key
pub fn generate_key(attributes: Attributes, id: Option<u32>) -> Result<Id> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've only modified the examples in this file, the other still import the function directly 🤡 but nevermind, we can discuss under #17

@hug-dev hug-dev merged commit c4113c9 into parallaxsecond:master May 27, 2020
@hug-dev hug-dev deleted the tests branch May 27, 2020 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants