Skip to content

Commit 7b2ca72

Browse files
committed
Move calls from C library to psa-crypto
Parsec no longer calls mbedtls directly, it goes through the psa-crypto Rust interface to the library Signed-off-by: Samuel Bailey <samuel.bailey@arm.com>
1 parent d426bd7 commit 7b2ca72

File tree

9 files changed

+108
-497
lines changed

9 files changed

+108
-497
lines changed

Cargo.lock

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name = "parsec"
1818
path = "src/bin/main.rs"
1919

2020
[dependencies]
21-
parsec-interface = "0.15.0"
21+
parsec-interface = { path = "../parsec-interface-rs" }
2222
rand = "0.7.2"
2323
base64 = "0.10.1"
2424
uuid = "0.7.4"
@@ -40,6 +40,7 @@ derivative = "2.1.1"
4040
version = "3.0.0"
4141
hex = "0.4.2"
4242
picky = "5.0.0"
43+
psa-crypto = { path = "../rust-psa-crypto/psa-crypto", default-features = false, features = ["with-mbed-crypto"] }
4344

4445
[dev-dependencies]
4546
ring = "0.16.12"

build.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,6 @@ fn main() -> Result<()> {
253253
setup_mbed_crypto(&mbed_config, &mbed_version)?;
254254
generate_mbed_bindings(&mbed_config, &mbed_version)?;
255255

256-
// Request rustc to link the Mbed Crypto static library
257-
println!(
258-
"cargo:rustc-link-search=native={}/mbed-crypto-{}/library/",
259-
mbed_config
260-
.mbed_path
261-
.unwrap_or_else(|| env::var("OUT_DIR").unwrap()),
262-
mbed_version,
263-
);
264-
println!("cargo:rustc-link-lib=static=mbedcrypto");
265256
}
266257

267258
Ok(())

