Skip to content

Commit e45d0e0

Browse files
committed
Added asymmetric encrypt and decrypt to Mbed Crypto provider
Signed-off-by: Samuel Bailey <[email protected]>
1 parent 6ca8010 commit e45d0e0

File tree

15 files changed

+521
-48
lines changed

15 files changed

+521
-48
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ This file aims to acknowledge the specific contributors referred to in the "Cont
1212
* Ionut Mihalcea (@ionut-arm)
1313
* Hugues de Valon (@hug-dev)
1414
* Jesper Brynolf (@Superhepper)
15+
* Samuel Bailey (@sbailey-arm)

Cargo.lock

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
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.17.0"
21+
parsec-interface = "0.18.0"
2222
rand = { version = "0.7.2", features = ["small_rng"] }
2323
base64 = "0.10.1"
2424
uuid = "0.7.4"
@@ -40,7 +40,7 @@ derivative = "2.1.1"
4040
version = "3.0.0"
4141
hex = "0.4.2"
4242
picky = "5.0.0"
43-
psa-crypto = { version = "0.2.1" , default-features = false, features = ["with-mbed-crypto"], optional = true }
43+
psa-crypto = { version = "0.2.2" , default-features = false, features = ["with-mbed-crypto"], optional = true }
4444
zeroize = { version = "1.1.0", features = ["zeroize_derive"] }
4545

4646
[dev-dependencies]

e2e_tests/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ picky-asn1-der = "0.2.2"
1818
picky-asn1 = "0.2.1"
1919
serde = { version = "1.0", features = ["derive"] }
2020
sha2 = "0.8.1"
21-
parsec-client = { git = "https://github.com/parallaxsecond/parsec-client-rust", features = ["testing"] }
21+
parsec-client = { version = "0.6.0", features = ["testing"] }
2222
log = "0.4.8"
2323
rand = "0.7.3"
2424

2525
[dev-dependencies]
2626
env_logger = "0.7.1"
2727
uuid = "0.7.4"
28+
rsa = "0.3.0"
29+
picky-asn1-x509 = "0.1.0"
30+
base64 = "0.12.3"

e2e_tests/src/lib.rs

