Skip to content

Commit ee7c5bb

Browse files
committed
Added macro calls for sign output size and export key buffer size
Signed-off-by: Samuel Bailey <[email protected]>
1 parent 0815866 commit ee7c5bb

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

psa-crypto-sys/src/c/shim.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,15 @@ shim_PSA_KEY_TYPE_DH_PUBLIC_KEY(psa_dh_group_t group)
236236
{
237237
return PSA_KEY_TYPE_DH_PUBLIC_KEY(group);
238238
}
239+
240+
size_t
241+
shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg)
242+
{
243+
return PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
244+
}
245+
246+
size_t
247+
shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits)
248+
{
249+
return PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
250+
}

psa-crypto-sys/src/c/shim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,5 @@ psa_key_type_t shim_PSA_KEY_TYPE_ECC_KEY_PAIR(psa_ecc_curve_t curve);
115115
psa_key_type_t shim_PSA_KEY_TYPE_ECC_PUBLIC_KEY(psa_ecc_curve_t curve);
116116
psa_key_type_t shim_PSA_KEY_TYPE_DH_KEY_PAIR(psa_dh_group_t group);
117117
psa_key_type_t shim_PSA_KEY_TYPE_DH_PUBLIC_KEY(psa_dh_group_t group);
118+
size_t shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
119+
size_t shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits);

psa-crypto-sys/src/shim_methods.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,15 @@ pub fn PSA_KEY_TYPE_DH_KEY_PAIR(group: psa_dh_group_t) -> psa_key_type_t {
171171
pub fn PSA_KEY_TYPE_DH_PUBLIC_KEY(group: psa_dh_group_t) -> psa_key_type_t {
172172
unsafe { psa_crypto_binding::shim_PSA_KEY_TYPE_DH_PUBLIC_KEY(group) }
173173
}
174+
175+
pub fn PSA_SIGN_OUTPUT_SIZE(
176+
key_type: psa_key_type_t,
177+
key_bits: usize,
178+
alg: psa_algorithm_t,
179+
) -> usize {
180+
unsafe { psa_crypto_binding::shim_PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) }
181+
}
182+
183+
pub fn PSA_EXPORT_KEY_OUTPUT_SIZE(key_type: psa_key_type_t, key_bits: usize) -> usize {
184+
unsafe { psa_crypto_binding::shim_PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) }
185+
}

psa-crypto/src/types/key.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,37 @@ impl Attributes {
305305
get_attributes_res?;
306306
Ok(attributes?)
307307
}
308+
309+
/// Sufficient size for a buffer to export the key, if supported
310+
#[cfg(feature = "with-mbed-crypto")]
311+
pub fn export_key_output_size(self) -> Result<usize> {
312+
match self.key_type {
313+
Type::RsaKeyPair
314+
| Type::RsaPublicKey
315+
| Type::EccKeyPair { .. }
316+
| Type::EccPublicKey { .. } => Ok(psa_crypto_sys::PSA_EXPORT_KEY_OUTPUT_SIZE(
317+
self.key_type.try_into()?,
318+
self.bits,
319+
)),
320+
_ => Err(Error::NotSupported),
321+
}
322+
}
323+
324+
/// Sufficient buffer size for a signature using the given key, if the key is supported
325+
#[cfg(feature = "with-mbed-crypto")]
326+
pub fn sign_output_size(self) -> Result<usize> {
327+
match self.key_type {
328+
Type::RsaPublicKey
329+
| Type::RsaKeyPair
330+
| Type::EccPublicKey { .. }
331+
| Type::EccKeyPair { .. } => Ok(psa_crypto_sys::PSA_SIGN_OUTPUT_SIZE(
332+
self.key_type.try_into()?,
333+
self.bits,
334+
self.policy.permitted_algorithms.try_into()?,
335+
)),
336+
_ => Err(Error::NotSupported),
337+
}
338+
}
308339
}
309340

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

0 commit comments

Comments
 (0)