e2e_tests/tests/per_provider/normal_tests/asym_sign_verify.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn asym_verify_fail() -> Result<()> {
5656
let status = client
5757
.verify_with_rsa_sha256(key_name, HASH.to_vec(), signature)
5858
.expect_err("Verification should fail.");
59+
dbg!("Status: {}", status);
5960
if !(status == ResponseStatus::PsaErrorInvalidSignature
6061
|| status == ResponseStatus::PsaErrorCorruptionDetected)
6162
{

e2e_tests/tests/per_provider/normal_tests/create_destroy_key.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
// Copyright 2019 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use e2e_tests::TestClient;
4-
use parsec_client::core::interface::operations::psa_algorithm::{
5-
Algorithm, AsymmetricSignature, Hash,
6-
};
7-
use parsec_client::core::interface::operations::psa_key_attributes::{
8-
Attributes, Lifetime, Policy, Type, UsageFlags,
9-
};
104
use parsec_client::core::interface::requests::ResponseStatus;
115
use parsec_client::core::interface::requests::Result;
126
use picky_asn1::wrapper::IntegerAsn1;
@@ -93,12 +87,14 @@ fn create_destroy_twice() -> Result<()> {
9387
fn generate_public_rsa_check_modulus() -> Result<()> {
9488
// As stated in the operation page, the public exponent of RSA key pair should be 65537
9589
// (0x010001).
90+
println!("test");
9691
let mut client = TestClient::new();
97-
let key_name = String::from("generate_public_rsa_check_modulus");
92+
let key_name = String::from("generate_public_rsa_check_modulus1");
9893
client.generate_rsa_sign_key(key_name.clone())?;
9994
let public_key = client.export_public_key(key_name)?;
10095

10196
let public_key: RsaPublicKey = picky_asn1_der::from_bytes(&public_key).unwrap();
97+
println!("{:?}", &public_key.public_exponent.as_unsigned_bytes_be()[..]);
10298
assert_eq!(
10399
public_key.public_exponent.as_unsigned_bytes_be(),
104100
[0x01, 0x00, 0x01]
@@ -110,35 +106,13 @@ fn generate_public_rsa_check_modulus() -> Result<()> {
110106
fn failed_created_key_should_be_removed() -> Result<()> {
111107
let mut client = TestClient::new();
112108
let key_name = String::from("failed_created_key_should_be_removed");
109+
const GARBAGE_IMPORT_DATA: [u8; 1] = [
110+
48,
111+
];
113112

114-
let attributes = Attributes {
115-
lifetime: Lifetime::Persistent,
116-
key_type: Type::Arc4,
117-
bits: 1024,
118-
policy: Policy {
119-
usage_flags: UsageFlags {
120-
sign_hash: false,
121-
verify_hash: true,
122-
sign_message: false,
123-
verify_message: true,
124-
export: false,
125-
encrypt: false,
126-
decrypt: false,
127-
cache: false,
128-
copy: false,
129-
derive: false,
130-
},
131-
permitted_algorithms: Algorithm::AsymmetricSignature(
132-
AsymmetricSignature::RsaPkcs1v15Sign {
133-
hash_alg: Hash::Sha256.into(),
134-
},
135-
),
136-
},
137-
};
138-
139-
// Unsupported parameter, should fail
113+
// The data being imported is garbage, should fail
140114
let _ = client
141-
.generate_key(key_name.clone(), attributes)
115+
.import_rsa_public_key(key_name.clone(), GARBAGE_IMPORT_DATA.to_vec())
142116
.unwrap_err();
143117
// The key should not exist anymore in the KIM
144118
client.generate_rsa_sign_key(key_name)?;
Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3-
use super::constants::PSA_SUCCESS;
4-
use super::utils::{self, KeyHandle};
5-
use super::{key_management, psa_crypto_binding, MbedProvider};
3+
use super::utils;
4+
use super::{key_management, MbedProvider};
65
use crate::authenticators::ApplicationName;
76
use crate::key_info_managers::KeyTriple;
87
use log::{error, info};
98
use parsec_interface::operations::{psa_sign_hash, psa_verify_hash};
10-
use parsec_interface::requests::{ProviderID, Result};
9+
use parsec_interface::requests::{ProviderID, ResponseStatus, Result};
10+
use psa_crypto::operations::key_management as new_key_management;
11+
use psa_crypto::operations::asym_signature;
12+
use psa_crypto::types::key;
1113

14+
#[allow(unused)]
1215
impl MbedProvider {
1316
pub(super) fn psa_sign_hash_internal(
1417
&self,
@@ -28,50 +31,29 @@ impl MbedProvider {
2831
.key_handle_mutex
2932
.lock()
3033
.expect("Grabbing key handle mutex failed");
31-
32-
let mut key_handle;
33-
let mut key_attrs;
34-
// Safety:
35-
// * at this point the provider has been instantiated so Mbed Crypto has been initialized
36-
// * self.key_handle_mutex prevents concurrent accesses
37-
// * self.key_slot_semaphore prevents overflowing key slots
38-
unsafe {
39-
key_handle = KeyHandle::open(key_id)?;
40-
key_attrs = key_handle.attributes()?;
41-
}
42-
43-
let buffer_size = utils::psa_asymmetric_sign_output_size(key_attrs.as_ref())?;
34+
let id = key::Id::from_persistent_key_id(key_id);
35+
let key_attributes = new_key_management::get_key_attributes(id)?;
36+
let buffer_size = utils::psa_asymmetric_sign_output_size(&key_attributes)?;
4437
let mut signature = vec![0u8; buffer_size];
4538
let mut signature_size = 0;
4639

47-
let sign_status;
48-
// Safety: same conditions than above.
49-
unsafe {
50-
sign_status = psa_crypto_binding::psa_asymmetric_sign(
51-
key_handle.raw(),
52-
utils::convert_algorithm(&alg.into())?,
53-
hash.as_ptr(),
54-
hash.len() as u64,
55-
signature.as_mut_ptr(),
56-
buffer_size as u64,
57-
&mut signature_size,
58-
);
59-
key_attrs.reset();
60-
key_handle.close()?;
61-
};
6240

63-
if sign_status == PSA_SUCCESS {
64-
let mut res = psa_sign_hash::Result {
65-
signature: Vec::new(),
66-
};
67-
res.signature.resize(signature_size as usize, 0);
68-
res.signature
69-
.copy_from_slice(&signature[0..signature_size as usize]);
70-
71-
Ok(res)
72-
} else {
73-
error!("Sign status: {}", sign_status);
74-
Err(utils::convert_status(sign_status))
41+
match asym_signature::sign_hash(id, alg, &hash, &mut signature)
42+
{
43+
Ok(size) => {
44+
let mut res = psa_sign_hash::Result {
45+
signature: Vec::new(),
46+
};
47+
res.signature.resize(size, 0);
48+
res.signature
49+
.copy_from_slice(&signature[0..size]);
50+
Ok(res)
51+
}
52+
Err(error) => {
53+
let error = ResponseStatus::from(error);
54+
error!("Sign status: {}", error);
55+
Err(error)
56+
}
7557
}
7658
}
7759

@@ -95,32 +77,14 @@ impl MbedProvider {
9577
.lock()
9678
.expect("Grabbing key handle mutex failed");
9779

98-
let mut key_handle;
99-
let mut key_attrs;
100-
let verify_status;
101-
// Safety:
102-
// * at this point the provider has been instantiated so Mbed Crypto has been initialized
103-
// * self.key_handle_mutex prevents concurrent accesses
104-
// * self.key_slot_semaphore prevents overflowing key slots
105-
unsafe {
106-
key_handle = KeyHandle::open(key_id)?;
107-
key_attrs = key_handle.attributes()?;
108-
verify_status = psa_crypto_binding::psa_asymmetric_verify(
109-
key_handle.raw(),
110-
utils::convert_algorithm(&alg.into())?,
111-
hash.as_ptr(),
112-
hash.len() as u64,
113-
signature.as_ptr(),
114-
signature.len() as u64,
115-
);
116-
key_attrs.reset();
117-
key_handle.close()?;
118-
}
119-
120-
if verify_status == PSA_SUCCESS {
121-
Ok(psa_verify_hash::Result {})
122-
} else {
123-
Err(utils::convert_status(verify_status))
80+
let id = key::Id::from_persistent_key_id(key_id);
81+
match asym_signature::verify_hash(id, alg, &hash, &signature) {
82+
Ok(()) => Ok(psa_verify_hash::Result {}),
83+
Err(error) => {
84+
let error = ResponseStatus::from(error);
85+
error!("Verify status: {}", error);
86+
Err(error)
87+
}
12488
}
12589
}
12690
}

0 commit comments

Comments
 (0)