Skip to content

Commit fbb239b

Browse files
authored
Merge pull request #186 from sbailey-arm/development
Move Parsec over to psa-crypto
2 parents f82b042 + 186980a commit fbb239b

File tree

13 files changed

+126
-2716
lines changed

13 files changed

+126
-2716
lines changed

Cargo.lock

Lines changed: 0 additions & 1592 deletions
This file was deleted.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ name = "parsec"
1818
path = "src/bin/main.rs"
1919

2020
[dependencies]
21-
parsec-interface = "0.15.0"
22-
rand = "0.7.2"
21+
parsec-interface = "0.16.0"
22+
rand = { version = "0.7.2", features = ["small_rng"] }
2323
base64 = "0.10.1"
2424
uuid = "0.7.4"
2525
threadpool = "1.7.1"
@@ -40,6 +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.0" , default-features = false, features = ["with-mbed-crypto"], optional = true }
4344

4445
[dev-dependencies]
4546
ring = "0.16.12"
@@ -59,7 +60,7 @@ features = ["docs"]
5960

6061
[features]
6162
default = []
62-
mbed-crypto-provider = []
63+
mbed-crypto-provider = ["psa-crypto"]
6364
pkcs11-provider = ["pkcs11", "picky-asn1-der", "picky-asn1"]
6465
tpm-provider = ["tss-esapi", "picky-asn1-der", "picky-asn1"]
6566
all-providers = ["tpm-provider", "pkcs11-provider", "mbed-crypto-provider"]

build-conf.toml

Lines changed: 0 additions & 19 deletions
This file was deleted.

build.rs

Lines changed: 1 addition & 266 deletions
Original file line numberDiff line numberDiff line change
@@ -1,268 +1,3 @@
1-
// Copyright 2019 Contributors to the Parsec project.
2-
// SPDX-License-Identifier: Apache-2.0
3-
4-
#![deny(
5-
nonstandard_style,
6-
const_err,
7-
dead_code,
8-
improper_ctypes,
9-
non_shorthand_field_patterns,
10-
no_mangle_generic_items,
11-
overflowing_literals,
12-
path_statements,
13-
patterns_in_fns_without_body,
14-
private_in_public,
15-
unconditional_recursion,
16-
unused,
17-
unused_allocation,
18-
unused_comparisons,
19-
unused_parens,
20-
while_true,
21-
missing_debug_implementations,
22-
trivial_casts,
23-
trivial_numeric_casts,
24-
unused_extern_crates,
25-
unused_import_braces,
26-
unused_qualifications,
27-
unused_results,
28-
missing_copy_implementations
29-
)]
30-
// This one is hard to avoid.
311
#![allow(clippy::multiple_crate_versions)]
322

