Skip to content

Commit b9b9881

Browse files
authored
Merge pull request #31 from sbailey-arm/move-output-size-macros
Added macro calls for sign output size and export key buffer size
2 parents 0815866 + 1e39ab4 commit b9b9881

File tree

7 files changed

+118
-7
lines changed

7 files changed

+118
-7
lines changed

ci.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ fi
3131
#############
3232
# Run tests #
3333
#############
34-
RUST_BACKTRACE=1 cargo test
34+
RUST_BACKTRACE=1 cargo test -- --test-threads=1
3535

36+
# Remove mbedtls directory if it exists
37+
rm -rf psa-crypto/mbedtls
3638
################################
3739
# Check feature configurations #
3840
################################

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,21 @@ 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+
psa_key_type_t
241+
shim_PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_key_type_t key_type)
242+
{
243+
return PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
244+
}
245+
246+
size_t
247+
shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg)
248+
{
249+
return PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
250+
}
251+
252+
size_t
253+
shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits)
254+
{
255+
return PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
256+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,6 @@ 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+
psa_key_type_t shim_PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_key_type_t key_type);
119+
size_t shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
120+
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,19 @@ 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 unsafe fn PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type: psa_key_type_t) -> psa_key_type_t {
176+
psa_crypto_binding::shim_PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type)
177+
}
178+
179+
pub unsafe fn PSA_SIGN_OUTPUT_SIZE(
180+
key_type: psa_key_type_t,
181+
key_bits: usize,
182+
alg: psa_algorithm_t,
183+
) -> usize {
184+
psa_crypto_binding::shim_PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
185+
}
186+
187+
pub unsafe fn PSA_EXPORT_KEY_OUTPUT_SIZE(key_type: psa_key_type_t, key_bits: usize) -> usize {
188+
psa_crypto_binding::shim_PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits)
189+
}

