Skip to content

Commit 0c61762

Browse files
authored
Merge pull request #196 from sbailey-arm/add-asym-encryption
Added asymmetric encrypt and decrypt to Mbed Crypto provider
2 parents 775775c + 599d8d1 commit 0c61762

File tree

15 files changed

+520
-48
lines changed

15 files changed

+520
-48
lines changed

Diff for: 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)

Diff for: Cargo.lock

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

Diff for: 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
picky-asn1-x509 = { version = "0.1.0", optional = true }
4646

Diff for: e2e_tests/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ rand = "0.7.3"
2525
[dev-dependencies]
2626
env_logger = "0.7.1"
2727
uuid = "0.7.4"
28-
picky-asn1-x509 = "0.1.0"
28+
rsa = "0.3.0"
29+
picky-asn1-x509 = "0.1.0"
30+
base64 = "0.12.3"

Diff for: e2e_tests/src/lib.rs

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

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

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

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

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

0 commit comments

Comments
 (0)