-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added asymmetric encrypt and decrypt to Mbed Crypto provider
Signed-off-by: Samuel Bailey <samuel.bailey@arm.com>
- Loading branch information
1 parent
6ca8010
commit 79a3a14
Showing
13 changed files
with
410 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
e2e_tests/tests/per_provider/normal_tests/asym_encryption.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2020 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use e2e_tests::TestClient; | ||
use parsec_client::core::interface::requests::ResponseStatus; | ||
use rsa::{RSAPublicKey, PaddingScheme, PublicKey}; | ||
use rand::rngs::OsRng; | ||
|
||
|
||
const PLAINTEXT_MESSAGE: [u8; 32] = [ | ||
0x69, 0x3E, 0xDB, 0x1B, 0x22, 0x79, 0x03, 0xF4, 0xC0, 0xBF, 0xD6, 0x91, 0x76, 0x37, 0x84, 0xA2, | ||
0x94, 0x8E, 0x92, 0x50, 0x35, 0xC2, 0x8C, 0x5C, 0x3C, 0xCA, 0xFE, 0x18, 0xE8, 0x81, 0x37, 0x78, | ||
]; | ||
|
||
#[test] | ||
fn simple_asym_encrypt_rsa_pkcs() { | ||
let key_name = String::from("asym_encrypt_and_decrypt_rsa_pkcs"); | ||
let mut client = TestClient::new(); | ||
client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone()).unwrap(); | ||
let _ciphertext = client.asymmetric_encrypt_message_with_rsapkcs1v15( | ||
key_name.clone(), | ||
PLAINTEXT_MESSAGE.to_vec(), | ||
).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn asym_encrypt_no_key() { | ||
let key_name = String::from("asym_encrypt_no_key"); | ||
let mut client = TestClient::new(); | ||
let status = client. | ||
asymmetric_encrypt_message_with_rsapkcs1v15( | ||
key_name, | ||
PLAINTEXT_MESSAGE.to_vec(), | ||
) | ||
.expect_err("Key should not exist."); | ||
assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist); | ||
} | ||
|
||
#[test] | ||
fn asym_decrypt_no_key() { | ||
let key_name = String::from("asym_decrypt_no_key"); | ||
let mut client = TestClient::new(); | ||
let status = client. | ||
asymmetric_decrypt_message_with_rsapkcs1v15( | ||
key_name, | ||
PLAINTEXT_MESSAGE.to_vec(), | ||
) | ||
.expect_err("Key should not exist."); | ||
assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist); | ||
} | ||
|
||
#[test] | ||
fn asym_encrypt_wrong_algorithm() { | ||
let key_name = String::from("asym_encrypt_wrong_algorithm"); | ||
let mut client = TestClient::new(); | ||
let _key_id = client.generate_rsa_encryption_keys_rsaoaep_sha256(key_name.clone()).unwrap(); | ||
let status = client.asymmetric_encrypt_message_with_rsapkcs1v15( | ||
key_name.clone(), | ||
PLAINTEXT_MESSAGE.to_vec(), | ||
).unwrap_err(); | ||
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted); | ||
} | ||
|
||
#[test] | ||
fn asym_encrypt_and_decrypt_rsa_pkcs() { | ||
let key_name = String::from("asym_encrypt_and_decrypt_rsa_pkcs"); | ||
let mut client = TestClient::new(); | ||
client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone()).unwrap(); | ||
let ciphertext = client.asymmetric_encrypt_message_with_rsapkcs1v15( | ||
key_name.clone(), | ||
PLAINTEXT_MESSAGE.to_vec(), | ||
).unwrap(); | ||
let plaintext = client.asymmetric_decrypt_message_with_rsapkcs1v15( | ||
key_name, | ||
ciphertext, | ||
).unwrap(); | ||
assert_eq!(PLAINTEXT_MESSAGE.to_vec(), plaintext); | ||
} | ||
|
||
#[test] | ||
fn asym_encrypt_decrypt_rsa_pkcs_different_keys() { | ||
let key_name_1 = String::from("asym_encrypt_and_decrypt_rsa_pkcs_different_keys_1"); | ||
let key_name_2 = String::from("asym_encrypt_and_decrypt_rsa_pkcs_different_keys_2"); | ||
let mut client = TestClient::new(); | ||
client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name_1.clone()).unwrap(); | ||
client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name_2.clone()).unwrap(); | ||
let ciphertext = client.asymmetric_encrypt_message_with_rsapkcs1v15( | ||
key_name_1.clone(), | ||
PLAINTEXT_MESSAGE.to_vec(), | ||
).unwrap(); | ||
let _res = client.asymmetric_decrypt_message_with_rsapkcs1v15( | ||
key_name_2.clone(), | ||
ciphertext, | ||
).unwrap_err(); | ||
} | ||
|
||
#[test] | ||
fn asym_encrypt_verify_decrypt_with_rsa_crate() { | ||
let key_name = String::from("asym_encrypt_verify_decrypt_with_rsa_crate"); | ||
let mut client = TestClient::new(); | ||
|
||
client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone()).unwrap(); | ||
let pub_key = client.export_public_key(key_name.clone()).unwrap(); | ||
|
||
let rsa_pub_key = RSAPublicKey::from_pkcs1(&pub_key).unwrap(); | ||
let ciphertext = rsa_pub_key.encrypt(&mut OsRng, PaddingScheme::new_pkcs1v15_encrypt(), &PLAINTEXT_MESSAGE).unwrap(); | ||
|
||
let plaintext = client.asymmetric_decrypt_message_with_rsapkcs1v15( | ||
key_name.clone(), | ||
ciphertext, | ||
).unwrap(); | ||
|
||
assert_eq!(&PLAINTEXT_MESSAGE[..], &plaintext[..]); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ mod export_public_key; | |
mod import_key; | ||
mod key_attributes; | ||
mod ping; | ||
mod asym_encryption; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.