diff --git a/consts/cbindgen.toml b/consts/cbindgen.toml deleted file mode 100644 index 639177d6..00000000 --- a/consts/cbindgen.toml +++ /dev/null @@ -1,18 +0,0 @@ -# call with: -# cbindgen --config consts/cbindgen.toml --crate edhoc-consts --output include/edhoc_consts.h -vv - -language = "C" -header = """ -/* - * ================================================================================================ - * WARNING: This file is automatically generated by cbindgen. Manual edits are likely to be lost. - * ================================================================================================ - */""" - -[export] -include = ["State", "EdhocMessageBuffer"] - -[parse.expand] -crates = ["edhoc-consts"] -default_features = false -features = ["rust"] diff --git a/examples/c-wrapper-riot/.gitignore b/examples/c-wrapper-riot/.gitignore deleted file mode 100644 index ba077a40..00000000 --- a/examples/c-wrapper-riot/.gitignore +++ /dev/null @@ -1 +0,0 @@ -bin diff --git a/examples/c-wrapper-riot/Makefile b/examples/c-wrapper-riot/Makefile deleted file mode 100644 index e4f6a241..00000000 --- a/examples/c-wrapper-riot/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -# name of your application -APPLICATION = c-wrapper-riot - -# If no BOARD is found in the environment, use this default: -BOARD ?= nrf52840dk - -INCLUDES += -I$(CURDIR)/../../target/include -ARCHIVES += $(CURDIR)/../../target/thumbv7em-none-eabihf/release/libedhoc_rs.a - -ifeq ($(EDHOC_CRYPTO), CRYPTO_PSA) - CFLAGS += -D$(EDHOC_CRYPTO) -else - CFLAGS += -DCRYPTO_CRYPTOCELL310 -endif - -# This is actually only needed in the CRYPTO_CRYPTOCELL310 configuration -CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=16384 -DISR_STACKSIZE=16384 - -USEMODULE += od -USEMODULE += ps - -# This has to be the absolute path to the RIOT base directory: -RIOTBASE ?= $(CURDIR)/../../../RIOT - -DEVELHELP ?= 1 - -include $(RIOTBASE)/Makefile.include diff --git a/examples/c-wrapper-riot/README.md b/examples/c-wrapper-riot/README.md deleted file mode 100644 index da5bf9c5..00000000 --- a/examples/c-wrapper-riot/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# Using the C Wrapper with RIOT - -This example enables using `edhoc-rs` alongside the [RIOT operating system](https://github.com/RIOT-OS/RIOT). -See [Requirements](#requirements) below. - -# Build and run - -First, go to the top level directory and generate the headers and static library: - -```bash -./build_for_c.sh crypto-cryptocell310 # or crypto-psa-baremetal -``` - -Then, compile and flash to the board (default is nRF52840), as shown below. - -With `crypto-cryptocell310`: - -```bash -make flash term -``` - -With `crypto-psa-baremetal`: - -```bash -make flash term EDHOC_CRYPTO=CRYPTO_PSA -``` - -# Requirements - -[RIOT](https://github.com/RIOT-OS/RIOT)'s source code must be available in a local folder, which the `Makefile` assumes to be `../../../RIOT`. - -If you have RIOT available in another directory, set the `RIOTBASE` variable when invoking the `make` commands. \ No newline at end of file diff --git a/examples/c-wrapper-riot/main.c b/examples/c-wrapper-riot/main.c deleted file mode 100644 index e21bb219..00000000 --- a/examples/c-wrapper-riot/main.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include "od.h" -#include "edhoc_rs.h" - -#ifdef CRYPTO_PSA -extern void mbedtls_memory_buffer_alloc_init(uint8_t *buf, size_t len); -#endif - -static const uint8_t CRED_I[] = {0xA2, 0x02, 0x77, 0x34, 0x32, 0x2D, 0x35, 0x30, 0x2D, 0x33, 0x31, 0x2D, 0x46, 0x46, 0x2D, 0x45, 0x46, 0x2D, 0x33, 0x37, 0x2D, 0x33, 0x32, 0x2D, 0x33, 0x39, 0x08, 0xA1, 0x01, 0xA5, 0x01, 0x02, 0x02, 0x41, 0x2B, 0x20, 0x01, 0x21, 0x58, 0x20, 0xAC, 0x75, 0xE9, 0xEC, 0xE3, 0xE5, 0x0B, 0xFC, 0x8E, 0xD6, 0x03, 0x99, 0x88, 0x95, 0x22, 0x40, 0x5C, 0x47, 0xBF, 0x16, 0xDF, 0x96, 0x66, 0x0A, 0x41, 0x29, 0x8C, 0xB4, 0x30, 0x7F, 0x7E, 0xB6, 0x22, 0x58, 0x20, 0x6E, 0x5D, 0xE6, 0x11, 0x38, 0x8A, 0x4B, 0x8A, 0x82, 0x11, 0x33, 0x4A, 0xC7, 0xD3, 0x7E, 0xCB, 0x52, 0xA3, 0x87, 0xD2, 0x57, 0xE6, 0xDB, 0x3C, 0x2A, 0x93, 0xDF, 0x21, 0xFF, 0x3A, 0xFF, 0xC8}; -static const uint8_t CRED_R[] = {0xA2, 0x02, 0x60, 0x08, 0xA1, 0x01, 0xA5, 0x01, 0x02, 0x02, 0x41, 0x0A, 0x20, 0x01, 0x21, 0x58, 0x20, 0xBB, 0xC3, 0x49, 0x60, 0x52, 0x6E, 0xA4, 0xD3, 0x2E, 0x94, 0x0C, 0xAD, 0x2A, 0x23, 0x41, 0x48, 0xDD, 0xC2, 0x17, 0x91, 0xA1, 0x2A, 0xFB, 0xCB, 0xAC, 0x93, 0x62, 0x20, 0x46, 0xDD, 0x44, 0xF0, 0x22, 0x58, 0x20, 0x45, 0x19, 0xE2, 0x57, 0x23, 0x6B, 0x2A, 0x0C, 0xE2, 0x02, 0x3F, 0x09, 0x31, 0xF1, 0xF3, 0x86, 0xCA, 0x7A, 0xFD, 0xA6, 0x4F, 0xCD, 0xE0, 0x10, 0x8C, 0x22, 0x4C, 0x51, 0xEA, 0xBF, 0x60, 0x72}; -static const uint8_t R[] = {0x72, 0xcc, 0x47, 0x61, 0xdb, 0xd4, 0xc7, 0x8f, 0x75, 0x89, 0x31, 0xaa, 0x58, 0x9d, 0x34, 0x8d, 0x1e, 0xf8, 0x74, 0xa7, 0xe3, 0x03, 0xed, 0xe2, 0xf1, 0x40, 0xdc, 0xf3, 0xe6, 0xaa, 0x4a, 0xac}; -static const uint8_t I[] = {0xfb, 0x13, 0xad, 0xeb, 0x65, 0x18, 0xce, 0xe5, 0xf8, 0x84, 0x17, 0x66, 0x08, 0x41, 0x14, 0x2e, 0x83, 0x0a, 0x81, 0xfe, 0x33, 0x43, 0x80, 0xa9, 0x53, 0x40, 0x6a, 0x13, 0x05, 0xe8, 0x70, 0x6b}; - -int main(void) - { - puts("Calling edhoc-rs from C!"); - -#ifdef CRYPTO_PSA - // Memory buffer for mbedtls - uint8_t buffer[4096 * 2] = {0}; - mbedtls_memory_buffer_alloc_init(buffer, 4096 * 2); -#endif - - puts("Begin test: generate key pair."); - uint8_t out_private_key[32] = {0}; - uint8_t out_public_key[32] = {0}; - p256_generate_key_pair_from_c(out_private_key, out_public_key); - puts("End test: generate key pair."); - od_hex_dump(out_private_key, 32, OD_WIDTH_DEFAULT); - od_hex_dump(out_public_key, 32, OD_WIDTH_DEFAULT); - - puts("Begin test: edhoc handshake."); - EdhocInitiatorC initiator = initiator_new(I, 32, CRED_I, 107, CRED_R, 84); - EdhocResponderC responder = responder_new(R, 32, CRED_R, 84, CRED_I, 107); - - EdhocMessageBuffer message_1; - initiator_prepare_message_1(&initiator, &message_1); - responder_process_message_1(&responder, &message_1); - EdhocMessageBuffer message_2; - uint8_t c_r_sent; - responder_prepare_message_2(&responder, &message_2, &c_r_sent); - uint8_t c_r_received; - initiator_process_message_2(&initiator, &message_2, &c_r_received); - EdhocMessageBuffer message_3; - uint8_t prk_out_initiator[SHA256_DIGEST_LEN]; - initiator_prepare_message_3(&initiator, &message_3, &prk_out_initiator); - uint8_t prk_out_responder[SHA256_DIGEST_LEN]; - responder_process_message_3(&responder, &message_3, &prk_out_responder); - - printf("\nprk_out_initiator: \n"); - od_hex_dump(prk_out_initiator, SHA256_DIGEST_LEN, OD_WIDTH_DEFAULT); - printf("\nprk_out_responder: \n"); - od_hex_dump(prk_out_responder, SHA256_DIGEST_LEN, OD_WIDTH_DEFAULT); - - // Compare prk_out_initiator and prk_out_responder - if (memcmp(prk_out_initiator, prk_out_responder, SHA256_DIGEST_LEN) != 0) { - printf("Error: prk_out_initiator and prk_out_responder do not match.\n"); - return 1; - } - - puts("End test: edhoc handshake."); - - return 0; -} diff --git a/lib/cbindgen.toml b/lib/cbindgen.toml deleted file mode 100644 index 38f579ac..00000000 --- a/lib/cbindgen.toml +++ /dev/null @@ -1,14 +0,0 @@ -# call with: -# cbindgen --config lib/cbindgen.toml --crate edhoc-rs --output include/edhoc_rs.h -vv - -language = "C" -header = """ -/* - * ================================================================================================ - * WARNING: This file is automatically generated by cbindgen. Manual edits are likely to be lost. - * ================================================================================================ - */""" -includes = ["edhoc_consts.h"] - -[parse.expand] -all_features = false diff --git a/lib/src/c_wrapper.rs b/lib/src/c_wrapper.rs deleted file mode 100644 index 4214c79e..00000000 --- a/lib/src/c_wrapper.rs +++ /dev/null @@ -1,255 +0,0 @@ -use crate::*; -use core::{slice, str}; -use edhoc_consts::*; -use hexlit::hex; - -use edhoc_crypto::{default_crypto, CryptoTrait}; - -// Panic handler for cortex-m targets -#[cfg(any(feature = "crypto-cryptocell310", feature = "crypto-psa-baremetal"))] -use panic_semihosting as _; - -// This function is mainly used to test the C wrapper -#[no_mangle] -pub extern "C" fn p256_generate_key_pair_from_c(out_private_key: *mut u8, out_public_key: *mut u8) { - let (private_key, public_key) = default_crypto().p256_generate_key_pair(); - - unsafe { - // copy the arrays to the pointers received from C - // this makes sure that data is not dropped when the function returns - core::ptr::copy_nonoverlapping(private_key.as_ptr(), out_private_key, P256_ELEM_LEN); - core::ptr::copy_nonoverlapping(public_key.as_ptr(), out_public_key, P256_ELEM_LEN); - } -} - -#[repr(C)] -pub struct EdhocInitiatorC { - pub state: State, - pub i: *const u8, - pub i_len: usize, - pub cred_i: *const u8, - pub cred_i_len: usize, - pub cred_r: *const u8, - pub cred_r_len: usize, -} - -impl EdhocInitiatorC { - pub fn to_rust(&self) -> EdhocInitiator { - EdhocInitiator::new( - self.state, - unsafe { slice::from_raw_parts(self.i, self.i_len) }, - unsafe { slice::from_raw_parts(self.cred_i, self.cred_i_len) }, - unsafe { Some(slice::from_raw_parts(self.cred_r, self.cred_r_len)) }, - ) - } -} - -#[repr(C)] -pub struct EdhocResponderC { - pub state: State, - pub r: *const u8, - pub r_len: usize, - pub cred_i: *const u8, - pub cred_i_len: usize, - pub cred_r: *const u8, - pub cred_r_len: usize, -} - -impl EdhocResponderC { - pub fn to_rust(&self) -> EdhocResponder { - EdhocResponder::new( - self.state, - unsafe { slice::from_raw_parts(self.r, self.r_len) }, - unsafe { slice::from_raw_parts(self.cred_r, self.cred_r_len) }, - unsafe { Some(slice::from_raw_parts(self.cred_i, self.cred_i_len)) }, - ) - } -} - -#[no_mangle] -pub unsafe extern "C" fn responder_new( - r: *const u8, - r_len: usize, - cred_r: *const u8, - cred_r_len: usize, - cred_i: *const u8, - cred_i_len: usize, -) -> EdhocResponderC { - EdhocResponder::new( - State::default(), - slice::from_raw_parts(r, r_len), - slice::from_raw_parts(cred_r, cred_r_len), - Some(slice::from_raw_parts(cred_i, cred_i_len)), - ) - .to_c() -} - -#[no_mangle] -pub unsafe extern "C" fn initiator_new( - i: *const u8, - i_len: usize, - cred_i: *const u8, - cred_i_len: usize, - cred_r: *const u8, - cred_r_len: usize, -) -> EdhocInitiatorC { - EdhocInitiator::new( - State::default(), - slice::from_raw_parts(i, i_len), - slice::from_raw_parts(cred_i, cred_i_len), - Some(slice::from_raw_parts(cred_r, cred_r_len)), - ) - .to_c() -} - -#[no_mangle] -pub unsafe extern "C" fn initiator_prepare_message_1( - initiator_c: *mut EdhocInitiatorC, - message_1: *mut EdhocMessageBuffer, -) -> i8 { - let mut initiator = (*initiator_c).to_rust(); - - let c_i: u8 = generate_connection_identifier_cbor().into(); - let result = match initiator.prepare_message_1(c_i) { - Ok(msg_1) => { - *message_1 = msg_1; - 0 - } - Err(err) => err as i8, - }; - - *initiator_c = initiator.to_c(); - - result -} - -#[no_mangle] -pub unsafe extern "C" fn responder_process_message_1( - responder_c: *mut EdhocResponderC, - message_1: *const EdhocMessageBuffer, -) -> i8 { - let mut responder = (*responder_c).to_rust(); - - let result = match responder.process_message_1(&*message_1) { - Ok(_) => 0, - Err(err) => err as i8, - }; - - *responder_c = responder.to_c(); - - result -} - -#[no_mangle] -pub unsafe extern "C" fn responder_prepare_message_2( - responder_c: *mut EdhocResponderC, - message_2: *mut EdhocMessageBuffer, - c_r: *mut u8, -) -> i8 { - let mut responder = (*responder_c).to_rust(); - - let c_r_chosen: u8 = generate_connection_identifier_cbor().into(); - let result = match responder.prepare_message_2(c_r_chosen) { - Ok(msg_2) => { - *message_2 = msg_2; - *c_r = c_r_chosen; - 0 - } - Err(err) => err as i8, - }; - - *responder_c = responder.to_c(); - - result -} - -#[no_mangle] -pub unsafe extern "C" fn initiator_process_message_2( - initiator_c: *mut EdhocInitiatorC, - message_2: *const EdhocMessageBuffer, - c_r: *mut u8, -) -> i8 { - let mut initiator = (*initiator_c).to_rust(); - - let result = match initiator.process_message_2(&*message_2) { - Ok(c_r_res) => { - *c_r = c_r_res; - 0 - } - Err(err) => err as i8, - }; - - *initiator_c = initiator.to_c(); - - result -} - -#[no_mangle] -pub unsafe extern "C" fn initiator_prepare_message_3( - initiator_c: *mut EdhocInitiatorC, - message_3: *mut EdhocMessageBuffer, - prk_out: *mut [u8; SHA256_DIGEST_LEN], -) -> i8 { - let mut initiator = (*initiator_c).to_rust(); - - let result = match initiator.prepare_message_3() { - Ok((msg_3, prk_out_res)) => { - *message_3 = msg_3; - *prk_out = prk_out_res; - 0 - } - Err(err) => err as i8, - }; - - *initiator_c = initiator.to_c(); - - result -} - -#[no_mangle] -pub unsafe extern "C" fn responder_process_message_3( - responder_c: *mut EdhocResponderC, - message_3: *const EdhocMessageBuffer, - prk_out: *mut [u8; SHA256_DIGEST_LEN], -) -> i8 { - let mut responder = (*responder_c).to_rust(); - - let result = match responder.process_message_3(&*message_3) { - Ok(prk_out_res) => { - *prk_out = prk_out_res; - 0 - } - Err(err) => err as i8, - }; - - *responder_c = responder.to_c(); - - result -} - -#[cfg(test)] -mod test_c { - use super::*; - - #[test] - fn test_new_responder() { - const ID_CRED_I: &[u8] = &hex!("a104412b"); - const ID_CRED_R: &[u8] = &hex!("a104410a"); - const CRED_I: &[u8] = &hex!("A2027734322D35302D33312D46462D45462D33372D33322D333908A101A5010202412B2001215820AC75E9ECE3E50BFC8ED60399889522405C47BF16DF96660A41298CB4307F7EB62258206E5DE611388A4B8A8211334AC7D37ECB52A387D257E6DB3C2A93DF21FF3AFFC8"); - const G_I: &[u8] = - &hex!("ac75e9ece3e50bfc8ed60399889522405c47bf16df96660a41298cb4307f7eb6"); - const CRED_R: &[u8] = &hex!("A2026008A101A5010202410A2001215820BBC34960526EA4D32E940CAD2A234148DDC21791A12AFBCBAC93622046DD44F02258204519E257236B2A0CE2023F0931F1F386CA7AFDA64FCDE0108C224C51EABF6072"); - const R: &[u8] = &hex!("72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3e6aa4aac"); - - let resp = unsafe { - responder_new( - R.as_ptr(), - R.len(), - CRED_I.as_ptr(), - CRED_I.len(), - CRED_R.as_ptr(), - CRED_R.len(), - ) - }; - } -} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index bc8d9037..ed8d2e46 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -13,9 +13,6 @@ pub use edhoc_ead::*; mod edhoc; use edhoc::*; -mod c_wrapper; -use c_wrapper::*; - use edhoc_consts::*; #[derive(Default, Copy, Clone, Debug)] @@ -51,19 +48,6 @@ impl<'a> EdhocResponderState<'a> { } } - pub fn to_c(&self) -> EdhocResponderC { - let cred_i = self.cred_i.unwrap_or_default(); - EdhocResponderC { - state: self.state, - r: self.r.as_ptr(), - r_len: self.r.len(), - cred_r: self.cred_r.as_ptr(), - cred_r_len: self.cred_r.len(), - cred_i: cred_i.as_ptr(), - cred_i_len: cred_i.len(), - } - } - pub fn process_message_1( self: &mut EdhocResponderState<'a>, message_1: &BufferMessage1, @@ -179,19 +163,6 @@ impl<'a> EdhocInitiatorState<'a> { } } - pub fn to_c(&self) -> EdhocInitiatorC { - let cred_r = self.cred_r.unwrap_or_default(); - EdhocInitiatorC { - state: self.state, - i: self.i.as_ptr(), - i_len: self.i.len(), - cred_i: self.cred_i.as_ptr(), - cred_i_len: self.cred_i.len(), - cred_r: cred_r.as_ptr(), - cred_r_len: cred_r.len(), - } - } - pub fn prepare_message_1( self: &mut EdhocInitiatorState<'a>, c_i: u8,