-
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.
Update TS submodule and add asym encryption
This commit pulls in new changes to the upstream TS project and adds asymmetric encryption support for the provider. Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
- Loading branch information
Showing
11 changed files
with
217 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use super::Provider; | ||
use crate::authenticators::ApplicationName; | ||
use crate::key_info_managers::KeyTriple; | ||
use log::error; | ||
use parsec_interface::operations::{psa_asymmetric_decrypt, psa_asymmetric_encrypt}; | ||
use parsec_interface::requests::{ProviderId, Result}; | ||
|
||
impl Provider { | ||
pub(super) fn psa_asymmetric_encrypt_internal( | ||
&self, | ||
app_name: ApplicationName, | ||
op: psa_asymmetric_encrypt::Operation, | ||
) -> Result<psa_asymmetric_encrypt::Result> { | ||
let key_name = op.key_name.clone(); | ||
|
||
let key_triple = KeyTriple::new(app_name, ProviderId::TrustedService, key_name); | ||
let key_id = self.key_info_store.get_key_id(&key_triple)?; | ||
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, | ||
app_name: ApplicationName, | ||
op: psa_asymmetric_decrypt::Operation, | ||
) -> Result<psa_asymmetric_decrypt::Result> { | ||
let key_triple = KeyTriple::new(app_name, ProviderId::TrustedService, op.key_name.clone()); | ||
let key_id = self.key_info_store.get_key_id(&key_triple)?; | ||
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
Oops, something went wrong.