Skip to content

Commit 4b2fa81

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

File tree

13 files changed

+522
-28
lines changed

13 files changed

+522
-28
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

+143-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, AsymmetricSignature, AsymmetricEncryption, Hash,
1717
};
1818
use parsec_client::core::interface::operations::psa_key_attributes::{
1919
Attributes, Lifetime, Policy, Type, UsageFlags,
@@ -157,6 +157,60 @@ impl TestClient {
157157
)
158158
}
159159

160+
pub fn generate_rsa_encryption_keys_rsapkcs1v15crypt(&mut self, key_name: String) -> Result<()> {
161+
self.generate_key(
162+
key_name,
163+
Attributes {
164+
lifetime: Lifetime::Persistent,
165+
key_type: Type::RsaKeyPair,
166+
bits: 1024,
167+
policy: Policy {
168+
usage_flags: UsageFlags {
169+
sign_hash: false,
170+
verify_hash: false,
171+
sign_message: false,
172+
verify_message: false,
173+
export: true,
174+
encrypt: true,
175+
decrypt: true,
176+
cache: false,
177+
copy: false,
178+
derive: false,
179+
},
180+
permitted_algorithms: AsymmetricEncryption::RsaPkcs1v15Crypt.into(),
181+
},
182+
}
183+
)
184+
}
185+
186+
pub fn generate_rsa_encryption_keys_rsaoaep_sha256(&mut self, key_name: String) -> Result<()> {
187+
self.generate_key(
188+
key_name,
189+
Attributes {
190+
lifetime: Lifetime::Persistent,
191+
key_type: Type::RsaKeyPair,
192+
bits: 1024,
193+
policy: Policy {
194+
usage_flags: UsageFlags {
195+
sign_hash: false,
196+
verify_hash: false,
197+
sign_message: false,
198+
verify_message: false,
199+
export: true,
200+
encrypt: true,
201+
decrypt: true,
202+
cache: false,
203+
copy: false,
204+
derive: false,
205+
},
206+
permitted_algorithms: AsymmetricEncryption::RsaOaep{
207+
hash_alg: Hash::Sha256,
208+
}.into(),
209+
},
210+
}
211+
)
212+
}
213+
160214
/// Imports and creates a key with specific attributes.
161215
pub fn import_key(
162216
&mut self,
@@ -178,7 +232,36 @@ impl TestClient {
178232
Ok(())
179233
}
180234

181-
/// Import a 1024 bits RSA public key.
235+
/// Import a 1024 bit RSA key pair
236+
/// The key pair can only be used for encryption and decryption with RSA PKCS 1v15
237+
pub fn import_rsa_key_pair(&mut self, key_name: String, data: Vec<u8>) -> Result<()> {
238+
self.import_key(
239+
key_name,
240+
Attributes {
241+
lifetime: Lifetime::Persistent,
242+
key_type: Type::RsaKeyPair,
243+
bits: 1024,
244+
policy: Policy {
245+
usage_flags: UsageFlags {
246+
sign_hash: false,
247+
verify_hash: false,
248+
sign_message: false,
249+
verify_message: true,
250+
export: false,
251+
encrypt: true,
252+
decrypt: true,
253+
cache: false,
254+
copy: false,
255+
derive: false,
256+
},
257+
permitted_algorithms: AsymmetricEncryption::RsaPkcs1v15Crypt.into(),
258+
},
259+
},
260+
data,
261+
)
262+
}
263+
264+
/// Import a 1024 bit RSA public key.
182265
/// The key can only be used for verifying with the RSA PKCS 1v15 signing algorithm with SHA-256.
183266
pub fn import_rsa_public_key(&mut self, key_name: String, data: Vec<u8>) -> Result<()> {
184267
self.import_key(
@@ -287,6 +370,64 @@ impl TestClient {
287370
)
288371
}
289372

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

0 commit comments

Comments
 (0)