Skip to content

Commit

Permalink
Current MC-Develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sugargoat committed Jan 13, 2020
1 parent 2087951 commit b9f0afb
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 120 deletions.
16 changes: 16 additions & 0 deletions includegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

ls -f1 $( \
( \
grep '^#include' * | \
grep -v '<' | \
grep -v MBEDTLS_ | \
sed 's/:#include//;s/"//g' | \
grep -v _alt.h; \
ls *.h | \
awk '{print $1 " " $1}' \
) | \
tsort | \
tac | \
egrep -v '^(compat-1.3.h|certs.h|config.h|check_config.h)$' \
)
6 changes: 3 additions & 3 deletions mbedtls-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mbedtls-sys-auto"
version = "2.18.2"
version = "2.18.1"
authors = ["Jethro Beekman <[email protected]>"]
build = "build/build.rs"
license = "Apache-2.0/GPL-2.0+"
Expand All @@ -21,7 +21,7 @@ libc = { version = "0.2.0", optional = true }
libz-sys = { version = "1.0.0", optional = true }

[build-dependencies]
bindgen = "0.19.0"
bindgen = "0.43.0"
cmake = "0.1.17"

[features]
Expand All @@ -42,7 +42,7 @@ aes_alt = []
custom_threading = ["threading"]
pthread = ["libc","threading"]
threading = []
time = []
time = ["libc"]
havege = ["time"]
zlib = ["libz-sys"]
pkcs11 = []
Expand Down
131 changes: 100 additions & 31 deletions mbedtls-sys/build/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,72 @@
* according to those terms. */

use bindgen;
use bindgen::callbacks::IntKind;

use std::env;
use std::fs::File;
use std::io::{stderr, Write};
use std::io::Write;
use std::process::Command;

use crate::headers;

#[derive(Debug)]
struct StderrLogger;
struct ParseCallbacks {}