psa-crypto/src/operations/asym_signature.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ use crate::types::status::{Result, Status};
4343
/// # 0x94, 0x8E, 0x92, 0x50, 0x35, 0xC2, 0x8C, 0x5C, 0x3C, 0xCA, 0xFE, 0x18, 0xE8, 0x81, 0x37, 0x78,
4444
/// # ];
4545
/// psa_crypto::init().unwrap();
46-
/// let mut signature = vec![0; 256];
4746
/// let my_key = generate(attributes, None).unwrap();
48-
/// let size = sign_hash(my_key,
49-
/// AsymmetricSignature::RsaPkcs1v15Sign {
47+
/// let alg = AsymmetricSignature::RsaPkcs1v15Sign {
5048
/// hash_alg: Hash::Sha256.into(),
51-
/// },
49+
/// };
50+
/// let buffer_size = attributes.sign_output_size(alg).unwrap();
51+
/// let mut signature = vec![0; buffer_size];
52+
///
53+
/// let size = sign_hash(my_key,
54+
/// alg,
5255
/// &HASH,
5356
/// &mut signature).unwrap();
5457
/// signature.resize(size, 0);
@@ -112,10 +115,11 @@ pub fn sign_hash(
112115
/// # 0x94, 0x8E, 0x92, 0x50, 0x35, 0xC2, 0x8C, 0x5C, 0x3C, 0xCA, 0xFE, 0x18, 0xE8, 0x81, 0x37, 0x78,
113116
/// # ];
114117
/// psa_crypto::init().unwrap();
115-
/// let mut signature = vec![0; 256];
116118
/// let alg = AsymmetricSignature::RsaPkcs1v15Sign {
117119
/// hash_alg: Hash::Sha256.into(),
118120
/// };
121+
/// let buffer_size = attributes.sign_output_size(alg).unwrap();
122+
/// let mut signature = vec![0; buffer_size];
119123
/// let my_key = generate(attributes, None).unwrap();
120124
/// let size = sign_hash(my_key,
121125
/// alg,

psa-crypto/src/operations/key_management.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ pub fn import(attributes: Attributes, id: Option<u32>, data: &[u8]) -> Result<Id
208208
/// # },
209209
/// # };
210210
/// psa_crypto::init().unwrap();
211-
/// let mut data = vec![0; 256];
211+
/// let buffer_size = attributes.export_public_key_output_size().unwrap();
212+
/// let mut data = vec![0; buffer_size];
212213
/// let my_key = key_management::generate(attributes, None).unwrap();
213214
/// let size = key_management::export_public(my_key, &mut data).unwrap();
214215
/// data.resize(size, 0);

psa-crypto/src/types/key.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#![allow(deprecated)]
77
#[cfg(feature = "with-mbed-crypto")]
88
use crate::initialized;
9+
#[cfg(feature = "with-mbed-crypto")]
10+
use crate::types::algorithm::AsymmetricSignature;
911
use crate::types::algorithm::{Algorithm, Cipher};
1012
#[cfg(feature = "with-mbed-crypto")]
1113
use crate::types::status::Status;
@@ -305,6 +307,50 @@ impl Attributes {
305307
get_attributes_res?;
306308
Ok(attributes?)
307309
}
310+
311+
/// Sufficient size for a buffer to export the key, if supported
312+
#[cfg(feature = "with-mbed-crypto")]
313+
pub fn export_key_output_size(self) -> Result<usize> {
314+
Attributes::export_key_output_size_base(self.key_type, self.bits)
315+
}
316+
317+
/// Sufficient size for a buffer to export the public key, if supported
318+
#[cfg(feature = "with-mbed-crypto")]
319+
pub fn export_public_key_output_size(self) -> Result<usize> {
320+
match self.key_type {
321+
Type::RsaKeyPair
322+
| Type::RsaPublicKey
323+
| Type::EccKeyPair { .. }
324+
| Type::EccPublicKey { .. }
325+
| Type::DhKeyPair { .. }
326+
| Type::DhPublicKey { .. } => {
327+
let pub_type = self.key_type.key_type_public_key_of_key_pair()?;
328+
Attributes::export_key_output_size_base(pub_type, self.bits)
329+
}
330+
_ => Err(Error::InvalidArgument),
331+
}
332+
}
333+
334+
/// Sufficient size for a buffer to export the given key type, if supported
335+
#[cfg(feature = "with-mbed-crypto")]
336+
fn export_key_output_size_base(key_type: Type, bits: usize) -> Result<usize> {
337+
let size =
338+
unsafe { psa_crypto_sys::PSA_EXPORT_KEY_OUTPUT_SIZE(key_type.try_into()?, bits) };
339+
if size > 0 {
340+
Ok(size)
341+
} else {
342+
Err(Error::NotSupported)
343+
}
344+
}
345+
346+
/// Sufficient buffer size for a signature using the given key, if the key is supported
347+
#[cfg(feature = "with-mbed-crypto")]
348+
pub fn sign_output_size(self, alg: AsymmetricSignature) -> Result<usize> {
349+
self.compatible_with_alg(Algorithm::AsymmetricSignature(alg))?;
350+
Ok(unsafe {
351+
psa_crypto_sys::PSA_SIGN_OUTPUT_SIZE(self.key_type.try_into()?, self.bits, alg.into())
352+
})
353+
}
308354
}
309355

310356
/// The lifetime of a key indicates where it is stored and which application and system actions
@@ -407,6 +453,27 @@ impl Type {
407453
_ => false,
408454
}
409455
}
456+
457+
/// If key is public or key pair, returns the corresponding public key type.
458+
#[cfg(feature = "with-mbed-crypto")]
459+
pub fn key_type_public_key_of_key_pair(self) -> Result<Type> {
460+
match self {
461+
Type::RsaKeyPair
462+
| Type::RsaPublicKey
463+
| Type::EccKeyPair { .. }
464+
| Type::EccPublicKey { .. }
465+
| Type::DhKeyPair { .. }
466+
| Type::DhPublicKey { .. } => {
467+
Ok(
468+
unsafe {
469+
psa_crypto_sys::PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(self.try_into()?)
470+
}
471+
.try_into()?,
472+
)
473+
}
474+
_ => Err(Error::InvalidArgument),
475+
}
476+
}
410477
}
411478

412479
/// Enumeration of elliptic curve families supported. They are needed to create an ECC key.

0 commit comments

Comments
 (0)