Skip to content

Commit 006c8d6

Browse files
authored
Merge pull request #580 from ionut-arm/ts-asym-encr-v3
Add asymmetric encryption to TS provider
2 parents 985d3bb + 0e589dc commit 006c8d6

File tree

12 files changed

+184
-21
lines changed

12 files changed

+184
-21
lines changed

ci.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ setup_mappings() {
126126
reload_service
127127
}
128128

129+
# Use the newest version of the Rust toolchain
130+
rustup update
131+
129132
# Parse arguments
130133
NO_CARGO_CLEAN=
131134
NO_STRESS_TEST=
@@ -288,7 +291,6 @@ if [ "$PROVIDER_NAME" = "cargo-check" ]; then
288291
# - openSUSE Tumbleweed
289292
# - openSUSE Leap 15.3
290293
# The oldest is currently in openSUSE Leap 15.3 and is 1.53.0.
291-
rustup update
292294

293295
rustup toolchain install 1.53.0
294296
# The "jwt-svid-authenticator" can not be compiled on 1.53.0

e2e_tests/docker_image/parsec-service-test-all.Dockerfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ RUN rm -rf tpm2-tss
3131
# Download and install TPM 2.0 Tools verison 4.1.1
3232
RUN git clone https://github.com/tpm2-software/tpm2-tools.git --branch 4.1.1
3333
RUN cd tpm2-tools \
34-
&& ./bootstrap \
35-
&& ./configure --prefix=/usr \
36-
&& make -j$(nproc) \
37-
&& make install
34+
&& ./bootstrap \
35+
&& ./configure --prefix=/usr \
36+
&& make -j$(nproc) \
37+
&& make install
3838
RUN rm -rf tpm2-tools
3939

4040
# Download and install software TPM
@@ -48,7 +48,7 @@ RUN mkdir -p $ibmtpm_name \
4848
WORKDIR $ibmtpm_name/src
4949
RUN sed -i 's/-DTPM_NUVOTON/-DTPM_NUVOTON $(CFLAGS)/' makefile
5050
RUN CFLAGS="-DNV_MEMORY_SIZE=32768 -DMIN_EVICT_OBJECTS=7" make -j$(nproc) \
51-
&& cp tpm_server /usr/local/bin
51+
&& cp tpm_server /usr/local/bin
5252
RUN rm -rf $ibmtpm_name/src $ibmtpm_name
5353

5454
# Download and install SoftHSMv2
@@ -90,13 +90,13 @@ RUN git config --global user.email "[email protected]"
9090
RUN git config --global user.name "Parsec Team"
9191
RUN git clone https://git.trustedfirmware.org/TS/trusted-services.git --branch integration \
9292
&& cd trusted-services \
93-
&& git reset --hard c1cf9120e4ab0b359a27176b079769b9a7e6bb87
93+
&& git reset --hard 389b50624f25dae860bbbf8b16f75b32f1589c8d
9494
# Install correct python dependencies
9595
RUN pip3 install -r trusted-services/requirements.txt
9696
RUN cd trusted-services/deployments/libts/linux-pc/ \
9797
&& cmake . \
9898
&& make \
99-
&& cp libts.so nanopb_install/lib/libprotobuf-nanopb.a mbedtls_install/lib/libmbedcrypto.a /usr/local/lib/
99+
&& cp libts.so* nanopb_install/lib/libprotobuf-nanopb.a mbedtls_install/lib/libmbedcrypto.a /usr/local/lib/
100100
RUN rm -rf trusted-services
101101

102102
# Create a new token in a new slot. The slot number assigned will be random

