Skip to content

Commit 6ce6ac8

Browse files
authored
Merge pull request #206 from ionut-arm/cross
Add cross-compilation example to nightly
2 parents cf0e60e + 477274d commit 6ce6ac8

File tree

9 files changed

+35471
-51
lines changed

9 files changed

+35471
-51
lines changed

.github/workflows/nightly.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ jobs:
4141
run: cargo audit
4242

4343
coverage:
44-
name: Calculate code coverage
44+
name: Calculate code coverage and cross compile
4545
runs-on: ubuntu-latest
4646
steps:
4747
- uses: actions/checkout@v2
4848
- name: Build the container
4949
run: docker build -t ubuntucontainer tss-esapi/tests/ --file tss-esapi/tests/Dockerfile-ubuntu
50-
- name: Run the container
50+
- name: Run the code coverage script
5151
run: docker run -v $(pwd):/tmp/rust-tss-esapi -w /tmp/rust-tss-esapi/tss-esapi --security-opt seccomp=unconfined ubuntucontainer /tmp/rust-tss-esapi/tss-esapi/tests/coverage.sh
5252
- name: Collect coverage results
5353
run: bash <(curl -s https://codecov.io/bash)
54+
- name: Run the cross-compilation script
55+
run: docker run -v $(pwd):/tmp/rust-tss-esapi -w /tmp/rust-tss-esapi/tss-esapi ubuntucontainer /tmp/rust-tss-esapi/tss-esapi/tests/cross-compile.sh

tss-esapi-sys/README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,22 @@ NOTE: Only a limited set of bindings are committed and their target triplet
3838
is included in the name of the file - if the triplet you require is not
3939
available, feel free to raise a Pull Request to add it or to use build-time
4040
generation of bindings. All the committed bindings **MUST** be generated from
41-
the library version found under the `vendor` submodule.
41+
the library version found under the `vendor` submodule.
42+
43+
## Cross compiling
44+
45+
Cross-compilation can be done as long as you have on your build system the TSS
46+
libraries compiled for your target system of choice. We rely on `pkg-config` to
47+
identify the libraries which we link against. Installing `tpm2-tss` does yield
48+
`.pc` files which can be used for this purpose, but depending on the exact build
49+
environment setup, the configuration and compilation of `tpm2-tss` could require
50+
some special tailoring.
51+
52+
We include cross-compilation builds as a nightly check in Github Actions - you
53+
can find them [here](../tss-esapi/tests/cross-compile.sh) as an example of the
54+
steps needed. You can find more information on using `pkg-config` when cross-compiling
55+
[here](https://github.com/parallaxsecond/rust-tss-esapi/issues/204). Our wrapper
56+
script around `pkg-config` can be seen [here](../tss-esapi/tests/pkg-config).
57+
58+
Be advised that in some cases the linker used might need to be set manually in
59+
`.cargo/config`.

tss-esapi-sys/src/bindings/aarch64-unknown-linux-gnu.rs

+17,666
Large diffs are not rendered by default.

tss-esapi-sys/src/bindings/armv7-unknown-linux-gnueabi.rs

+17,664
Large diffs are not rendered by default.

tss-esapi-sys/src/bindings/x86_64-unknown-linux-gnu.rs

+3-47
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,6 @@ pub const TSS2_BASE_RC_BAD_TR: u32 = 24;
112112
pub const TSS2_BASE_RC_MULTIPLE_DECRYPT_SESSIONS: u32 = 25;
113113
pub const TSS2_BASE_RC_MULTIPLE_ENCRYPT_SESSIONS: u32 = 26;
114114
pub const TSS2_BASE_RC_RSP_AUTH_FAILED: u32 = 27;
115-
pub const TSS2_BASE_RC_NO_CONFIG: u32 = 28;
116-
pub const TSS2_BASE_RC_BAD_PATH: u32 = 29;
117-
pub const TSS2_BASE_RC_NOT_DELETABLE: u32 = 30;
118-
pub const TSS2_BASE_RC_PATH_ALREADY_EXISTS: u32 = 31;
119-
pub const TSS2_BASE_RC_KEY_NOT_FOUND: u32 = 32;
120-
pub const TSS2_BASE_RC_SIGNATURE_VERIFICATION_FAILED: u32 = 33;
121-
pub const TSS2_BASE_RC_HASH_MISMATCH: u32 = 34;
122-
pub const TSS2_BASE_RC_KEY_NOT_DUPLICABLE: u32 = 35;
123-
pub const TSS2_BASE_RC_PATH_NOT_FOUND: u32 = 36;
124-
pub const TSS2_BASE_RC_NO_CERT: u32 = 37;
125-
pub const TSS2_BASE_RC_NO_PCR: u32 = 38;
126-
pub const TSS2_BASE_RC_PCR_NOT_RESETTABLE: u32 = 39;
127-
pub const TSS2_BASE_RC_BAD_TEMPLATE: u32 = 40;
128-
pub const TSS2_BASE_RC_AUTHORIZATION_FAILED: u32 = 41;
129-
pub const TSS2_BASE_RC_AUTHORIZATION_UNKNOWN: u32 = 42;
130-
pub const TSS2_BASE_RC_NV_NOT_READABLE: u32 = 43;
131-
pub const TSS2_BASE_RC_NV_TOO_SMALL: u32 = 44;
132-
pub const TSS2_BASE_RC_NV_NOT_WRITEABLE: u32 = 45;
133-
pub const TSS2_BASE_RC_POLICY_UNKNOWN: u32 = 46;
134-
pub const TSS2_BASE_RC_NV_WRONG_TYPE: u32 = 47;
135-
pub const TSS2_BASE_RC_NAME_ALREADY_EXISTS: u32 = 48;
136-
pub const TSS2_BASE_RC_NO_TPM: u32 = 49;
137-
pub const TSS2_BASE_RC_BAD_KEY: u32 = 50;
138-
pub const TSS2_BASE_RC_NO_HANDLE: u32 = 51;
139115
pub const TSS2_LAYER_IMPLEMENTATION_SPECIFIC_OFFSET: u32 = 63488;
140116
pub const TSS2_LEVEL_IMPLEMENTATION_SPECIFIC_SHIFT: u32 = 11;
141117
pub const TPM2_MAX_COMMAND_SIZE: u32 = 4096;
@@ -442,7 +418,7 @@ pub const _SYS_SELECT_H: u32 = 1;
442418
pub const __FD_ZERO_STOS: &'static [u8; 6usize] = b"stosq\0";
443419
pub const __sigset_t_defined: u32 = 1;
444420
pub const __timeval_defined: u32 = 1;
445-
pub const __timespec_defined: u32 = 1;
421+
pub const _STRUCT_TIMESPEC: u32 = 1;
446422
pub const FD_SETSIZE: u32 = 1024;
447423
pub const _SYS_SYSMACROS_H: u32 = 1;
448424
pub const _BITS_SYSMACROS_H: u32 = 1;
@@ -3306,13 +3282,13 @@ impl Default for TPMS_AUTH_RESPONSE {
33063282
}
33073283
pub type TPMI_AES_KEY_BITS = TPM2_KEY_BITS;
33083284
pub type TPMI_SM4_KEY_BITS = TPM2_KEY_BITS;
3309-
pub type TPMI_CAMELLIA_KEY_BITS = TPM2_KEY_BITS;
3285+
pub type TPMI_TPM2_CAMELLIA_KEY_BITS = TPM2_KEY_BITS;
33103286
#[repr(C)]
33113287
#[derive(Copy, Clone)]
33123288
pub union TPMU_SYM_KEY_BITS {
33133289
pub aes: TPMI_AES_KEY_BITS,
33143290
pub sm4: TPMI_SM4_KEY_BITS,
3315-
pub camellia: TPMI_CAMELLIA_KEY_BITS,
3291+
pub camellia: TPMI_TPM2_CAMELLIA_KEY_BITS,
33163292
pub sym: TPM2_KEY_BITS,
33173293
pub exclusiveOr: TPMI_ALG_HASH,
33183294
_bindgen_union_align: u16,
@@ -10262,20 +10238,6 @@ extern "C" {
1026210238
nonceTPM: *mut *mut TPM2B_NONCE,
1026310239
) -> TSS2_RC;
1026410240
}
10265-
extern "C" {
10266-
pub fn Esys_TR_GetTpmHandle(
10267-
esys_context: *mut ESYS_CONTEXT,
10268-
esys_handle: ESYS_TR,
10269-
tpm_handle: *mut TPM2_HANDLE,
10270-
) -> TSS2_RC;
10271-
}
10272-
extern "C" {
10273-
pub fn Esys_TRSess_GetAuthRequired(
10274-
esys_context: *mut ESYS_CONTEXT,
10275-
esys_handle: ESYS_TR,
10276-
auth_needed: *mut TPMI_YES_NO,
10277-
) -> TSS2_RC;
10278-
}
1027910241
extern "C" {
1028010242
pub fn Esys_Startup(esysContext: *mut ESYS_CONTEXT, startupType: TPM2_SU) -> TSS2_RC;
1028110243
}
@@ -13321,12 +13283,6 @@ extern "C" {
1332113283
extern "C" {
1332213284
pub fn Esys_Free(__ptr: *mut ::std::os::raw::c_void);
1332313285
}
13324-
extern "C" {
13325-
pub fn Esys_GetSysContext(
13326-
esys_context: *mut ESYS_CONTEXT,
13327-
sys_context: *mut *mut TSS2_SYS_CONTEXT,
13328-
) -> TSS2_RC;
13329-
}
1333013286
pub type __gwchar_t = ::std::os::raw::c_int;
1333113287
#[repr(C)]
1333213288
#[derive(Debug, Default, Copy, Clone)]

tss-esapi-sys/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ include!(concat!(
2727
"/src/bindings/x86_64-unknown-linux-gnu.rs"
2828
));
2929

30+
#[cfg(all(
31+
not(feature = "generate-bindings"),
32+
target_arch = "aarch64",
33+
target_os = "linux"
34+
))]
35+
include!(concat!(
36+
env!("CARGO_MANIFEST_DIR"),
37+
"/src/bindings/aarch64-unknown-linux-gnu.rs"
38+
));
39+
40+
#[cfg(all(
41+
not(feature = "generate-bindings"),
42+
target_arch = "armv7",
43+
target_os = "linux"
44+
))]
45+
include!(concat!(
46+
env!("CARGO_MANIFEST_DIR"),
47+
"/src/bindings/armv7-unknown-linux-gnueabi.rs"
48+
));
49+
3050
// If the "generate-bindings" feature is on, use the generated bindings.
3151
#[cfg(feature = "generate-bindings")]
3252
include!(concat!(env!("OUT_DIR"), "/tss_esapi_bindings.rs"));

