Skip to content

Commit

Permalink
libspdm: Support spdm_context in the HAL functions
Browse files Browse the repository at this point in the history
libspdm PR DMTF/libspdm#2592 adds the
LIBSPDM_HAL_PASS_SPDM_CONTEXT macro which allows passing the
spdm_context to the HAL functions.

According to [1] this will eventually be the default, so let's update
now to be ready and then we can use the context in the HAL functions.

This updates the build instructions and CI to expose the spdm_context to
our `extern "C"` HAL functions.

1: https://github.com/bluerise/libspdm/blob/7dd35de89943090dfcabe39192af31fa26259a36/include/library/spdm_lib_config.h#L404

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
  • Loading branch information
alistair23 committed Jul 24, 2024
1 parent bf22885 commit 4d57ab3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 78 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
-DCMAKE_C_FLAGS=" \
-DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 \
-DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 \
-DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1 \
" \
.. ; \
make -j8; \
Expand Down Expand Up @@ -104,6 +105,7 @@ jobs:
-DMBEDTLS_SKIP_TIME_CHECK \
-DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 \
-DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 \
-DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1 \
-DMBEDTLS_PLATFORM_MS_TIME_ALT \
" \
.. ; \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ To build libspdm in the third-party directory
```shell
cd libspdm/
mkdir build; cd build
cmake -DARCH=x64 -DTOOLCHAIN=GCC -DTARGET=Debug -DCRYPTO=openssl -DENABLE_BINARY_BUILD=1 -DCOMPILED_LIBCRYPTO_PATH=/usr/lib/ -DCOMPILED_LIBSSL_PATH=/usr/lib/ -DDISABLE_TESTS=1 -DCMAKE_C_FLAGS="-DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 -DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0" ..
cmake -DARCH=x64 -DTOOLCHAIN=GCC -DTARGET=Debug -DCRYPTO=openssl -DENABLE_BINARY_BUILD=1 -DCOMPILED_LIBCRYPTO_PATH=/usr/lib/ -DCOMPILED_LIBSSL_PATH=/usr/lib/ -DDISABLE_TESTS=1 -DCMAKE_C_FLAGS="-DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 -DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 -DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1" ..
make -j8
```

Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ fn main() {

bindgen::Builder::default()
.clang_arg("-DLIBSPDM_TESTS")
.clang_arg("-DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1")
.header("wrapper.h")
.clang_arg("-Ithird-party/libspdm/include")
.clang_arg("-Ithird-party/libspdm")
.clang_arg("-Ithird-party/SPDM-Responder-Validator/include")
.clang_arg("-Ithird-party/SPDM-Responder-Validator/common_test_framework/include/")
} else {
bindgen::Builder::default()
.clang_arg("-DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1")
.header("wrapper.h")
.clang_arg("-Ithird-party/libspdm/include")
.clang_arg("-Ithird-party/libspdm")
Expand Down
170 changes: 95 additions & 75 deletions src/libspdm/spdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ pub unsafe fn start_session(

#[no_mangle]
pub unsafe extern "C" fn libspdm_requester_data_sign(
_spdm_context: *mut c_void,
_spdm_version: libspdm_rs::spdm_version_number_t,
_op_code: u8,
_req_base_asym_alg: u16,
Expand All @@ -1046,6 +1047,7 @@ pub unsafe extern "C" fn libspdm_requester_data_sign(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
*mut c_void,
libspdm_rs::spdm_version_number_t,
u8,
u16,
Expand Down Expand Up @@ -1085,6 +1087,7 @@ libspdm_match_fn_prototypes!(
/// True signing success, false otherwise
#[no_mangle]
pub unsafe extern "C" fn libspdm_responder_data_sign(
_spdm_context: *mut c_void,
spdm_version: libspdm_rs::spdm_version_number_t,
op_code: u8,
base_asym_algo: u32,
Expand Down Expand Up @@ -1172,15 +1175,16 @@ pub unsafe extern "C" fn libspdm_responder_data_sign(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
spdm_version: libspdm_rs::spdm_version_number_t,
op_code: u8,
base_asym_algo: u32,
base_hash_algo: u32,
is_data_hash: bool,
message: *const u8,
message_size: usize,
signature: *mut u8,
sig_size: *mut usize,
*mut c_void,
libspdm_rs::spdm_version_number_t,
u8,
u32,
u32,
bool,
*const u8,
usize,
*mut u8,
*mut usize,
) -> bool,
libspdm_responder_data_sign
);
Expand All @@ -1204,6 +1208,7 @@ libspdm_match_fn_prototypes!(
/// True if certificate saved to NV successfully, otherwise, false
#[no_mangle]
pub unsafe extern "C" fn libspdm_write_certificate_to_nvm(
_spdm_context: *mut c_void,
slot_id: u8,

Check warning on line 1212 in src/libspdm/spdm.rs

View workflow job for this annotation

GitHub Actions / ci-check

unused variable: `slot_id`
cert_chain: *const c_void,

Check warning on line 1213 in src/libspdm/spdm.rs

View workflow job for this annotation

GitHub Actions / ci-check

unused variable: `cert_chain`
cert_chain_size: usize,

Check warning on line 1214 in src/libspdm/spdm.rs

View workflow job for this annotation

GitHub Actions / ci-check

unused variable: `cert_chain_size`
Expand Down Expand Up @@ -1242,10 +1247,9 @@ pub unsafe extern "C" fn libspdm_write_certificate_to_nvm(
true
}
}

#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(u8, *const c_void, usize, u32, u32) -> bool,
unsafe extern "C" fn(*mut c_void, u8, *const c_void, usize, u32, u32) -> bool,
libspdm_write_certificate_to_nvm
);

Expand Down Expand Up @@ -1274,6 +1278,7 @@ libspdm_match_fn_prototypes!(
/// True if CSR generation was a success, false otherwise
#[no_mangle]
pub unsafe extern "C" fn libspdm_gen_csr(
_spdm_context: *mut c_void,
base_hash_algo: u32,
base_asym_algo: u32,
need_reset: *mut bool,
Expand Down Expand Up @@ -1413,18 +1418,19 @@ pub unsafe extern "C" fn libspdm_gen_csr(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
base_hash_algo: u32,
base_asym_algo: u32,
need_reset: *mut bool,
_request: *const c_void,
_request_size: usize,
requester_info: *mut u8,
requester_info_length: usize,
_opaque_data: *mut u8,
_opaque_data_length: u16,
csr_len: *mut usize,
csr_pointer: *mut u8,
is_device_cert_model: bool,
*mut c_void,
u32,
u32,
*mut bool,
*const c_void,
usize,
*mut u8,
usize,
*mut u8,
u16,
*mut usize,
*mut u8,
bool,
) -> bool,
libspdm_gen_csr
);
Expand Down Expand Up @@ -1455,6 +1461,7 @@ libspdm_match_fn_prototypes!(
/// False if the measurement summary has is not generated/generation failure
#[no_mangle]
pub unsafe extern "C" fn libspdm_generate_measurement_summary_hash(
spdm_context: *mut c_void,
spdm_version: libspdm_rs::spdm_version_number_t,
base_hash_algo: u32,
measurement_specification: u8,
Expand All @@ -1480,6 +1487,7 @@ pub unsafe extern "C" fn libspdm_generate_measurement_summary_hash(
let mut device_measurement_count = 0;

let ret = libspdm_measurement_collection(
spdm_context,
spdm_version,
measurement_specification,
measurement_hash_algo,
Expand Down Expand Up @@ -1575,13 +1583,14 @@ pub unsafe extern "C" fn libspdm_generate_measurement_summary_hash(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
spdm_version: libspdm_rs::spdm_version_number_t,
base_hash_algo: u32,
measurement_specification: u8,
measurement_hash_algo: u32,
measurement_summary_hash_type: u8,
measurement_summary_hash: *mut u8,
measurement_summary_hash_size: u32,
*mut c_void,
libspdm_rs::spdm_version_number_t,
u32,
u8,
u32,
u8,
*mut u8,
u32,
) -> bool,
libspdm_generate_measurement_summary_hash
);
Expand Down Expand Up @@ -1900,6 +1909,7 @@ unsafe fn libspdm_fill_measurement_device_mode_block(
/// RETURN_***: Any other RETURN_ error indicating the type of failure.
#[no_mangle]
pub unsafe extern "C" fn libspdm_measurement_collection(
_spdm_context: *mut c_void,
spdm_version: libspdm_rs::spdm_version_number_t,
measurement_specification: u8,
measurement_hash_algo: u32,
Expand Down Expand Up @@ -2084,15 +2094,16 @@ pub unsafe extern "C" fn libspdm_measurement_collection(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
spdm_version: libspdm_rs::spdm_version_number_t,
measurement_specification: u8,
measurement_hash_algo: u32,
measurements_index: u8,
request_attribute: u8,
content_changed: *mut u8,
measurements_count: *mut u8,
measurements: *mut c_void,
measurements_size: *mut usize,
*mut c_void,
libspdm_rs::spdm_version_number_t,
u8,
u32,
u8,
u8,
*mut u8,
*mut u8,
*mut c_void,
*mut usize,
) -> u32,
libspdm_measurement_collection
);
Expand Down Expand Up @@ -2353,6 +2364,7 @@ libspdm_match_fn_prototypes!(
/// Format the certificate specified by `path` into what `libspdm` expects
#[no_mangle]
pub unsafe extern "C" fn libspdm_measurement_opaque_data(
_spdm_context: *mut c_void,
_spdm_version: libspdm_rs::spdm_version_number_t,
_measurement_specification: u8,
_measurement_hash_algo: u32,
Expand All @@ -2376,19 +2388,21 @@ pub unsafe extern "C" fn libspdm_measurement_opaque_data(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
_spdm_version: libspdm_rs::spdm_version_number_t,
_measurement_specification: u8,
_measurement_hash_algo: u32,
_measurement_index: u8,
_request_attribute: u8,
opaque_data: *mut c_void,
opaque_data_size: *mut usize,
*mut c_void,
libspdm_rs::spdm_version_number_t,
u8,
u32,
u8,
u8,
*mut c_void,
*mut usize,
) -> bool,
libspdm_measurement_opaque_data
);

#[no_mangle]
pub unsafe extern "C" fn libspdm_challenge_opaque_data(
_spdm_context: *mut c_void,
_spdm_version: libspdm_rs::spdm_version_number_t,
_slot_id: u8,
_measurement_summary_hash: *mut u8,
Expand All @@ -2411,18 +2425,20 @@ pub unsafe extern "C" fn libspdm_challenge_opaque_data(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
_spdm_version: libspdm_rs::spdm_version_number_t,
_slot_id: u8,
_measurement_summary_hash: *mut u8,
_measurement_summary_hash_size: usize,
opaque_data: *mut c_void,
opaque_data_size: *mut usize,
*mut c_void,
libspdm_rs::spdm_version_number_t,
u8,
*mut u8,
usize,
*mut c_void,
*mut usize,
) -> bool,
libspdm_challenge_opaque_data
);

#[no_mangle]
pub unsafe extern "C" fn libspdm_encap_challenge_opaque_data(
_spdm_context: *mut c_void,
_spdm_version: libspdm_rs::spdm_version_number_t,
_slot_id: u8,
_measurement_summary_hash: *mut u8,
Expand All @@ -2445,12 +2461,13 @@ pub unsafe extern "C" fn libspdm_encap_challenge_opaque_data(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
_spdm_version: libspdm_rs::spdm_version_number_t,
_slot_id: u8,
_measurement_summary_hash: *mut u8,
_measurement_summary_hash_size: usize,
opaque_data: *mut c_void,
opaque_data_size: *mut usize,
*mut c_void,
libspdm_rs::spdm_version_number_t,
u8,
*mut u8,
usize,
*mut c_void,
*mut usize,
) -> bool,
libspdm_encap_challenge_opaque_data
);
Expand Down Expand Up @@ -2495,6 +2512,7 @@ libspdm_match_fn_prototypes!(
/// True if CSR generation was a success, false otherwise
#[no_mangle]
pub unsafe extern "C" fn libspdm_gen_csr_ex(
spdm_context: *mut c_void,
base_hash_algo: c_uint,
base_asym_algo: c_uint,
need_reset: *mut bool,
Expand Down Expand Up @@ -2528,6 +2546,7 @@ pub unsafe extern "C" fn libspdm_gen_csr_ex(
}
};
return libspdm_gen_csr(
spdm_context,
base_hash_algo,
base_asym_algo,
need_reset,
Expand All @@ -2546,21 +2565,22 @@ pub unsafe extern "C" fn libspdm_gen_csr_ex(
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn(
base_hash_algo: c_uint,
base_asym_algo: c_uint,
need_reset: *mut bool,
request: *const c_void,
request_size: usize,
requester_info: *mut c_uchar,
requester_info_length: usize,
opaque_data: *mut c_uchar,
opaque_data_length: u16,
csr_len: *mut usize,
csr_pointer: *mut c_uchar,
req_cert_model: c_uchar,
csr_tracking_tag: *mut c_uchar,
_req_key_pair_id: u8,
overwrite: bool,
*mut c_void,
c_uint,
c_uint,
*mut bool,
*const c_void,
usize,
*mut c_uchar,
usize,
*mut c_uchar,
u16,
*mut usize,
*mut c_uchar,
c_uchar,
*mut c_uchar,
u8,
bool,
) -> bool,
libspdm_gen_csr_ex
);
Expand Down Expand Up @@ -2806,12 +2826,12 @@ pub unsafe fn get_negotiated_algos(cntx_ptr: *mut c_void, slot_id: u8) -> Result

/// return if current code is running in a trusted environment.
#[no_mangle]
pub unsafe extern "C" fn libspdm_is_in_trusted_environment() -> bool {
pub unsafe extern "C" fn libspdm_is_in_trusted_environment(_spdm_context: *mut c_void) -> bool {
true
}
#[cfg(test)]
libspdm_match_fn_prototypes!(
unsafe extern "C" fn() -> bool,
unsafe extern "C" fn(*mut c_void) -> bool,
libspdm_is_in_trusted_environment
);

Expand Down
4 changes: 2 additions & 2 deletions tock-responder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cd build_no_std_arm
# an accurate time.
find ../os_stub/mbedtlslib/include/mbedtls/libspdm_mbedtls_config.h -type f -exec sed -i 's|#define MBEDTLS_HAVE_TIME_DATE|// #define MBEDTLS_HAVE_TIME_DATE|g' {} +

cmake -DARCH=arm -DTOOLCHAIN=ARM_GNU_BARE_METAL -DTARGET=Release -DCRYPTO=mbedtls -DDISABLE_TESTS=1 -DMARCH=armv7e-m -DDISABLE_LTO=1 -DCMAKE_C_FLAGS="-DLIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP=1 -DMBEDTLS_SKIP_TIME_CHECK -DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 -DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 -DMBEDTLS_PLATFORM_MS_TIME_ALT" ..
cmake -DARCH=arm -DTOOLCHAIN=ARM_GNU_BARE_METAL -DTARGET=Release -DCRYPTO=mbedtls -DDISABLE_TESTS=1 -DMARCH=armv7e-m -DDISABLE_LTO=1 -DCMAKE_C_FLAGS="-DLIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP=1 -DMBEDTLS_SKIP_TIME_CHECK -DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 -DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 -DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1 -DMBEDTLS_PLATFORM_MS_TIME_ALT" ..
make -j8
cd ../

Expand All @@ -62,7 +62,7 @@ cd build_no_std_riscv
# an accurate time.
find ../os_stub/mbedtlslib/include/mbedtls/libspdm_mbedtls_config.h -type f -exec sed -i 's|#define MBEDTLS_HAVE_TIME_DATE|// #define MBEDTLS_HAVE_TIME_DATE|g' {} +

cmake -DARCH=riscv32 -DTOOLCHAIN=RISCV_NONE -DTARGET=Release -DCRYPTO=mbedtls -DDISABLE_TESTS=1 -DCMAKE_C_FLAGS="-DLIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP=1 -DMBEDTLS_SKIP_TIME_CHECK -DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 -DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 -DMBEDTLS_PLATFORM_MS_TIME_ALT" ..
cmake -DARCH=riscv32 -DTOOLCHAIN=RISCV_NONE -DTARGET=Release -DCRYPTO=mbedtls -DDISABLE_TESTS=1 -DCMAKE_C_FLAGS="-DLIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP=1 -DMBEDTLS_SKIP_TIME_CHECK -DLIBSPDM_ENABLE_CAPABILITY_EVENT_CAP=0 -DLIBSPDM_ENABLE_CAPABILITY_MEL_CAP=0 -DLIBSPDM_HAL_PASS_SPDM_CONTEXT=1 -DMBEDTLS_PLATFORM_MS_TIME_ALT" ..
make -j8
cd ../
```
Expand Down

0 comments on commit 4d57ab3

Please sign in to comment.