33-
use cargo_toml::{Manifest, Value};
34-
use serde::Deserialize;
35-
use std::env;
36-
use std::io::{Error, ErrorKind, Result};
37-
use std::path::{Path, PathBuf};
38-
39-
const CONFIG_TABLE_NAME: &str = "config";
40-
const MBED_CRYPTO_VERSION_KEY: &str = "mbed-crypto-version";
41-
42-
const SETUP_MBED_SCRIPT_PATH: &str = "./setup_mbed_crypto.sh";
43-
const BUILD_CONFIG_FILE_PATH: &str = "./build-conf.toml";
44-
45-
const DEFAULT_NATIVE_MBED_COMPILER: &str = "clang";
46-
const DEFAULT_NATIVE_MBED_ARCHIVER: &str = "ar";
47-
const DEFAULT_ARM64_MBED_COMPILER: &str = "aarch64-linux-gnu-gcc";
48-
const DEFAULT_ARM64_MBED_ARCHIVER: &str = "aarch64-linux-gnu-ar";
49-
50-
#[derive(Debug, Deserialize)]
51-
struct Configuration {
52-
mbed_config: Option<MbedConfig>,
53-
}
54-
55-
#[derive(Debug, Deserialize)]
56-
struct MbedConfig {
57-
mbed_path: Option<String>,
58-
native: Option<Toolchain>,
59-
aarch64_unknown_linux_gnu: Option<Toolchain>,
60-
}
61-
62-
#[derive(Debug, Deserialize)]
63-
struct Toolchain {
64-
mbed_compiler: Option<String>,
65-
mbed_archiver: Option<String>,
66-
}
67-
68-
fn get_configuration_string(parsec_config: &Value, key: &str) -> Result<String> {
69-
let config_value = get_value_from_table(parsec_config, key)?;
70-
match config_value {
71-
Value::String(string) => Ok(string.clone()),
72-
_ => Err(Error::new(
73-
ErrorKind::InvalidInput,
74-
"Configuration key missing",
75-
)),
76-
}
77-
}
78-
79-
fn get_value_from_table<'a>(table: &'a Value, key: &str) -> Result<&'a Value> {
80-
match table {
81-
Value::Table(table) => table.get(key).ok_or_else(|| {
82-
println!("Config table does not contain configuration key: {}", key);
83-
Error::new(ErrorKind::InvalidInput, "Configuration key missing.")
84-
}),
85-
_ => Err(Error::new(
86-
ErrorKind::InvalidInput,
87-
"Value provided is not a TOML table",
88-
)),
89-
}
90-
}
91-
92-
// Get the Mbed Crypto version to branch on from Cargo.toml file. Use that and MbedConfig to pass
93-
// parameters to the setup_mbed_crypto.sh script which clones and builds Mbed Crypto and create
94-
// a static library.
95-
fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) -> Result<()> {
96-
let (mbed_compiler, mbed_archiver) =
97-
if std::env::var("TARGET").unwrap() == "aarch64-unknown-linux-gnu" {
98-
let toolchain;
99-
toolchain = mbed_config
100-
.aarch64_unknown_linux_gnu
101-
.as_ref()
102-
.ok_or_else(|| {
103-
Error::new(
104-
ErrorKind::InvalidInput,
105-
"The aarch64_unknown_linux_gnu subtable of mbed_config should exist",
106-
)
107-
})?;
108-
(
109-
toolchain
110-
.mbed_compiler
111-
.clone()
112-
.unwrap_or_else(|| DEFAULT_ARM64_MBED_COMPILER.to_string()),
113-
toolchain
114-
.mbed_archiver
115-
.clone()
116-
.unwrap_or_else(|| DEFAULT_ARM64_MBED_ARCHIVER.to_string()),
117-
)
118-
} else {
119-
let toolchain;
120-
toolchain = mbed_config.native.as_ref().ok_or_else(|| {
121-
Error::new(
122-
ErrorKind::InvalidInput,
123-
"The native subtable of mbed_config should exist",
124-
)
125-
})?;
126-
(
127-
toolchain
128-
.mbed_compiler
129-
.clone()
130-
.unwrap_or_else(|| DEFAULT_NATIVE_MBED_COMPILER.to_string()),
131-
toolchain
132-
.mbed_archiver
133-
.clone()
134-
.unwrap_or_else(|| DEFAULT_NATIVE_MBED_ARCHIVER.to_string()),
135-
)
136-
};
137-
138-
let script_fail = |_| {
139-
Err(Error::new(
140-
ErrorKind::Other,
141-
"setup_mbed_crypto.sh script failed",
142-
))
143-
};
144-
145-
if !::std::process::Command::new(SETUP_MBED_SCRIPT_PATH)
146-
.arg(mbed_version)
147-
.arg(
148-
mbed_config
149-
.mbed_path
150-
.clone()
151-
.unwrap_or_else(|| env::var("OUT_DIR").unwrap()),
152-
)
153-
.arg(format!("CC={}", mbed_compiler))
154-
.arg(format!("AR={}", mbed_archiver))
155-
.status()
156-
.or_else(script_fail)?
157-
.success()
158-
{
159-
Err(Error::new(
160-
ErrorKind::Other,
161-
"setup_mbed_crypto.sh returned an error status.",
162-
))
163-
} else {
164-
Ok(())
165-
}
166-
}
167-
168-
fn generate_mbed_bindings(mbed_config: &MbedConfig, mbed_version: &str) -> Result<()> {
169-
let mbed_include_dir = mbed_config
170-
.mbed_path
171-
.clone()
172-
.unwrap_or_else(|| env::var("OUT_DIR").unwrap())
173-
+ "/mbed-crypto-"
174-
+ mbed_version
175-
+ "/include";
176-
let header = mbed_include_dir.clone() + "/psa/crypto.h";
177-
178-
println!("cargo:rerun-if-changed={}", header);
179-
180-
let bindings = bindgen::Builder::default()
181-
.clang_arg(format!("-I{}", mbed_include_dir))
182-
.rustfmt_bindings(true)
183-
.header(header)
184-
.generate_comments(false)
185-
.generate()
186-
.or_else(|_| {
187-
Err(Error::new(
188-
ErrorKind::Other,
189-
"Unable to generate bindings to mbed crypto",
190-
))
191-
})?;
192-
193-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
194-
bindings.write_to_file(out_path.join("psa_crypto_bindings.rs"))
195-
}
196-
197-
// Get the compiler, the archiver and the location where to clone the Mbed Crypto repository.
198-
fn parse_config_file() -> Result<Configuration> {
199-
let config_str = ::std::fs::read_to_string(Path::new(BUILD_CONFIG_FILE_PATH))?;
200-
Ok(toml::from_str(&config_str).or_else(|e| {
201-
println!("Error parsing build configuration file ({}).", e);
202-
Err(Error::new(
203-
ErrorKind::InvalidInput,
204-
"Could not parse build configuration file.",
205-
))
206-
})?)
207-
}
208-
209-
fn main() -> Result<()> {
210-
// Parsing build-conf.toml
211-
let config = parse_config_file()?;
212-
213-
// Parsing Cargo.toml
214-
let toml_path = std::path::Path::new("./Cargo.toml");
215-
if !toml_path.exists() {
216-
return Err(Error::new(
217-
ErrorKind::InvalidInput,
218-
"Could not find Cargo.toml.",
219-
));
220-
}
221-
let manifest = Manifest::from_path(&toml_path).or_else(|e| {
222-
println!("Error parsing Cargo.toml ({}).", e);
223-
Err(Error::new(
224-
ErrorKind::InvalidInput,
225-
"Could not parse Cargo.toml.",
226-
))
227-
})?;
228-
229-
let package = manifest.package.ok_or_else(|| {
230-
Error::new(
231-
ErrorKind::InvalidInput,
232-
"Cargo.toml does not contain package information.",
233-
)
234-
})?;
235-
let metadata = package.metadata.ok_or_else(|| {
236-
Error::new(
237-
ErrorKind::InvalidInput,
238-
"Cargo.toml does not contain package metadata.",
239-
)
240-
})?;
241-
let parsec_config = get_value_from_table(&metadata, CONFIG_TABLE_NAME)?;
242-
243-
if cfg!(feature = "mbed-crypto-provider") {
244-
let mbed_config = config.mbed_config.ok_or_else(|| {
245-
Error::new(
246-
ErrorKind::InvalidInput,
247-
"Could not find mbed_config table in the config file.",
248-
)
249-
})?;
250-
251-
let mbed_version = get_configuration_string(&parsec_config, MBED_CRYPTO_VERSION_KEY)?;
252-
253-
setup_mbed_crypto(&mbed_config, &mbed_version)?;
254-
generate_mbed_bindings(&mbed_config, &mbed_version)?;
255-
256-
// Request rustc to link the Mbed Crypto static library
257-
println!(
258-
"cargo:rustc-link-search=native={}/mbed-crypto-{}/library/",
259-
mbed_config
260-
.mbed_path
261-
.unwrap_or_else(|| env::var("OUT_DIR").unwrap()),
262-
mbed_version,
263-
);
264-
println!("cargo:rustc-link-lib=static=mbedcrypto");
265-
}
266-
267-
Ok(())
268-
}
3+
fn main() {}

