-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #580 from ionut-arm/ts-asym-encr-v3
Add asymmetric encryption to TS provider
- Loading branch information
Showing
12 changed files
with
184 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use super::Provider; | ||
use crate::authenticators::ApplicationIdentity; | ||
use crate::key_info_managers::KeyIdentity; | ||
use log::error; | ||
use parsec_interface::operations::{psa_asymmetric_decrypt, psa_asymmetric_encrypt}; | ||
use parsec_interface::requests::Result; | ||
|
||
impl Provider { | ||
pub(super) fn psa_asymmetric_encrypt_internal( | ||
&self, | ||
application_identity: &ApplicationIdentity, | ||
op: psa_asymmetric_encrypt::Operation, | ||
) -> Result<psa_asymmetric_encrypt::Result> { | ||
let key_identity = KeyIdentity::new( | ||
application_identity.clone(), | ||
self.provider_identity.clone(), | ||
op.key_name.clone(), | ||
); | ||
let key_id = self.key_info_store.get_key_id(&key_identity)?; | ||
let salt_buff = match &op.salt { | ||
Some(salt) => salt.to_vec(), | ||
None => Vec::new(), | ||
}; | ||
|
||
match self | ||
.context | ||
.asym_encrypt(key_id, op.alg, op.plaintext.to_vec(), salt_buff) | ||
{ | ||
Ok(ciphertext) => Ok(psa_asymmetric_encrypt::Result { | ||
ciphertext: ciphertext.into(), | ||
}), | ||
Err(error) => { | ||
error!("Encrypt failed with status: {}", error); | ||
Err(error) | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn psa_asymmetric_decrypt_internal( | ||
&self, | ||
application_identity: &ApplicationIdentity, | ||
op: psa_asymmetric_decrypt::Operation, | ||
) -> Result<psa_asymmetric_decrypt::Result> { | ||
let key_identity = KeyIdentity::new( | ||
application_identity.clone(), | ||
self.provider_identity.clone(), | ||
op.key_name.clone(), | ||
); | ||
let key_id = self.key_info_store.get_key_id(&key_identity)?; | ||
let salt_buff = match &op.salt { | ||
Some(salt) => salt.to_vec(), | ||
None => Vec::new(), | ||
}; | ||
|
||
match self | ||
.context | ||
.asym_decrypt(key_id, op.alg, op.ciphertext.to_vec(), salt_buff) | ||
{ | ||
Ok(plaintext) => Ok(psa_asymmetric_decrypt::Result { | ||
plaintext: plaintext.into(), | ||
}), | ||
Err(error) => { | ||
error!("Decrypt failed with status: {}", error); | ||
Err(error) | ||
} | ||
} | ||
} | ||
} |
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,58 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use super::ts_protobuf::{ | ||
AsymmetricDecryptIn, AsymmetricDecryptOut, AsymmetricEncryptIn, AsymmetricEncryptOut, | ||
}; | ||
use super::Context; | ||
use parsec_interface::operations::psa_algorithm::AsymmetricEncryption; | ||
use parsec_interface::requests::ResponseStatus; | ||
use std::convert::TryInto; | ||
use zeroize::Zeroize; | ||
|
||
impl Context { | ||
pub fn asym_encrypt( | ||
&self, | ||
key_id: u32, | ||
alg: AsymmetricEncryption, | ||
mut plaintext: Vec<u8>, | ||
mut salt: Vec<u8>, | ||
) -> Result<Vec<u8>, ResponseStatus> { | ||
let alg = alg.try_into().map_err(|e| { | ||
plaintext.zeroize(); | ||
salt.zeroize(); | ||
e | ||
})?; | ||
let req = AsymmetricEncryptIn { | ||
id: key_id, | ||
alg, | ||
plaintext, | ||
salt, | ||
}; | ||
let AsymmetricEncryptOut { ciphertext } = self.send_request(&req)?; | ||
|
||
Ok(ciphertext) | ||
} | ||
|
||
pub fn asym_decrypt( | ||
&self, | ||
key_id: u32, | ||
alg: AsymmetricEncryption, | ||
mut ciphertext: Vec<u8>, | ||
mut salt: Vec<u8>, | ||
) -> Result<Vec<u8>, ResponseStatus> { | ||
let alg = alg.try_into().map_err(|e| { | ||
ciphertext.zeroize(); | ||
salt.zeroize(); | ||
e | ||
})?; | ||
let req = AsymmetricDecryptIn { | ||
id: key_id, | ||
alg, | ||
ciphertext, | ||
salt, | ||
}; | ||
let AsymmetricDecryptOut { plaintext } = self.send_request(&req)?; | ||
|
||
Ok(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
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
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
Submodule trusted-services-vendor
updated
from c1cf91 to 389b50