+145-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use parsec_client::auth::AuthenticationData;
1313
use parsec_client::core::basic_client::BasicClient;
1414
use parsec_client::core::interface::operations::list_providers::ProviderInfo;
1515
use parsec_client::core::interface::operations::psa_algorithm::{
16-
Algorithm, AsymmetricSignature, Hash,
16+
Algorithm, AsymmetricEncryption, AsymmetricSignature, Hash,
1717
};
1818
use parsec_client::core::interface::operations::psa_key_attributes::{
1919
Attributes, Lifetime, Policy, Type, UsageFlags,
@@ -78,6 +78,12 @@ impl TestClient {
7878
ProviderID::Core
7979
}
8080

81+
pub fn is_operation_supported(&mut self, op: Opcode) -> bool {
82+
self.list_opcodes(self.provider().unwrap())
83+
.unwrap()
84+
.contains(&op)
85+
}
86+
8187
/// Manually set the provider to execute the requests.
8288
pub fn set_provider(&mut self, provider: ProviderID) {
8389
self.basic_client.set_implicit_provider(provider);
@@ -157,6 +163,64 @@ impl TestClient {
157163
)
158164
}
159165

166+
pub fn generate_rsa_encryption_keys_rsapkcs1v15crypt(
167+
&mut self,
168+
key_name: String,
169+
) -> Result<()> {
170+
self.generate_key(
171+
key_name,
172+
Attributes {
173+
lifetime: Lifetime::Persistent,
174+
key_type: Type::RsaKeyPair,
175+
bits: 1024,
176+
policy: Policy {
177+
usage_flags: UsageFlags {
178+
sign_hash: false,
179+
verify_hash: false,
180+
sign_message: false,
181+
verify_message: false,
182+
export: true,
183+
encrypt: true,
184+
decrypt: true,
185+
cache: false,
186+
copy: false,
187+
derive: false,
188+
},
189+
permitted_algorithms: AsymmetricEncryption::RsaPkcs1v15Crypt.into(),
190+
},
191+
},
192+
)
193+
}
194+
195+
pub fn generate_rsa_encryption_keys_rsaoaep_sha256(&mut self, key_name: String) -> Result<()> {
196+
self.generate_key(
197+
key_name,
198+
Attributes {
199+
lifetime: Lifetime::Persistent,
200+
key_type: Type::RsaKeyPair,
201+
bits: 1024,
202+
policy: Policy {
203+
usage_flags: UsageFlags {
204+
sign_hash: false,
205+
verify_hash: false,
206+
sign_message: false,
207+
verify_message: false,
208+
export: true,
209+
encrypt: true,
210+
decrypt: true,
211+
cache: false,
212+
copy: false,
213+
derive: false,
214+
},
215+
permitted_algorithms: AsymmetricEncryption::RsaOaep {
216+
hash_alg: Hash::Sha256,
217+
}
218+
.into(),
219+
},
220+
},
221+
)
222+
}
223+
160224
/// Imports and creates a key with specific attributes.
161225
pub fn import_key(
162226
&mut self,
@@ -178,7 +242,36 @@ impl TestClient {
178242
Ok(())
179243
}
180244

181-
/// Import a 1024 bits RSA public key.
245+
/// Import a 1024 bit RSA key pair
246+
/// The key pair can only be used for encryption and decryption with RSA PKCS 1v15
247+
pub fn import_rsa_key_pair(&mut self, key_name: String, data: Vec<u8>) -> Result<()> {
248+
self.import_key(
249+
key_name,
250+
Attributes {
251+
lifetime: Lifetime::Persistent,
252+
key_type: Type::RsaKeyPair,
253+
bits: 1024,
254+
policy: Policy {
255+
usage_flags: UsageFlags {
256+
sign_hash: false,
257+
verify_hash: false,
258+
sign_message: false,
259+
verify_message: true,
260+
export: false,
261+
encrypt: true,
262+
decrypt: true,
263+
cache: false,
264+
copy: false,
265+
derive: false,
266+
},
267+
permitted_algorithms: AsymmetricEncryption::RsaPkcs1v15Crypt.into(),
268+
},
269+
},
270+
data,
271+
)
272+
}
273+
274+
/// Import a 1024 bit RSA public key.
182275
/// The key can only be used for verifying with the RSA PKCS 1v15 signing algorithm with SHA-256.
183276
pub fn import_rsa_public_key(&mut self, key_name: String, data: Vec<u8>) -> Result<()> {
184277
self.import_key(
@@ -287,6 +380,56 @@ impl TestClient {
287380
)
288381
}
289382

383+
pub fn asymmetric_encrypt_message_with_rsapkcs1v15(
384+
&mut self,
385+
key_name: String,
386+
plaintext: Vec<u8>,
387+
) -> Result<Vec<u8>> {
388+
self.asymmetric_encrypt_message(
389+
key_name,
390+
AsymmetricEncryption::RsaPkcs1v15Crypt,
391+
&plaintext,
392+
None,
393+
)
394+
}
395+
396+
pub fn asymmetric_decrypt_message_with_rsapkcs1v15(
397+
&mut self,
398+
key_name: String,
399+
ciphertext: Vec<u8>,
400+
) -> Result<Vec<u8>> {
401+
self.asymmetric_decrypt_message(
402+
key_name,
403+
AsymmetricEncryption::RsaPkcs1v15Crypt,
404+
&ciphertext,
405+
None,
406+
)
407+
}
408+
409+
pub fn asymmetric_encrypt_message(
410+
&mut self,
411+
key_name: String,
412+
encryption_alg: AsymmetricEncryption,
413+
plaintext: &[u8],
414+
salt: Option<&[u8]>,
415+
) -> Result<Vec<u8>> {
416+
self.basic_client
417+
.psa_asymmetric_encrypt(key_name, encryption_alg, &plaintext, salt)
418+
.map_err(convert_error)
419+
}
420+
421+
pub fn asymmetric_decrypt_message(
422+
&mut self,
423+
key_name: String,
424+
encryption_alg: AsymmetricEncryption,
425+
ciphertext: &[u8],
426+
salt: Option<&[u8]>,
427+
) -> Result<Vec<u8>> {
428+
self.basic_client
429+
.psa_asymmetric_decrypt(key_name, encryption_alg, &ciphertext, salt)
430+
.map_err(convert_error)
431+
}
432+
290433
/// Lists the provider available for the Parsec service.
291434
pub fn list_providers(&mut self) -> Result<Vec<ProviderInfo>> {
292435
self.basic_client.list_providers().map_err(convert_error)

0 commit comments

Comments
 (0)