e2e_tests/provider_cfg/all/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM tpm2software/tpm2-tss:ubuntu-18.04
33
ENV PKG_CONFIG_PATH /usr/local/lib/pkgconfig
44

55
RUN apt-get update && \
6-
apt-get install -y git make gcc python3 python curl wget && \
6+
apt-get install -y git make gcc python3 python curl wget cmake && \
77
apt-get install -y automake autoconf libtool pkg-config libssl-dev && \
88
# These libraries are needed for bindgen as it uses libclang.so
99
apt-get install -y clang libclang-dev libc6-dev-i386

e2e_tests/provider_cfg/mbed-crypto/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM ubuntu:18.04
22

33
RUN apt-get update && \
4-
apt-get install -y git make gcc python3 python curl wget libgcc1 && \
4+
apt-get install -y git make gcc python3 python curl wget libgcc1 cmake && \
55
# These libraries are needed for bindgen as it uses libclang.so
66
apt-get install -y clang libclang-dev && \
77
# Needed for Open SSL

e2e_tests/tests/per_provider/normal_tests/create_destroy_key.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
// Copyright 2019 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use e2e_tests::TestClient;
4-
use parsec_client::core::interface::operations::psa_algorithm::{
5-
Algorithm, AsymmetricSignature, Hash,
6-
};
7-
use parsec_client::core::interface::operations::psa_key_attributes::{
8-
Attributes, Lifetime, Policy, Type, UsageFlags,
9-
};
104
use parsec_client::core::interface::requests::ResponseStatus;
115
use parsec_client::core::interface::requests::Result;
126
use picky_asn1::wrapper::IntegerAsn1;
@@ -110,35 +104,13 @@ fn generate_public_rsa_check_modulus() -> Result<()> {
110104
fn failed_created_key_should_be_removed() -> Result<()> {
111105
let mut client = TestClient::new();
112106
let key_name = String::from("failed_created_key_should_be_removed");
107+
const GARBAGE_IMPORT_DATA: [u8; 1] = [
108+
48,
109+
];
113110

114-
let attributes = Attributes {
115-
lifetime: Lifetime::Persistent,
116-
key_type: Type::Arc4,
117-
bits: 1024,
118-
policy: Policy {
119-
usage_flags: UsageFlags {
120-
sign_hash: false,
121-
verify_hash: true,
122-
sign_message: false,
123-
verify_message: true,
124-
export: false,
125-
encrypt: false,
126-
decrypt: false,
127-
cache: false,
128-
copy: false,
129-
derive: false,
130-
},
131-
permitted_algorithms: Algorithm::AsymmetricSignature(
132-
AsymmetricSignature::RsaPkcs1v15Sign {
133-
hash_alg: Hash::Sha256.into(),
134-
},
135-
),
136-
},
137-
};
138-
139-
// Unsupported parameter, should fail
111+
// The data being imported is garbage, should fail
140112
let _ = client
141-
.generate_key(key_name.clone(), attributes)
113+
.import_rsa_public_key(key_name.clone(), GARBAGE_IMPORT_DATA.to_vec())
142114
.unwrap_err();
143115
// The key should not exist anymore in the KIM
144116
client.generate_rsa_sign_key(key_name)?;

0 commit comments

Comments
 (0)