impl bindgen::Logger for StderrLogger {
fn error(&self, msg: &str) {
let _ = writeln!(stderr(), "Bindgen ERROR: {}", msg);
impl bindgen::callbacks::ParseCallbacks for ParseCallbacks {
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
if name.starts_with("MBEDTLS_SSL_IS_") ||
name.starts_with("MBEDTLS_SSL_PRESET_") ||
name.starts_with("MBEDTLS_SSL_TRANSPORT_") ||
name.starts_with("MBEDTLS_SSL_VERIFY_") ||
name.starts_with("MBEDTLS_TLS_RSA_WITH_") ||
name.starts_with("MBEDTLS_TLS_RSA_PSK_WITH_") ||
name.starts_with("MBEDTLS_TLS_ECJPAKE_WITH_") ||
name.starts_with("MBEDTLS_TLS_DHE_RSA_WITH_") ||
name.starts_with("MBEDTLS_TLS_ECDH_ECDSA_WITH_") ||
name.starts_with("MBEDTLS_TLS_ECDHE_ECDSA_WITH_") ||
name.starts_with("MBEDTLS_TLS_ECDH_RSA_WITH_") ||
name.starts_with("MBEDTLS_TLS_ECDHE_RSA_WITH_") ||
name.starts_with("MBEDTLS_TLS_ECDHE_PSK_WITH_") ||
name.starts_with("MBEDTLS_TLS_PSK_WITH_") ||
name.starts_with("MBEDTLS_TLS_DHE_PSK_WITH_") ||
name.starts_with("MBEDTLS_SSL_SESSION_TICKETS_") ||
name.starts_with("MBEDTLS_CTR_DRBG_PR_") ||
name.starts_with("MBEDTLS_ENTROPY_SOURCE_") ||
name.starts_with("MBEDTLS_HMAC_DRBG_PR_") ||
name.starts_with("MBEDTLS_RSA_PKCS_")
{
Some(IntKind::Int)
} else {
None
}
}
fn warn(&self, msg: &str) {
let _ = writeln!(stderr(), "Bindgen WARNING: {}", msg);

fn item_name(&self, original_item_name: &str) -> Option<String> {
if original_item_name.starts_with("mbedtls_") {
if original_item_name == "mbedtls_time_t" {
None
} else {
Some(original_item_name.trim_start_matches("mbedtls_").to_string())
}
} else if original_item_name.starts_with("MBEDTLS_") {
Some(original_item_name.trim_start_matches("MBEDTLS_").to_string())
} else {
None
}
}

fn enum_variant_name(
&self,
_enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: bindgen::callbacks::EnumVariantValue
) -> Option<String> {
if original_variant_name.starts_with("MBEDTLS_") {
Some(original_variant_name.trim_start_matches("MBEDTLS_").to_string())
} else {
None
}
}
}

Expand All @@ -36,38 +87,56 @@ impl super::BuildConfig {
}).expect("bindgen-input.h I/O error");

let include = self.mbedtls_src.join("include");
let target = env::var("TARGET").expect("TARGET environment variable not set");

let bindings = (match target.as_str() {
/* iOS */
"armv7-apple-ios" | "armv7s-apple-ios" | "aarch64-apple-ios" => {
let ios_sdk_path_output = Command::new("xcrun").args(&["--sdk", "iphoneos", "--show-sdk-path"])
.output().expect("Failed to get xcode iphoneos sdk path");
let ios_sdk_path = String::from_utf8(ios_sdk_path_output.stdout)
.expect("Command output from xcrun not UTF-8")
.trim().to_string();

let logger = StderrLogger;
let mut bindgen = bindgen::Builder::new(header.into_os_string().into_string().unwrap());
let bindings = bindgen
.log(&logger)
(match target.as_str() {
"armv7-apple-ios" | "armv7s-apple-ios" => bindgen::builder()
.clang_args(&["-target", &target]),
"aarch64-apple-ios" => bindgen::builder()
.clang_args(&["-target", "arm64-apple-ios"]),
_ => bindgen::builder()
}).clang_args(&["-isysroot", ios_sdk_path.as_str()])
},
/* Android*/
"i686-linux-andoid" | "armv7-linux-androideabi" | "aarch64-linux-android" => bindgen::builder()
.clang_args(&["-isysroot", &env::var("ISYSROOT").expect("Need ISYSROOT env var for Android compilation")])
.clang_args(&["-isystem", &env::var("ISYSTEM").expect("Need ISYSTEM env var for Android compilation")]),
/* Everything else */
_ => bindgen::builder()
})
.clang_arg("-Dmbedtls_t_udbl=mbedtls_t_udbl;") // bindgen can't handle unused uint128
.clang_arg(format!(
"-DMBEDTLS_CONFIG_FILE=<{}>",
"-DMBEDTLS_CONFIG_FILE=\"{}\"",
self.config_h.to_str().expect("config.h UTF-8 error")
)).clang_arg(format!(
"-I{}",
include.to_str().expect("include/ UTF-8 error")
)).match_pat(include.to_str().expect("include/ UTF-8 error"))
.match_pat(self.config_h.to_str().expect("config.h UTF-8 error"))
.use_core(true)
)).header(
header
.to_str()
.expect("failed to convert header path to string"),
).use_core()
.derive_debug(false) // buggy :(
.ctypes_prefix(vec!["types".to_owned(), "raw_types".to_owned()])
.remove_prefix("mbedtls_")
.rust_enums(false)
.convert_macros(true)
.macro_int_types(
vec![
"sint",
"sint",
"sint",
"slonglong",
"sint",
"sint",
"sint",
"slonglong",
].into_iter(),
).generate()
.disable_name_namespacing()
.prepend_enum_name(false)
.ctypes_prefix("raw_types")
.parse_callbacks(Box::new(ParseCallbacks{}))
// max_align_t is causing bindgen generated tests to fail an alignment check and
// is not needed by the bindings.
.blacklist_type("max_align_t")
// Including the comments breaks the generated code because it contains formatting
// that is interpreted as escaped characters.
.generate_comments(false)
.generate()
.expect("bindgen error");

