Skip to content

Commit

Permalink
wip: ffi for prepare_message_1
Browse files Browse the repository at this point in the history
  • Loading branch information
geonnave committed Jan 17, 2024
1 parent ea8ecb8 commit 80dcc1f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
40 changes: 16 additions & 24 deletions build_for_c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,24 @@ set -e
# for example, running tests required a panic handler and an "eh_personality" function, but then
# this would clash when building the static library or the no_std example.

cargo_features=$1
# cargo_features=$1

if [[ $cargo_features != "crypto-cryptocell310" && $cargo_features != "crypto-psa-baremetal" ]]; then
echo "Select one of: crypto-cryptocell310, crypto-psa-baremetal"
echo "Example: ./build_static_lib.sh crypto-cryptocell310"
exit 1
fi

original_value=`grep crate-type lib/Cargo.toml`

new_value='crate-type = ["staticlib"]'
echo "Changing crate-type to: $new_value"
sed -i -E "s/crate-type.*/$new_value/" lib/Cargo.toml
# if [[ $cargo_features != "crypto-cryptocell310" && $cargo_features != "crypto-psa-baremetal" ]]; then
# echo "Select one of: crypto-cryptocell310, crypto-psa-baremetal"
# echo "Example: ./build_static_lib.sh crypto-cryptocell310"
# exit 1
# fi

# generate the static library
cargo build --target thumbv7em-none-eabihf --package edhoc-rs --package lakers-crypto --package lakers-ead --features="$cargo_features" --release
# cargo build --target thumbv7em-none-eabihf --package edhoc-rs --package lakers-crypto --package lakers-ead --features="$cargo_features" --release
cargo build --target=thumbv7em-none-eabihf -p lakers-ffi --no-default-features --features="crypto-cryptocell310" --release

# generate the headers
cbindgen --config consts/cbindgen.toml --crate lakers-shared --output ./target/include/lakers_shared.h -v
cbindgen --config lib/cbindgen.toml --crate edhoc-rs --output ./target/include/edhoc_rs.h -v

# zip to a single file
cd target
zip -r staticlib-"$cargo_features"-thumbv7em-none-eabihf.zip include/
zip -u -j staticlib-"$cargo_features"-thumbv7em-none-eabihf.zip thumbv7em-none-eabihf/release/libedhoc_rs.a
cd -

echo "Reverting crate-type to original value: $original_value"
sed -i -E "s/crate-type.*/$original_value/" lib/Cargo.toml
cbindgen --config shared/cbindgen.toml --crate lakers-shared --output target/include/lakers_shared.h -v
cbindgen --config lakers-ffi/cbindgen.toml --crate lakers-ffi --output target/include/lakers_ffi.h -v

# # zip to a single file
# cd target
# zip -r staticlib-"$cargo_features"-thumbv7em-none-eabihf.zip include/
# zip -u -j staticlib-"$cargo_features"-thumbv7em-none-eabihf.zip thumbv7em-none-eabihf/release/libedhoc_rs.a
# cd -
5 changes: 5 additions & 0 deletions examples/lakers-ffi-riot/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ int main(void)
od_hex_dump(out_private_key, 32, OD_WIDTH_DEFAULT);
od_hex_dump(out_public_key, 32, OD_WIDTH_DEFAULT);

EdhocMessageBuffer message_1;
int res = initiator_prepare_message_1(NULL, &message_1);
if (res != 0) printf("Error prep msg1: %d\n", res);

puts("All went good.");
return 0;
}
2 changes: 1 addition & 1 deletion lakers-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ header = """
* WARNING: This file is automatically generated by cbindgen. Manual edits are likely to be lost.
* ================================================================================================
*/"""
# includes = ["edhoc_consts.h"]
includes = ["lakers_shared.h"]

[parse.expand]
all_features = false
49 changes: 49 additions & 0 deletions lakers-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,52 @@ pub extern "C" fn p256_generate_key_pair_from_c(out_private_key: *mut u8, out_pu
core::ptr::copy_nonoverlapping(public_key.as_ptr(), out_public_key, P256_ELEM_LEN);
}
}

#[repr(C)]
pub struct EdhocInitiatorWaitM2C {
state: WaitM2, // opaque state
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct EADItemC {
pub label: u8,
pub is_critical: bool,
pub value: *mut EdhocMessageBuffer,
}

#[no_mangle]
pub unsafe extern "C" fn initiator_prepare_message_1(
// input params
c_i: *mut u8,
// ead_1_c: *mut EADItemC,
// output params
// initiator_c: *mut EdhocInitiatorWaitM2C,
message_1: *mut EdhocMessageBuffer,
) -> i8 {
let c_i = if c_i.is_null() {
Some(generate_connection_identifier_cbor(&mut default_crypto()))
} else {
Some(*c_i)
};

// let ead_1_c = if ead_1_c.is_null() {
// None
// } else {
// let ead_1 = (*ead_1_c).to_rust();
// Some(*ead_1)
// };

let initiator = EdhocInitiator::new(default_crypto());

let result = match initiator.prepare_message_1(c_i, &None) {
Ok((init, msg_1)) => {
*message_1 = msg_1;
// *initiator_c = initiator.to_c();
0
}
Err(err) => err as i8,
};

result
}
18 changes: 18 additions & 0 deletions shared/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# call with:
# cbindgen --config shared/cbindgen.toml --crate lakers-shared --output target/include/lakers_shared.h -vv

language = "C"
header = """
/*
* ================================================================================================
* WARNING: This file is automatically generated by cbindgen. Manual edits are likely to be lost.
* ================================================================================================
*/"""

[export]
include = ["EdhocMessageBuffer"]

[parse.expand]
crates = ["lakers-shared"]
# default_features = false
# features = ["rust"]

0 comments on commit 80dcc1f

Please sign in to comment.