-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move Parsec over to psa-crypto #186
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d426bd7
Fixed PKCS#11 provieder failing failed_created_key_should_be_removed …
sbailey-arm 7b2ca72
Move calls from C library to psa-crypto
sbailey-arm a8847bf
Moved setup and drop calls to psa-crypto and removed all references t…
sbailey-arm 683f52d
Removed unecessary build files
sbailey-arm e1ab2b7
Tweaks to pass CI tests
sbailey-arm b7438a3
Tweaks for mbed_crypto:
sbailey-arm d5b4be4
Update psa_crypto and parsec-interface dependencies
sbailey-arm 186980a
Added cmake to dockerfile for mbed_crypto and all-providers
sbailey-arm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,268 +1,3 @@ | ||
// Copyright 2019 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![deny( | ||
nonstandard_style, | ||
const_err, | ||
dead_code, | ||
improper_ctypes, | ||
non_shorthand_field_patterns, | ||
no_mangle_generic_items, | ||
overflowing_literals, | ||
path_statements, | ||
patterns_in_fns_without_body, | ||
private_in_public, | ||
unconditional_recursion, | ||
unused, | ||
unused_allocation, | ||
unused_comparisons, | ||
unused_parens, | ||
while_true, | ||
missing_debug_implementations, | ||
trivial_casts, | ||
trivial_numeric_casts, | ||
unused_extern_crates, | ||
unused_import_braces, | ||
unused_qualifications, | ||
unused_results, | ||
missing_copy_implementations | ||
)] | ||
// This one is hard to avoid. | ||
#![allow(clippy::multiple_crate_versions)] | ||
|
||
use cargo_toml::{Manifest, Value}; | ||
use serde::Deserialize; | ||
use std::env; | ||
use std::io::{Error, ErrorKind, Result}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
const CONFIG_TABLE_NAME: &str = "config"; | ||
const MBED_CRYPTO_VERSION_KEY: &str = "mbed-crypto-version"; | ||
|
||
const SETUP_MBED_SCRIPT_PATH: &str = "./setup_mbed_crypto.sh"; | ||
const BUILD_CONFIG_FILE_PATH: &str = "./build-conf.toml"; | ||
|
||
const DEFAULT_NATIVE_MBED_COMPILER: &str = "clang"; | ||
const DEFAULT_NATIVE_MBED_ARCHIVER: &str = "ar"; | ||
const DEFAULT_ARM64_MBED_COMPILER: &str = "aarch64-linux-gnu-gcc"; | ||
const DEFAULT_ARM64_MBED_ARCHIVER: &str = "aarch64-linux-gnu-ar"; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Configuration { | ||
mbed_config: Option<MbedConfig>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct MbedConfig { | ||
mbed_path: Option<String>, | ||
native: Option<Toolchain>, | ||
aarch64_unknown_linux_gnu: Option<Toolchain>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Toolchain { | ||
mbed_compiler: Option<String>, | ||
mbed_archiver: Option<String>, | ||
} | ||
|
||
fn get_configuration_string(parsec_config: &Value, key: &str) -> Result<String> { | ||
let config_value = get_value_from_table(parsec_config, key)?; | ||
match config_value { | ||
Value::String(string) => Ok(string.clone()), | ||
_ => Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"Configuration key missing", | ||
)), | ||
} | ||
} | ||
|
||
fn get_value_from_table<'a>(table: &'a Value, key: &str) -> Result<&'a Value> { | ||
match table { | ||
Value::Table(table) => table.get(key).ok_or_else(|| { | ||
println!("Config table does not contain configuration key: {}", key); | ||
Error::new(ErrorKind::InvalidInput, "Configuration key missing.") | ||
}), | ||
_ => Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"Value provided is not a TOML table", | ||
)), | ||
} | ||
} | ||
|
||
// Get the Mbed Crypto version to branch on from Cargo.toml file. Use that and MbedConfig to pass | ||
// parameters to the setup_mbed_crypto.sh script which clones and builds Mbed Crypto and create | ||
// a static library. | ||
fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) -> Result<()> { | ||
let (mbed_compiler, mbed_archiver) = | ||
if std::env::var("TARGET").unwrap() == "aarch64-unknown-linux-gnu" { | ||
let toolchain; | ||
toolchain = mbed_config | ||
.aarch64_unknown_linux_gnu | ||
.as_ref() | ||
.ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
"The aarch64_unknown_linux_gnu subtable of mbed_config should exist", | ||
) | ||
})?; | ||
( | ||
toolchain | ||
.mbed_compiler | ||
.clone() | ||
.unwrap_or_else(|| DEFAULT_ARM64_MBED_COMPILER.to_string()), | ||
toolchain | ||
.mbed_archiver | ||
.clone() | ||
.unwrap_or_else(|| DEFAULT_ARM64_MBED_ARCHIVER.to_string()), | ||
) | ||
} else { | ||
let toolchain; | ||
toolchain = mbed_config.native.as_ref().ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
"The native subtable of mbed_config should exist", | ||
) | ||
})?; | ||
( | ||
toolchain | ||
.mbed_compiler | ||
.clone() | ||
.unwrap_or_else(|| DEFAULT_NATIVE_MBED_COMPILER.to_string()), | ||
toolchain | ||
.mbed_archiver | ||
.clone() | ||
.unwrap_or_else(|| DEFAULT_NATIVE_MBED_ARCHIVER.to_string()), | ||
) | ||
}; | ||
|
||
let script_fail = |_| { | ||
Err(Error::new( | ||
ErrorKind::Other, | ||
"setup_mbed_crypto.sh script failed", | ||
)) | ||
}; | ||
|
||
if !::std::process::Command::new(SETUP_MBED_SCRIPT_PATH) | ||
.arg(mbed_version) | ||
.arg( | ||
mbed_config | ||
.mbed_path | ||
.clone() | ||
.unwrap_or_else(|| env::var("OUT_DIR").unwrap()), | ||
) | ||
.arg(format!("CC={}", mbed_compiler)) | ||
.arg(format!("AR={}", mbed_archiver)) | ||
.status() | ||
.or_else(script_fail)? | ||
.success() | ||
{ | ||
Err(Error::new( | ||
ErrorKind::Other, | ||
"setup_mbed_crypto.sh returned an error status.", | ||
)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn generate_mbed_bindings(mbed_config: &MbedConfig, mbed_version: &str) -> Result<()> { | ||
let mbed_include_dir = mbed_config | ||
.mbed_path | ||
.clone() | ||
.unwrap_or_else(|| env::var("OUT_DIR").unwrap()) | ||
+ "/mbed-crypto-" | ||
+ mbed_version | ||
+ "/include"; | ||
let header = mbed_include_dir.clone() + "/psa/crypto.h"; | ||
|
||
println!("cargo:rerun-if-changed={}", header); | ||
|
||
let bindings = bindgen::Builder::default() | ||
.clang_arg(format!("-I{}", mbed_include_dir)) | ||
.rustfmt_bindings(true) | ||
.header(header) | ||
.generate_comments(false) | ||
.generate() | ||
.or_else(|_| { | ||
Err(Error::new( | ||
ErrorKind::Other, | ||
"Unable to generate bindings to mbed crypto", | ||
)) | ||
})?; | ||
|
||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
bindings.write_to_file(out_path.join("psa_crypto_bindings.rs")) | ||
} | ||
|
||
// Get the compiler, the archiver and the location where to clone the Mbed Crypto repository. | ||
fn parse_config_file() -> Result<Configuration> { | ||
let config_str = ::std::fs::read_to_string(Path::new(BUILD_CONFIG_FILE_PATH))?; | ||
Ok(toml::from_str(&config_str).or_else(|e| { | ||
println!("Error parsing build configuration file ({}).", e); | ||
Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"Could not parse build configuration file.", | ||
)) | ||
})?) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
// Parsing build-conf.toml | ||
let config = parse_config_file()?; | ||
|
||
// Parsing Cargo.toml | ||
let toml_path = std::path::Path::new("./Cargo.toml"); | ||
if !toml_path.exists() { | ||
return Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"Could not find Cargo.toml.", | ||
)); | ||
} | ||
let manifest = Manifest::from_path(&toml_path).or_else(|e| { | ||
println!("Error parsing Cargo.toml ({}).", e); | ||
Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"Could not parse Cargo.toml.", | ||
)) | ||
})?; | ||
|
||
let package = manifest.package.ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
"Cargo.toml does not contain package information.", | ||
) | ||
})?; | ||
let metadata = package.metadata.ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
"Cargo.toml does not contain package metadata.", | ||
) | ||
})?; | ||
let parsec_config = get_value_from_table(&metadata, CONFIG_TABLE_NAME)?; | ||
|
||
if cfg!(feature = "mbed-crypto-provider") { | ||
let mbed_config = config.mbed_config.ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
"Could not find mbed_config table in the config file.", | ||
) | ||
})?; | ||
|
||
let mbed_version = get_configuration_string(&parsec_config, MBED_CRYPTO_VERSION_KEY)?; | ||
|
||
setup_mbed_crypto(&mbed_config, &mbed_version)?; | ||
generate_mbed_bindings(&mbed_config, &mbed_version)?; | ||
|
||
// Request rustc to link the Mbed Crypto static library | ||
println!( | ||
"cargo:rustc-link-search=native={}/mbed-crypto-{}/library/", | ||
mbed_config | ||
.mbed_path | ||
.unwrap_or_else(|| env::var("OUT_DIR").unwrap()), | ||
mbed_version, | ||
); | ||
println!("cargo:rustc-link-lib=static=mbedcrypto"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
fn main() {} | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this file is empty we might as well delete it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason we still have that file is that the
OUT_DIR
environment variable will only exist if that file exist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I did remove it at first but ran into compilation errors. @hug-dev found this.