let bindings_rs = self.out_dir.join("bindings.rs");
Expand Down
39 changes: 38 additions & 1 deletion mbedtls-sys/build/cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,54 @@ impl super::BuildConfig {
pub fn cmake(&self) {
let mut cmk = cmake::Config::new(&self.mbedtls_src);
cmk.cflag(format!(
r#"-DMBEDTLS_CONFIG_FILE="<{}>""#,
"-DMBEDTLS_CONFIG_FILE=\"\\\"{}\\\"\"",
self.config_h.to_str().expect("config.h UTF-8 error")
))
.define("ENABLE_PROGRAMS", "OFF")
.define("ENABLE_TESTING", "OFF")
.build_target("lib");

match ::std::env::var("TARGET").unwrap_or("".to_owned()).as_str() {
"i686-linux-android" => {
cmk.define("TOOLCHAIN_PREFIX", "i686-linux-android")
.target("i686-linux-android26");
}
"armv7-linux-androideabi" => {
cmk.define("TOOLCHAIN_PREFIX", "arm-linux-androideabi")
.target("armv7a-linux-androideabi26");
}
"aarch64-linux-android" => {
cmk.define("TOOLCHAIN_PREFIX", "aarch64-linux-android")
.target("aarch64-linux-android26");
}
_ => {}
};

let target_vendor = ::std::env::var("CARGO_CFG_TARGET_VENDOR")
.expect("CARGO_CFG_TARGET_VENDOR is set by cargo.");

// Workaround for Cmake not setting `-m<platform>-version-min` flags properly for asm files
// See https://gitlab.kitware.com/cmake/cmake/issues/19794
match ::std::env::var("TARGET").unwrap_or("".to_owned()).as_str() {
"aarch64-apple-ios" | "armv7-apple-ios" | "armv7s-apple-ios" => {
cmk.cflag("-miphoneos-version-min=7.0");
}
"i386-apple-ios" | "x86_64-apple-ios" => {
cmk.cflag("-mios-simulator-version-min=7.0");
}
_ => {}
};

if !have_feature("std")
|| ::std::env::var("TARGET")
.map(|s| (s == "x86_64-unknown-none-gnu") || (s == "x86_64-fortanix-unknown-sgx"))
== Ok(true)
{
if target_vendor != "apple" {
println!("cargo:rustc-link-lib=gcc");
}
// println!("cargo:rustc-link-lib=gcc");

cmk.cflag("-fno-builtin")
.cflag("-D_FORTIFY_SOURCE=0")
.cflag("-fno-stack-protector");
Expand Down
6 changes: 3 additions & 3 deletions mbedtls-sys/build/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub const DEFAULT_DEFINES: &'static [CDefine] = &[
("MBEDTLS_DHM_C", Defined),
("MBEDTLS_ECDH_C", Defined),
("MBEDTLS_ECDSA_C", Defined),
("MBEDTLS_ECJPAKE_C", Defined),
("MBEDTLS_ECJPAKE_C", Undefined),
("MBEDTLS_ECP_C", Defined),
("MBEDTLS_ENTROPY_C", Undefined),
("MBEDTLS_ERROR_C", Defined),
Expand All @@ -274,8 +274,8 @@ pub const DEFAULT_DEFINES: &'static [CDefine] = &[
("MBEDTLS_HMAC_DRBG_C", Defined),
("MBEDTLS_NIST_KW_C", Defined),
("MBEDTLS_MD_C", Defined),
("MBEDTLS_MD2_C", Defined),
("MBEDTLS_MD4_C", Defined),
("MBEDTLS_MD2_C", Undefined),
("MBEDTLS_MD4_C", Undefined),
("MBEDTLS_MD5_C", Defined),
("MBEDTLS_MEMORY_BUFFER_ALLOC_C", Undefined),
("MBEDTLS_NET_C", Undefined),
Expand Down
4 changes: 4 additions & 0 deletions mbedtls-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* option. This file may not be copied, modified, or distributed except
* according to those terms. */

#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(clippy::all)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
extern crate core;
Expand Down
5 changes: 5 additions & 0 deletions mbedtls-sys/vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ else()
project("mbed TLS" C)
endif()

if(TOOLCHAIN_PREFIX)
set( CMAKE_AR "${TOOLCHAIN_PREFIX}-ar" )
set( CMAKE_RANLIB "${TOOLCHAIN_PREFIX}-ranlib" )
endif()

option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)

Expand Down
12 changes: 5 additions & 7 deletions mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ keywords = ["MbedTLS","mbed","TLS","SSL","cryptography"]
[dependencies]
bitflags = "1"
chrono = { version = "0.4", optional = true }
core_io = { version = "0.1", features = ["collections"], optional = true }
# MobileCoin: genio
genio = { version = "0.2.0", default-features = false }
spin = { version = "0.4.0", default-features = false, optional = true }
serde = { version = "1.0.7", default-features = false }
serde_derive = "1.0.7"
byteorder = "1.0.0"
byteorder = { version = "1.0.0", default-features = false }
yasna = { version = "0.2", optional = true }
block-modes = { version = "0.3", optional = true }
rc2 = { version = "0.3", optional = true }

[target.x86_64-fortanix-unknown-sgx.dependencies]
rs-libc = "0.1.0"

[dependencies.mbedtls-sys-auto]
version = "2.18.0"
default-features = false
Expand All @@ -40,7 +38,7 @@ path = "../mbedtls-sys"

[dev-dependencies]
libc = "0.2.0"
rand = "0.4.0"
rand = "0.7"
serde_cbor = "0.6"
hex = "0.3"

Expand All @@ -50,7 +48,7 @@ cc = "1.0"
[features]
# Features are documented in the README
default = ["std", "aesni", "time", "padlock", "legacy_protocols", "use_libc"]
std = ["mbedtls-sys-auto/std", "serde/std", "yasna"]
std = ["mbedtls-sys-auto/std", "serde/std", "yasna", "genio/use_std"]
threading = []
pthread = ["threading","std","mbedtls-sys-auto/pthread"]
spin_threading = ["threading","spin","mbedtls-sys-auto/custom_threading"]
Expand Down
3 changes: 2 additions & 1 deletion mbedtls/src/bignum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use crate::error::{Error, IntoResult, Result};
use mbedtls_sys::*;
use mbedtls_sys::types::raw_types::c_char;

#[cfg(not(feature = "std"))]
use crate::alloc_prelude::*;
Expand Down Expand Up @@ -173,7 +174,7 @@ impl Mpi {
mpi_write_string(
&self.inner,
radix,
buf.as_mut_ptr() as *mut i8,
buf.as_mut_ptr() as *mut c_char,
buf.len(),
&mut olen,
)
Expand Down
3 changes: 2 additions & 1 deletion mbedtls/src/cipher/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use mbedtls_sys::*;

use crate::error::{Error, IntoResult, Result};

#[cfg(buggy)]
mod serde;

define!(
Expand Down Expand Up @@ -403,7 +404,7 @@ impl Cipher {
}
self.reset()?;
unsafe {
cipher_cmac(&*self.inner.cipher_info, key.as_ptr(), (key.len() * 8) as _, data.as_ptr(), data.len(),
cipher_cmac(&*self.inner.cipher_info, key.as_ptr(), (key.len() * 8) as _, data.as_ptr(), data.len(),
outdata.as_mut_ptr()).into_result()?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion mbedtls/src/ecp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl EcPoint {
ecp_point_write_binary(
&group.inner,
&self.inner,
format,
format as i32,
&mut olen,
buf.as_mut_ptr(),
buf.len(),
Expand Down
Loading

0 comments on commit b9f0afb

Please sign in to comment.