src/providers/mbed_crypto/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,10 @@ impl Provider {
149149
}
150150
};
151151
for key_identity in to_remove.iter() {
152-
if mbed_crypto_provider
152+
mbed_crypto_provider
153153
.key_info_store
154154
.remove_key_info(key_identity)
155-
.is_err()
156-
{
157-
return None;
158-
}
155+
.ok()?;
159156
}
160157
}
161158
mbed_crypto_provider.id_counter.store(max_key_id, Relaxed);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2021 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use super::Provider;
4+
use crate::authenticators::ApplicationIdentity;
5+
use crate::key_info_managers::KeyIdentity;
6+
use log::error;
7+
use parsec_interface::operations::{psa_asymmetric_decrypt, psa_asymmetric_encrypt};
8+
use parsec_interface::requests::Result;
9+
10+
impl Provider {
11+
pub(super) fn psa_asymmetric_encrypt_internal(
12+
&self,
13+
application_identity: &ApplicationIdentity,
14+
op: psa_asymmetric_encrypt::Operation,
15+
) -> Result<psa_asymmetric_encrypt::Result> {
16+
let key_identity = KeyIdentity::new(
17+
application_identity.clone(),
18+
self.provider_identity.clone(),
19+
op.key_name.clone(),
20+
);
21+
let key_id = self.key_info_store.get_key_id(&key_identity)?;
22+
let salt_buff = match &op.salt {
23+
Some(salt) => salt.to_vec(),
24+
None => Vec::new(),
25+
};
26+
27+
match self
28+
.context
29+
.asym_encrypt(key_id, op.alg, op.plaintext.to_vec(), salt_buff)
30+
{
31+
Ok(ciphertext) => Ok(psa_asymmetric_encrypt::Result {
32+
ciphertext: ciphertext.into(),
33+
}),
34+
Err(error) => {
35+
error!("Encrypt failed with status: {}", error);
36+
Err(error)
37+
}
38+
}
39+
}
40+
41+
pub(super) fn psa_asymmetric_decrypt_internal(
42+
&self,
43+
application_identity: &ApplicationIdentity,
44+
op: psa_asymmetric_decrypt::Operation,
45+
) -> Result<psa_asymmetric_decrypt::Result> {
46+
let key_identity = KeyIdentity::new(
47+
application_identity.clone(),
48+
self.provider_identity.clone(),
49+
op.key_name.clone(),
50+
);
51+
let key_id = self.key_info_store.get_key_id(&key_identity)?;
52+
let salt_buff = match &op.salt {
53+
Some(salt) => salt.to_vec(),
54+
None => Vec::new(),
55+
};
56+
57+
match self
58+
.context
59+
.asym_decrypt(key_id, op.alg, op.ciphertext.to_vec(), salt_buff)
60+
{
61+
Ok(plaintext) => Ok(psa_asymmetric_decrypt::Result {
62+
plaintext: plaintext.into(),
63+
}),
64+
Err(error) => {
65+
error!("Decrypt failed with status: {}", error);
66+
Err(error)
67+
}
68+
}
69+
}
70+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use super::ts_protobuf::{
4+
AsymmetricDecryptIn, AsymmetricDecryptOut, AsymmetricEncryptIn, AsymmetricEncryptOut,
5+
};
6+
use super::Context;
7+
use parsec_interface::operations::psa_algorithm::AsymmetricEncryption;
8+
use parsec_interface::requests::ResponseStatus;
9+
use std::convert::TryInto;
10+
use zeroize::Zeroize;
11+
12+
impl Context {
13+
pub fn asym_encrypt(
14+
&self,
15+
key_id: u32,
16+
alg: AsymmetricEncryption,
17+
mut plaintext: Vec<u8>,
18+
mut salt: Vec<u8>,
19+
) -> Result<Vec<u8>, ResponseStatus> {
20+
let alg = alg.try_into().map_err(|e| {
21+
plaintext.zeroize();
22+
salt.zeroize();
23+
e
24+
})?;
25+
let req = AsymmetricEncryptIn {
26+
id: key_id,
27+
alg,
28+
plaintext,
29+
salt,
30+
};
31+
let AsymmetricEncryptOut { ciphertext } = self.send_request(&req)?;
32+
33+
Ok(ciphertext)
34+
}
35+
36+
pub fn asym_decrypt(
37+
&self,
38+
key_id: u32,
39+
alg: AsymmetricEncryption,
40+
mut ciphertext: Vec<u8>,
41+
mut salt: Vec<u8>,
42+
) -> Result<Vec<u8>, ResponseStatus> {
43+
let alg = alg.try_into().map_err(|e| {
44+
ciphertext.zeroize();
45+
salt.zeroize();
46+
e
47+
})?;
48+
let req = AsymmetricDecryptIn {
49+
id: key_id,
50+
alg,
51+
ciphertext,
52+
salt,
53+
};
54+
let AsymmetricDecryptOut { plaintext } = self.send_request(&req)?;
55+
56+
Ok(plaintext)
57+
}
58+
}

src/providers/trusted_service/context/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ pub enum WrapperError {
178178
CallBufferNull,
179179
/// serialization or deserialization of protobuf message failed
180180
FailedPbConversion,
181+
/// invalid operation status value
182+
InvalidOpStatus,
181183
/// a parameter passed to the function was invalid
182184
InvalidParam,
183185
}
@@ -196,6 +198,9 @@ impl fmt::Display for WrapperError {
196198
WrapperError::InvalidParam => {
197199
write!(f, "a parameter passed to the function was invalid")
198200
}
201+
WrapperError::InvalidOpStatus => {
202+
write!(f, "the RPC layer returned an invalid operation status")
203+
}
199204
}
200205
}
201206
}

