Skip to content

Commit

Permalink
Added macro calls for sign output size and export key buffer size
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Bailey <samuel.bailey@arm.com>
  • Loading branch information
sbailey-arm committed Jun 22, 2020
1 parent 0815866 commit ee7c5bb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions psa-crypto-sys/src/c/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,15 @@ shim_PSA_KEY_TYPE_DH_PUBLIC_KEY(psa_dh_group_t group)
{
return PSA_KEY_TYPE_DH_PUBLIC_KEY(group);
}

size_t
shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg)
{
return PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
}

size_t
shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits)
{
return PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
}
2 changes: 2 additions & 0 deletions psa-crypto-sys/src/c/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,5 @@ psa_key_type_t shim_PSA_KEY_TYPE_ECC_KEY_PAIR(psa_ecc_curve_t curve);
psa_key_type_t shim_PSA_KEY_TYPE_ECC_PUBLIC_KEY(psa_ecc_curve_t curve);
psa_key_type_t shim_PSA_KEY_TYPE_DH_KEY_PAIR(psa_dh_group_t group);
psa_key_type_t shim_PSA_KEY_TYPE_DH_PUBLIC_KEY(psa_dh_group_t group);
size_t shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
size_t shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits);
12 changes: 12 additions & 0 deletions psa-crypto-sys/src/shim_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,15 @@ pub fn PSA_KEY_TYPE_DH_KEY_PAIR(group: psa_dh_group_t) -> psa_key_type_t {
pub fn PSA_KEY_TYPE_DH_PUBLIC_KEY(group: psa_dh_group_t) -> psa_key_type_t {
unsafe { psa_crypto_binding::shim_PSA_KEY_TYPE_DH_PUBLIC_KEY(group) }
}

pub fn PSA_SIGN_OUTPUT_SIZE(
key_type: psa_key_type_t,
key_bits: usize,
alg: psa_algorithm_t,
) -> usize {
unsafe { psa_crypto_binding::shim_PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) }
}

pub fn PSA_EXPORT_KEY_OUTPUT_SIZE(key_type: psa_key_type_t, key_bits: usize) -> usize {
unsafe { psa_crypto_binding::shim_PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) }
}
31 changes: 31 additions & 0 deletions psa-crypto/src/types/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,37 @@ 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> {
match self.key_type {
Type::RsaKeyPair
| Type::RsaPublicKey
| Type::EccKeyPair { .. }
| Type::EccPublicKey { .. } => Ok(psa_crypto_sys::PSA_EXPORT_KEY_OUTPUT_SIZE(
self.key_type.try_into()?,
self.bits,
)),
_ => Err(Error::NotSupported),
}
}

/// 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) -> Result<usize> {
match self.key_type {
Type::RsaPublicKey
| Type::RsaKeyPair
| Type::EccPublicKey { .. }
| Type::EccKeyPair { .. } => Ok(psa_crypto_sys::PSA_SIGN_OUTPUT_SIZE(
self.key_type.try_into()?,
self.bits,
self.policy.permitted_algorithms.try_into()?,
)),
_ => Err(Error::NotSupported),
}
}
}

/// The lifetime of a key indicates where it is stored and which application and system actions
Expand Down

0 comments on commit ee7c5bb

Please sign in to comment.