tss-esapi/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ Our end-goal is to achieve a fully Rust-native interface that offers strong safe
2424

2525
## Versioning
2626

27-
The `tss-esapi` crate is still under development and thus the interface is not stable (despite the version number). As a rule of thumb, all versions marked `alpha` are expected to be short-lived and superseded by a better, more complete interface that relies on breaking changes.
27+
The `tss-esapi` crate is still under development and thus the interface is not stable (despite the version number). As a rule of thumb, all versions marked `alpha` are expected to be short-lived and superseded by a better, more complete interface that relies on breaking changes.
28+
29+
## Cross compiling
30+
31+
For more information on cross-compiling the `tss-esapi` crate, please see the README of the `tss-esapi-sys` crate.

tss-esapi/tests/cross-compile.sh

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 Contributors to the Parsec project.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
# Cross compile the `tss-esapi` crate (and its dependencies) for Armv7 and Aarch64
7+
# In order to cross-compile the TSS library we need to also cross-compile OpenSSL
8+
9+
set -euf -o pipefail
10+
11+
OPENSSL_VERSION="OpenSSL_1_1_1j"
12+
13+
cross-compile-openssl() {
14+
# Prepare directory for cross-compiled OpenSSL files
15+
mkdir -p /tmp/openssl-$1
16+
export INSTALL_DIR=/tmp/openssl-$1
17+
18+
pushd /tmp/openssl
19+
# Compile and copy files over
20+
./Configure $2 shared --prefix=$INSTALL_DIR --openssldir=$INSTALL_DIR/openssl --cross-compile-prefix=$1-
21+
make clean
22+
make depend
23+
make -j$(nproc)
24+
make install
25+
popd
26+
27+
export INSTALL_DIR=
28+
}
29+
30+
cross-compile-tpm2-tss() {
31+
# Prepare directory for cross-compiled TSS lib
32+
# `DESTDIR` is used in `make install` below to set the root of the installation paths.
33+
# The `./configure` script accepts a `--prefix` input variable which sets the same root,
34+
# but also adds it to the paths in `.pc` files used by `pkg-config`. This prevents the
35+
# use of `PKG_CONFIG_SYSROOT_DIR`.
36+
mkdir -p /tmp/tpm2-tss-$1
37+
export DESTDIR=/tmp/tpm2-tss-$1
38+
# Set sysroot to be used by the `pkg-config` wrapper
39+
export SYSROOT=/tmp/tpm2-tss-$1
40+
41+
pushd /tpm2-tss
42+
# Compile and copy files over
43+
./configure --build=x86_64-pc-linux-gnu --host=$1 --target=$1 CC=$1-gcc \
44+
LIBCRYPTO_CFLAGS="-I/tmp/openssl-$1/include" LIBCRYPTO_LIBS="-L/tmp/openssl-$1/lib -lcrypto"
45+
make clean
46+
make -j$(nproc)
47+
make install
48+
popd
49+
50+
export DESTDIR=
51+
}
52+
53+
# Download cross-compilers
54+
apt update
55+
apt install -y gcc-multilib
56+
apt install -y gcc-arm-linux-gnueabi
57+
apt install -y gcc-aarch64-linux-gnu
58+
59+
# Download OpenSSL source code
60+
pushd /tmp
61+
git clone https://github.com/openssl/openssl.git --branch $OPENSSL_VERSION
62+
popd
63+
64+
# Allow the `pkg-config` crate to cross-compile
65+
export PKG_CONFIG_ALLOW_CROSS=1
66+
# Make the `pkg-config` crate use our wrapper
67+
export PKG_CONFIG=$(pwd)/tests/pkg-config
68+
69+
# Compile OpenSSL and TSS stack for aarch64-unknown-linux-gnu
70+
cross-compile-openssl aarch64-linux-gnu linux-generic64
71+
cross-compile-tpm2-tss aarch64-linux-gnu
72+
73+
rustup target add aarch64-unknown-linux-gnu
74+
cargo build --features generate-bindings --target aarch64-unknown-linux-gnu
75+
cargo build --target aarch64-unknown-linux-gnu
76+
77+
# Compile OpenSSL and TSS stack for armv7-unknown-linux-gnueabi
78+
cross-compile-openssl arm-linux-gnueabi linux-generic32
79+
cross-compile-tpm2-tss arm-linux-gnueabi
80+
81+
rustup target add armv7-unknown-linux-gnueabi
82+
cargo build --features generate-bindings --target armv7-unknown-linux-gnueabi
83+
cargo build --target armv7-unknown-linux-gnueabi

tss-esapi/tests/pkg-config

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
export PKG_CONFIG_PATH=
4+
export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig:$(SYSROOT)/usr/local/lib/pkgconfig
5+
export PKG_CONFIG_SYSROOT_DIR=${SYSROOT}
6+
7+
exec pkg-config "$@"

0 commit comments

Comments
 (0)