src/providers/trusted_service/context/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use error::{Error, WrapperError};
44
use log::{error, info, trace};
55
use prost::Message;
6-
use std::convert::TryInto;
6+
use std::convert::{TryFrom, TryInto};
77
use std::ffi::{c_void, CString};
88
use std::io::{self};
99
use std::ptr::null_mut;
@@ -27,9 +27,11 @@ use ts_protobuf::GetOpcode;
2727
unused_qualifications
2828
)]
2929
pub mod ts_binding {
30+
#![allow(deref_nullptr)]
3031
include!(concat!(env!("OUT_DIR"), "/ts_bindings.rs"));
3132
}
3233

34+
mod asym_encryption;
3335
mod asym_sign;
3436
pub mod error;
3537
mod generate_random;
@@ -158,7 +160,11 @@ impl Context {
158160
&mut resp_buf_size,
159161
)
160162
};
161-
Error::from_status_opstatus(status, opstatus).map_err(|e| {
163+
Error::from_status_opstatus(
164+
status,
165+
i32::try_from(opstatus).map_err(|_| Error::Wrapper(WrapperError::InvalidOpStatus))?,
166+
)
167+
.map_err(|e| {
162168
unsafe { rpc_caller_end(self.rpc_caller, call_handle) };
163169
e
164170
})?;

src/providers/trusted_service/context/ts_protobuf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ opcode_impl!(ImportKeyIn, ImportKeyOut, ImportKey);
5656
opcode_impl!(ExportPublicKeyIn, ExportPublicKeyOut, ExportPublicKey);
5757
opcode_impl!(ExportKeyIn, ExportKeyOut, ExportKey);
5858
opcode_impl!(GenerateRandomIn, GenerateRandomOut, GenerateRandom);
59+
opcode_impl!(AsymmetricDecryptIn, AsymmetricDecryptOut, AsymmetricDecrypt);
60+
opcode_impl!(AsymmetricEncryptIn, AsymmetricEncryptOut, AsymmetricEncrypt);
5961

6062
impl Drop for ImportKeyIn {
6163
fn drop(&mut self) {

src/providers/trusted_service/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ impl From<WrapperError> for ResponseStatus {
2626
WrapperError::CallBufferNull
2727
| WrapperError::CallHandleNull
2828
| WrapperError::FailedPbConversion
29-
| WrapperError::InvalidParam => ResponseStatus::PsaErrorCommunicationFailure,
29+
| WrapperError::InvalidParam
30+
| WrapperError::InvalidOpStatus => ResponseStatus::PsaErrorCommunicationFailure,
3031
}
3132
}
3233
}

src/providers/trusted_service/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ use derivative::Derivative;
1212
use log::{error, trace};
1313
use parsec_interface::operations::list_providers::ProviderInfo;
1414
use parsec_interface::operations::{
15-
can_do_crypto, list_clients, list_keys, psa_destroy_key, psa_export_key, psa_export_public_key,
16-
psa_generate_key, psa_generate_random, psa_import_key, psa_sign_hash, psa_verify_hash,
15+
can_do_crypto, list_clients, list_keys, psa_asymmetric_decrypt, psa_asymmetric_encrypt,
16+
psa_destroy_key, psa_export_key, psa_export_public_key, psa_generate_key, psa_generate_random,
17+
psa_import_key, psa_sign_hash, psa_verify_hash,
1718
};
1819
use parsec_interface::requests::{Opcode, ProviderId, Result};
1920
use psa_crypto::types::key;
2021
use std::collections::HashSet;
2122
use std::sync::atomic::{AtomicU32, Ordering};
2223
use uuid::Uuid;
2324

25+
mod asym_encryption;
2426
mod asym_sign;
2527
mod capability_discovery;
2628
mod context;
2729
mod error;
2830
mod generate_random;
2931
mod key_management;
3032

31-
const SUPPORTED_OPCODES: [Opcode; 9] = [
33+
const SUPPORTED_OPCODES: [Opcode; 11] = [
3234
Opcode::PsaDestroyKey,
3335
Opcode::PsaGenerateKey,
3436
Opcode::PsaSignHash,
@@ -38,6 +40,8 @@ const SUPPORTED_OPCODES: [Opcode; 9] = [
3840
Opcode::PsaExportKey,
3941
Opcode::PsaGenerateRandom,
4042
Opcode::CanDoCrypto,
43+
Opcode::PsaAsymmetricEncrypt,
44+
Opcode::PsaAsymmetricDecrypt,
4145
];
4246
/// Trusted Service provider structure
4347
///
@@ -239,6 +243,24 @@ impl Provide for Provider {
239243
trace!("can_do_crypto ingress");
240244
self.can_do_crypto_main(application_identity, op)
241245
}
246+
247+
fn psa_asymmetric_encrypt(
248+
&self,
249+
application_identity: &ApplicationIdentity,
250+
op: psa_asymmetric_encrypt::Operation,
251+
) -> Result<psa_asymmetric_encrypt::Result> {
252+
trace!("psa_asymmetric_encrypt ingress");
253+
self.psa_asymmetric_encrypt_internal(application_identity, op)
254+
}
255+
256+
fn psa_asymmetric_decrypt(
257+
&self,
258+
application_identity: &ApplicationIdentity,
259+
op: psa_asymmetric_decrypt::Operation,
260+
) -> Result<psa_asymmetric_decrypt::Result> {
261+
trace!("psa_asymmetric_decrypt ingress");
262+
self.psa_asymmetric_decrypt_internal(application_identity, op)
263+
}
242264
}
243265

244266
/// Trusted Service provider builder

src/utils/service_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ unsafe fn get_provider(
422422
}
423423
#[cfg(feature = "trusted-service-provider")]
424424
ProviderConfig::TrustedService { .. } => {
425-
info!("Creating a TPM Provider.");
425+
info!("Creating a Trusted Service Provider.");
426426
let provider_identity = ProviderIdentity::new(
427427
TrustedServiceProvider::PROVIDER_UUID.to_string(),
428428
config.provider_name()?,

trusted-services-vendor

Submodule trusted-services-vendor updated from c1cf912 to 389b506

0 commit comments

Comments
 (0)