From 73ae14b4a48ef5fe73242fad1fc1cec54b0bfedf Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Tue, 18 Jul 2023 19:54:55 +0200 Subject: [PATCH 1/8] Only use once_cell dependency when required to fix builds for no-std targets --- mbedtls-platform-support/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mbedtls-platform-support/Cargo.toml b/mbedtls-platform-support/Cargo.toml index 72c2c0cb7..129e93c93 100644 --- a/mbedtls-platform-support/Cargo.toml +++ b/mbedtls-platform-support/Cargo.toml @@ -22,7 +22,7 @@ links = "mbedtls-platform-support" cfg-if = "1.0.0" spin = { version = "0.4.0", default-features = false, optional = true } chrono = { version = "0.4", optional = true } -once_cell = "1.17.1" +once_cell = { version = "1.17.1", optional = true } [target.x86_64-fortanix-unknown-sgx.dependencies] chrono = "0.4" @@ -42,5 +42,5 @@ time = ["mbedtls-sys-auto/time"] std = ["mbedtls-sys-auto/std"] force_aesni_support = ["mbedtls-sys-auto/custom_has_support", "mbedtls-sys-auto/aes_alt", "aesni"] aesni = ["mbedtls-sys-auto/aesni"] -tls13 = ["mbedtls-sys-auto/tls13"] +tls13 = ["mbedtls-sys-auto/tls13", "once_cell"] rdrand = [] From 4e8143e68537e25af7df758fcb57dad785fe62de Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Tue, 18 Jul 2023 20:38:08 +0200 Subject: [PATCH 2/8] For once_cell, use either the std or the critical-section feature --- Cargo.lock | 19 +++++++++++++++++++ mbedtls-platform-support/Cargo.toml | 7 ++++--- mbedtls/Cargo.toml | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6dc9090d..54f7cfe28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,15 @@ dependencies = [ "syn 2.0.16", ] +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +dependencies = [ + "critical-section", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -200,6 +209,12 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" +[[package]] +name = "critical-section" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52" + [[package]] name = "cxx" version = "1.0.80" @@ -741,6 +756,10 @@ name = "once_cell" version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +dependencies = [ + "atomic-polyfill", + "critical-section", +] [[package]] name = "peeking_take_while" diff --git a/mbedtls-platform-support/Cargo.toml b/mbedtls-platform-support/Cargo.toml index 129e93c93..74eb7128b 100644 --- a/mbedtls-platform-support/Cargo.toml +++ b/mbedtls-platform-support/Cargo.toml @@ -22,7 +22,7 @@ links = "mbedtls-platform-support" cfg-if = "1.0.0" spin = { version = "0.4.0", default-features = false, optional = true } chrono = { version = "0.4", optional = true } -once_cell = { version = "1.17.1", optional = true } +once_cell = { version = "1.17.1", default-features = false } [target.x86_64-fortanix-unknown-sgx.dependencies] chrono = "0.4" @@ -39,8 +39,9 @@ cc = "1.0" [features] time = ["mbedtls-sys-auto/time"] -std = ["mbedtls-sys-auto/std"] +std = ["mbedtls-sys-auto/std", "once_cell/std"] +no_std_deps = ["spin", "once_cell/critical-section"] force_aesni_support = ["mbedtls-sys-auto/custom_has_support", "mbedtls-sys-auto/aes_alt", "aesni"] aesni = ["mbedtls-sys-auto/aesni"] -tls13 = ["mbedtls-sys-auto/tls13", "once_cell"] +tls13 = ["mbedtls-sys-auto/tls13"] rdrand = [] diff --git a/mbedtls/Cargo.toml b/mbedtls/Cargo.toml index 9fe306117..635826f59 100644 --- a/mbedtls/Cargo.toml +++ b/mbedtls/Cargo.toml @@ -64,7 +64,7 @@ cc = "1.0" default = ["std", "aesni", "time", "padlock"] std = ["byteorder/std", "mbedtls-sys-auto/std", "serde/std", "yasna", "mbedtls-platform-support/std"] debug = ["mbedtls-sys-auto/debug"] -no_std_deps = ["mbedtls-platform-support/spin", "serde/alloc"] +no_std_deps = ["mbedtls-platform-support/no_std_deps", "serde/alloc"] force_aesni_support = ["mbedtls-platform-support/force_aesni_support", "aesni"] mpi_force_c_code = ["mbedtls-sys-auto/mpi_force_c_code"] rdrand = ["mbedtls-platform-support/rdrand"] From 74eeb22ad22e19e621e1c6d4c9296455d7590efa Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Thu, 20 Jul 2023 13:21:41 +0200 Subject: [PATCH 3/8] Build for thumbv7em-none-eabihf as a no_std target to check that build succeed without std support --- .github/workflows/test.yml | 11 ++++++++++- ct.sh | 13 ++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9024b2c47..51d445427 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,6 +33,9 @@ jobs: - rust: stable target: x86_64-fortanix-unknown-sgx os: ubuntu-20.04 + - rust: stable + target: thumbv7em-none-eabihf + os: ubuntu-20.04 - rust: beta target: x86_64-unknown-linux-gnu os: ubuntu-20.04 @@ -48,12 +51,18 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install dependencies + - name: Install qemu for aarch64-unknown-linux-musl if: matrix.target == 'aarch64-unknown-linux-musl' run: | sudo apt-get update sudo apt-get install -y qemu-user + - name: Install arm-none-eabi-gcc toolchain for thumbv7em-none-eabihf + if: matrix.target == 'thumbv7em-none-eabihf' + run: | + sudo apt-get update + sudo apt-get install -y gcc-arm-none-eabi + - name: Setup Rust toolchain uses: actions-rs/toolchain@v1 with: diff --git a/ct.sh b/ct.sh index bda77d8c8..111bc9f96 100755 --- a/ct.sh +++ b/ct.sh @@ -87,8 +87,14 @@ if [ "$TRAVIS_RUST_VERSION" == "stable" ] || [ "$TRAVIS_RUST_VERSION" == "beta" rustup target add --toolchain $TRAVIS_RUST_VERSION $TARGET printenv - # The SGX target cannot be run under test like a ELF binary - if [ "$TARGET" != "x86_64-fortanix-unknown-sgx" ]; then + if [ "$TARGET" == "x86_64-fortanix-unknown-sgx" ]; then + # The SGX target cannot be run under test like a ELF binary + cargo +$TRAVIS_RUST_VERSION test --no-run --target=$TARGET + cargo +$TRAVIS_RUST_VERSION test --no-default-features --features dsa,force_aesni_support,mpi_force_c_code,rdrand,std,time,tls13 --no-run --target=$TARGET + elif [ "$TARGET" == "thumbv7em-none-eabihf" ]; then + # thumbv7em-none-eabihf is a no_std target, the tests can not be run, we can only check that it builds successfully + cargo build --no-default-features --features no_std_deps --target $TARGET + else # make sure that explicitly providing the default target works cargo nextest run --target $TARGET --release cargo nextest run --features dsa --target $TARGET @@ -107,9 +113,6 @@ if [ "$TRAVIS_RUST_VERSION" == "stable" ] || [ "$TRAVIS_RUST_VERSION" == "beta" if [ "$TARGET" == "x86_64-apple-darwin" ]; then cargo nextest run --no-default-features --features no_std_deps --target $TARGET fi - else - cargo +$TRAVIS_RUST_VERSION test --no-run --target=$TARGET - cargo +$TRAVIS_RUST_VERSION test --no-default-features --features dsa,force_aesni_support,mpi_force_c_code,rdrand,std,time,tls13 --no-run --target=$TARGET fi else From 5b949ee3f3361cf47ca386664666f0361239bba2 Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Thu, 20 Jul 2023 18:18:40 +0200 Subject: [PATCH 4/8] Make threading feature optional and do not include platform_time.h if time feature is no active --- mbedtls-platform-support/Cargo.toml | 2 +- mbedtls-sys/build/headers.rs | 2 +- mbedtls/Cargo.toml | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/mbedtls-platform-support/Cargo.toml b/mbedtls-platform-support/Cargo.toml index 74eb7128b..021f2095f 100644 --- a/mbedtls-platform-support/Cargo.toml +++ b/mbedtls-platform-support/Cargo.toml @@ -31,7 +31,7 @@ zeroize = { version = "1.6.0", default-features = false } [dependencies.mbedtls-sys-auto] version = "3.5.0-alpha.1+0b3de6f" default-features = false -features = ["threading", "custom_printf"] +features = ["custom_printf"] path = "../mbedtls-sys" [build-dependencies] diff --git a/mbedtls-sys/build/headers.rs b/mbedtls-sys/build/headers.rs index dcee4fcc3..0b37f97a1 100644 --- a/mbedtls-sys/build/headers.rs +++ b/mbedtls-sys/build/headers.rs @@ -28,7 +28,7 @@ use crate::features::FEATURES; #[cfg_attr(rustfmt, rustfmt_skip)] pub const ORDERED: &'static [(Option<&'static str>, &'static str)] = &[ (None, "build_info.h"), - (None, "platform_time.h"), + (Some("time"), "platform_time.h"), (None, "private_access.h"), (None, "platform_util.h"), (None, "bignum.h"), diff --git a/mbedtls/Cargo.toml b/mbedtls/Cargo.toml index 635826f59..b51f8355b 100644 --- a/mbedtls/Cargo.toml +++ b/mbedtls/Cargo.toml @@ -34,7 +34,7 @@ rs-libc = "0.2.4" [dependencies.mbedtls-sys-auto] version = "3.5.0-alpha.1+0b3de6f" default-features = false -features = ["trusted_cert_callback", "threading"] +features = ["trusted_cert_callback"] path = "../mbedtls-sys" [dependencies.mbedtls-platform-support] @@ -61,7 +61,7 @@ cc = "1.0" [features] # Features are documented in the README -default = ["std", "aesni", "time", "padlock"] +default = ["std", "aesni", "time", "threading", "padlock"] std = ["byteorder/std", "mbedtls-sys-auto/std", "serde/std", "yasna", "mbedtls-platform-support/std"] debug = ["mbedtls-sys-auto/debug"] no_std_deps = ["mbedtls-platform-support/no_std_deps", "serde/alloc"] @@ -70,6 +70,7 @@ mpi_force_c_code = ["mbedtls-sys-auto/mpi_force_c_code"] rdrand = ["mbedtls-platform-support/rdrand"] aesni = ["mbedtls-platform-support/aesni"] time = ["mbedtls-platform-support/time"] +threading = ["mbedtls-sys-auto/threading"] padlock = ["mbedtls-sys-auto/padlock"] dsa = ["std", "yasna", "num-bigint", "bit-vec"] async = ["std", "tokio", "tokio/net", "tokio/io-util", "tokio/macros"] From 3d9e8d1ac2638d9c01e398793bfc09c0dc85ad0d Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Fri, 21 Jul 2023 11:25:58 +0200 Subject: [PATCH 5/8] Make threading-related error variants dependent on threading feature --- mbedtls/src/error.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mbedtls/src/error.rs b/mbedtls/src/error.rs index 34421bb7e..2cc097a34 100644 --- a/mbedtls/src/error.rs +++ b/mbedtls/src/error.rs @@ -38,21 +38,21 @@ pub const ERR_UTF8_INVALID: c_int = -0x10000; macro_rules! error_enum { { const MASK: c_int = $mask:literal; - enum $n:ident {$($rust:ident = $c:ident,)*} + enum $n:ident {$($(#[$attribute:meta])*$rust:ident = $c:ident,)*} } => { #[non_exhaustive] #[derive(Debug, Eq, PartialEq, Copy, Clone)] pub enum $n { - $($rust,)* + $($(#[$attribute])* $rust,)* Unknown(c_int) } impl From for $n { fn from(code: c_int) -> $n { // check against mask here (not in match blook) to make it compile-time - $(const $c: c_int = $n::assert_in_mask(::mbedtls_sys::$c);)* + $($(#[$attribute])* const $c: c_int = $n::assert_in_mask(::mbedtls_sys::$c);)* match -code { - $($c => return $n::$rust),*, + $($(#[$attribute])* $c => return $n::$rust),*, _ => return $n::Unknown(-code) } } @@ -61,7 +61,7 @@ macro_rules! error_enum { impl From<$n> for c_int { fn from(error: $n) -> c_int { match error { - $($n::$rust => return ::mbedtls_sys::$c,)* + $($(#[$attribute])* $n::$rust => return ::mbedtls_sys::$c,)* $n::Unknown(code) => return code, } } @@ -79,7 +79,7 @@ macro_rules! error_enum { pub fn as_str(&self)-> &'static str { match self { - $($n::$rust => concat!("mbedTLS error ", stringify!($n::$rust)),)* + $($(#[$attribute])* $n::$rust => concat!("mbedTLS error ", stringify!($n::$rust)),)* $n::Unknown(_) => concat!("mbedTLS unknown ", stringify!($n), " error") } } @@ -445,7 +445,9 @@ error_enum!( Sha1BadInputData = ERR_SHA1_BAD_INPUT_DATA, Sha256BadInputData = ERR_SHA256_BAD_INPUT_DATA, Sha512BadInputData = ERR_SHA512_BAD_INPUT_DATA, + #[cfg(feature = "threading")] ThreadingBadInputData = ERR_THREADING_BAD_INPUT_DATA, + #[cfg(feature = "threading")] ThreadingMutexError = ERR_THREADING_MUTEX_ERROR, } ); From bc0f3c4f561c122f844e45e017afd9f616a74c1b Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Fri, 21 Jul 2023 11:31:48 +0200 Subject: [PATCH 6/8] Reorganize ct.sh --- ct.sh | 92 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/ct.sh b/ct.sh index 111bc9f96..23425eab2 100755 --- a/ct.sh +++ b/ct.sh @@ -7,6 +7,21 @@ if [ -z $TRAVIS_RUST_VERSION ]; then exit 1 fi +# Set additional environment variables + +export CFLAGS_x86_64_fortanix_unknown_sgx="-isystem/usr/include/x86_64-linux-gnu -mlvi-hardening -mllvm -x86-experimental-lvi-inline-asm-hardening" +export CC_x86_64_fortanix_unknown_sgx=clang-11 +# For aarch64-unknown-linux-musl, refer to linux-musl-cross toolchain which is downloaded below and use qemu-aarch64 as runner +export CC_aarch64_unknown_linux_musl=/tmp/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc +export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=/tmp/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc +export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER=qemu-aarch64 + + +# Setup dependencies and tools + +# According to `mbedtls-sys/vendor/README.md`, need to install needed pkgs +python3 -m pip install -r ./mbedtls-sys/vendor/scripts/basic.requirements.txt + # checks if a file has a specific sha512 hash, compatible for Linux and macOS check_sha512() { local hash="$1" @@ -26,6 +41,7 @@ check_sha512() { esac } +# Download aarch64-linux-musl-cross toolchain aarch64_cross_toolchain_hash=c8ee0e7fd58f5ec6811e3cec5fcdd8fc47cb2b49fb50e9d7717696ddb69c812547b5f389558f62dfbf9db7d6ad808a5a515cc466b8ea3e9ab3daeb20ba1adf33 # save to directory that will be cached aarch64_cross_toolchain_save_path=/tmp/aarch64-linux-musl-cross.tgz @@ -37,46 +53,42 @@ if [ "$TARGET" == "aarch64-unknown-linux-musl" ]; then tar -xf ${aarch64_cross_toolchain_save_path} -C /tmp; fi -export CFLAGS_x86_64_fortanix_unknown_sgx="-isystem/usr/include/x86_64-linux-gnu -mlvi-hardening -mllvm -x86-experimental-lvi-inline-asm-hardening" -export CC_x86_64_fortanix_unknown_sgx=clang-11 -export CC_aarch64_unknown_linux_musl=/tmp/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc -export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=/tmp/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc -export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER=qemu-aarch64 - -# Setup dependencies and tools - -# According to `mbedtls-sys/vendor/README.md`, need to install needed pkgs -python3 -m pip install -r ./mbedtls-sys/vendor/scripts/basic.requirements.txt - -# function for downloading pre-built `cargo-nextest` on various platforms -download_cargo_nextest() { - local platform="$1" - local cargo_nextest_hash="$2" - local url="$3" - echo "Check if need to download pre-built $platform 'cargo-nextest'" - if ! check_sha512 "${cargo_nextest_hash}" "${CARGO_HOME:-$HOME/.cargo}/bin/cargo-nextest"; then - curl -LsSf "$url" | tar zxf - -C "${CARGO_HOME:-$HOME/.cargo}/bin" - check_sha512 "${cargo_nextest_hash}" "${CARGO_HOME:-$HOME/.cargo}/bin/cargo-nextest" - fi -} -# download pre-built `cargo-nextest` -kernel=$(uname) -architecture=$(uname -m) -case "$kernel-$architecture" in - Linux-x86_64 | Linux-amd64) - download_cargo_nextest "amd64" "d22ce5799f3056807fd0cd8223a290c7153a5f084d5ab931fce755c2cabd33f79c0f75542eb724fe07a7ca083f415ec1f84edc46584b06df43d97a0ff91018da" "https://get.nexte.st/0.9.52/linux" - ;; - Linux-arm64) - download_cargo_nextest "arm64" "cff3297c84560de8693e7f887fcf6cf33ab0036e27a9debf2b0a0832094555335f34dc30d0f9d1128ce8472dcb4594a3cf33be2357b19dcc94269b58090cc1a9" "https://get.nexte.st/0.9.52/linux-arm" - ;; - Darwin-x86_64) - download_cargo_nextest "Darwin-amd64" "0bb8b77ce019de3d06ee6b7382d830ed67309f187781e0de3866a0635879b494c7db48d55eee7553cfaa0bfca59abd8f8540a6d81ed703f06f9c81514d20073d" "https://get.nexte.st/0.9.52/mac" - ;; - *) - echo "Unknown platform '$kernel-$architecture'" - exit 1 - ;; -esac +# Download cargo-nextest +if [ "$TARGET" == "x86_64-fortanix-unknown-sgx" ] || [ "$TARGET" == "thumbv7em-none-eabihf" ]; then + # The x86_64-fortanix-unknown-sgx target is not tested with nextest, for thumbv7em-none-eabihf + # we just build without running anything. + true +else + # function for downloading pre-built `cargo-nextest` on various platforms + download_cargo_nextest() { + local platform="$1" + local cargo_nextest_hash="$2" + local url="$3" + echo "Check if need to download pre-built $platform 'cargo-nextest'" + if ! check_sha512 "${cargo_nextest_hash}" "${CARGO_HOME:-$HOME/.cargo}/bin/cargo-nextest"; then + curl -LsSf "$url" | tar zxf - -C "${CARGO_HOME:-$HOME/.cargo}/bin" + check_sha512 "${cargo_nextest_hash}" "${CARGO_HOME:-$HOME/.cargo}/bin/cargo-nextest" + fi + } + # download pre-built `cargo-nextest` + kernel=$(uname) + architecture=$(uname -m) + case "$kernel-$architecture" in + Linux-x86_64 | Linux-amd64) + download_cargo_nextest "amd64" "d22ce5799f3056807fd0cd8223a290c7153a5f084d5ab931fce755c2cabd33f79c0f75542eb724fe07a7ca083f415ec1f84edc46584b06df43d97a0ff91018da" "https://get.nexte.st/0.9.52/linux" + ;; + Linux-arm64) + download_cargo_nextest "arm64" "cff3297c84560de8693e7f887fcf6cf33ab0036e27a9debf2b0a0832094555335f34dc30d0f9d1128ce8472dcb4594a3cf33be2357b19dcc94269b58090cc1a9" "https://get.nexte.st/0.9.52/linux-arm" + ;; + Darwin-x86_64) + download_cargo_nextest "Darwin-amd64" "0bb8b77ce019de3d06ee6b7382d830ed67309f187781e0de3866a0635879b494c7db48d55eee7553cfaa0bfca59abd8f8540a6d81ed703f06f9c81514d20073d" "https://get.nexte.st/0.9.52/mac" + ;; + *) + echo "Unknown platform '$kernel-$architecture'" + exit 1 + ;; + esac +fi # Test logic start from here From 0dff25c171ba332aa45cc1521fa53fd4ec55672a Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Fri, 21 Jul 2023 11:45:49 +0200 Subject: [PATCH 7/8] Check if tests succeed without thumbv7em-none-eabihf target --- ct.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ct.sh b/ct.sh index 23425eab2..4226e70e7 100755 --- a/ct.sh +++ b/ct.sh @@ -105,7 +105,8 @@ if [ "$TRAVIS_RUST_VERSION" == "stable" ] || [ "$TRAVIS_RUST_VERSION" == "beta" cargo +$TRAVIS_RUST_VERSION test --no-default-features --features dsa,force_aesni_support,mpi_force_c_code,rdrand,std,time,tls13 --no-run --target=$TARGET elif [ "$TARGET" == "thumbv7em-none-eabihf" ]; then # thumbv7em-none-eabihf is a no_std target, the tests can not be run, we can only check that it builds successfully - cargo build --no-default-features --features no_std_deps --target $TARGET + #cargo build --no-default-features --features no_std_deps --target $TARGET + true else # make sure that explicitly providing the default target works cargo nextest run --target $TARGET --release From 0c0c63353d6860b51b71530d7cd11c0a1d03006f Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Fri, 21 Jul 2023 13:09:39 +0200 Subject: [PATCH 8/8] Rename alloc_prelude to no_std_prelude and remove types which are not part of std::prelude --- mbedtls/src/bignum/mod.rs | 2 +- mbedtls/src/cipher/raw/mod.rs | 2 +- mbedtls/src/cipher/raw/serde.rs | 2 +- mbedtls/src/ecp/mod.rs | 2 +- mbedtls/src/lib.rs | 7 ++++--- mbedtls/src/pk/mod.rs | 2 +- mbedtls/src/pk/rfc6979.rs | 2 +- mbedtls/src/private.rs | 2 +- mbedtls/src/rng/ctr_drbg.rs | 4 +++- mbedtls/src/rng/hmac_drbg.rs | 5 ++++- mbedtls/src/ssl/ciphersuites.rs | 2 +- mbedtls/src/ssl/config.rs | 5 ++++- mbedtls/src/ssl/context.rs | 5 ++++- mbedtls/src/ssl/cookie.rs | 9 +++++++-- mbedtls/src/ssl/ticket.rs | 7 ++++++- mbedtls/src/x509/certificate.rs | 2 +- mbedtls/src/x509/csr.rs | 2 +- mbedtls/src/x509/mod.rs | 2 +- 18 files changed, 43 insertions(+), 21 deletions(-) diff --git a/mbedtls/src/bignum/mod.rs b/mbedtls/src/bignum/mod.rs index 6ac172bdf..2e0dd4d55 100644 --- a/mbedtls/src/bignum/mod.rs +++ b/mbedtls/src/bignum/mod.rs @@ -13,7 +13,7 @@ use crate::error::{IntoResult, Result, codes}; use mbedtls_sys::*; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use core::cmp::Ordering; use core::fmt::{Binary, Debug, Display, Formatter, Octal, Result as FmtResult, UpperHex}; diff --git a/mbedtls/src/cipher/raw/mod.rs b/mbedtls/src/cipher/raw/mod.rs index 729469677..1ac23263c 100644 --- a/mbedtls/src/cipher/raw/mod.rs +++ b/mbedtls/src/cipher/raw/mod.rs @@ -13,7 +13,7 @@ use mbedtls_sys::*; use crate::error::{IntoResult, Result, codes}; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; mod serde; diff --git a/mbedtls/src/cipher/raw/serde.rs b/mbedtls/src/cipher/raw/serde.rs index 7b51c5fa9..ea695ab59 100644 --- a/mbedtls/src/cipher/raw/serde.rs +++ b/mbedtls/src/cipher/raw/serde.rs @@ -7,7 +7,7 @@ * according to those terms. */ #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use crate::cipher::*; use core::convert::TryInto; use core::fmt; diff --git a/mbedtls/src/ecp/mod.rs b/mbedtls/src/ecp/mod.rs index 19185948f..3121b2aa2 100644 --- a/mbedtls/src/ecp/mod.rs +++ b/mbedtls/src/ecp/mod.rs @@ -11,7 +11,7 @@ use crate::error::{Error, IntoResult, Result, codes}; use mbedtls_sys::*; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use crate::bignum::Mpi; use crate::pk::EcGroupId; diff --git a/mbedtls/src/lib.rs b/mbedtls/src/lib.rs index 1461eb71c..cb5fc07ea 100644 --- a/mbedtls/src/lib.rs +++ b/mbedtls/src/lib.rs @@ -94,15 +94,16 @@ pub use mbedtls_platform_support::psa_crypto_init; extern crate alloc as rust_alloc; #[cfg(not(feature = "std"))] -mod alloc_prelude { +/// Contains types which are normally part of std::prelude +/// (see https://doc.rust-lang.org/std/prelude/index.html) so they can easiliy be used in no_std +/// builds with `use no_std_prelude::*` +mod no_std_prelude { #![allow(unused)] pub(crate) use rust_alloc::borrow::ToOwned; pub(crate) use rust_alloc::boxed::Box; - pub(crate) use rust_alloc::sync::Arc; pub(crate) use rust_alloc::string::String; pub(crate) use rust_alloc::string::ToString; pub(crate) use rust_alloc::vec::Vec; - pub(crate) use rust_alloc::borrow::Cow; } cfg_if::cfg_if! { diff --git a/mbedtls/src/pk/mod.rs b/mbedtls/src/pk/mod.rs index a47484183..8cb27eec1 100644 --- a/mbedtls/src/pk/mod.rs +++ b/mbedtls/src/pk/mod.rs @@ -7,7 +7,7 @@ * according to those terms. */ #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use mbedtls_sys::*; use core::ptr; diff --git a/mbedtls/src/pk/rfc6979.rs b/mbedtls/src/pk/rfc6979.rs index 01624d60b..50a5a3b3e 100644 --- a/mbedtls/src/pk/rfc6979.rs +++ b/mbedtls/src/pk/rfc6979.rs @@ -7,7 +7,7 @@ * according to those terms. */ #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use mbedtls_sys::types::raw_types::{c_int, c_uchar, c_void}; use mbedtls_sys::types::size_t; diff --git a/mbedtls/src/private.rs b/mbedtls/src/private.rs index b2f573dc5..8605bac12 100644 --- a/mbedtls/src/private.rs +++ b/mbedtls/src/private.rs @@ -7,7 +7,7 @@ * according to those terms. */ #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use mbedtls_sys::types::raw_types::c_char; use mbedtls_sys::types::raw_types::{c_int, c_uchar}; diff --git a/mbedtls/src/rng/ctr_drbg.rs b/mbedtls/src/rng/ctr_drbg.rs index d05dad539..a9295aeb8 100644 --- a/mbedtls/src/rng/ctr_drbg.rs +++ b/mbedtls/src/rng/ctr_drbg.rs @@ -8,6 +8,8 @@ #[cfg(feature = "std")] use std::sync::Arc; +#[cfg(not(feature = "std"))] +use rust_alloc::sync::Arc; pub use mbedtls_sys::CTR_DRBG_RESEED_INTERVAL as RESEED_INTERVAL; use mbedtls_sys::*; @@ -15,7 +17,7 @@ use mbedtls_sys::types::raw_types::{c_int, c_uchar, c_void}; use mbedtls_sys::types::size_t; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use crate::error::{IntoResult, Result}; use crate::rng::{EntropyCallback, EntropyCallbackMut, RngCallback, RngCallbackMut}; diff --git a/mbedtls/src/rng/hmac_drbg.rs b/mbedtls/src/rng/hmac_drbg.rs index 2caa3c98c..a79cbc6f9 100644 --- a/mbedtls/src/rng/hmac_drbg.rs +++ b/mbedtls/src/rng/hmac_drbg.rs @@ -9,6 +9,8 @@ #[cfg(feature = "std")] use std::sync::Arc; +#[cfg(not(feature = "std"))] +use rust_alloc::sync::Arc; pub use mbedtls_sys::HMAC_DRBG_RESEED_INTERVAL as RESEED_INTERVAL; use mbedtls_sys::*; @@ -16,7 +18,8 @@ use mbedtls_sys::types::raw_types::{c_int, c_uchar, c_void}; use mbedtls_sys::types::size_t; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +#[allow(unused)] +use crate::no_std_prelude::*; use crate::error::{IntoResult, Result}; use crate::hash::MdInfo; use crate::rng::{EntropyCallback, RngCallback, RngCallbackMut}; diff --git a/mbedtls/src/ssl/ciphersuites.rs b/mbedtls/src/ssl/ciphersuites.rs index a87a551fb..f517fc151 100644 --- a/mbedtls/src/ssl/ciphersuites.rs +++ b/mbedtls/src/ssl/ciphersuites.rs @@ -271,7 +271,7 @@ define!( ); #[cfg(all(not(feature = "std"), feature = "tls13"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; #[cfg(feature = "tls13")] pub fn tls13_preset_default_sig_algs() -> Vec { diff --git a/mbedtls/src/ssl/config.rs b/mbedtls/src/ssl/config.rs index 3adc7f0d4..489325541 100644 --- a/mbedtls/src/ssl/config.rs +++ b/mbedtls/src/ssl/config.rs @@ -11,6 +11,9 @@ use std::sync::Arc; #[cfg(feature = "std")] use std::borrow::Cow; +#[cfg(not(feature = "std"))] +use rust_alloc::sync::Arc; + use core::slice::from_raw_parts; use mbedtls_sys::*; @@ -20,7 +23,7 @@ use mbedtls_sys::types::size_t; use crate::alloc::List as MbedtlsList; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use crate::error::{Result, IntoResult}; use crate::pk::Pk; use crate::pk::dhparam::Dhm; diff --git a/mbedtls/src/ssl/context.rs b/mbedtls/src/ssl/context.rs index 3282b714b..c15dfc57d 100644 --- a/mbedtls/src/ssl/context.rs +++ b/mbedtls/src/ssl/context.rs @@ -11,11 +11,14 @@ use core::result::Result as StdResult; #[cfg(feature = "std")] use std::sync::Arc; +#[cfg(not(feature = "std"))] +use rust_alloc::sync::Arc; + use mbedtls_sys::types::raw_types::{c_int, c_void}; use mbedtls_sys::*; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use crate::alloc::List as MbedtlsList; use crate::error::{Result, IntoResult, codes}; use crate::pk::Pk; diff --git a/mbedtls/src/ssl/cookie.rs b/mbedtls/src/ssl/cookie.rs index 17a50ca9b..958a020a3 100644 --- a/mbedtls/src/ssl/cookie.rs +++ b/mbedtls/src/ssl/cookie.rs @@ -1,8 +1,13 @@ -#[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; #[cfg(feature = "std")] use std::sync::Arc; +#[cfg(not(feature = "std"))] +use rust_alloc::sync::Arc; + +#[cfg(not(feature = "std"))] +#[allow(unused)] +use crate::no_std_prelude::*; + use mbedtls_sys::types::raw_types::*; use mbedtls_sys::types::size_t; use mbedtls_sys::*; diff --git a/mbedtls/src/ssl/ticket.rs b/mbedtls/src/ssl/ticket.rs index 438050765..321ab750d 100644 --- a/mbedtls/src/ssl/ticket.rs +++ b/mbedtls/src/ssl/ticket.rs @@ -9,12 +9,17 @@ #[cfg(feature = "std")] use std::sync::Arc; +#[cfg(not(feature = "std"))] +use rust_alloc::sync::Arc; + use mbedtls_sys::*; use mbedtls_sys::types::raw_types::{c_int, c_uchar, c_void}; use mbedtls_sys::types::size_t; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +#[allow(unused)] +use crate::no_std_prelude::*; + use crate::cipher::raw::CipherType; use crate::error::{IntoResult, Result}; use crate::rng::RngCallback; diff --git a/mbedtls/src/x509/certificate.rs b/mbedtls/src/x509/certificate.rs index a3564ebfe..9aba67e2a 100644 --- a/mbedtls/src/x509/certificate.rs +++ b/mbedtls/src/x509/certificate.rs @@ -15,7 +15,7 @@ use mbedtls_sys::types::raw_types::{c_char, c_void}; use crate::alloc::{List as MbedtlsList, Box as MbedtlsBox, mbedtls_calloc}; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use crate::error::{IntoResult, Result, codes}; use crate::hash::Type as MdType; use crate::pk::Pk; diff --git a/mbedtls/src/x509/csr.rs b/mbedtls/src/x509/csr.rs index b48d66eb0..89c5d7d75 100644 --- a/mbedtls/src/x509/csr.rs +++ b/mbedtls/src/x509/csr.rs @@ -9,7 +9,7 @@ use core::fmt; #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; use mbedtls_sys::*; diff --git a/mbedtls/src/x509/mod.rs b/mbedtls/src/x509/mod.rs index fcb12763c..5580a21f9 100644 --- a/mbedtls/src/x509/mod.rs +++ b/mbedtls/src/x509/mod.rs @@ -7,7 +7,7 @@ * according to those terms. */ #[cfg(not(feature = "std"))] -use crate::alloc_prelude::*; +use crate::no_std_prelude::*; pub mod certificate; mod crl;